outer_element.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "outer_element.h"
  9. #include <iostream>
  10. #include <vector>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedI,
  15. typename IndexType,
  16. typename DerivedA
  17. >
  18. IGL_INLINE void igl::copyleft::cgal::outer_vertex(
  19. const Eigen::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedF> & F,
  21. const Eigen::PlainObjectBase<DerivedI> & I,
  22. IndexType & v_index,
  23. Eigen::PlainObjectBase<DerivedA> & A)
  24. {
  25. // Algorithm:
  26. // Find an outer vertex (i.e. vertex reachable from infinity)
  27. // Return the vertex with the largest X value.
  28. // If there is a tie, pick the one with largest Y value.
  29. // If there is still a tie, pick the one with the largest Z value.
  30. // If there is still a tie, then there are duplicated vertices within the
  31. // mesh, which violates the precondition.
  32. typedef typename DerivedF::Scalar Index;
  33. const Index INVALID = std::numeric_limits<Index>::max();
  34. const size_t num_selected_faces = I.rows();
  35. std::vector<size_t> candidate_faces;
  36. Index outer_vid = INVALID;
  37. typename DerivedV::Scalar outer_val = 0;
  38. for (size_t i=0; i<num_selected_faces; i++)
  39. {
  40. size_t f = I(i);
  41. for (size_t j=0; j<3; j++)
  42. {
  43. Index v = F(f, j);
  44. auto vx = V(v, 0);
  45. if (outer_vid == INVALID || vx > outer_val)
  46. {
  47. outer_val = vx;
  48. outer_vid = v;
  49. candidate_faces = {f};
  50. } else if (v == outer_vid)
  51. {
  52. candidate_faces.push_back(f);
  53. } else if (vx == outer_val)
  54. {
  55. // Break tie.
  56. auto vy = V(v,1);
  57. auto vz = V(v, 2);
  58. auto outer_y = V(outer_vid, 1);
  59. auto outer_z = V(outer_vid, 2);
  60. assert(!(vy == outer_y && vz == outer_z));
  61. bool replace = (vy > outer_y) ||
  62. ((vy == outer_y) && (vz > outer_z));
  63. if (replace)
  64. {
  65. outer_val = vx;
  66. outer_vid = v;
  67. candidate_faces = {f};
  68. }
  69. }
  70. }
  71. }
  72. assert(outer_vid != INVALID);
  73. assert(candidate_faces.size() > 0);
  74. v_index = outer_vid;
  75. A.resize(candidate_faces.size());
  76. std::copy(candidate_faces.begin(), candidate_faces.end(), A.data());
  77. }
  78. template<
  79. typename DerivedV,
  80. typename DerivedF,
  81. typename DerivedI,
  82. typename IndexType,
  83. typename DerivedA
  84. >
  85. IGL_INLINE void igl::copyleft::cgal::outer_edge(
  86. const Eigen::PlainObjectBase<DerivedV> & V,
  87. const Eigen::PlainObjectBase<DerivedF> & F,
  88. const Eigen::PlainObjectBase<DerivedI> & I,
  89. IndexType & v1,
  90. IndexType & v2,
  91. Eigen::PlainObjectBase<DerivedA> & A) {
  92. // Algorithm:
  93. // Find an outer vertex first.
  94. // Find the incident edge with largest abs slope when projected onto XY plane.
  95. // If there is a tie, check the signed slope and use the positive one.
  96. // If there is still a tie, break it using the projected slope onto ZX plane.
  97. // If there is still a tie, again check the signed slope and use the positive one.
  98. // If there is still a tie, then there are multiple overlapping edges,
  99. // which violates the precondition.
  100. typedef typename DerivedV::Scalar Scalar;
  101. typedef typename DerivedV::Index Index;
  102. typedef typename Eigen::Matrix<Scalar, 3, 1> ScalarArray3;
  103. typedef typename Eigen::Matrix<typename DerivedF::Scalar, 3, 1> IndexArray3;
  104. const Index INVALID = std::numeric_limits<Index>::max();
  105. Index outer_vid;
  106. Eigen::Matrix<Index,Eigen::Dynamic,1> candidate_faces;
  107. outer_vertex(V, F, I, outer_vid, candidate_faces);
  108. const ScalarArray3& outer_v = V.row(outer_vid);
  109. assert(candidate_faces.size() > 0);
  110. auto get_vertex_index = [&](const IndexArray3& f, Index vid) -> Index
  111. {
  112. if (f[0] == vid) return 0;
  113. if (f[1] == vid) return 1;
  114. if (f[2] == vid) return 2;
  115. assert(false);
  116. return -1;
  117. };
  118. auto unsigned_value = [](Scalar v) -> Scalar {
  119. if (v < 0) return v * -1;
  120. else return v;
  121. };
  122. Scalar outer_slope_YX = 0;
  123. Scalar outer_slope_ZX = 0;
  124. Index outer_opp_vid = INVALID;
  125. bool infinite_slope_detected = false;
  126. std::vector<Index> incident_faces;
  127. auto check_and_update_outer_edge = [&](Index opp_vid, Index fid) -> void {
  128. if (opp_vid == outer_opp_vid)
  129. {
  130. incident_faces.push_back(fid);
  131. return;
  132. }
  133. const ScalarArray3 opp_v = V.row(opp_vid);
  134. if (!infinite_slope_detected && outer_v[0] != opp_v[0])
  135. {
  136. // Finite slope
  137. const ScalarArray3 diff = opp_v - outer_v;
  138. const Scalar slope_YX = diff[1] / diff[0];
  139. const Scalar slope_ZX = diff[2] / diff[0];
  140. const Scalar u_slope_YX = unsigned_value(slope_YX);
  141. const Scalar u_slope_ZX = unsigned_value(slope_ZX);
  142. bool update = false;
  143. if (outer_opp_vid == INVALID) {
  144. update = true;
  145. } else {
  146. const Scalar u_outer_slope_YX = unsigned_value(outer_slope_YX);
  147. if (u_slope_YX > u_outer_slope_YX) {
  148. update = true;
  149. } else if (u_slope_YX == u_outer_slope_YX &&
  150. slope_YX > outer_slope_YX) {
  151. update = true;
  152. } else if (slope_YX == outer_slope_YX) {
  153. const Scalar u_outer_slope_ZX =
  154. unsigned_value(outer_slope_ZX);
  155. if (u_slope_ZX > u_outer_slope_ZX) {
  156. update = true;
  157. } else if (u_slope_ZX == u_outer_slope_ZX &&
  158. slope_ZX > outer_slope_ZX) {
  159. update = true;
  160. } else if (slope_ZX == u_outer_slope_ZX) {
  161. assert(false);
  162. }
  163. }
  164. }
  165. if (update) {
  166. outer_opp_vid = opp_vid;
  167. outer_slope_YX = slope_YX;
  168. outer_slope_ZX = slope_ZX;
  169. incident_faces = {fid};
  170. }
  171. } else if (!infinite_slope_detected)
  172. {
  173. // Infinite slope
  174. outer_opp_vid = opp_vid;
  175. infinite_slope_detected = true;
  176. incident_faces = {fid};
  177. }
  178. };
  179. const size_t num_candidate_faces = candidate_faces.size();
  180. for (size_t i=0; i<num_candidate_faces; i++)
  181. {
  182. const Index fid = candidate_faces(i);
  183. const IndexArray3& f = F.row(fid);
  184. size_t id = get_vertex_index(f, outer_vid);
  185. Index next_vid = f((id+1)%3);
  186. Index prev_vid = f((id+2)%3);
  187. check_and_update_outer_edge(next_vid, fid);
  188. check_and_update_outer_edge(prev_vid, fid);
  189. }
  190. v1 = outer_vid;
  191. v2 = outer_opp_vid;
  192. A.resize(incident_faces.size());
  193. std::copy(incident_faces.begin(), incident_faces.end(), A.data());
  194. }
  195. #ifdef IGL_STATIC_LIBRARY
  196. // Explicit template specialization
  197. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  198. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, long&, long&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  199. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, long, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -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> > const&, long&, long&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  200. 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<long, -1, 1, 0, -1, 1>, long, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(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<long, -1, 1, 0, -1, 1> > const&, long&, long&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  201. 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>, long, Eigen::Matrix<long, -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> > const&, long&, long&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  202. template void igl::copyleft::cgal::outer_edge<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, long, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, long&, long&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  203. #endif