From e63c1680a6a0fc288491480f26c482a484e419b6 Mon Sep 17 00:00:00 2001 From: RonaldoCMP Date: Fri, 25 Jun 2021 14:40:03 +0100 Subject: [PATCH] Better cides for _closest_s --- geometry.scad | 85 +++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/geometry.scad b/geometry.scad index 92d474f..a61298f 100644 --- a/geometry.scad +++ b/geometry.scad @@ -1,4 +1,4 @@ -////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////// // LibFile: geometry.scad // Geometry helpers. // Includes: @@ -1778,7 +1778,7 @@ function circle_line_intersection(c,r,d,line,bounded=false,eps=EPSILON) = function noncollinear_triple(points,error=true,eps=EPSILON) = assert( is_path(points), "Invalid input points." ) assert( is_finite(eps) && (eps>=0), "The tolerance should be a non-negative value." ) - len(pts)<3 ? [] : + len(pts)<3 ? [] : let( pa = points[0], b = furthest_point(pa, points), @@ -2106,6 +2106,7 @@ function point_in_polygon(point, poly, nonzero=true, eps=EPSILON) = // Usage: // test = polygon_is_clockwise(poly); // Topics: Geometry, Polygons, Clockwise +// See Also: clockwise_polygon(), ccw_polygon(), reverse_polygon() // Description: // Return true if the given 2D simple polygon is in clockwise order, false otherwise. // Results for complex (self-intersecting) polygon are indeterminate. @@ -2489,16 +2490,15 @@ function _GJK_collide(points1, points2, d, simplex, eps=EPSILON) = // - the point of the s closest to the origin // - the smallest sub-simplex of s that contains that point function _closest_simplex(s,eps=EPSILON) = - assert(len(s)>=2 && len(s)<=4, "Internal error.") len(s)==2 ? _closest_s1(s,eps) : - len(s)==3 ? _closest_s2(s,eps) - : _closest_s3(s,eps); + len(s)==3 ? _closest_s2(s,eps) : + len(s)==4 ? _closest_s3(s,eps) : + assert(false, "Internal error."); // find the point of a 1-simplex closest to the origin -// Based on: http://uu.diva-portal.org/smash/get/diva2/FFULLTEXT01.pdf function _closest_s1(s,eps=EPSILON) = - norm(s[1]-s[0]) 0 ? _closest_s1([s[0],s[1]]) : _closest_s1([s[0],s[2]]) : - class==3 ? _closest_s1([s[0],s[2]],eps) : -// class==5 ? b*(b-c)<=0 ? _closest_s1([s[0],s[1]]) : _closest_s1([s[1],s[2]]) : - class==5 ? _closest_s1([s[1],s[2]],eps) : - c*(c-a)>0 ? _closest_s1([s[0],s[2]],eps) : _closest_s1([s[1],s[2]],eps); - + // all have the same signal -> origin projects inside the tri + max(crx1, crx0, crx2) < 0 || min(crx1, crx0, crx2) > 0 + ? // baricentric coords of projection + [ [abs(crx0),abs(crx1),abs(crx2)]*s/area2, s ] + : let( + cl12 = _closest_s1([s[1],s[2]]), + cl02 = _closest_s1([s[0],s[2]]) + ) + norm(cl12[0])