geometry_3d.h 28 KB

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