delaunay_3d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**************************************************************************/
  2. /* delaunay_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef DELAUNAY_3D_H
  31. #define DELAUNAY_3D_H
  32. #include "core/io/file_access.h"
  33. #include "core/math/aabb.h"
  34. #include "core/math/projection.h"
  35. #include "core/math/vector3.h"
  36. #include "core/templates/local_vector.h"
  37. #include "core/templates/oa_hash_map.h"
  38. #include "core/templates/vector.h"
  39. #include "core/variant/variant.h"
  40. #include "thirdparty/misc/r128.h"
  41. class Delaunay3D {
  42. struct Simplex;
  43. enum {
  44. ACCEL_GRID_SIZE = 16
  45. };
  46. struct GridPos {
  47. Vector3i pos;
  48. List<Simplex *>::Element *E = nullptr;
  49. };
  50. struct Simplex {
  51. uint32_t points[4];
  52. R128 circum_center_x;
  53. R128 circum_center_y;
  54. R128 circum_center_z;
  55. R128 circum_r2;
  56. LocalVector<GridPos> grid_positions;
  57. List<Simplex *>::Element *SE = nullptr;
  58. _FORCE_INLINE_ Simplex() {}
  59. _FORCE_INLINE_ Simplex(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d) {
  60. points[0] = p_a;
  61. points[1] = p_b;
  62. points[2] = p_c;
  63. points[3] = p_d;
  64. }
  65. };
  66. struct Triangle {
  67. uint32_t triangle[3];
  68. bool bad = false;
  69. _FORCE_INLINE_ bool operator==(const Triangle &p_triangle) const {
  70. return triangle[0] == p_triangle.triangle[0] && triangle[1] == p_triangle.triangle[1] && triangle[2] == p_triangle.triangle[2];
  71. }
  72. _FORCE_INLINE_ Triangle() {}
  73. _FORCE_INLINE_ Triangle(uint32_t p_a, uint32_t p_b, uint32_t p_c) {
  74. if (p_a > p_b) {
  75. SWAP(p_a, p_b);
  76. }
  77. if (p_b > p_c) {
  78. SWAP(p_b, p_c);
  79. }
  80. if (p_a > p_b) {
  81. SWAP(p_a, p_b);
  82. }
  83. triangle[0] = p_a;
  84. triangle[1] = p_b;
  85. triangle[2] = p_c;
  86. }
  87. };
  88. struct TriangleHasher {
  89. _FORCE_INLINE_ static uint32_t hash(const Triangle &p_triangle) {
  90. uint32_t h = hash_djb2_one_32(p_triangle.triangle[0]);
  91. h = hash_djb2_one_32(p_triangle.triangle[1], h);
  92. return hash_fmix32(hash_djb2_one_32(p_triangle.triangle[2], h));
  93. }
  94. };
  95. _FORCE_INLINE_ static void circum_sphere_compute(const Vector3 *p_points, Simplex *p_simplex) {
  96. // The only part in the algorithm where there may be precision errors is this one,
  97. // so ensure that we do it with the maximum precision possible.
  98. R128 v0_x = p_points[p_simplex->points[0]].x;
  99. R128 v0_y = p_points[p_simplex->points[0]].y;
  100. R128 v0_z = p_points[p_simplex->points[0]].z;
  101. R128 v1_x = p_points[p_simplex->points[1]].x;
  102. R128 v1_y = p_points[p_simplex->points[1]].y;
  103. R128 v1_z = p_points[p_simplex->points[1]].z;
  104. R128 v2_x = p_points[p_simplex->points[2]].x;
  105. R128 v2_y = p_points[p_simplex->points[2]].y;
  106. R128 v2_z = p_points[p_simplex->points[2]].z;
  107. R128 v3_x = p_points[p_simplex->points[3]].x;
  108. R128 v3_y = p_points[p_simplex->points[3]].y;
  109. R128 v3_z = p_points[p_simplex->points[3]].z;
  110. // Create the rows of our "unrolled" 3x3 matrix.
  111. R128 row1_x = v1_x - v0_x;
  112. R128 row1_y = v1_y - v0_y;
  113. R128 row1_z = v1_z - v0_z;
  114. R128 row2_x = v2_x - v0_x;
  115. R128 row2_y = v2_y - v0_y;
  116. R128 row2_z = v2_z - v0_z;
  117. R128 row3_x = v3_x - v0_x;
  118. R128 row3_y = v3_y - v0_y;
  119. R128 row3_z = v3_z - v0_z;
  120. R128 sq_lenght1 = row1_x * row1_x + row1_y * row1_y + row1_z * row1_z;
  121. R128 sq_lenght2 = row2_x * row2_x + row2_y * row2_y + row2_z * row2_z;
  122. R128 sq_lenght3 = row3_x * row3_x + row3_y * row3_y + row3_z * row3_z;
  123. // Compute the determinant of said matrix.
  124. R128 determinant = row1_x * (row2_y * row3_z - row3_y * row2_z) - row2_x * (row1_y * row3_z - row3_y * row1_z) + row3_x * (row1_y * row2_z - row2_y * row1_z);
  125. // Compute the volume of the tetrahedron, and precompute a scalar quantity for reuse in the formula.
  126. R128 volume = determinant / R128(6.f);
  127. R128 i12volume = R128(1.f) / (volume * R128(12.f));
  128. R128 center_x = v0_x + i12volume * ((row2_y * row3_z - row3_y * row2_z) * sq_lenght1 - (row1_y * row3_z - row3_y * row1_z) * sq_lenght2 + (row1_y * row2_z - row2_y * row1_z) * sq_lenght3);
  129. R128 center_y = v0_y + i12volume * (-(row2_x * row3_z - row3_x * row2_z) * sq_lenght1 + (row1_x * row3_z - row3_x * row1_z) * sq_lenght2 - (row1_x * row2_z - row2_x * row1_z) * sq_lenght3);
  130. R128 center_z = v0_z + i12volume * ((row2_x * row3_y - row3_x * row2_y) * sq_lenght1 - (row1_x * row3_y - row3_x * row1_y) * sq_lenght2 + (row1_x * row2_y - row2_x * row1_y) * sq_lenght3);
  131. // Once we know the center, the radius is clearly the distance to any vertex.
  132. R128 rel1_x = center_x - v0_x;
  133. R128 rel1_y = center_y - v0_y;
  134. R128 rel1_z = center_z - v0_z;
  135. R128 radius1 = rel1_x * rel1_x + rel1_y * rel1_y + rel1_z * rel1_z;
  136. p_simplex->circum_center_x = center_x;
  137. p_simplex->circum_center_y = center_y;
  138. p_simplex->circum_center_z = center_z;
  139. p_simplex->circum_r2 = radius1;
  140. }
  141. _FORCE_INLINE_ static bool simplex_contains(const Vector3 *p_points, const Simplex &p_simplex, uint32_t p_vertex) {
  142. R128 v_x = p_points[p_vertex].x;
  143. R128 v_y = p_points[p_vertex].y;
  144. R128 v_z = p_points[p_vertex].z;
  145. R128 rel2_x = p_simplex.circum_center_x - v_x;
  146. R128 rel2_y = p_simplex.circum_center_y - v_y;
  147. R128 rel2_z = p_simplex.circum_center_z - v_z;
  148. R128 radius2 = rel2_x * rel2_x + rel2_y * rel2_y + rel2_z * rel2_z;
  149. return radius2 < (p_simplex.circum_r2 - R128(0.00001));
  150. }
  151. static bool simplex_is_coplanar(const Vector3 *p_points, const Simplex &p_simplex) {
  152. Plane p(p_points[p_simplex.points[0]], p_points[p_simplex.points[1]], p_points[p_simplex.points[2]]);
  153. if (ABS(p.distance_to(p_points[p_simplex.points[3]])) < CMP_EPSILON) {
  154. return true;
  155. }
  156. Projection cm;
  157. cm.columns[0][0] = p_points[p_simplex.points[0]].x;
  158. cm.columns[0][1] = p_points[p_simplex.points[1]].x;
  159. cm.columns[0][2] = p_points[p_simplex.points[2]].x;
  160. cm.columns[0][3] = p_points[p_simplex.points[3]].x;
  161. cm.columns[1][0] = p_points[p_simplex.points[0]].y;
  162. cm.columns[1][1] = p_points[p_simplex.points[1]].y;
  163. cm.columns[1][2] = p_points[p_simplex.points[2]].y;
  164. cm.columns[1][3] = p_points[p_simplex.points[3]].y;
  165. cm.columns[2][0] = p_points[p_simplex.points[0]].z;
  166. cm.columns[2][1] = p_points[p_simplex.points[1]].z;
  167. cm.columns[2][2] = p_points[p_simplex.points[2]].z;
  168. cm.columns[2][3] = p_points[p_simplex.points[3]].z;
  169. cm.columns[3][0] = 1.0;
  170. cm.columns[3][1] = 1.0;
  171. cm.columns[3][2] = 1.0;
  172. cm.columns[3][3] = 1.0;
  173. return ABS(cm.determinant()) <= CMP_EPSILON;
  174. }
  175. public:
  176. struct OutputSimplex {
  177. uint32_t points[4];
  178. };
  179. static Vector<OutputSimplex> tetrahedralize(const Vector<Vector3> &p_points) {
  180. uint32_t point_count = p_points.size();
  181. Vector3 *points = (Vector3 *)memalloc(sizeof(Vector3) * (point_count + 4));
  182. {
  183. const Vector3 *src_points = p_points.ptr();
  184. AABB rect;
  185. for (uint32_t i = 0; i < point_count; i++) {
  186. Vector3 point = src_points[i];
  187. if (i == 0) {
  188. rect.position = point;
  189. } else {
  190. rect.expand_to(point);
  191. }
  192. points[i] = point;
  193. }
  194. for (uint32_t i = 0; i < point_count; i++) {
  195. points[i] = (points[i] - rect.position) / rect.size;
  196. }
  197. float delta_max = Math::sqrt(2.0) * 20.0;
  198. Vector3 center = Vector3(0.5, 0.5, 0.5);
  199. // any simplex that contains everything is good
  200. points[point_count + 0] = center + Vector3(0, 1, 0) * delta_max;
  201. points[point_count + 1] = center + Vector3(0, -1, 1) * delta_max;
  202. points[point_count + 2] = center + Vector3(1, -1, -1) * delta_max;
  203. points[point_count + 3] = center + Vector3(-1, -1, -1) * delta_max;
  204. }
  205. List<Simplex *> acceleration_grid[ACCEL_GRID_SIZE][ACCEL_GRID_SIZE][ACCEL_GRID_SIZE];
  206. List<Simplex *> simplex_list;
  207. {
  208. //create root simplex
  209. Simplex *root = memnew(Simplex(point_count + 0, point_count + 1, point_count + 2, point_count + 3));
  210. root->SE = simplex_list.push_back(root);
  211. for (uint32_t i = 0; i < ACCEL_GRID_SIZE; i++) {
  212. for (uint32_t j = 0; j < ACCEL_GRID_SIZE; j++) {
  213. for (uint32_t k = 0; k < ACCEL_GRID_SIZE; k++) {
  214. GridPos gp;
  215. gp.E = acceleration_grid[i][j][k].push_back(root);
  216. gp.pos = Vector3i(i, j, k);
  217. root->grid_positions.push_back(gp);
  218. }
  219. }
  220. }
  221. circum_sphere_compute(points, root);
  222. }
  223. OAHashMap<Triangle, uint32_t, TriangleHasher> triangles_inserted;
  224. LocalVector<Triangle> triangles;
  225. for (uint32_t i = 0; i < point_count; i++) {
  226. bool unique = true;
  227. for (uint32_t j = i + 1; j < point_count; j++) {
  228. if (points[i].is_equal_approx(points[j])) {
  229. unique = false;
  230. break;
  231. }
  232. }
  233. if (!unique) {
  234. continue;
  235. }
  236. Vector3i grid_pos = Vector3i(points[i] * ACCEL_GRID_SIZE);
  237. grid_pos.x = CLAMP(grid_pos.x, 0, ACCEL_GRID_SIZE - 1);
  238. grid_pos.y = CLAMP(grid_pos.y, 0, ACCEL_GRID_SIZE - 1);
  239. grid_pos.z = CLAMP(grid_pos.z, 0, ACCEL_GRID_SIZE - 1);
  240. for (List<Simplex *>::Element *E = acceleration_grid[grid_pos.x][grid_pos.y][grid_pos.z].front(); E;) {
  241. List<Simplex *>::Element *N = E->next(); //may be deleted
  242. Simplex *simplex = E->get();
  243. if (simplex_contains(points, *simplex, i)) {
  244. static const uint32_t triangle_order[4][3] = {
  245. { 0, 1, 2 },
  246. { 0, 1, 3 },
  247. { 0, 2, 3 },
  248. { 1, 2, 3 },
  249. };
  250. for (uint32_t k = 0; k < 4; k++) {
  251. Triangle t = Triangle(simplex->points[triangle_order[k][0]], simplex->points[triangle_order[k][1]], simplex->points[triangle_order[k][2]]);
  252. uint32_t *p = triangles_inserted.lookup_ptr(t);
  253. if (p) {
  254. triangles[*p].bad = true;
  255. } else {
  256. triangles_inserted.insert(t, triangles.size());
  257. triangles.push_back(t);
  258. }
  259. }
  260. //remove simplex and continue
  261. simplex_list.erase(simplex->SE);
  262. for (const GridPos &gp : simplex->grid_positions) {
  263. Vector3i p = gp.pos;
  264. acceleration_grid[p.x][p.y][p.z].erase(gp.E);
  265. }
  266. memdelete(simplex);
  267. }
  268. E = N;
  269. }
  270. for (const Triangle &triangle : triangles) {
  271. if (triangle.bad) {
  272. continue;
  273. }
  274. Simplex *new_simplex = memnew(Simplex(triangle.triangle[0], triangle.triangle[1], triangle.triangle[2], i));
  275. circum_sphere_compute(points, new_simplex);
  276. new_simplex->SE = simplex_list.push_back(new_simplex);
  277. {
  278. Vector3 center;
  279. center.x = double(new_simplex->circum_center_x);
  280. center.y = double(new_simplex->circum_center_y);
  281. center.z = double(new_simplex->circum_center_z);
  282. float radius2 = Math::sqrt(double(new_simplex->circum_r2));
  283. radius2 += 0.0001; //
  284. Vector3 extents = Vector3(radius2, radius2, radius2);
  285. Vector3i from = Vector3i((center - extents) * ACCEL_GRID_SIZE);
  286. Vector3i to = Vector3i((center + extents) * ACCEL_GRID_SIZE);
  287. from.x = CLAMP(from.x, 0, ACCEL_GRID_SIZE - 1);
  288. from.y = CLAMP(from.y, 0, ACCEL_GRID_SIZE - 1);
  289. from.z = CLAMP(from.z, 0, ACCEL_GRID_SIZE - 1);
  290. to.x = CLAMP(to.x, 0, ACCEL_GRID_SIZE - 1);
  291. to.y = CLAMP(to.y, 0, ACCEL_GRID_SIZE - 1);
  292. to.z = CLAMP(to.z, 0, ACCEL_GRID_SIZE - 1);
  293. for (int32_t x = from.x; x <= to.x; x++) {
  294. for (int32_t y = from.y; y <= to.y; y++) {
  295. for (int32_t z = from.z; z <= to.z; z++) {
  296. GridPos gp;
  297. gp.pos = Vector3(x, y, z);
  298. gp.E = acceleration_grid[x][y][z].push_back(new_simplex);
  299. new_simplex->grid_positions.push_back(gp);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. triangles.clear();
  306. triangles_inserted.clear();
  307. }
  308. //print_line("end with simplices: " + itos(simplex_list.size()));
  309. Vector<OutputSimplex> ret_simplices;
  310. ret_simplices.resize(simplex_list.size());
  311. OutputSimplex *ret_simplicesw = ret_simplices.ptrw();
  312. uint32_t simplices_written = 0;
  313. for (Simplex *simplex : simplex_list) {
  314. bool invalid = false;
  315. for (int j = 0; j < 4; j++) {
  316. if (simplex->points[j] >= point_count) {
  317. invalid = true;
  318. break;
  319. }
  320. }
  321. if (invalid || simplex_is_coplanar(points, *simplex)) {
  322. memdelete(simplex);
  323. continue;
  324. }
  325. ret_simplicesw[simplices_written].points[0] = simplex->points[0];
  326. ret_simplicesw[simplices_written].points[1] = simplex->points[1];
  327. ret_simplicesw[simplices_written].points[2] = simplex->points[2];
  328. ret_simplicesw[simplices_written].points[3] = simplex->points[3];
  329. simplices_written++;
  330. memdelete(simplex);
  331. }
  332. ret_simplices.resize(simplices_written);
  333. memfree(points);
  334. return ret_simplices;
  335. }
  336. };
  337. #endif // DELAUNAY_3D_H