geometry_3d.h 28 KB

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