triangle_triangle_adjacency.cpp 11 KB

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