outer_element.cpp 9.9 KB

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