triangle_triangle_adjacency.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "parallel_for.h"
  13. #include "unique_edge_map.h"
  14. #include <algorithm>
  15. #include <iostream>
  16. // Extract the face adjacencies
  17. template <typename DerivedF, typename TTT_type, typename DerivedTT>
  18. IGL_INLINE void igl::triangle_triangle_adjacency_extractTT(
  19. const Eigen::PlainObjectBase<DerivedF>& F,
  20. std::vector<std::vector<TTT_type> >& TTT,
  21. Eigen::PlainObjectBase<DerivedTT>& TT)
  22. {
  23. TT.setConstant((int)(F.rows()),F.cols(),-1);
  24. for(int i=1;i<(int)TTT.size();++i)
  25. {
  26. std::vector<int>& r1 = TTT[i-1];
  27. std::vector<int>& r2 = TTT[i];
  28. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  29. {
  30. TT(r1[2],r1[3]) = r2[2];
  31. TT(r2[2],r2[3]) = r1[2];
  32. }
  33. }
  34. }
  35. template <typename DerivedF, typename DerivedTT>
  36. IGL_INLINE void igl::triangle_triangle_adjacency(
  37. const Eigen::PlainObjectBase<DerivedF>& F,
  38. Eigen::PlainObjectBase<DerivedTT>& TT)
  39. {
  40. DerivedTT TTi;
  41. return triangle_triangle_adjacency(F,TT,TTi);
  42. }
  43. template <typename DerivedF, typename TTT_type>
  44. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(
  45. const Eigen::PlainObjectBase<DerivedF>& F,
  46. std::vector<std::vector<TTT_type> >& TTT)
  47. {
  48. for(int f=0;f<F.rows();++f)
  49. for (int i=0;i<F.cols();++i)
  50. {
  51. // v1 v2 f ei
  52. int v1 = F(f,i);
  53. int v2 = F(f,(i+1)%F.cols());
  54. if (v1 > v2) std::swap(v1,v2);
  55. std::vector<int> r(4);
  56. r[0] = v1; r[1] = v2;
  57. r[2] = f; r[3] = i;
  58. TTT.push_back(r);
  59. }
  60. std::sort(TTT.begin(),TTT.end());
  61. }
  62. // Extract the face adjacencies indices (needed for fast traversal)
  63. template <typename DerivedF, typename TTT_type, typename DerivedTTi>
  64. IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi(
  65. const Eigen::PlainObjectBase<DerivedF>& F,
  66. std::vector<std::vector<TTT_type> >& TTT,
  67. Eigen::PlainObjectBase<DerivedTTi>& TTi)
  68. {
  69. TTi.setConstant((int)(F.rows()),F.cols(),-1);
  70. for(int i=1;i<(int)TTT.size();++i)
  71. {
  72. std::vector<int>& r1 = TTT[i-1];
  73. std::vector<int>& r2 = TTT[i];
  74. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  75. {
  76. TTi(r1[2],r1[3]) = r2[3];
  77. TTi(r2[2],r2[3]) = r1[3];
  78. }
  79. }
  80. }
  81. // Compute triangle-triangle adjacency with indices
  82. template <typename DerivedF, typename DerivedTT, typename DerivedTTi>
  83. IGL_INLINE void igl::triangle_triangle_adjacency(
  84. const Eigen::PlainObjectBase<DerivedF>& F,
  85. Eigen::PlainObjectBase<DerivedTT>& TT,
  86. Eigen::PlainObjectBase<DerivedTTi>& TTi)
  87. {
  88. std::vector<std::vector<int> > TTT;
  89. triangle_triangle_adjacency_preprocess(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 items per thread
  165. //const size_t num_e = E.rows();
  166. // Slightly better memory access than loop over E
  167. igl::parallel_for(
  168. m,
  169. [&](const Index & f)
  170. {
  171. for(Index c = 0;c<3;c++)
  172. {
  173. const Index e = f + m*c;
  174. //const Index c = e/m;
  175. const vector<uE2EType> & N = uE2E[EMAP(e)];
  176. for(const auto & ne : N)
  177. {
  178. const Index nf = ne%m;
  179. // don't add self
  180. if(nf != f)
  181. {
  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. 1000ul);
  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, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, 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>, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > 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> > > > > >&);
  200. // generated by autoexplicit.sh
  201. 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> >&);
  202. // generated by autoexplicit.sh
  203. 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> >&);
  204. 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> >&);
  205. 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> > > > > >&);
  206. 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> > > > > >&);
  207. template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, 3, 0, -1, 3>, 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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  208. #endif