outer_element.cpp 11 KB

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