geometry.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*************************************************************************/
  2. /* geometry.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_H
  31. #define GEOMETRY_H
  32. #include "core/math/delaunay.h"
  33. #include "core/math/face3.h"
  34. #include "core/math/rect2.h"
  35. #include "core/math/triangulate.h"
  36. #include "core/math/vector3.h"
  37. #include "core/object.h"
  38. #include "core/print_string.h"
  39. #include "core/vector.h"
  40. class Geometry {
  41. Geometry();
  42. public:
  43. static real_t get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2, Vector2 &c1, Vector2 &c2) {
  44. Vector2 d1 = q1 - p1; // Direction vector of segment S1.
  45. Vector2 d2 = q2 - p2; // Direction vector of segment S2.
  46. Vector2 r = p1 - p2;
  47. real_t a = d1.dot(d1); // Squared length of segment S1, always nonnegative.
  48. real_t e = d2.dot(d2); // Squared length of segment S2, always nonnegative.
  49. real_t f = d2.dot(r);
  50. real_t s, t;
  51. // Check if either or both segments degenerate into points.
  52. if (a <= CMP_EPSILON && e <= CMP_EPSILON) {
  53. // Both segments degenerate into points.
  54. c1 = p1;
  55. c2 = p2;
  56. return Math::sqrt((c1 - c2).dot(c1 - c2));
  57. }
  58. if (a <= CMP_EPSILON) {
  59. // First segment degenerates into a point.
  60. s = 0.0;
  61. t = f / e; // s = 0 => t = (b*s + f) / e = f / e
  62. t = CLAMP(t, 0.0, 1.0);
  63. } else {
  64. real_t c = d1.dot(r);
  65. if (e <= CMP_EPSILON) {
  66. // Second segment degenerates into a point.
  67. t = 0.0;
  68. s = CLAMP(-c / a, 0.0, 1.0); // t = 0 => s = (b*t - c) / a = -c / a
  69. } else {
  70. // The general nondegenerate case starts here.
  71. real_t b = d1.dot(d2);
  72. real_t denom = a * e - b * b; // Always nonnegative.
  73. // If segments not parallel, compute closest point on L1 to L2 and
  74. // clamp to segment S1. Else pick arbitrary s (here 0).
  75. if (denom != 0.0) {
  76. s = CLAMP((b * f - c * e) / denom, 0.0, 1.0);
  77. } else
  78. s = 0.0;
  79. // Compute point on L2 closest to S1(s) using
  80. // t = Dot((P1 + D1*s) - P2,D2) / Dot(D2,D2) = (b*s + f) / e
  81. t = (b * s + f) / e;
  82. //If t in [0,1] done. Else clamp t, recompute s for the new value
  83. // of t using s = Dot((P2 + D2*t) - P1,D1) / Dot(D1,D1)= (t*b - c) / a
  84. // and clamp s to [0, 1].
  85. if (t < 0.0) {
  86. t = 0.0;
  87. s = CLAMP(-c / a, 0.0, 1.0);
  88. } else if (t > 1.0) {
  89. t = 1.0;
  90. s = CLAMP((b - c) / a, 0.0, 1.0);
  91. }
  92. }
  93. }
  94. c1 = p1 + d1 * s;
  95. c2 = p2 + d2 * t;
  96. return Math::sqrt((c1 - c2).dot(c1 - c2));
  97. }
  98. static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) {
  99. // Do the function 'd' as defined by pb. I think is is dot product of some sort.
  100. #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))
  101. // Calculate the parametric position on the 2 curves, mua and mub.
  102. 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));
  103. real_t mub = (d_of(p1, q1, q2, q1) + mua * d_of(q2, q1, p2, p1)) / d_of(q2, q1, q2, q1);
  104. // Clip the value between [0..1] constraining the solution to lie on the original curves.
  105. if (mua < 0)
  106. mua = 0;
  107. if (mub < 0)
  108. mub = 0;
  109. if (mua > 1)
  110. mua = 1;
  111. if (mub > 1)
  112. mub = 1;
  113. c1 = p1.lerp(p2, mua);
  114. c2 = q1.lerp(q2, mub);
  115. }
  116. 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) {
  117. Vector3 u = p_to_a - p_from_a;
  118. Vector3 v = p_to_b - p_from_b;
  119. Vector3 w = p_from_a - p_to_a;
  120. real_t a = u.dot(u); // Always >= 0
  121. real_t b = u.dot(v);
  122. real_t c = v.dot(v); // Always >= 0
  123. real_t d = u.dot(w);
  124. real_t e = v.dot(w);
  125. real_t D = a * c - b * b; // Always >= 0
  126. real_t sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0
  127. real_t tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0
  128. // Compute the line parameters of the two closest points.
  129. if (D < CMP_EPSILON) { // The lines are almost parallel.
  130. sN = 0.0; // Force using point P0 on segment S1
  131. sD = 1.0; // to prevent possible division by 0.0 later.
  132. tN = e;
  133. tD = c;
  134. } else { // Get the closest points on the infinite lines
  135. sN = (b * e - c * d);
  136. tN = (a * e - b * d);
  137. if (sN < 0.0) { // sc < 0 => the s=0 edge is visible.
  138. sN = 0.0;
  139. tN = e;
  140. tD = c;
  141. } else if (sN > sD) { // sc > 1 => the s=1 edge is visible.
  142. sN = sD;
  143. tN = e + b;
  144. tD = c;
  145. }
  146. }
  147. if (tN < 0.0) { // tc < 0 => the t=0 edge is visible.
  148. tN = 0.0;
  149. // Recompute sc for this edge.
  150. if (-d < 0.0)
  151. sN = 0.0;
  152. else if (-d > a)
  153. sN = sD;
  154. else {
  155. sN = -d;
  156. sD = a;
  157. }
  158. } else if (tN > tD) { // tc > 1 => the t=1 edge is visible.
  159. tN = tD;
  160. // Recompute sc for this edge.
  161. if ((-d + b) < 0.0)
  162. sN = 0;
  163. else if ((-d + b) > a)
  164. sN = sD;
  165. else {
  166. sN = (-d + b);
  167. sD = a;
  168. }
  169. }
  170. // Finally do the division to get sc and tc.
  171. sc = (Math::is_zero_approx(sN) ? 0.0 : sN / sD);
  172. tc = (Math::is_zero_approx(tN) ? 0.0 : tN / tD);
  173. // Get the difference of the two closest points.
  174. Vector3 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc)
  175. return dP.length(); // Return the closest distance.
  176. }
  177. 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 = 0) {
  178. Vector3 e1 = p_v1 - p_v0;
  179. Vector3 e2 = p_v2 - p_v0;
  180. Vector3 h = p_dir.cross(e2);
  181. real_t a = e1.dot(h);
  182. if (Math::is_zero_approx(a)) // Parallel test.
  183. return false;
  184. real_t f = 1.0 / a;
  185. Vector3 s = p_from - p_v0;
  186. real_t u = f * s.dot(h);
  187. if (u < 0.0 || u > 1.0)
  188. return false;
  189. Vector3 q = s.cross(e1);
  190. real_t v = f * p_dir.dot(q);
  191. if (v < 0.0 || u + v > 1.0)
  192. return false;
  193. // At this stage we can compute t to find out where
  194. // the intersection point is on the line.
  195. real_t t = f * e2.dot(q);
  196. if (t > 0.00001) { // ray intersection
  197. if (r_res)
  198. *r_res = p_from + p_dir * t;
  199. return true;
  200. } else // This means that there is a line intersection but not a ray intersection.
  201. return false;
  202. }
  203. 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 = 0) {
  204. Vector3 rel = p_to - p_from;
  205. Vector3 e1 = p_v1 - p_v0;
  206. Vector3 e2 = p_v2 - p_v0;
  207. Vector3 h = rel.cross(e2);
  208. real_t a = e1.dot(h);
  209. if (Math::is_zero_approx(a)) // Parallel test.
  210. return false;
  211. real_t f = 1.0 / a;
  212. Vector3 s = p_from - p_v0;
  213. real_t u = f * s.dot(h);
  214. if (u < 0.0 || u > 1.0)
  215. return false;
  216. Vector3 q = s.cross(e1);
  217. real_t v = f * rel.dot(q);
  218. if (v < 0.0 || u + v > 1.0)
  219. return false;
  220. // At this stage we can compute t to find out where
  221. // the intersection point is on the line.
  222. real_t t = f * e2.dot(q);
  223. if (t > CMP_EPSILON && t <= 1.0) { // Ray intersection.
  224. if (r_res)
  225. *r_res = p_from + rel * t;
  226. return true;
  227. } else // This means that there is a line intersection but not a ray intersection.
  228. return false;
  229. }
  230. 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 = 0, Vector3 *r_norm = 0) {
  231. Vector3 sphere_pos = p_sphere_pos - p_from;
  232. Vector3 rel = (p_to - p_from);
  233. real_t rel_l = rel.length();
  234. if (rel_l < CMP_EPSILON)
  235. return false; // Both points are the same.
  236. Vector3 normal = rel / rel_l;
  237. real_t sphere_d = normal.dot(sphere_pos);
  238. real_t ray_distance = sphere_pos.distance_to(normal * sphere_d);
  239. if (ray_distance >= p_sphere_radius)
  240. return false;
  241. real_t inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
  242. real_t inters_d = sphere_d;
  243. if (inters_d2 >= CMP_EPSILON)
  244. inters_d -= Math::sqrt(inters_d2);
  245. // Check in segment.
  246. if (inters_d < 0 || inters_d > rel_l)
  247. return false;
  248. Vector3 result = p_from + normal * inters_d;
  249. if (r_res)
  250. *r_res = result;
  251. if (r_norm)
  252. *r_norm = (result - p_sphere_pos).normalized();
  253. return true;
  254. }
  255. 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 = 0, Vector3 *r_norm = 0) {
  256. Vector3 rel = (p_to - p_from);
  257. real_t rel_l = rel.length();
  258. if (rel_l < CMP_EPSILON)
  259. return false; // Both points are the same.
  260. // First check if they are parallel.
  261. Vector3 normal = (rel / rel_l);
  262. Vector3 crs = normal.cross(Vector3(0, 0, 1));
  263. real_t crs_l = crs.length();
  264. Vector3 z_dir;
  265. if (crs_l < CMP_EPSILON) {
  266. z_dir = Vector3(1, 0, 0); // Any x/y vector OK.
  267. } else {
  268. z_dir = crs / crs_l;
  269. }
  270. real_t dist = z_dir.dot(p_from);
  271. if (dist >= p_radius)
  272. return false; // Too far away.
  273. // Convert to 2D.
  274. real_t w2 = p_radius * p_radius - dist * dist;
  275. if (w2 < CMP_EPSILON)
  276. return false; // Avoid numerical error.
  277. Size2 size(Math::sqrt(w2), p_height * 0.5);
  278. Vector3 x_dir = z_dir.cross(Vector3(0, 0, 1)).normalized();
  279. Vector2 from2D(x_dir.dot(p_from), p_from.z);
  280. Vector2 to2D(x_dir.dot(p_to), p_to.z);
  281. real_t min = 0, max = 1;
  282. int axis = -1;
  283. for (int i = 0; i < 2; i++) {
  284. real_t seg_from = from2D[i];
  285. real_t seg_to = to2D[i];
  286. real_t box_begin = -size[i];
  287. real_t box_end = size[i];
  288. real_t cmin, cmax;
  289. if (seg_from < seg_to) {
  290. if (seg_from > box_end || seg_to < box_begin)
  291. return false;
  292. real_t length = seg_to - seg_from;
  293. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  294. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  295. } else {
  296. if (seg_to > box_end || seg_from < box_begin)
  297. return false;
  298. real_t length = seg_to - seg_from;
  299. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  300. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  301. }
  302. if (cmin > min) {
  303. min = cmin;
  304. axis = i;
  305. }
  306. if (cmax < max)
  307. max = cmax;
  308. if (max < min)
  309. return false;
  310. }
  311. // Convert to 3D again.
  312. Vector3 result = p_from + (rel * min);
  313. Vector3 res_normal = result;
  314. if (axis == 0) {
  315. res_normal.z = 0;
  316. } else {
  317. res_normal.x = 0;
  318. res_normal.y = 0;
  319. }
  320. res_normal.normalize();
  321. if (r_res)
  322. *r_res = result;
  323. if (r_norm)
  324. *r_norm = res_normal;
  325. return true;
  326. }
  327. 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) {
  328. real_t min = -1e20, max = 1e20;
  329. Vector3 rel = p_to - p_from;
  330. real_t rel_l = rel.length();
  331. if (rel_l < CMP_EPSILON)
  332. return false;
  333. Vector3 dir = rel / rel_l;
  334. int min_index = -1;
  335. for (int i = 0; i < p_plane_count; i++) {
  336. const Plane &p = p_planes[i];
  337. real_t den = p.normal.dot(dir);
  338. if (Math::abs(den) <= CMP_EPSILON)
  339. continue; // Ignore parallel plane.
  340. real_t dist = -p.distance_to(p_from) / den;
  341. if (den > 0) {
  342. // Backwards facing plane.
  343. if (dist < max)
  344. max = dist;
  345. } else {
  346. // Front facing plane.
  347. if (dist > min) {
  348. min = dist;
  349. min_index = i;
  350. }
  351. }
  352. }
  353. if (max <= min || min < 0 || min > rel_l || min_index == -1) // Exit conditions.
  354. return false; // No intersection.
  355. if (p_res)
  356. *p_res = p_from + dir * min;
  357. if (p_norm)
  358. *p_norm = p_planes[min_index].normal;
  359. return true;
  360. }
  361. static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) {
  362. Vector3 p = p_point - p_segment[0];
  363. Vector3 n = p_segment[1] - p_segment[0];
  364. real_t l2 = n.length_squared();
  365. if (l2 < 1e-20)
  366. return p_segment[0]; // Both points are the same, just give any.
  367. real_t d = n.dot(p) / l2;
  368. if (d <= 0.0)
  369. return p_segment[0]; // Before first point.
  370. else if (d >= 1.0)
  371. return p_segment[1]; // After first point.
  372. else
  373. return p_segment[0] + n * d; // Inside.
  374. }
  375. static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) {
  376. Vector3 p = p_point - p_segment[0];
  377. Vector3 n = p_segment[1] - p_segment[0];
  378. real_t l2 = n.length_squared();
  379. if (l2 < 1e-20)
  380. return p_segment[0]; // Both points are the same, just give any.
  381. real_t d = n.dot(p) / l2;
  382. return p_segment[0] + n * d; // Inside.
  383. }
  384. static Vector2 get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 *p_segment) {
  385. Vector2 p = p_point - p_segment[0];
  386. Vector2 n = p_segment[1] - p_segment[0];
  387. real_t l2 = n.length_squared();
  388. if (l2 < 1e-20)
  389. return p_segment[0]; // Both points are the same, just give any.
  390. real_t d = n.dot(p) / l2;
  391. if (d <= 0.0)
  392. return p_segment[0]; // Before first point.
  393. else if (d >= 1.0)
  394. return p_segment[1]; // After first point.
  395. else
  396. return p_segment[0] + n * d; // Inside.
  397. }
  398. static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) {
  399. Vector2 an = a - s;
  400. Vector2 bn = b - s;
  401. Vector2 cn = c - s;
  402. bool orientation = an.cross(bn) > 0;
  403. if ((bn.cross(cn) > 0) != orientation)
  404. return false;
  405. return (cn.cross(an) > 0) == orientation;
  406. }
  407. static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) {
  408. Vector2 p = p_point - p_segment[0];
  409. Vector2 n = p_segment[1] - p_segment[0];
  410. real_t l2 = n.length_squared();
  411. if (l2 < 1e-20)
  412. return p_segment[0]; // Both points are the same, just give any.
  413. real_t d = n.dot(p) / l2;
  414. return p_segment[0] + n * d; // Inside.
  415. }
  416. static bool line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b, Vector2 &r_result) {
  417. // See http://paulbourke.net/geometry/pointlineplane/
  418. const real_t denom = p_dir_b.y * p_dir_a.x - p_dir_b.x * p_dir_a.y;
  419. if (Math::is_zero_approx(denom)) { // Parallel?
  420. return false;
  421. }
  422. const Vector2 v = p_from_a - p_from_b;
  423. const real_t t = (p_dir_b.x * v.y - p_dir_b.y * v.x) / denom;
  424. r_result = p_from_a + t * p_dir_a;
  425. return true;
  426. }
  427. static bool segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b, Vector2 *r_result) {
  428. Vector2 B = p_to_a - p_from_a;
  429. Vector2 C = p_from_b - p_from_a;
  430. Vector2 D = p_to_b - p_from_a;
  431. real_t ABlen = B.dot(B);
  432. if (ABlen <= 0)
  433. return false;
  434. Vector2 Bn = B / ABlen;
  435. C = Vector2(C.x * Bn.x + C.y * Bn.y, C.y * Bn.x - C.x * Bn.y);
  436. D = Vector2(D.x * Bn.x + D.y * Bn.y, D.y * Bn.x - D.x * Bn.y);
  437. if ((C.y < 0 && D.y < 0) || (C.y >= 0 && D.y >= 0))
  438. return false;
  439. real_t ABpos = D.x + (C.x - D.x) * D.y / (D.y - C.y);
  440. // Fail if segment C-D crosses line A-B outside of segment A-B.
  441. if (ABpos < 0 || ABpos > 1.0)
  442. return false;
  443. // (4) Apply the discovered position to line A-B in the original coordinate system.
  444. if (r_result)
  445. *r_result = p_from_a + B * ABpos;
  446. return true;
  447. }
  448. static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
  449. Vector3 face_n = (p_v1 - p_v3).cross(p_v1 - p_v2);
  450. Vector3 n1 = (p_point - p_v3).cross(p_point - p_v2);
  451. if (face_n.dot(n1) < 0)
  452. return false;
  453. Vector3 n2 = (p_v1 - p_v3).cross(p_v1 - p_point);
  454. if (face_n.dot(n2) < 0)
  455. return false;
  456. Vector3 n3 = (p_v1 - p_point).cross(p_v1 - p_v2);
  457. if (face_n.dot(n3) < 0)
  458. return false;
  459. return true;
  460. }
  461. 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) {
  462. real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]);
  463. if (d > p_sphere_radius || d < -p_sphere_radius) // Not touching the plane of the face, return.
  464. return false;
  465. Vector3 contact = p_sphere_pos - (p_normal * d);
  466. /** 2nd) TEST INSIDE TRIANGLE **/
  467. if (Geometry::point_in_projected_triangle(contact, p_triangle[0], p_triangle[1], p_triangle[2])) {
  468. r_triangle_contact = contact;
  469. r_sphere_contact = p_sphere_pos - p_normal * p_sphere_radius;
  470. //printf("solved inside triangle\n");
  471. return true;
  472. }
  473. /** 3rd TEST INSIDE EDGE CYLINDERS **/
  474. const Vector3 verts[4] = { p_triangle[0], p_triangle[1], p_triangle[2], p_triangle[0] }; // for() friendly
  475. for (int i = 0; i < 3; i++) {
  476. // Check edge cylinder.
  477. Vector3 n1 = verts[i] - verts[i + 1];
  478. Vector3 n2 = p_sphere_pos - verts[i + 1];
  479. ///@TODO Maybe discard by range here to make the algorithm quicker.
  480. // Check point within cylinder radius.
  481. Vector3 axis = n1.cross(n2).cross(n1);
  482. axis.normalize();
  483. real_t ad = axis.dot(n2);
  484. if (ABS(ad) > p_sphere_radius) {
  485. // No chance with this edge, too far away.
  486. continue;
  487. }
  488. // Check point within edge capsule cylinder.
  489. /** 4th TEST INSIDE EDGE POINTS **/
  490. real_t sphere_at = n1.dot(n2);
  491. if (sphere_at >= 0 && sphere_at < n1.dot(n1)) {
  492. r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2));
  493. r_sphere_contact = p_sphere_pos - axis * p_sphere_radius;
  494. // Point inside here.
  495. return true;
  496. }
  497. real_t r2 = p_sphere_radius * p_sphere_radius;
  498. if (n2.length_squared() < r2) {
  499. Vector3 n = (p_sphere_pos - verts[i + 1]).normalized();
  500. r_triangle_contact = verts[i + 1];
  501. r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
  502. return true;
  503. }
  504. if (n2.distance_squared_to(n1) < r2) {
  505. Vector3 n = (p_sphere_pos - verts[i]).normalized();
  506. r_triangle_contact = verts[i];
  507. r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
  508. return true;
  509. }
  510. break; // It's pointless to continue at this point, so save some CPU cycles.
  511. }
  512. return false;
  513. }
  514. static inline bool is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  515. return p_point.distance_squared_to(p_circle_pos) <= p_circle_radius * p_circle_radius;
  516. }
  517. static real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  518. Vector2 line_vec = p_to - p_from;
  519. Vector2 vec_to_line = p_from - p_circle_pos;
  520. // Create a quadratic formula of the form ax^2 + bx + c = 0
  521. real_t a, b, c;
  522. a = line_vec.dot(line_vec);
  523. b = 2 * vec_to_line.dot(line_vec);
  524. c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius;
  525. // Solve for t.
  526. real_t sqrtterm = b * b - 4 * a * c;
  527. // If the term we intend to square root is less than 0 then the answer won't be real,
  528. // so it definitely won't be t in the range 0 to 1.
  529. if (sqrtterm < 0)
  530. return -1;
  531. // If we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection)
  532. // then the following can be skipped and we can just return the equivalent of res1.
  533. sqrtterm = Math::sqrt(sqrtterm);
  534. real_t res1 = (-b - sqrtterm) / (2 * a);
  535. real_t res2 = (-b + sqrtterm) / (2 * a);
  536. if (res1 >= 0 && res1 <= 1)
  537. return res1;
  538. if (res2 >= 0 && res2 <= 1)
  539. return res2;
  540. return -1;
  541. }
  542. static inline Vector<Vector3> clip_polygon(const Vector<Vector3> &polygon, const Plane &p_plane) {
  543. enum LocationCache {
  544. LOC_INSIDE = 1,
  545. LOC_BOUNDARY = 0,
  546. LOC_OUTSIDE = -1
  547. };
  548. if (polygon.size() == 0)
  549. return polygon;
  550. int *location_cache = (int *)alloca(sizeof(int) * polygon.size());
  551. int inside_count = 0;
  552. int outside_count = 0;
  553. for (int a = 0; a < polygon.size(); a++) {
  554. real_t dist = p_plane.distance_to(polygon[a]);
  555. if (dist < -CMP_POINT_IN_PLANE_EPSILON) {
  556. location_cache[a] = LOC_INSIDE;
  557. inside_count++;
  558. } else {
  559. if (dist > CMP_POINT_IN_PLANE_EPSILON) {
  560. location_cache[a] = LOC_OUTSIDE;
  561. outside_count++;
  562. } else {
  563. location_cache[a] = LOC_BOUNDARY;
  564. }
  565. }
  566. }
  567. if (outside_count == 0) {
  568. return polygon; // No changes.
  569. } else if (inside_count == 0) {
  570. return Vector<Vector3>(); // Empty.
  571. }
  572. long previous = polygon.size() - 1;
  573. Vector<Vector3> clipped;
  574. for (int index = 0; index < polygon.size(); index++) {
  575. int loc = location_cache[index];
  576. if (loc == LOC_OUTSIDE) {
  577. if (location_cache[previous] == LOC_INSIDE) {
  578. const Vector3 &v1 = polygon[previous];
  579. const Vector3 &v2 = polygon[index];
  580. Vector3 segment = v1 - v2;
  581. real_t den = p_plane.normal.dot(segment);
  582. real_t dist = p_plane.distance_to(v1) / den;
  583. dist = -dist;
  584. clipped.push_back(v1 + segment * dist);
  585. }
  586. } else {
  587. const Vector3 &v1 = polygon[index];
  588. if ((loc == LOC_INSIDE) && (location_cache[previous] == LOC_OUTSIDE)) {
  589. const Vector3 &v2 = polygon[previous];
  590. Vector3 segment = v1 - v2;
  591. real_t den = p_plane.normal.dot(segment);
  592. real_t dist = p_plane.distance_to(v1) / den;
  593. dist = -dist;
  594. clipped.push_back(v1 + segment * dist);
  595. }
  596. clipped.push_back(v1);
  597. }
  598. previous = index;
  599. }
  600. return clipped;
  601. }
  602. enum PolyBooleanOperation {
  603. OPERATION_UNION,
  604. OPERATION_DIFFERENCE,
  605. OPERATION_INTERSECTION,
  606. OPERATION_XOR
  607. };
  608. enum PolyJoinType {
  609. JOIN_SQUARE,
  610. JOIN_ROUND,
  611. JOIN_MITER
  612. };
  613. enum PolyEndType {
  614. END_POLYGON,
  615. END_JOINED,
  616. END_BUTT,
  617. END_SQUARE,
  618. END_ROUND
  619. };
  620. static Vector<Vector<Point2>> merge_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) {
  621. return _polypaths_do_operation(OPERATION_UNION, p_polygon_a, p_polygon_b);
  622. }
  623. static Vector<Vector<Point2>> clip_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) {
  624. return _polypaths_do_operation(OPERATION_DIFFERENCE, p_polygon_a, p_polygon_b);
  625. }
  626. static Vector<Vector<Point2>> intersect_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) {
  627. return _polypaths_do_operation(OPERATION_INTERSECTION, p_polygon_a, p_polygon_b);
  628. }
  629. static Vector<Vector<Point2>> exclude_polygons_2d(const Vector<Point2> &p_polygon_a, const Vector<Point2> &p_polygon_b) {
  630. return _polypaths_do_operation(OPERATION_XOR, p_polygon_a, p_polygon_b);
  631. }
  632. static Vector<Vector<Point2>> clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  633. return _polypaths_do_operation(OPERATION_DIFFERENCE, p_polyline, p_polygon, true);
  634. }
  635. static Vector<Vector<Point2>> intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  636. return _polypaths_do_operation(OPERATION_INTERSECTION, p_polyline, p_polygon, true);
  637. }
  638. static Vector<Vector<Point2>> offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  639. return _polypath_offset(p_polygon, p_delta, p_join_type, END_POLYGON);
  640. }
  641. static Vector<Vector<Point2>> offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  642. ERR_FAIL_COND_V_MSG(p_end_type == END_POLYGON, Vector<Vector<Point2>>(), "Attempt to offset a polyline like a polygon (use offset_polygon_2d instead).");
  643. return _polypath_offset(p_polygon, p_delta, p_join_type, p_end_type);
  644. }
  645. static Vector<int> triangulate_delaunay_2d(const Vector<Vector2> &p_points) {
  646. Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(p_points);
  647. Vector<int> triangles;
  648. for (int i = 0; i < tr.size(); i++) {
  649. triangles.push_back(tr[i].points[0]);
  650. triangles.push_back(tr[i].points[1]);
  651. triangles.push_back(tr[i].points[2]);
  652. }
  653. return triangles;
  654. }
  655. static Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon) {
  656. Vector<int> triangles;
  657. if (!Triangulate::triangulate(p_polygon, triangles))
  658. return Vector<int>(); //fail
  659. return triangles;
  660. }
  661. static bool is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  662. int c = p_polygon.size();
  663. if (c < 3)
  664. return false;
  665. const Vector2 *p = p_polygon.ptr();
  666. real_t sum = 0;
  667. for (int i = 0; i < c; i++) {
  668. const Vector2 &v1 = p[i];
  669. const Vector2 &v2 = p[(i + 1) % c];
  670. sum += (v2.x - v1.x) * (v2.y + v1.y);
  671. }
  672. return sum > 0.0f;
  673. }
  674. // Alternate implementation that should be faster.
  675. static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) {
  676. int c = p_polygon.size();
  677. if (c < 3)
  678. return false;
  679. const Vector2 *p = p_polygon.ptr();
  680. Vector2 further_away(-1e20, -1e20);
  681. Vector2 further_away_opposite(1e20, 1e20);
  682. for (int i = 0; i < c; i++) {
  683. further_away.x = MAX(p[i].x, further_away.x);
  684. further_away.y = MAX(p[i].y, further_away.y);
  685. further_away_opposite.x = MIN(p[i].x, further_away_opposite.x);
  686. further_away_opposite.y = MIN(p[i].y, further_away_opposite.y);
  687. }
  688. // Make point outside that won't intersect with points in segment from p_point.
  689. further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312);
  690. int intersections = 0;
  691. for (int i = 0; i < c; i++) {
  692. const Vector2 &v1 = p[i];
  693. const Vector2 &v2 = p[(i + 1) % c];
  694. if (segment_intersects_segment_2d(v1, v2, p_point, further_away, nullptr)) {
  695. intersections++;
  696. }
  697. }
  698. return (intersections & 1);
  699. }
  700. static Vector<Vector<Face3>> separate_objects(Vector<Face3> p_array);
  701. // Create a "wrap" that encloses the given geometry.
  702. static Vector<Face3> wrap_geometry(Vector<Face3> p_array, real_t *p_error = nullptr);
  703. struct MeshData {
  704. struct Face {
  705. Plane plane;
  706. Vector<int> indices;
  707. };
  708. Vector<Face> faces;
  709. struct Edge {
  710. int a, b;
  711. };
  712. Vector<Edge> edges;
  713. Vector<Vector3> vertices;
  714. void optimize_vertices();
  715. };
  716. _FORCE_INLINE_ static int get_uv84_normal_bit(const Vector3 &p_vector) {
  717. int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(Vector3(0, 1, 0))) * 4.0 / Math_PI + 0.5));
  718. if (lat == 0) {
  719. return 24;
  720. } else if (lat == 4) {
  721. return 25;
  722. }
  723. int lon = Math::fast_ftoi(Math::floor((Math_PI + Math::atan2(p_vector.x, p_vector.z)) * 8.0 / (Math_PI * 2.0) + 0.5)) % 8;
  724. return lon + (lat - 1) * 8;
  725. }
  726. _FORCE_INLINE_ static int get_uv84_normal_bit_neighbors(int p_idx) {
  727. if (p_idx == 24) {
  728. return 1 | 2 | 4 | 8;
  729. } else if (p_idx == 25) {
  730. return (1 << 23) | (1 << 22) | (1 << 21) | (1 << 20);
  731. } else {
  732. int ret = 0;
  733. if ((p_idx % 8) == 0)
  734. ret |= (1 << (p_idx + 7));
  735. else
  736. ret |= (1 << (p_idx - 1));
  737. if ((p_idx % 8) == 7)
  738. ret |= (1 << (p_idx - 7));
  739. else
  740. ret |= (1 << (p_idx + 1));
  741. int mask = ret | (1 << p_idx);
  742. if (p_idx < 8)
  743. ret |= 24;
  744. else
  745. ret |= mask >> 8;
  746. if (p_idx >= 16)
  747. ret |= 25;
  748. else
  749. ret |= mask << 8;
  750. return ret;
  751. }
  752. }
  753. static real_t vec2_cross(const Point2 &O, const Point2 &A, const Point2 &B) {
  754. return (real_t)(A.x - O.x) * (B.y - O.y) - (real_t)(A.y - O.y) * (B.x - O.x);
  755. }
  756. // Returns a list of points on the convex hull in counter-clockwise order.
  757. // Note: the last point in the returned list is the same as the first one.
  758. static Vector<Point2> convex_hull_2d(Vector<Point2> P) {
  759. int n = P.size(), k = 0;
  760. Vector<Point2> H;
  761. H.resize(2 * n);
  762. // Sort points lexicographically.
  763. P.sort();
  764. // Build lower hull.
  765. for (int i = 0; i < n; ++i) {
  766. while (k >= 2 && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0)
  767. k--;
  768. H.write[k++] = P[i];
  769. }
  770. // Build upper hull.
  771. for (int i = n - 2, t = k + 1; i >= 0; i--) {
  772. while (k >= t && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0)
  773. k--;
  774. H.write[k++] = P[i];
  775. }
  776. H.resize(k);
  777. return H;
  778. }
  779. static Vector<Vector<Vector2>> decompose_polygon_in_convex(Vector<Point2> polygon);
  780. static MeshData build_convex_mesh(const Vector<Plane> &p_planes);
  781. static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
  782. static Vector<Plane> build_box_planes(const Vector3 &p_extents);
  783. static Vector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
  784. 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);
  785. static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size);
  786. static Vector<Vector3> compute_convex_mesh_points(const Plane *p_planes, int p_plane_count);
  787. private:
  788. static Vector<Vector<Point2>> _polypaths_do_operation(PolyBooleanOperation p_op, const Vector<Point2> &p_polypath_a, const Vector<Point2> &p_polypath_b, bool is_a_open = false);
  789. static Vector<Vector<Point2>> _polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type);
  790. };
  791. #endif // GEOMETRY_H