outer_element.cpp 9.7 KB

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