From 6a1b141d5c62dd692620f12efb9791f11d88a3c9 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Fri, 5 Mar 2021 20:39:15 -0500 Subject: [PATCH] hull 2d tweak --- hull.scad | 18 +++++++++--------- tests/test_hull.scad | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/hull.scad b/hull.scad index fbfecdb..5658bec 100644 --- a/hull.scad +++ b/hull.scad @@ -80,7 +80,7 @@ function _backtracking(i,points,h,t,m) = _backtracking(i,points,h,t,m-1) ; // clockwise check (2d) -function _is_cw(a,b,c) = cross(a-c,b-c)<=0; +function _is_cw(a,b,c) = cross(a-c,b-c)<-EPSILON*norm(a-c)*norm(b-c); // Function: hull2d_path() @@ -100,13 +100,7 @@ function _is_cw(a,b,c) = cross(a-c,b-c)<=0; // function hull2d_path(points) = assert(is_path(points,2),"Invalid input to hull2d_path") - len(points) < 2 ? [] - : len(points) == 2 ? [0,1] - : let(tri=noncollinear_triple(points, error=false)) - tri == [] ? _hull_collinear(points) - : - assert(is_path(points,2)) - assert(len(points)>=3, "Point list must contain at least 3 points.") + len(points) < 2 ? [] : let( n = len(points), ip = sortidx(points) ) // lower hull points @@ -134,13 +128,19 @@ function hull2d_path(points) = function _hull_collinear(points) = let( a = points[0], - n = points[1] - a, + i = max_index([for(pt=points) norm(pt-a)]), + n = points[i] - a + ) + norm(n)==0 ? [0] + : + let( points1d = [ for(p = points) (p-a)*n ], min_i = min_index(points1d), max_i = max_index(points1d) ) [min_i, max_i]; + // Function: hull3d_faces() // Usage: // hull3d_faces(points) diff --git a/tests/test_hull.scad b/tests/test_hull.scad index 3aba71a..a6a7926 100644 --- a/tests/test_hull.scad +++ b/tests/test_hull.scad @@ -6,9 +6,9 @@ module test_hull() { assert_equal(hull([[3,4,1],[5,5,3]]), [0,1]); test_collinear_2d = let(u = unit([5,3])) [ for(i = [9,2,3,4,5,7,12,15,13]) i * u ]; - assert_equal(hull(test_collinear_2d), [7,1]); + assert_equal(sort(hull(test_collinear_2d)), [1,7]); test_collinear_3d = let(u = unit([5,3,2])) [ for(i = [9,2,3,4,5,7,12,15,13]) i * u ]; - assert_equal(hull(test_collinear_3d), [7,1]); + assert_equal(sort(hull(test_collinear_3d)), [1,7]); /* // produces some extra points along edges test_square_2d = [for(x=[1:5], y=[2:6]) [x,y]]; @@ -105,9 +105,9 @@ module test_hull2d_path() { assert_equal(hull([[3,4,1],[5,5,3]]), [0,1]); test_collinear_2d = let(u = unit([5,3])) [ for(i = [9,2,3,4,5,7,12,15,13]) i * u ]; - assert_equal(hull(test_collinear_2d), [7,1]); + assert_equal(sort(hull(test_collinear_2d)), [1,7]); test_collinear_3d = let(u = unit([5,3,2])) [ for(i = [9,2,3,4,5,7,12,15,13]) i * u ]; - assert_equal(hull(test_collinear_3d), [7,1]); + assert_equal(sort(hull(test_collinear_3d)), [1,7]); rand10_2d = [[1.55356, -1.98965], [4.23157, -0.947788], [-4.06193, -1.55463], [1.23889, -3.73133], [-1.02637, -4.0155], [4.26806, -4.61909],