delaunay_3d.h 14 KB

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