triangle_triangle_adjacency.cpp 8.1 KB

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