From 5435bb1fc27b4ea959a3b23083e99b975af0ff87 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Sun, 28 Jan 2024 18:22:04 -0500 Subject: [PATCH] add deduplicate to rect(), add deduplicate to is_path_simple add epsilon to degenerate bezier patch detection --- beziers.scad | 4 ++-- paths.scad | 5 ++++- shapes2d.scad | 2 +- tests/test_paths.scad | 8 +++++++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/beziers.scad b/beziers.scad index 21b6bed..605ce5e 100644 --- a/beziers.scad +++ b/beziers.scad @@ -1252,8 +1252,8 @@ function bezier_vnf_degenerate_patch(patch, splinesteps=16, reverse=false, retur assert(is_bezier_patch(patch), "Input is not a Bezier patch") assert(is_int(splinesteps) && splinesteps>0, "splinesteps must be a positive integer") let( - row_degen = [for(row=patch) all_equal(row)], - col_degen = [for(col=transpose(patch)) all_equal(col)], + row_degen = [for(row=patch) all_equal(row,eps=EPSILON)], + col_degen = [for(col=transpose(patch)) all_equal(col,eps=EPSILON)], top_degen = row_degen[0], bot_degen = last(row_degen), left_degen = col_degen[0], diff --git a/paths.scad b/paths.scad index 52f18f5..3d3b1e8 100644 --- a/paths.scad +++ b/paths.scad @@ -280,7 +280,7 @@ function _path_self_intersections(path, closed=true, eps=EPSILON) = // [a1,a2]. The variable signals is zero when abs(vals[j]-ref) is less than // eps and the sign of vals[j]-ref otherwise. signals = [for(j=[i+2:1:plen-(i==0 && closed? 2: 1)]) - abs(vals[j]-ref) < eps ? 0 : sign(vals[j]-ref) ] + abs(vals[j]-ref) < eps ? 0 : sign(vals[j]-ref) ] ) if(max(signals)>=0 && min(signals)<=0 ) // some remaining edge intersects line [a1,a2] for(j=[i+2:1:plen-(i==0 && closed? 3: 2)]) @@ -581,6 +581,9 @@ function is_path_simple(path, closed, eps=EPSILON) = let(closed=default(closed,false)) assert(is_path(path, 2),"Must give a 2D path") assert(is_bool(closed)) + let( + path = deduplicate(path,closed=closed,eps=eps) + ) // check for path reversals [for(i=[0:1:len(path)-(closed?2:3)]) let(v1=path[i+1]-path[i], diff --git a/shapes2d.scad b/shapes2d.scad index 20d8920..fcf17f4 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -214,7 +214,7 @@ function rect(size=1, rounding=0, chamfer=0, atype="box", anchor=CENTER, spin=0, assert(is_undef(cornerpt) || len(cornerpt)==1,"Cannot find corner point to anchor") [move(cp, p=qrpts), is_undef(cornerpt)? undef : move(cp,p=cornerpt[0])] ], - path = flatten(column(corners,0)), + path = deduplicate(flatten(column(corners,0)),closed=true), override = [for(i=[0:3]) let(quad=quadorder[i]) if (is_def(corners[i][1])) [quadpos[quad], [corners[i][1], min(chamfer[quad],rounding[quad])<0 ? [quadpos[quad].x,0] : undef]]] diff --git a/tests/test_paths.scad b/tests/test_paths.scad index cf8d740..f86c35f 100644 --- a/tests/test_paths.scad +++ b/tests/test_paths.scad @@ -298,5 +298,11 @@ test_path_torsion(); -//echo(fmt_float(sampled)); +module test_is_path_simple(){ + assert(is_path_simple([[0,0],[1,1],[1,1],[2,1]])); + assert(is_path_simple([[0,0],[10,0],[0,20],[10,20]],closed=false)); + assert(!is_path_simple([[0,0],[10,0],[0,20],[10,20]],closed=true)); + assert(is_path_simple(circle($fn=20, r=10))); +} +