geometry_3d.h 25 KB

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