exterior_element.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "exterior_element.h"
  9. #include "vertex_triangle_adjacency.h"
  10. #include <iostream>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedI,
  15. typename IndexType,
  16. typename DerivedA
  17. >
  18. IGL_INLINE void igl::exterior_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. // Algorithm:
  25. // Find an exterior vertex (i.e. vertex reachable from infinity)
  26. // Return the vertex with the largest X value.
  27. // If there is a tie, pick the one with largest Y value.
  28. // If there is still a tie, pick the one with the largest Z value.
  29. // If there is still a tie, then there are duplicated vertices within the
  30. // mesh, which violates the precondition.
  31. const size_t INVALID = std::numeric_limits<size_t>::max();
  32. const size_t num_selected_faces = I.rows();
  33. std::vector<size_t> candidate_faces;
  34. size_t exterior_vid = INVALID;
  35. typename DerivedV::Scalar exterior_val = 0;
  36. for (size_t i=0; i<num_selected_faces; i++) {
  37. size_t f = I[i];
  38. for (size_t j=0; j<3; j++) {
  39. auto v = F(f, j);
  40. auto vx = V(v, 0);
  41. if (exterior_vid == INVALID || vx > exterior_val) {
  42. exterior_val = vx;
  43. exterior_vid = v;
  44. candidate_faces = {f};
  45. } else if (v == exterior_vid) {
  46. candidate_faces.push_back(f);
  47. } else if (vx == exterior_val) {
  48. // Break tie.
  49. auto vy = V(v,1);
  50. auto vz = V(v, 2);
  51. auto exterior_y = V(exterior_vid, 1);
  52. auto exterior_z = V(exterior_vid, 2);
  53. assert(!(vy == exterior_y && vz == exterior_z));
  54. bool replace = (vy > exterior_y) ||
  55. ((vy == exterior_y) && (vz > exterior_z));
  56. if (replace) {
  57. exterior_val = vx;
  58. exterior_vid = v;
  59. candidate_faces = {f};
  60. }
  61. }
  62. }
  63. }
  64. assert(exterior_vid != INVALID);
  65. assert(candidate_faces.size() > 0);
  66. v_index = exterior_vid;
  67. A.resize(candidate_faces.size());
  68. std::copy(candidate_faces.begin(), candidate_faces.end(), A.data());
  69. }
  70. template<
  71. typename DerivedV,
  72. typename DerivedF,
  73. typename DerivedI,
  74. typename IndexType,
  75. typename DerivedA
  76. >
  77. IGL_INLINE void igl::exterior_edge(
  78. const Eigen::PlainObjectBase<DerivedV> & V,
  79. const Eigen::PlainObjectBase<DerivedF> & F,
  80. const Eigen::PlainObjectBase<DerivedI> & I,
  81. IndexType & v1,
  82. IndexType & v2,
  83. Eigen::PlainObjectBase<DerivedA> & A) {
  84. // Algorithm:
  85. // Find an exterior vertex first.
  86. // Find the incident edge with largest slope when projected onto XY plane.
  87. // If there is still a tie, break it using the projected splot onto ZX plane.
  88. // If there is still a tie, then there are multiple overlapping edges,
  89. // which violates the precondition.
  90. typedef typename DerivedV::Scalar Scalar;
  91. typedef typename DerivedV::Index Index;
  92. typedef typename Eigen::Matrix<Scalar, 3, 1> ScalarArray3;
  93. typedef typename Eigen::Matrix<typename DerivedF::Scalar, 3, 1> IndexArray3;
  94. const size_t INVALID = std::numeric_limits<size_t>::max();
  95. Index exterior_vid;
  96. DerivedI candidate_faces;
  97. exterior_vertex(V, F, I, exterior_vid, candidate_faces);
  98. const ScalarArray3& exterior_v = V.row(exterior_vid);
  99. assert(candidate_faces.size() > 0);
  100. auto get_vertex_index = [&](const IndexArray3& f,
  101. Index vid) {
  102. if (f[0] == vid) return 0;
  103. if (f[1] == vid) return 1;
  104. if (f[2] == vid) return 2;
  105. assert(false);
  106. };
  107. Scalar exterior_slope_YX = 0;
  108. Scalar exterior_slope_ZX = 0;
  109. size_t exterior_opp_vid = INVALID;
  110. bool infinite_slope_detected = false;
  111. std::vector<Index> incident_faces;
  112. auto check_and_update_exterior_edge = [&](Index opp_vid, Index fid) {
  113. if (opp_vid == exterior_opp_vid) {
  114. incident_faces.push_back(fid);
  115. return;
  116. }
  117. const ScalarArray3 opp_v = V.row(opp_vid);
  118. if (!infinite_slope_detected && exterior_v[0] != opp_v[0]) {
  119. // Finite slope
  120. const ScalarArray3 diff = opp_v - exterior_v;
  121. const Scalar slope_YX = diff[1] / diff[0];
  122. const Scalar slope_ZX = diff[2] / diff[0];
  123. if (exterior_opp_vid == INVALID ||
  124. slope_YX > exterior_slope_YX ||
  125. (slope_YX == exterior_slope_YX &&
  126. slope_ZX > exterior_slope_ZX)) {
  127. exterior_opp_vid = opp_vid;
  128. exterior_slope_YX = slope_YX;
  129. exterior_slope_ZX = slope_ZX;
  130. incident_faces = {fid};
  131. }
  132. } else if (!infinite_slope_detected) {
  133. // Infinite slope
  134. exterior_opp_vid = opp_vid;
  135. infinite_slope_detected = true;
  136. incident_faces = {fid};
  137. }
  138. };
  139. const auto num_candidate_faces = candidate_faces.size();
  140. for (size_t i=0; i<num_candidate_faces; i++) {
  141. const Index fid = candidate_faces[i];
  142. const IndexArray3& f = F.row(fid);
  143. size_t id = get_vertex_index(f, exterior_vid);
  144. Index next_vid = f[(id+1)%3];
  145. Index prev_vid = f[(id+2)%3];
  146. check_and_update_exterior_edge(next_vid, fid);
  147. check_and_update_exterior_edge(prev_vid, fid);
  148. }
  149. v1 = exterior_vid;
  150. v2 = exterior_opp_vid;
  151. A.resize(incident_faces.size());
  152. std::copy(incident_faces.begin(), incident_faces.end(), A.data());
  153. }
  154. template<
  155. typename DerivedV,
  156. typename DerivedF,
  157. typename DerivedN,
  158. typename DerivedI,
  159. typename IndexType
  160. >
  161. IGL_INLINE void igl::exterior_facet(
  162. const Eigen::PlainObjectBase<DerivedV> & V,
  163. const Eigen::PlainObjectBase<DerivedF> & F,
  164. const Eigen::PlainObjectBase<DerivedN> & N,
  165. const Eigen::PlainObjectBase<DerivedI> & I,
  166. IndexType & f,
  167. bool & flipped) {
  168. // Algorithm:
  169. // Compute the exterior edge.
  170. // Find the facet with the largest absolute X normal component.
  171. // If there is a tie, keep the one with positive X component.
  172. // If there is still a tie, just pick first candidate.
  173. typedef typename DerivedV::Scalar Scalar;
  174. typedef typename DerivedV::Index Index;
  175. const size_t INVALID = std::numeric_limits<size_t>::max();
  176. Index v1,v2;
  177. DerivedI incident_faces;
  178. exterior_edge(V, F, I, v1, v2, incident_faces);
  179. assert(incident_faces.size() > 0);
  180. auto generic_fabs = [&](const Scalar& val) -> const Scalar {
  181. if (val >= 0) return val;
  182. else return -val;
  183. };
  184. Scalar max_nx = 0;
  185. size_t exterior_fid = INVALID;
  186. const size_t num_incident_faces = incident_faces.size();
  187. for (size_t i=0; i<num_incident_faces; i++) {
  188. const auto& fid = incident_faces[i];
  189. const Scalar nx = N(fid, 0);
  190. if (exterior_fid == INVALID) {
  191. max_nx = nx;
  192. exterior_fid = fid;
  193. } else {
  194. if (generic_fabs(nx) > generic_fabs(max_nx)) {
  195. max_nx = nx;
  196. exterior_fid = fid;
  197. } else if (nx == -max_nx && nx > 0) {
  198. max_nx = nx;
  199. exterior_fid = fid;
  200. }
  201. }
  202. }
  203. assert(exterior_fid != INVALID);
  204. f = exterior_fid;
  205. flipped = max_nx < 0;
  206. }
  207. #ifdef IGL_STATIC_LIBRARY
  208. // Explicit template specialization
  209. template void igl::exterior_facet<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, int>(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<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, int&, bool&);
  210. template void igl::exterior_facet<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, unsigned long>(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<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, unsigned long&, bool&);
  211. template void igl::exterior_facet<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(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<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int&, bool&);
  212. template void igl::exterior_facet<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(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<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int&, bool&);
  213. #endif