geometry_3d.h 26 KB

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