mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
add error checking and force_region on all functions
This commit is contained in:
parent
b0bcdade7e
commit
c4438e2d3e
1 changed files with 27 additions and 11 deletions
38
regions.scad
38
regions.scad
|
@ -93,7 +93,8 @@ function check_and_fix_path(path, valid_dim=undef, closed=false, name="path") =
|
||||||
// Examples:
|
// Examples:
|
||||||
//
|
//
|
||||||
function sanitize_region(r,nonzero=false,eps=EPSILON) =
|
function sanitize_region(r,nonzero=false,eps=EPSILON) =
|
||||||
assert(is_region(r))
|
let(r=force_region(r))
|
||||||
|
assert(is_region(r), "Input is not a region")
|
||||||
exclusive_or(
|
exclusive_or(
|
||||||
[for(poly=r) each polygon_parts(poly,nonzero,eps)],
|
[for(poly=r) each polygon_parts(poly,nonzero,eps)],
|
||||||
eps=eps);
|
eps=eps);
|
||||||
|
@ -117,7 +118,8 @@ function sanitize_region(r,nonzero=false,eps=EPSILON) =
|
||||||
module region(r)
|
module region(r)
|
||||||
{
|
{
|
||||||
no_children($children);
|
no_children($children);
|
||||||
r = is_path(r) ? [r] : r;
|
r = force_region(r);
|
||||||
|
dummy=assert(is_region(r), "Input is not a region");
|
||||||
points = flatten(r);
|
points = flatten(r);
|
||||||
lengths = [for(path=r) len(path)];
|
lengths = [for(path=r) len(path)];
|
||||||
starts = [0,each cumsum(lengths)];
|
starts = [0,each cumsum(lengths)];
|
||||||
|
@ -158,10 +160,12 @@ function point_in_region(point, region, eps=EPSILON, _i=0, _cnt=0) =
|
||||||
// region = region to check
|
// region = region to check
|
||||||
// eps = tolerance for geometric comparisons. Default: `EPSILON` = 1e-9
|
// eps = tolerance for geometric comparisons. Default: `EPSILON` = 1e-9
|
||||||
function is_region_simple(region, eps=EPSILON) =
|
function is_region_simple(region, eps=EPSILON) =
|
||||||
|
let(region=force_region(region))
|
||||||
|
assert(is_region(region), "Input is not a region")
|
||||||
[for(p=region) if (!is_path_simple(p,closed=true,eps)) 1] == []
|
[for(p=region) if (!is_path_simple(p,closed=true,eps)) 1] == []
|
||||||
&&
|
&&
|
||||||
[for(i=[0:1:len(region)-2])
|
[for(i=[0:1:len(region)-2])
|
||||||
if (_path_region_intersections(region[i], list_tail(region,i+1), eps=eps) != []) 1
|
if (_region_region_intersections([region[i]], list_tail(region,i+1), eps=eps)[0][0] != []) 1
|
||||||
] ==[];
|
] ==[];
|
||||||
|
|
||||||
function _clockwise_region(r) = [for(p=r) clockwise_polygon(p)];
|
function _clockwise_region(r) = [for(p=r) clockwise_polygon(p)];
|
||||||
|
@ -181,7 +185,7 @@ function are_regions_equal(region1, region2, either_winding=false) =
|
||||||
region1=force_region(region1),
|
region1=force_region(region1),
|
||||||
region2=force_region(region2)
|
region2=force_region(region2)
|
||||||
)
|
)
|
||||||
assert(is_region(region1) && is_region(region2))
|
assert(is_region(region1) && is_region(region2), "One of the inputs is not a region")
|
||||||
len(region1) != len(region2)? false :
|
len(region1) != len(region2)? false :
|
||||||
__are_regions_equal(either_winding?_clockwise_region(region1):region1,
|
__are_regions_equal(either_winding?_clockwise_region(region1):region1,
|
||||||
either_winding?_clockwise_region(region2):region2,
|
either_winding?_clockwise_region(region2):region2,
|
||||||
|
@ -299,7 +303,10 @@ function _region_region_intersections(region1, region2, closed1=true,closed2=tru
|
||||||
function split_region_at_region_crossings(region1, region2, closed1=true, closed2=true, eps=EPSILON) =
|
function split_region_at_region_crossings(region1, region2, closed1=true, closed2=true, eps=EPSILON) =
|
||||||
let(
|
let(
|
||||||
region1=force_region(region1),
|
region1=force_region(region1),
|
||||||
region2=force_region(region2),
|
region2=force_region(region2)
|
||||||
|
)
|
||||||
|
assert(is_region(region1) && is_region(region2),"One of the inputs is not a region")
|
||||||
|
let(
|
||||||
xings = _region_region_intersections(region1, region2, closed1, closed2, eps),
|
xings = _region_region_intersections(region1, region2, closed1, closed2, eps),
|
||||||
regions = [region1,region2],
|
regions = [region1,region2],
|
||||||
closed = [closed1,closed2]
|
closed = [closed1,closed2]
|
||||||
|
@ -347,7 +354,10 @@ function split_region_at_region_crossings(region1, region2, closed1=true, closed
|
||||||
// rainbow(region_list) region($item);
|
// rainbow(region_list) region($item);
|
||||||
function region_parts(region) =
|
function region_parts(region) =
|
||||||
let(
|
let(
|
||||||
region = force_region(region),
|
region = force_region(region)
|
||||||
|
)
|
||||||
|
assert(is_region(region), "Input is not a region")
|
||||||
|
let(
|
||||||
inside = [for(i=idx(region))
|
inside = [for(i=idx(region))
|
||||||
let(pt = mean([region[i][0], region[i][1]]))
|
let(pt = mean([region[i][0], region[i][1]]))
|
||||||
[for(j=idx(region)) i==j ? 0
|
[for(j=idx(region)) i==j ? 0
|
||||||
|
@ -436,7 +446,7 @@ function _cleave_connected_region(region) =
|
||||||
// vnf = If given, the faces are added to this VNF. Default: `EMPTY_VNF`
|
// vnf = If given, the faces are added to this VNF. Default: `EMPTY_VNF`
|
||||||
function region_faces(region, transform, reverse=false, vnf=EMPTY_VNF) =
|
function region_faces(region, transform, reverse=false, vnf=EMPTY_VNF) =
|
||||||
let (
|
let (
|
||||||
regions = region_parts(region),
|
regions = region_parts(force_region(region)),
|
||||||
vnfs = [
|
vnfs = [
|
||||||
if (vnf != EMPTY_VNF) vnf,
|
if (vnf != EMPTY_VNF) vnf,
|
||||||
for (rgn = regions) let(
|
for (rgn = regions) let(
|
||||||
|
@ -494,7 +504,8 @@ function region_faces(region, transform, reverse=false, vnf=EMPTY_VNF) =
|
||||||
// orgn = difference(mrgn,rgn3);
|
// orgn = difference(mrgn,rgn3);
|
||||||
// linear_sweep(orgn,height=20,convexity=16) show_anchors();
|
// linear_sweep(orgn,height=20,convexity=16) show_anchors();
|
||||||
module linear_sweep(region, height=1, center, twist=0, scale=1, slices, maxseg, style="default", convexity, anchor_isect=false, anchor, spin=0, orient=UP) {
|
module linear_sweep(region, height=1, center, twist=0, scale=1, slices, maxseg, style="default", convexity, anchor_isect=false, anchor, spin=0, orient=UP) {
|
||||||
region = is_path(region)? [region] : region;
|
region = force_region(region);
|
||||||
|
dummy=assert(is_region(region),"Input is not a region");
|
||||||
cp = mean(pointlist_bounds(flatten(region)));
|
cp = mean(pointlist_bounds(flatten(region)));
|
||||||
anchor = get_anchor(anchor, center, "origin", "origin");
|
anchor = get_anchor(anchor, center, "origin", "origin");
|
||||||
vnf = linear_sweep(
|
vnf = linear_sweep(
|
||||||
|
@ -512,11 +523,14 @@ module linear_sweep(region, height=1, center, twist=0, scale=1, slices, maxseg,
|
||||||
|
|
||||||
function linear_sweep(region, height=1, center, twist=0, scale=1, slices,
|
function linear_sweep(region, height=1, center, twist=0, scale=1, slices,
|
||||||
maxseg, style="default", anchor_isect=false, anchor, spin=0, orient=UP) =
|
maxseg, style="default", anchor_isect=false, anchor, spin=0, orient=UP) =
|
||||||
|
let(
|
||||||
|
region = force_region(region)
|
||||||
|
)
|
||||||
|
assert(is_region(region), "Input is not a region")
|
||||||
let(
|
let(
|
||||||
anchor = get_anchor(anchor,center,BOT,BOT),
|
anchor = get_anchor(anchor,center,BOT,BOT),
|
||||||
region = is_path(region)? [region] : region,
|
|
||||||
cp = mean(pointlist_bounds(flatten(region))),
|
|
||||||
regions = region_parts(region),
|
regions = region_parts(region),
|
||||||
|
cp = mean(pointlist_bounds(flatten(region))),
|
||||||
slices = default(slices, floor(twist/5+1)),
|
slices = default(slices, floor(twist/5+1)),
|
||||||
step = twist/slices,
|
step = twist/slices,
|
||||||
hstep = height/slices,
|
hstep = height/slices,
|
||||||
|
@ -788,8 +802,10 @@ function offset(
|
||||||
chamfer=chamfer, check_valid=check_valid, quality=quality,closed=true)])]
|
chamfer=chamfer, check_valid=check_valid, quality=quality,closed=true)])]
|
||||||
)
|
)
|
||||||
union(ofsregs)
|
union(ofsregs)
|
||||||
: let(rcount = num_defined([r,delta]))
|
:
|
||||||
|
let(rcount = num_defined([r,delta]))
|
||||||
assert(rcount==1,"Must define exactly one of 'delta' and 'r'")
|
assert(rcount==1,"Must define exactly one of 'delta' and 'r'")
|
||||||
|
assert(is_path(path), "Input must be a path or region")
|
||||||
let(
|
let(
|
||||||
chamfer = is_def(r) ? false : chamfer,
|
chamfer = is_def(r) ? false : chamfer,
|
||||||
quality = max(0,round(quality)),
|
quality = max(0,round(quality)),
|
||||||
|
|
Loading…
Reference in a new issue