geometry.h 32 KB

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