triangle_triangle_adjacency.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. template <typename Scalar, typename Index>
  15. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(const Eigen::PlainObjectBase<Scalar>& /*V*/,
  16. const Eigen::PlainObjectBase<Index>& F,
  17. std::vector<std::vector<int> >& TTT)
  18. {
  19. for(int f=0;f<F.rows();++f)
  20. for (int i=0;i<F.cols();++i)
  21. {
  22. // v1 v2 f ei
  23. int v1 = F(f,i);
  24. int v2 = F(f,(i+1)%F.cols());
  25. if (v1 > v2) std::swap(v1,v2);
  26. std::vector<int> r(4);
  27. r[0] = v1; r[1] = v2;
  28. r[2] = f; r[3] = i;
  29. TTT.push_back(r);
  30. }
  31. std::sort(TTT.begin(),TTT.end());
  32. }
  33. // Extract the face adjacencies
  34. template <typename Index>
  35. IGL_INLINE void igl::triangle_triangle_adjacency_extractTT(const Eigen::PlainObjectBase<Index>& F,
  36. std::vector<std::vector<int> >& TTT,
  37. Eigen::PlainObjectBase<Index>& TT)
  38. {
  39. TT = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  40. for(int i=1;i<(int)TTT.size();++i)
  41. {
  42. std::vector<int>& r1 = TTT[i-1];
  43. std::vector<int>& r2 = TTT[i];
  44. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  45. {
  46. TT(r1[2],r1[3]) = r2[2];
  47. TT(r2[2],r2[3]) = r1[2];
  48. }
  49. }
  50. }
  51. // Extract the face adjacencies indices (needed for fast traversal)
  52. template <typename Index>
  53. IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi(const Eigen::PlainObjectBase<Index>& F,
  54. std::vector<std::vector<int> >& TTT,
  55. Eigen::PlainObjectBase<Index>& TTi)
  56. {
  57. TTi = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  58. for(int i=1;i<(int)TTT.size();++i)
  59. {
  60. std::vector<int>& r1 = TTT[i-1];
  61. std::vector<int>& r2 = TTT[i];
  62. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  63. {
  64. TTi(r1[2],r1[3]) = r2[3];
  65. TTi(r2[2],r2[3]) = r1[3];
  66. }
  67. }
  68. }
  69. // Compute triangle-triangle adjacency
  70. template <typename Scalar, typename Index>
  71. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  72. const Eigen::PlainObjectBase<Index>& F,
  73. Eigen::PlainObjectBase<Index>& TT)
  74. {
  75. //assert(igl::is_edge_manifold(V,F));
  76. std::vector<std::vector<int> > TTT;
  77. triangle_triangle_adjacency_preprocess(V,F,TTT);
  78. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  79. }
  80. // Compute triangle-triangle adjacency with indices
  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. Eigen::PlainObjectBase<Index>& TTi)
  86. {
  87. //assert(igl::is_edge_manifold(V,F));
  88. std::vector<std::vector<int> > TTT;
  89. triangle_triangle_adjacency_preprocess(V,F,TTT);
  90. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  91. triangle_triangle_adjacency_extractTTi(F,TTT,TTi);
  92. }
  93. template <
  94. typename DerivedF,
  95. typename TTIndex,
  96. typename TTiIndex>
  97. IGL_INLINE void igl::triangle_triangle_adjacency(
  98. const Eigen::PlainObjectBase<DerivedF> & F,
  99. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  100. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  101. {
  102. return triangle_triangle_adjacency(F,true,TT,TTi);
  103. }
  104. template <
  105. typename DerivedF,
  106. typename TTIndex>
  107. IGL_INLINE void igl::triangle_triangle_adjacency(
  108. const Eigen::PlainObjectBase<DerivedF> & F,
  109. std::vector<std::vector<std::vector<TTIndex> > > & TT)
  110. {
  111. std::vector<std::vector<std::vector<TTIndex> > > not_used;
  112. return triangle_triangle_adjacency(F,false,TT,not_used);
  113. }
  114. template <
  115. typename DerivedF,
  116. typename TTIndex,
  117. typename TTiIndex>
  118. IGL_INLINE void igl::triangle_triangle_adjacency(
  119. const Eigen::PlainObjectBase<DerivedF> & F,
  120. const bool construct_TTi,
  121. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  122. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  123. {
  124. using namespace Eigen;
  125. using namespace std;
  126. assert(F.cols() == 3 && "Faces must be triangles");
  127. // number of faces
  128. typedef typename DerivedF::Index Index;
  129. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  130. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  131. MatrixX2I E,uE;
  132. VectorXI EMAP;
  133. vector<vector<Index> > uE2E;
  134. unique_edge_map(F,E,uE,EMAP,uE2E);
  135. return triangle_triangle_adjacency(E,EMAP,uE2E,construct_TTi,TT,TTi);
  136. }
  137. template <
  138. typename DerivedE,
  139. typename DerivedEMAP,
  140. typename uE2EType,
  141. typename TTIndex,
  142. typename TTiIndex>
  143. IGL_INLINE void igl::triangle_triangle_adjacency(
  144. const Eigen::PlainObjectBase<DerivedE> & E,
  145. const Eigen::PlainObjectBase<DerivedEMAP> & EMAP,
  146. const std::vector<std::vector<uE2EType> > & uE2E,
  147. const bool construct_TTi,
  148. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  149. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  150. {
  151. using namespace std;
  152. using namespace Eigen;
  153. typedef typename DerivedE::Index Index;
  154. const size_t m = E.rows()/3;
  155. assert((size_t)E.rows() == m*3 && "E should come from list of triangles.");
  156. // E2E[i] --> {j,k,...} means face edge i corresponds to other faces edges j
  157. // and k
  158. TT.resize (m,vector<vector<TTIndex> >(3));
  159. if(construct_TTi)
  160. {
  161. TTi.resize(m,vector<vector<TTiIndex> >(3));
  162. }
  163. // No race conditions because TT*[f][c]'s are in bijection with e's
  164. // Minimum number of iterms per openmp thread
  165. //const size_t num_e = E.rows();
  166. # ifndef IGL_OMP_MIN_VALUE
  167. # define IGL_OMP_MIN_VALUE 1000
  168. # endif
  169. # pragma omp parallel for if (m>IGL_OMP_MIN_VALUE)
  170. // Slightly better memory access than loop over E
  171. for(Index f = 0;f<(Index)m;f++)
  172. {
  173. for(Index c = 0;c<3;c++)
  174. {
  175. const Index e = f + m*c;
  176. //const Index c = e/m;
  177. const vector<Index> & N = uE2E[EMAP(e)];
  178. for(const auto & ne : N)
  179. {
  180. const Index nf = ne%m;
  181. TT[f][c].push_back(nf);
  182. if(construct_TTi)
  183. {
  184. const Index nc = ne/m;
  185. TTi[f][c].push_back(nc);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. #ifdef IGL_STATIC_LIBRARY
  192. // Explicit template specialization
  193. 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> > > > > >&);
  194. 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> >&);
  195. template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, -1, 0, -1, -1>, long, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, 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> > > > > >&);
  196. #endif