triangle_triangle_adjacency.cpp 8.9 KB

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