geometry.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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_2d.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. #define FINDMINMAX(x0, x1, x2, min, max) \
  788. min = max = x0; \
  789. if (x1 < min) \
  790. min = x1; \
  791. if (x1 > max) \
  792. max = x1; \
  793. if (x2 < min) \
  794. min = x2; \
  795. if (x2 > max) \
  796. max = x2;
  797. _FORCE_INLINE_ static bool planeBoxOverlap(Vector3 normal, float d, Vector3 maxbox) {
  798. int q;
  799. Vector3 vmin, vmax;
  800. for (q = 0; q <= 2; q++) {
  801. if (normal[q] > 0.0f) {
  802. vmin[q] = -maxbox[q];
  803. vmax[q] = maxbox[q];
  804. } else {
  805. vmin[q] = maxbox[q];
  806. vmax[q] = -maxbox[q];
  807. }
  808. }
  809. if (normal.dot(vmin) + d > 0.0f)
  810. return false;
  811. if (normal.dot(vmax) + d >= 0.0f)
  812. return true;
  813. return false;
  814. }
  815. /*======================== X-tests ========================*/
  816. #define AXISTEST_X01(a, b, fa, fb) \
  817. p0 = a * v0.y - b * v0.z; \
  818. p2 = a * v2.y - b * v2.z; \
  819. if (p0 < p2) { \
  820. min = p0; \
  821. max = p2; \
  822. } else { \
  823. min = p2; \
  824. max = p0; \
  825. } \
  826. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  827. if (min > rad || max < -rad) \
  828. return false;
  829. #define AXISTEST_X2(a, b, fa, fb) \
  830. p0 = a * v0.y - b * v0.z; \
  831. p1 = a * v1.y - b * v1.z; \
  832. if (p0 < p1) { \
  833. min = p0; \
  834. max = p1; \
  835. } else { \
  836. min = p1; \
  837. max = p0; \
  838. } \
  839. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  840. if (min > rad || max < -rad) \
  841. return false;
  842. /*======================== Y-tests ========================*/
  843. #define AXISTEST_Y02(a, b, fa, fb) \
  844. p0 = -a * v0.x + b * v0.z; \
  845. p2 = -a * v2.x + b * v2.z; \
  846. if (p0 < p2) { \
  847. min = p0; \
  848. max = p2; \
  849. } else { \
  850. min = p2; \
  851. max = p0; \
  852. } \
  853. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  854. if (min > rad || max < -rad) \
  855. return false;
  856. #define AXISTEST_Y1(a, b, fa, fb) \
  857. p0 = -a * v0.x + b * v0.z; \
  858. p1 = -a * v1.x + b * v1.z; \
  859. if (p0 < p1) { \
  860. min = p0; \
  861. max = p1; \
  862. } else { \
  863. min = p1; \
  864. max = p0; \
  865. } \
  866. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  867. if (min > rad || max < -rad) \
  868. return false;
  869. /*======================== Z-tests ========================*/
  870. #define AXISTEST_Z12(a, b, fa, fb) \
  871. p1 = a * v1.x - b * v1.y; \
  872. p2 = a * v2.x - b * v2.y; \
  873. if (p2 < p1) { \
  874. min = p2; \
  875. max = p1; \
  876. } else { \
  877. min = p1; \
  878. max = p2; \
  879. } \
  880. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  881. if (min > rad || max < -rad) \
  882. return false;
  883. #define AXISTEST_Z0(a, b, fa, fb) \
  884. p0 = a * v0.x - b * v0.y; \
  885. p1 = a * v1.x - b * v1.y; \
  886. if (p0 < p1) { \
  887. min = p0; \
  888. max = p1; \
  889. } else { \
  890. min = p1; \
  891. max = p0; \
  892. } \
  893. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  894. if (min > rad || max < -rad) \
  895. return false;
  896. _FORCE_INLINE_ static bool triangle_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
  897. /* use separating axis theorem to test overlap between triangle and box */
  898. /* need to test for overlap in these directions: */
  899. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  900. /* we do not even need to test these) */
  901. /* 2) normal of the triangle */
  902. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  903. /* this gives 3x3=9 more tests */
  904. Vector3 v0, v1, v2;
  905. float min, max, d, p0, p1, p2, rad, fex, fey, fez;
  906. Vector3 normal, e0, e1, e2;
  907. /* This is the fastest branch on Sun */
  908. /* move everything so that the boxcenter is in (0,0,0) */
  909. v0 = triverts[0] - boxcenter;
  910. v1 = triverts[1] - boxcenter;
  911. v2 = triverts[2] - boxcenter;
  912. /* compute triangle edges */
  913. e0 = v1 - v0; /* tri edge 0 */
  914. e1 = v2 - v1; /* tri edge 1 */
  915. e2 = v0 - v2; /* tri edge 2 */
  916. /* Bullet 3: */
  917. /* test the 9 tests first (this was faster) */
  918. fex = Math::abs(e0.x);
  919. fey = Math::abs(e0.y);
  920. fez = Math::abs(e0.z);
  921. AXISTEST_X01(e0.z, e0.y, fez, fey);
  922. AXISTEST_Y02(e0.z, e0.x, fez, fex);
  923. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  924. fex = Math::abs(e1.x);
  925. fey = Math::abs(e1.y);
  926. fez = Math::abs(e1.z);
  927. AXISTEST_X01(e1.z, e1.y, fez, fey);
  928. AXISTEST_Y02(e1.z, e1.x, fez, fex);
  929. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  930. fex = Math::abs(e2.x);
  931. fey = Math::abs(e2.y);
  932. fez = Math::abs(e2.z);
  933. AXISTEST_X2(e2.z, e2.y, fez, fey);
  934. AXISTEST_Y1(e2.z, e2.x, fez, fex);
  935. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  936. /* Bullet 1: */
  937. /* first test overlap in the {x,y,z}-directions */
  938. /* find min, max of the triangle each direction, and test for overlap in */
  939. /* that direction -- this is equivalent to testing a minimal AABB around */
  940. /* the triangle against the AABB */
  941. /* test in X-direction */
  942. FINDMINMAX(v0.x, v1.x, v2.x, min, max);
  943. if (min > boxhalfsize.x || max < -boxhalfsize.x)
  944. return false;
  945. /* test in Y-direction */
  946. FINDMINMAX(v0.y, v1.y, v2.y, min, max);
  947. if (min > boxhalfsize.y || max < -boxhalfsize.y)
  948. return false;
  949. /* test in Z-direction */
  950. FINDMINMAX(v0.z, v1.z, v2.z, min, max);
  951. if (min > boxhalfsize.z || max < -boxhalfsize.z)
  952. return false;
  953. /* Bullet 2: */
  954. /* test if the box intersects the plane of the triangle */
  955. /* compute plane equation of triangle: normal*x+d=0 */
  956. normal = e0.cross(e1);
  957. d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
  958. return planeBoxOverlap(normal, d, boxhalfsize); /* if true, box and triangle overlaps */
  959. }
  960. static Vector<Point2i> pack_rects(const Vector<Size2i> &p_sizes, const Size2i &p_atlas_size);
  961. static Vector<Vector3i> partial_pack_rects(const Vector<Vector2i> &p_sizes, const Size2i &p_atlas_size);
  962. static Vector<uint32_t> generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative);
  963. static Vector<int8_t> generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative);
  964. static Vector3 triangle_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_pos) {
  965. Vector3 v0 = p_b - p_a;
  966. Vector3 v1 = p_c - p_a;
  967. Vector3 v2 = p_pos - p_a;
  968. float d00 = v0.dot(v0);
  969. float d01 = v0.dot(v1);
  970. float d11 = v1.dot(v1);
  971. float d20 = v2.dot(v0);
  972. float d21 = v2.dot(v1);
  973. float denom = (d00 * d11 - d01 * d01);
  974. if (denom == 0) {
  975. return Vector3(); //invalid triangle, return empty
  976. }
  977. float v = (d11 * d20 - d01 * d21) / denom;
  978. float w = (d00 * d21 - d01 * d20) / denom;
  979. float u = 1.0f - v - w;
  980. return Vector3(u, v, w);
  981. }
  982. 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) {
  983. Vector3 vap = p_pos - p_a;
  984. Vector3 vbp = p_pos - p_b;
  985. Vector3 vab = p_b - p_a;
  986. Vector3 vac = p_c - p_a;
  987. Vector3 vad = p_d - p_a;
  988. Vector3 vbc = p_c - p_b;
  989. Vector3 vbd = p_d - p_b;
  990. // ScTP computes the scalar triple product
  991. #define STP(m_a, m_b, m_c) ((m_a).dot((m_b).cross((m_c))))
  992. float va6 = STP(vbp, vbd, vbc);
  993. float vb6 = STP(vap, vac, vad);
  994. float vc6 = STP(vap, vad, vab);
  995. float vd6 = STP(vap, vab, vac);
  996. float v6 = 1 / STP(vab, vac, vad);
  997. return Color(va6 * v6, vb6 * v6, vc6 * v6, vd6 * v6);
  998. #undef STP
  999. }
  1000. private:
  1001. 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);
  1002. static Vector<Vector<Point2>> _polypath_offset(const Vector<Point2> &p_polypath, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type);
  1003. };
  1004. #endif // GEOMETRY_H