geometry_3d.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*************************************************************************/
  2. /* geometry_3d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GEOMETRY_3D_H
  31. #define GEOMETRY_3D_H
  32. #include "core/math/face3.h"
  33. #include "core/object/object.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 (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<Vector<Face3>> separate_objects(Vector<Face3> p_array);
  421. // Create a "wrap" that encloses the given geometry.
  422. static Vector<Face3> wrap_geometry(Vector<Face3> p_array, real_t *p_error = nullptr);
  423. struct MeshData {
  424. struct Face {
  425. Plane plane;
  426. Vector<int> indices;
  427. };
  428. Vector<Face> faces;
  429. struct Edge {
  430. int a, b;
  431. };
  432. Vector<Edge> edges;
  433. Vector<Vector3> vertices;
  434. void optimize_vertices();
  435. };
  436. static MeshData build_convex_mesh(const Vector<Plane> &p_planes);
  437. static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
  438. static Vector<Plane> build_box_planes(const Vector3 &p_extents);
  439. static Vector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
  440. 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);
  441. static Vector<Vector3> compute_convex_mesh_points(const Plane *p_planes, int p_plane_count);
  442. #define FINDMINMAX(x0, x1, x2, min, max) \
  443. min = max = x0; \
  444. if (x1 < min) { \
  445. min = x1; \
  446. } \
  447. if (x1 > max) { \
  448. max = x1; \
  449. } \
  450. if (x2 < min) { \
  451. min = x2; \
  452. } \
  453. if (x2 > max) { \
  454. max = x2; \
  455. }
  456. _FORCE_INLINE_ static bool planeBoxOverlap(Vector3 normal, float d, Vector3 maxbox) {
  457. int q;
  458. Vector3 vmin, vmax;
  459. for (q = 0; q <= 2; q++) {
  460. if (normal[q] > 0.0f) {
  461. vmin[q] = -maxbox[q];
  462. vmax[q] = maxbox[q];
  463. } else {
  464. vmin[q] = maxbox[q];
  465. vmax[q] = -maxbox[q];
  466. }
  467. }
  468. if (normal.dot(vmin) + d > 0.0f) {
  469. return false;
  470. }
  471. if (normal.dot(vmax) + d >= 0.0f) {
  472. return true;
  473. }
  474. return false;
  475. }
  476. /*======================== X-tests ========================*/
  477. #define AXISTEST_X01(a, b, fa, fb) \
  478. p0 = a * v0.y - b * v0.z; \
  479. p2 = a * v2.y - b * v2.z; \
  480. if (p0 < p2) { \
  481. min = p0; \
  482. max = p2; \
  483. } else { \
  484. min = p2; \
  485. max = p0; \
  486. } \
  487. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  488. if (min > rad || max < -rad) { \
  489. return false; \
  490. }
  491. #define AXISTEST_X2(a, b, fa, fb) \
  492. p0 = a * v0.y - b * v0.z; \
  493. p1 = a * v1.y - b * v1.z; \
  494. if (p0 < p1) { \
  495. min = p0; \
  496. max = p1; \
  497. } else { \
  498. min = p1; \
  499. max = p0; \
  500. } \
  501. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  502. if (min > rad || max < -rad) { \
  503. return false; \
  504. }
  505. /*======================== Y-tests ========================*/
  506. #define AXISTEST_Y02(a, b, fa, fb) \
  507. p0 = -a * v0.x + b * v0.z; \
  508. p2 = -a * v2.x + b * v2.z; \
  509. if (p0 < p2) { \
  510. min = p0; \
  511. max = p2; \
  512. } else { \
  513. min = p2; \
  514. max = p0; \
  515. } \
  516. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  517. if (min > rad || max < -rad) { \
  518. return false; \
  519. }
  520. #define AXISTEST_Y1(a, b, fa, fb) \
  521. p0 = -a * v0.x + b * v0.z; \
  522. p1 = -a * v1.x + b * v1.z; \
  523. if (p0 < p1) { \
  524. min = p0; \
  525. max = p1; \
  526. } else { \
  527. min = p1; \
  528. max = p0; \
  529. } \
  530. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  531. if (min > rad || max < -rad) { \
  532. return false; \
  533. }
  534. /*======================== Z-tests ========================*/
  535. #define AXISTEST_Z12(a, b, fa, fb) \
  536. p1 = a * v1.x - b * v1.y; \
  537. p2 = a * v2.x - b * v2.y; \
  538. if (p2 < p1) { \
  539. min = p2; \
  540. max = p1; \
  541. } else { \
  542. min = p1; \
  543. max = p2; \
  544. } \
  545. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  546. if (min > rad || max < -rad) { \
  547. return false; \
  548. }
  549. #define AXISTEST_Z0(a, b, fa, fb) \
  550. p0 = a * v0.x - b * v0.y; \
  551. p1 = a * v1.x - b * v1.y; \
  552. if (p0 < p1) { \
  553. min = p0; \
  554. max = p1; \
  555. } else { \
  556. min = p1; \
  557. max = p0; \
  558. } \
  559. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  560. if (min > rad || max < -rad) { \
  561. return false; \
  562. }
  563. _FORCE_INLINE_ static bool triangle_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
  564. /* use separating axis theorem to test overlap between triangle and box */
  565. /* need to test for overlap in these directions: */
  566. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  567. /* we do not even need to test these) */
  568. /* 2) normal of the triangle */
  569. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  570. /* this gives 3x3=9 more tests */
  571. Vector3 v0, v1, v2;
  572. float min, max, d, p0, p1, p2, rad, fex, fey, fez;
  573. Vector3 normal, e0, e1, e2;
  574. /* This is the fastest branch on Sun */
  575. /* move everything so that the boxcenter is in (0,0,0) */
  576. v0 = triverts[0] - boxcenter;
  577. v1 = triverts[1] - boxcenter;
  578. v2 = triverts[2] - boxcenter;
  579. /* compute triangle edges */
  580. e0 = v1 - v0; /* tri edge 0 */
  581. e1 = v2 - v1; /* tri edge 1 */
  582. e2 = v0 - v2; /* tri edge 2 */
  583. /* Bullet 3: */
  584. /* test the 9 tests first (this was faster) */
  585. fex = Math::abs(e0.x);
  586. fey = Math::abs(e0.y);
  587. fez = Math::abs(e0.z);
  588. AXISTEST_X01(e0.z, e0.y, fez, fey);
  589. AXISTEST_Y02(e0.z, e0.x, fez, fex);
  590. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  591. fex = Math::abs(e1.x);
  592. fey = Math::abs(e1.y);
  593. fez = Math::abs(e1.z);
  594. AXISTEST_X01(e1.z, e1.y, fez, fey);
  595. AXISTEST_Y02(e1.z, e1.x, fez, fex);
  596. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  597. fex = Math::abs(e2.x);
  598. fey = Math::abs(e2.y);
  599. fez = Math::abs(e2.z);
  600. AXISTEST_X2(e2.z, e2.y, fez, fey);
  601. AXISTEST_Y1(e2.z, e2.x, fez, fex);
  602. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  603. /* Bullet 1: */
  604. /* first test overlap in the {x,y,z}-directions */
  605. /* find min, max of the triangle each direction, and test for overlap in */
  606. /* that direction -- this is equivalent to testing a minimal AABB around */
  607. /* the triangle against the AABB */
  608. /* test in X-direction */
  609. FINDMINMAX(v0.x, v1.x, v2.x, min, max);
  610. if (min > boxhalfsize.x || max < -boxhalfsize.x) {
  611. return false;
  612. }
  613. /* test in Y-direction */
  614. FINDMINMAX(v0.y, v1.y, v2.y, min, max);
  615. if (min > boxhalfsize.y || max < -boxhalfsize.y) {
  616. return false;
  617. }
  618. /* test in Z-direction */
  619. FINDMINMAX(v0.z, v1.z, v2.z, min, max);
  620. if (min > boxhalfsize.z || max < -boxhalfsize.z) {
  621. return false;
  622. }
  623. /* Bullet 2: */
  624. /* test if the box intersects the plane of the triangle */
  625. /* compute plane equation of triangle: normal*x+d=0 */
  626. normal = e0.cross(e1);
  627. d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
  628. return planeBoxOverlap(normal, d, boxhalfsize); /* if true, box and triangle overlaps */
  629. }
  630. static Vector<uint32_t> generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative);
  631. static Vector<int8_t> generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative);
  632. static Vector3 triangle_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_pos) {
  633. Vector3 v0 = p_b - p_a;
  634. Vector3 v1 = p_c - p_a;
  635. Vector3 v2 = p_pos - p_a;
  636. float d00 = v0.dot(v0);
  637. float d01 = v0.dot(v1);
  638. float d11 = v1.dot(v1);
  639. float d20 = v2.dot(v0);
  640. float d21 = v2.dot(v1);
  641. float denom = (d00 * d11 - d01 * d01);
  642. if (denom == 0) {
  643. return Vector3(); //invalid triangle, return empty
  644. }
  645. float v = (d11 * d20 - d01 * d21) / denom;
  646. float w = (d00 * d21 - d01 * d20) / denom;
  647. float u = 1.0f - v - w;
  648. return Vector3(u, v, w);
  649. }
  650. 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) {
  651. Vector3 vap = p_pos - p_a;
  652. Vector3 vbp = p_pos - p_b;
  653. Vector3 vab = p_b - p_a;
  654. Vector3 vac = p_c - p_a;
  655. Vector3 vad = p_d - p_a;
  656. Vector3 vbc = p_c - p_b;
  657. Vector3 vbd = p_d - p_b;
  658. // ScTP computes the scalar triple product
  659. #define STP(m_a, m_b, m_c) ((m_a).dot((m_b).cross((m_c))))
  660. float va6 = STP(vbp, vbd, vbc);
  661. float vb6 = STP(vap, vac, vad);
  662. float vc6 = STP(vap, vad, vab);
  663. float vd6 = STP(vap, vab, vac);
  664. float v6 = 1 / STP(vab, vac, vad);
  665. return Color(va6 * v6, vb6 * v6, vc6 * v6, vd6 * v6);
  666. #undef STP
  667. }
  668. _FORCE_INLINE_ static Vector3 octahedron_map_decode(const Vector2 &p_uv) {
  669. // https://twitter.com/Stubbesaurus/status/937994790553227264
  670. Vector2 f = p_uv * 2.0f - Vector2(1.0f, 1.0f);
  671. Vector3 n = Vector3(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
  672. float t = CLAMP(-n.z, 0.0f, 1.0f);
  673. n.x += n.x >= 0 ? -t : t;
  674. n.y += n.y >= 0 ? -t : t;
  675. return n.normalized();
  676. }
  677. };
  678. #endif // GEOMETRY_3D_H