extract_feature.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Qingnan Zhou <qnzhou@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 "extract_feature.h"
  9. #include <igl/unique_edge_map.h>
  10. #include <CGAL/Kernel/global_functions.h>
  11. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  12. template<
  13. typename DerivedV,
  14. typename DerivedF,
  15. typename DerivedE >
  16. IGL_INLINE void igl::copyleft::cgal::extract_feature(
  17. const Eigen::PlainObjectBase<DerivedV>& V,
  18. const Eigen::PlainObjectBase<DerivedF>& F,
  19. const double tol,
  20. Eigen::PlainObjectBase<DerivedE>& feature_edges) {
  21. using IndexType = typename DerivedE::Scalar;
  22. DerivedE E, uE;
  23. Eigen::VectorXi EMAP;
  24. std::vector<std::vector<IndexType> > uE2E;
  25. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  26. igl::copyleft::cgal::extract_feature(V, F, tol, E, uE, uE2E, feature_edges);
  27. }
  28. template<
  29. typename DerivedV,
  30. typename DerivedF,
  31. typename DerivedE >
  32. IGL_INLINE void igl::copyleft::cgal::extract_feature(
  33. const Eigen::PlainObjectBase<DerivedV>& V,
  34. const Eigen::PlainObjectBase<DerivedF>& F,
  35. const double tol,
  36. const Eigen::PlainObjectBase<DerivedE>& E,
  37. const Eigen::PlainObjectBase<DerivedE>& uE,
  38. const std::vector<std::vector<typename DerivedE::Scalar> >& uE2E,
  39. Eigen::PlainObjectBase<DerivedE>& feature_edges) {
  40. assert(V.cols() == 3);
  41. assert(F.cols() == 3);
  42. using Scalar = typename DerivedV::Scalar;
  43. using IndexType = typename DerivedE::Scalar;
  44. using Vertex = Eigen::Matrix<Scalar, 3, 1>;
  45. using Kernel = typename CGAL::Exact_predicates_exact_constructions_kernel;
  46. using Point = typename Kernel::Point_3;
  47. const size_t num_unique_edges = uE.rows();
  48. const size_t num_faces = F.rows();
  49. // NOTE: CGAL's definition of dihedral angle measures the angle between two
  50. // facets instead of facet normals.
  51. const double cos_tol = cos(igl::PI - tol);
  52. std::vector<size_t> result; // Indices into uE
  53. auto is_non_manifold = [&uE2E](size_t ei) -> bool {
  54. return uE2E[ei].size() > 2;
  55. };
  56. auto is_boundary = [&uE2E](size_t ei) -> bool {
  57. return uE2E[ei].size() == 1;
  58. };
  59. auto opposite_vertex = [&uE, &F](size_t ei, size_t fi) -> IndexType {
  60. const size_t v0 = uE(ei, 0);
  61. const size_t v1 = uE(ei, 1);
  62. for (size_t i=0; i<3; i++) {
  63. const size_t v = F(fi, i);
  64. if (v != v0 && v != v1) { return v; }
  65. }
  66. throw "Input face must be topologically degenerate!";
  67. };
  68. auto is_feature = [&V, &F, &uE, &uE2E, &opposite_vertex, num_faces](
  69. size_t ei, double cos_tol) -> bool {
  70. auto adj_faces = uE2E[ei];
  71. assert(adj_faces.size() == 2);
  72. const Vertex v0 = V.row(uE(ei, 0));
  73. const Vertex v1 = V.row(uE(ei, 1));
  74. const Vertex v2 = V.row(opposite_vertex(ei, adj_faces[0] % num_faces));
  75. const Vertex v3 = V.row(opposite_vertex(ei, adj_faces[1] % num_faces));
  76. const Point p0(v0[0], v0[1], v0[2]);
  77. const Point p1(v1[0], v1[1], v1[2]);
  78. const Point p2(v2[0], v2[1], v2[2]);
  79. const Point p3(v3[0], v3[1], v3[2]);
  80. const auto ori = CGAL::orientation(p0, p1, p2, p3);
  81. switch (ori) {
  82. case CGAL::POSITIVE:
  83. return CGAL::compare_dihedral_angle(p0, p1, p2, p3, cos_tol) ==
  84. CGAL::SMALLER;
  85. case CGAL::NEGATIVE:
  86. return CGAL::compare_dihedral_angle(p0, p1, p3, p2, cos_tol) ==
  87. CGAL::SMALLER;
  88. case CGAL::COPLANAR:
  89. if (!CGAL::collinear(p0, p1, p2) && !CGAL::collinear(p0, p1, p3)) {
  90. return CGAL::compare_dihedral_angle(p0, p1, p2, p3, cos_tol) ==
  91. CGAL::SMALLER;
  92. } else {
  93. throw "Dihedral angle (and feature edge) is not well defined for"
  94. " degenerate triangles!";
  95. }
  96. default:
  97. throw "Unknown CGAL orientation";
  98. }
  99. };
  100. for (size_t i=0; i<num_unique_edges; i++) {
  101. if (is_boundary(i) || is_non_manifold(i) || is_feature(i, cos_tol)) {
  102. result.push_back(i);
  103. }
  104. }
  105. const size_t num_feature_edges = result.size();
  106. feature_edges.resize(num_feature_edges, 2);
  107. for (size_t i=0; i<num_feature_edges; i++) {
  108. feature_edges.row(i) = uE.row(result[i]);
  109. }
  110. }