2
0

geometry_3d.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /**************************************************************************/
  2. /* geometry_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. #pragma once
  31. #include "core/math/delaunay_3d.h"
  32. #include "core/math/face3.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/vector.h"
  35. class Geometry3D {
  36. public:
  37. static void get_closest_points_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1, Vector3 &r_ps, Vector3 &r_qt);
  38. static real_t get_closest_distance_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1);
  39. static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) {
  40. Vector3 e1 = p_v1 - p_v0;
  41. Vector3 e2 = p_v2 - p_v0;
  42. Vector3 h = p_dir.cross(e2);
  43. real_t a = e1.dot(h);
  44. if (Math::is_zero_approx(a)) { // Parallel test.
  45. return false;
  46. }
  47. real_t f = 1.0f / a;
  48. Vector3 s = p_from - p_v0;
  49. real_t u = f * s.dot(h);
  50. if ((u < 0.0f) || (u > 1.0f)) {
  51. return false;
  52. }
  53. Vector3 q = s.cross(e1);
  54. real_t v = f * p_dir.dot(q);
  55. if ((v < 0.0f) || (u + v > 1.0f)) {
  56. return false;
  57. }
  58. // At this stage we can compute t to find out where
  59. // the intersection point is on the line.
  60. real_t t = f * e2.dot(q);
  61. if (t > 0.00001f) { // ray intersection
  62. if (r_res) {
  63. *r_res = p_from + p_dir * t;
  64. }
  65. return true;
  66. } else { // This means that there is a line intersection but not a ray intersection.
  67. return false;
  68. }
  69. }
  70. static inline bool segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) {
  71. Vector3 rel = p_to - p_from;
  72. Vector3 e1 = p_v1 - p_v0;
  73. Vector3 e2 = p_v2 - p_v0;
  74. Vector3 h = rel.cross(e2);
  75. real_t a = e1.dot(h);
  76. if (Math::is_zero_approx(a)) { // Parallel test.
  77. return false;
  78. }
  79. real_t f = 1.0f / a;
  80. Vector3 s = p_from - p_v0;
  81. real_t u = f * s.dot(h);
  82. if ((u < 0.0f) || (u > 1.0f)) {
  83. return false;
  84. }
  85. Vector3 q = s.cross(e1);
  86. real_t v = f * rel.dot(q);
  87. if ((v < 0.0f) || (u + v > 1.0f)) {
  88. return false;
  89. }
  90. // At this stage we can compute t to find out where
  91. // the intersection point is on the line.
  92. real_t t = f * e2.dot(q);
  93. if (t > (real_t)CMP_EPSILON && t <= 1.0f) { // Ray intersection.
  94. if (r_res) {
  95. *r_res = p_from + rel * t;
  96. }
  97. return true;
  98. } else { // This means that there is a line intersection but not a ray intersection.
  99. return false;
  100. }
  101. }
  102. static inline bool segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr) {
  103. Vector3 sphere_pos = p_sphere_pos - p_from;
  104. Vector3 rel = (p_to - p_from);
  105. real_t rel_l = rel.length();
  106. if (rel_l < (real_t)CMP_EPSILON) {
  107. return false; // Both points are the same.
  108. }
  109. Vector3 normal = rel / rel_l;
  110. real_t sphere_d = normal.dot(sphere_pos);
  111. real_t ray_distance = sphere_pos.distance_to(normal * sphere_d);
  112. if (ray_distance >= p_sphere_radius) {
  113. return false;
  114. }
  115. real_t inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
  116. real_t inters_d = sphere_d;
  117. if (inters_d2 >= (real_t)CMP_EPSILON) {
  118. inters_d -= Math::sqrt(inters_d2);
  119. }
  120. // Check in segment.
  121. if (inters_d < 0 || inters_d > rel_l) {
  122. return false;
  123. }
  124. Vector3 result = p_from + normal * inters_d;
  125. if (r_res) {
  126. *r_res = result;
  127. }
  128. if (r_norm) {
  129. *r_norm = (result - p_sphere_pos).normalized();
  130. }
  131. return true;
  132. }
  133. static inline bool segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, real_t p_height, real_t p_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr, int p_cylinder_axis = 2) {
  134. Vector3 rel = (p_to - p_from);
  135. real_t rel_l = rel.length();
  136. if (rel_l < (real_t)CMP_EPSILON) {
  137. return false; // Both points are the same.
  138. }
  139. ERR_FAIL_COND_V(p_cylinder_axis < 0, false);
  140. ERR_FAIL_COND_V(p_cylinder_axis > 2, false);
  141. Vector3 cylinder_axis;
  142. cylinder_axis[p_cylinder_axis] = 1.0f;
  143. // First check if they are parallel.
  144. Vector3 normal = (rel / rel_l);
  145. Vector3 crs = normal.cross(cylinder_axis);
  146. real_t crs_l = crs.length();
  147. Vector3 axis_dir;
  148. if (crs_l < (real_t)CMP_EPSILON) {
  149. Vector3 side_axis;
  150. side_axis[(p_cylinder_axis + 1) % 3] = 1.0f; // Any side axis OK.
  151. axis_dir = side_axis;
  152. } else {
  153. axis_dir = crs / crs_l;
  154. }
  155. real_t dist = axis_dir.dot(p_from);
  156. if (dist >= p_radius) {
  157. return false; // Too far away.
  158. }
  159. // Convert to 2D.
  160. real_t w2 = p_radius * p_radius - dist * dist;
  161. if (w2 < (real_t)CMP_EPSILON) {
  162. return false; // Avoid numerical error.
  163. }
  164. Size2 size(Math::sqrt(w2), p_height * 0.5f);
  165. Vector3 side_dir = axis_dir.cross(cylinder_axis).normalized();
  166. Vector2 from2D(side_dir.dot(p_from), p_from[p_cylinder_axis]);
  167. Vector2 to2D(side_dir.dot(p_to), p_to[p_cylinder_axis]);
  168. real_t min = 0, max = 1;
  169. int axis = -1;
  170. for (int i = 0; i < 2; i++) {
  171. real_t seg_from = from2D[i];
  172. real_t seg_to = to2D[i];
  173. real_t box_begin = -size[i];
  174. real_t box_end = size[i];
  175. real_t cmin, cmax;
  176. if (seg_from < seg_to) {
  177. if (seg_from > box_end || seg_to < box_begin) {
  178. return false;
  179. }
  180. real_t length = seg_to - seg_from;
  181. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  182. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  183. } else {
  184. if (seg_to > box_end || seg_from < box_begin) {
  185. return false;
  186. }
  187. real_t length = seg_to - seg_from;
  188. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  189. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  190. }
  191. if (cmin > min) {
  192. min = cmin;
  193. axis = i;
  194. }
  195. if (cmax < max) {
  196. max = cmax;
  197. }
  198. if (max < min) {
  199. return false;
  200. }
  201. }
  202. // Convert to 3D again.
  203. Vector3 result = p_from + (rel * min);
  204. Vector3 res_normal = result;
  205. if (axis == 0) {
  206. res_normal[p_cylinder_axis] = 0;
  207. } else {
  208. int axis_side = (p_cylinder_axis + 1) % 3;
  209. res_normal[axis_side] = 0;
  210. axis_side = (axis_side + 1) % 3;
  211. res_normal[axis_side] = 0;
  212. }
  213. res_normal.normalize();
  214. if (r_res) {
  215. *r_res = result;
  216. }
  217. if (r_norm) {
  218. *r_norm = res_normal;
  219. }
  220. return true;
  221. }
  222. static bool segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Plane *p_planes, int p_plane_count, Vector3 *p_res, Vector3 *p_norm) {
  223. real_t min = -1e20, max = 1e20;
  224. Vector3 rel = p_to - p_from;
  225. real_t rel_l = rel.length();
  226. if (rel_l < (real_t)CMP_EPSILON) {
  227. return false;
  228. }
  229. Vector3 dir = rel / rel_l;
  230. int min_index = -1;
  231. for (int i = 0; i < p_plane_count; i++) {
  232. const Plane &p = p_planes[i];
  233. real_t den = p.normal.dot(dir);
  234. if (Math::abs(den) <= (real_t)CMP_EPSILON) {
  235. continue; // Ignore parallel plane.
  236. }
  237. real_t dist = -p.distance_to(p_from) / den;
  238. if (den > 0) {
  239. // Backwards facing plane.
  240. if (dist < max) {
  241. max = dist;
  242. }
  243. } else {
  244. // Front facing plane.
  245. if (dist > min) {
  246. min = dist;
  247. min_index = i;
  248. }
  249. }
  250. }
  251. if (max <= min || min < 0 || min > rel_l || min_index == -1) { // Exit conditions.
  252. return false; // No intersection.
  253. }
  254. if (p_res) {
  255. *p_res = p_from + dir * min;
  256. }
  257. if (p_norm) {
  258. *p_norm = p_planes[min_index].normal;
  259. }
  260. return true;
  261. }
  262. static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) {
  263. Vector3 p = p_point - p_segment[0];
  264. Vector3 n = p_segment[1] - p_segment[0];
  265. real_t l2 = n.length_squared();
  266. if (l2 < 1e-20f) {
  267. return p_segment[0]; // Both points are the same, just give any.
  268. }
  269. real_t d = n.dot(p) / l2;
  270. if (d <= 0.0f) {
  271. return p_segment[0]; // Before first point.
  272. } else if (d >= 1.0f) {
  273. return p_segment[1]; // After first point.
  274. } else {
  275. return p_segment[0] + n * d; // Inside.
  276. }
  277. }
  278. static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) {
  279. Vector3 p = p_point - p_segment[0];
  280. Vector3 n = p_segment[1] - p_segment[0];
  281. real_t l2 = n.length_squared();
  282. if (l2 < 1e-20f) {
  283. return p_segment[0]; // Both points are the same, just give any.
  284. }
  285. real_t d = n.dot(p) / l2;
  286. return p_segment[0] + n * d; // Inside.
  287. }
  288. static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
  289. Vector3 face_n = (p_v1 - p_v3).cross(p_v1 - p_v2);
  290. Vector3 n1 = (p_point - p_v3).cross(p_point - p_v2);
  291. if (face_n.dot(n1) < 0) {
  292. return false;
  293. }
  294. Vector3 n2 = (p_v1 - p_v3).cross(p_v1 - p_point);
  295. if (face_n.dot(n2) < 0) {
  296. return false;
  297. }
  298. Vector3 n3 = (p_v1 - p_point).cross(p_v1 - p_v2);
  299. if (face_n.dot(n3) < 0) {
  300. return false;
  301. }
  302. return true;
  303. }
  304. static inline bool triangle_sphere_intersection_test(const Vector3 *p_triangle, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact) {
  305. real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]);
  306. if (d > p_sphere_radius || d < -p_sphere_radius) {
  307. // Not touching the plane of the face, return.
  308. return false;
  309. }
  310. Vector3 contact = p_sphere_pos - (p_normal * d);
  311. /** 2nd) TEST INSIDE TRIANGLE **/
  312. if (Geometry3D::point_in_projected_triangle(contact, p_triangle[0], p_triangle[1], p_triangle[2])) {
  313. r_triangle_contact = contact;
  314. r_sphere_contact = p_sphere_pos - p_normal * p_sphere_radius;
  315. //printf("solved inside triangle\n");
  316. return true;
  317. }
  318. /** 3rd TEST INSIDE EDGE CYLINDERS **/
  319. const Vector3 verts[4] = { p_triangle[0], p_triangle[1], p_triangle[2], p_triangle[0] }; // for() friendly
  320. for (int i = 0; i < 3; i++) {
  321. // Check edge cylinder.
  322. Vector3 n1 = verts[i] - verts[i + 1];
  323. Vector3 n2 = p_sphere_pos - verts[i + 1];
  324. ///@TODO Maybe discard by range here to make the algorithm quicker.
  325. // Check point within cylinder radius.
  326. Vector3 axis = n1.cross(n2).cross(n1);
  327. axis.normalize();
  328. real_t ad = axis.dot(n2);
  329. if (Math::abs(ad) > p_sphere_radius) {
  330. // No chance with this edge, too far away.
  331. continue;
  332. }
  333. // Check point within edge capsule cylinder.
  334. /** 4th TEST INSIDE EDGE POINTS **/
  335. real_t sphere_at = n1.dot(n2);
  336. if (sphere_at >= 0 && sphere_at < n1.dot(n1)) {
  337. r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2));
  338. r_sphere_contact = p_sphere_pos - axis * p_sphere_radius;
  339. // Point inside here.
  340. return true;
  341. }
  342. real_t r2 = p_sphere_radius * p_sphere_radius;
  343. if (n2.length_squared() < r2) {
  344. Vector3 n = (p_sphere_pos - verts[i + 1]).normalized();
  345. r_triangle_contact = verts[i + 1];
  346. r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
  347. return true;
  348. }
  349. if (n2.distance_squared_to(n1) < r2) {
  350. Vector3 n = (p_sphere_pos - verts[i]).normalized();
  351. r_triangle_contact = verts[i];
  352. r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
  353. return true;
  354. }
  355. break; // It's pointless to continue at this point, so save some CPU cycles.
  356. }
  357. return false;
  358. }
  359. static inline Vector<Vector3> clip_polygon(const Vector<Vector3> &polygon, const Plane &p_plane) {
  360. enum LocationCache {
  361. LOC_INSIDE = 1,
  362. LOC_BOUNDARY = 0,
  363. LOC_OUTSIDE = -1
  364. };
  365. if (polygon.size() == 0) {
  366. return polygon;
  367. }
  368. int *location_cache = (int *)alloca(sizeof(int) * polygon.size());
  369. int inside_count = 0;
  370. int outside_count = 0;
  371. for (int a = 0; a < polygon.size(); a++) {
  372. real_t dist = p_plane.distance_to(polygon[a]);
  373. if (dist < (real_t)-CMP_POINT_IN_PLANE_EPSILON) {
  374. location_cache[a] = LOC_INSIDE;
  375. inside_count++;
  376. } else {
  377. if (dist > (real_t)CMP_POINT_IN_PLANE_EPSILON) {
  378. location_cache[a] = LOC_OUTSIDE;
  379. outside_count++;
  380. } else {
  381. location_cache[a] = LOC_BOUNDARY;
  382. }
  383. }
  384. }
  385. if (outside_count == 0) {
  386. return polygon; // No changes.
  387. } else if (inside_count == 0) {
  388. return Vector<Vector3>(); // Empty.
  389. }
  390. long previous = polygon.size() - 1;
  391. Vector<Vector3> clipped;
  392. for (int index = 0; index < polygon.size(); index++) {
  393. int loc = location_cache[index];
  394. if (loc == LOC_OUTSIDE) {
  395. if (location_cache[previous] == LOC_INSIDE) {
  396. const Vector3 &v1 = polygon[previous];
  397. const Vector3 &v2 = polygon[index];
  398. Vector3 segment = v1 - v2;
  399. real_t den = p_plane.normal.dot(segment);
  400. real_t dist = p_plane.distance_to(v1) / den;
  401. dist = -dist;
  402. clipped.push_back(v1 + segment * dist);
  403. }
  404. } else {
  405. const Vector3 &v1 = polygon[index];
  406. if ((loc == LOC_INSIDE) && (location_cache[previous] == LOC_OUTSIDE)) {
  407. const Vector3 &v2 = polygon[previous];
  408. Vector3 segment = v1 - v2;
  409. real_t den = p_plane.normal.dot(segment);
  410. real_t dist = p_plane.distance_to(v1) / den;
  411. dist = -dist;
  412. clipped.push_back(v1 + segment * dist);
  413. }
  414. clipped.push_back(v1);
  415. }
  416. previous = index;
  417. }
  418. return clipped;
  419. }
  420. static Vector<int32_t> tetrahedralize_delaunay(const Vector<Vector3> &p_points) {
  421. Vector<Delaunay3D::OutputSimplex> tetr = Delaunay3D::tetrahedralize(p_points);
  422. Vector<int32_t> tetrahedrons;
  423. tetrahedrons.resize(4 * tetr.size());
  424. int32_t *ptr = tetrahedrons.ptrw();
  425. for (int i = 0; i < tetr.size(); i++) {
  426. *ptr++ = tetr[i].points[0];
  427. *ptr++ = tetr[i].points[1];
  428. *ptr++ = tetr[i].points[2];
  429. *ptr++ = tetr[i].points[3];
  430. }
  431. return tetrahedrons;
  432. }
  433. // Create a "wrap" that encloses the given geometry.
  434. static Vector<Face3> wrap_geometry(const Vector<Face3> &p_array, real_t *p_error = nullptr);
  435. struct MeshData {
  436. struct Face {
  437. Plane plane;
  438. LocalVector<int> indices;
  439. };
  440. LocalVector<Face> faces;
  441. struct Edge {
  442. int vertex_a, vertex_b;
  443. int face_a, face_b;
  444. };
  445. LocalVector<Edge> edges;
  446. LocalVector<Vector3> vertices;
  447. void optimize_vertices();
  448. };
  449. static MeshData build_convex_mesh(const Vector<Plane> &p_planes);
  450. static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
  451. static Vector<Plane> build_box_planes(const Vector3 &p_extents);
  452. static Vector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
  453. static Vector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z);
  454. static Vector<Vector3> compute_convex_mesh_points(const Plane *p_planes, int p_plane_count);
  455. #define FINDMINMAX(x0, x1, x2, min, max) \
  456. min = max = x0; \
  457. if (x1 < min) { \
  458. min = x1; \
  459. } \
  460. if (x1 > max) { \
  461. max = x1; \
  462. } \
  463. if (x2 < min) { \
  464. min = x2; \
  465. } \
  466. if (x2 > max) { \
  467. max = x2; \
  468. }
  469. _FORCE_INLINE_ static bool planeBoxOverlap(Vector3 normal, real_t d, Vector3 maxbox) {
  470. int q;
  471. Vector3 vmin, vmax;
  472. for (q = 0; q <= 2; q++) {
  473. if (normal[q] > 0.0f) {
  474. vmin[q] = -maxbox[q];
  475. vmax[q] = maxbox[q];
  476. } else {
  477. vmin[q] = maxbox[q];
  478. vmax[q] = -maxbox[q];
  479. }
  480. }
  481. if (normal.dot(vmin) + d > 0.0f) {
  482. return false;
  483. }
  484. if (normal.dot(vmax) + d >= 0.0f) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. /*======================== X-tests ========================*/
  490. #define AXISTEST_X01(a, b, fa, fb) \
  491. p0 = a * v0.y - b * v0.z; \
  492. p2 = a * v2.y - b * v2.z; \
  493. if (p0 < p2) { \
  494. min = p0; \
  495. max = p2; \
  496. } else { \
  497. min = p2; \
  498. max = p0; \
  499. } \
  500. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  501. if (min > rad || max < -rad) { \
  502. return false; \
  503. }
  504. #define AXISTEST_X2(a, b, fa, fb) \
  505. p0 = a * v0.y - b * v0.z; \
  506. p1 = a * v1.y - b * v1.z; \
  507. if (p0 < p1) { \
  508. min = p0; \
  509. max = p1; \
  510. } else { \
  511. min = p1; \
  512. max = p0; \
  513. } \
  514. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  515. if (min > rad || max < -rad) { \
  516. return false; \
  517. }
  518. /*======================== Y-tests ========================*/
  519. #define AXISTEST_Y02(a, b, fa, fb) \
  520. p0 = -a * v0.x + b * v0.z; \
  521. p2 = -a * v2.x + b * v2.z; \
  522. if (p0 < p2) { \
  523. min = p0; \
  524. max = p2; \
  525. } else { \
  526. min = p2; \
  527. max = p0; \
  528. } \
  529. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  530. if (min > rad || max < -rad) { \
  531. return false; \
  532. }
  533. #define AXISTEST_Y1(a, b, fa, fb) \
  534. p0 = -a * v0.x + b * v0.z; \
  535. p1 = -a * v1.x + b * v1.z; \
  536. if (p0 < p1) { \
  537. min = p0; \
  538. max = p1; \
  539. } else { \
  540. min = p1; \
  541. max = p0; \
  542. } \
  543. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  544. if (min > rad || max < -rad) { \
  545. return false; \
  546. }
  547. /*======================== Z-tests ========================*/
  548. #define AXISTEST_Z12(a, b, fa, fb) \
  549. p1 = a * v1.x - b * v1.y; \
  550. p2 = a * v2.x - b * v2.y; \
  551. if (p2 < p1) { \
  552. min = p2; \
  553. max = p1; \
  554. } else { \
  555. min = p1; \
  556. max = p2; \
  557. } \
  558. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  559. if (min > rad || max < -rad) { \
  560. return false; \
  561. }
  562. #define AXISTEST_Z0(a, b, fa, fb) \
  563. p0 = a * v0.x - b * v0.y; \
  564. p1 = a * v1.x - b * v1.y; \
  565. if (p0 < p1) { \
  566. min = p0; \
  567. max = p1; \
  568. } else { \
  569. min = p1; \
  570. max = p0; \
  571. } \
  572. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  573. if (min > rad || max < -rad) { \
  574. return false; \
  575. }
  576. _FORCE_INLINE_ static bool triangle_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
  577. /* use separating axis theorem to test overlap between triangle and box */
  578. /* need to test for overlap in these directions: */
  579. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  580. /* we do not even need to test these) */
  581. /* 2) normal of the triangle */
  582. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  583. /* this gives 3x3=9 more tests */
  584. real_t min, max, p0, p1, p2, rad, fex, fey, fez;
  585. /* This is the fastest branch on Sun */
  586. /* move everything so that the boxcenter is in (0,0,0) */
  587. const Vector3 v0 = triverts[0] - boxcenter;
  588. const Vector3 v1 = triverts[1] - boxcenter;
  589. const Vector3 v2 = triverts[2] - boxcenter;
  590. /* compute triangle edges */
  591. const Vector3 e0 = v1 - v0; /* tri edge 0 */
  592. const Vector3 e1 = v2 - v1; /* tri edge 1 */
  593. const Vector3 e2 = v0 - v2; /* tri edge 2 */
  594. /* Bullet 3: */
  595. /* test the 9 tests first (this was faster) */
  596. fex = Math::abs(e0.x);
  597. fey = Math::abs(e0.y);
  598. fez = Math::abs(e0.z);
  599. AXISTEST_X01(e0.z, e0.y, fez, fey);
  600. AXISTEST_Y02(e0.z, e0.x, fez, fex);
  601. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  602. fex = Math::abs(e1.x);
  603. fey = Math::abs(e1.y);
  604. fez = Math::abs(e1.z);
  605. AXISTEST_X01(e1.z, e1.y, fez, fey);
  606. AXISTEST_Y02(e1.z, e1.x, fez, fex);
  607. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  608. fex = Math::abs(e2.x);
  609. fey = Math::abs(e2.y);
  610. fez = Math::abs(e2.z);
  611. AXISTEST_X2(e2.z, e2.y, fez, fey);
  612. AXISTEST_Y1(e2.z, e2.x, fez, fex);
  613. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  614. /* Bullet 1: */
  615. /* first test overlap in the {x,y,z}-directions */
  616. /* find min, max of the triangle each direction, and test for overlap in */
  617. /* that direction -- this is equivalent to testing a minimal AABB around */
  618. /* the triangle against the AABB */
  619. /* test in X-direction */
  620. FINDMINMAX(v0.x, v1.x, v2.x, min, max);
  621. if (min > boxhalfsize.x || max < -boxhalfsize.x) {
  622. return false;
  623. }
  624. /* test in Y-direction */
  625. FINDMINMAX(v0.y, v1.y, v2.y, min, max);
  626. if (min > boxhalfsize.y || max < -boxhalfsize.y) {
  627. return false;
  628. }
  629. /* test in Z-direction */
  630. FINDMINMAX(v0.z, v1.z, v2.z, min, max);
  631. if (min > boxhalfsize.z || max < -boxhalfsize.z) {
  632. return false;
  633. }
  634. /* Bullet 2: */
  635. /* test if the box intersects the plane of the triangle */
  636. /* compute plane equation of triangle: normal*x+d=0 */
  637. const Vector3 normal = e0.cross(e1);
  638. const real_t d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
  639. return planeBoxOverlap(normal, d, boxhalfsize); /* if true, box and triangle overlaps */
  640. }
  641. static Vector<uint32_t> generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative);
  642. static Vector<int8_t> generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative);
  643. static Vector3 triangle_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_pos) {
  644. const Vector3 v0 = p_b - p_a;
  645. const Vector3 v1 = p_c - p_a;
  646. const Vector3 v2 = p_pos - p_a;
  647. const real_t d00 = v0.dot(v0);
  648. const real_t d01 = v0.dot(v1);
  649. const real_t d11 = v1.dot(v1);
  650. const real_t d20 = v2.dot(v0);
  651. const real_t d21 = v2.dot(v1);
  652. const real_t denom = (d00 * d11 - d01 * d01);
  653. if (denom == 0) {
  654. return Vector3(); //invalid triangle, return empty
  655. }
  656. const real_t v = (d11 * d20 - d01 * d21) / denom;
  657. const real_t w = (d00 * d21 - d01 * d20) / denom;
  658. const real_t u = 1.0f - v - w;
  659. return Vector3(u, v, w);
  660. }
  661. static Color tetrahedron_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_d, const Vector3 &p_pos) {
  662. const Vector3 vap = p_pos - p_a;
  663. const Vector3 vbp = p_pos - p_b;
  664. const Vector3 vab = p_b - p_a;
  665. const Vector3 vac = p_c - p_a;
  666. const Vector3 vad = p_d - p_a;
  667. const Vector3 vbc = p_c - p_b;
  668. const Vector3 vbd = p_d - p_b;
  669. // ScTP computes the scalar triple product
  670. #define STP(m_a, m_b, m_c) ((m_a).dot((m_b).cross((m_c))))
  671. const real_t va6 = STP(vbp, vbd, vbc);
  672. const real_t vb6 = STP(vap, vac, vad);
  673. const real_t vc6 = STP(vap, vad, vab);
  674. const real_t vd6 = STP(vap, vab, vac);
  675. const real_t v6 = 1 / STP(vab, vac, vad);
  676. return Color(va6 * v6, vb6 * v6, vc6 * v6, vd6 * v6);
  677. #undef STP
  678. }
  679. _FORCE_INLINE_ static Vector3 octahedron_map_decode(const Vector2 &p_uv) {
  680. // https://twitter.com/Stubbesaurus/status/937994790553227264
  681. const Vector2 f = p_uv * 2.0f - Vector2(1.0f, 1.0f);
  682. Vector3 n = Vector3(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
  683. const real_t t = CLAMP(-n.z, 0.0f, 1.0f);
  684. n.x += n.x >= 0 ? -t : t;
  685. n.y += n.y >= 0 ? -t : t;
  686. return n.normalized();
  687. }
  688. };