SplitMeshByPlane.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/DistPoint3Plane3.h>
  9. #include <Mathematics/EdgeKey.h>
  10. #include <map>
  11. // The algorithm for splitting a mesh by a plane is described in
  12. // https://www.geometrictools.com/Documentation/ClipMesh.pdf
  13. // Currently, the code here does not include generating a closed
  14. // mesh (from the "positive" and "zero" vertices) by attaching
  15. // triangulated faces to the mesh, where the those faces live in
  16. // the splitting plane. (TODO: Add this code.)
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class SplitMeshByPlane
  21. {
  22. public:
  23. // The 'indices' are lookups into the 'vertices' array. The indices
  24. // represent a triangle mesh. The number of indices must be a
  25. // multiple of 3, each triple representing a triangle. If t is a
  26. // triangle index, then the triangle is formed by
  27. // vertices[indices[3 * t + i]] for 0 <= i <= 2. The outputs
  28. // 'negIndices' and 'posIndices' are formatted similarly.
  29. void operator()(
  30. std::vector<Vector3<Real>> const& vertices,
  31. std::vector<int> const& indices,
  32. Plane3<Real> const& plane,
  33. std::vector<Vector3<Real>>& clipVertices,
  34. std::vector<int>& negIndices,
  35. std::vector<int>& posIndices)
  36. {
  37. mSignedDistances.resize(vertices.size());
  38. mEMap.clear();
  39. // Make a copy of the incoming vertices. If the mesh intersects
  40. // the plane, new vertices must be generated. These are appended
  41. // to the clipVertices array.
  42. clipVertices = vertices;
  43. ClassifyVertices(clipVertices, plane);
  44. ClassifyEdges(clipVertices, indices);
  45. ClassifyTriangles(indices, negIndices, posIndices);
  46. }
  47. private:
  48. void ClassifyVertices(std::vector<Vector3<Real>> const& clipVertices,
  49. Plane3<Real> const& plane)
  50. {
  51. DCPQuery<Real, Vector3<Real>, Plane3<Real>> query;
  52. for (size_t i = 0; i < clipVertices.size(); ++i)
  53. {
  54. mSignedDistances[i] = query(clipVertices[i], plane).signedDistance;
  55. }
  56. }
  57. void ClassifyEdges(std::vector<Vector3<Real>>& clipVertices,
  58. std::vector<int> const& indices)
  59. {
  60. int const numTriangles = static_cast<int>(indices.size() / 3);
  61. int nextIndex = static_cast<int>(clipVertices.size());
  62. for (int i = 0; i < numTriangles; ++i)
  63. {
  64. int v0 = indices[3 * i + 0];
  65. int v1 = indices[3 * i + 1];
  66. int v2 = indices[3 * i + 2];
  67. Real sDist0 = mSignedDistances[v0];
  68. Real sDist1 = mSignedDistances[v1];
  69. Real sDist2 = mSignedDistances[v2];
  70. EdgeKey<false> key;
  71. Real t;
  72. Vector3<Real> intr, diff;
  73. // The change-in-sign tests are structured this way to avoid
  74. // numerical round-off problems. For example, sDist0 > 0 and
  75. // sDist1 < 0, but both are very small and sDist0 * sDist1 = 0
  76. // because of round-off errors. The tests also guarantee
  77. // consistency between this function and ClassifyTriangles,
  78. // the latter function using sign tests only on the individual
  79. // sDist values.
  80. if ((sDist0 > (Real)0 && sDist1 < (Real)0)
  81. || (sDist0 < (Real)0 && sDist1 >(Real)0))
  82. {
  83. key = EdgeKey<false>(v0, v1);
  84. if (mEMap.find(key) == mEMap.end())
  85. {
  86. t = sDist0 / (sDist0 - sDist1);
  87. diff = clipVertices[v1] - clipVertices[v0];
  88. intr = clipVertices[v0] + t * diff;
  89. clipVertices.push_back(intr);
  90. mEMap[key] = std::make_pair(intr, nextIndex);
  91. ++nextIndex;
  92. }
  93. }
  94. if ((sDist1 > (Real)0 && sDist2 < (Real)0)
  95. || (sDist1 < (Real)0 && sDist2 >(Real)0))
  96. {
  97. key = EdgeKey<false>(v1, v2);
  98. if (mEMap.find(key) == mEMap.end())
  99. {
  100. t = sDist1 / (sDist1 - sDist2);
  101. diff = clipVertices[v2] - clipVertices[v1];
  102. intr = clipVertices[v1] + t * diff;
  103. clipVertices.push_back(intr);
  104. mEMap[key] = std::make_pair(intr, nextIndex);
  105. ++nextIndex;
  106. }
  107. }
  108. if ((sDist2 > (Real)0 && sDist0 < (Real)0)
  109. || (sDist2 < (Real)0 && sDist0 >(Real)0))
  110. {
  111. key = EdgeKey<false>(v2, v0);
  112. if (mEMap.find(key) == mEMap.end())
  113. {
  114. t = sDist2 / (sDist2 - sDist0);
  115. diff = clipVertices[v0] - clipVertices[v2];
  116. intr = clipVertices[v2] + t * diff;
  117. clipVertices.push_back(intr);
  118. mEMap[key] = std::make_pair(intr, nextIndex);
  119. ++nextIndex;
  120. }
  121. }
  122. }
  123. }
  124. void ClassifyTriangles(std::vector<int> const& indices,
  125. std::vector<int>& negIndices, std::vector<int>& posIndices)
  126. {
  127. int const numTriangles = static_cast<int>(indices.size() / 3);
  128. for (int i = 0; i < numTriangles; ++i)
  129. {
  130. int v0 = indices[3 * i + 0];
  131. int v1 = indices[3 * i + 1];
  132. int v2 = indices[3 * i + 2];
  133. Real sDist0 = mSignedDistances[v0];
  134. Real sDist1 = mSignedDistances[v1];
  135. Real sDist2 = mSignedDistances[v2];
  136. if (sDist0 > (Real)0)
  137. {
  138. if (sDist1 > (Real)0)
  139. {
  140. if (sDist2 > (Real)0)
  141. {
  142. // +++
  143. AppendTriangle(posIndices, v0, v1, v2);
  144. }
  145. else if (sDist2 < (Real)0)
  146. {
  147. // ++-
  148. SplitTrianglePPM(negIndices, posIndices, v0, v1, v2);
  149. }
  150. else
  151. {
  152. // ++0
  153. AppendTriangle(posIndices, v0, v1, v2);
  154. }
  155. }
  156. else if (sDist1 < (Real)0)
  157. {
  158. if (sDist2 > (Real)0)
  159. {
  160. // +-+
  161. SplitTrianglePPM(negIndices, posIndices, v2, v0, v1);
  162. }
  163. else if (sDist2 < (Real)0)
  164. {
  165. // +--
  166. SplitTriangleMMP(negIndices, posIndices, v1, v2, v0);
  167. }
  168. else
  169. {
  170. // +-0
  171. SplitTrianglePMZ(negIndices, posIndices, v0, v1, v2);
  172. }
  173. }
  174. else
  175. {
  176. if (sDist2 > (Real)0)
  177. {
  178. // +0+
  179. AppendTriangle(posIndices, v0, v1, v2);
  180. }
  181. else if (sDist2 < (Real)0)
  182. {
  183. // +0-
  184. SplitTriangleMPZ(negIndices, posIndices, v2, v0, v1);
  185. }
  186. else
  187. {
  188. // +00
  189. AppendTriangle(posIndices, v0, v1, v2);
  190. }
  191. }
  192. }
  193. else if (sDist0 < (Real)0)
  194. {
  195. if (sDist1 > (Real)0)
  196. {
  197. if (sDist2 > (Real)0)
  198. {
  199. // -++
  200. SplitTrianglePPM(negIndices, posIndices, v1, v2, v0);
  201. }
  202. else if (sDist2 < (Real)0)
  203. {
  204. // -+-
  205. SplitTriangleMMP(negIndices, posIndices, v2, v0, v1);
  206. }
  207. else
  208. {
  209. // -+0
  210. SplitTriangleMPZ(negIndices, posIndices, v0, v1, v2);
  211. }
  212. }
  213. else if (sDist1 < (Real)0)
  214. {
  215. if (sDist2 > (Real)0)
  216. {
  217. // --+
  218. SplitTriangleMMP(negIndices, posIndices, v0, v1, v2);
  219. }
  220. else if (sDist2 < (Real)0)
  221. {
  222. // ---
  223. AppendTriangle(negIndices, v0, v1, v2);
  224. }
  225. else
  226. {
  227. // --0
  228. AppendTriangle(negIndices, v0, v1, v2);
  229. }
  230. }
  231. else
  232. {
  233. if (sDist2 > (Real)0)
  234. {
  235. // -0+
  236. SplitTrianglePMZ(negIndices, posIndices, v2, v0, v1);
  237. }
  238. else if (sDist2 < (Real)0)
  239. {
  240. // -0-
  241. AppendTriangle(negIndices, v0, v1, v2);
  242. }
  243. else
  244. {
  245. // -00
  246. AppendTriangle(negIndices, v0, v1, v2);
  247. }
  248. }
  249. }
  250. else
  251. {
  252. if (sDist1 > (Real)0)
  253. {
  254. if (sDist2 > (Real)0)
  255. {
  256. // 0++
  257. AppendTriangle(posIndices, v0, v1, v2);
  258. }
  259. else if (sDist2 < (Real)0)
  260. {
  261. // 0+-
  262. SplitTrianglePMZ(negIndices, posIndices, v1, v2, v0);
  263. }
  264. else
  265. {
  266. // 0+0
  267. AppendTriangle(posIndices, v0, v1, v2);
  268. }
  269. }
  270. else if (sDist1 < (Real)0)
  271. {
  272. if (sDist2 > (Real)0)
  273. {
  274. // 0-+
  275. SplitTriangleMPZ(negIndices, posIndices, v1, v2, v0);
  276. }
  277. else if (sDist2 < (Real)0)
  278. {
  279. // 0--
  280. AppendTriangle(negIndices, v0, v1, v2);
  281. }
  282. else
  283. {
  284. // 0-0
  285. AppendTriangle(negIndices, v0, v1, v2);
  286. }
  287. }
  288. else
  289. {
  290. if (sDist2 > (Real)0)
  291. {
  292. // 00+
  293. AppendTriangle(posIndices, v0, v1, v2);
  294. }
  295. else if (sDist2 < (Real)0)
  296. {
  297. // 00-
  298. AppendTriangle(negIndices, v0, v1, v2);
  299. }
  300. else
  301. {
  302. // 000, reject triangles lying in the plane
  303. }
  304. }
  305. }
  306. }
  307. }
  308. void AppendTriangle(std::vector<int>& indices, int v0, int v1, int v2)
  309. {
  310. indices.push_back(v0);
  311. indices.push_back(v1);
  312. indices.push_back(v2);
  313. }
  314. void SplitTrianglePPM(std::vector<int>& negIndices,
  315. std::vector<int>& posIndices, int v0, int v1, int v2)
  316. {
  317. int v12 = mEMap[EdgeKey<false>(v1, v2)].second;
  318. int v20 = mEMap[EdgeKey<false>(v2, v0)].second;
  319. posIndices.push_back(v0);
  320. posIndices.push_back(v1);
  321. posIndices.push_back(v12);
  322. posIndices.push_back(v0);
  323. posIndices.push_back(v12);
  324. posIndices.push_back(v20);
  325. negIndices.push_back(v2);
  326. negIndices.push_back(v20);
  327. negIndices.push_back(v12);
  328. }
  329. void SplitTriangleMMP(std::vector<int>& negIndices,
  330. std::vector<int>& posIndices, int v0, int v1, int v2)
  331. {
  332. int v12 = mEMap[EdgeKey<false>(v1, v2)].second;
  333. int v20 = mEMap[EdgeKey<false>(v2, v0)].second;
  334. negIndices.push_back(v0);
  335. negIndices.push_back(v1);
  336. negIndices.push_back(v12);
  337. negIndices.push_back(v0);
  338. negIndices.push_back(v12);
  339. negIndices.push_back(v20);
  340. posIndices.push_back(v2);
  341. posIndices.push_back(v20);
  342. posIndices.push_back(v12);
  343. }
  344. void SplitTrianglePMZ(std::vector<int>& negIndices,
  345. std::vector<int>& posIndices, int v0, int v1, int v2)
  346. {
  347. int v01 = mEMap[EdgeKey<false>(v0, v1)].second;
  348. posIndices.push_back(v2);
  349. posIndices.push_back(v0);
  350. posIndices.push_back(v01);
  351. negIndices.push_back(v2);
  352. negIndices.push_back(v01);
  353. negIndices.push_back(v1);
  354. }
  355. void SplitTriangleMPZ(std::vector<int>& negIndices,
  356. std::vector<int>& posIndices, int v0, int v1, int v2)
  357. {
  358. int v01 = mEMap[EdgeKey<false>(v0, v1)].second;
  359. negIndices.push_back(v2);
  360. negIndices.push_back(v0);
  361. negIndices.push_back(v01);
  362. posIndices.push_back(v2);
  363. posIndices.push_back(v01);
  364. posIndices.push_back(v1);
  365. }
  366. // Stores the signed distances from the vertices to the plane.
  367. std::vector<Real> mSignedDistances;
  368. // Stores the edges whose vertices are on opposite sides of the
  369. // plane. The key is a pair of indices into the vertex array.
  370. // The value is the point of intersection of the edge with the
  371. // plane and an index into m_kVertices (the index is larger or
  372. // equal to the number of vertices of incoming rkVertices).
  373. std::map<EdgeKey<false>, std::pair<Vector3<Real>, int>> mEMap;
  374. };
  375. }