outer_edge.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <[email protected]>
  4. // Copyright (C) 2021 Alec Jacobson <[email protected]>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "outer_edge.h"
  10. #include "outer_vertex.h"
  11. #include <iostream>
  12. #include <vector>
  13. template<
  14. typename DerivedV,
  15. typename DerivedF,
  16. typename DerivedI,
  17. typename IndexType,
  18. typename DerivedA
  19. >
  20. IGL_INLINE void igl::copyleft::cgal::outer_edge(
  21. const Eigen::MatrixBase<DerivedV> & V,
  22. const Eigen::MatrixBase<DerivedF> & F,
  23. const Eigen::MatrixBase<DerivedI> & I,
  24. IndexType & v1,
  25. IndexType & v2,
  26. Eigen::PlainObjectBase<DerivedA> & A)
  27. {
  28. // Algorithm:
  29. // Find an outer vertex first.
  30. // Find the incident edge with largest abs slope when projected onto XY plane.
  31. // If there is a tie, check the signed slope and use the positive one.
  32. // If there is still a tie, break it using the projected slope onto ZX plane.
  33. // If there is still a tie, again check the signed slope and use the positive one.
  34. // If there is still a tie, then there are multiple overlapping edges,
  35. // which violates the precondition.
  36. typedef typename DerivedV::Scalar Scalar;
  37. typedef typename DerivedV::Index Index;
  38. typedef typename Eigen::Matrix<Scalar, 3, 1> ScalarArray3;
  39. typedef typename Eigen::Matrix<typename DerivedF::Scalar, 3, 1> IndexArray3;
  40. const Index INVALID = std::numeric_limits<Index>::max();
  41. Index outer_vid;
  42. Eigen::Matrix<Index,Eigen::Dynamic,1> candidate_faces;
  43. outer_vertex(V, F, I, outer_vid, candidate_faces);
  44. const ScalarArray3& outer_v = V.row(outer_vid);
  45. assert(candidate_faces.size() > 0);
  46. auto get_vertex_index = [&](const IndexArray3& f, Index vid) -> Index
  47. {
  48. if (f[0] == vid) return 0;
  49. if (f[1] == vid) return 1;
  50. if (f[2] == vid) return 2;
  51. assert(false);
  52. return -1;
  53. };
  54. auto unsigned_value = [](Scalar v) -> Scalar {
  55. if (v < 0) return v * -1;
  56. else return v;
  57. };
  58. Scalar outer_slope_YX = 0;
  59. Scalar outer_slope_ZX = 0;
  60. Index outer_opp_vid = INVALID;
  61. bool infinite_slope_detected = false;
  62. std::vector<Index> incident_faces;
  63. auto check_and_update_outer_edge = [&](Index opp_vid, Index fid) -> void {
  64. if (opp_vid == outer_opp_vid)
  65. {
  66. incident_faces.push_back(fid);
  67. return;
  68. }
  69. const ScalarArray3 opp_v = V.row(opp_vid);
  70. if (!infinite_slope_detected && outer_v[0] != opp_v[0])
  71. {
  72. // Finite slope
  73. const ScalarArray3 diff = opp_v - outer_v;
  74. const Scalar slope_YX = diff[1] / diff[0];
  75. const Scalar slope_ZX = diff[2] / diff[0];
  76. const Scalar u_slope_YX = unsigned_value(slope_YX);
  77. const Scalar u_slope_ZX = unsigned_value(slope_ZX);
  78. bool update = false;
  79. if (outer_opp_vid == INVALID) {
  80. update = true;
  81. } else {
  82. const Scalar u_outer_slope_YX = unsigned_value(outer_slope_YX);
  83. if (u_slope_YX > u_outer_slope_YX) {
  84. update = true;
  85. } else if (u_slope_YX == u_outer_slope_YX &&
  86. slope_YX > outer_slope_YX) {
  87. update = true;
  88. } else if (slope_YX == outer_slope_YX) {
  89. const Scalar u_outer_slope_ZX =
  90. unsigned_value(outer_slope_ZX);
  91. if (u_slope_ZX > u_outer_slope_ZX) {
  92. update = true;
  93. } else if (u_slope_ZX == u_outer_slope_ZX &&
  94. slope_ZX > outer_slope_ZX) {
  95. update = true;
  96. } else if (slope_ZX == u_outer_slope_ZX) {
  97. assert(false);
  98. }
  99. }
  100. }
  101. if (update) {
  102. outer_opp_vid = opp_vid;
  103. outer_slope_YX = slope_YX;
  104. outer_slope_ZX = slope_ZX;
  105. incident_faces = {fid};
  106. }
  107. } else if (!infinite_slope_detected)
  108. {
  109. // Infinite slope
  110. outer_opp_vid = opp_vid;
  111. infinite_slope_detected = true;
  112. incident_faces = {fid};
  113. }
  114. };
  115. const size_t num_candidate_faces = candidate_faces.size();
  116. for (size_t i=0; i<num_candidate_faces; i++)
  117. {
  118. const Index fid = candidate_faces(i);
  119. const IndexArray3& f = F.row(fid);
  120. size_t id = get_vertex_index(f, outer_vid);
  121. Index next_vid = f((id+1)%3);
  122. Index prev_vid = f((id+2)%3);
  123. check_and_update_outer_edge(next_vid, fid);
  124. check_and_update_outer_edge(prev_vid, fid);
  125. }
  126. v1 = outer_vid;
  127. v2 = outer_opp_vid;
  128. A.resize(incident_faces.size());
  129. std::copy(incident_faces.begin(), incident_faces.end(), A.data());
  130. }
  131. #ifdef IGL_STATIC_LIBRARY
  132. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  133. // Explicit template instantiation
  134. // generated by autoexplicit.sh
  135. #include <cstdint>
  136. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::ptrdiff_t, Eigen::Matrix<std::ptrdiff_t, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, std::ptrdiff_t&, std::ptrdiff_t&, Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t, -1, 1, 0, -1, 1> >&);
  137. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<CGAL::Epeck::FT,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,1,0,-1,1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<CGAL::Epeck::FT,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,1,0,-1,1> > const&,std::ptrdiff_t&,std::ptrdiff_t&,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >&);
  138. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<CGAL::Epeck::FT,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,-1,0,-1,-1>,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<CGAL::Epeck::FT,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> > const&,std::ptrdiff_t&,std::ptrdiff_t&,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >&);
  139. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<double,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,-1,0,-1,-1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<double,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,-1,0,-1,-1> > const&,std::ptrdiff_t&,std::ptrdiff_t&,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >&);
  140. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<double,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,-1,0,-1,-1>,Eigen::Matrix<int,-1,1,0,-1,1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<double,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,-1,0,-1,-1> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,1,0,-1,1> > const&,std::ptrdiff_t&,std::ptrdiff_t&,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >&);
  141. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<double,-1,3,0,-1,3>,Eigen::Matrix<int,-1,3,0,-1,3>,Eigen::Matrix<int,-1,1,0,-1,1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<double,-1,3,0,-1,3> > const &,Eigen::MatrixBase<Eigen::Matrix<int,-1,3,0,-1,3> > const &,Eigen::MatrixBase<Eigen::Matrix<int,-1,1,0,-1,1> > const &,std::ptrdiff_t &,std::ptrdiff_t &,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> > &);
  142. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<double,-1,3,0,-1,3>,Eigen::Matrix<int,-1,3,0,-1,3>,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1>,std::ptrdiff_t,Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(Eigen::MatrixBase<Eigen::Matrix<double,-1,3,0,-1,3> > const&,Eigen::MatrixBase<Eigen::Matrix<int,-1,3,0,-1,3> > const&,Eigen::MatrixBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> > const&,std::ptrdiff_t&,std::ptrdiff_t&,Eigen::PlainObjectBase<Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >&);
  143. #ifdef WIN32
  144. template void __cdecl igl::copyleft::cgal::outer_edge<class Eigen::Matrix<class CGAL::Epeck::FT,-1,-1,0,-1,-1>,class Eigen::Matrix<int,-1,-1,0,-1,-1>,class Eigen::Matrix<long,-1,1,0,-1,1>,std::ptrdiff_t,class Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(class Eigen::MatrixBase<class Eigen::Matrix<class CGAL::Epeck::FT,-1,-1,0,-1,-1> > const &,class Eigen::MatrixBase<class Eigen::Matrix<int,-1,-1,0,-1,-1> > const &,class Eigen::MatrixBase<class Eigen::Matrix<long,-1,1,0,-1,1> > const &,std::ptrdiff_t &,std::ptrdiff_t &,class Eigen::PlainObjectBase<class Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> > &);
  145. template void __cdecl igl::copyleft::cgal::outer_edge<class Eigen::Matrix<double,-1,3,0,-1,3>,class Eigen::Matrix<int,-1,3,0,-1,3>,class Eigen::Matrix<long,-1,1,0,-1,1>,std::ptrdiff_t,class Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> >(class Eigen::MatrixBase<class Eigen::Matrix<double,-1,3,0,-1,3> > const &,class Eigen::MatrixBase<class Eigen::Matrix<int,-1,3,0,-1,3> > const &,class Eigen::MatrixBase<class Eigen::Matrix<long,-1,1,0,-1,1> > const &,std::ptrdiff_t &,std::ptrdiff_t &,class Eigen::PlainObjectBase<class Eigen::Matrix<std::ptrdiff_t,-1,1,0,-1,1> > &);
  146. #endif
  147. #endif