hull 2d tweak

This commit is contained in:
Adrian Mariano 2021-03-05 20:39:15 -05:00
parent f736ef98f7
commit 6a1b141d5c
2 changed files with 13 additions and 13 deletions

View file

@ -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)

View file

@ -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],