triangle_triangle_adjacency.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "triangle_triangle_adjacency.h"
  9. #include "is_edge_manifold.h"
  10. #include "all_edges.h"
  11. #include "unique_simplices.h"
  12. #include "unique_edge_map.h"
  13. #include <algorithm>
  14. #include <iostream>
  15. template <typename Scalar, typename Index>
  16. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(
  17. const Eigen::PlainObjectBase<Scalar>& /*V*/,
  18. const Eigen::PlainObjectBase<Index>& F,
  19. std::vector<std::vector<int> >& TTT)
  20. {
  21. return triangle_triangle_adjacency_preprocess(F,TTT);
  22. }
  23. template <typename Index>
  24. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(
  25. const Eigen::PlainObjectBase<Index>& F,
  26. std::vector<std::vector<int> >& TTT)
  27. {
  28. for(int f=0;f<F.rows();++f)
  29. for (int i=0;i<F.cols();++i)
  30. {
  31. // v1 v2 f ei
  32. int v1 = F(f,i);
  33. int v2 = F(f,(i+1)%F.cols());
  34. if (v1 > v2) std::swap(v1,v2);
  35. std::vector<int> r(4);
  36. r[0] = v1; r[1] = v2;
  37. r[2] = f; r[3] = i;
  38. TTT.push_back(r);
  39. }
  40. std::sort(TTT.begin(),TTT.end());
  41. }
  42. // Extract the face adjacencies
  43. template <typename DerivedF, typename DerivedTT>
  44. IGL_INLINE void igl::triangle_triangle_adjacency_extractTT(
  45. const Eigen::PlainObjectBase<DerivedF>& F,
  46. std::vector<std::vector<int> >& TTT,
  47. Eigen::PlainObjectBase<DerivedTT>& TT)
  48. {
  49. TT.setConstant((int)(F.rows()),F.cols(),-1);
  50. for(int i=1;i<(int)TTT.size();++i)
  51. {
  52. std::vector<int>& r1 = TTT[i-1];
  53. std::vector<int>& r2 = TTT[i];
  54. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  55. {
  56. TT(r1[2],r1[3]) = r2[2];
  57. TT(r2[2],r2[3]) = r1[2];
  58. }
  59. }
  60. }
  61. // Extract the face adjacencies indices (needed for fast traversal)
  62. template <typename DerivedF, typename DerivedTTi>
  63. IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi(
  64. const Eigen::PlainObjectBase<DerivedF>& F,
  65. std::vector<std::vector<int> >& TTT,
  66. Eigen::PlainObjectBase<DerivedTTi>& TTi)
  67. {
  68. TTi.setConstant((int)(F.rows()),F.cols(),-1);
  69. for(int i=1;i<(int)TTT.size();++i)
  70. {
  71. std::vector<int>& r1 = TTT[i-1];
  72. std::vector<int>& r2 = TTT[i];
  73. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  74. {
  75. TTi(r1[2],r1[3]) = r2[3];
  76. TTi(r2[2],r2[3]) = r1[3];
  77. }
  78. }
  79. }
  80. // Compute triangle-triangle adjacency
  81. template <typename Scalar, typename Index>
  82. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  83. const Eigen::PlainObjectBase<Index>& F,
  84. Eigen::PlainObjectBase<Index>& TT)
  85. {
  86. //assert(igl::is_edge_manifold(V,F));
  87. std::vector<std::vector<int> > TTT;
  88. triangle_triangle_adjacency_preprocess(V,F,TTT);
  89. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  90. }
  91. // Compute triangle-triangle adjacency with indices
  92. template <typename Scalar, typename Index>
  93. IGL_INLINE void igl::triangle_triangle_adjacency(
  94. const Eigen::PlainObjectBase<Scalar>& /*V*/,
  95. const Eigen::PlainObjectBase<Index>& F,
  96. Eigen::PlainObjectBase<Index>& TT,
  97. Eigen::PlainObjectBase<Index>& TTi)
  98. {
  99. //assert(igl::is_edge_manifold(V,F));
  100. std::vector<std::vector<int> > TTT;
  101. triangle_triangle_adjacency_preprocess(F,TTT);
  102. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  103. triangle_triangle_adjacency_extractTTi(F,TTT,TTi);
  104. }
  105. // Compute triangle-triangle adjacency with indices
  106. template <typename DerivedF, typename DerivedTT, typename DerivedTTi>
  107. IGL_INLINE void igl::triangle_triangle_adjacency(
  108. const Eigen::PlainObjectBase<DerivedF>& F,
  109. Eigen::PlainObjectBase<DerivedTT>& TT,
  110. Eigen::PlainObjectBase<DerivedTTi>& TTi)
  111. {
  112. //assert(igl::is_edge_manifold(V,F));
  113. std::vector<std::vector<int> > TTT;
  114. triangle_triangle_adjacency_preprocess(F,TTT);
  115. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  116. triangle_triangle_adjacency_extractTTi(F,TTT,TTi);
  117. }
  118. template <
  119. typename DerivedF,
  120. typename TTIndex,
  121. typename TTiIndex>
  122. IGL_INLINE void igl::triangle_triangle_adjacency(
  123. const Eigen::PlainObjectBase<DerivedF> & F,
  124. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  125. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  126. {
  127. return triangle_triangle_adjacency(F,true,TT,TTi);
  128. }
  129. template <
  130. typename DerivedF,
  131. typename TTIndex>
  132. IGL_INLINE void igl::triangle_triangle_adjacency(
  133. const Eigen::PlainObjectBase<DerivedF> & F,
  134. std::vector<std::vector<std::vector<TTIndex> > > & TT)
  135. {
  136. std::vector<std::vector<std::vector<TTIndex> > > not_used;
  137. return triangle_triangle_adjacency(F,false,TT,not_used);
  138. }
  139. template <
  140. typename DerivedF,
  141. typename TTIndex,
  142. typename TTiIndex>
  143. IGL_INLINE void igl::triangle_triangle_adjacency(
  144. const Eigen::PlainObjectBase<DerivedF> & F,
  145. const bool construct_TTi,
  146. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  147. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  148. {
  149. using namespace Eigen;
  150. using namespace std;
  151. assert(F.cols() == 3 && "Faces must be triangles");
  152. // number of faces
  153. typedef typename DerivedF::Index Index;
  154. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  155. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  156. MatrixX2I E,uE;
  157. VectorXI EMAP;
  158. vector<vector<Index> > uE2E;
  159. unique_edge_map(F,E,uE,EMAP,uE2E);
  160. return triangle_triangle_adjacency(E,EMAP,uE2E,construct_TTi,TT,TTi);
  161. }
  162. template <
  163. typename DerivedE,
  164. typename DerivedEMAP,
  165. typename uE2EType,
  166. typename TTIndex,
  167. typename TTiIndex>
  168. IGL_INLINE void igl::triangle_triangle_adjacency(
  169. const Eigen::PlainObjectBase<DerivedE> & E,
  170. const Eigen::PlainObjectBase<DerivedEMAP> & EMAP,
  171. const std::vector<std::vector<uE2EType> > & uE2E,
  172. const bool construct_TTi,
  173. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  174. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  175. {
  176. using namespace std;
  177. using namespace Eigen;
  178. typedef typename DerivedE::Index Index;
  179. const size_t m = E.rows()/3;
  180. assert((size_t)E.rows() == m*3 && "E should come from list of triangles.");
  181. // E2E[i] --> {j,k,...} means face edge i corresponds to other faces edges j
  182. // and k
  183. TT.resize (m,vector<vector<TTIndex> >(3));
  184. if(construct_TTi)
  185. {
  186. TTi.resize(m,vector<vector<TTiIndex> >(3));
  187. }
  188. // No race conditions because TT*[f][c]'s are in bijection with e's
  189. // Minimum number of iterms per openmp thread
  190. //const size_t num_e = E.rows();
  191. # ifndef IGL_OMP_MIN_VALUE
  192. # define IGL_OMP_MIN_VALUE 1000
  193. # endif
  194. # pragma omp parallel for if (m>IGL_OMP_MIN_VALUE)
  195. // Slightly better memory access than loop over E
  196. for(Index f = 0;f<(Index)m;f++)
  197. {
  198. for(Index c = 0;c<3;c++)
  199. {
  200. const Index e = f + m*c;
  201. //const Index c = e/m;
  202. const vector<uE2EType> & N = uE2E[EMAP(e)];
  203. for(const auto & ne : N)
  204. {
  205. const Index nf = ne%m;
  206. // don't add self
  207. if(nf != f)
  208. {
  209. TT[f][c].push_back(nf);
  210. if(construct_TTi)
  211. {
  212. const Index nc = ne/m;
  213. TTi[f][c].push_back(nc);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. #ifdef IGL_STATIC_LIBRARY
  221. // Explicit template specialization
  222. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  223. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  224. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  225. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  226. template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  227. template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, long, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > const&, bool, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&);
  228. template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, unsigned long, int, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, bool, std::vector<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > > >&, std::vector<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > > >&);
  229. #endif