triangle_triangle_adjacency.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <igl/is_edge_manifold.h>
  10. #include <algorithm>
  11. template <typename Scalar, typename Index>
  12. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(const Eigen::PlainObjectBase<Scalar>& /*V*/,
  13. const Eigen::PlainObjectBase<Index>& F,
  14. std::vector<std::vector<int> >& TTT)
  15. {
  16. for(int f=0;f<F.rows();++f)
  17. for (int i=0;i<F.cols();++i)
  18. {
  19. // v1 v2 f ei
  20. int v1 = F(f,i);
  21. int v2 = F(f,(i+1)%F.cols());
  22. if (v1 > v2) std::swap(v1,v2);
  23. std::vector<int> r(4);
  24. r[0] = v1; r[1] = v2;
  25. r[2] = f; r[3] = i;
  26. TTT.push_back(r);
  27. }
  28. std::sort(TTT.begin(),TTT.end());
  29. }
  30. // Extract the face adjacencies
  31. template <typename Index>
  32. IGL_INLINE void igl::triangle_triangle_adjacency_extractTT(const Eigen::PlainObjectBase<Index>& F,
  33. std::vector<std::vector<int> >& TTT,
  34. Eigen::PlainObjectBase<Index>& TT)
  35. {
  36. TT = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  37. for(int i=1;i<(int)TTT.size();++i)
  38. {
  39. std::vector<int>& r1 = TTT[i-1];
  40. std::vector<int>& r2 = TTT[i];
  41. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  42. {
  43. TT(r1[2],r1[3]) = r2[2];
  44. TT(r2[2],r2[3]) = r1[2];
  45. }
  46. }
  47. }
  48. // Extract the face adjacencies indices (needed for fast traversal)
  49. template <typename Index>
  50. IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi(const Eigen::PlainObjectBase<Index>& F,
  51. std::vector<std::vector<int> >& TTT,
  52. Eigen::PlainObjectBase<Index>& TTi)
  53. {
  54. TTi = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  55. for(int i=1;i<(int)TTT.size();++i)
  56. {
  57. std::vector<int>& r1 = TTT[i-1];
  58. std::vector<int>& r2 = TTT[i];
  59. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  60. {
  61. TTi(r1[2],r1[3]) = r2[3];
  62. TTi(r2[2],r2[3]) = r1[3];
  63. }
  64. }
  65. }
  66. // Compute triangle-triangle adjacency
  67. template <typename Scalar, typename Index>
  68. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  69. const Eigen::PlainObjectBase<Index>& F,
  70. Eigen::PlainObjectBase<Index>& TT)
  71. {
  72. //assert(igl::is_edge_manifold(V,F));
  73. std::vector<std::vector<int> > TTT;
  74. triangle_triangle_adjacency_preprocess(V,F,TTT);
  75. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  76. }
  77. // Compute triangle-triangle adjacency with indices
  78. template <typename Scalar, typename Index>
  79. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  80. const Eigen::PlainObjectBase<Index>& F,
  81. Eigen::PlainObjectBase<Index>& TT,
  82. Eigen::PlainObjectBase<Index>& TTi)
  83. {
  84. //assert(igl::is_edge_manifold(V,F));
  85. std::vector<std::vector<int> > TTT;
  86. triangle_triangle_adjacency_preprocess(V,F,TTT);
  87. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  88. triangle_triangle_adjacency_extractTTi(F,TTT,TTi);
  89. }
  90. #ifdef IGL_STATIC_LIBRARY
  91. // Explicit template specialization
  92. 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> >&);
  93. // generated by autoexplicit.sh
  94. 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> >&);
  95. 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> >&);
  96. 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> >&);
  97. #endif