ContPointInPolyhedron3.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <Mathematics/ContPointInPolygon2.h>
  10. #include <Mathematics/IntrRay3Plane3.h>
  11. #include <Mathematics/IntrRay3Triangle3.h>
  12. #include <vector>
  13. // This class contains various implementations for point-in-polyhedron
  14. // queries. The planes stored with the faces are used in all cases to
  15. // reject ray-face intersection tests, a quick culling operation.
  16. //
  17. // The algorithm is to cast a ray from the input point P and test for
  18. // intersection against each face of the polyhedron. If the ray only
  19. // intersects faces at interior points (not vertices, not edge points),
  20. // then the point is inside when the number of intersections is odd and
  21. // the point is outside when the number of intersections is even. If the
  22. // ray intersects an edge or a vertex, then the counting must be handled
  23. // differently. The details are tedious. As an alternative, the approach
  24. // here is to allow you to specify 2*N+1 rays, where N >= 0. You should
  25. // choose these rays randomly. Each ray reports "inside" or "outside".
  26. // Whichever result occurs N+1 or more times is the "winner". The input
  27. // rayQuantity is 2*N+1. The input array Direction must have rayQuantity
  28. // elements. If you are feeling lucky, choose rayQuantity to be 1.
  29. namespace WwiseGTE
  30. {
  31. template <typename Real>
  32. class PointInPolyhedron3
  33. {
  34. public:
  35. // For simple polyhedra with triangle faces.
  36. class TriangleFace
  37. {
  38. public:
  39. // When you view the face from outside, the vertices are
  40. // counterclockwise ordered. The indices array stores the indices
  41. // into the vertex array.
  42. std::array<int, 3> indices;
  43. // The normal vector is unit length and points to the outside of
  44. // the polyhedron.
  45. Plane3<Real> plane;
  46. };
  47. // The Contains query will use ray-triangle intersection queries.
  48. PointInPolyhedron3(int numPoints, Vector3<Real> const* points,
  49. int numFaces, TriangleFace const* faces, int numRays,
  50. Vector3<Real> const* directions)
  51. :
  52. mNumPoints(numPoints),
  53. mPoints(points),
  54. mNumFaces(numFaces),
  55. mTFaces(faces),
  56. mCFaces(nullptr),
  57. mSFaces(nullptr),
  58. mMethod(0),
  59. mNumRays(numRays),
  60. mDirections(directions)
  61. {
  62. }
  63. // For simple polyhedra with convex polygon faces.
  64. class ConvexFace
  65. {
  66. public:
  67. // When you view the face from outside, the vertices are
  68. // counterclockwise ordered. The indices array stores the indices
  69. // into the vertex array.
  70. std::vector<int> indices;
  71. // The normal vector is unit length and points to the outside of
  72. // the polyhedron.
  73. Plane3<Real> plane;
  74. };
  75. // The Contains() query will use ray-convexpolygon intersection
  76. // queries. A ray-convexpolygon intersection query can be implemented
  77. // in many ways. In this context, uiMethod is one of three value:
  78. // 0 : Use a triangle fan and perform a ray-triangle intersection
  79. // query for each triangle.
  80. // 1 : Find the point of intersection of ray and plane of polygon.
  81. // Test whether that point is inside the convex polygon using an
  82. // O(N) test.
  83. // 2 : Find the point of intersection of ray and plane of polygon.
  84. // Test whether that point is inside the convex polygon using an
  85. // O(log N) test.
  86. PointInPolyhedron3(int numPoints, Vector3<Real> const* points,
  87. int numFaces, ConvexFace const* faces, int numRays,
  88. Vector3<Real> const* directions, unsigned int method)
  89. :
  90. mNumPoints(numPoints),
  91. mPoints(points),
  92. mNumFaces(numFaces),
  93. mTFaces(nullptr),
  94. mCFaces(faces),
  95. mSFaces(nullptr),
  96. mMethod(method),
  97. mNumRays(numRays),
  98. mDirections(directions)
  99. {
  100. }
  101. // For simple polyhedra with simple polygon faces that are generally
  102. // not all convex.
  103. class SimpleFace
  104. {
  105. public:
  106. // When you view the face from outside, the vertices are
  107. // counterclockwise ordered. The Indices array stores the indices
  108. // into the vertex array.
  109. std::vector<int> indices;
  110. // The normal vector is unit length and points to the outside of
  111. // the polyhedron.
  112. Plane3<Real> plane;
  113. // Each simple face may be triangulated. The indices are relative
  114. // to the vertex array. Each triple of indices represents a
  115. // triangle in the triangulation.
  116. std::vector<int> triangles;
  117. };
  118. // The Contains query will use ray-simplepolygon intersection queries.
  119. // A ray-simplepolygon intersection query can be implemented in a
  120. // couple of ways. In this context, uiMethod is one of two value:
  121. // 0 : Iterate over the triangles of each face and perform a
  122. // ray-triangle intersection query for each triangle. This
  123. // requires that the SimpleFace::Triangles array be initialized
  124. // for each face.
  125. // 1 : Find the point of intersection of ray and plane of polygon.
  126. // Test whether that point is inside the polygon using an O(N)
  127. // test. The SimpleFace::Triangles array is not used for this
  128. // method, so it does not have to be initialized for each face.
  129. PointInPolyhedron3(int numPoints, Vector3<Real> const* points,
  130. int numFaces, SimpleFace const* faces, int numRays,
  131. Vector3<Real> const* directions, unsigned int method)
  132. :
  133. mNumPoints(numPoints),
  134. mPoints(points),
  135. mNumFaces(numFaces),
  136. mTFaces(nullptr),
  137. mCFaces(nullptr),
  138. mSFaces(faces),
  139. mMethod(method),
  140. mNumRays(numRays),
  141. mDirections(directions)
  142. {
  143. }
  144. // This function will select the actual algorithm based on which
  145. // constructor you used for this class.
  146. bool Contains(Vector3<Real> const& p) const
  147. {
  148. if (mTFaces)
  149. {
  150. return ContainsT0(p);
  151. }
  152. if (mCFaces)
  153. {
  154. if (mMethod == 0)
  155. {
  156. return ContainsC0(p);
  157. }
  158. return ContainsC1C2(p, mMethod);
  159. }
  160. if (mSFaces)
  161. {
  162. if (mMethod == 0)
  163. {
  164. return ContainsS0(p);
  165. }
  166. if (mMethod == 1)
  167. {
  168. return ContainsS1(p);
  169. }
  170. }
  171. return false;
  172. }
  173. private:
  174. // For all types of faces. The ray origin is the test point. The ray
  175. // direction is one of those passed to the constructors. The plane
  176. // origin is a point on the plane of the face. The plane normal is a
  177. // unit-length normal to the face and that points outside the
  178. // polyhedron.
  179. static bool FastNoIntersect(Ray3<Real> const& ray, Plane3<Real> const& plane)
  180. {
  181. Real planeDistance = Dot(plane.normal, ray.origin) - plane.constant;
  182. Real planeAngle = Dot(plane.normal, ray.direction);
  183. if (planeDistance < (Real)0)
  184. {
  185. // The ray origin is on the negative side of the plane.
  186. if (planeAngle <= (Real)0)
  187. {
  188. // The ray points away from the plane.
  189. return true;
  190. }
  191. }
  192. if (planeDistance > (Real)0)
  193. {
  194. // The ray origin is on the positive side of the plane.
  195. if (planeAngle >= (Real)0)
  196. {
  197. // The ray points away from the plane.
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. // For triangle faces.
  204. bool ContainsT0(Vector3<Real> const& p) const
  205. {
  206. int insideCount = 0;
  207. TIQuery<Real, Ray3<Real>, Triangle3<Real>> rtQuery;
  208. Triangle3<Real> triangle;
  209. Ray3<Real> ray;
  210. ray.origin = p;
  211. for (int j = 0; j < mNumRays; ++j)
  212. {
  213. ray.direction = mDirections[j];
  214. // Zero intersections to start with.
  215. bool odd = false;
  216. TriangleFace const* face = mTFaces;
  217. for (int i = 0; i < mNumFaces; ++i, ++face)
  218. {
  219. // Attempt to quickly cull the triangle.
  220. if (FastNoIntersect(ray, face->plane))
  221. {
  222. continue;
  223. }
  224. // Get the triangle vertices.
  225. for (int k = 0; k < 3; ++k)
  226. {
  227. triangle.v[k] = mPoints[face->indices[k]];
  228. }
  229. // Test for intersection.
  230. if (rtQuery(ray, triangle).intersect)
  231. {
  232. // The ray intersects the triangle.
  233. odd = !odd;
  234. }
  235. }
  236. if (odd)
  237. {
  238. insideCount++;
  239. }
  240. }
  241. return insideCount > mNumRays / 2;
  242. }
  243. // For convex faces.
  244. bool ContainsC0(Vector3<Real> const& p) const
  245. {
  246. int insideCount = 0;
  247. TIQuery<Real, Ray3<Real>, Triangle3<Real>> rtQuery;
  248. Triangle3<Real> triangle;
  249. Ray3<Real> ray;
  250. ray.origin = p;
  251. for (int j = 0; j < mNumRays; ++j)
  252. {
  253. ray.direction = mDirections[j];
  254. // Zero intersections to start with.
  255. bool odd = false;
  256. ConvexFace const* face = mCFaces;
  257. for (int i = 0; i < mNumFaces; ++i, ++face)
  258. {
  259. // Attempt to quickly cull the triangle.
  260. if (FastNoIntersect(ray, face->plane))
  261. {
  262. continue;
  263. }
  264. // Process the triangles in a trifan of the face.
  265. size_t numVerticesM1 = face->indices.size() - 1;
  266. triangle.v[0] = mPoints[face->indices[0]];
  267. for (size_t k = 1; k < numVerticesM1; ++k)
  268. {
  269. triangle.v[1] = mPoints[face->indices[k]];
  270. triangle.v[2] = mPoints[face->indices[k + 1]];
  271. if (rtQuery(ray, triangle).intersect)
  272. {
  273. // The ray intersects the triangle.
  274. odd = !odd;
  275. }
  276. }
  277. }
  278. if (odd)
  279. {
  280. insideCount++;
  281. }
  282. }
  283. return insideCount > mNumRays / 2;
  284. }
  285. bool ContainsC1C2(Vector3<Real> const& p, unsigned int method) const
  286. {
  287. int insideCount = 0;
  288. FIQuery<Real, Ray3<Real>, Plane3<Real>> rpQuery;
  289. Ray3<Real> ray;
  290. ray.origin = p;
  291. for (int j = 0; j < mNumRays; ++j)
  292. {
  293. ray.direction = mDirections[j];
  294. // Zero intersections to start with.
  295. bool odd = false;
  296. ConvexFace const* face = mCFaces;
  297. for (int i = 0; i < mNumFaces; ++i, ++face)
  298. {
  299. // Attempt to quickly cull the triangle.
  300. if (FastNoIntersect(ray, face->plane))
  301. {
  302. continue;
  303. }
  304. // Compute the ray-plane intersection.
  305. auto result = rpQuery(ray, face->plane);
  306. // If you trigger this assertion, numerical round-off
  307. // errors have led to a discrepancy between
  308. // FastNoIntersect and the Find() result.
  309. LogAssert(result.intersect, "Unexpected condition.");
  310. // Get a coordinate system for the plane. Use vertex 0
  311. // as the origin.
  312. Vector3<Real> const& V0 = mPoints[face->indices[0]];
  313. Vector3<Real> basis[3];
  314. basis[0] = face->plane.normal;
  315. ComputeOrthogonalComplement(1, basis);
  316. // Project the intersection onto the plane.
  317. Vector3<Real> diff = result.point - V0;
  318. Vector2<Real> projIntersect{ Dot(basis[1], diff), Dot(basis[2], diff) };
  319. // Project the face vertices onto the plane of the face.
  320. if (face->indices.size() > mProjVertices.size())
  321. {
  322. mProjVertices.resize(face->indices.size());
  323. }
  324. // Project the remaining vertices. Vertex 0 is always the
  325. // origin.
  326. size_t numIndices = face->indices.size();
  327. mProjVertices[0] = Vector2<Real>::Zero();
  328. for (size_t k = 1; k < numIndices; ++k)
  329. {
  330. diff = mPoints[face->indices[k]] - V0;
  331. mProjVertices[k][0] = Dot(basis[1], diff);
  332. mProjVertices[k][1] = Dot(basis[2], diff);
  333. }
  334. // Test whether the intersection point is in the convex
  335. // polygon.
  336. PointInPolygon2<Real> PIP(static_cast<int>(mProjVertices.size()),
  337. &mProjVertices[0]);
  338. if (method == 1)
  339. {
  340. if (PIP.ContainsConvexOrderN(projIntersect))
  341. {
  342. // The ray intersects the triangle.
  343. odd = !odd;
  344. }
  345. }
  346. else
  347. {
  348. if (PIP.ContainsConvexOrderLogN(projIntersect))
  349. {
  350. // The ray intersects the triangle.
  351. odd = !odd;
  352. }
  353. }
  354. }
  355. if (odd)
  356. {
  357. insideCount++;
  358. }
  359. }
  360. return insideCount > mNumRays / 2;
  361. }
  362. // For simple faces.
  363. bool ContainsS0(Vector3<Real> const& p) const
  364. {
  365. int insideCount = 0;
  366. TIQuery<Real, Ray3<Real>, Triangle3<Real>> rtQuery;
  367. Triangle3<Real> triangle;
  368. Ray3<Real> ray;
  369. ray.origin = p;
  370. for (int j = 0; j < mNumRays; ++j)
  371. {
  372. ray.direction = mDirections[j];
  373. // Zero intersections to start with.
  374. bool odd = false;
  375. SimpleFace const* face = mSFaces;
  376. for (int i = 0; i < mNumFaces; ++i, ++face)
  377. {
  378. // Attempt to quickly cull the triangle.
  379. if (FastNoIntersect(ray, face->plane))
  380. {
  381. continue;
  382. }
  383. // The triangulation must exist to use it.
  384. size_t numTriangles = face->triangles.size() / 3;
  385. LogAssert(numTriangles > 0, "Triangulation must exist.");
  386. // Process the triangles in a triangulation of the face.
  387. int const* currIndex = &face->triangles[0];
  388. for (size_t t = 0; t < numTriangles; ++t)
  389. {
  390. // Get the triangle vertices.
  391. for (int k = 0; k < 3; ++k)
  392. {
  393. triangle.v[k] = mPoints[*currIndex++];
  394. }
  395. // Test for intersection.
  396. if (rtQuery(ray, triangle).intersect)
  397. {
  398. // The ray intersects the triangle.
  399. odd = !odd;
  400. }
  401. }
  402. }
  403. if (odd)
  404. {
  405. insideCount++;
  406. }
  407. }
  408. return insideCount > mNumRays / 2;
  409. }
  410. bool ContainsS1(Vector3<Real> const& p) const
  411. {
  412. int insideCount = 0;
  413. FIQuery<Real, Ray3<Real>, Plane3<Real>> rpQuery;
  414. Ray3<Real> ray;
  415. ray.origin = p;
  416. for (int j = 0; j < mNumRays; ++j)
  417. {
  418. ray.direction = mDirections[j];
  419. // Zero intersections to start with.
  420. bool odd = false;
  421. SimpleFace const* face = mSFaces;
  422. for (int i = 0; i < mNumFaces; ++i, ++face)
  423. {
  424. // Attempt to quickly cull the triangle.
  425. if (FastNoIntersect(ray, face->plane))
  426. {
  427. continue;
  428. }
  429. // Compute the ray-plane intersection.
  430. auto result = rpQuery(ray, face->plane);
  431. // If you trigger this assertion, numerical round-off
  432. // errors have led to a discrepancy between
  433. // FastNoIntersect and the Find() result.
  434. LogAssert(result.intersect, "Unexpected condition.");
  435. // Get a coordinate system for the plane. Use vertex 0
  436. // as the origin.
  437. Vector3<Real> const& V0 = mPoints[face->indices[0]];
  438. Vector3<Real> basis[3];
  439. basis[0] = face->plane.normal;
  440. ComputeOrthogonalComplement(1, basis);
  441. // Project the intersection onto the plane.
  442. Vector3<Real> diff = result.point - V0;
  443. Vector2<Real> projIntersect{ Dot(basis[1], diff), Dot(basis[2], diff) };
  444. // Project the face vertices onto the plane of the face.
  445. if (face->indices.size() > mProjVertices.size())
  446. {
  447. mProjVertices.resize(face->indices.size());
  448. }
  449. // Project the remaining vertices. Vertex 0 is always the
  450. // origin.
  451. size_t numIndices = face->indices.size();
  452. mProjVertices[0] = Vector2<Real>::Zero();
  453. for (size_t k = 1; k < numIndices; ++k)
  454. {
  455. diff = mPoints[face->indices[k]] - V0;
  456. mProjVertices[k][0] = Dot(basis[1], diff);
  457. mProjVertices[k][1] = Dot(basis[2], diff);
  458. }
  459. // Test whether the intersection point is in the convex
  460. // polygon.
  461. PointInPolygon2<Real> PIP(static_cast<int>(mProjVertices.size()),
  462. &mProjVertices[0]);
  463. if (PIP.Contains(projIntersect))
  464. {
  465. // The ray intersects the triangle.
  466. odd = !odd;
  467. }
  468. }
  469. if (odd)
  470. {
  471. insideCount++;
  472. }
  473. }
  474. return insideCount > mNumRays / 2;
  475. }
  476. int mNumPoints;
  477. Vector3<Real> const* mPoints;
  478. int mNumFaces;
  479. TriangleFace const* mTFaces;
  480. ConvexFace const* mCFaces;
  481. SimpleFace const* mSFaces;
  482. unsigned int mMethod;
  483. int mNumRays;
  484. Vector3<Real> const* mDirections;
  485. // Temporary storage for those methods that reduce the problem to 2D
  486. // point-in-polygon queries. The array stores the projections of
  487. // face vertices onto the plane of the face. It is resized as needed.
  488. mutable std::vector<Vector2<Real>> mProjVertices;
  489. };
  490. }