Blender V5.0
math_geom.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "BLI_array.hh"
12#include "BLI_math_base.h"
13#include "BLI_math_base.hh"
14#include "BLI_math_geom.h"
15
16#include "BLI_math_bits.h"
17#include "BLI_math_matrix.h"
18#include "BLI_math_rotation.h"
19#include "BLI_math_vector.h"
20#include "BLI_utildefines.h"
21
22#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
23
24/********************************** Polygons *********************************/
25
26void cross_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
27{
28 float n1[3], n2[3];
29
30 n1[0] = v1[0] - v2[0];
31 n2[0] = v2[0] - v3[0];
32 n1[1] = v1[1] - v2[1];
33 n2[1] = v2[1] - v3[1];
34 n1[2] = v1[2] - v2[2];
35 n2[2] = v2[2] - v3[2];
36 n[0] = n1[1] * n2[2] - n1[2] * n2[1];
37 n[1] = n1[2] * n2[0] - n1[0] * n2[2];
38 n[2] = n1[0] * n2[1] - n1[1] * n2[0];
39}
40
41float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
42{
43 float n1[3], n2[3];
44
45 n1[0] = v1[0] - v2[0];
46 n2[0] = v2[0] - v3[0];
47 n1[1] = v1[1] - v2[1];
48 n2[1] = v2[1] - v3[1];
49 n1[2] = v1[2] - v2[2];
50 n2[2] = v2[2] - v3[2];
51 n[0] = n1[1] * n2[2] - n1[2] * n2[1];
52 n[1] = n1[2] * n2[0] - n1[0] * n2[2];
53 n[2] = n1[0] * n2[1] - n1[1] * n2[0];
54
55 return normalize_v3(n);
56}
57
59 float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
60{
61 /* real cross! */
62 float n1[3], n2[3];
63
64 n1[0] = v1[0] - v3[0];
65 n1[1] = v1[1] - v3[1];
66 n1[2] = v1[2] - v3[2];
67
68 n2[0] = v2[0] - v4[0];
69 n2[1] = v2[1] - v4[1];
70 n2[2] = v2[2] - v4[2];
71
72 n[0] = n1[1] * n2[2] - n1[2] * n2[1];
73 n[1] = n1[2] * n2[0] - n1[0] * n2[2];
74 n[2] = n1[0] * n2[1] - n1[1] * n2[0];
75
76 return normalize_v3(n);
77}
78
79float normal_poly_v3(float n[3], const float verts[][3], uint nr)
80{
81 cross_poly_v3(n, verts, nr);
82 return normalize_v3(n);
83}
84
85float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
86{
87 const float verts[4][3] = {{UNPACK3(v1)}, {UNPACK3(v2)}, {UNPACK3(v3)}, {UNPACK3(v4)}};
88 return area_poly_v3(verts, 4);
89}
90
91float area_squared_quad_v3(const float v1[3],
92 const float v2[3],
93 const float v3[3],
94 const float v4[3])
95{
96 const float verts[4][3] = {{UNPACK3(v1)}, {UNPACK3(v2)}, {UNPACK3(v3)}, {UNPACK3(v4)}};
97 return area_squared_poly_v3(verts, 4);
98}
99
100float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
101{
102 float n[3];
103 cross_tri_v3(n, v1, v2, v3);
104 return len_v3(n) * 0.5f;
105}
106
107float area_squared_tri_v3(const float v1[3], const float v2[3], const float v3[3])
108{
109 float n[3];
110 cross_tri_v3(n, v1, v2, v3);
111 mul_v3_fl(n, 0.5f);
112 return len_squared_v3(n);
113}
114
115float area_tri_signed_v3(const float v1[3],
116 const float v2[3],
117 const float v3[3],
118 const float normal[3])
119{
120 float area, n[3];
121
122 cross_tri_v3(n, v1, v2, v3);
123 area = len_v3(n) * 0.5f;
124
125 /* negate area for flipped triangles */
126 if (dot_v3v3(n, normal) < 0.0f) {
127 area = -area;
128 }
129
130 return area;
131}
132
133float area_poly_v3(const float verts[][3], uint nr)
134{
135 float n[3];
136 cross_poly_v3(n, verts, nr);
137 return len_v3(n) * 0.5f;
138}
139
140float area_squared_poly_v3(const float verts[][3], uint nr)
141{
142 float n[3];
143
144 cross_poly_v3(n, verts, nr);
145 mul_v3_fl(n, 0.5f);
146 return len_squared_v3(n);
147}
148
149float cross_poly_v2(const float verts[][2], uint nr)
150{
151 uint a;
152 float cross;
153 const float *co_curr, *co_prev;
154
155 /* The Trapezium Area Rule */
156 co_prev = verts[nr - 1];
157 co_curr = verts[0];
158 cross = 0.0f;
159 for (a = 0; a < nr; a++) {
160 cross += (co_prev[0] - co_curr[0]) * (co_curr[1] + co_prev[1]);
161 co_prev = co_curr;
162 co_curr += 2;
163 }
164
165 return cross;
166}
167
168void cross_poly_v3(float n[3], const float verts[][3], uint nr)
169{
170 const float *v_prev = verts[nr - 1];
171 const float *v_curr = verts[0];
172 uint i;
173
174 zero_v3(n);
175
176 /* Newell's Method */
177 for (i = 0; i < nr; v_prev = v_curr, v_curr = verts[++i]) {
178 add_newell_cross_v3_v3v3(n, v_prev, v_curr);
179 }
180}
181
182float area_poly_v2(const float verts[][2], uint nr)
183{
184 return fabsf(0.5f * cross_poly_v2(verts, nr));
185}
186
187float area_poly_signed_v2(const float verts[][2], uint nr)
188{
189 return (0.5f * cross_poly_v2(verts, nr));
190}
191
192float area_squared_poly_v2(const float verts[][2], uint nr)
193{
194 float area = area_poly_signed_v2(verts, nr);
195 return area * area;
196}
197
198float cotangent_tri_weight_v3(const float v1[3], const float v2[3], const float v3[3])
199{
200 float a[3], b[3], c[3], c_len;
201
202 sub_v3_v3v3(a, v2, v1);
203 sub_v3_v3v3(b, v3, v1);
204 cross_v3_v3v3(c, a, b);
205
206 c_len = len_v3(c);
207
208 if (c_len > FLT_EPSILON) {
209 return dot_v3v3(a, b) / c_len;
210 }
211
212 return 0.0f;
213}
214
215/********************************* Planes **********************************/
216
217void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
218{
219 copy_v3_v3(r_plane, plane_no);
220 r_plane[3] = -dot_v3v3(r_plane, plane_co);
221}
222
223void plane_to_point_vector_v3(const float plane[4], float r_plane_co[3], float r_plane_no[3])
224{
225 mul_v3_v3fl(r_plane_co, plane, (-plane[3] / len_squared_v3(plane)));
226 copy_v3_v3(r_plane_no, plane);
227}
228
229void plane_to_point_vector_v3_normalized(const float plane[4],
230 float r_plane_co[3],
231 float r_plane_no[3])
232{
233 const float length = normalize_v3_v3(r_plane_no, plane);
234 mul_v3_v3fl(r_plane_co, r_plane_no, (-plane[3] / length));
235}
236
237/********************************* Volume **********************************/
238
239float volume_tetrahedron_v3(const float v1[3],
240 const float v2[3],
241 const float v3[3],
242 const float v4[3])
243{
244 float m[3][3];
245 sub_v3_v3v3(m[0], v1, v2);
246 sub_v3_v3v3(m[1], v2, v3);
247 sub_v3_v3v3(m[2], v3, v4);
248 return fabsf(determinant_m3_array(m)) / 6.0f;
249}
250
251float volume_tetrahedron_signed_v3(const float v1[3],
252 const float v2[3],
253 const float v3[3],
254 const float v4[3])
255{
256 float m[3][3];
257 sub_v3_v3v3(m[0], v1, v2);
258 sub_v3_v3v3(m[1], v2, v3);
259 sub_v3_v3v3(m[2], v3, v4);
260 return determinant_m3_array(m) / 6.0f;
261}
262
263float volume_tri_tetrahedron_signed_v3_6x(const float v1[3], const float v2[3], const float v3[3])
264{
265 float v_cross[3];
266 cross_v3_v3v3(v_cross, v1, v2);
267 float tetra_volume = dot_v3v3(v_cross, v3);
268 return tetra_volume;
269}
270
271float volume_tri_tetrahedron_signed_v3(const float v1[3], const float v2[3], const float v3[3])
272{
273 return volume_tri_tetrahedron_signed_v3_6x(v1, v2, v3) / 6.0f;
274}
275
276/********************************* Distance **********************************/
277
278float dist_squared_to_line_v2(const float p[2], const float l1[2], const float l2[2])
279{
280 float closest[2];
281
282 closest_to_line_v2(closest, p, l1, l2);
283
284 return len_squared_v2v2(closest, p);
285}
286float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2])
287{
288 return sqrtf(dist_squared_to_line_v2(p, l1, l2));
289}
290
291float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
292{
293 float closest[2];
294
296
297 return len_squared_v2v2(closest, p);
298}
299
300float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
301{
302 return sqrtf(dist_squared_to_line_segment_v2(p, l1, l2));
303}
304
305float closest_seg_seg_v2(float r_closest_a[2],
306 float r_closest_b[2],
307 float *r_lambda_a,
308 float *r_lambda_b,
309 const float a1[2],
310 const float a2[2],
311 const float b1[2],
312 const float b2[2])
313{
314 if (isect_seg_seg_v2_simple(a1, a2, b1, b2)) {
315 float intersection[2];
316 isect_line_line_v2_point(a1, a2, b1, b2, intersection);
317 copy_v2_v2(r_closest_a, intersection);
318 copy_v2_v2(r_closest_b, intersection);
319 float tmp[2];
320 *r_lambda_a = closest_to_line_v2(tmp, intersection, a1, a2);
321 *r_lambda_b = closest_to_line_v2(tmp, intersection, b1, b2);
322 const float min_dist_sq = len_squared_v2v2(r_closest_a, r_closest_b);
323 return min_dist_sq;
324 }
325
326 float p1[2], p2[2], p3[2], p4[2];
327 const float lambda1 = closest_to_line_segment_v2(p1, a1, b1, b2);
328 const float lambda2 = closest_to_line_segment_v2(p2, a2, b1, b2);
329 const float lambda3 = closest_to_line_segment_v2(p3, b1, a1, a2);
330 const float lambda4 = closest_to_line_segment_v2(p4, b2, a1, a2);
331 const float dist_sq1 = len_squared_v2v2(p1, a1);
332 const float dist_sq2 = len_squared_v2v2(p2, a2);
333 const float dist_sq3 = len_squared_v2v2(p3, b1);
334 const float dist_sq4 = len_squared_v2v2(p4, b2);
335
336 const float min_dist_sq = min_ffff(dist_sq1, dist_sq2, dist_sq3, dist_sq4);
337 if (min_dist_sq == dist_sq1) {
338 copy_v2_v2(r_closest_a, a1);
339 copy_v2_v2(r_closest_b, p1);
340 *r_lambda_a = 0.0f;
341 *r_lambda_b = lambda1;
342 }
343 else if (min_dist_sq == dist_sq2) {
344 copy_v2_v2(r_closest_a, a2);
345 copy_v2_v2(r_closest_b, p2);
346 *r_lambda_a = 1.0f;
347 *r_lambda_b = lambda2;
348 }
349 else if (min_dist_sq == dist_sq3) {
350 copy_v2_v2(r_closest_a, p3);
351 copy_v2_v2(r_closest_b, b1);
352 *r_lambda_a = lambda3;
353 *r_lambda_b = 0.0f;
354 }
355 else {
356 BLI_assert(min_dist_sq == dist_sq4);
357 copy_v2_v2(r_closest_a, p4);
358 copy_v2_v2(r_closest_b, b2);
359 *r_lambda_a = lambda4;
360 *r_lambda_b = 1.0f;
361 }
362 return min_dist_sq;
363}
364
365float closest_to_line_segment_v2(float r_close[2],
366 const float p[2],
367 const float l1[2],
368 const float l2[2])
369{
370 float lambda, cp[2];
371
372 lambda = closest_to_line_v2(cp, p, l1, l2);
373
374 /* flip checks for !finite case (when segment is a point) */
375 if (lambda <= 0.0f) {
376 copy_v2_v2(r_close, l1);
377 return 0.0f;
378 }
379 if (lambda >= 1.0f) {
380 copy_v2_v2(r_close, l2);
381 return 1.0f;
382 }
383 copy_v2_v2(r_close, cp);
384 return lambda;
385}
386
387float closest_to_line_segment_v3(float r_close[3],
388 const float p[3],
389 const float l1[3],
390 const float l2[3])
391{
392 float lambda, cp[3];
393
394 lambda = closest_to_line_v3(cp, p, l1, l2);
395
396 /* flip checks for !finite case (when segment is a point) */
397 if (lambda <= 0.0f) {
398 copy_v3_v3(r_close, l1);
399 return 0.0f;
400 }
401 if (lambda >= 1.0f) {
402 copy_v3_v3(r_close, l2);
403 return 1.0f;
404 }
405 copy_v3_v3(r_close, cp);
406 return lambda;
407}
408
409float closest_ray_to_segment_v3(const float ray_origin[3],
410 const float ray_direction[3],
411 const float v0[3],
412 const float v1[3],
413 float r_close[3])
414{
415 float lambda;
416 if (!isect_ray_line_v3(ray_origin, ray_direction, v0, v1, &lambda)) {
417 copy_v3_v3(r_close, v0);
418 return 0.0f;
419 }
420
421 if (lambda <= 0.0f) {
422 copy_v3_v3(r_close, v0);
423 return 0.0f;
424 }
425
426 if (lambda >= 1.0f) {
427 copy_v3_v3(r_close, v1);
428 return 1.0f;
429 }
430
431 interp_v3_v3v3(r_close, v0, v1, lambda);
432 return lambda;
433}
434
435void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
436{
437 const float len_sq = len_squared_v3(plane);
438 const float side = plane_point_side_v3(plane, pt);
439 madd_v3_v3v3fl(r_close, pt, plane, -side / len_sq);
440}
441
442void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
443{
444 const float side = plane_point_side_v3(plane, pt);
445 BLI_ASSERT_UNIT_V3(plane);
446 madd_v3_v3v3fl(r_close, pt, plane, -side);
447}
448
449void closest_to_plane3_v3(float r_close[3], const float plane[3], const float pt[3])
450{
451 const float len_sq = len_squared_v3(plane);
452 const float side = dot_v3v3(plane, pt);
453 madd_v3_v3v3fl(r_close, pt, plane, -side / len_sq);
454}
455
456void closest_to_plane3_normalized_v3(float r_close[3], const float plane[3], const float pt[3])
457{
458 const float side = dot_v3v3(plane, pt);
459 BLI_ASSERT_UNIT_V3(plane);
460 madd_v3_v3v3fl(r_close, pt, plane, -side);
461}
462
463float dist_signed_squared_to_plane_v3(const float p[3], const float plane[4])
464{
465 const float len_sq = len_squared_v3(plane);
466 const float side = plane_point_side_v3(plane, p);
467 const float fac = side / len_sq;
468 return copysignf(len_sq * (fac * fac), side);
469}
470float dist_squared_to_plane_v3(const float p[3], const float plane[4])
471{
472 const float len_sq = len_squared_v3(plane);
473 const float side = plane_point_side_v3(plane, p);
474 const float fac = side / len_sq;
475 /* only difference to code above - no 'copysignf' */
476 return len_sq * (fac * fac);
477}
478
479float dist_signed_squared_to_plane3_v3(const float p[3], const float plane[3])
480{
481 const float len_sq = len_squared_v3(plane);
482 const float side = dot_v3v3(plane, p); /* only difference with 'plane[4]' version */
483 const float fac = side / len_sq;
484 return copysignf(len_sq * (fac * fac), side);
485}
486float dist_squared_to_plane3_v3(const float p[3], const float plane[3])
487{
488 const float len_sq = len_squared_v3(plane);
489 const float side = dot_v3v3(plane, p); /* only difference with 'plane[4]' version */
490 const float fac = side / len_sq;
491 /* only difference to code above - no 'copysignf' */
492 return len_sq * (fac * fac);
493}
494
495float dist_signed_to_plane_v3(const float p[3], const float plane[4])
496{
497 const float len_sq = len_squared_v3(plane);
498 const float side = plane_point_side_v3(plane, p);
499 const float fac = side / len_sq;
500 return sqrtf(len_sq) * fac;
501}
502float dist_to_plane_v3(const float p[3], const float plane[4])
503{
504 return fabsf(dist_signed_to_plane_v3(p, plane));
505}
506
507float dist_signed_to_plane3_v3(const float p[3], const float plane[3])
508{
509 const float len_sq = len_squared_v3(plane);
510 const float side = dot_v3v3(plane, p); /* only difference with 'plane[4]' version */
511 const float fac = side / len_sq;
512 return sqrtf(len_sq) * fac;
513}
514float dist_to_plane3_v3(const float p[3], const float plane[3])
515{
516 return fabsf(dist_signed_to_plane3_v3(p, plane));
517}
518
519float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
520{
521 float closest[3];
522
524
525 return len_squared_v3v3(closest, p);
526}
527
528float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
529{
530 return sqrtf(dist_squared_to_line_segment_v3(p, l1, l2));
531}
532
533float dist_squared_to_line_v3(const float p[3], const float l1[3], const float l2[3])
534{
535 float closest[3];
536
537 closest_to_line_v3(closest, p, l1, l2);
538
539 return len_squared_v3v3(closest, p);
540}
541float dist_to_line_v3(const float p[3], const float l1[3], const float l2[3])
542{
543 return sqrtf(dist_squared_to_line_v3(p, l1, l2));
544}
545
547 const float v1[3],
548 const float v2[3],
549 const float v3[3],
550 const float axis_ref[3])
551{
552 float dir_a[3], dir_b[3];
553 float plane_a[3], plane_b[3];
554 float dist_a, dist_b;
555 float axis[3];
556 float s_p_v2[3];
557 bool flip = false;
558
559 sub_v3_v3v3(dir_a, v1, v2);
560 sub_v3_v3v3(dir_b, v3, v2);
561
562 cross_v3_v3v3(axis, dir_a, dir_b);
563
564 if (len_squared_v3(axis) < FLT_EPSILON) {
565 copy_v3_v3(axis, axis_ref);
566 }
567 else if (dot_v3v3(axis, axis_ref) < 0.0f) {
568 /* concave */
569 flip = true;
570 negate_v3(axis);
571 }
572
573 cross_v3_v3v3(plane_a, dir_a, axis);
574 cross_v3_v3v3(plane_b, axis, dir_b);
575
576#if 0
577 plane_from_point_normal_v3(plane_a, v2, plane_a);
578 plane_from_point_normal_v3(plane_b, v2, plane_b);
579
580 dist_a = dist_signed_squared_to_plane_v3(p, plane_a);
581 dist_b = dist_signed_squared_to_plane_v3(p, plane_b);
582#else
583 /* calculate without the planes 4th component to avoid float precision issues */
584 sub_v3_v3v3(s_p_v2, p, v2);
585
586 dist_a = dist_signed_squared_to_plane3_v3(s_p_v2, plane_a);
587 dist_b = dist_signed_squared_to_plane3_v3(s_p_v2, plane_b);
588#endif
589
590 if (flip) {
591 return min_ff(dist_a, dist_b);
592 }
593
594 return max_ff(dist_a, dist_b);
595}
596
597float dist_squared_to_ray_v3_normalized(const float ray_origin[3],
598 const float ray_direction[3],
599 const float co[3])
600{
601 float origin_to_co[3];
602 sub_v3_v3v3(origin_to_co, co, ray_origin);
603
604 float origin_to_proj[3];
605 project_v3_v3v3_normalized(origin_to_proj, origin_to_co, ray_direction);
606
607 float co_projected_on_ray[3];
608 add_v3_v3v3(co_projected_on_ray, ray_origin, origin_to_proj);
609
610 return len_squared_v3v3(co, co_projected_on_ray);
611}
612
613float dist_squared_ray_to_seg_v3(const float ray_origin[3],
614 const float ray_direction[3],
615 const float v0[3],
616 const float v1[3],
617 float r_point[3],
618 float *r_depth)
619{
620 float lambda, depth;
621 if (isect_ray_line_v3(ray_origin, ray_direction, v0, v1, &lambda)) {
622 if (lambda <= 0.0f) {
623 copy_v3_v3(r_point, v0);
624 }
625 else if (lambda >= 1.0f) {
626 copy_v3_v3(r_point, v1);
627 }
628 else {
629 interp_v3_v3v3(r_point, v0, v1, lambda);
630 }
631 }
632 else {
633 /* has no nearest point, only distance squared. */
634 /* Calculate the distance to the point v0 then */
635 copy_v3_v3(r_point, v0);
636 }
637
638 float dvec[3];
639 sub_v3_v3v3(dvec, r_point, ray_origin);
640 depth = dot_v3v3(dvec, ray_direction);
641
642 if (r_depth) {
643 *r_depth = depth;
644 }
645
646 return len_squared_v3(dvec) - square_f(depth);
647}
648
649void aabb_get_near_far_from_plane(const float plane_no[3],
650 const float bbmin[3],
651 const float bbmax[3],
652 float bb_near[3],
653 float bb_afar[3])
654{
655 if (plane_no[0] < 0.0f) {
656 bb_near[0] = bbmax[0];
657 bb_afar[0] = bbmin[0];
658 }
659 else {
660 bb_near[0] = bbmin[0];
661 bb_afar[0] = bbmax[0];
662 }
663 if (plane_no[1] < 0.0f) {
664 bb_near[1] = bbmax[1];
665 bb_afar[1] = bbmin[1];
666 }
667 else {
668 bb_near[1] = bbmin[1];
669 bb_afar[1] = bbmax[1];
670 }
671 if (plane_no[2] < 0.0f) {
672 bb_near[2] = bbmax[2];
673 bb_afar[2] = bbmin[2];
674 }
675 else {
676 bb_near[2] = bbmin[2];
677 bb_afar[2] = bbmax[2];
678 }
679}
680
681/* -------------------------------------------------------------------- */
684
686 const float ray_direction[3])
687{
688 DistRayAABB_Precalc nearest_precalc{};
689 copy_v3_v3(nearest_precalc.ray_origin, ray_origin);
690 copy_v3_v3(nearest_precalc.ray_direction, ray_direction);
691
692 for (int i = 0; i < 3; i++) {
693 nearest_precalc.ray_inv_dir[i] = (nearest_precalc.ray_direction[i] != 0.0f) ?
694 (1.0f / nearest_precalc.ray_direction[i]) :
695 FLT_MAX;
696 }
697 return nearest_precalc;
698}
699
701 const float bb_min[3],
702 const float bb_max[3],
703 float r_point[3],
704 float *r_depth)
705{
706 // bool r_axis_closest[3];
707 float local_bvmin[3], local_bvmax[3];
708 aabb_get_near_far_from_plane(data->ray_direction, bb_min, bb_max, local_bvmin, local_bvmax);
709
710 const float tmin[3] = {
711 (local_bvmin[0] - data->ray_origin[0]) * data->ray_inv_dir[0],
712 (local_bvmin[1] - data->ray_origin[1]) * data->ray_inv_dir[1],
713 (local_bvmin[2] - data->ray_origin[2]) * data->ray_inv_dir[2],
714 };
715 const float tmax[3] = {
716 (local_bvmax[0] - data->ray_origin[0]) * data->ray_inv_dir[0],
717 (local_bvmax[1] - data->ray_origin[1]) * data->ray_inv_dir[1],
718 (local_bvmax[2] - data->ray_origin[2]) * data->ray_inv_dir[2],
719 };
720 /* `va` and `vb` are the coordinates of the AABB edge closest to the ray */
721 float va[3], vb[3];
722 /* `rtmin` and `rtmax` are the minimum and maximum distances of the ray hits on the AABB */
723 float rtmin, rtmax;
724 int main_axis;
725
726 if ((tmax[0] <= tmax[1]) && (tmax[0] <= tmax[2])) {
727 rtmax = tmax[0];
728 va[0] = vb[0] = local_bvmax[0];
729 main_axis = 3;
730 // r_axis_closest[0] = neasrest_precalc->ray_direction[0] < 0.0f;
731 }
732 else if ((tmax[1] <= tmax[0]) && (tmax[1] <= tmax[2])) {
733 rtmax = tmax[1];
734 va[1] = vb[1] = local_bvmax[1];
735 main_axis = 2;
736 // r_axis_closest[1] = neasrest_precalc->ray_direction[1] < 0.0f;
737 }
738 else {
739 rtmax = tmax[2];
740 va[2] = vb[2] = local_bvmax[2];
741 main_axis = 1;
742 // r_axis_closest[2] = neasrest_precalc->ray_direction[2] < 0.0f;
743 }
744
745 if ((tmin[0] >= tmin[1]) && (tmin[0] >= tmin[2])) {
746 rtmin = tmin[0];
747 va[0] = vb[0] = local_bvmin[0];
748 main_axis -= 3;
749 // r_axis_closest[0] = neasrest_precalc->ray_direction[0] >= 0.0f;
750 }
751 else if ((tmin[1] >= tmin[0]) && (tmin[1] >= tmin[2])) {
752 rtmin = tmin[1];
753 va[1] = vb[1] = local_bvmin[1];
754 main_axis -= 1;
755 // r_axis_closest[1] = neasrest_precalc->ray_direction[1] >= 0.0f;
756 }
757 else {
758 rtmin = tmin[2];
759 va[2] = vb[2] = local_bvmin[2];
760 main_axis -= 2;
761 // r_axis_closest[2] = neasrest_precalc->ray_direction[2] >= 0.0f;
762 }
763 if (main_axis < 0) {
764 main_axis += 3;
765 }
766
767 /* if rtmin <= rtmax, ray intersect `AABB` */
768 if (rtmin <= rtmax) {
769 float dvec[3];
770 copy_v3_v3(r_point, local_bvmax);
771 sub_v3_v3v3(dvec, local_bvmax, data->ray_origin);
772 *r_depth = dot_v3v3(dvec, data->ray_direction);
773 return 0.0f;
774 }
775
776 if (data->ray_direction[main_axis] >= 0.0f) {
777 va[main_axis] = local_bvmin[main_axis];
778 vb[main_axis] = local_bvmax[main_axis];
779 }
780 else {
781 va[main_axis] = local_bvmax[main_axis];
782 vb[main_axis] = local_bvmin[main_axis];
783 }
784
786 data->ray_origin, data->ray_direction, va, vb, r_point, r_depth);
787}
788
789float dist_squared_ray_to_aabb_v3_simple(const float ray_origin[3],
790 const float ray_direction[3],
791 const float bb_min[3],
792 const float bb_max[3],
793 float r_point[3],
794 float *r_depth)
795{
796 const DistRayAABB_Precalc data = dist_squared_ray_to_aabb_v3_precalc(ray_origin, ray_direction);
797 return dist_squared_ray_to_aabb_v3(&data, bb_min, bb_max, r_point, r_depth);
798}
799
801
802/* -------------------------------------------------------------------- */
805
807 const float projmat[4][4],
808 const float winsize[2],
809 const float mval[2])
810{
811 float win_half[2], relative_mval[2], px[4], py[4];
812
813 mul_v2_v2fl(win_half, winsize, 0.5f);
814 sub_v2_v2v2(precalc->mval, mval, win_half);
815
816 relative_mval[0] = precalc->mval[0] / win_half[0];
817 relative_mval[1] = precalc->mval[1] / win_half[1];
818
819 copy_m4_m4(precalc->pmat, projmat);
820 for (int i = 0; i < 4; i++) {
821 px[i] = precalc->pmat[i][0] - precalc->pmat[i][3] * relative_mval[0];
822 py[i] = precalc->pmat[i][1] - precalc->pmat[i][3] * relative_mval[1];
823
824 precalc->pmat[i][0] *= win_half[0];
825 precalc->pmat[i][1] *= win_half[1];
826 }
827#if 0
828 float projmat_trans[4][4];
829 transpose_m4_m4(projmat_trans, projmat);
831 projmat_trans[0], projmat_trans[1], projmat_trans[3], precalc->ray_origin))
832 {
833 /* Orthographic projection. */
834 isect_plane_plane_v3(px, py, precalc->ray_origin, precalc->ray_direction);
835 }
836 else {
837 /* Perspective projection. */
838 cross_v3_v3v3(precalc->ray_direction, py, px);
839 // normalize_v3(precalc->ray_direction);
840 }
841#else
842 if (!isect_plane_plane_v3(px, py, precalc->ray_origin, precalc->ray_direction)) {
843 /* Matrix with weird co-planar planes. Undetermined origin. */
844 zero_v3(precalc->ray_origin);
845 precalc->ray_direction[0] = precalc->pmat[0][3];
846 precalc->ray_direction[1] = precalc->pmat[1][3];
847 precalc->ray_direction[2] = precalc->pmat[2][3];
848 }
849#endif
850
851 for (int i = 0; i < 3; i++) {
852 precalc->ray_inv_dir[i] = (precalc->ray_direction[i] != 0.0f) ?
853 (1.0f / precalc->ray_direction[i]) :
854 FLT_MAX;
855 }
856}
857
859 const float bbmin[3],
860 const float bbmax[3],
861 bool r_axis_closest[3])
862{
863 float local_bvmin[3], local_bvmax[3];
864 aabb_get_near_far_from_plane(data->ray_direction, bbmin, bbmax, local_bvmin, local_bvmax);
865
866 const float tmin[3] = {
867 (local_bvmin[0] - data->ray_origin[0]) * data->ray_inv_dir[0],
868 (local_bvmin[1] - data->ray_origin[1]) * data->ray_inv_dir[1],
869 (local_bvmin[2] - data->ray_origin[2]) * data->ray_inv_dir[2],
870 };
871 const float tmax[3] = {
872 (local_bvmax[0] - data->ray_origin[0]) * data->ray_inv_dir[0],
873 (local_bvmax[1] - data->ray_origin[1]) * data->ray_inv_dir[1],
874 (local_bvmax[2] - data->ray_origin[2]) * data->ray_inv_dir[2],
875 };
876 /* `va` and `vb` are the coordinates of the AABB edge closest to the ray */
877 float va[3], vb[3];
878 /* `rtmin` and `rtmax` are the minimum and maximum distances of the ray hits on the AABB */
879 float rtmin, rtmax;
880 int main_axis;
881
882 r_axis_closest[0] = false;
883 r_axis_closest[1] = false;
884 r_axis_closest[2] = false;
885
886 if ((tmax[0] <= tmax[1]) && (tmax[0] <= tmax[2])) {
887 rtmax = tmax[0];
888 va[0] = vb[0] = local_bvmax[0];
889 main_axis = 3;
890 r_axis_closest[0] = data->ray_direction[0] < 0.0f;
891 }
892 else if ((tmax[1] <= tmax[0]) && (tmax[1] <= tmax[2])) {
893 rtmax = tmax[1];
894 va[1] = vb[1] = local_bvmax[1];
895 main_axis = 2;
896 r_axis_closest[1] = data->ray_direction[1] < 0.0f;
897 }
898 else {
899 rtmax = tmax[2];
900 va[2] = vb[2] = local_bvmax[2];
901 main_axis = 1;
902 r_axis_closest[2] = data->ray_direction[2] < 0.0f;
903 }
904
905 if ((tmin[0] >= tmin[1]) && (tmin[0] >= tmin[2])) {
906 rtmin = tmin[0];
907 va[0] = vb[0] = local_bvmin[0];
908 main_axis -= 3;
909 r_axis_closest[0] = data->ray_direction[0] >= 0.0f;
910 }
911 else if ((tmin[1] >= tmin[0]) && (tmin[1] >= tmin[2])) {
912 rtmin = tmin[1];
913 va[1] = vb[1] = local_bvmin[1];
914 main_axis -= 1;
915 r_axis_closest[1] = data->ray_direction[1] >= 0.0f;
916 }
917 else {
918 rtmin = tmin[2];
919 va[2] = vb[2] = local_bvmin[2];
920 main_axis -= 2;
921 r_axis_closest[2] = data->ray_direction[2] >= 0.0f;
922 }
923 if (main_axis < 0) {
924 main_axis += 3;
925 }
926
927 /* if rtmin <= rtmax, ray intersect `AABB` */
928 if (rtmin <= rtmax) {
929 return 0;
930 }
931
932 if (data->ray_direction[main_axis] >= 0.0f) {
933 va[main_axis] = local_bvmin[main_axis];
934 vb[main_axis] = local_bvmax[main_axis];
935 }
936 else {
937 va[main_axis] = local_bvmax[main_axis];
938 vb[main_axis] = local_bvmin[main_axis];
939 }
940 float scale = fabsf(local_bvmax[main_axis] - local_bvmin[main_axis]);
941
942 float va2d[2] = {
943 (dot_m4_v3_row_x(data->pmat, va) + data->pmat[3][0]),
944 (dot_m4_v3_row_y(data->pmat, va) + data->pmat[3][1]),
945 };
946 float vb2d[2] = {
947 (va2d[0] + data->pmat[main_axis][0] * scale),
948 (va2d[1] + data->pmat[main_axis][1] * scale),
949 };
950
951 float w_a = mul_project_m4_v3_zfac(data->pmat, va);
952 if (w_a != 1.0f) {
953 /* Perspective Projection. */
954 float w_b = w_a + data->pmat[main_axis][3] * scale;
955 va2d[0] /= w_a;
956 va2d[1] /= w_a;
957 vb2d[0] /= w_b;
958 vb2d[1] /= w_b;
959 }
960
961 float dvec[2], edge[2], lambda, rdist_sq;
962 sub_v2_v2v2(dvec, data->mval, va2d);
963 sub_v2_v2v2(edge, vb2d, va2d);
964 lambda = dot_v2v2(dvec, edge);
965 if (lambda != 0.0f) {
966 lambda /= len_squared_v2(edge);
967 if (lambda <= 0.0f) {
968 rdist_sq = len_squared_v2v2(data->mval, va2d);
969 r_axis_closest[main_axis] = true;
970 }
971 else if (lambda >= 1.0f) {
972 rdist_sq = len_squared_v2v2(data->mval, vb2d);
973 r_axis_closest[main_axis] = false;
974 }
975 else {
976 madd_v2_v2fl(va2d, edge, lambda);
977 rdist_sq = len_squared_v2v2(data->mval, va2d);
978 r_axis_closest[main_axis] = lambda < 0.5f;
979 }
980 }
981 else {
982 rdist_sq = len_squared_v2v2(data->mval, va2d);
983 }
984
985 return rdist_sq;
986}
987
988float dist_squared_to_projected_aabb_simple(const float projmat[4][4],
989 const float winsize[2],
990 const float mval[2],
991 const float bbmin[3],
992 const float bbmax[3])
993{
995 dist_squared_to_projected_aabb_precalc(&data, projmat, winsize, mval);
996
997 bool dummy[3] = {true, true, true};
998 return dist_squared_to_projected_aabb(&data, bbmin, bbmax, dummy);
999}
1000
1002
1003float dist_seg_seg_v2(const float a1[3], const float a2[3], const float b1[3], const float b2[3])
1004{
1005 if (isect_seg_seg_v2_simple(a1, a2, b1, b2)) {
1006 return 0.0f;
1007 }
1008 const float d1 = dist_squared_to_line_segment_v2(a1, b1, b2);
1009 const float d2 = dist_squared_to_line_segment_v2(a2, b1, b2);
1010 const float d3 = dist_squared_to_line_segment_v2(b1, a1, a2);
1011 const float d4 = dist_squared_to_line_segment_v2(b2, a1, a2);
1012 return sqrtf(min_ffff(d1, d2, d3, d4));
1013}
1014
1016 float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
1017{
1018 /* Adapted from "Real-Time Collision Detection" by Christer Ericson,
1019 * published by Morgan Kaufmann Publishers, copyright 2005 Elsevier Inc. */
1020
1021 float ab[3], ac[3], ap[3], d1, d2;
1022 float bp[3], d3, d4, vc, cp[3], d5, d6, vb, va;
1023 float denom, v, w;
1024
1025 /* Check if P in vertex region outside A */
1026 sub_v3_v3v3(ab, v2, v1);
1027 sub_v3_v3v3(ac, v3, v1);
1028 sub_v3_v3v3(ap, p, v1);
1029 d1 = dot_v3v3(ab, ap);
1030 d2 = dot_v3v3(ac, ap);
1031 if (d1 <= 0.0f && d2 <= 0.0f) {
1032 /* barycentric coordinates (1,0,0) */
1033 copy_v3_v3(r, v1);
1034 return;
1035 }
1036
1037 /* Check if P in vertex region outside B */
1038 sub_v3_v3v3(bp, p, v2);
1039 d3 = dot_v3v3(ab, bp);
1040 d4 = dot_v3v3(ac, bp);
1041 if (d3 >= 0.0f && d4 <= d3) {
1042 /* barycentric coordinates (0,1,0) */
1043 copy_v3_v3(r, v2);
1044 return;
1045 }
1046 /* Check if P in edge region of AB, if so return projection of P onto AB */
1047 vc = d1 * d4 - d3 * d2;
1048 if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) {
1049 v = d1 / (d1 - d3);
1050 /* barycentric coordinates (1-v,v,0) */
1051 madd_v3_v3v3fl(r, v1, ab, v);
1052 return;
1053 }
1054 /* Check if P in vertex region outside C */
1055 sub_v3_v3v3(cp, p, v3);
1056 d5 = dot_v3v3(ab, cp);
1057 d6 = dot_v3v3(ac, cp);
1058 if (d6 >= 0.0f && d5 <= d6) {
1059 /* barycentric coordinates (0,0,1) */
1060 copy_v3_v3(r, v3);
1061 return;
1062 }
1063 /* Check if P in edge region of AC, if so return projection of P onto AC */
1064 vb = d5 * d2 - d1 * d6;
1065 if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) {
1066 w = d2 / (d2 - d6);
1067 /* barycentric coordinates (1-w,0,w) */
1068 madd_v3_v3v3fl(r, v1, ac, w);
1069 return;
1070 }
1071 /* Check if P in edge region of BC, if so return projection of P onto BC */
1072 va = d3 * d6 - d5 * d4;
1073 if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) {
1074 w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
1075 /* barycentric coordinates (0,1-w,w) */
1076 sub_v3_v3v3(r, v3, v2);
1077 mul_v3_fl(r, w);
1078 add_v3_v3(r, v2);
1079 return;
1080 }
1081
1082 /* P inside face region. Compute Q through its barycentric coordinates (u,v,w) */
1083 denom = 1.0f / (va + vb + vc);
1084 v = vb * denom;
1085 w = vc * denom;
1086
1087 /* = u*a + v*b + w*c, u = va * denom = 1.0f - v - w */
1088 /* ac * w */
1089 mul_v3_fl(ac, w);
1090 /* a + ab * v */
1091 madd_v3_v3v3fl(r, v1, ab, v);
1092 /* a + ab * v + ac * w */
1093 add_v3_v3(r, ac);
1094}
1095
1096/******************************* Intersection ********************************/
1097
1098int isect_seg_seg_v2_int(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
1099{
1100 float div, lambda, mu;
1101
1102 div = float((v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]));
1103 if (div == 0.0f) {
1105 }
1106
1107 lambda = float((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
1108
1109 mu = float((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
1110
1111 if (lambda >= 0.0f && lambda <= 1.0f && mu >= 0.0f && mu <= 1.0f) {
1112 if (lambda == 0.0f || lambda == 1.0f || mu == 0.0f || mu == 1.0f) {
1113 return ISECT_LINE_LINE_EXACT;
1114 }
1115 return ISECT_LINE_LINE_CROSS;
1116 }
1117 return ISECT_LINE_LINE_NONE;
1118}
1119
1121 const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
1122{
1123 float s10[2], s32[2];
1124 float div;
1125
1126 sub_v2_v2v2(s10, v1, v0);
1127 sub_v2_v2v2(s32, v3, v2);
1128
1129 div = cross_v2v2(s10, s32);
1130 if (div != 0.0f) {
1131 const float u = cross_v2v2(v1, v0);
1132 const float v = cross_v2v2(v3, v2);
1133
1134 r_vi[0] = ((s32[0] * u) - (s10[0] * v)) / div;
1135 r_vi[1] = ((s32[1] * u) - (s10[1] * v)) / div;
1136
1137 return ISECT_LINE_LINE_CROSS;
1138 }
1139
1141}
1142
1143int isect_seg_seg_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
1144{
1145 float div, lambda, mu;
1146
1147 div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
1148 if (div == 0.0f) {
1150 }
1151
1152 lambda = (float(v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
1153
1154 mu = (float(v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
1155
1156 if (lambda >= 0.0f && lambda <= 1.0f && mu >= 0.0f && mu <= 1.0f) {
1157 if (lambda == 0.0f || lambda == 1.0f || mu == 0.0f || mu == 1.0f) {
1158 return ISECT_LINE_LINE_EXACT;
1159 }
1160 return ISECT_LINE_LINE_CROSS;
1161 }
1162 return ISECT_LINE_LINE_NONE;
1163}
1164
1165void isect_seg_seg_v3(const float a0[3],
1166 const float a1[3],
1167 const float b0[3],
1168 const float b1[3],
1169 float r_a[3],
1170 float r_b[3])
1171{
1172 float fac_a, fac_b;
1173 float a_dir[3], b_dir[3], a0b0[3], crs_ab[3];
1174 sub_v3_v3v3(a_dir, a1, a0);
1175 sub_v3_v3v3(b_dir, b1, b0);
1176 sub_v3_v3v3(a0b0, b0, a0);
1177 cross_v3_v3v3(crs_ab, b_dir, a_dir);
1178 const float nlen = len_squared_v3(crs_ab);
1179
1180 if (nlen == 0.0f) {
1181 /* Parallel Lines */
1182 /* In this case return any point that
1183 * is between the closest segments. */
1184 float a0b1[3], a1b0[3], len_a, len_b, fac1, fac2;
1185 sub_v3_v3v3(a0b1, b1, a0);
1186 sub_v3_v3v3(a1b0, b0, a1);
1187 len_a = len_squared_v3(a_dir);
1188 len_b = len_squared_v3(b_dir);
1189
1190 if (len_a) {
1191 fac1 = dot_v3v3(a0b0, a_dir);
1192 fac2 = dot_v3v3(a0b1, a_dir);
1193 CLAMP(fac1, 0.0f, len_a);
1194 CLAMP(fac2, 0.0f, len_a);
1195 fac_a = (fac1 + fac2) / (2 * len_a);
1196 }
1197 else {
1198 fac_a = 0.0f;
1199 }
1200
1201 if (len_b) {
1202 fac1 = -dot_v3v3(a0b0, b_dir);
1203 fac2 = -dot_v3v3(a1b0, b_dir);
1204 CLAMP(fac1, 0.0f, len_b);
1205 CLAMP(fac2, 0.0f, len_b);
1206 fac_b = (fac1 + fac2) / (2 * len_b);
1207 }
1208 else {
1209 fac_b = 0.0f;
1210 }
1211 }
1212 else {
1213 float c[3], cray[3];
1214 sub_v3_v3v3(c, crs_ab, a0b0);
1215
1216 cross_v3_v3v3(cray, c, b_dir);
1217 fac_a = dot_v3v3(cray, crs_ab) / nlen;
1218
1219 cross_v3_v3v3(cray, c, a_dir);
1220 fac_b = dot_v3v3(cray, crs_ab) / nlen;
1221
1222 CLAMP(fac_a, 0.0f, 1.0f);
1223 CLAMP(fac_b, 0.0f, 1.0f);
1224 }
1225
1226 madd_v3_v3v3fl(r_a, a0, a_dir, fac_a);
1227 madd_v3_v3v3fl(r_b, b0, b_dir, fac_b);
1228}
1229
1230int isect_seg_seg_v2_point_ex(const float v0[2],
1231 const float v1[2],
1232 const float v2[2],
1233 const float v3[2],
1234 const float endpoint_bias,
1235 float r_vi[2])
1236{
1237 float s10[2], s32[2], s30[2], d;
1238 const float eps = 1e-6f;
1239 const float endpoint_min = -endpoint_bias;
1240 const float endpoint_max = endpoint_bias + 1.0f;
1241
1242 sub_v2_v2v2(s10, v1, v0);
1243 sub_v2_v2v2(s32, v3, v2);
1244 sub_v2_v2v2(s30, v3, v0);
1245
1246 d = cross_v2v2(s10, s32);
1247
1248 if (d != 0) {
1249 float u, v;
1250
1251 u = cross_v2v2(s30, s32) / d;
1252 v = cross_v2v2(s10, s30) / d;
1253
1254 if ((u >= endpoint_min && u <= endpoint_max) && (v >= endpoint_min && v <= endpoint_max)) {
1255 /* intersection */
1256 float vi_test[2];
1257 float s_vi_v2[2];
1258
1259 madd_v2_v2v2fl(vi_test, v0, s10, u);
1260
1261 /* When 'd' approaches zero, float precision lets non-overlapping co-linear segments
1262 * detect as an intersection. So re-calculate 'v' to ensure the point overlaps both.
1263 * see #45123 */
1264
1265 /* inline since we have most vars already */
1266#if 0
1267 v = line_point_factor_v2(ix_test, v2, v3);
1268#else
1269 sub_v2_v2v2(s_vi_v2, vi_test, v2);
1270 v = (dot_v2v2(s32, s_vi_v2) / dot_v2v2(s32, s32));
1271#endif
1272 if (v >= endpoint_min && v <= endpoint_max) {
1273 copy_v2_v2(r_vi, vi_test);
1274 return 1;
1275 }
1276 }
1277
1278 /* out of segment intersection */
1279 return -1;
1280 }
1281
1282 if ((cross_v2v2(s10, s30) == 0.0f) && (cross_v2v2(s32, s30) == 0.0f)) {
1283 /* equal lines */
1284 float s20[2];
1285 float u_a, u_b;
1286
1287 if (equals_v2v2(v0, v1)) {
1288 if (len_squared_v2v2(v2, v3) > square_f(eps)) {
1289 /* use non-point segment as basis */
1290 std::swap(v0, v2);
1291 std::swap(v1, v3);
1292
1293 sub_v2_v2v2(s10, v1, v0);
1294 sub_v2_v2v2(s30, v3, v0);
1295 }
1296 else { /* both of segments are points */
1297 if (equals_v2v2(v0, v2)) { /* points are equal */
1298 copy_v2_v2(r_vi, v0);
1299 return 1;
1300 }
1301
1302 /* two different points */
1303 return -1;
1304 }
1305 }
1306
1307 sub_v2_v2v2(s20, v2, v0);
1308
1309 u_a = dot_v2v2(s20, s10) / dot_v2v2(s10, s10);
1310 u_b = dot_v2v2(s30, s10) / dot_v2v2(s10, s10);
1311
1312 if (u_a > u_b) {
1313 std::swap(u_a, u_b);
1314 }
1315
1316 if (u_a > endpoint_max || u_b < endpoint_min) {
1317 /* non-overlapping segments */
1318 return -1;
1319 }
1320 if (max_ff(0.0f, u_a) == min_ff(1.0f, u_b)) {
1321 /* one common point: can return result */
1322 madd_v2_v2v2fl(r_vi, v0, s10, max_ff(0, u_a));
1323 return 1;
1324 }
1325 }
1326
1327 /* lines are collinear */
1328 return -1;
1329}
1330
1332 const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
1333{
1334 const float endpoint_bias = 1e-6f;
1335 return isect_seg_seg_v2_point_ex(v0, v1, v2, v3, endpoint_bias, r_vi);
1336}
1337
1338bool isect_seg_seg_v2_simple(const float v1[2],
1339 const float v2[2],
1340 const float v3[2],
1341 const float v4[2])
1342{
1343#define CCW(A, B, C) ((C[1] - A[1]) * (B[0] - A[0]) > (B[1] - A[1]) * (C[0] - A[0]))
1344
1345 return CCW(v1, v3, v4) != CCW(v2, v3, v4) && CCW(v1, v2, v3) != CCW(v1, v2, v4);
1346
1347#undef CCW
1348}
1349
1350int isect_seg_seg_v2_lambda_mu_db(const double v1[2],
1351 const double v2[2],
1352 const double v3[2],
1353 const double v4[2],
1354 double *r_lambda,
1355 double *r_mu)
1356{
1357 double div, lambda, mu;
1358
1359 div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]);
1360 if (fabs(div) < DBL_EPSILON) {
1362 }
1363
1364 lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
1365
1366 mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
1367
1368 if (r_lambda) {
1369 *r_lambda = lambda;
1370 }
1371 if (r_mu) {
1372 *r_mu = mu;
1373 }
1374
1375 if (lambda >= 0.0 && lambda <= 1.0 && mu >= 0.0 && mu <= 1.0) {
1376 if (lambda == 0.0 || lambda == 1.0 || mu == 0.0 || mu == 1.0) {
1377 return ISECT_LINE_LINE_EXACT;
1378 }
1379 return ISECT_LINE_LINE_CROSS;
1380 }
1381 return ISECT_LINE_LINE_NONE;
1382}
1383
1384int isect_line_sphere_v3(const float l1[3],
1385 const float l2[3],
1386 const float sp[3],
1387 const float r,
1388 float r_p1[3],
1389 float r_p2[3])
1390{
1391 /* Adapted for use in blender by Campbell Barton, 2011.
1392 *
1393 * http://www.iebele.nl
1394 * `Atelier Iebele Abel <atelier@iebele.nl>` - 2001.
1395 *
1396 * sphere_line_intersection function adapted from:
1397 * http://astronomy.swin.edu.au/pbourke/geometry/sphereline
1398 * `Paul Bourke <pbourke@swin.edu.au>`. */
1399
1400 const float ldir[3] = {
1401 l2[0] - l1[0],
1402 l2[1] - l1[1],
1403 l2[2] - l1[2],
1404 };
1405
1406 const float a = len_squared_v3(ldir);
1407
1408 const float b = 2.0f * (ldir[0] * (l1[0] - sp[0]) + ldir[1] * (l1[1] - sp[1]) +
1409 ldir[2] * (l1[2] - sp[2]));
1410
1411 const float c = len_squared_v3(sp) + len_squared_v3(l1) - (2.0f * dot_v3v3(sp, l1)) - (r * r);
1412
1413 const float i = b * b - 4.0f * a * c;
1414
1415 float mu;
1416
1417 if (i < 0.0f) {
1418 /* no intersections */
1419 return 0;
1420 }
1421 if (i == 0.0f) {
1422 /* one intersection */
1423 mu = -b / (2.0f * a);
1424 madd_v3_v3v3fl(r_p1, l1, ldir, mu);
1425 return 1;
1426 }
1427 if (i > 0.0f) {
1428 const float i_sqrt = sqrtf(i); /* avoid calc twice */
1429
1430 /* first intersection */
1431 mu = (-b + i_sqrt) / (2.0f * a);
1432 madd_v3_v3v3fl(r_p1, l1, ldir, mu);
1433
1434 /* second intersection */
1435 mu = (-b - i_sqrt) / (2.0f * a);
1436 madd_v3_v3v3fl(r_p2, l1, ldir, mu);
1437 return 2;
1438 }
1439
1440 /* math domain error - nan */
1441 return -1;
1442}
1443
1444int isect_line_sphere_v2(const float l1[2],
1445 const float l2[2],
1446 const float sp[2],
1447 const float r,
1448 float r_p1[2],
1449 float r_p2[2])
1450{
1451 /* Keep in sync with #isect_line_sphere_v3. */
1452
1453 const float ldir[2] = {l2[0] - l1[0], l2[1] - l1[1]};
1454
1455 const float a = dot_v2v2(ldir, ldir);
1456
1457 const float b = 2.0f * (ldir[0] * (l1[0] - sp[0]) + ldir[1] * (l1[1] - sp[1]));
1458
1459 const float c = dot_v2v2(sp, sp) + dot_v2v2(l1, l1) - (2.0f * dot_v2v2(sp, l1)) - (r * r);
1460
1461 const float i = b * b - 4.0f * a * c;
1462
1463 float mu;
1464
1465 if (i < 0.0f) {
1466 /* no intersections */
1467 return 0;
1468 }
1469 if (i == 0.0f) {
1470 /* one intersection */
1471 mu = -b / (2.0f * a);
1472 madd_v2_v2v2fl(r_p1, l1, ldir, mu);
1473 return 1;
1474 }
1475 if (i > 0.0f) {
1476 const float i_sqrt = sqrtf(i); /* avoid calc twice */
1477
1478 /* first intersection */
1479 mu = (-b + i_sqrt) / (2.0f * a);
1480 madd_v2_v2v2fl(r_p1, l1, ldir, mu);
1481
1482 /* second intersection */
1483 mu = (-b - i_sqrt) / (2.0f * a);
1484 madd_v2_v2v2fl(r_p2, l1, ldir, mu);
1485 return 2;
1486 }
1487
1488 /* math domain error - nan */
1489 return -1;
1490}
1491
1492bool isect_point_poly_v2(const float pt[2], const float verts[][2], const uint nr)
1493{
1494 /* Keep in sync with #isect_point_poly_v2_int. */
1495
1496 uint i, j;
1497 bool isect = false;
1498 for (i = 0, j = nr - 1; i < nr; j = i++) {
1499 if (((verts[i][1] > pt[1]) != (verts[j][1] > pt[1])) &&
1500 (pt[0] <
1501 (verts[j][0] - verts[i][0]) * (pt[1] - verts[i][1]) / (verts[j][1] - verts[i][1]) +
1502 verts[i][0]))
1503 {
1504 isect = !isect;
1505 }
1506 }
1507 return isect;
1508}
1509bool isect_point_poly_v2_int(const int pt[2], const int verts[][2], const uint nr)
1510{
1511 /* Keep in sync with #isect_point_poly_v2. */
1512
1513 uint i, j;
1514 bool isect = false;
1515 for (i = 0, j = nr - 1; i < nr; j = i++) {
1516 if (((verts[i][1] > pt[1]) != (verts[j][1] > pt[1])) &&
1517 (pt[0] <
1518 (verts[j][0] - verts[i][0]) * (pt[1] - verts[i][1]) / (verts[j][1] - verts[i][1]) +
1519 verts[i][0]))
1520 {
1521 isect = !isect;
1522 }
1523 }
1524 return isect;
1525}
1526
1527/* point in tri */
1528
1529bool isect_point_tri_v2_cw(const float pt[2],
1530 const float v1[2],
1531 const float v2[2],
1532 const float v3[2])
1533{
1534 if (line_point_side_v2(v1, v2, pt) >= 0.0f) {
1535 if (line_point_side_v2(v2, v3, pt) >= 0.0f) {
1536 if (line_point_side_v2(v3, v1, pt) >= 0.0f) {
1537 return true;
1538 }
1539 }
1540 }
1541
1542 return false;
1543}
1544
1545int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
1546{
1547 float side12 = line_point_side_v2(v1, v2, pt);
1548 float side23 = line_point_side_v2(v2, v3, pt);
1549 float side31 = line_point_side_v2(v3, v1, pt);
1550 if (side12 >= 0.0f && side23 >= 0.0f && side31 >= 0.0f) {
1551 return 1;
1552 }
1553 if (side12 <= 0.0f && side23 <= 0.0f && side31 <= 0.0f) {
1554 return -1;
1555 }
1556
1557 return 0;
1558}
1559
1561 const float p[2], const float v1[2], const float v2[2], const float v3[2], const float v4[2])
1562{
1563 float side12 = line_point_side_v2(v1, v2, p);
1564 float side23 = line_point_side_v2(v2, v3, p);
1565 float side34 = line_point_side_v2(v3, v4, p);
1566 float side41 = line_point_side_v2(v4, v1, p);
1567 if (side12 >= 0.0f && side23 >= 0.0f && side34 >= 0.0f && side41 >= 0.0f) {
1568 return 1;
1569 }
1570 if (side12 <= 0.0f && side23 <= 0.0f && side34 <= 0.0f && side41 <= 0.0f) {
1571 return -1;
1572 }
1573 return 0;
1574}
1575
1576bool isect_line_segment_tri_v3(const float p1[3],
1577 const float p2[3],
1578 const float v0[3],
1579 const float v1[3],
1580 const float v2[3],
1581 float *r_lambda,
1582 float r_uv[2])
1583{
1584
1585 float p[3], s[3], d[3], e1[3], e2[3], q[3];
1586 float a, f, u, v;
1587
1588 sub_v3_v3v3(e1, v1, v0);
1589 sub_v3_v3v3(e2, v2, v0);
1590 sub_v3_v3v3(d, p2, p1);
1591
1592 cross_v3_v3v3(p, d, e2);
1593 a = dot_v3v3(e1, p);
1594 if (a == 0.0f) {
1595 return false;
1596 }
1597 f = 1.0f / a;
1598
1599 sub_v3_v3v3(s, p1, v0);
1600
1601 u = f * dot_v3v3(s, p);
1602 if ((u < 0.0f) || (u > 1.0f)) {
1603 return false;
1604 }
1605
1606 cross_v3_v3v3(q, s, e1);
1607
1608 v = f * dot_v3v3(d, q);
1609 if ((v < 0.0f) || ((u + v) > 1.0f)) {
1610 return false;
1611 }
1612
1613 *r_lambda = f * dot_v3v3(e2, q);
1614 if ((*r_lambda < 0.0f) || (*r_lambda > 1.0f)) {
1615 return false;
1616 }
1617
1618 if (r_uv) {
1619 r_uv[0] = u;
1620 r_uv[1] = v;
1621 }
1622
1623 return true;
1624}
1625
1627 const float p2[3],
1628 const float v0[3],
1629 const float v1[3],
1630 const float v2[3],
1631 float *r_lambda,
1632 float r_uv[2],
1633 const float epsilon)
1634{
1635
1636 float p[3], s[3], d[3], e1[3], e2[3], q[3];
1637 float a, f, u, v;
1638
1639 sub_v3_v3v3(e1, v1, v0);
1640 sub_v3_v3v3(e2, v2, v0);
1641 sub_v3_v3v3(d, p2, p1);
1642
1643 cross_v3_v3v3(p, d, e2);
1644 a = dot_v3v3(e1, p);
1645 if (a == 0.0f) {
1646 return false;
1647 }
1648 f = 1.0f / a;
1649
1650 sub_v3_v3v3(s, p1, v0);
1651
1652 u = f * dot_v3v3(s, p);
1653 if ((u < -epsilon) || (u > 1.0f + epsilon)) {
1654 return false;
1655 }
1656
1657 cross_v3_v3v3(q, s, e1);
1658
1659 v = f * dot_v3v3(d, q);
1660 if ((v < -epsilon) || ((u + v) > 1.0f + epsilon)) {
1661 return false;
1662 }
1663
1664 *r_lambda = f * dot_v3v3(e2, q);
1665 if ((*r_lambda < 0.0f) || (*r_lambda > 1.0f)) {
1666 return false;
1667 }
1668
1669 if (r_uv) {
1670 r_uv[0] = u;
1671 r_uv[1] = v;
1672 }
1673
1674 return true;
1675}
1676
1677bool isect_ray_tri_v3(const float ray_origin[3],
1678 const float ray_direction[3],
1679 const float v0[3],
1680 const float v1[3],
1681 const float v2[3],
1682 float *r_lambda,
1683 float r_uv[2])
1684{
1685 /* NOTE(@ideasman42): these values were 0.000001 in 2.4x but for projection snapping on
1686 * a human head `(1BU == 1m)`, subdivision-surface level 2, this gave many errors. */
1687 const float epsilon = 0.00000001f;
1688 float p[3], s[3], e1[3], e2[3], q[3];
1689 float a, f, u, v;
1690
1691 sub_v3_v3v3(e1, v1, v0);
1692 sub_v3_v3v3(e2, v2, v0);
1693
1694 cross_v3_v3v3(p, ray_direction, e2);
1695 a = dot_v3v3(e1, p);
1696 if ((a > -epsilon) && (a < epsilon)) {
1697 return false;
1698 }
1699 f = 1.0f / a;
1700
1701 sub_v3_v3v3(s, ray_origin, v0);
1702
1703 u = f * dot_v3v3(s, p);
1704 if ((u < 0.0f) || (u > 1.0f)) {
1705 return false;
1706 }
1707
1708 cross_v3_v3v3(q, s, e1);
1709
1710 v = f * dot_v3v3(ray_direction, q);
1711 if ((v < 0.0f) || ((u + v) > 1.0f)) {
1712 return false;
1713 }
1714
1715 *r_lambda = f * dot_v3v3(e2, q);
1716 if (*r_lambda < 0.0f) {
1717 return false;
1718 }
1719
1720 if (r_uv) {
1721 r_uv[0] = u;
1722 r_uv[1] = v;
1723 }
1724
1725 return true;
1726}
1727
1728bool isect_ray_plane_v3_factor(const float ray_origin[3],
1729 const float ray_direction[3],
1730 const float plane_co[3],
1731 const float plane_no[3],
1732 float *r_lambda)
1733{
1734 float h[3];
1735 float dot = dot_v3v3(plane_no, ray_direction);
1736 if (dot == 0.0f) {
1737 return false;
1738 }
1739 sub_v3_v3v3(h, ray_origin, plane_co);
1740 *r_lambda = -dot_v3v3(plane_no, h) / dot;
1741 return true;
1742}
1743
1744bool isect_ray_plane_v3(const float ray_origin[3],
1745 const float ray_direction[3],
1746 const float plane[4],
1747 float *r_lambda,
1748 const bool clip)
1749{
1750 float plane_co[3], plane_no[3];
1751 plane_to_point_vector_v3(plane, plane_co, plane_no);
1752 if (!isect_ray_plane_v3_factor(ray_origin, ray_direction, plane_co, plane_no, r_lambda)) {
1753 return false;
1754 }
1755 if (clip && (*r_lambda < 0.0f)) {
1756 return false;
1757 }
1758 return true;
1759}
1760
1761bool isect_ray_tri_epsilon_v3(const float ray_origin[3],
1762 const float ray_direction[3],
1763 const float v0[3],
1764 const float v1[3],
1765 const float v2[3],
1766 float *r_lambda,
1767 float r_uv[2],
1768 const float epsilon)
1769{
1770 float p[3], s[3], e1[3], e2[3], q[3];
1771 float a, f, u, v;
1772
1773 sub_v3_v3v3(e1, v1, v0);
1774 sub_v3_v3v3(e2, v2, v0);
1775
1776 cross_v3_v3v3(p, ray_direction, e2);
1777 a = dot_v3v3(e1, p);
1778 if (a == 0.0f) {
1779 return false;
1780 }
1781 f = 1.0f / a;
1782
1783 sub_v3_v3v3(s, ray_origin, v0);
1784
1785 u = f * dot_v3v3(s, p);
1786 if ((u < -epsilon) || (u > 1.0f + epsilon)) {
1787 return false;
1788 }
1789
1790 cross_v3_v3v3(q, s, e1);
1791
1792 v = f * dot_v3v3(ray_direction, q);
1793 if ((v < -epsilon) || ((u + v) > 1.0f + epsilon)) {
1794 return false;
1795 }
1796
1797 *r_lambda = f * dot_v3v3(e2, q);
1798 if (*r_lambda < 0.0f) {
1799 return false;
1800 }
1801
1802 if (r_uv) {
1803 r_uv[0] = u;
1804 r_uv[1] = v;
1805 }
1806
1807 return true;
1808}
1809
1811 const float ray_direction[3])
1812{
1813 float inv_dir_z;
1814
1815 /* Calculate dimension where the ray direction is maximal. */
1816 int kz = axis_dominant_v3_single(ray_direction);
1817 int kx = (kz != 2) ? (kz + 1) : 0;
1818 int ky = (kx != 2) ? (kx + 1) : 0;
1819
1820 /* Swap kx and ky dimensions to preserve winding direction of triangles. */
1821 if (ray_direction[kz] < 0.0f) {
1822 std::swap(kx, ky);
1823 }
1824
1825 /* Calculate the shear constants. */
1826 inv_dir_z = 1.0f / ray_direction[kz];
1827 isect_precalc->sx = ray_direction[kx] * inv_dir_z;
1828 isect_precalc->sy = ray_direction[ky] * inv_dir_z;
1829 isect_precalc->sz = inv_dir_z;
1830
1831 /* Store the dimensions. */
1832 isect_precalc->kx = kx;
1833 isect_precalc->ky = ky;
1834 isect_precalc->kz = kz;
1835}
1836
1837bool isect_ray_tri_watertight_v3(const float ray_origin[3],
1838 const IsectRayPrecalc *isect_precalc,
1839 const float v0[3],
1840 const float v1[3],
1841 const float v2[3],
1842 float *r_lambda,
1843 float r_uv[2])
1844{
1845 const int kx = isect_precalc->kx;
1846 const int ky = isect_precalc->ky;
1847 const int kz = isect_precalc->kz;
1848 const float sx = isect_precalc->sx;
1849 const float sy = isect_precalc->sy;
1850 const float sz = isect_precalc->sz;
1851
1852 /* Calculate vertices relative to ray origin. */
1853 const float a[3] = {v0[0] - ray_origin[0], v0[1] - ray_origin[1], v0[2] - ray_origin[2]};
1854 const float b[3] = {v1[0] - ray_origin[0], v1[1] - ray_origin[1], v1[2] - ray_origin[2]};
1855 const float c[3] = {v2[0] - ray_origin[0], v2[1] - ray_origin[1], v2[2] - ray_origin[2]};
1856
1857 const float a_kx = a[kx], a_ky = a[ky], a_kz = a[kz];
1858 const float b_kx = b[kx], b_ky = b[ky], b_kz = b[kz];
1859 const float c_kx = c[kx], c_ky = c[ky], c_kz = c[kz];
1860
1861 /* Perform shear and scale of vertices. */
1862 const float ax = a_kx - sx * a_kz;
1863 const float ay = a_ky - sy * a_kz;
1864 const float bx = b_kx - sx * b_kz;
1865 const float by = b_ky - sy * b_kz;
1866 const float cx = c_kx - sx * c_kz;
1867 const float cy = c_ky - sy * c_kz;
1868
1869 /* Calculate scaled barycentric coordinates. */
1870 const float u = cx * by - cy * bx;
1871 const float v = ax * cy - ay * cx;
1872 const float w = bx * ay - by * ax;
1873 float det;
1874
1875 if ((u < 0.0f || v < 0.0f || w < 0.0f) && (u > 0.0f || v > 0.0f || w > 0.0f)) {
1876 return false;
1877 }
1878
1879 /* Calculate determinant. */
1880 det = u + v + w;
1881 if (UNLIKELY(det == 0.0f || !isfinite(det))) {
1882 return false;
1883 }
1884
1885 /* Calculate scaled z-coordinates of vertices and use them to calculate
1886 * the hit distance.
1887 */
1888 const int sign_det = (float_as_int(det) & int(0x80000000));
1889 const float t = (u * a_kz + v * b_kz + w * c_kz) * sz;
1890 const float sign_t = xor_fl(t, sign_det);
1891 if ((sign_t < 0.0f)
1892 /* Differ from Cycles, don't read r_lambda's original value
1893 * otherwise we won't match any of the other intersect functions here...
1894 * which would be confusing. */
1895#if 0
1896 || (sign_T > *r_lambda * xor_signmask(det, sign_mask))
1897#endif
1898 )
1899 {
1900 return false;
1901 }
1902
1903 /* Normalize u, v and t. */
1904 const float inv_det = 1.0f / det;
1905 if (r_uv) {
1906 r_uv[0] = u * inv_det;
1907 r_uv[1] = v * inv_det;
1908 }
1909 *r_lambda = t * inv_det;
1910 return true;
1911}
1912
1913bool isect_ray_tri_watertight_v3_simple(const float ray_origin[3],
1914 const float ray_direction[3],
1915 const float v0[3],
1916 const float v1[3],
1917 const float v2[3],
1918 float *r_lambda,
1919 float r_uv[2])
1920{
1921 IsectRayPrecalc isect_precalc;
1922 isect_ray_tri_watertight_v3_precalc(&isect_precalc, ray_direction);
1923 return isect_ray_tri_watertight_v3(ray_origin, &isect_precalc, v0, v1, v2, r_lambda, r_uv);
1924}
1925
1926#if 0 /* UNUSED */
1931bool isect_ray_tri_threshold_v3(const float ray_origin[3],
1932 const float ray_direction[3],
1933 const float v0[3],
1934 const float v1[3],
1935 const float v2[3],
1936 float *r_lambda,
1937 float r_uv[2],
1938 const float threshold)
1939{
1940 const float epsilon = 0.00000001f;
1941 float p[3], s[3], e1[3], e2[3], q[3];
1942 float a, f, u, v;
1943 float du, dv;
1944
1945 sub_v3_v3v3(e1, v1, v0);
1946 sub_v3_v3v3(e2, v2, v0);
1947
1948 cross_v3_v3v3(p, ray_direction, e2);
1949 a = dot_v3v3(e1, p);
1950 if ((a > -epsilon) && (a < epsilon)) {
1951 return false;
1952 }
1953 f = 1.0f / a;
1954
1955 sub_v3_v3v3(s, ray_origin, v0);
1956
1957 cross_v3_v3v3(q, s, e1);
1958 *r_lambda = f * dot_v3v3(e2, q);
1959 if (*r_lambda < 0.0f) {
1960 return false;
1961 }
1962
1963 u = f * dot_v3v3(s, p);
1964 v = f * dot_v3v3(ray_direction, q);
1965
1966 if (u > 0 && v > 0 && u + v > 1) {
1967 float t = (u + v - 1) / 2;
1968 du = u - t;
1969 dv = v - t;
1970 }
1971 else {
1972 if (u < 0) {
1973 du = u;
1974 }
1975 else if (u > 1) {
1976 du = u - 1;
1977 }
1978 else {
1979 du = 0.0f;
1980 }
1981
1982 if (v < 0) {
1983 dv = v;
1984 }
1985 else if (v > 1) {
1986 dv = v - 1;
1987 }
1988 else {
1989 dv = 0.0f;
1990 }
1991 }
1992
1993 mul_v3_fl(e1, du);
1994 mul_v3_fl(e2, dv);
1995
1996 if (len_squared_v3(e1) + len_squared_v3(e2) > threshold * threshold) {
1997 return false;
1998 }
1999
2000 if (r_uv) {
2001 r_uv[0] = u;
2002 r_uv[1] = v;
2003 }
2004
2005 return true;
2006}
2007#endif
2008
2009bool isect_ray_seg_v2(const float ray_origin[2],
2010 const float ray_direction[2],
2011 const float v0[2],
2012 const float v1[2],
2013 float *r_lambda,
2014 float *r_u)
2015{
2016 float v0_local[2], v1_local[2];
2017 sub_v2_v2v2(v0_local, v0, ray_origin);
2018 sub_v2_v2v2(v1_local, v1, ray_origin);
2019
2020 float s10[2];
2021 float det;
2022
2023 sub_v2_v2v2(s10, v1_local, v0_local);
2024
2025 det = cross_v2v2(ray_direction, s10);
2026 if (det != 0.0f) {
2027 const float v = cross_v2v2(v0_local, v1_local);
2028 const float p[2] = {(ray_direction[0] * v) / det, (ray_direction[1] * v) / det};
2029
2030 const float t = (dot_v2v2(p, ray_direction) / dot_v2v2(ray_direction, ray_direction));
2031 if ((t >= 0.0f) == 0) {
2032 return false;
2033 }
2034
2035 float h[2];
2036 sub_v2_v2v2(h, v1_local, p);
2037 const float u = (dot_v2v2(s10, h) / dot_v2v2(s10, s10));
2038 if ((u >= 0.0f && u <= 1.0f) == 0) {
2039 return false;
2040 }
2041
2042 if (r_lambda) {
2043 *r_lambda = t;
2044 }
2045 if (r_u) {
2046 *r_u = u;
2047 }
2048
2049 return true;
2050 }
2051
2052 return false;
2053}
2054
2055bool isect_ray_line_v3(const float ray_origin[3],
2056 const float ray_direction[3],
2057 const float v0[3],
2058 const float v1[3],
2059 float *r_lambda)
2060{
2061 float a[3], t[3], n[3];
2062 sub_v3_v3v3(a, v1, v0);
2063 sub_v3_v3v3(t, v0, ray_origin);
2064 cross_v3_v3v3(n, a, ray_direction);
2065 const float nlen = len_squared_v3(n);
2066
2067 if (nlen == 0.0f) {
2068 /* The lines are parallel. */
2069 return false;
2070 }
2071
2072 float c[3], cray[3];
2073 sub_v3_v3v3(c, n, t);
2074 cross_v3_v3v3(cray, c, ray_direction);
2075
2076 *r_lambda = dot_v3v3(cray, n) / nlen;
2077
2078 return true;
2079}
2080
2081bool isect_point_planes_v3(const float (*planes)[4], int totplane, const float p[3])
2082{
2083 int i;
2084
2085 for (i = 0; i < totplane; i++) {
2086 if (plane_point_side_v3(planes[i], p) > 0.0f) {
2087 return false;
2088 }
2089 }
2090
2091 return true;
2092}
2093
2094bool isect_point_planes_v3_negated(const float (*planes)[4], const int totplane, const float p[3])
2095{
2096 for (int i = 0; i < totplane; i++) {
2097 if (plane_point_side_v3(planes[i], p) <= 0.0f) {
2098 return false;
2099 }
2100 }
2101
2102 return true;
2103}
2104
2105bool isect_line_plane_v3(float r_isect_co[3],
2106 const float l1[3],
2107 const float l2[3],
2108 const float plane_co[3],
2109 const float plane_no[3])
2110{
2111 float u[3], h[3];
2112 float dot;
2113
2114 sub_v3_v3v3(u, l2, l1);
2115 sub_v3_v3v3(h, l1, plane_co);
2116 dot = dot_v3v3(plane_no, u);
2117
2118 if (fabsf(dot) > FLT_EPSILON) {
2119 float lambda = -dot_v3v3(plane_no, h) / dot;
2120 madd_v3_v3v3fl(r_isect_co, l1, u, lambda);
2121 return true;
2122 }
2123
2124 /* The segment is parallel to plane */
2125 return false;
2126}
2127
2128bool isect_plane_plane_plane_v3(const float plane_a[4],
2129 const float plane_b[4],
2130 const float plane_c[4],
2131 float r_isect_co[3])
2132{
2133 float det;
2134
2135 det = determinant_m3(UNPACK3(plane_a), UNPACK3(plane_b), UNPACK3(plane_c));
2136
2137 if (det != 0.0f) {
2138 float tmp[3];
2139
2140 /* (plane_b.xyz.cross(plane_c.xyz) * -plane_a[3] +
2141 * plane_c.xyz.cross(plane_a.xyz) * -plane_b[3] +
2142 * plane_a.xyz.cross(plane_b.xyz) * -plane_c[3]) / det; */
2143
2144 cross_v3_v3v3(tmp, plane_c, plane_b);
2145 mul_v3_v3fl(r_isect_co, tmp, plane_a[3]);
2146
2147 cross_v3_v3v3(tmp, plane_a, plane_c);
2148 madd_v3_v3fl(r_isect_co, tmp, plane_b[3]);
2149
2150 cross_v3_v3v3(tmp, plane_b, plane_a);
2151 madd_v3_v3fl(r_isect_co, tmp, plane_c[3]);
2152
2153 mul_v3_fl(r_isect_co, 1.0f / det);
2154
2155 return true;
2156 }
2157
2158 return false;
2159}
2160
2161bool isect_plane_plane_v3(const float plane_a[4],
2162 const float plane_b[4],
2163 float r_isect_co[3],
2164 float r_isect_no[3])
2165{
2166 float det, plane_c[3];
2167
2168 /* direction is simply the cross product */
2169 cross_v3_v3v3(plane_c, plane_a, plane_b);
2170
2171 /* in this case we don't need to use 'determinant_m3' */
2172 det = len_squared_v3(plane_c);
2173
2174 if (det != 0.0f) {
2175 float tmp[3];
2176
2177 /* (plane_b.xyz.cross(plane_c.xyz) * -plane_a[3] +
2178 * plane_c.xyz.cross(plane_a.xyz) * -plane_b[3]) / det; */
2179 cross_v3_v3v3(tmp, plane_c, plane_b);
2180 mul_v3_v3fl(r_isect_co, tmp, plane_a[3]);
2181
2182 cross_v3_v3v3(tmp, plane_a, plane_c);
2183 madd_v3_v3fl(r_isect_co, tmp, plane_b[3]);
2184
2185 mul_v3_fl(r_isect_co, 1.0f / det);
2186
2187 copy_v3_v3(r_isect_no, plane_c);
2188
2189 return true;
2190 }
2191
2192 return false;
2193}
2194
2196 const float planes[][4],
2197 const int planes_len,
2198 const float eps_coplanar,
2199 const float eps_isect,
2200 void (*callback_fn)(const float co[3], int i, int j, int k, void *user_data),
2201 void *user_data)
2202{
2203 bool found = false;
2204
2205 float n1n2[3], n2n3[3], n3n1[3];
2206
2207 for (int i = 0; i < planes_len; i++) {
2208 const float *n1 = planes[i];
2209 for (int j = i + 1; j < planes_len; j++) {
2210 const float *n2 = planes[j];
2211 cross_v3_v3v3(n1n2, n1, n2);
2212 if (len_squared_v3(n1n2) <= eps_coplanar) {
2213 continue;
2214 }
2215 for (int k = j + 1; k < planes_len; k++) {
2216 const float *n3 = planes[k];
2217 cross_v3_v3v3(n2n3, n2, n3);
2218 if (len_squared_v3(n2n3) <= eps_coplanar) {
2219 continue;
2220 }
2221
2222 cross_v3_v3v3(n3n1, n3, n1);
2223 if (len_squared_v3(n3n1) <= eps_coplanar) {
2224 continue;
2225 }
2226 const float quotient = -dot_v3v3(n1, n2n3);
2227 if (fabsf(quotient) < eps_coplanar) {
2228 continue;
2229 }
2230 const float co_test[3] = {
2231 ((n2n3[0] * n1[3]) + (n3n1[0] * n2[3]) + (n1n2[0] * n3[3])) / quotient,
2232 ((n2n3[1] * n1[3]) + (n3n1[1] * n2[3]) + (n1n2[1] * n3[3])) / quotient,
2233 ((n2n3[2] * n1[3]) + (n3n1[2] * n2[3]) + (n1n2[2] * n3[3])) / quotient,
2234 };
2235 int i_test;
2236 for (i_test = 0; i_test < planes_len; i_test++) {
2237 const float *np_test = planes[i_test];
2238 if ((dot_v3v3(np_test, co_test) + np_test[3]) > eps_isect) {
2239 /* For low epsilon values the point could intersect its own plane. */
2240 if (!ELEM(i_test, i, j, k)) {
2241 break;
2242 }
2243 }
2244 }
2245
2246 if (i_test == planes_len) { /* ok */
2247 callback_fn(co_test, i, j, k, user_data);
2248 found = true;
2249 }
2250 }
2251 }
2252 }
2253
2254 return found;
2255}
2256
2257bool isect_tri_tri_v3_ex(const float tri_a[3][3],
2258 const float tri_b[3][3],
2259 float r_i1[3],
2260 float r_i2[3],
2261 int *r_tri_a_edge_isect_count)
2262{
2263 struct {
2264 /* Factor that indicates the position of the intersection point on the line
2265 * that intersects the planes of the triangles. */
2266 float min, max;
2267 /* Intersection point location. */
2268 float loc[2][3];
2269 } range[2];
2270
2271 float side[2][3];
2272 double ba[3], bc[3], plane_a[4], plane_b[4];
2273 *r_tri_a_edge_isect_count = 0;
2274
2275 sub_v3db_v3fl_v3fl(ba, tri_a[0], tri_a[1]);
2276 sub_v3db_v3fl_v3fl(bc, tri_a[2], tri_a[1]);
2277 cross_v3_v3v3_db(plane_a, ba, bc);
2278 plane_a[3] = -dot_v3db_v3fl(plane_a, tri_a[1]);
2279 side[1][0] = float(dot_v3db_v3fl(plane_a, tri_b[0]) + plane_a[3]);
2280 side[1][1] = float(dot_v3db_v3fl(plane_a, tri_b[1]) + plane_a[3]);
2281 side[1][2] = float(dot_v3db_v3fl(plane_a, tri_b[2]) + plane_a[3]);
2282
2283 if (!side[1][0] && !side[1][1] && !side[1][2]) {
2284 /* Coplanar case is not supported. */
2285 return false;
2286 }
2287
2288 if ((side[1][0] && side[1][1] && side[1][2]) && (side[1][0] < 0.0f) == (side[1][1] < 0.0f) &&
2289 (side[1][0] < 0.0f) == (side[1][2] < 0.0f))
2290 {
2291 /* All vertices of the 2nd triangle are positioned on the same side to the
2292 * plane defined by the 1st triangle. */
2293 return false;
2294 }
2295
2296 sub_v3db_v3fl_v3fl(ba, tri_b[0], tri_b[1]);
2297 sub_v3db_v3fl_v3fl(bc, tri_b[2], tri_b[1]);
2298 cross_v3_v3v3_db(plane_b, ba, bc);
2299 plane_b[3] = -dot_v3db_v3fl(plane_b, tri_b[1]);
2300 side[0][0] = float(dot_v3db_v3fl(plane_b, tri_a[0]) + plane_b[3]);
2301 side[0][1] = float(dot_v3db_v3fl(plane_b, tri_a[1]) + plane_b[3]);
2302 side[0][2] = float(dot_v3db_v3fl(plane_b, tri_a[2]) + plane_b[3]);
2303
2304 if ((side[0][0] && side[0][1] && side[0][2]) && (side[0][0] < 0.0f) == (side[0][1] < 0.0f) &&
2305 (side[0][0] < 0.0f) == (side[0][2] < 0.0f))
2306 {
2307 /* All vertices of the 1st triangle are positioned on the same side to the
2308 * plane defined by the 2nd triangle. */
2309 return false;
2310 }
2311
2312 /* Direction of the line that intersects the planes of the triangles. */
2313 double isect_dir[3];
2314 cross_v3_v3v3_db(isect_dir, plane_a, plane_b);
2315 for (int i = 0; i < 2; i++) {
2316 const float (*tri)[3] = i == 0 ? tri_a : tri_b;
2317 /* Rearrange the triangle so that the vertex that is alone on one side
2318 * of the plane is located at index 1. */
2319 int tri_i[3];
2320 if ((side[i][0] && side[i][1]) && (side[i][0] < 0.0f) == (side[i][1] < 0.0f)) {
2321 tri_i[0] = 1;
2322 tri_i[1] = 2;
2323 tri_i[2] = 0;
2324 }
2325 else if ((side[i][1] && side[i][2]) && (side[i][1] < 0.0f) == (side[i][2] < 0.0f)) {
2326 tri_i[0] = 2;
2327 tri_i[1] = 0;
2328 tri_i[2] = 1;
2329 }
2330 else {
2331 tri_i[0] = 0;
2332 tri_i[1] = 1;
2333 tri_i[2] = 2;
2334 }
2335
2336 double dot_b = dot_v3db_v3fl(isect_dir, tri[tri_i[1]]);
2337 float sidec = side[i][tri_i[1]];
2338 if (sidec) {
2339 double dot_a = dot_v3db_v3fl(isect_dir, tri[tri_i[0]]);
2340 double dot_c = dot_v3db_v3fl(isect_dir, tri[tri_i[2]]);
2341 float fac0 = sidec / (sidec - side[i][tri_i[0]]);
2342 float fac1 = sidec / (sidec - side[i][tri_i[2]]);
2343 double offset0 = fac0 * (dot_a - dot_b);
2344 double offset1 = fac1 * (dot_c - dot_b);
2345 if (offset0 > offset1) {
2346 /* Sort min max. */
2347 std::swap(offset0, offset1);
2348 std::swap(fac0, fac1);
2349 std::swap(tri_i[0], tri_i[2]);
2350 }
2351
2352 range[i].min = float(dot_b + offset0);
2353 range[i].max = float(dot_b + offset1);
2354 interp_v3_v3v3(range[i].loc[0], tri[tri_i[1]], tri[tri_i[0]], fac0);
2355 interp_v3_v3v3(range[i].loc[1], tri[tri_i[1]], tri[tri_i[2]], fac1);
2356 }
2357 else {
2358 range[i].min = range[i].max = float(dot_b);
2359 copy_v3_v3(range[i].loc[0], tri[tri_i[1]]);
2360 copy_v3_v3(range[i].loc[1], tri[tri_i[1]]);
2361 }
2362 }
2363
2364 if ((range[0].max > range[1].min) && (range[0].min < range[1].max)) {
2365 /* The triangles intersect because they overlap on the intersection line.
2366 * Now identify the two points of intersection that are in the middle to get the actual
2367 * intersection between the triangles. (B--C from A--B--C--D) */
2368 if (range[0].min >= range[1].min) {
2369 copy_v3_v3(r_i1, range[0].loc[0]);
2370 if (range[0].max <= range[1].max) {
2371 copy_v3_v3(r_i2, range[0].loc[1]);
2372 *r_tri_a_edge_isect_count = 2;
2373 }
2374 else {
2375 copy_v3_v3(r_i2, range[1].loc[1]);
2376 *r_tri_a_edge_isect_count = 1;
2377 }
2378 }
2379 else {
2380 if (range[0].max <= range[1].max) {
2381 copy_v3_v3(r_i1, range[0].loc[1]);
2382 copy_v3_v3(r_i2, range[1].loc[0]);
2383 *r_tri_a_edge_isect_count = 1;
2384 }
2385 else {
2386 copy_v3_v3(r_i1, range[1].loc[0]);
2387 copy_v3_v3(r_i2, range[1].loc[1]);
2388 }
2389 }
2390 return true;
2391 }
2392
2393 return false;
2394}
2395
2396bool isect_tri_tri_v3(const float t_a0[3],
2397 const float t_a1[3],
2398 const float t_a2[3],
2399 const float t_b0[3],
2400 const float t_b1[3],
2401 const float t_b2[3],
2402 float r_i1[3],
2403 float r_i2[3])
2404{
2405 float tri_a[3][3], tri_b[3][3];
2406 int dummy;
2407 copy_v3_v3(tri_a[0], t_a0);
2408 copy_v3_v3(tri_a[1], t_a1);
2409 copy_v3_v3(tri_a[2], t_a2);
2410 copy_v3_v3(tri_b[0], t_b0);
2411 copy_v3_v3(tri_b[1], t_b1);
2412 copy_v3_v3(tri_b[2], t_b2);
2413 return isect_tri_tri_v3_ex(tri_a, tri_b, r_i1, r_i2, &dummy);
2414}
2415
2416/* -------------------------------------------------------------------- */
2424
2425static bool isect_tri_tri_v2_impl_vert(const float t_a0[2],
2426 const float t_a1[2],
2427 const float t_a2[2],
2428 const float t_b0[2],
2429 const float t_b1[2],
2430 const float t_b2[2])
2431{
2432 if (line_point_side_v2(t_b2, t_b0, t_a1) >= 0.0f) {
2433 if (line_point_side_v2(t_b2, t_b1, t_a1) <= 0.0f) {
2434 if (line_point_side_v2(t_a0, t_b0, t_a1) > 0.0f) {
2435 if (line_point_side_v2(t_a0, t_b1, t_a1) <= 0.0f) {
2436 return true;
2437 }
2438
2439 return false;
2440 }
2441
2442 if (line_point_side_v2(t_a0, t_b0, t_a2) >= 0.0f) {
2443 if (line_point_side_v2(t_a1, t_a2, t_b0) >= 0.0f) {
2444 return true;
2445 }
2446
2447 return false;
2448 }
2449
2450 return false;
2451 }
2452 if (line_point_side_v2(t_a0, t_b1, t_a1) <= 0.0f) {
2453 if (line_point_side_v2(t_b2, t_b1, t_a2) <= 0.0f) {
2454 if (line_point_side_v2(t_a1, t_a2, t_b1) >= 0.0f) {
2455 return true;
2456 }
2457
2458 return false;
2459 }
2460
2461 return false;
2462 }
2463
2464 return false;
2465 }
2466 if (line_point_side_v2(t_b2, t_b0, t_a2) >= 0.0f) {
2467 if (line_point_side_v2(t_a1, t_a2, t_b2) >= 0.0f) {
2468 if (line_point_side_v2(t_a0, t_b0, t_a2) >= 0.0f) {
2469 return true;
2470 }
2471
2472 return false;
2473 }
2474 if (line_point_side_v2(t_a1, t_a2, t_b1) >= 0.0f) {
2475 if (line_point_side_v2(t_b2, t_a2, t_b1) >= 0.0f) {
2476 return true;
2477 }
2478
2479 return false;
2480 }
2481
2482 return false;
2483 }
2484
2485 return false;
2486}
2487
2488static bool isect_tri_tri_v2_impl_edge(const float t_a0[2],
2489 const float t_a1[2],
2490 const float t_a2[2],
2491 const float t_b0[2],
2492 const float t_b1[2],
2493 const float t_b2[2])
2494{
2495 UNUSED_VARS(t_b1);
2496
2497 if (line_point_side_v2(t_b2, t_b0, t_a1) >= 0.0f) {
2498 if (line_point_side_v2(t_a0, t_b0, t_a1) >= 0.0f) {
2499 if (line_point_side_v2(t_a0, t_a1, t_b2) >= 0.0f) {
2500 return true;
2501 }
2502
2503 return false;
2504 }
2505
2506 if (line_point_side_v2(t_a1, t_a2, t_b0) >= 0.0f) {
2507 if (line_point_side_v2(t_a2, t_a0, t_b0) >= 0.0f) {
2508 return true;
2509 }
2510
2511 return false;
2512 }
2513
2514 return false;
2515 }
2516
2517 if (line_point_side_v2(t_b2, t_b0, t_a2) >= 0.0f) {
2518 if (line_point_side_v2(t_a0, t_b0, t_a2) >= 0.0f) {
2519 if (line_point_side_v2(t_a0, t_a2, t_b2) >= 0.0f) {
2520 return true;
2521 }
2522
2523 if (line_point_side_v2(t_a1, t_a2, t_b2) >= 0.0f) {
2524 return true;
2525 }
2526
2527 return false;
2528 }
2529
2530 return false;
2531 }
2532
2533 return false;
2534}
2535
2536static int isect_tri_tri_impl_ccw_v2(const float t_a0[2],
2537 const float t_a1[2],
2538 const float t_a2[2],
2539 const float t_b0[2],
2540 const float t_b1[2],
2541 const float t_b2[2])
2542{
2543 if (line_point_side_v2(t_b0, t_b1, t_a0) >= 0.0f) {
2544 if (line_point_side_v2(t_b1, t_b2, t_a0) >= 0.0f) {
2545 if (line_point_side_v2(t_b2, t_b0, t_a0) >= 0.0f) {
2546 return 1;
2547 }
2548
2549 return isect_tri_tri_v2_impl_edge(t_a0, t_a1, t_a2, t_b0, t_b1, t_b2);
2550 }
2551
2552 if (line_point_side_v2(t_b2, t_b0, t_a0) >= 0.0f) {
2553 return isect_tri_tri_v2_impl_edge(t_a0, t_a1, t_a2, t_b2, t_b0, t_b1);
2554 }
2555
2556 return isect_tri_tri_v2_impl_vert(t_a0, t_a1, t_a2, t_b0, t_b1, t_b2);
2557 }
2558
2559 if (line_point_side_v2(t_b1, t_b2, t_a0) >= 0.0f) {
2560 if (line_point_side_v2(t_b2, t_b0, t_a0) >= 0.0f) {
2561 return isect_tri_tri_v2_impl_edge(t_a0, t_a1, t_a2, t_b1, t_b2, t_b0);
2562 }
2563
2564 return isect_tri_tri_v2_impl_vert(t_a0, t_a1, t_a2, t_b1, t_b2, t_b0);
2565 }
2566
2567 return isect_tri_tri_v2_impl_vert(t_a0, t_a1, t_a2, t_b2, t_b0, t_b1);
2568}
2569
2570bool isect_tri_tri_v2(const float t_a0[2],
2571 const float t_a1[2],
2572 const float t_a2[2],
2573 const float t_b0[2],
2574 const float t_b1[2],
2575 const float t_b2[2])
2576{
2577 if (line_point_side_v2(t_a0, t_a1, t_a2) < 0.0f) {
2578 if (line_point_side_v2(t_b0, t_b1, t_b2) < 0.0f) {
2579 return isect_tri_tri_impl_ccw_v2(t_a0, t_a2, t_a1, t_b0, t_b2, t_b1);
2580 }
2581
2582 return isect_tri_tri_impl_ccw_v2(t_a0, t_a2, t_a1, t_b0, t_b1, t_b2);
2583 }
2584
2585 if (line_point_side_v2(t_b0, t_b1, t_b2) < 0.0f) {
2586 return isect_tri_tri_impl_ccw_v2(t_a0, t_a1, t_a2, t_b0, t_b2, t_b1);
2587 }
2588
2589 return isect_tri_tri_impl_ccw_v2(t_a0, t_a1, t_a2, t_b0, t_b1, t_b2);
2590}
2591
2593
2594/* Adapted from the paper by Kasper Fauerby */
2595
2596/* "Improved Collision detection and Response" */
2597static bool getLowestRoot(
2598 const float a, const float b, const float c, const float maxR, float *root)
2599{
2600 /* Check if a solution exists */
2601 const float determinant = b * b - 4.0f * a * c;
2602
2603 /* If determinant is negative it means no solutions. */
2604 if (determinant >= 0.0f) {
2605 /* calculate the two roots: (if determinant == 0 then
2606 * x1==x2 but lets disregard that slight optimization) */
2607 const float sqrtD = sqrtf(determinant);
2608 float r1 = (-b - sqrtD) / (2.0f * a);
2609 float r2 = (-b + sqrtD) / (2.0f * a);
2610
2611 /* Sort so x1 <= x2 */
2612 if (r1 > r2) {
2613 std::swap(r1, r2);
2614 }
2615
2616 /* Get lowest root: */
2617 if (r1 > 0.0f && r1 < maxR) {
2618 *root = r1;
2619 return true;
2620 }
2621
2622 /* It is possible that we want x2 - this can happen */
2623 /* if x1 < 0 */
2624 if (r2 > 0.0f && r2 < maxR) {
2625 *root = r2;
2626 return true;
2627 }
2628 }
2629 /* No (valid) solutions */
2630 return false;
2631}
2632
2633int isect_aabb_planes_v3(const float (*planes)[4],
2634 const int totplane,
2635 const float bbmin[3],
2636 const float bbmax[3])
2637{
2639
2640 float bb_near[3], bb_far[3];
2641 for (int i = 0; i < totplane; i++) {
2642 aabb_get_near_far_from_plane(planes[i], bbmin, bbmax, bb_near, bb_far);
2643
2644 if (plane_point_side_v3(planes[i], bb_far) < 0.0f) {
2646 }
2647 if ((ret != ISECT_AABB_PLANE_CROSS_ANY) && (plane_point_side_v3(planes[i], bb_near) < 0.0f)) {
2649 }
2650 }
2651
2652 return ret;
2653}
2654
2655bool isect_sweeping_sphere_tri_v3(const float p1[3],
2656 const float p2[3],
2657 const float radius,
2658 const float v0[3],
2659 const float v1[3],
2660 const float v2[3],
2661 float *r_lambda,
2662 float ipoint[3])
2663{
2664 float e1[3], e2[3], e3[3], point[3], vel[3], /*dist[3],*/ nor[3], temp[3], bv[3];
2665 float a, b, c, d, e, x, y, z, radius2 = radius * radius;
2666 float elen2, edotv, edotbv, nordotv;
2667 float newLambda;
2668 bool found_by_sweep = false;
2669
2670 sub_v3_v3v3(e1, v1, v0);
2671 sub_v3_v3v3(e2, v2, v0);
2672 sub_v3_v3v3(vel, p2, p1);
2673
2674 /*---test plane of tri---*/
2675 cross_v3_v3v3(nor, e1, e2);
2677
2678 /* flip normal */
2679 if (dot_v3v3(nor, vel) > 0.0f) {
2680 negate_v3(nor);
2681 }
2682
2683 a = dot_v3v3(p1, nor) - dot_v3v3(v0, nor);
2684 nordotv = dot_v3v3(nor, vel);
2685
2686 if (fabsf(nordotv) < 0.000001f) {
2687 if (fabsf(a) >= radius) {
2688 return false;
2689 }
2690 }
2691 else {
2692 float t0 = (-a + radius) / nordotv;
2693 float t1 = (-a - radius) / nordotv;
2694
2695 if (t0 > t1) {
2696 std::swap(t0, t1);
2697 }
2698
2699 if (t0 > 1.0f || t1 < 0.0f) {
2700 return false;
2701 }
2702
2703 /* clamp to [0, 1] */
2704 CLAMP(t0, 0.0f, 1.0f);
2705 // CLAMP(t1, 0.0f, 1.0f); /* UNUSED. */
2706
2707 /*---test inside of tri---*/
2708 /* plane intersection point */
2709
2710 point[0] = p1[0] + vel[0] * t0 - nor[0] * radius;
2711 point[1] = p1[1] + vel[1] * t0 - nor[1] * radius;
2712 point[2] = p1[2] + vel[2] * t0 - nor[2] * radius;
2713
2714 /* is the point in the tri? */
2715 a = dot_v3v3(e1, e1);
2716 b = dot_v3v3(e1, e2);
2717 c = dot_v3v3(e2, e2);
2718
2719 sub_v3_v3v3(temp, point, v0);
2720 d = dot_v3v3(temp, e1);
2721 e = dot_v3v3(temp, e2);
2722
2723 x = d * c - e * b;
2724 y = e * a - d * b;
2725 z = x + y - (a * c - b * b);
2726
2727 if (z <= 0.0f && (x >= 0.0f && y >= 0.0f)) {
2728 // ((uint32_t(z) & ~(uint32_t(x) | uint32_t(y))) & 0x80000000)
2729 *r_lambda = t0;
2730 copy_v3_v3(ipoint, point);
2731 return true;
2732 }
2733 }
2734
2735 *r_lambda = 1.0f;
2736
2737 /*---test points---*/
2738 a = dot_v3v3(vel, vel);
2739
2740 /*v0*/
2741 sub_v3_v3v3(temp, p1, v0);
2742 b = 2.0f * dot_v3v3(vel, temp);
2743 c = dot_v3v3(temp, temp) - radius2;
2744
2745 if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) {
2746 copy_v3_v3(ipoint, v0);
2747 found_by_sweep = true;
2748 }
2749
2750 /*v1*/
2751 sub_v3_v3v3(temp, p1, v1);
2752 b = 2.0f * dot_v3v3(vel, temp);
2753 c = dot_v3v3(temp, temp) - radius2;
2754
2755 if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) {
2756 copy_v3_v3(ipoint, v1);
2757 found_by_sweep = true;
2758 }
2759
2760 /*v2*/
2761 sub_v3_v3v3(temp, p1, v2);
2762 b = 2.0f * dot_v3v3(vel, temp);
2763 c = dot_v3v3(temp, temp) - radius2;
2764
2765 if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) {
2766 copy_v3_v3(ipoint, v2);
2767 found_by_sweep = true;
2768 }
2769
2770 /*---test edges---*/
2771 sub_v3_v3v3(e3, v2, v1); /* wasn't yet calculated */
2772
2773 /* `e1` */
2774 sub_v3_v3v3(bv, v0, p1);
2775
2776 elen2 = dot_v3v3(e1, e1);
2777 edotv = dot_v3v3(e1, vel);
2778 edotbv = dot_v3v3(e1, bv);
2779
2780 a = elen2 * -dot_v3v3(vel, vel) + edotv * edotv;
2781 b = 2.0f * (elen2 * dot_v3v3(vel, bv) - edotv * edotbv);
2782 c = elen2 * (radius2 - dot_v3v3(bv, bv)) + edotbv * edotbv;
2783
2784 if (getLowestRoot(a, b, c, *r_lambda, &newLambda)) {
2785 e = (edotv * newLambda - edotbv) / elen2;
2786
2787 if (e >= 0.0f && e <= 1.0f) {
2788 *r_lambda = newLambda;
2789 copy_v3_v3(ipoint, e1);
2790 mul_v3_fl(ipoint, e);
2791 add_v3_v3(ipoint, v0);
2792 found_by_sweep = true;
2793 }
2794 }
2795
2796 /* `e2` */
2797 /* `bv` is same. */
2798 elen2 = dot_v3v3(e2, e2);
2799 edotv = dot_v3v3(e2, vel);
2800 edotbv = dot_v3v3(e2, bv);
2801
2802 a = elen2 * -dot_v3v3(vel, vel) + edotv * edotv;
2803 b = 2.0f * (elen2 * dot_v3v3(vel, bv) - edotv * edotbv);
2804 c = elen2 * (radius2 - dot_v3v3(bv, bv)) + edotbv * edotbv;
2805
2806 if (getLowestRoot(a, b, c, *r_lambda, &newLambda)) {
2807 e = (edotv * newLambda - edotbv) / elen2;
2808
2809 if (e >= 0.0f && e <= 1.0f) {
2810 *r_lambda = newLambda;
2811 copy_v3_v3(ipoint, e2);
2812 mul_v3_fl(ipoint, e);
2813 add_v3_v3(ipoint, v0);
2814 found_by_sweep = true;
2815 }
2816 }
2817
2818 /* `e3` */
2819 // sub_v3_v3v3(bv, v0, p1); /* UNUSED */
2820 // elen2 = dot_v3v3(e1, e1); /* UNUSED */
2821 // edotv = dot_v3v3(e1, vel); /* UNUSED */
2822 // edotbv = dot_v3v3(e1, bv); /* UNUSED */
2823
2824 sub_v3_v3v3(bv, v1, p1);
2825 elen2 = dot_v3v3(e3, e3);
2826 edotv = dot_v3v3(e3, vel);
2827 edotbv = dot_v3v3(e3, bv);
2828
2829 a = elen2 * -dot_v3v3(vel, vel) + edotv * edotv;
2830 b = 2.0f * (elen2 * dot_v3v3(vel, bv) - edotv * edotbv);
2831 c = elen2 * (radius2 - dot_v3v3(bv, bv)) + edotbv * edotbv;
2832
2833 if (getLowestRoot(a, b, c, *r_lambda, &newLambda)) {
2834 e = (edotv * newLambda - edotbv) / elen2;
2835
2836 if (e >= 0.0f && e <= 1.0f) {
2837 *r_lambda = newLambda;
2838 copy_v3_v3(ipoint, e3);
2839 mul_v3_fl(ipoint, e);
2840 add_v3_v3(ipoint, v1);
2841 found_by_sweep = true;
2842 }
2843 }
2844
2845 return found_by_sweep;
2846}
2847
2849 const float p1[3],
2850 const float p2[3],
2851 const float v0[3],
2852 const float v1[3],
2853 const float v2[3],
2854 float *r_lambda)
2855{
2856 const float epsilon = 0.000001f;
2857 float p[3], e1[3], e2[3];
2858 float u, v, f;
2859 int a0 = axis, a1 = (axis + 1) % 3, a2 = (axis + 2) % 3;
2860
2861 sub_v3_v3v3(e1, v1, v0);
2862 sub_v3_v3v3(e2, v2, v0);
2863 sub_v3_v3v3(p, v0, p1);
2864
2865 f = (e2[a1] * e1[a2] - e2[a2] * e1[a1]);
2866 if ((f > -epsilon) && (f < epsilon)) {
2867 return false;
2868 }
2869
2870 v = (p[a2] * e1[a1] - p[a1] * e1[a2]) / f;
2871 if ((v < 0.0f) || (v > 1.0f)) {
2872 return false;
2873 }
2874
2875 f = e1[a1];
2876 if ((f > -epsilon) && (f < epsilon)) {
2877 f = e1[a2];
2878 if ((f > -epsilon) && (f < epsilon)) {
2879 return false;
2880 }
2881 u = (-p[a2] - v * e2[a2]) / f;
2882 }
2883 else {
2884 u = (-p[a1] - v * e2[a1]) / f;
2885 }
2886
2887 if ((u < 0.0f) || ((u + v) > 1.0f)) {
2888 return false;
2889 }
2890
2891 *r_lambda = (p[a0] + u * e1[a0] + v * e2[a0]) / (p2[a0] - p1[a0]);
2892
2893 if ((*r_lambda < 0.0f) || (*r_lambda > 1.0f)) {
2894 return false;
2895 }
2896
2897 return true;
2898}
2899
2900int isect_line_line_epsilon_v3(const float v1[3],
2901 const float v2[3],
2902 const float v3[3],
2903 const float v4[3],
2904 float r_i1[3],
2905 float r_i2[3],
2906 const float epsilon)
2907{
2908 float a[3], b[3], c[3], ab[3], cb[3];
2909 float d, div;
2910
2911 sub_v3_v3v3(c, v3, v1);
2912 sub_v3_v3v3(a, v2, v1);
2913 sub_v3_v3v3(b, v4, v3);
2914
2915 cross_v3_v3v3(ab, a, b);
2916 d = dot_v3v3(c, ab);
2917 div = dot_v3v3(ab, ab);
2918
2919 /* important not to use an epsilon here, see: #45919 */
2920 /* test zero length line */
2921 if (UNLIKELY(div == 0.0f)) {
2922 return 0;
2923 }
2924 /* test if the two lines are coplanar */
2925 if (UNLIKELY(fabsf(d) <= epsilon)) {
2926 cross_v3_v3v3(cb, c, b);
2927
2928 mul_v3_fl(a, dot_v3v3(cb, ab) / div);
2929 add_v3_v3v3(r_i1, v1, a);
2930 copy_v3_v3(r_i2, r_i1);
2931
2932 return 1; /* one intersection only */
2933 }
2934 /* if not */
2935
2936 float n[3], t[3];
2937 float v3t[3], v4t[3];
2938 sub_v3_v3v3(t, v1, v3);
2939
2940 /* offset between both plane where the lines lies */
2941 cross_v3_v3v3(n, a, b);
2942 project_v3_v3v3(t, t, n);
2943
2944 /* for the first line, offset the second line until it is coplanar */
2945 add_v3_v3v3(v3t, v3, t);
2946 add_v3_v3v3(v4t, v4, t);
2947
2948 sub_v3_v3v3(c, v3t, v1);
2949 sub_v3_v3v3(a, v2, v1);
2950 sub_v3_v3v3(b, v4t, v3t);
2951
2952 cross_v3_v3v3(ab, a, b);
2953 cross_v3_v3v3(cb, c, b);
2954
2955 mul_v3_fl(a, dot_v3v3(cb, ab) / dot_v3v3(ab, ab));
2956 add_v3_v3v3(r_i1, v1, a);
2957
2958 /* for the second line, just subtract the offset from the first intersection point */
2959 sub_v3_v3v3(r_i2, r_i1, t);
2960
2961 return 2; /* two nearest points */
2962}
2963
2964int isect_line_line_v3(const float v1[3],
2965 const float v2[3],
2966 const float v3[3],
2967 const float v4[3],
2968 float r_i1[3],
2969 float r_i2[3])
2970{
2971 const float epsilon = 0.000001f;
2972 return isect_line_line_epsilon_v3(v1, v2, v3, v4, r_i1, r_i2, epsilon);
2973}
2974
2975bool isect_line_line_strict_v3(const float v1[3],
2976 const float v2[3],
2977 const float v3[3],
2978 const float v4[3],
2979 float vi[3],
2980 float *r_lambda)
2981{
2982 const float epsilon = 0.000001f;
2983 float a[3], b[3], c[3], ab[3], cb[3], ca[3];
2984 float d, div;
2985
2986 sub_v3_v3v3(c, v3, v1);
2987 sub_v3_v3v3(a, v2, v1);
2988 sub_v3_v3v3(b, v4, v3);
2989
2990 cross_v3_v3v3(ab, a, b);
2991 d = dot_v3v3(c, ab);
2992 div = dot_v3v3(ab, ab);
2993
2994 /* important not to use an epsilon here, see: #45919 */
2995 /* test zero length line */
2996 if (UNLIKELY(div == 0.0f)) {
2997 return false;
2998 }
2999 /* test if the two lines are coplanar */
3000 if (UNLIKELY(fabsf(d) < epsilon)) {
3001 return false;
3002 }
3003
3004 float f1, f2;
3005 cross_v3_v3v3(cb, c, b);
3006 cross_v3_v3v3(ca, c, a);
3007
3008 f1 = dot_v3v3(cb, ab) / div;
3009 f2 = dot_v3v3(ca, ab) / div;
3010
3011 if (f1 >= 0 && f1 <= 1 && f2 >= 0 && f2 <= 1) {
3012 mul_v3_fl(a, f1);
3013 add_v3_v3v3(vi, v1, a);
3014
3015 if (r_lambda) {
3016 *r_lambda = f1;
3017 }
3018
3019 return true; /* intersection found */
3020 }
3021
3022 return false;
3023}
3024
3025bool isect_ray_ray_epsilon_v3(const float ray_origin_a[3],
3026 const float ray_direction_a[3],
3027 const float ray_origin_b[3],
3028 const float ray_direction_b[3],
3029 const float epsilon,
3030 float *r_lambda_a,
3031 float *r_lambda_b)
3032{
3033 BLI_assert(r_lambda_a || r_lambda_b);
3034 float n[3];
3035 cross_v3_v3v3(n, ray_direction_b, ray_direction_a);
3036 const float nlen = len_squared_v3(n);
3037
3038 /* `nlen` is the square of the area formed by the two vectors. */
3039 if (UNLIKELY(nlen < epsilon)) {
3040 /* The lines are parallel. */
3041 return false;
3042 }
3043
3044 float t[3], c[3], cray[3];
3045 sub_v3_v3v3(t, ray_origin_b, ray_origin_a);
3046 sub_v3_v3v3(c, n, t);
3047
3048 if (r_lambda_a != nullptr) {
3049 cross_v3_v3v3(cray, c, ray_direction_b);
3050 *r_lambda_a = dot_v3v3(cray, n) / nlen;
3051 }
3052
3053 if (r_lambda_b != nullptr) {
3054 cross_v3_v3v3(cray, c, ray_direction_a);
3055 *r_lambda_b = dot_v3v3(cray, n) / nlen;
3056 }
3057
3058 return true;
3059}
3060
3061bool isect_ray_ray_v3(const float ray_origin_a[3],
3062 const float ray_direction_a[3],
3063 const float ray_origin_b[3],
3064 const float ray_direction_b[3],
3065 float *r_lambda_a,
3066 float *r_lambda_b)
3067{
3068 return isect_ray_ray_epsilon_v3(ray_origin_a,
3069 ray_direction_a,
3070 ray_origin_b,
3071 ray_direction_b,
3072 FLT_MIN,
3073 r_lambda_a,
3074 r_lambda_b);
3075}
3076
3077bool isect_aabb_aabb_v3(const float min1[3],
3078 const float max1[3],
3079 const float min2[3],
3080 const float max2[3])
3081{
3082 return (min1[0] < max2[0] && min1[1] < max2[1] && min1[2] < max2[2] && min2[0] < max1[0] &&
3083 min2[1] < max1[1] && min2[2] < max1[2]);
3084}
3085
3087 const float ray_origin[3],
3088 const float ray_direction[3])
3089{
3090 copy_v3_v3(data->ray_origin, ray_origin);
3091
3092 data->ray_inv_dir[0] = 1.0f / ray_direction[0];
3093 data->ray_inv_dir[1] = 1.0f / ray_direction[1];
3094 data->ray_inv_dir[2] = 1.0f / ray_direction[2];
3095
3096 data->sign[0] = data->ray_inv_dir[0] < 0.0f;
3097 data->sign[1] = data->ray_inv_dir[1] < 0.0f;
3098 data->sign[2] = data->ray_inv_dir[2] < 0.0f;
3099}
3100
3102 const float bb_min[3],
3103 const float bb_max[3],
3104 float *r_tmin)
3105{
3106 /* Adapted from http://www.gamedev.net/community/forums/topic.asp?topic_id=459973 */
3107
3108 float bbox[2][3];
3109
3110 copy_v3_v3(bbox[0], bb_min);
3111 copy_v3_v3(bbox[1], bb_max);
3112
3113 float tmin = (bbox[data->sign[0]][0] - data->ray_origin[0]) * data->ray_inv_dir[0];
3114 float tmax = (bbox[1 - data->sign[0]][0] - data->ray_origin[0]) * data->ray_inv_dir[0];
3115
3116 const float tymin = (bbox[data->sign[1]][1] - data->ray_origin[1]) * data->ray_inv_dir[1];
3117 const float tymax = (bbox[1 - data->sign[1]][1] - data->ray_origin[1]) * data->ray_inv_dir[1];
3118
3119 if ((tmin > tymax) || (tymin > tmax)) {
3120 return false;
3121 }
3122
3123 tmin = std::max(tymin, tmin);
3124 tmax = std::min(tymax, tmax);
3125
3126 const float tzmin = (bbox[data->sign[2]][2] - data->ray_origin[2]) * data->ray_inv_dir[2];
3127 const float tzmax = (bbox[1 - data->sign[2]][2] - data->ray_origin[2]) * data->ray_inv_dir[2];
3128
3129 if ((tmin > tzmax) || (tzmin > tmax)) {
3130 return false;
3131 }
3132
3133 tmin = std::max(tzmin, tmin);
3134
3135 /* NOTE(jwilkins): tmax does not need to be updated since we don't use it
3136 * keeping this here for future reference. */
3137 // if (tzmax < tmax) tmax = tzmax;
3138
3139 if (r_tmin) {
3140 (*r_tmin) = tmin;
3141 }
3142
3143 return true;
3144}
3145
3146bool isect_ray_aabb_v3_simple(const float orig[3],
3147 const float dir[3],
3148 const float bb_min[3],
3149 const float bb_max[3],
3150 float *tmin,
3151 float *tmax)
3152{
3153 double t[6];
3154 float hit_dist[2];
3155 const double invdirx = (dir[0] > 1e-35f || dir[0] < -1e-35f) ? 1.0 / double(dir[0]) : DBL_MAX;
3156 const double invdiry = (dir[1] > 1e-35f || dir[1] < -1e-35f) ? 1.0 / double(dir[1]) : DBL_MAX;
3157 const double invdirz = (dir[2] > 1e-35f || dir[2] < -1e-35f) ? 1.0 / double(dir[2]) : DBL_MAX;
3158 t[0] = double(bb_min[0] - orig[0]) * invdirx;
3159 t[1] = double(bb_max[0] - orig[0]) * invdirx;
3160 t[2] = double(bb_min[1] - orig[1]) * invdiry;
3161 t[3] = double(bb_max[1] - orig[1]) * invdiry;
3162 t[4] = double(bb_min[2] - orig[2]) * invdirz;
3163 t[5] = double(bb_max[2] - orig[2]) * invdirz;
3164 hit_dist[0] = float(fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])));
3165 hit_dist[1] = float(fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])));
3166 if ((hit_dist[1] < 0.0f) || (hit_dist[0] > hit_dist[1])) {
3167 return false;
3168 }
3169
3170 if (tmin) {
3171 *tmin = hit_dist[0];
3172 }
3173 if (tmax) {
3174 *tmax = hit_dist[1];
3175 }
3176 return true;
3177}
3178
3179float closest_to_ray_v3(float r_close[3],
3180 const float p[3],
3181 const float ray_orig[3],
3182 const float ray_dir[3])
3183{
3184 float h[3], lambda;
3185
3186 if (UNLIKELY(is_zero_v3(ray_dir))) {
3187 lambda = 0.0f;
3188 copy_v3_v3(r_close, ray_orig);
3189 return lambda;
3190 }
3191
3192 sub_v3_v3v3(h, p, ray_orig);
3193 lambda = dot_v3v3(ray_dir, h) / dot_v3v3(ray_dir, ray_dir);
3194 madd_v3_v3v3fl(r_close, ray_orig, ray_dir, lambda);
3195 return lambda;
3196}
3197
3198float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
3199{
3200 float u[3];
3201 sub_v3_v3v3(u, l2, l1);
3202 return closest_to_ray_v3(r_close, p, l1, u);
3203}
3204
3205float closest_to_line_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
3206{
3207 float h[2], u[2], lambda, denom;
3208 sub_v2_v2v2(u, l2, l1);
3209 sub_v2_v2v2(h, p, l1);
3210 denom = dot_v2v2(u, u);
3211 if (denom == 0.0f) {
3212 r_close[0] = l1[0];
3213 r_close[1] = l1[1];
3214 return 0.0f;
3215 }
3216 lambda = dot_v2v2(u, h) / denom;
3217 r_close[0] = l1[0] + u[0] * lambda;
3218 r_close[1] = l1[1] + u[1] * lambda;
3219 return lambda;
3220}
3221
3222double closest_to_line_v2_db(double r_close[2],
3223 const double p[2],
3224 const double l1[2],
3225 const double l2[2])
3226{
3227 double h[2], u[2], lambda, denom;
3228 sub_v2_v2v2_db(u, l2, l1);
3229 sub_v2_v2v2_db(h, p, l1);
3230 denom = dot_v2v2_db(u, u);
3231 if (denom == 0.0) {
3232 r_close[0] = l1[0];
3233 r_close[1] = l1[1];
3234 return 0.0;
3235 }
3236 lambda = dot_v2v2_db(u, h) / denom;
3237 r_close[0] = l1[0] + u[0] * lambda;
3238 r_close[1] = l1[1] + u[1] * lambda;
3239 return lambda;
3240}
3241
3242float ray_point_factor_v3_ex(const float p[3],
3243 const float ray_origin[3],
3244 const float ray_direction[3],
3245 const float epsilon,
3246 const float fallback)
3247{
3248 float p_relative[3];
3249 sub_v3_v3v3(p_relative, p, ray_origin);
3250 const float dot = len_squared_v3(ray_direction);
3251 return (dot > epsilon) ? (dot_v3v3(ray_direction, p_relative) / dot) : fallback;
3252}
3253
3254float ray_point_factor_v3(const float p[3],
3255 const float ray_origin[3],
3256 const float ray_direction[3])
3257{
3258 return ray_point_factor_v3_ex(p, ray_origin, ray_direction, 0.0f, 0.0f);
3259}
3260
3261float line_point_factor_v3_ex(const float p[3],
3262 const float l1[3],
3263 const float l2[3],
3264 const float epsilon,
3265 const float fallback)
3266{
3267 float h[3], u[3];
3268 float dot;
3269 sub_v3_v3v3(u, l2, l1);
3270 sub_v3_v3v3(h, p, l1);
3271
3272 /* better check for zero */
3273 dot = len_squared_v3(u);
3274 return (dot > epsilon) ? (dot_v3v3(u, h) / dot) : fallback;
3275}
3276float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
3277{
3278 return line_point_factor_v3_ex(p, l1, l2, 0.0f, 0.0f);
3279}
3280
3281float line_point_factor_v2_ex(const float p[2],
3282 const float l1[2],
3283 const float l2[2],
3284 const float epsilon,
3285 const float fallback)
3286{
3287 float h[2], u[2];
3288 float dot;
3289 sub_v2_v2v2(u, l2, l1);
3290 sub_v2_v2v2(h, p, l1);
3291 /* better check for zero */
3292 dot = len_squared_v2(u);
3293 return (dot > epsilon) ? (dot_v2v2(u, h) / dot) : fallback;
3294}
3295
3296float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
3297{
3298 return line_point_factor_v2_ex(p, l1, l2, 0.0f, 0.0f);
3299}
3300
3301float line_plane_factor_v3(const float plane_co[3],
3302 const float plane_no[3],
3303 const float l1[3],
3304 const float l2[3])
3305{
3306 float u[3], h[3];
3307 float dot;
3308 sub_v3_v3v3(u, l2, l1);
3309 sub_v3_v3v3(h, l1, plane_co);
3310 dot = dot_v3v3(plane_no, u);
3311 return (dot != 0.0f) ? -dot_v3v3(plane_no, h) / dot : 0.0f;
3312}
3313
3314void limit_dist_v3(float v1[3], float v2[3], const float dist)
3315{
3316 const float dist_old = len_v3v3(v1, v2);
3317
3318 if (dist_old > dist) {
3319 float v1_old[3];
3320 float v2_old[3];
3321 float fac = (dist / dist_old) * 0.5f;
3322
3323 copy_v3_v3(v1_old, v1);
3324 copy_v3_v3(v2_old, v2);
3325
3326 interp_v3_v3v3(v1, v1_old, v2_old, 0.5f - fac);
3327 interp_v3_v3v3(v2, v1_old, v2_old, 0.5f + fac);
3328 }
3329}
3330
3332 const int x1, const int y1, const int x2, const int y2, const int a, const int b)
3333{
3334 float v1[2], v2[2], v3[2], p[2];
3335
3336 v1[0] = float(x1);
3337 v1[1] = float(y1);
3338
3339 v2[0] = float(x1);
3340 v2[1] = float(y2);
3341
3342 v3[0] = float(x2);
3343 v3[1] = float(y1);
3344
3345 p[0] = float(a);
3346 p[1] = float(b);
3347
3348 return isect_point_tri_v2(p, v1, v2, v3);
3349}
3350
3351static bool point_in_slice(const float p[3],
3352 const float v1[3],
3353 const float l1[3],
3354 const float l2[3])
3355{
3356 /* What is a slice?
3357 * Some math:
3358 * a line including (l1, l2) and a point not on the line
3359 * define a subset of R3 delimited by planes parallel to the line and orthogonal
3360 * to the (point --> line) distance vector, one plane on the line one on the point,
3361 * the room inside usually is rather small compared to R3 though still infinite
3362 * useful for restricting (speeding up) searches
3363 * e.g. all points of triangular prism are within the intersection of 3 "slices"
3364 * Another trivial case is a cube, but see a "spat" which is a deformed cube
3365 * with paired parallel planes needs only 3 slices too. */
3366 float h, rp[3], cp[3], q[3];
3367
3368 closest_to_line_v3(cp, v1, l1, l2);
3369 sub_v3_v3v3(q, cp, v1);
3370
3371 sub_v3_v3v3(rp, p, v1);
3372 h = dot_v3v3(q, rp) / dot_v3v3(q, q);
3373 /* NOTE: when 'h' is nan/-nan, this check returns false
3374 * without explicit check - covering the degenerate case */
3375 return (h >= 0.0f && h <= 1.0f);
3376}
3377
3378/* Adult sister defining the slice planes by the origin and the normal.
3379 * NOTE: |normal| may not be 1 but defining the thickness of the slice. */
3380static bool point_in_slice_as(const float p[3], const float origin[3], const float normal[3])
3381{
3382 float h, rp[3];
3383 sub_v3_v3v3(rp, p, origin);
3384 h = dot_v3v3(normal, rp) / dot_v3v3(normal, normal);
3385 if (h < 0.0f || h > 1.0f) {
3386 return false;
3387 }
3388 return true;
3389}
3390
3391bool point_in_slice_seg(float p[3], float l1[3], float l2[3])
3392{
3393 float normal[3];
3394
3395 sub_v3_v3v3(normal, l2, l1);
3396
3397 return point_in_slice_as(p, l1, normal);
3398}
3399
3400bool isect_point_tri_prism_v3(const float p[3],
3401 const float v1[3],
3402 const float v2[3],
3403 const float v3[3])
3404{
3405 if (!point_in_slice(p, v1, v2, v3)) {
3406 return false;
3407 }
3408 if (!point_in_slice(p, v2, v3, v1)) {
3409 return false;
3410 }
3411 if (!point_in_slice(p, v3, v1, v2)) {
3412 return false;
3413 }
3414 return true;
3415}
3416
3418 const float p[3], const float v1[3], const float v2[3], const float v3[3], float r_isect_co[3])
3419{
3420 if (isect_point_tri_prism_v3(p, v1, v2, v3)) {
3421 float plane[4];
3422 float no[3];
3423
3424 /* Could use normal_tri_v3, but doesn't have to be unit-length */
3425 cross_tri_v3(no, v1, v2, v3);
3426 BLI_assert(len_squared_v3(no) != 0.0f);
3427
3428 plane_from_point_normal_v3(plane, v1, no);
3429 closest_to_plane_v3(r_isect_co, plane, p);
3430
3431 return true;
3432 }
3433
3434 return false;
3435}
3436
3438 const float p1[3], const float p2[3], const float plane[4], float r_p1[3], float r_p2[3])
3439{
3440 float dp[3], div;
3441
3442 sub_v3_v3v3(dp, p2, p1);
3443 div = dot_v3v3(dp, plane);
3444
3445 if (div == 0.0f) {
3446 /* parallel */
3447 return true;
3448 }
3449
3450 float t = -plane_point_side_v3(plane, p1);
3451
3452 if (div > 0.0f) {
3453 /* behind plane, completely clipped */
3454 if (t >= div) {
3455 return false;
3456 }
3457 if (t > 0.0f) {
3458 const float p1_copy[3] = {UNPACK3(p1)};
3459 copy_v3_v3(r_p2, p2);
3460 madd_v3_v3v3fl(r_p1, p1_copy, dp, t / div);
3461 return true;
3462 }
3463 }
3464 else {
3465 /* behind plane, completely clipped */
3466 if (t >= 0.0f) {
3467 return false;
3468 }
3469 if (t > div) {
3470 const float p1_copy[3] = {UNPACK3(p1)};
3471 copy_v3_v3(r_p1, p1);
3472 madd_v3_v3v3fl(r_p2, p1_copy, dp, t / div);
3473 return true;
3474 }
3475 }
3476
3477 /* In case input/output values match (above also). */
3478 const float p1_copy[3] = {UNPACK3(p1)};
3479 copy_v3_v3(r_p2, p2);
3480 copy_v3_v3(r_p1, p1_copy);
3481 return true;
3482}
3483
3484bool clip_segment_v3_plane_n(const float p1[3],
3485 const float p2[3],
3486 const float plane_array[][4],
3487 const int plane_num,
3488 float r_p1[3],
3489 float r_p2[3])
3490{
3491 /* intersect from both directions */
3492 float p1_fac = 0.0f, p2_fac = 1.0f;
3493
3494 float dp[3];
3495 sub_v3_v3v3(dp, p2, p1);
3496
3497 for (int i = 0; i < plane_num; i++) {
3498 const float *plane = plane_array[i];
3499 const float div = dot_v3v3(dp, plane);
3500
3501 if (div != 0.0f) {
3502 float t = -plane_point_side_v3(plane, p1);
3503 if (div > 0.0f) {
3504 /* clip p1 lower bounds */
3505 if (t >= div) {
3506 return false;
3507 }
3508 if (t > 0.0f) {
3509 t /= div;
3510 if (t > p1_fac) {
3511 p1_fac = t;
3512 if (p1_fac > p2_fac) {
3513 return false;
3514 }
3515 }
3516 }
3517 }
3518 else if (div < 0.0f) {
3519 /* clip p2 upper bounds */
3520 if (t >= 0.0f) {
3521 return false;
3522 }
3523 if (t > div) {
3524 t /= div;
3525 if (t < p2_fac) {
3526 p2_fac = t;
3527 if (p1_fac > p2_fac) {
3528 return false;
3529 }
3530 }
3531 }
3532 }
3533 }
3534 }
3535
3536 /* In case input/output values match. */
3537 const float p1_copy[3] = {UNPACK3(p1)};
3538
3539 madd_v3_v3v3fl(r_p1, p1_copy, dp, p1_fac);
3540 madd_v3_v3v3fl(r_p2, p1_copy, dp, p2_fac);
3541
3542 return true;
3543}
3544
3545/****************************** Axis Utils ********************************/
3546
3547void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
3548{
3549 BLI_ASSERT_UNIT_V3(normal);
3550
3551 copy_v3_v3(r_mat[2], normal);
3552 ortho_basis_v3v3_v3(r_mat[0], r_mat[1], r_mat[2]);
3553
3554 BLI_ASSERT_UNIT_V3(r_mat[0]);
3555 BLI_ASSERT_UNIT_V3(r_mat[1]);
3556
3557 transpose_m3(r_mat);
3558
3559 BLI_assert(!is_negative_m3(r_mat));
3560 BLI_assert((fabsf(dot_m3_v3_row_z(r_mat, normal) - 1.0f) < BLI_ASSERT_UNIT_EPSILON) ||
3561 is_zero_v3(normal));
3562}
3563
3564void axis_dominant_v3_to_m3_negate(float r_mat[3][3], const float normal[3])
3565{
3566 BLI_ASSERT_UNIT_V3(normal);
3567
3568 negate_v3_v3(r_mat[2], normal);
3569 ortho_basis_v3v3_v3(r_mat[0], r_mat[1], r_mat[2]);
3570
3571 BLI_ASSERT_UNIT_V3(r_mat[0]);
3572 BLI_ASSERT_UNIT_V3(r_mat[1]);
3573
3574 transpose_m3(r_mat);
3575
3576 BLI_assert(!is_negative_m3(r_mat));
3577 BLI_assert((dot_m3_v3_row_z(r_mat, normal) < BLI_ASSERT_UNIT_EPSILON) || is_zero_v3(normal));
3578}
3579
3580/****************************** Interpolation ********************************/
3581
3582static float tri_signed_area(
3583 const float v1[3], const float v2[3], const float v3[3], const int i, const int j)
3584{
3585 return 0.5f * ((v1[i] - v2[i]) * (v2[j] - v3[j]) + (v1[j] - v2[j]) * (v3[i] - v2[i]));
3586}
3587
3591static bool barycentric_weights(const float v1[3],
3592 const float v2[3],
3593 const float v3[3],
3594 const float co[3],
3595 const float n[3],
3596 float w[3])
3597{
3598 float wtot;
3599 int i, j;
3600
3601 axis_dominant_v3(&i, &j, n);
3602
3603 w[0] = tri_signed_area(v2, v3, co, i, j);
3604 w[1] = tri_signed_area(v3, v1, co, i, j);
3605 w[2] = tri_signed_area(v1, v2, co, i, j);
3606
3607 wtot = w[0] + w[1] + w[2];
3608
3609#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3610 if (wtot != 0.0f)
3611#endif
3612 {
3613 mul_v3_fl(w, 1.0f / wtot);
3614 if (is_finite_v3(w)) {
3615 return true;
3616 }
3617 }
3618 /* Zero area triangle. */
3619 copy_v3_fl(w, 1.0f / 3.0f);
3620 return false;
3621}
3622
3624 float w[3], const float v1[3], const float v2[3], const float v3[3], const float co[3])
3625{
3626 float n[3];
3627
3628 normal_tri_v3(n, v1, v2, v3);
3629 barycentric_weights(v1, v2, v3, co, n, w);
3630}
3631
3633 const float v1[3],
3634 const float v2[3],
3635 const float v3[3],
3636 const float v4[3],
3637 const float co[3])
3638{
3639 float w2[3];
3640
3641 zero_v4(w);
3642
3643 /* first check for exact match */
3644 if (equals_v3v3(co, v1)) {
3645 w[0] = 1.0f;
3646 }
3647 else if (equals_v3v3(co, v2)) {
3648 w[1] = 1.0f;
3649 }
3650 else if (equals_v3v3(co, v3)) {
3651 w[2] = 1.0f;
3652 }
3653 else if (equals_v3v3(co, v4)) {
3654 w[3] = 1.0f;
3655 }
3656 else {
3657 /* otherwise compute barycentric interpolation weights */
3658 float n1[3], n2[3], n[3];
3659 bool ok;
3660
3661 sub_v3_v3v3(n1, v1, v3);
3662 sub_v3_v3v3(n2, v2, v4);
3663 cross_v3_v3v3(n, n1, n2);
3664
3665 ok = barycentric_weights(v1, v2, v4, co, n, w);
3666 std::swap(w[2], w[3]);
3667
3668 if (!ok || (w[0] < 0.0f)) {
3669 /* if w[1] is negative, co is on the other side of the v1-v3 edge,
3670 * so we interpolate using the other triangle */
3671 ok = barycentric_weights(v2, v3, v4, co, n, w2);
3672
3673 if (ok) {
3674 w[0] = 0.0f;
3675 w[1] = w2[0];
3676 w[2] = w2[1];
3677 w[3] = w2[2];
3678 }
3679 }
3680 }
3681}
3682
3684{
3685 if (IN_RANGE(w[0], 0.0f, 1.0f) && IN_RANGE(w[1], 0.0f, 1.0f) && IN_RANGE(w[2], 0.0f, 1.0f)) {
3686 return 1;
3687 }
3688 if (IN_RANGE_INCL(w[0], 0.0f, 1.0f) && IN_RANGE_INCL(w[1], 0.0f, 1.0f) &&
3689 IN_RANGE_INCL(w[2], 0.0f, 1.0f))
3690 {
3691 return 2;
3692 }
3693
3694 return 0;
3695}
3696
3698 const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
3699{
3700 const float x = co[0], y = co[1];
3701 const float x1 = v1[0], y1 = v1[1];
3702 const float x2 = v2[0], y2 = v2[1];
3703 const float x3 = v3[0], y3 = v3[1];
3704 const float det = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
3705
3706#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3707 if (det != 0.0f)
3708#endif
3709 {
3710 w[0] = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / det;
3711 w[1] = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / det;
3712 w[2] = 1.0f - w[0] - w[1];
3713 if (is_finite_v3(w)) {
3714 return true;
3715 }
3716 }
3717
3718 return false;
3719}
3720
3722 const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
3723{
3724 float wtot;
3725
3726 w[0] = cross_tri_v2(v2, v3, co);
3727 w[1] = cross_tri_v2(v3, v1, co);
3728 w[2] = cross_tri_v2(v1, v2, co);
3729 wtot = w[0] + w[1] + w[2];
3730
3731#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3732 if (wtot != 0.0f)
3733#endif
3734 {
3735 mul_v3_fl(w, 1.0f / wtot);
3736 if (is_finite_v3(w)) {
3737 return;
3738 }
3739 }
3740 /* Dummy values for zero area face. */
3741 copy_v3_fl(w, 1.0f / 3.0f);
3742}
3743
3745 const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
3746{
3747 float wtot;
3748
3749 w[0] = max_ff(cross_tri_v2(v2, v3, co), 0.0f);
3750 w[1] = max_ff(cross_tri_v2(v3, v1, co), 0.0f);
3751 w[2] = max_ff(cross_tri_v2(v1, v2, co), 0.0f);
3752 wtot = w[0] + w[1] + w[2];
3753
3754#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3755 if (wtot != 0.0f)
3756#endif
3757 {
3758 mul_v3_fl(w, 1.0f / wtot);
3759 if (is_finite_v3(w)) {
3760 return;
3761 }
3762 }
3763 /* Dummy values for zero area face. */
3764 copy_v3_fl(w, 1.0f / 3.0f);
3765}
3766
3768 const float v1[4], const float v2[4], const float v3[4], const float co[2], float w[3])
3769{
3770 float wtot;
3771
3772 w[0] = cross_tri_v2(v2, v3, co) / v1[3];
3773 w[1] = cross_tri_v2(v3, v1, co) / v2[3];
3774 w[2] = cross_tri_v2(v1, v2, co) / v3[3];
3775 wtot = w[0] + w[1] + w[2];
3776
3777#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3778 if (wtot != 0.0f)
3779#endif
3780 {
3781 mul_v3_fl(w, 1.0f / wtot);
3782 if (is_finite_v3(w)) {
3783 return;
3784 }
3785 }
3786 /* Dummy values for zero area face. */
3787 copy_v3_fl(w, 1.0f / 3.0f);
3788}
3789
3790void barycentric_weights_v2_quad(const float v1[2],
3791 const float v2[2],
3792 const float v3[2],
3793 const float v4[2],
3794 const float co[2],
3795 float w[4])
3796{
3797 /* NOTE(@ideasman42): fabsf() here is not needed for convex quads
3798 * (and not used in #interp_weights_poly_v2).
3799 * But in the case of concave/bow-tie quads for the mask rasterizer it
3800 * gives unreliable results without adding `absf()`. If this becomes an issue for more general
3801 * usage we could have this optional or use a different function. */
3802#define MEAN_VALUE_HALF_TAN_V2(_area, i1, i2) \
3803 ((_area = cross_v2v2(dirs[i1], dirs[i2])) != 0.0f ? \
3804 fabsf(((lens[i1] * lens[i2]) - dot_v2v2(dirs[i1], dirs[i2])) / _area) : \
3805 0.0f)
3806
3807 const float dirs[4][2] = {
3808 {v1[0] - co[0], v1[1] - co[1]},
3809 {v2[0] - co[0], v2[1] - co[1]},
3810 {v3[0] - co[0], v3[1] - co[1]},
3811 {v4[0] - co[0], v4[1] - co[1]},
3812 };
3813
3814 const float lens[4] = {
3815 len_v2(dirs[0]),
3816 len_v2(dirs[1]),
3817 len_v2(dirs[2]),
3818 len_v2(dirs[3]),
3819 };
3820
3821 /* avoid divide by zero */
3822 if (UNLIKELY(lens[0] < FLT_EPSILON)) {
3823 w[0] = 1.0f;
3824 w[1] = w[2] = w[3] = 0.0f;
3825 }
3826 else if (UNLIKELY(lens[1] < FLT_EPSILON)) {
3827 w[1] = 1.0f;
3828 w[0] = w[2] = w[3] = 0.0f;
3829 }
3830 else if (UNLIKELY(lens[2] < FLT_EPSILON)) {
3831 w[2] = 1.0f;
3832 w[0] = w[1] = w[3] = 0.0f;
3833 }
3834 else if (UNLIKELY(lens[3] < FLT_EPSILON)) {
3835 w[3] = 1.0f;
3836 w[0] = w[1] = w[2] = 0.0f;
3837 }
3838 else {
3839 float wtot, area;
3840
3841 /* variable 'area' is just for storage,
3842 * the order its initialized doesn't matter */
3843#ifdef __clang__
3844# pragma clang diagnostic push
3845# pragma clang diagnostic ignored "-Wunsequenced"
3846#endif
3847
3848 /* inline mean_value_half_tan four times here */
3849 const float t[4] = {
3850 MEAN_VALUE_HALF_TAN_V2(area, 0, 1),
3851 MEAN_VALUE_HALF_TAN_V2(area, 1, 2),
3852 MEAN_VALUE_HALF_TAN_V2(area, 2, 3),
3853 MEAN_VALUE_HALF_TAN_V2(area, 3, 0),
3854 };
3855
3856#ifdef __clang__
3857# pragma clang diagnostic pop
3858#endif
3859
3860#undef MEAN_VALUE_HALF_TAN_V2
3861
3862 w[0] = (t[3] + t[0]) / lens[0];
3863 w[1] = (t[0] + t[1]) / lens[1];
3864 w[2] = (t[1] + t[2]) / lens[2];
3865 w[3] = (t[2] + t[3]) / lens[3];
3866
3867 wtot = w[0] + w[1] + w[2] + w[3];
3868
3869#ifndef NDEBUG /* Avoid floating point exception when debugging. */
3870 if (wtot != 0.0f)
3871#endif
3872 {
3873 mul_v4_fl(w, 1.0f / wtot);
3874 if (is_finite_v4(w)) {
3875 return;
3876 }
3877 }
3878 /* Dummy values for zero area face. */
3879 copy_v4_fl(w, 1.0f / 4.0f);
3880 }
3881}
3882
3883void transform_point_by_tri_v3(float pt_tar[3],
3884 float const pt_src[3],
3885 const float tri_tar_p1[3],
3886 const float tri_tar_p2[3],
3887 const float tri_tar_p3[3],
3888 const float tri_src_p1[3],
3889 const float tri_src_p2[3],
3890 const float tri_src_p3[3])
3891{
3892 /* this works by moving the source triangle so its normal is pointing on the Z
3893 * axis where its barycentric weights can be calculated in 2D and its Z offset can
3894 * be re-applied. The weights are applied directly to the targets 3D points and the
3895 * z-depth is used to scale the targets normal as an offset.
3896 * This saves transforming the target into its Z-Up orientation and back
3897 * (which could also work) */
3898 float no_tar[3], no_src[3];
3899 float mat_src[3][3];
3900 float pt_src_xy[3];
3901 float tri_xy_src[3][3];
3902 float w_src[3];
3903 float area_tar, area_src;
3904 float z_ofs_src;
3905
3906 normal_tri_v3(no_tar, tri_tar_p1, tri_tar_p2, tri_tar_p3);
3907 normal_tri_v3(no_src, tri_src_p1, tri_src_p2, tri_src_p3);
3908
3909 axis_dominant_v3_to_m3(mat_src, no_src);
3910
3911 /* make the source tri xy space */
3912 mul_v3_m3v3(pt_src_xy, mat_src, pt_src);
3913 mul_v3_m3v3(tri_xy_src[0], mat_src, tri_src_p1);
3914 mul_v3_m3v3(tri_xy_src[1], mat_src, tri_src_p2);
3915 mul_v3_m3v3(tri_xy_src[2], mat_src, tri_src_p3);
3916
3917 barycentric_weights_v2(tri_xy_src[0], tri_xy_src[1], tri_xy_src[2], pt_src_xy, w_src);
3918 interp_v3_v3v3v3(pt_tar, tri_tar_p1, tri_tar_p2, tri_tar_p3, w_src);
3919
3920 area_tar = sqrtf(area_tri_v3(tri_tar_p1, tri_tar_p2, tri_tar_p3));
3921 area_src = sqrtf(area_tri_v2(tri_xy_src[0], tri_xy_src[1], tri_xy_src[2]));
3922
3923 z_ofs_src = pt_src_xy[2] - tri_xy_src[0][2];
3924 madd_v3_v3v3fl(pt_tar, pt_tar, no_tar, (z_ofs_src / area_src) * area_tar);
3925}
3926
3927void transform_point_by_seg_v3(float p_dst[3],
3928 const float p_src[3],
3929 const float l_dst_p1[3],
3930 const float l_dst_p2[3],
3931 const float l_src_p1[3],
3932 const float l_src_p2[3])
3933{
3934 float t = line_point_factor_v3(p_src, l_src_p1, l_src_p2);
3935 interp_v3_v3v3(p_dst, l_dst_p1, l_dst_p2, t);
3936}
3937
3938int interp_sparse_array(float *array, const int list_size, const float skipval)
3939{
3940 int found_invalid = 0;
3941 int found_valid = 0;
3942 int i;
3943
3944 for (i = 0; i < list_size; i++) {
3945 if (array[i] == skipval) {
3946 found_invalid = 1;
3947 }
3948 else {
3949 found_valid = 1;
3950 }
3951 }
3952
3953 if (found_valid == 0) {
3954 return -1;
3955 }
3956 if (found_invalid == 0) {
3957 return 0;
3958 }
3959
3960 /* found invalid depths, interpolate */
3961 float valid_last = skipval;
3962 int valid_ofs = 0;
3963
3964 blender::Array<float> array_up(list_size);
3965 blender::Array<float> array_down(list_size);
3966
3967 blender::Array<int> ofs_tot_up(list_size);
3968 blender::Array<int> ofs_tot_down(list_size);
3969
3970 for (i = 0; i < list_size; i++) {
3971 if (array[i] == skipval) {
3972 array_up[i] = valid_last;
3973 ofs_tot_up[i] = ++valid_ofs;
3974 }
3975 else {
3976 valid_last = array[i];
3977 valid_ofs = 0;
3978 }
3979 }
3980
3981 valid_last = skipval;
3982 valid_ofs = 0;
3983
3984 for (i = list_size - 1; i >= 0; i--) {
3985 if (array[i] == skipval) {
3986 array_down[i] = valid_last;
3987 ofs_tot_down[i] = ++valid_ofs;
3988 }
3989 else {
3990 valid_last = array[i];
3991 valid_ofs = 0;
3992 }
3993 }
3994
3995 /* now blend */
3996 for (i = 0; i < list_size; i++) {
3997 if (array[i] == skipval) {
3998 if (array_up[i] != skipval && array_down[i] != skipval) {
3999 array[i] = ((array_up[i] * float(ofs_tot_down[i])) +
4000 (array_down[i] * float(ofs_tot_up[i]))) /
4001 float(ofs_tot_down[i] + ofs_tot_up[i]);
4002 }
4003 else if (array_up[i] != skipval) {
4004 array[i] = array_up[i];
4005 }
4006 else if (array_down[i] != skipval) {
4007 array[i] = array_down[i];
4008 }
4009 }
4010 }
4011
4012 return 1;
4013}
4014
4015/* -------------------------------------------------------------------- */
4018
4019#define IS_POINT_IX (1 << 0)
4020#define IS_SEGMENT_IX (1 << 1)
4021
4022#define DIR_V3_SET(d_len, va, vb) \
4023 { \
4024 sub_v3_v3v3((d_len)->dir, va, vb); \
4025 (d_len)->len = len_v3((d_len)->dir); \
4026 } \
4027 (void)0
4028
4029#define DIR_V2_SET(d_len, va, vb) \
4030 { \
4031 sub_v2db_v2fl_v2fl((d_len)->dir, va, vb); \
4032 (d_len)->len = len_v2_db((d_len)->dir); \
4033 } \
4034 (void)0
4035
4037 float dir[3], len;
4038};
4039
4041 double dir[2], len;
4042};
4043
4044/* Mean value weights - smooth interpolation weights for polygons with
4045 * more than 3 vertices */
4046static float mean_value_half_tan_v3(const Float3_Len *d_curr, const Float3_Len *d_next)
4047{
4048 float cross[3];
4049 cross_v3_v3v3(cross, d_curr->dir, d_next->dir);
4050 const float area = len_v3(cross);
4051 /* Compare against zero since 'FLT_EPSILON' can be too large, see: #73348. */
4052 if (LIKELY(area != 0.0f)) {
4053 const float dot = dot_v3v3(d_curr->dir, d_next->dir);
4054 const float len = d_curr->len * d_next->len;
4055 const float result = (len - dot) / area;
4056 if (isfinite(result)) {
4057 return result;
4058 }
4059 }
4060 return 0.0f;
4061}
4062
4072static double mean_value_half_tan_v2_db(const Double2_Len *d_curr, const Double2_Len *d_next)
4073{
4074 /* Different from the 3d version but still correct. */
4075 const double area = cross_v2v2_db(d_curr->dir, d_next->dir);
4076 /* Compare against zero since 'FLT_EPSILON' can be too large, see: #73348. */
4077 if (LIKELY(area != 0.0)) {
4078 const double dot = dot_v2v2_db(d_curr->dir, d_next->dir);
4079 const double len = d_curr->len * d_next->len;
4080 const double result = (len - dot) / area;
4081 if (isfinite(result)) {
4082 return result;
4083 }
4084 }
4085 return 0.0;
4086}
4087
4088void interp_weights_poly_v3(float *w, float v[][3], const int n, const float co[3])
4089{
4090 /* Before starting to calculate the weight, we need to figure out the floating point precision we
4091 * can expect from the supplied data. */
4092 float max_value = 0;
4093
4094 for (int i = 0; i < n; i++) {
4095 max_value = max_ff(max_value, fabsf(v[i][0] - co[0]));
4096 max_value = max_ff(max_value, fabsf(v[i][1] - co[1]));
4097 max_value = max_ff(max_value, fabsf(v[i][2] - co[2]));
4098 }
4099
4100 /* These to values we derived by empirically testing different values that works for the test
4101 * files in D7772. */
4102 const float eps = 16.0f * FLT_EPSILON * max_value;
4103 const float eps_sq = eps * eps;
4104 const float *v_curr, *v_next;
4105 float ht_prev, ht; /* half tangents */
4106 float totweight = 0.0f;
4107 int i_curr, i_next;
4108 char ix_flag = 0;
4109 Float3_Len d_curr, d_next;
4110
4111 /* loop over 'i_next' */
4112 i_curr = n - 1;
4113 i_next = 0;
4114
4115 v_curr = v[i_curr];
4116 v_next = v[i_next];
4117
4118 DIR_V3_SET(&d_curr, v_curr - 3 /* v[n - 2] */, co);
4119 DIR_V3_SET(&d_next, v_curr /* v[n - 1] */, co);
4120 ht_prev = mean_value_half_tan_v3(&d_curr, &d_next);
4121
4122 while (i_next < n) {
4123 /* Mark Mayer et al algorithm that is used here does not operate well if vertex is close
4124 * to borders of face.
4125 * In that case, do simple linear interpolation between the two edge vertices */
4126
4127 /* 'd_next.len' is in fact 'd_curr.len', just avoid copy to begin with */
4128 if (UNLIKELY(d_next.len < eps)) {
4129 ix_flag = IS_POINT_IX;
4130 break;
4131 }
4132 if (UNLIKELY(dist_squared_to_line_segment_v3(co, v_curr, v_next) < eps_sq)) {
4133 ix_flag = IS_SEGMENT_IX;
4134 break;
4135 }
4136
4137 d_curr = d_next;
4138 DIR_V3_SET(&d_next, v_next, co);
4139 ht = mean_value_half_tan_v3(&d_curr, &d_next);
4140 w[i_curr] = (ht_prev + ht) / d_curr.len;
4141 totweight += w[i_curr];
4142
4143 /* step */
4144 i_curr = i_next++;
4145 v_curr = v_next;
4146 v_next = v[i_next];
4147
4148 ht_prev = ht;
4149 }
4150
4151 if (ix_flag) {
4152 memset(w, 0, sizeof(*w) * size_t(n));
4153
4154 if (ix_flag & IS_POINT_IX) {
4155 w[i_curr] = 1.0f;
4156 }
4157 else {
4158 float fac = line_point_factor_v3(co, v_curr, v_next);
4159 CLAMP(fac, 0.0f, 1.0f);
4160 w[i_curr] = 1.0f - fac;
4161 w[i_next] = fac;
4162 }
4163 }
4164 else {
4165 if (totweight != 0.0f) {
4166 for (i_curr = 0; i_curr < n; i_curr++) {
4167 w[i_curr] /= totweight;
4168 }
4169 }
4170 }
4171}
4172
4173void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[2])
4174{
4175 /* Before starting to calculate the weight, we need to figure out the floating point precision we
4176 * can expect from the supplied data. */
4177 float max_value = 0;
4178
4179 for (int i = 0; i < n; i++) {
4180 max_value = max_ff(max_value, fabsf(v[i][0] - co[0]));
4181 max_value = max_ff(max_value, fabsf(v[i][1] - co[1]));
4182 }
4183
4184 /* These to values we derived by empirically testing different values that works for the test
4185 * files in D7772. */
4186 const float eps = 16.0f * FLT_EPSILON * max_value;
4187 const float eps_sq = eps * eps;
4188
4189 const float *v_curr, *v_next;
4190 double ht_prev, ht; /* half tangents */
4191 float totweight = 0.0f;
4192 int i_curr, i_next;
4193 char ix_flag = 0;
4194 Double2_Len d_curr, d_next;
4195
4196 /* loop over 'i_next' */
4197 i_curr = n - 1;
4198 i_next = 0;
4199
4200 v_curr = v[i_curr];
4201 v_next = v[i_next];
4202
4203 DIR_V2_SET(&d_curr, v_curr - 2 /* v[n - 2] */, co);
4204 DIR_V2_SET(&d_next, v_curr /* v[n - 1] */, co);
4205 ht_prev = mean_value_half_tan_v2_db(&d_curr, &d_next);
4206
4207 while (i_next < n) {
4208 /* Mark Mayer et al algorithm that is used here does not operate well if vertex is close
4209 * to borders of face. In that case,
4210 * do simple linear interpolation between the two edge vertices */
4211
4212 /* 'd_next.len' is in fact 'd_curr.len', just avoid copy to begin with */
4213 if (UNLIKELY(d_next.len < eps)) {
4214 ix_flag = IS_POINT_IX;
4215 break;
4216 }
4217 if (UNLIKELY(dist_squared_to_line_segment_v2(co, v_curr, v_next) < eps_sq)) {
4218 ix_flag = IS_SEGMENT_IX;
4219 break;
4220 }
4221
4222 d_curr = d_next;
4223 DIR_V2_SET(&d_next, v_next, co);
4224 ht = mean_value_half_tan_v2_db(&d_curr, &d_next);
4225 w[i_curr] = (d_curr.len == 0.0) ? 0.0f : float((ht_prev + ht) / d_curr.len);
4226 totweight += w[i_curr];
4227
4228 /* step */
4229 i_curr = i_next++;
4230 v_curr = v_next;
4231 v_next = v[i_next];
4232
4233 ht_prev = ht;
4234 }
4235
4236 if (ix_flag) {
4237 memset(w, 0, sizeof(*w) * size_t(n));
4238
4239 if (ix_flag & IS_POINT_IX) {
4240 w[i_curr] = 1.0f;
4241 }
4242 else {
4243 float fac = line_point_factor_v2(co, v_curr, v_next);
4244 CLAMP(fac, 0.0f, 1.0f);
4245 w[i_curr] = 1.0f - fac;
4246 w[i_next] = fac;
4247 }
4248 }
4249 else {
4250 if (totweight != 0.0f) {
4251 for (i_curr = 0; i_curr < n; i_curr++) {
4252 w[i_curr] /= totweight;
4253 }
4254 }
4255 }
4256}
4257
4258#undef IS_POINT_IX
4259#undef IS_SEGMENT_IX
4260
4261#undef DIR_V3_SET
4262#undef DIR_V2_SET
4263
4265
4266void interp_cubic_v3(float x[3],
4267 float v[3],
4268 const float x1[3],
4269 const float v1[3],
4270 const float x2[3],
4271 const float v2[3],
4272 const float t)
4273{
4274 float a[3], b[3];
4275 const float t2 = t * t;
4276 const float t3 = t2 * t;
4277
4278 /* cubic interpolation */
4279 a[0] = v1[0] + v2[0] + 2 * (x1[0] - x2[0]);
4280 a[1] = v1[1] + v2[1] + 2 * (x1[1] - x2[1]);
4281 a[2] = v1[2] + v2[2] + 2 * (x1[2] - x2[2]);
4282
4283 b[0] = -2 * v1[0] - v2[0] - 3 * (x1[0] - x2[0]);
4284 b[1] = -2 * v1[1] - v2[1] - 3 * (x1[1] - x2[1]);
4285 b[2] = -2 * v1[2] - v2[2] - 3 * (x1[2] - x2[2]);
4286
4287 x[0] = a[0] * t3 + b[0] * t2 + v1[0] * t + x1[0];
4288 x[1] = a[1] * t3 + b[1] * t2 + v1[1] * t + x1[1];
4289 x[2] = a[2] * t3 + b[2] * t2 + v1[2] * t + x1[2];
4290
4291 v[0] = 3 * a[0] * t2 + 2 * b[0] * t + v1[0];
4292 v[1] = 3 * a[1] * t2 + 2 * b[1] * t + v1[1];
4293 v[2] = 3 * a[2] * t2 + 2 * b[2] * t + v1[2];
4294}
4295
4296/* unfortunately internal calculations have to be done at double precision
4297 * to achieve correct/stable results. */
4298
4299#define IS_ZERO(x) ((x > (-DBL_EPSILON) && x < DBL_EPSILON) ? 1 : 0)
4300
4302 float r_uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2])
4303{
4304 /* find UV such that
4305 * t = u * t0 + v * t1 + (1 - u - v) * t2
4306 * u * (t0 - t2) + v * (t1 - t2) = t - t2 */
4307 const double a = st0[0] - st2[0], b = st1[0] - st2[0];
4308 const double c = st0[1] - st2[1], d = st1[1] - st2[1];
4309 const double det = a * d - c * b;
4310
4311 /* det should never be zero since the determinant is the signed ST area of the triangle. */
4312 if (IS_ZERO(det) == 0) {
4313 const double x[2] = {st[0] - st2[0], st[1] - st2[1]};
4314
4315 r_uv[0] = float((d * x[0] - b * x[1]) / det);
4316 r_uv[1] = float(((-c) * x[0] + a * x[1]) / det);
4317 }
4318 else {
4319 zero_v2(r_uv);
4320 }
4321}
4322
4324 float r_uv[2], const float st[3], const float st0[3], const float st1[3], const float st2[3])
4325{
4326 float v0[3], v1[3], v2[3];
4327 double d00, d01, d11, d20, d21, det;
4328
4329 sub_v3_v3v3(v0, st1, st0);
4330 sub_v3_v3v3(v1, st2, st0);
4331 sub_v3_v3v3(v2, st, st0);
4332
4333 d00 = dot_v3v3(v0, v0);
4334 d01 = dot_v3v3(v0, v1);
4335 d11 = dot_v3v3(v1, v1);
4336 d20 = dot_v3v3(v2, v0);
4337 d21 = dot_v3v3(v2, v1);
4338
4339 det = d00 * d11 - d01 * d01;
4340
4341 /* det should never be zero since the determinant is the signed ST area of the triangle. */
4342 if (IS_ZERO(det) == 0) {
4343 float w;
4344
4345 w = float((d00 * d21 - d01 * d20) / det);
4346 r_uv[1] = float((d11 * d20 - d01 * d21) / det);
4347 r_uv[0] = 1.0f - r_uv[1] - w;
4348 }
4349 else {
4350 zero_v2(r_uv);
4351 }
4352}
4353
4354void resolve_quad_uv_v2(float r_uv[2],
4355 const float st[2],
4356 const float st0[2],
4357 const float st1[2],
4358 const float st2[2],
4359 const float st3[2])
4360{
4361 resolve_quad_uv_v2_deriv(r_uv, nullptr, st, st0, st1, st2, st3);
4362}
4363
4364void resolve_quad_uv_v2_deriv(float r_uv[2],
4365 float r_deriv[2][2],
4366 const float st[2],
4367 const float st0[2],
4368 const float st1[2],
4369 const float st2[2],
4370 const float st3[2])
4371{
4372 const double signed_area = (st0[0] * st1[1] - st0[1] * st1[0]) +
4373 (st1[0] * st2[1] - st1[1] * st2[0]) +
4374 (st2[0] * st3[1] - st2[1] * st3[0]) +
4375 (st3[0] * st0[1] - st3[1] * st0[0]);
4376
4377 /* X is 2D cross product (determinant)
4378 * A = (p0 - p) X (p0 - p3) */
4379 const double a = (st0[0] - st[0]) * (st0[1] - st3[1]) - (st0[1] - st[1]) * (st0[0] - st3[0]);
4380
4381 /* B = ( (p0 - p) X (p1 - p2) + (p1 - p) X (p0 - p3) ) / 2 */
4382 const double b =
4383 0.5 * double(((st0[0] - st[0]) * (st1[1] - st2[1]) - (st0[1] - st[1]) * (st1[0] - st2[0])) +
4384 ((st1[0] - st[0]) * (st0[1] - st3[1]) - (st1[1] - st[1]) * (st0[0] - st3[0])));
4385
4386 /* C = (p1-p) X (p1-p2) */
4387 const double fC = (st1[0] - st[0]) * (st1[1] - st2[1]) - (st1[1] - st[1]) * (st1[0] - st2[0]);
4388 double denom = a - 2 * b + fC;
4389
4390 /* clear outputs */
4391 zero_v2(r_uv);
4392
4393 if (IS_ZERO(denom) != 0) {
4394 const double fDen = a - fC;
4395 if (IS_ZERO(fDen) == 0) {
4396 r_uv[0] = float(a / fDen);
4397 }
4398 }
4399 else {
4400 const double desc_sq = b * b - a * fC;
4401 const double desc = sqrt(desc_sq < 0.0 ? 0.0 : desc_sq);
4402 const double s = signed_area > 0 ? (-1.0) : 1.0;
4403
4404 r_uv[0] = float(((a - b) + s * desc) / denom);
4405 }
4406
4407 /* find UV such that
4408 * fST = (1-u)(1-v) * ST0 + u * (1-v) * ST1 + u * v * ST2 + (1-u) * v * ST3 */
4409 {
4410 const double denom_s = (1 - r_uv[0]) * (st0[0] - st3[0]) + r_uv[0] * (st1[0] - st2[0]);
4411 const double denom_t = (1 - r_uv[0]) * (st0[1] - st3[1]) + r_uv[0] * (st1[1] - st2[1]);
4412 int i = 0;
4413 denom = denom_s;
4414
4415 if (fabs(denom_s) < fabs(denom_t)) {
4416 i = 1;
4417 denom = denom_t;
4418 }
4419
4420 if (IS_ZERO(denom) == 0) {
4421 r_uv[1] = float(double((1.0f - r_uv[0]) * (st0[i] - st[i]) + r_uv[0] * (st1[i] - st[i])) /
4422 denom);
4423 }
4424 }
4425
4426 if (r_deriv) {
4427 float tmp1[2], tmp2[2], s[2], t[2];
4428
4429 /* clear outputs */
4430 zero_v2(r_deriv[0]);
4431 zero_v2(r_deriv[1]);
4432
4433 sub_v2_v2v2(tmp1, st1, st0);
4434 sub_v2_v2v2(tmp2, st2, st3);
4435 interp_v2_v2v2(s, tmp1, tmp2, r_uv[1]);
4436 sub_v2_v2v2(tmp1, st3, st0);
4437 sub_v2_v2v2(tmp2, st2, st1);
4438 interp_v2_v2v2(t, tmp1, tmp2, r_uv[0]);
4439
4440 denom = t[0] * s[1] - t[1] * s[0];
4441
4442 if (!IS_ZERO(denom)) {
4443 double inv_denom = 1.0 / denom;
4444 r_deriv[0][0] = float(double(-t[1]) * inv_denom);
4445 r_deriv[0][1] = float(double(t[0]) * inv_denom);
4446 r_deriv[1][0] = float(double(s[1]) * inv_denom);
4447 r_deriv[1][1] = float(double(-s[0]) * inv_denom);
4448 }
4449 }
4450}
4451
4452float resolve_quad_u_v2(const float st[2],
4453 const float st0[2],
4454 const float st1[2],
4455 const float st2[2],
4456 const float st3[2])
4457{
4458 const double signed_area = (st0[0] * st1[1] - st0[1] * st1[0]) +
4459 (st1[0] * st2[1] - st1[1] * st2[0]) +
4460 (st2[0] * st3[1] - st2[1] * st3[0]) +
4461 (st3[0] * st0[1] - st3[1] * st0[0]);
4462
4463 /* X is 2D cross product (determinant)
4464 * A = (p0 - p) X (p0 - p3) */
4465 const double a = (st0[0] - st[0]) * (st0[1] - st3[1]) - (st0[1] - st[1]) * (st0[0] - st3[0]);
4466
4467 /* B = ( (p0 - p) X (p1 - p2) + (p1 - p) X (p0 - p3) ) / 2 */
4468 const double b =
4469 0.5 * double(((st0[0] - st[0]) * (st1[1] - st2[1]) - (st0[1] - st[1]) * (st1[0] - st2[0])) +
4470 ((st1[0] - st[0]) * (st0[1] - st3[1]) - (st1[1] - st[1]) * (st0[0] - st3[0])));
4471
4472 /* C = (p1-p) X (p1-p2) */
4473 const double fC = (st1[0] - st[0]) * (st1[1] - st2[1]) - (st1[1] - st[1]) * (st1[0] - st2[0]);
4474 double denom = a - 2 * b + fC;
4475
4476 if (IS_ZERO(denom) != 0) {
4477 const double fDen = a - fC;
4478 if (IS_ZERO(fDen) == 0) {
4479 return float(a / fDen);
4480 }
4481
4482 return 0.0f;
4483 }
4484
4485 const double desc_sq = b * b - a * fC;
4486 const double desc = sqrt(desc_sq < 0.0 ? 0.0 : desc_sq);
4487 const double s = signed_area > 0 ? (-1.0) : 1.0;
4488
4489 return float(((a - b) + s * desc) / denom);
4490}
4491
4492#undef IS_ZERO
4493
4494void interp_bilinear_quad_v3(float data[4][3], float u, float v, float res[3])
4495{
4496 float vec[3];
4497
4498 copy_v3_v3(res, data[0]);
4499 mul_v3_fl(res, (1 - u) * (1 - v));
4500 copy_v3_v3(vec, data[1]);
4501 mul_v3_fl(vec, u * (1 - v));
4502 add_v3_v3(res, vec);
4503 copy_v3_v3(vec, data[2]);
4504 mul_v3_fl(vec, u * v);
4505 add_v3_v3(res, vec);
4506 copy_v3_v3(vec, data[3]);
4507 mul_v3_fl(vec, (1 - u) * v);
4508 add_v3_v3(res, vec);
4509}
4510
4511void interp_barycentric_tri_v3(float data[3][3], float u, float v, float res[3])
4512{
4513 float vec[3];
4514
4515 copy_v3_v3(res, data[0]);
4516 mul_v3_fl(res, u);
4517 copy_v3_v3(vec, data[1]);
4518 mul_v3_fl(vec, v);
4519 add_v3_v3(res, vec);
4520 copy_v3_v3(vec, data[2]);
4521 mul_v3_fl(vec, 1.0f - u - v);
4522 add_v3_v3(res, vec);
4523}
4524
4525/***************************** View & Projection *****************************/
4526
4527void orthographic_m4(float mat[4][4],
4528 const float left,
4529 const float right,
4530 const float bottom,
4531 const float top,
4532 const float nearClip,
4533 const float farClip)
4534{
4535 float Xdelta, Ydelta, Zdelta;
4536
4537 Xdelta = right - left;
4538 Ydelta = top - bottom;
4539 Zdelta = farClip - nearClip;
4540 if (Xdelta == 0.0f || Ydelta == 0.0f || Zdelta == 0.0f) {
4541 return;
4542 }
4543 unit_m4(mat);
4544 mat[0][0] = 2.0f / Xdelta;
4545 mat[3][0] = -(right + left) / Xdelta;
4546 mat[1][1] = 2.0f / Ydelta;
4547 mat[3][1] = -(top + bottom) / Ydelta;
4548 mat[2][2] = -2.0f / Zdelta; /* NOTE: negate Z. */
4549 mat[3][2] = -(farClip + nearClip) / Zdelta;
4550}
4551
4552void perspective_m4(float mat[4][4],
4553 const float left,
4554 const float right,
4555 const float bottom,
4556 const float top,
4557 const float nearClip,
4558 const float farClip)
4559{
4560 const float Xdelta = right - left;
4561 const float Ydelta = top - bottom;
4562 const float Zdelta = farClip - nearClip;
4563
4564 if (Xdelta == 0.0f || Ydelta == 0.0f || Zdelta == 0.0f) {
4565 return;
4566 }
4567 mat[0][0] = nearClip * 2.0f / Xdelta;
4568 mat[1][1] = nearClip * 2.0f / Ydelta;
4569 mat[2][0] = (right + left) / Xdelta; /* NOTE: negate Z. */
4570 mat[2][1] = (top + bottom) / Ydelta;
4571 mat[2][2] = -(farClip + nearClip) / Zdelta;
4572 mat[2][3] = -1.0f;
4573 mat[3][2] = (-2.0f * nearClip * farClip) / Zdelta;
4574 mat[0][1] = mat[0][2] = mat[0][3] = mat[1][0] = mat[1][2] = mat[1][3] = mat[3][0] = mat[3][1] =
4575 mat[3][3] = 0.0f;
4576}
4577
4578void perspective_m4_fov(float mat[4][4],
4579 const float angle_left,
4580 const float angle_right,
4581 const float angle_up,
4582 const float angle_down,
4583 const float nearClip,
4584 const float farClip)
4585{
4586 const float tan_angle_left = tanf(angle_left);
4587 const float tan_angle_right = tanf(angle_right);
4588 const float tan_angle_bottom = tanf(angle_up);
4589 const float tan_angle_top = tanf(angle_down);
4590
4592 mat, tan_angle_left, tan_angle_right, tan_angle_top, tan_angle_bottom, nearClip, farClip);
4593 mat[0][0] /= nearClip;
4594 mat[1][1] /= nearClip;
4595}
4596
4597void window_translate_m4(float winmat[4][4], float perspmat[4][4], const float x, const float y)
4598{
4599 if (winmat[2][3] == -1.0f) {
4600 /* in the case of a win-matrix, this means perspective always */
4601 float v1[3];
4602 float v2[3];
4603 float len1, len2;
4604
4605 v1[0] = perspmat[0][0];
4606 v1[1] = perspmat[1][0];
4607 v1[2] = perspmat[2][0];
4608
4609 v2[0] = perspmat[0][1];
4610 v2[1] = perspmat[1][1];
4611 v2[2] = perspmat[2][1];
4612
4613 len1 = (1.0f / len_v3(v1));
4614 len2 = (1.0f / len_v3(v2));
4615
4616 winmat[2][0] -= len1 * winmat[0][0] * x;
4617 winmat[2][1] -= len2 * winmat[1][1] * y;
4618 }
4619 else {
4620 winmat[3][0] += x;
4621 winmat[3][1] += y;
4622 }
4623}
4624
4625void planes_from_projmat(const float mat[4][4],
4626 float left[4],
4627 float right[4],
4628 float bottom[4],
4629 float top[4],
4630 float near[4],
4631 float far[4])
4632{
4633 /* References:
4634 *
4635 * https://fgiesen.wordpress.com/2012/08/31/frustum-planes-from-the-projection-matrix/
4636 * http://www8.cs.umu.se/kurser/5DV051/HT12/lab/plane_extraction.pdf
4637 */
4638
4639 int i;
4640
4641 if (left) {
4642 for (i = 4; i--;) {
4643 left[i] = mat[i][3] + mat[i][0];
4644 }
4645 }
4646
4647 if (right) {
4648 for (i = 4; i--;) {
4649 right[i] = mat[i][3] - mat[i][0];
4650 }
4651 }
4652
4653 if (bottom) {
4654 for (i = 4; i--;) {
4655 bottom[i] = mat[i][3] + mat[i][1];
4656 }
4657 }
4658
4659 if (top) {
4660 for (i = 4; i--;) {
4661 top[i] = mat[i][3] - mat[i][1];
4662 }
4663 }
4664
4665 if (near) {
4666 for (i = 4; i--;) {
4667 near[i] = mat[i][3] + mat[i][2];
4668 }
4669 }
4670
4671 if (far) {
4672 for (i = 4; i--;) {
4673 far[i] = mat[i][3] - mat[i][2];
4674 }
4675 }
4676}
4677
4678void projmat_dimensions(const float winmat[4][4],
4679 float *r_left,
4680 float *r_right,
4681 float *r_bottom,
4682 float *r_top,
4683 float *r_near,
4684 float *r_far)
4685{
4686 const bool is_persp = winmat[3][3] == 0.0f;
4687 if (is_persp) {
4688 const float near = winmat[3][2] / (winmat[2][2] - 1.0f);
4689 *r_left = near * ((winmat[2][0] - 1.0f) / winmat[0][0]);
4690 *r_right = near * ((winmat[2][0] + 1.0f) / winmat[0][0]);
4691 *r_bottom = near * ((winmat[2][1] - 1.0f) / winmat[1][1]);
4692 *r_top = near * ((winmat[2][1] + 1.0f) / winmat[1][1]);
4693 *r_near = near;
4694 *r_far = winmat[3][2] / (winmat[2][2] + 1.0f);
4695 }
4696 else {
4697 *r_left = (-winmat[3][0] - 1.0f) / winmat[0][0];
4698 *r_right = (-winmat[3][0] + 1.0f) / winmat[0][0];
4699 *r_bottom = (-winmat[3][1] - 1.0f) / winmat[1][1];
4700 *r_top = (-winmat[3][1] + 1.0f) / winmat[1][1];
4701 *r_near = (winmat[3][2] + 1.0f) / winmat[2][2];
4702 *r_far = (winmat[3][2] - 1.0f) / winmat[2][2];
4703 }
4704}
4705
4706void projmat_dimensions_db(const float winmat_fl[4][4],
4707 double *r_left,
4708 double *r_right,
4709 double *r_bottom,
4710 double *r_top,
4711 double *r_near,
4712 double *r_far)
4713{
4714 double winmat[4][4];
4715 copy_m4d_m4(winmat, winmat_fl);
4716
4717 const bool is_persp = winmat[3][3] == 0.0f;
4718 if (is_persp) {
4719 const double near = winmat[3][2] / (winmat[2][2] - 1.0);
4720 *r_left = near * ((winmat[2][0] - 1.0) / winmat[0][0]);
4721 *r_right = near * ((winmat[2][0] + 1.0) / winmat[0][0]);
4722 *r_bottom = near * ((winmat[2][1] - 1.0) / winmat[1][1]);
4723 *r_top = near * ((winmat[2][1] + 1.0) / winmat[1][1]);
4724 *r_near = near;
4725 *r_far = winmat[3][2] / (winmat[2][2] + 1.0);
4726 }
4727 else {
4728 *r_left = (-winmat[3][0] - 1.0) / winmat[0][0];
4729 *r_right = (-winmat[3][0] + 1.0) / winmat[0][0];
4730 *r_bottom = (-winmat[3][1] - 1.0) / winmat[1][1];
4731 *r_top = (-winmat[3][1] + 1.0) / winmat[1][1];
4732 *r_near = (winmat[3][2] + 1.0) / winmat[2][2];
4733 *r_far = (winmat[3][2] - 1.0) / winmat[2][2];
4734 }
4735}
4736
4737void projmat_from_subregion(const float projmat[4][4],
4738 const int win_size[2],
4739 const int x_min,
4740 const int x_max,
4741 const int y_min,
4742 const int y_max,
4743 float r_projmat[4][4])
4744{
4745 float rect_width = float(x_max - x_min);
4746 float rect_height = float(y_max - y_min);
4747
4748 float x_sca = float(win_size[0]) / rect_width;
4749 float y_sca = float(win_size[1]) / rect_height;
4750
4751 float x_fac = float((x_min + x_max) - win_size[0]) / rect_width;
4752 float y_fac = float((y_min + y_max) - win_size[1]) / rect_height;
4753
4754 copy_m4_m4(r_projmat, projmat);
4755 r_projmat[0][0] *= x_sca;
4756 r_projmat[1][1] *= y_sca;
4757
4758 if (projmat[3][3] == 0.0f) {
4759 r_projmat[2][0] = r_projmat[2][0] * x_sca + x_fac;
4760 r_projmat[2][1] = r_projmat[2][1] * y_sca + y_fac;
4761 }
4762 else {
4763 r_projmat[3][0] = r_projmat[3][0] * x_sca - x_fac;
4764 r_projmat[3][1] = r_projmat[3][1] * y_sca - y_fac;
4765 }
4766}
4767
4768static void i_multmatrix(const float icand[4][4], float mat[4][4])
4769{
4770 int row, col;
4771 float temp[4][4];
4772
4773 for (row = 0; row < 4; row++) {
4774 for (col = 0; col < 4; col++) {
4775 temp[row][col] = (icand[row][0] * mat[0][col] + icand[row][1] * mat[1][col] +
4776 icand[row][2] * mat[2][col] + icand[row][3] * mat[3][col]);
4777 }
4778 }
4779 copy_m4_m4(mat, temp);
4780}
4781
4782void polarview_m4(float mat[4][4], float dist, float azimuth, float incidence, float twist)
4783{
4784 unit_m4(mat);
4785
4786 translate_m4(mat, 0.0, 0.0, -dist);
4787 rotate_m4(mat, 'Z', -twist);
4788 rotate_m4(mat, 'X', -incidence);
4789 rotate_m4(mat, 'Z', -azimuth);
4790}
4791
4793 float mat[4][4], float vx, float vy, float vz, float px, float py, float pz, float twist)
4794{
4795 float sine, cosine, hyp, hyp1, dx, dy, dz;
4796 float mat1[4][4];
4797
4798 unit_m4(mat1);
4799
4800 axis_angle_to_mat4_single(mat, 'Z', -twist);
4801
4802 dx = px - vx;
4803 dy = py - vy;
4804 dz = pz - vz;
4805 hyp = dx * dx + dz * dz; /* hyp squared */
4806 hyp1 = sqrtf(dy * dy + hyp);
4807 hyp = sqrtf(hyp); /* the real hyp */
4808
4809 if (hyp1 != 0.0f) { /* rotate X */
4810 sine = -dy / hyp1;
4811 cosine = hyp / hyp1;
4812 }
4813 else {
4814 sine = 0.0f;
4815 cosine = 1.0f;
4816 }
4817 mat1[1][1] = cosine;
4818 mat1[1][2] = sine;
4819 mat1[2][1] = -sine;
4820 mat1[2][2] = cosine;
4821
4822 i_multmatrix(mat1, mat);
4823
4824 /* Be careful here to reinitialize those modified by the last. */
4825 mat1[1][1] = mat1[2][2] = 1.0f;
4826 mat1[1][2] = mat1[2][1] = 0.0f;
4827
4828 /* paragraph */
4829 if (hyp != 0.0f) { /* rotate Y */
4830 sine = dx / hyp;
4831 cosine = -dz / hyp;
4832 }
4833 else {
4834 sine = 0.0f;
4835 cosine = 1.0f;
4836 }
4837 mat1[0][0] = cosine;
4838 mat1[0][2] = -sine;
4839 mat1[2][0] = sine;
4840 mat1[2][2] = cosine;
4841
4842 i_multmatrix(mat1, mat);
4843 translate_m4(mat, -vx, -vy, -vz); /* translate viewpoint to origin */
4844}
4845
4846int box_clip_bounds_m4(float boundbox[2][3], const float bounds[4], float winmat[4][4])
4847{
4848 float mat[4][4], vec[4];
4849 int a, fl, flag = -1;
4850
4851 copy_m4_m4(mat, winmat);
4852
4853 for (a = 0; a < 8; a++) {
4854 vec[0] = (a & 1) ? boundbox[0][0] : boundbox[1][0];
4855 vec[1] = (a & 2) ? boundbox[0][1] : boundbox[1][1];
4856 vec[2] = (a & 4) ? boundbox[0][2] : boundbox[1][2];
4857 vec[3] = 1.0;
4858 mul_m4_v4(mat, vec);
4859
4860 fl = 0;
4861 if (bounds) {
4862 if (vec[0] > bounds[1] * vec[3]) {
4863 fl |= 1;
4864 }
4865 if (vec[0] < bounds[0] * vec[3]) {
4866 fl |= 2;
4867 }
4868 if (vec[1] > bounds[3] * vec[3]) {
4869 fl |= 4;
4870 }
4871 if (vec[1] < bounds[2] * vec[3]) {
4872 fl |= 8;
4873 }
4874 }
4875 else {
4876 if (vec[0] < -vec[3]) {
4877 fl |= 1;
4878 }
4879 if (vec[0] > vec[3]) {
4880 fl |= 2;
4881 }
4882 if (vec[1] < -vec[3]) {
4883 fl |= 4;
4884 }
4885 if (vec[1] > vec[3]) {
4886 fl |= 8;
4887 }
4888 }
4889 if (vec[2] < -vec[3]) {
4890 fl |= 16;
4891 }
4892 if (vec[2] > vec[3]) {
4893 fl |= 32;
4894 }
4895
4896 flag &= fl;
4897 if (flag == 0) {
4898 return 0;
4899 }
4900 }
4901
4902 return flag;
4903}
4904
4905void box_minmax_bounds_m4(float min[3], float max[3], float boundbox[2][3], float mat[4][4])
4906{
4907 float mn[3], mx[3], vec[3];
4908 int a;
4909
4910 copy_v3_v3(mn, min);
4911 copy_v3_v3(mx, max);
4912
4913 for (a = 0; a < 8; a++) {
4914 vec[0] = (a & 1) ? boundbox[0][0] : boundbox[1][0];
4915 vec[1] = (a & 2) ? boundbox[0][1] : boundbox[1][1];
4916 vec[2] = (a & 4) ? boundbox[0][2] : boundbox[1][2];
4917
4918 mul_m4_v3(mat, vec);
4919 minmax_v3v3_v3(mn, mx, vec);
4920 }
4921
4922 copy_v3_v3(min, mn);
4923 copy_v3_v3(max, mx);
4924}
4925
4926/********************************** Mapping **********************************/
4927
4928static float snap_coordinate(float u)
4929{
4930 /* Adjust a coordinate value `u` to obtain a value inside the (closed) unit interval.
4931 * i.e. 0.0 <= snap_coordinate(u) <= 1.0.
4932 * Due to round-off errors, it is possible that the value of `u` may be close to the boundary of
4933 * the unit interval, but not exactly on it. In order to handle these cases, `snap_coordinate`
4934 * checks whether `u` is within `epsilon` of the boundary, and if so, it snaps the return value
4935 * to the boundary. */
4936 if (u < 0.0f) {
4937 u += 1.0f; /* Get back into the unit interval. */
4938 }
4939 BLI_assert(0.0f <= u);
4940 BLI_assert(u <= 1.0f);
4941 const float epsilon = 0.25f / 65536.0f; /* i.e. Quarter of a texel on a 65536 x 65536 texture. */
4942 if (u < epsilon) {
4943 return 0.0f; /* `u` is close to 0, just return 0. */
4944 }
4945 if (1.0f - epsilon < u) {
4946 return 1.0f; /* `u` is close to 1, just return 1. */
4947 }
4948 return u;
4949}
4950
4951bool map_to_tube(float *r_u, float *r_v, const float x, const float y, const float z)
4952{
4953 bool regular = true;
4954 if (x * x + y * y < 1e-6f * 1e-6f) {
4955 regular = false; /* We're too close to the cylinder's axis. */
4956 *r_u = 0.5f;
4957 }
4958 else {
4959 /* The "Regular" case, just compute the coordinate. */
4960 *r_u = snap_coordinate(atan2f(x, -y) / float(2.0f * M_PI));
4961 }
4962 *r_v = (z + 1.0f) / 2.0f;
4963 return regular;
4964}
4965
4966bool map_to_sphere(float *r_u, float *r_v, const float x, const float y, const float z)
4967{
4968 bool regular = true;
4969 const float epsilon = 0.25f / 65536.0f; /* i.e. Quarter of a texel on a 65536 x 65536 texture. */
4970 const float len_xy = sqrtf(x * x + y * y);
4971 if (len_xy <= fabsf(z) * epsilon) {
4972 regular = false; /* We're on the line that runs through the north and south poles. */
4973 *r_u = 0.5f;
4974 }
4975 else {
4976 /* The "Regular" case, just compute the coordinate. */
4977 *r_u = snap_coordinate(atan2f(x, -y) / float(2.0f * M_PI));
4978 }
4979 *r_v = snap_coordinate(atan2f(len_xy, -z) / float(M_PI));
4980 return regular;
4981}
4982
4983void map_to_plane_v2_v3v3(float r_co[2], const float co[3], const float no[3])
4984{
4985 const float target[3] = {0.0f, 0.0f, 1.0f};
4986 float axis[3];
4987
4988 cross_v3_v3v3(axis, no, target);
4989 normalize_v3(axis);
4990
4991 map_to_plane_axis_angle_v2_v3v3fl(r_co, co, axis, angle_normalized_v3v3(no, target));
4992}
4993
4995 const float co[3],
4996 const float axis[3],
4997 const float angle)
4998{
4999 float tmp[3];
5000
5001 rotate_normalized_v3_v3v3fl(tmp, co, axis, angle);
5002
5003 copy_v2_v2(r_co, tmp);
5004}
5005
5006/********************************* Normals **********************************/
5007
5009 float n2[3],
5010 float n3[3],
5011 const float f_no[3],
5012 const float co1[3],
5013 const float co2[3],
5014 const float co3[3])
5015{
5016 float vdiffs[3][3];
5017 const int nverts = 3;
5018
5019 /* compute normalized edge vectors */
5020 sub_v3_v3v3(vdiffs[0], co2, co1);
5021 sub_v3_v3v3(vdiffs[1], co3, co2);
5022 sub_v3_v3v3(vdiffs[2], co1, co3);
5023
5024 normalize_v3(vdiffs[0]);
5025 normalize_v3(vdiffs[1]);
5026 normalize_v3(vdiffs[2]);
5027
5028 /* accumulate angle weighted face normal */
5029 {
5030 float *vn[] = {n1, n2, n3};
5031 const float *prev_edge = vdiffs[nverts - 1];
5032 int i;
5033
5034 for (i = 0; i < nverts; i++) {
5035 const float *cur_edge = vdiffs[i];
5036 const float fac = blender::math::safe_acos_approx(-dot_v3v3(cur_edge, prev_edge));
5037
5038 /* accumulate */
5039 madd_v3_v3fl(vn[i], f_no, fac);
5040 prev_edge = cur_edge;
5041 }
5042 }
5043}
5044
5046 float n2[3],
5047 float n3[3],
5048 float n4[3],
5049 const float f_no[3],
5050 const float co1[3],
5051 const float co2[3],
5052 const float co3[3],
5053 const float co4[3])
5054{
5055 float vdiffs[4][3];
5056 const int nverts = (n4 != nullptr && co4 != nullptr) ? 4 : 3;
5057
5058 /* compute normalized edge vectors */
5059 sub_v3_v3v3(vdiffs[0], co2, co1);
5060 sub_v3_v3v3(vdiffs[1], co3, co2);
5061
5062 if (nverts == 3) {
5063 sub_v3_v3v3(vdiffs[2], co1, co3);
5064 }
5065 else {
5066 sub_v3_v3v3(vdiffs[2], co4, co3);
5067 sub_v3_v3v3(vdiffs[3], co1, co4);
5068 normalize_v3(vdiffs[3]);
5069 }
5070
5071 normalize_v3(vdiffs[0]);
5072 normalize_v3(vdiffs[1]);
5073 normalize_v3(vdiffs[2]);
5074
5075 /* accumulate angle weighted face normal */
5076 {
5077 float *vn[] = {n1, n2, n3, n4};
5078 const float *prev_edge = vdiffs[nverts - 1];
5079 int i;
5080
5081 for (i = 0; i < nverts; i++) {
5082 const float *cur_edge = vdiffs[i];
5083 const float fac = blender::math::safe_acos_approx(-dot_v3v3(cur_edge, prev_edge));
5084
5085 /* accumulate */
5086 madd_v3_v3fl(vn[i], f_no, fac);
5087 prev_edge = cur_edge;
5088 }
5089 }
5090}
5091
5093 const float polyno[3],
5094 const float **vertcos,
5095 float vdiffs[][3],
5096 const int nverts)
5097{
5098 int i;
5099
5100 /* calculate normalized edge directions for each edge in the poly */
5101 for (i = 0; i < nverts; i++) {
5102 sub_v3_v3v3(vdiffs[i], vertcos[(i + 1) % nverts], vertcos[i]);
5103 normalize_v3(vdiffs[i]);
5104 }
5105
5106 /* accumulate angle weighted face normal */
5107 {
5108 const float *prev_edge = vdiffs[nverts - 1];
5109
5110 for (i = 0; i < nverts; i++) {
5111 const float *cur_edge = vdiffs[i];
5112
5113 /* calculate angle between the two poly edges incident on
5114 * this vertex */
5115 const float fac = blender::math::safe_acos_approx(-dot_v3v3(cur_edge, prev_edge));
5116
5117 /* accumulate */
5118 madd_v3_v3fl(vertnos[i], polyno, fac);
5119 prev_edge = cur_edge;
5120 }
5121 }
5122}
5123
5124/********************************* Tangents **********************************/
5125
5126void tangent_from_uv_v3(const float uv1[2],
5127 const float uv2[2],
5128 const float uv3[2],
5129 const float co1[3],
5130 const float co2[3],
5131 const float co3[3],
5132 const float n[3],
5133 float r_tang[3])
5134{
5135 const float s1 = uv2[0] - uv1[0];
5136 const float s2 = uv3[0] - uv1[0];
5137 const float t1 = uv2[1] - uv1[1];
5138 const float t2 = uv3[1] - uv1[1];
5139 float det = (s1 * t2 - s2 * t1);
5140
5141 /* otherwise 'r_tang' becomes nan */
5142 if (det != 0.0f) {
5143 float tangv[3], ct[3], e1[3], e2[3];
5144
5145 det = 1.0f / det;
5146
5147 /* normals in render are inversed... */
5148 sub_v3_v3v3(e1, co1, co2);
5149 sub_v3_v3v3(e2, co1, co3);
5150 r_tang[0] = (t2 * e1[0] - t1 * e2[0]) * det;
5151 r_tang[1] = (t2 * e1[1] - t1 * e2[1]) * det;
5152 r_tang[2] = (t2 * e1[2] - t1 * e2[2]) * det;
5153 tangv[0] = (s1 * e2[0] - s2 * e1[0]) * det;
5154 tangv[1] = (s1 * e2[1] - s2 * e1[1]) * det;
5155 tangv[2] = (s1 * e2[2] - s2 * e1[2]) * det;
5156 cross_v3_v3v3(ct, r_tang, tangv);
5157
5158 /* check flip */
5159 if (dot_v3v3(ct, n) < 0.0f) {
5160 negate_v3(r_tang);
5161 }
5162 }
5163 else {
5164 zero_v3(r_tang);
5165 }
5166}
5167
5168/****************************** Vector Clouds ********************************/
5169
5170/* vector clouds */
5171
5172void vcloud_estimate_transform_v3(const int list_size,
5173 const float (*pos)[3],
5174 const float *weight,
5175 const float (*rpos)[3],
5176 const float *rweight,
5177 float lloc[3],
5178 float rloc[3],
5179 float lrot[3][3],
5180 float lscale[3][3])
5181{
5182 float accu_com[3] = {0.0f, 0.0f, 0.0f}, accu_rcom[3] = {0.0f, 0.0f, 0.0f};
5183 float accu_weight = 0.0f, accu_rweight = 0.0f;
5184 const float eps = 1e-6f;
5185
5186 int a;
5187 /* first set up a nice default response */
5188 if (lloc) {
5189 zero_v3(lloc);
5190 }
5191 if (rloc) {
5192 zero_v3(rloc);
5193 }
5194 if (lrot) {
5195 unit_m3(lrot);
5196 }
5197 if (lscale) {
5198 unit_m3(lscale);
5199 }
5200 /* do com for both clouds */
5201 if (pos && rpos && (list_size > 0)) { /* paranoia check */
5202 /* do com for both clouds */
5203 for (a = 0; a < list_size; a++) {
5204 if (weight) {
5205 float v[3];
5206 copy_v3_v3(v, pos[a]);
5207 mul_v3_fl(v, weight[a]);
5208 add_v3_v3(accu_com, v);
5209 accu_weight += weight[a];
5210 }
5211 else {
5212 add_v3_v3(accu_com, pos[a]);
5213 }
5214
5215 if (rweight) {
5216 float v[3];
5217 copy_v3_v3(v, rpos[a]);
5218 mul_v3_fl(v, rweight[a]);
5219 add_v3_v3(accu_rcom, v);
5220 accu_rweight += rweight[a];
5221 }
5222 else {
5223 add_v3_v3(accu_rcom, rpos[a]);
5224 }
5225 }
5226 if (!weight || !rweight) {
5227 accu_weight = accu_rweight = float(list_size);
5228 }
5229
5230 mul_v3_fl(accu_com, 1.0f / accu_weight);
5231 mul_v3_fl(accu_rcom, 1.0f / accu_rweight);
5232 if (lloc) {
5233 copy_v3_v3(lloc, accu_com);
5234 }
5235 if (rloc) {
5236 copy_v3_v3(rloc, accu_rcom);
5237 }
5238 if (lrot || lscale) { /* caller does not want rot nor scale, strange but legal */
5239 /* so now do some reverse engineering and see if we can
5240 * split rotation from scale -> Polar-decompose. */
5241 /* build 'projection' matrix */
5242 float m[3][3], mr[3][3], q[3][3], qi[3][3];
5243 float va[3], vb[3], stunt[3];
5244 float odet, ndet;
5245 int i = 0, imax = 15;
5246 zero_m3(m);
5247 zero_m3(mr);
5248
5249 /* build 'projection' matrix */
5250 for (a = 0; a < list_size; a++) {
5251 sub_v3_v3v3(va, rpos[a], accu_rcom);
5252 // mul_v3_fl(va, bp->mass); /* Mass needs re-normalization here? */
5253 sub_v3_v3v3(vb, pos[a], accu_com);
5254 // mul_v3_fl(va, rp->mass);
5255 m[0][0] += va[0] * vb[0];
5256 m[0][1] += va[0] * vb[1];
5257 m[0][2] += va[0] * vb[2];
5258
5259 m[1][0] += va[1] * vb[0];
5260 m[1][1] += va[1] * vb[1];
5261 m[1][2] += va[1] * vb[2];
5262
5263 m[2][0] += va[2] * vb[0];
5264 m[2][1] += va[2] * vb[1];
5265 m[2][2] += va[2] * vb[2];
5266
5267 /* building the reference matrix on the fly
5268 * needed to scale properly later */
5269
5270 mr[0][0] += va[0] * va[0];
5271 mr[0][1] += va[0] * va[1];
5272 mr[0][2] += va[0] * va[2];
5273
5274 mr[1][0] += va[1] * va[0];
5275 mr[1][1] += va[1] * va[1];
5276 mr[1][2] += va[1] * va[2];
5277
5278 mr[2][0] += va[2] * va[0];
5279 mr[2][1] += va[2] * va[1];
5280 mr[2][2] += va[2] * va[2];
5281 }
5282 copy_m3_m3(q, m);
5283 stunt[0] = q[0][0];
5284 stunt[1] = q[1][1];
5285 stunt[2] = q[2][2];
5286 /* Re-normalizing for numeric stability. */
5287 mul_m3_fl(q, 1.0f / len_v3(stunt));
5288
5289 /* This is pretty much Polar-decompose 'inline' the algorithm based on Higham's thesis
5290 * without the far case ... but seems to work here pretty neat. */
5291 odet = 0.0f;
5292 ndet = determinant_m3_array(q);
5293 while ((odet - ndet) * (odet - ndet) > eps && i < imax) {
5294 invert_m3_m3(qi, q);
5295 transpose_m3(qi);
5296 add_m3_m3m3(q, q, qi);
5297 mul_m3_fl(q, 0.5f);
5298 odet = ndet;
5299 ndet = determinant_m3_array(q);
5300 i++;
5301 }
5302
5303 if (i) {
5304 float scale[3][3];
5305 float irot[3][3];
5306 if (lrot) {
5307 copy_m3_m3(lrot, q);
5308 }
5309 invert_m3_m3(irot, q);
5310 invert_m3_m3(qi, mr);
5311 mul_m3_m3m3(q, m, qi);
5312 mul_m3_m3m3(scale, irot, q);
5313 if (lscale) {
5314 copy_m3_m3(lscale, scale);
5315 }
5316 }
5317 }
5318 }
5319}
5320
5321bool is_edge_convex_v3(const float v1[3],
5322 const float v2[3],
5323 const float f1_no[3],
5324 const float f2_no[3])
5325{
5326 if (!equals_v3v3(f1_no, f2_no)) {
5327 float cross[3];
5328 float l_dir[3];
5329 cross_v3_v3v3(cross, f1_no, f2_no);
5330 /* we assume contiguous normals, otherwise the result isn't meaningful */
5331 sub_v3_v3v3(l_dir, v2, v1);
5332 return (dot_v3v3(l_dir, cross) > 0.0f);
5333 }
5334 return false;
5335}
5336
5337bool is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
5338{
5347
5348 /* non-unit length normal, used as a projection plane */
5349 float plane[3];
5350
5351 {
5352 float v13[3], v24[3];
5353
5354 sub_v3_v3v3(v13, v1, v3);
5355 sub_v3_v3v3(v24, v2, v4);
5356
5357 cross_v3_v3v3(plane, v13, v24);
5358
5359 /* Ignore planes that are small or (near) zero area,
5360 * scale the threshold down as the length of the cross product is also squared.
5361 * With this value quads with edges smaller than 1e-05 may be detected as too small. */
5362 const float eps_sq = square_f(1e-8f);
5363 if (len_squared_v3(plane) < eps_sq) {
5364 return false;
5365 }
5366 }
5367
5368 const float *quad_coords[4] = {v1, v2, v3, v4};
5369 float quad_proj[4][3];
5370
5371 for (int i = 0; i < 4; i++) {
5372 project_plane_v3_v3v3(quad_proj[i], quad_coords[i], plane);
5373 }
5374
5375 float quad_dirs[4][3];
5376 for (int i = 0, j = 3; i < 4; j = i++) {
5377 sub_v3_v3v3(quad_dirs[i], quad_proj[i], quad_proj[j]);
5378 }
5379
5380 float test_dir[3];
5381
5382#define CROSS_SIGN(dir_a, dir_b) \
5383 ((void)cross_v3_v3v3(test_dir, dir_a, dir_b), (dot_v3v3(plane, test_dir) > 0.0f))
5384
5385 return (CROSS_SIGN(quad_dirs[0], quad_dirs[1]) && CROSS_SIGN(quad_dirs[1], quad_dirs[2]) &&
5386 CROSS_SIGN(quad_dirs[2], quad_dirs[3]) && CROSS_SIGN(quad_dirs[3], quad_dirs[0]));
5387
5388#undef CROSS_SIGN
5389}
5390
5391bool is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
5392{
5393 /* Line-tests, the 2 diagonals have to intersect to be convex. */
5394 return (isect_seg_seg_v2(v1, v3, v2, v4) > 0);
5395}
5396
5397bool is_poly_convex_v2(const float verts[][2], uint nr)
5398{
5399 uint sign_flag = 0;
5400 uint a;
5401 const float *co_curr, *co_prev;
5402 float dir_curr[2], dir_prev[2];
5403
5404 co_prev = verts[nr - 1];
5405 co_curr = verts[0];
5406
5407 sub_v2_v2v2(dir_prev, verts[nr - 2], co_prev);
5408
5409 for (a = 0; a < nr; a++) {
5410 float cross;
5411
5412 sub_v2_v2v2(dir_curr, co_prev, co_curr);
5413
5414 cross = cross_v2v2(dir_prev, dir_curr);
5415
5416 if (cross < 0.0f) {
5417 sign_flag |= 1;
5418 }
5419 else if (cross > 0.0f) {
5420 sign_flag |= 2;
5421 }
5422
5423 if (sign_flag == (1 | 2)) {
5424 return false;
5425 }
5426
5427 copy_v2_v2(dir_prev, dir_curr);
5428
5429 co_prev = co_curr;
5430 co_curr += 2;
5431 }
5432
5433 return true;
5434}
5435
5436int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
5437{
5438 float d_12[3], d_23[3], d_34[3], d_41[3];
5439 float cross_a[3], cross_b[3];
5440 int ret = 0;
5441
5442 sub_v3_v3v3(d_12, v1, v2);
5443 sub_v3_v3v3(d_23, v2, v3);
5444 sub_v3_v3v3(d_34, v3, v4);
5445 sub_v3_v3v3(d_41, v4, v1);
5446
5447 cross_v3_v3v3(cross_a, d_12, d_23);
5448 cross_v3_v3v3(cross_b, d_34, d_41);
5449 ret |= ((dot_v3v3(cross_a, cross_b) < 0.0f) << 0);
5450
5451 cross_v3_v3v3(cross_a, d_23, d_34);
5452 cross_v3_v3v3(cross_b, d_41, d_12);
5453 ret |= ((dot_v3v3(cross_a, cross_b) < 0.0f) << 1);
5454
5455 return ret;
5456}
5457
5459 const float v2[3],
5460 const float v3[3],
5461 const float v4[3])
5462{
5463 /* NOTE: if the faces normal has been calculated it's possible to simplify the following checks,
5464 * however this means the solution may be different depending on the existence of normals
5465 * causing tessellation to be "unstable" depending on the existence of normals, see #106469. */
5466 float d_12[3], d_13[3], d_14[3];
5467 float cross_a[3], cross_b[3];
5468 sub_v3_v3v3(d_12, v2, v1);
5469 sub_v3_v3v3(d_13, v3, v1);
5470 sub_v3_v3v3(d_14, v4, v1);
5471 cross_v3_v3v3(cross_a, d_12, d_13);
5472 cross_v3_v3v3(cross_b, d_14, d_13);
5473 return dot_v3v3(cross_a, cross_b) > 0.0f;
5474}
5475
5476float cubic_tangent_factor_circle_v3(const float tan_l[3], const float tan_r[3])
5477{
5478 BLI_ASSERT_UNIT_V3(tan_l);
5479 BLI_ASSERT_UNIT_V3(tan_r);
5480
5481 /* -7f causes instability/glitches with Bendy Bones + Custom Refs. */
5482 const float eps = 1e-5f;
5483
5484 const float tan_dot = dot_v3v3(tan_l, tan_r);
5485 if (tan_dot > 1.0f - eps) {
5486 /* no angle difference (use fallback, length won't make any difference) */
5487 return (1.0f / 3.0f) * 0.75f;
5488 }
5489 if (tan_dot < -1.0f + eps) {
5490 /* Parallel tangents (half-circle). */
5491 return (1.0f / 2.0f);
5492 }
5493
5494 /* non-aligned tangents, calculate handle length */
5495 const float angle = acosf(tan_dot) / 2.0f;
5496
5497 /* could also use 'angle_sin = len_vnvn(tan_l, tan_r, dims) / 2.0' */
5498 const float angle_sin = sinf(angle);
5499 const float angle_cos = cosf(angle);
5500 return ((1.0f - angle_cos) / (angle_sin * 2.0f)) / angle_sin;
5501}
5502
5504 const float v0[3], const float v1[3], const float v2[3], const float dist1, const float dist2)
5505{
5506 /* Vectors along triangle edges. */
5507 float v10[3], v12[3];
5508 sub_v3_v3v3(v10, v0, v1);
5509 sub_v3_v3v3(v12, v2, v1);
5510
5511 if (dist1 != 0.0f && dist2 != 0.0f) {
5512 /* Local coordinate system in the triangle plane. */
5513 float u[3], v[3], n[3];
5514 const float d12 = normalize_v3_v3(u, v12);
5515
5516 if (d12 * d12 > 0.0f) {
5517 cross_v3_v3v3(n, v12, v10);
5518 normalize_v3(n);
5519 cross_v3_v3v3(v, n, u);
5520
5521 /* v0 in local coordinates */
5522 const float v0_[2] = {dot_v3v3(v10, u), fabsf(dot_v3v3(v10, v))};
5523
5524 /* Compute virtual source point in local coordinates, that we estimate the geodesic
5525 * distance is being computed from. See figure 9 in the paper for the derivation. */
5526 const float a = 0.5f * (1.0f + (dist1 * dist1 - dist2 * dist2) / (d12 * d12));
5527 const float hh = dist1 * dist1 - a * a * d12 * d12;
5528
5529 if (hh > 0.0f) {
5530 const float h = sqrtf(hh);
5531 const float S_[2] = {a * d12, -h};
5532
5533 /* Only valid if the line between the source point and v0 crosses
5534 * the edge between v1 and v2. */
5535 const float x_intercept = S_[0] + h * (v0_[0] - S_[0]) / (v0_[1] + h);
5536 if (x_intercept >= 0.0f && x_intercept <= d12) {
5537 return len_v2v2(S_, v0_);
5538 }
5539 }
5540 }
5541 }
5542
5543 /* Fall back to Dijsktra approximation in trivial case, or if no valid source
5544 * point found that connects to v0 across the triangle. */
5545 return min_ff(dist1 + len_v3(v10), dist2 + len_v3v3(v0, v2));
5546}
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float max_ff(float a, float b)
MINLINE float min_ffff(float a, float b, float c, float d)
#define BLI_ASSERT_UNIT_EPSILON
MINLINE float min_ff(float a, float b)
MINLINE float square_f(float a)
#define BLI_ASSERT_UNIT_V3(v)
MINLINE int float_as_int(float f)
MINLINE float xor_fl(float x, int y)
#define M_PI
bool isect_ray_tri_threshold_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2], float threshold)
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
#define ISECT_AABB_PLANE_IN_FRONT_ALL
MINLINE int axis_dominant_v3_single(const float vec[3])
MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
#define ISECT_LINE_LINE_NONE
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
#define ISECT_AABB_PLANE_CROSS_ANY
#define ISECT_AABB_PLANE_BEHIND_ANY
#define ISECT_LINE_LINE_COLINEAR
#define ISECT_LINE_LINE_EXACT
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
#define ISECT_LINE_LINE_CROSS
bool is_negative_m3(const float mat[3][3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void unit_m3(float m[3][3])
void transpose_m4_m4(float R[4][4], const float M[4][4])
void mul_m3_fl(float R[3][3], float f)
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void add_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void zero_m3(float m[3][3])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
void copy_m4d_m4(double m1[4][4], const float m2[4][4])
void mul_m4_v3(const float M[4][4], float r[3])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
float determinant_m3_array(const float m[3][3])
void mul_m4_v4(const float mat[4][4], float r[4])
void rotate_m4(float mat[4][4], char axis, float angle)
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void transpose_m3(float R[3][3])
float determinant_m3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3)
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void unit_m4(float m[4][4])
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
MINLINE void sub_v3db_v3fl_v3fl(double r[3], const float a[3], const float b[3])
MINLINE void mul_v4_fl(float r[4], float f)
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE double dot_v2v2_db(const double a[2], const double b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
MINLINE bool is_finite_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE double dot_v3db_v3fl(const double a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void project_v3_v3v3_normalized(float out[3], const float p[3], const float v_proj[3])
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void negate_v3(float r[3])
MINLINE float dot_m3_v3_row_z(const float M[3][3], const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v4(float r[4])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float line_point_side_v2(const float l1[2], const float l2[2], const float pt[2]) ATTR_WARN_UNUSED_RESULT
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v2(float r[2])
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3_db(double r[3], const double a[3], const double b[3])
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void ortho_basis_v3v3_v3(float r_n1[3], float r_n2[3], const float n[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_m4_v3_row_x(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void copy_v4_fl(float r[4], float f)
MINLINE bool is_finite_v4(const float v[4]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_m4_v3_row_y(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float n[3])
MINLINE double cross_v2v2_db(const double a[2], const double b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v2_v2v2_db(double r[2], const double a[2], const double b[2])
unsigned int uint
#define CLAMP(a, b, c)
#define UNUSED_VARS(...)
#define IN_RANGE(a, b, c)
#define UNPACK3(a)
#define UNLIKELY(x)
#define ELEM(...)
#define IN_RANGE_INCL(a, b, c)
#define LIKELY(x)
int rect_width(int rect[2][2])
Definition Basic.c:43
int rect_height(int rect[2][2])
Definition Basic.c:47
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
bool closest(btVector3 &v)
nullptr float
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define acosf(x)
#define copysignf(x, y)
static float verts[][3]
uint pos
uint nor
uint col
uint top
float determinant(MatBase< C, R >) RET
#define sqrt
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float length(VecOp< float, D >) RET
ccl_device_inline float2 fabs(const float2 a)
float area_poly_v2(const float verts[][2], uint nr)
Definition math_geom.cc:182
int isect_seg_seg_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
bool isect_point_planes_v3(const float(*planes)[4], int totplane, const float p[3])
static double mean_value_half_tan_v2_db(const Double2_Len *d_curr, const Double2_Len *d_next)
bool map_to_sphere(float *r_u, float *r_v, const float x, const float y, const float z)
bool isect_ray_aabb_v3(const IsectRayAABB_Precalc *data, const float bb_min[3], const float bb_max[3], float *r_tmin)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:217
bool isect_ray_ray_v3(const float ray_origin_a[3], const float ray_direction_a[3], const float ray_origin_b[3], const float ray_direction_b[3], float *r_lambda_a, float *r_lambda_b)
#define IS_ZERO(x)
float dist_signed_to_plane3_v3(const float p[3], const float plane[3])
Definition math_geom.cc:507
int isect_point_quad_v2(const float p[2], const float v1[2], const float v2[2], const float v3[2], const float v4[2])
float cross_poly_v2(const float verts[][2], uint nr)
Definition math_geom.cc:149
float closest_to_ray_v3(float r_close[3], const float p[3], const float ray_orig[3], const float ray_dir[3])
void window_translate_m4(float winmat[4][4], float perspmat[4][4], const float x, const float y)
float closest_seg_seg_v2(float r_closest_a[2], float r_closest_b[2], float *r_lambda_a, float *r_lambda_b, const float a1[2], const float a2[2], const float b1[2], const float b2[2])
Definition math_geom.cc:305
float dist_squared_to_line_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:533
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:519
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:58
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])
float line_point_factor_v2_ex(const float p[2], const float l1[2], const float l2[2], const float epsilon, const float fallback)
static bool getLowestRoot(const float a, const float b, const float c, const float maxR, float *root)
float dist_squared_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:470
bool isect_ray_line_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float *r_lambda)
int barycentric_inside_triangle_v2(const float w[3])
int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
float dist_squared_to_plane3_v3(const float p[3], const float plane[3])
Definition math_geom.cc:486
bool is_poly_convex_v2(const float verts[][2], uint nr)
void transform_point_by_seg_v3(float p_dst[3], const float p_src[3], const float l_dst_p1[3], const float l_dst_p2[3], const float l_src_p1[3], const float l_src_p2[3])
bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const float radius, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float ipoint[3])
float area_poly_v3(const float verts[][3], uint nr)
Definition math_geom.cc:133
bool isect_ray_tri_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
float dist_to_plane3_v3(const float p[3], const float plane[3])
Definition math_geom.cc:514
void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
void accumulate_vertex_normals_tri_v3(float n1[3], float n2[3], float n3[3], const float f_no[3], const float co1[3], const float co2[3], const float co3[3])
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:387
float ray_point_factor_v3(const float p[3], const float ray_origin[3], const float ray_direction[3])
void barycentric_weights_v2_clamped(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
#define IS_POINT_IX
void isect_seg_seg_v3(const float a0[3], const float a1[3], const float b0[3], const float b1[3], float r_a[3], float r_b[3])
double closest_to_line_v2_db(double r_close[2], const double p[2], const double l1[2], const double l2[2])
bool isect_line_segment_tri_epsilon_v3(const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2], const float epsilon)
void cross_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:26
bool point_in_slice_seg(float p[3], float l1[3], float l2[3])
static float mean_value_half_tan_v3(const Float3_Len *d_curr, const Float3_Len *d_next)
float dist_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:502
bool isect_tri_tri_v2(const float t_a0[2], const float t_a1[2], const float t_a2[2], const float t_b0[2], const float t_b1[2], const float t_b2[2])
void transform_point_by_tri_v3(float pt_tar[3], float const pt_src[3], const float tri_tar_p1[3], const float tri_tar_p2[3], const float tri_tar_p3[3], const float tri_src_p1[3], const float tri_src_p2[3], const float tri_src_p3[3])
void polarview_m4(float mat[4][4], float dist, float azimuth, float incidence, float twist)
int isect_seg_seg_v2_lambda_mu_db(const double v1[2], const double v2[2], const double v3[2], const double v4[2], double *r_lambda, double *r_mu)
void interp_weights_quad_v3(float w[4], const float v1[3], const float v2[3], const float v3[3], const float v4[3], const float co[3])
float area_tri_signed_v3(const float v1[3], const float v2[3], const float v3[3], const float normal[3])
Definition math_geom.cc:115
float area_poly_signed_v2(const float verts[][2], uint nr)
Definition math_geom.cc:187
float volume_tri_tetrahedron_signed_v3_6x(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:263
void closest_to_plane3_normalized_v3(float r_close[3], const float plane[3], const float pt[3])
Definition math_geom.cc:456
bool isect_seg_seg_v2_simple(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
#define DIR_V3_SET(d_len, va, vb)
void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:442
int isect_point_tri_v2_int(const int x1, const int y1, const int x2, const int y2, const int a, const int b)
bool isect_ray_plane_v3_factor(const float ray_origin[3], const float ray_direction[3], const float plane_co[3], const float plane_no[3], float *r_lambda)
bool isect_point_tri_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], float r_isect_co[3])
void interp_weights_tri_v3(float w[3], const float v1[3], const float v2[3], const float v3[3], const float co[3])
void resolve_tri_uv_v2(float r_uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2])
float closest_ray_to_segment_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_close[3])
Definition math_geom.cc:409
float line_point_factor_v3_ex(const float p[3], const float l1[3], const float l2[3], const float epsilon, const float fallback)
void cross_poly_v3(float n[3], const float verts[][3], uint nr)
Definition math_geom.cc:168
bool isect_ray_aabb_v3_simple(const float orig[3], const float dir[3], const float bb_min[3], const float bb_max[3], float *tmin, float *tmax)
float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:528
bool isect_aabb_aabb_v3(const float min1[3], const float max1[3], const float min2[3], const float max2[3])
void vcloud_estimate_transform_v3(const int list_size, const float(*pos)[3], const float *weight, const float(*rpos)[3], const float *rweight, float lloc[3], float rloc[3], float lrot[3][3], float lscale[3][3])
static void i_multmatrix(const float icand[4][4], float mat[4][4])
void projmat_from_subregion(const float projmat[4][4], const int win_size[2], const int x_min, const int x_max, const int y_min, const int y_max, float r_projmat[4][4])
float volume_tri_tetrahedron_signed_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:271
#define CCW(A, B, C)
bool isect_line_plane_v3(float r_isect_co[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3])
bool isect_ray_seg_v2(const float ray_origin[2], const float ray_direction[2], const float v0[2], const float v1[2], float *r_lambda, float *r_u)
float dist_squared_to_projected_aabb_simple(const float projmat[4][4], const float winsize[2], const float mval[2], const float bbmin[3], const float bbmax[3])
Definition math_geom.cc:988
void accumulate_vertex_normals_poly_v3(float **vertnos, const float polyno[3], const float **vertcos, float vdiffs[][3], const int nverts)
float normal_poly_v3(float n[3], const float verts[][3], uint nr)
Definition math_geom.cc:79
float dist_squared_ray_to_aabb_v3_simple(const float ray_origin[3], const float ray_direction[3], const float bb_min[3], const float bb_max[3], float r_point[3], float *r_depth)
Definition math_geom.cc:789
static bool point_in_slice(const float p[3], const float v1[3], const float l1[3], const float l2[3])
bool is_quad_flip_v3_first_third_fast(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
#define DIR_V2_SET(d_len, va, vb)
#define MEAN_VALUE_HALF_TAN_V2(_area, i1, i2)
bool clip_segment_v3_plane(const float p1[3], const float p2[3], const float plane[4], float r_p1[3], float r_p2[3])
void closest_to_plane3_v3(float r_close[3], const float plane[3], const float pt[3])
Definition math_geom.cc:449
bool isect_plane_plane_plane_v3(const float plane_a[4], const float plane_b[4], const float plane_c[4], float r_isect_co[3])
void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[2])
void resolve_quad_uv_v2(float r_uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2], const float st3[2])
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
void projmat_dimensions_db(const float winmat_fl[4][4], double *r_left, double *r_right, double *r_bottom, double *r_top, double *r_near, double *r_far)
bool isect_ray_tri_watertight_v3_simple(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
void tangent_from_uv_v3(const float uv1[2], const float uv2[2], const float uv3[2], const float co1[3], const float co2[3], const float co3[3], const float n[3], float r_tang[3])
bool map_to_tube(float *r_u, float *r_v, const float x, const float y, const float z)
void perspective_m4(float mat[4][4], const float left, const float right, const float bottom, const float top, const float nearClip, const float farClip)
static bool isect_tri_tri_v2_impl_vert(const float t_a0[2], const float t_a1[2], const float t_a2[2], const float t_b0[2], const float t_b1[2], const float t_b2[2])
void lookat_m4(float mat[4][4], float vx, float vy, float vz, float px, float py, float pz, float twist)
void axis_dominant_v3_to_m3_negate(float r_mat[3][3], const float normal[3])
static bool barycentric_weights(const float v1[3], const float v2[3], const float v3[3], const float co[3], const float n[3], float w[3])
void accumulate_vertex_normals_v3(float n1[3], float n2[3], float n3[3], float n4[3], const float f_no[3], const float co1[3], const float co2[3], const float co3[3], const float co4[3])
bool isect_tri_tri_v3(const float t_a0[3], const float t_a1[3], const float t_a2[3], const float t_b0[3], const float t_b1[3], const float t_b2[3], float r_i1[3], float r_i2[3])
float line_plane_factor_v3(const float plane_co[3], const float plane_no[3], const float l1[3], const float l2[3])
static int isect_tri_tri_impl_ccw_v2(const float t_a0[2], const float t_a1[2], const float t_a2[2], const float t_b0[2], const float t_b1[2], const float t_b2[2])
#define IS_SEGMENT_IX
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:495
void aabb_get_near_far_from_plane(const float plane_no[3], const float bbmin[3], const float bbmax[3], float bb_near[3], float bb_afar[3])
Definition math_geom.cc:649
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void isect_ray_aabb_v3_precalc(IsectRayAABB_Precalc *data, const float ray_origin[3], const float ray_direction[3])
float dist_seg_seg_v2(const float a1[3], const float a2[3], const float b1[3], const float b2[3])
void resolve_tri_uv_v3(float r_uv[2], const float st[3], const float st0[3], const float st1[3], const float st2[3])
bool isect_point_poly_v2_int(const int pt[2], const int verts[][2], const uint nr)
bool isect_point_poly_v2(const float pt[2], const float verts[][2], const uint nr)
bool isect_plane_plane_v3(const float plane_a[4], const float plane_b[4], float r_isect_co[3], float r_isect_no[3])
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:100
bool isect_point_tri_v2_cw(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
DistRayAABB_Precalc dist_squared_ray_to_aabb_v3_precalc(const float ray_origin[3], const float ray_direction[3])
Definition math_geom.cc:685
int isect_line_sphere_v3(const float l1[3], const float l2[3], const float sp[3], const float r, float r_p1[3], float r_p2[3])
bool is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
void dist_squared_to_projected_aabb_precalc(DistProjectedAABBPrecalc *precalc, const float projmat[4][4], const float winsize[2], const float mval[2])
Definition math_geom.cc:806
bool is_edge_convex_v3(const float v1[3], const float v2[3], const float f1_no[3], const float f2_no[3])
float resolve_quad_u_v2(const float st[2], const float st0[2], const float st1[2], const float st2[2], const float st3[2])
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
void interp_barycentric_tri_v3(float data[3][3], float u, float v, float res[3])
float volume_tetrahedron_signed_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:251
void perspective_m4_fov(float mat[4][4], const float angle_left, const float angle_right, const float angle_up, const float angle_down, const float nearClip, const float farClip)
float closest_to_line_segment_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:365
static bool point_in_slice_as(const float p[3], const float origin[3], const float normal[3])
bool isect_line_line_strict_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float vi[3], float *r_lambda)
float dist_squared_to_projected_aabb(DistProjectedAABBPrecalc *data, const float bbmin[3], const float bbmax[3], bool r_axis_closest[3])
Definition math_geom.cc:858
void interp_cubic_v3(float x[3], float v[3], const float x1[3], const float v1[3], const float x2[3], const float v2[3], const float t)
static bool isect_tri_tri_v2_impl_edge(const float t_a0[2], const float t_a1[2], const float t_a2[2], const float t_b0[2], const float t_b1[2], const float t_b2[2])
float area_squared_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:107
bool isect_tri_tri_v3_ex(const float tri_a[3][3], const float tri_b[3][3], float r_i1[3], float r_i2[3], int *r_tri_a_edge_isect_count)
float dist_squared_ray_to_seg_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_point[3], float *r_depth)
Definition math_geom.cc:613
float area_squared_poly_v3(const float verts[][3], uint nr)
Definition math_geom.cc:140
void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const float v3[2], const float v4[2], const float co[2], float w[4])
int isect_line_sphere_v2(const float l1[2], const float l2[2], const float sp[2], const float r, float r_p1[2], float r_p2[2])
float geodesic_distance_propagate_across_triangle(const float v0[3], const float v1[3], const float v2[3], const float dist1, const float dist2)
int box_clip_bounds_m4(float boundbox[2][3], const float bounds[4], float winmat[4][4])
bool isect_planes_v3_fn(const float planes[][4], const int planes_len, const float eps_coplanar, const float eps_isect, void(*callback_fn)(const float co[3], int i, int j, int k, void *user_data), void *user_data)
float cubic_tangent_factor_circle_v3(const float tan_l[3], const float tan_r[3])
float dist_squared_ray_to_aabb_v3(const DistRayAABB_Precalc *data, const float bb_min[3], const float bb_max[3], float r_point[3], float *r_depth)
Definition math_geom.cc:700
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
float dist_squared_to_ray_v3_normalized(const float ray_origin[3], const float ray_direction[3], const float co[3])
Definition math_geom.cc:597
float area_squared_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:91
void limit_dist_v3(float v1[3], float v2[3], const float dist)
bool barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
bool clip_segment_v3_plane_n(const float p1[3], const float p2[3], const float plane_array[][4], const int plane_num, float r_p1[3], float r_p2[3])
int isect_line_line_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:435
void plane_to_point_vector_v3(const float plane[4], float r_plane_co[3], float r_plane_no[3])
Definition math_geom.cc:223
int isect_seg_seg_v2_point_ex(const float v0[2], const float v1[2], const float v2[2], const float v3[2], const float endpoint_bias, float r_vi[2])
float closest_to_line_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
void isect_ray_tri_watertight_v3_precalc(IsectRayPrecalc *isect_precalc, const float ray_direction[3])
float area_squared_poly_v2(const float verts[][2], uint nr)
Definition math_geom.cc:192
float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:300
#define CROSS_SIGN(dir_a, dir_b)
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
void projmat_dimensions(const float winmat[4][4], float *r_left, float *r_right, float *r_bottom, float *r_top, float *r_near, float *r_far)
void map_to_plane_v2_v3v3(float r_co[2], const float co[3], const float no[3])
bool is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
float cotangent_tri_weight_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:198
void resolve_quad_uv_v2_deriv(float r_uv[2], float r_deriv[2][2], const float st[2], const float st0[2], const float st1[2], const float st2[2], const float st3[2])
bool isect_ray_ray_epsilon_v3(const float ray_origin_a[3], const float ray_direction_a[3], const float ray_origin_b[3], const float ray_direction_b[3], const float epsilon, float *r_lambda_a, float *r_lambda_b)
void barycentric_weights_v2_persp(const float v1[4], const float v2[4], const float v3[4], const float co[2], float w[3])
float dist_to_line_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:541
float ray_point_factor_v3_ex(const float p[3], const float ray_origin[3], const float ray_direction[3], const float epsilon, const float fallback)
static float tri_signed_area(const float v1[3], const float v2[3], const float v3[3], const int i, const int j)
float dist_signed_squared_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:463
int interp_sparse_array(float *array, const int list_size, const float skipval)
void orthographic_m4(float mat[4][4], const float left, const float right, const float bottom, const float top, const float nearClip, const float farClip)
bool isect_ray_tri_epsilon_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2], const float epsilon)
float dist_squared_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:278
int isect_line_line_epsilon_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3], const float epsilon)
int isect_seg_seg_v2_int(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
int isect_seg_seg_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
bool isect_axial_line_segment_tri_v3(const int axis, const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda)
bool isect_line_segment_tri_v3(const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:85
float dist_signed_squared_to_plane3_v3(const float p[3], const float plane[3])
Definition math_geom.cc:479
void interp_weights_poly_v3(float *w, float v[][3], const int n, const float co[3])
float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:286
void plane_to_point_vector_v3_normalized(const float plane[4], float r_plane_co[3], float r_plane_no[3])
Definition math_geom.cc:229
int isect_aabb_planes_v3(const float(*planes)[4], const int totplane, const float bbmin[3], const float bbmax[3])
void interp_bilinear_quad_v3(float data[4][3], float u, float v, float res[3])
bool isect_point_planes_v3_negated(const float(*planes)[4], const int totplane, const float p[3])
void planes_from_projmat(const float mat[4][4], float left[4], float right[4], float bottom[4], float top[4], float near[4], float far[4])
bool isect_point_tri_prism_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3])
void map_to_plane_axis_angle_v2_v3v3fl(float r_co[2], const float co[3], const float axis[3], const float angle)
bool isect_ray_plane_v3(const float ray_origin[3], const float ray_direction[3], const float plane[4], float *r_lambda, const bool clip)
float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:239
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:291
void box_minmax_bounds_m4(float min[3], float max[3], float boundbox[2][3], float mat[4][4])
static float snap_coordinate(float u)
float dist_signed_squared_to_corner_v3v3v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], const float axis_ref[3])
Definition math_geom.cc:546
static int left
double epsilon
default precision while comparing with Equal(..,..) functions. Initialized at 0.0000001.
Definition utility.cpp:22
float safe_acos_approx(float x)
const btScalar eps
Definition poly34.cpp:11
return ret
#define fabsf
#define sqrtf
#define sinf
#define tanf
#define cosf
#define atan2f
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
double dir[2]
float dir[3]
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
uint len
uint8_t flag
Definition wm_window.cc:145