points_inside_component.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "points_inside_component.h"
  9. #include <cassert>
  10. #include <list>
  11. #include <limits>
  12. #include <vector>
  13. #include <CGAL/AABB_tree.h>
  14. #include <CGAL/AABB_traits.h>
  15. #include <CGAL/AABB_triangle_primitive.h>
  16. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  17. #include "order_facets_around_edge.h"
  18. #include "assign_scalar.h"
  19. namespace igl {
  20. namespace copyleft
  21. {
  22. namespace cgal {
  23. namespace points_inside_component_helper {
  24. typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
  25. typedef Kernel::Ray_3 Ray_3;
  26. typedef Kernel::Point_3 Point_3;
  27. typedef Kernel::Vector_3 Vector_3;
  28. typedef Kernel::Triangle_3 Triangle;
  29. typedef Kernel::Plane_3 Plane_3;
  30. typedef std::vector<Triangle>::iterator Iterator;
  31. typedef CGAL::AABB_triangle_primitive<Kernel, Iterator> Primitive;
  32. typedef CGAL::AABB_traits<Kernel, Primitive> AABB_triangle_traits;
  33. typedef CGAL::AABB_tree<AABB_triangle_traits> Tree;
  34. enum ElementType { VERTEX, EDGE, FACE };
  35. template<typename DerivedV, typename DerivedF, typename DerivedI>
  36. ElementType determine_element_type(
  37. const Eigen::PlainObjectBase<DerivedV>& V,
  38. const Eigen::PlainObjectBase<DerivedF>& F,
  39. const Eigen::PlainObjectBase<DerivedI>& I,
  40. const size_t fid, const Point_3& p,
  41. size_t& element_index) {
  42. const Eigen::Vector3i f = F.row(I(fid, 0));
  43. const Point_3 p0(V(f[0], 0), V(f[0], 1), V(f[0], 2));
  44. const Point_3 p1(V(f[1], 0), V(f[1], 1), V(f[1], 2));
  45. const Point_3 p2(V(f[2], 0), V(f[2], 1), V(f[2], 2));
  46. if (p == p0) { element_index = 0; return VERTEX; }
  47. if (p == p1) { element_index = 1; return VERTEX; }
  48. if (p == p2) { element_index = 2; return VERTEX; }
  49. if (CGAL::collinear(p0, p1, p)) { element_index = 2; return EDGE; }
  50. if (CGAL::collinear(p1, p2, p)) { element_index = 0; return EDGE; }
  51. if (CGAL::collinear(p2, p0, p)) { element_index = 1; return EDGE; }
  52. element_index = 0;
  53. return FACE;
  54. }
  55. template<typename DerivedF, typename DerivedI>
  56. void extract_adj_faces(
  57. const Eigen::PlainObjectBase<DerivedF>& F,
  58. const Eigen::PlainObjectBase<DerivedI>& I,
  59. const size_t s, const size_t d,
  60. std::vector<int>& adj_faces) {
  61. const size_t num_faces = I.rows();
  62. for (size_t i=0; i<num_faces; i++) {
  63. Eigen::Vector3i f = F.row(I(i, 0));
  64. if (((size_t)f[0] == s && (size_t)f[1] == d) ||
  65. ((size_t)f[1] == s && (size_t)f[2] == d) ||
  66. ((size_t)f[2] == s && (size_t)f[0] == d)) {
  67. adj_faces.push_back((I(i, 0)+1) * -1);
  68. continue;
  69. }
  70. if (((size_t)f[0] == d && (size_t)f[1] == s) ||
  71. ((size_t)f[1] == d && (size_t)f[2] == s) ||
  72. ((size_t)f[2] == d && (size_t)f[0] == s)) {
  73. adj_faces.push_back(I(i, 0)+1);
  74. continue;
  75. }
  76. }
  77. }
  78. template<typename DerivedF, typename DerivedI>
  79. void extract_adj_vertices(
  80. const Eigen::PlainObjectBase<DerivedF>& F,
  81. const Eigen::PlainObjectBase<DerivedI>& I,
  82. const size_t v, std::vector<int>& adj_vertices) {
  83. std::set<size_t> unique_adj_vertices;
  84. const size_t num_faces = I.rows();
  85. for (size_t i=0; i<num_faces; i++) {
  86. Eigen::Vector3i f = F.row(I(i, 0));
  87. if ((size_t)f[0] == v) {
  88. unique_adj_vertices.insert(f[1]);
  89. unique_adj_vertices.insert(f[2]);
  90. } else if ((size_t)f[1] == v) {
  91. unique_adj_vertices.insert(f[0]);
  92. unique_adj_vertices.insert(f[2]);
  93. } else if ((size_t)f[2] == v) {
  94. unique_adj_vertices.insert(f[0]);
  95. unique_adj_vertices.insert(f[1]);
  96. }
  97. }
  98. adj_vertices.resize(unique_adj_vertices.size());
  99. std::copy(unique_adj_vertices.begin(),
  100. unique_adj_vertices.end(),
  101. adj_vertices.begin());
  102. }
  103. template<typename DerivedV, typename DerivedF, typename DerivedI>
  104. bool determine_point_edge_orientation(
  105. const Eigen::PlainObjectBase<DerivedV>& V,
  106. const Eigen::PlainObjectBase<DerivedF>& F,
  107. const Eigen::PlainObjectBase<DerivedI>& I,
  108. const Point_3& query, size_t s, size_t d) {
  109. // Algorithm:
  110. //
  111. // Order the adj faces around the edge (s,d) clockwise using
  112. // query point as pivot. (i.e. The first face of the ordering
  113. // is directly after the pivot point, and the last face is
  114. // directly before the pivot.)
  115. //
  116. // The point is outside if the first and last faces of the
  117. // ordering forms a convex angle. This check can be done
  118. // without any construction by looking at the orientation of the
  119. // faces. The angle is convex iff the first face contains (s,d)
  120. // as an edge and the last face contains (d,s) as an edge.
  121. //
  122. // The point is inside if the first and last faces of the
  123. // ordering forms a concave angle. That is the first face
  124. // contains (d,s) as an edge and the last face contains (s,d) as
  125. // an edge.
  126. //
  127. // In the special case of duplicated faces. I.e. multiple faces
  128. // sharing the same 3 corners, but not necessarily the same
  129. // orientation. The ordering will always rank faces containing
  130. // edge (s,d) before faces containing edge (d,s).
  131. //
  132. // Therefore, if there are any duplicates of the first faces,
  133. // the ordering will always choose the one with edge (s,d) if
  134. // possible. The same for the last face.
  135. //
  136. // In the very degenerated case where the first and last face
  137. // are duplicates, but with different orientations, it is
  138. // equally valid to think the angle formed by them is either 0
  139. // or 360 degrees. By default, 0 degree is used, and thus the
  140. // query point is outside.
  141. std::vector<int> adj_faces;
  142. extract_adj_faces(F, I, s, d, adj_faces);
  143. const size_t num_adj_faces = adj_faces.size();
  144. assert(num_adj_faces > 0);
  145. DerivedV pivot_point(1, 3);
  146. igl::copyleft::cgal::assign_scalar(query.x(), pivot_point(0, 0));
  147. igl::copyleft::cgal::assign_scalar(query.y(), pivot_point(0, 1));
  148. igl::copyleft::cgal::assign_scalar(query.z(), pivot_point(0, 2));
  149. Eigen::VectorXi order;
  150. order_facets_around_edge(V, F, s, d,
  151. adj_faces, pivot_point, order);
  152. assert((size_t)order.size() == num_adj_faces);
  153. if (adj_faces[order[0]] > 0 &&
  154. adj_faces[order[num_adj_faces-1] < 0]) {
  155. return true;
  156. } else if (adj_faces[order[0]] < 0 &&
  157. adj_faces[order[num_adj_faces-1] > 0]) {
  158. return false;
  159. } else {
  160. throw "The input mesh does not represent a valid volume";
  161. }
  162. throw "The input mesh does not represent a valid volume";
  163. return false;
  164. }
  165. template<typename DerivedV, typename DerivedF, typename DerivedI>
  166. bool determine_point_vertex_orientation(
  167. const Eigen::PlainObjectBase<DerivedV>& V,
  168. const Eigen::PlainObjectBase<DerivedF>& F,
  169. const Eigen::PlainObjectBase<DerivedI>& I,
  170. const Point_3& query, size_t s) {
  171. std::vector<int> adj_vertices;
  172. extract_adj_vertices(F, I, s, adj_vertices);
  173. const size_t num_adj_vertices = adj_vertices.size();
  174. std::vector<Point_3> adj_points;
  175. for (size_t i=0; i<num_adj_vertices; i++) {
  176. const size_t vi = adj_vertices[i];
  177. adj_points.emplace_back(V(vi,0), V(vi,1), V(vi,2));
  178. }
  179. // A plane is on the exterior if all adj_points lies on or to
  180. // one side of the plane.
  181. auto is_on_exterior = [&](const Plane_3& separator) {
  182. size_t positive=0;
  183. size_t negative=0;
  184. size_t coplanar=0;
  185. for (const auto& point : adj_points) {
  186. switch(separator.oriented_side(point)) {
  187. case CGAL::ON_POSITIVE_SIDE:
  188. positive++;
  189. break;
  190. case CGAL::ON_NEGATIVE_SIDE:
  191. negative++;
  192. break;
  193. case CGAL::ON_ORIENTED_BOUNDARY:
  194. coplanar++;
  195. break;
  196. default:
  197. throw "Unknown plane-point orientation";
  198. }
  199. }
  200. auto query_orientation = separator.oriented_side(query);
  201. bool r =
  202. (positive == 0 && query_orientation == CGAL::POSITIVE)
  203. ||
  204. (negative == 0 && query_orientation == CGAL::NEGATIVE);
  205. return r;
  206. };
  207. size_t d = std::numeric_limits<size_t>::max();
  208. Point_3 p(V(s,0), V(s,1), V(s,2));
  209. for (size_t i=0; i<num_adj_vertices; i++) {
  210. const size_t vi = adj_vertices[i];
  211. for (size_t j=i+1; j<num_adj_vertices; j++) {
  212. Plane_3 separator(p, adj_points[i], adj_points[j]);
  213. if (separator.is_degenerate()) {
  214. throw "Input mesh contains degenerated faces";
  215. }
  216. if (is_on_exterior(separator)) {
  217. d = vi;
  218. assert(!CGAL::collinear(p, adj_points[i], query));
  219. break;
  220. }
  221. }
  222. if (d < (size_t)V.rows()) break;
  223. }
  224. if (d > (size_t)V.rows()) {
  225. // All adj faces are coplanar, use the first edge.
  226. d = adj_vertices[0];
  227. }
  228. return determine_point_edge_orientation(V, F, I, query, s, d);
  229. }
  230. template<typename DerivedV, typename DerivedF, typename DerivedI>
  231. bool determine_point_face_orientation(
  232. const Eigen::PlainObjectBase<DerivedV>& V,
  233. const Eigen::PlainObjectBase<DerivedF>& F,
  234. const Eigen::PlainObjectBase<DerivedI>& I,
  235. const Point_3& query, size_t fid) {
  236. // Algorithm: A point is on the inside of a face if the
  237. // tetrahedron formed by them is negatively oriented.
  238. Eigen::Vector3i f = F.row(I(fid, 0));
  239. const Point_3 v0(V(f[0], 0), V(f[0], 1), V(f[0], 2));
  240. const Point_3 v1(V(f[1], 0), V(f[1], 1), V(f[1], 2));
  241. const Point_3 v2(V(f[2], 0), V(f[2], 1), V(f[2], 2));
  242. auto result = CGAL::orientation(v0, v1, v2, query);
  243. if (result == CGAL::COPLANAR) {
  244. throw "Cannot determine inside/outside because query point lies exactly on the input surface.";
  245. }
  246. return result == CGAL::NEGATIVE;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. template<typename DerivedV, typename DerivedF, typename DerivedI,
  253. typename DerivedP, typename DerivedB>
  254. IGL_INLINE void igl::copyleft::cgal::points_inside_component(
  255. const Eigen::PlainObjectBase<DerivedV>& V,
  256. const Eigen::PlainObjectBase<DerivedF>& F,
  257. const Eigen::PlainObjectBase<DerivedI>& I,
  258. const Eigen::PlainObjectBase<DerivedP>& P,
  259. Eigen::PlainObjectBase<DerivedB>& inside) {
  260. using namespace igl::copyleft::cgal::points_inside_component_helper;
  261. if (F.rows() <= 0 || I.rows() <= 0) {
  262. throw "Inside check cannot be done on empty facet component.";
  263. }
  264. const size_t num_faces = I.rows();
  265. std::vector<Triangle> triangles;
  266. for (size_t i=0; i<num_faces; i++) {
  267. const Eigen::Vector3i f = F.row(I(i, 0));
  268. triangles.emplace_back(
  269. Point_3(V(f[0], 0), V(f[0], 1), V(f[0], 2)),
  270. Point_3(V(f[1], 0), V(f[1], 1), V(f[1], 2)),
  271. Point_3(V(f[2], 0), V(f[2], 1), V(f[2], 2)));
  272. if (triangles.back().is_degenerate()) {
  273. throw "Input facet components contains degenerated triangles";
  274. }
  275. }
  276. Tree tree(triangles.begin(), triangles.end());
  277. tree.accelerate_distance_queries();
  278. const size_t num_queries = P.rows();
  279. inside.resize(num_queries, 1);
  280. for (size_t i=0; i<num_queries; i++) {
  281. const Point_3 query(P(i,0), P(i,1), P(i,2));
  282. auto projection = tree.closest_point_and_primitive(query);
  283. auto closest_point = projection.first;
  284. size_t fid = projection.second - triangles.begin();
  285. size_t element_index;
  286. switch (determine_element_type(
  287. V, F, I, fid, closest_point, element_index)) {
  288. case VERTEX:
  289. {
  290. const size_t s = F(I(fid, 0), element_index);
  291. inside(i,0) = determine_point_vertex_orientation(
  292. V, F, I, query, s);
  293. }
  294. break;
  295. case EDGE:
  296. {
  297. const size_t s = F(I(fid, 0), (element_index+1)%3);
  298. const size_t d = F(I(fid, 0), (element_index+2)%3);
  299. inside(i,0) = determine_point_edge_orientation(
  300. V, F, I, query, s, d);
  301. }
  302. break;
  303. case FACE:
  304. inside(i,0) = determine_point_face_orientation(V, F, I, query, fid);
  305. break;
  306. default:
  307. throw "Unknow closest element type!";
  308. }
  309. }
  310. }
  311. template<typename DerivedV, typename DerivedF, typename DerivedP,
  312. typename DerivedB>
  313. IGL_INLINE void igl::copyleft::cgal::points_inside_component(
  314. const Eigen::PlainObjectBase<DerivedV>& V,
  315. const Eigen::PlainObjectBase<DerivedF>& F,
  316. const Eigen::PlainObjectBase<DerivedP>& P,
  317. Eigen::PlainObjectBase<DerivedB>& inside) {
  318. Eigen::VectorXi I(F.rows());
  319. I.setLinSpaced(F.rows(), 0, F.rows()-1);
  320. igl::copyleft::cgal::points_inside_component(V, F, I, P, inside);
  321. }
  322. #ifdef IGL_STATIC_LIBRARY
  323. // Explicit template specialization
  324. template void igl::copyleft::cgal::points_inside_component< Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix< int, -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> > ( 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&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix< int, -1, -1, 0, -1, -1> >&);
  325. template void igl::copyleft::cgal::points_inside_component< 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> > ( 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> >&);
  326. template void igl::copyleft::cgal::points_inside_component<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -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<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  327. template void igl::copyleft::cgal::points_inside_component<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<int, -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<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  328. template void igl::copyleft::cgal::points_inside_component<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> >(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> >&);
  329. #endif