37 #include "print_string.h" 39 #include "triangulate.h" 61 if (a <= CMP_EPSILON && e <= CMP_EPSILON) {
65 return Math::sqrt((c1 - c2).dot(c1 - c2));
67 if (a <= CMP_EPSILON) {
71 t = CLAMP(t, 0.0f, 1.0f);
74 if (e <= CMP_EPSILON) {
77 s = CLAMP(-c / a, 0.0f, 1.0f);
81 float denom = a*e-b*b;
85 s = CLAMP((b*f - c*e) / denom, 0.0f, 1.0f);
97 s = CLAMP(-c / a, 0.0f, 1.0f);
98 }
else if (t > 1.0f) {
100 s = CLAMP((b - c) / a, 0.0f, 1.0f);
106 return Math::sqrt((c1 - c2).dot(c1 - c2));
113 #define d_of(m,n,o,p) ( (m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z) ) 116 float mua = ( d_of(p1,q1,q2,q1) * d_of(q2,q1,p2,p1) - d_of(p1,q1,p2,p1) * d_of(q2,q1,q2,q1) ) / ( d_of(p2,p1,p2,p1) * d_of(q2,q1,q2,q1) - d_of(q2,q1,p2,p1) * d_of(q2,q1,p2,p1) );
117 float mub = ( d_of(p1,q1,q2,q1) + mua * d_of(q2,q1,p2,p1) ) / d_of(q2,q1,q2,q1);
120 if (mua < 0) mua = 0;
121 if (mub < 0) mub = 0;
122 if (mua > 1) mua = 1;
123 if (mub > 1) mub = 1;
124 c1 = p1.linear_interpolate(p2,mua);
125 c2 = q1.linear_interpolate(q2,mub);
128 static float get_closest_distance_between_segments(
const Vector3& p_from_a,
const Vector3& p_to_a,
const Vector3& p_from_b,
const Vector3& p_to_b) {
137 real_t D = a*c - b*b;
138 real_t sc, sN, sD = D;
139 real_t tc, tN, tD = D;
142 if (D < CMP_EPSILON) {
180 else if ((-d + b) > a)
188 sc = (Math::abs(sN) < CMP_EPSILON ? 0.0 : sN / sD);
189 tc = (Math::abs(tN) < CMP_EPSILON ? 0.0 : tN / tD);
192 Vector3 dP = w + (sc * u) - (tc * v);
202 if (a>-CMP_EPSILON && a < CMP_EPSILON)
208 real_t u = f * s.dot(h);
210 if ( u< 0.0 || u > 1.0)
215 real_t v = f * p_dir.dot(q);
217 if (v < 0.0 || u + v > 1.0)
222 real_t t = f * e2.dot(q);
226 *r_res=p_from+p_dir*t;
240 if (a>-CMP_EPSILON && a < CMP_EPSILON)
246 real_t u = f * s.dot(h);
248 if ( u< 0.0 || u > 1.0)
253 real_t v = f * rel.dot(q);
255 if (v < 0.0 || u + v > 1.0)
260 real_t t = f * e2.dot(q);
262 if (t > CMP_EPSILON && t<=1.0) {
271 static inline bool segment_intersects_sphere(
const Vector3& p_from,
const Vector3& p_to,
const Vector3& p_sphere_pos,real_t p_sphere_radius,
Vector3* r_res=0,
Vector3 *r_norm=0) {
274 Vector3 sphere_pos=p_sphere_pos-p_from;
276 float rel_l=rel.length();
277 if (rel_l<CMP_EPSILON)
281 float sphere_d=normal.dot(sphere_pos);
285 float ray_distance=sphere_pos.distance_to(normal*sphere_d);
287 if (ray_distance>=p_sphere_radius)
290 float inters_d2=p_sphere_radius*p_sphere_radius - ray_distance*ray_distance;
291 float inters_d=sphere_d;
293 if (inters_d2>=CMP_EPSILON)
294 inters_d-=Math::sqrt(inters_d2);
297 if (inters_d<0 || inters_d>rel_l)
300 Vector3 result=p_from+normal*inters_d;;
305 *r_norm=(result-p_sphere_pos).normalized();
310 static inline bool segment_intersects_cylinder(
const Vector3& p_from,
const Vector3& p_to,
float p_height,
float p_radius,
Vector3* r_res=0,
Vector3 *r_norm=0) {
313 float rel_l=rel.length();
314 if (rel_l<CMP_EPSILON)
320 float crs_l=crs.length();
324 if(crs_l<CMP_EPSILON) {
331 float dist=z_dir.dot(p_from);
337 float w2=p_radius*p_radius-dist*dist;
340 Size2 size(Math::sqrt(w2),p_height*0.5);
344 Vector2 from2D(x_dir.dot(p_from),p_from.z);
345 Vector2 to2D(x_dir.dot(p_to),p_to.z);
351 for(
int i=0;i<2;i++) {
353 real_t seg_from=from2D[i];
354 real_t seg_to=to2D[i];
355 real_t box_begin=-size[i];
356 real_t box_end=size[i];
360 if (seg_from < seg_to) {
362 if (seg_from > box_end || seg_to < box_begin)
364 real_t length=seg_to-seg_from;
365 cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
366 cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
370 if (seg_to > box_end || seg_from < box_begin)
372 real_t length=seg_to-seg_from;
373 cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
374 cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
389 Vector3 result = p_from + (rel*min);
400 res_normal.normalize();
413 real_t min=-1e20,max=1e20;
416 real_t rel_l=rel.length();
418 if (rel_l<CMP_EPSILON)
425 for (
int i=0;i<p_plane_count;i++) {
427 const Plane&p=p_planes[i];
429 real_t den=p.normal.dot( dir );
432 if (Math::abs(den)<=CMP_EPSILON)
436 real_t dist=-p.distance_to(p_from ) / den;
452 if (max<=min || min<0 || min>rel_l || min_index==-1)
456 *p_res=p_from+dir*min;
458 *p_norm=p_planes[min_index].normal;
465 Vector3 p=p_point-p_segment[0];
466 Vector3 n=p_segment[1]-p_segment[0];
479 return p_segment[0]+n*d;
482 static Vector3 get_closest_point_to_segment_uncapped(
const Vector3& p_point,
const Vector3 *p_segment) {
484 Vector3 p=p_point-p_segment[0];
485 Vector3 n=p_segment[1]-p_segment[0];
493 return p_segment[0]+n*d;
498 Vector2 p=p_point-p_segment[0];
499 Vector2 n=p_segment[1]-p_segment[0];
512 return p_segment[0]+n*d;
520 bool s_ab = (b.x-a.x)*as_y-(b.y-a.y)*as_x > 0;
522 if(((c.x-a.x)*as_y-(c.y-a.y)*as_x > 0) == s_ab)
return false;
524 if(((c.x-b.x)*(s.y-b.y)-(c.y-b.y)*(s.x-b.x) > 0) != s_ab)
return false;
528 static Vector2 get_closest_point_to_segment_uncapped_2d(
const Vector2& p_point,
const Vector2 *p_segment) {
530 Vector2 p=p_point-p_segment[0];
531 Vector2 n=p_segment[1]-p_segment[0];
539 return p_segment[0]+n*d;
548 real_t ABlen = B.dot(B);
552 C =
Vector2( C.x*Bn.x + C.y*Bn.y, C.y*Bn.x - C.x*Bn.y );
553 D =
Vector2( D.x*Bn.x + D.y*Bn.y, D.y*Bn.x - D.x*Bn.y );
555 if ((C.y<0 && D.y<0) || (C.y>=0 && D.y>=0))
558 float ABpos=D.x+(C.x-D.x)*D.y/(D.y-C.y);
561 if (ABpos<0 || ABpos>1.0)
566 *r_result=p_from_a+B*ABpos;
575 Vector3 face_n = (p_v1-p_v3).cross(p_v1-p_v2);
577 Vector3 n1 = (p_point-p_v3).cross(p_point-p_v2);
579 if (face_n.dot(n1)<0)
582 Vector3 n2 = (p_v1-p_v3).cross(p_v1-p_point);
584 if (face_n.dot(n2)<0)
587 Vector3 n3 = (p_v1-p_point).cross(p_v1-p_v2);
589 if (face_n.dot(n3)<0)
598 float d=p_normal.dot(p_sphere_pos)-p_normal.dot(p_triangle[0]);
600 if (d > p_sphere_radius || d < -p_sphere_radius)
603 Vector3 contact=p_sphere_pos - (p_normal*d);
608 if (Geometry::point_in_projected_triangle(contact,p_triangle[0],p_triangle[1],p_triangle[2])) {
609 r_triangle_contact=contact;
610 r_sphere_contact=p_sphere_pos-p_normal*p_sphere_radius;
617 const Vector3 verts[4]={p_triangle[0],p_triangle[1],p_triangle[2],p_triangle[0]};
619 for (
int i=0;i<3;i++) {
623 Vector3 n1=verts[i]-verts[i+1];
624 Vector3 n2=p_sphere_pos-verts[i+1];
629 Vector3 axis =n1.cross(n2).cross(n1);
632 float ad=axis.dot(n2);
634 if (ABS(ad)>p_sphere_radius) {
642 float sphere_at = n1.dot(n2);
644 if (sphere_at>=0 && sphere_at<n1.dot(n1)) {
646 r_triangle_contact=p_sphere_pos-axis*(axis.dot(n2));
647 r_sphere_contact=p_sphere_pos-axis*p_sphere_radius;
653 float r2=p_sphere_radius*p_sphere_radius;
655 if (n2.length_squared()<r2) {
657 Vector3 n=(p_sphere_pos-verts[i+1]).normalized();
660 r_triangle_contact=verts[i+1];
661 r_sphere_contact=p_sphere_pos-n*p_sphere_radius;
666 if (n2.distance_squared_to(n1)<r2) {
667 Vector3 n=(p_sphere_pos-verts[i]).normalized();
670 r_triangle_contact=verts[i];
671 r_sphere_contact=p_sphere_pos-n*p_sphere_radius;
684 static real_t segment_intersects_circle(
const Vector2& p_from,
const Vector2& p_to,
const Vector2& p_circle_pos, real_t p_circle_radius) {
686 Vector2 line_vec = p_to - p_from;
687 Vector2 vec_to_line = p_from - p_circle_pos;
692 a = line_vec.dot(line_vec);
693 b = 2 * vec_to_line.dot(line_vec);
694 c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius;
697 real_t sqrtterm = b*b - 4*a*c;
700 if(sqrtterm < 0)
return -1;
703 sqrtterm = Math::sqrt(sqrtterm);
704 real_t res1 = ( -b - sqrtterm ) / (2 * a);
707 return (res1 >= 0 && res1 <= 1) ? res1 : -1;
721 if (polygon.size()==0)
724 int *location_cache = (
int*)alloca(
sizeof(
int)*polygon.size());
725 int inside_count = 0;
726 int outside_count = 0;
728 for (
int a = 0; a < polygon.size(); a++) {
730 float dist = p_plane.distance_to(polygon[a]);
731 if (dist <-CMP_POINT_IN_PLANE_EPSILON) {
732 location_cache[a] = LOC_INSIDE;
735 if (dist > CMP_POINT_IN_PLANE_EPSILON) {
736 location_cache[a] = LOC_OUTSIDE;
739 location_cache[a] = LOC_BOUNDARY;
744 if (outside_count == 0) {
748 }
else if (inside_count == 0) {
754 long previous = polygon.size() - 1;
758 for (
int index = 0; index < polygon.size(); index++) {
759 int loc = location_cache[index];
760 if (loc == LOC_OUTSIDE) {
761 if (location_cache[previous] == LOC_INSIDE) {
762 const Vector3& v1 = polygon[previous];
763 const Vector3& v2 = polygon[index];
766 double den=p_plane.normal.dot( segment );
767 double dist=p_plane.distance_to( v1 ) / den;
769 clipped.push_back( v1 + segment * dist );
772 const Vector3& v1 = polygon[index];
773 if ((loc == LOC_INSIDE) && (location_cache[previous] == LOC_OUTSIDE)) {
774 const Vector3& v2 = polygon[previous];
776 double den=p_plane.normal.dot( segment );
777 double dist=p_plane.distance_to( v1 ) / den;
779 clipped.push_back( v1 + segment * dist );
782 clipped.push_back(v1);
795 if (!Triangulate::triangulate(p_polygon,triangles))
804 return _decompose_func(p_polygon);
834 void optimize_vertices();
839 _FORCE_INLINE_
static int get_uv84_normal_bit(
const Vector3& p_vector) {
841 int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(
Vector3(0,1,0)))*4.0/Math_PI+0.5));
849 int lon = Math::fast_ftoi(Math::floor( (Math_PI+Math::atan2(p_vector.x,p_vector.z))*8.0/(Math_PI*2.0) + 0.5))%8;
851 return lon+(lat-1)*8;
854 _FORCE_INLINE_
static int get_uv84_normal_bit_neighbors(
int p_idx) {
858 }
else if (p_idx==25) {
859 return (1<<23)|(1<<22)|(1<<21)|(1<<20);
872 int mask = ret|(1<<p_idx);
891 return (
double)(A.x - O.x) * (B.y - O.y) - (double)(A.y - O.y) * (B.x - O.x);
898 int n = P.size(), k = 0;
907 for (
int i = 0; i < n; ++i) {
908 while (k >= 2 && vec2_cross(H[k-2], H[k-1], P[i]) <= 0) k--;
913 for (
int i = n-2, t = k+1; i >= 0; i--) {
914 while (k >= t && vec2_cross(H[k-2], H[k-1], P[i]) <= 0) k--;
923 static DVector<Plane> build_sphere_planes(
float p_radius,
int p_lats,
int p_lons, Vector3::Axis p_axis=Vector3::AXIS_Z);
925 static DVector<Plane> build_cylinder_planes(
float p_radius,
float p_height,
int p_sides, Vector3::Axis p_axis=Vector3::AXIS_Z);
926 static DVector<Plane> build_capsule_planes(
float p_radius,
float p_height,
int p_sides,
int p_lats, Vector3::Axis p_axis=Vector3::AXIS_Z);
static DVector< Face3 > wrap_geometry(DVector< Face3 > p_array, float *p_error=NULL)
create a "wrap" that encloses the given geometry
Definition: geometry.cpp:583
Definition: math_2d.h:369
Definition: geometry.h:818
Definition: geometry.h:825
Definition: geometry.h:816
Definition: geometry.h:44
static bool triangle_sphere_intersection_test(const Vector3 *p_triangle, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact)
Definition: geometry.h:596