Merge pull request #865 from revarbat/revarbat_dev

Make asserts functional due to OpenSCAD operation ordering.
This commit is contained in:
Revar Desmera 2022-05-02 19:11:34 -07:00 committed by GitHub
commit 5b2354f2c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 83 deletions

View file

@ -392,7 +392,7 @@ module ellipse(r, d, realign=false, circum=false, uniform=false, anchor=CENTER,
ry = r.y * sc; ry = r.y * sc;
attachable(anchor,spin, two_d=true, r=[rx,ry]) { attachable(anchor,spin, two_d=true, r=[rx,ry]) {
if (uniform) { if (uniform) {
assert(!circum, "Circum option not allowed when \"uniform\" is true"); check = assert(!circum, "Circum option not allowed when \"uniform\" is true");
polygon(ellipse(r,realign=realign, circum=circum, uniform=true)); polygon(ellipse(r,realign=realign, circum=circum, uniform=true));
} }
else if (rx < ry) { else if (rx < ry) {
@ -591,7 +591,7 @@ module regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false,
id = is_finite(id)? id*sc : undef; id = is_finite(id)? id*sc : undef;
side = is_finite(side)? side/2/sin(180/n) : undef; side = is_finite(side)? side/2/sin(180/n) : undef;
r = get_radius(r1=ir, r2=or, r=r, d1=id, d2=od, d=d, dflt=side); r = get_radius(r1=ir, r2=or, r=r, d1=id, d2=od, d=d, dflt=side);
assert(!is_undef(r), "regular_ngon(): need to specify one of r, d, or, od, ir, id, side."); check = assert(!is_undef(r), "regular_ngon(): need to specify one of r, d, or, od, ir, id, side.");
mat = ( realign? zrot(-180/n) : ident(4) ) * ( mat = ( realign? zrot(-180/n) : ident(4) ) * (
!is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip)) : !is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip)) :
!is_undef(align_side)? rot(from=RIGHT, to=point2d(align_side)) * zrot(180/n) : !is_undef(align_side)? rot(from=RIGHT, to=point2d(align_side)) * zrot(180/n) :
@ -828,7 +828,7 @@ function right_triangle(size=[1,1], center, anchor, spin=0) =
module right_triangle(size=[1,1], center, anchor, spin=0) { module right_triangle(size=[1,1], center, anchor, spin=0) {
size = is_num(size)? [size,size] : size; size = is_num(size)? [size,size] : size;
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]); anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]);
assert(is_vector(size,2)); check = assert(is_vector(size,2));
path = right_triangle(size, center=true); path = right_triangle(size, center=true);
attachable(anchor,spin, two_d=true, size=[size.x,size.y], size2=0, shift=-size.x/2) { attachable(anchor,spin, two_d=true, size=[size.x,size.y], size2=0, shift=-size.x/2) {
polygon(path); polygon(path);
@ -1083,10 +1083,11 @@ function star(n, r, ir, d, or, od, id, step, realign=false, align_tip, align_pit
module star(n, r, ir, d, or, od, id, step, realign=false, align_tip, align_pit, anchor=CENTER, spin=0, atype="hull") { module star(n, r, ir, d, or, od, id, step, realign=false, align_tip, align_pit, anchor=CENTER, spin=0, atype="hull") {
assert(in_list(atype, _ANCHOR_TYPES), "Anchor type must be \"hull\" or \"intersect\""); checks =
assert(is_undef(align_tip) || is_vector(align_tip)); assert(in_list(atype, _ANCHOR_TYPES), "Anchor type must be \"hull\" or \"intersect\"")
assert(is_undef(align_pit) || is_vector(align_pit)); assert(is_undef(align_tip) || is_vector(align_tip))
assert(is_undef(align_tip) || is_undef(align_pit), "Can only specify one of align_tip and align_pit"); assert(is_undef(align_pit) || is_vector(align_pit))
assert(is_undef(align_tip) || is_undef(align_pit), "Can only specify one of align_tip and align_pit");
r = get_radius(r1=or, d1=od, r=r, d=d, dflt=undef); r = get_radius(r1=or, d1=od, r=r, d=d, dflt=undef);
stepr = is_undef(step)? r : r*cos(180*step/n)/cos(180*(step-1)/n); stepr = is_undef(step)? r : r*cos(180*step/n)/cos(180*(step-1)/n);
ir = get_radius(r=ir, d=id, dflt=stepr); ir = get_radius(r=ir, d=id, dflt=stepr);
@ -1463,7 +1464,7 @@ function supershape(step=0.5, m1=4, m2, n1=1, n2, n3, a=1, b, r, d,anchor=CENTER
) reorient(anchor,spin, two_d=true, path=path, p=path, extent=atype=="hull"); ) reorient(anchor,spin, two_d=true, path=path, p=path, extent=atype=="hull");
module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=undef, d=undef, anchor=CENTER, spin=0, atype="hull") { module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=undef, d=undef, anchor=CENTER, spin=0, atype="hull") {
assert(in_list(atype, _ANCHOR_TYPES), "Anchor type must be \"hull\" or \"intersect\""); check = assert(in_list(atype, _ANCHOR_TYPES), "Anchor type must be \"hull\" or \"intersect\"");
path = supershape(step=step,m1=m1,m2=m2,n1=n1,n2=n2,n3=n3,a=a,b=b,r=r,d=d); path = supershape(step=step,m1=m1,m2=m2,n1=n1,n2=n2,n3=n3,a=a,b=b,r=r,d=d);
attachable(anchor,spin,extent=atype=="hull", two_d=true, path=path) { attachable(anchor,spin,extent=atype=="hull", two_d=true, path=path) {
polygon(path); polygon(path);
@ -1498,7 +1499,7 @@ module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=und
// Examples(2D): Named anchors exist for the tips // Examples(2D): Named anchors exist for the tips
// reuleaux_polygon(n=3, d=50) show_anchors(std=false); // reuleaux_polygon(n=3, d=50) show_anchors(std=false);
module reuleaux_polygon(n=3, r, d, anchor=CENTER, spin=0) { module reuleaux_polygon(n=3, r, d, anchor=CENTER, spin=0) {
assert(n>=3 && (n%2)==1); check = assert(n>=3 && (n%2)==1);
r = get_radius(r=r, d=d, dflt=1); r = get_radius(r=r, d=d, dflt=1);
path = reuleaux_polygon(n=n, r=r); path = reuleaux_polygon(n=n, r=r);
anchors = [ anchors = [

View file

@ -214,7 +214,7 @@ module cuboid(
e = _corner_edges(edges, corner); e = _corner_edges(edges, corner);
cnt = sum(e); cnt = sum(e);
r = first_defined([chamfer, rounding]); r = first_defined([chamfer, rounding]);
dummy=assert(is_finite(r) && !approx(r,0)); dummy = assert(is_finite(r) && !approx(r,0));
c = [r,r,r]; c = [r,r,r];
m = 0.01; m = 0.01;
c2 = v_mul(corner,c/2); c2 = v_mul(corner,c/2);
@ -266,15 +266,16 @@ module cuboid(
teardrop = is_bool(teardrop)&&teardrop? 45 : teardrop; teardrop = is_bool(teardrop)&&teardrop? 45 : teardrop;
chamfer = approx(chamfer,0) ? undef : chamfer; chamfer = approx(chamfer,0) ? undef : chamfer;
rounding = approx(rounding,0) ? undef : rounding; rounding = approx(rounding,0) ? undef : rounding;
assert(is_vector(size,3)); checks =
assert(all_positive(size)); assert(is_vector(size,3))
assert(is_undef(chamfer) || is_finite(chamfer),"chamfer must be a finite value"); assert(all_positive(size))
assert(is_undef(rounding) || is_finite(rounding),"rounding must be a finite value"); assert(is_undef(chamfer) || is_finite(chamfer),"chamfer must be a finite value")
assert(is_undef(rounding) || is_undef(chamfer), "Cannot specify nonzero value for both chamfer and rounding"); assert(is_undef(rounding) || is_finite(rounding),"rounding must be a finite value")
assert(teardrop==false || (is_finite(teardrop) && teardrop>0 && teardrop<90), "teardrop must be either false or an angle number between 0 and 90") assert(is_undef(rounding) || is_undef(chamfer), "Cannot specify nonzero value for both chamfer and rounding")
assert(is_undef(p1) || is_vector(p1)); assert(teardrop==false || (is_finite(teardrop) && teardrop>0 && teardrop<90), "teardrop must be either false or an angle number between 0 and 90")
assert(is_undef(p2) || is_vector(p2)); assert(is_undef(p1) || is_vector(p1))
assert(is_bool(trimcorners)); assert(is_undef(p2) || is_vector(p2))
assert(is_bool(trimcorners));
if (!is_undef(p1)) { if (!is_undef(p1)) {
if (!is_undef(p2)) { if (!is_undef(p2)) {
translate(pointlist_bounds([p1,p2])[0]) { translate(pointlist_bounds([p1,p2])[0]) {
@ -321,7 +322,7 @@ module cuboid(
} }
} }
} else if (chamfer<0) { } else if (chamfer<0) {
assert(edges == EDGES_ALL || edges[2] == [0,0,0,0], "Cannot use negative chamfer with Z aligned edges."); checks = assert(edges == EDGES_ALL || edges[2] == [0,0,0,0], "Cannot use negative chamfer with Z aligned edges.");
ach = abs(chamfer); ach = abs(chamfer);
cube(size, center=true); cube(size, center=true);
@ -408,7 +409,7 @@ module cuboid(
} }
} }
} else if (rounding<0) { } else if (rounding<0) {
assert(edges == EDGES_ALL || edges[2] == [0,0,0,0], "Cannot use negative rounding with Z aligned edges."); checks = assert(edges == EDGES_ALL || edges[2] == [0,0,0,0], "Cannot use negative rounding with Z aligned edges.");
ard = abs(rounding); ard = abs(rounding);
cube(size, center=true); cube(size, center=true);
@ -573,23 +574,25 @@ module prismoid(
l, center, l, center,
anchor, spin=0, orient=UP anchor, spin=0, orient=UP
) { ) {
assert(is_num(size1) || is_vector(size1,2)); checks =
assert(is_num(size2) || is_vector(size2,2)); assert(is_num(size1) || is_vector(size1,2))
assert(is_num(h) || is_num(l)); assert(is_num(size2) || is_vector(size2,2))
assert(is_vector(shift,2)); assert(is_num(h) || is_num(l))
assert(is_num(rounding) || is_vector(rounding,4), "Bad rounding argument."); assert(is_vector(shift,2))
assert(is_undef(rounding1) || is_num(rounding1) || is_vector(rounding1,4), "Bad rounding1 argument."); assert(is_num(rounding) || is_vector(rounding,4), "Bad rounding argument.")
assert(is_undef(rounding2) || is_num(rounding2) || is_vector(rounding2,4), "Bad rounding2 argument."); assert(is_undef(rounding1) || is_num(rounding1) || is_vector(rounding1,4), "Bad rounding1 argument.")
assert(is_num(chamfer) || is_vector(chamfer,4), "Bad chamfer argument."); assert(is_undef(rounding2) || is_num(rounding2) || is_vector(rounding2,4), "Bad rounding2 argument.")
assert(is_undef(chamfer1) || is_num(chamfer1) || is_vector(chamfer1,4), "Bad chamfer1 argument."); assert(is_num(chamfer) || is_vector(chamfer,4), "Bad chamfer argument.")
assert(is_undef(chamfer2) || is_num(chamfer2) || is_vector(chamfer2,4), "Bad chamfer2 argument."); assert(is_undef(chamfer1) || is_num(chamfer1) || is_vector(chamfer1,4), "Bad chamfer1 argument.")
assert(is_undef(chamfer2) || is_num(chamfer2) || is_vector(chamfer2,4), "Bad chamfer2 argument.");
eps = pow(2,-14); eps = pow(2,-14);
size1 = is_num(size1)? [size1,size1] : size1; size1 = is_num(size1)? [size1,size1] : size1;
size2 = is_num(size2)? [size2,size2] : size2; size2 = is_num(size2)? [size2,size2] : size2;
assert(all_nonnegative(size1)); checks2 =
assert(all_nonnegative(size2)); assert(all_nonnegative(size1))
assert(size1.x + size2.x > 0); assert(all_nonnegative(size2))
assert(size1.y + size2.y > 0); assert(size1.x + size2.x > 0)
assert(size1.y + size2.y > 0);
s1 = [max(size1.x, eps), max(size1.y, eps)]; s1 = [max(size1.x, eps), max(size1.y, eps)];
s2 = [max(size2.x, eps), max(size2.y, eps)]; s2 = [max(size2.x, eps), max(size2.y, eps)];
rounding1 = default(rounding1, rounding); rounding1 = default(rounding1, rounding);
@ -841,8 +844,9 @@ module rect_tube(
l l
) { ) {
h = one_defined([h,l],"h,l"); h = one_defined([h,l],"h,l");
assert(is_num(h), "l or h argument required."); checks =
assert(is_vector(shift,2)); assert(is_num(h), "l or h argument required.")
assert(is_vector(shift,2));
s1 = is_num(size1)? [size1, size1] : s1 = is_num(size1)? [size1, size1] :
is_vector(size1,2)? size1 : is_vector(size1,2)? size1 :
is_num(size)? [size, size] : is_num(size)? [size, size] :
@ -875,15 +879,16 @@ module rect_tube(
isize2 = is_def(is2)? is2 : isize2 = is_def(is2)? is2 :
(is_def(wall) && is_def(s2))? (s2-2*[wall,wall]) : (is_def(wall) && is_def(s2))? (s2-2*[wall,wall]) :
undef; undef;
assert(wall==undef || is_num(wall)); checks2 =
assert(size1!=undef, "Bad size/size1 argument."); assert(wall==undef || is_num(wall))
assert(size2!=undef, "Bad size/size2 argument."); assert(size1!=undef, "Bad size/size1 argument.")
assert(isize1!=undef, "Bad isize/isize1 argument."); assert(size2!=undef, "Bad size/size2 argument.")
assert(isize2!=undef, "Bad isize/isize2 argument."); assert(isize1!=undef, "Bad isize/isize1 argument.")
assert(isize1.x < size1.x, "Inner size is larger than outer size."); assert(isize2!=undef, "Bad isize/isize2 argument.")
assert(isize1.y < size1.y, "Inner size is larger than outer size."); assert(isize1.x < size1.x, "Inner size is larger than outer size.")
assert(isize2.x < size2.x, "Inner size is larger than outer size."); assert(isize1.y < size1.y, "Inner size is larger than outer size.")
assert(isize2.y < size2.y, "Inner size is larger than outer size."); assert(isize2.x < size2.x, "Inner size is larger than outer size.")
assert(isize2.y < size2.y, "Inner size is larger than outer size.");
anchor = get_anchor(anchor, center, BOT, BOT); anchor = get_anchor(anchor, center, BOT, BOT);
attachable(anchor,spin,orient, size=[each size1, h], size2=size2, shift=shift) { attachable(anchor,spin,orient, size=[each size1, h], size2=size2, shift=shift) {
diff("_H_o_L_e_") diff("_H_o_L_e_")
@ -1190,28 +1195,30 @@ module cyl(
fil1 = first_defined([rounding1, rounding]); fil1 = first_defined([rounding1, rounding]);
fil2 = first_defined([rounding2, rounding]); fil2 = first_defined([rounding2, rounding]);
if (chamfer != undef) { if (chamfer != undef) {
assert(chamfer <= r1, "chamfer is larger than the r1 radius of the cylinder."); checks =
assert(chamfer <= r2, "chamfer is larger than the r2 radius of the cylinder."); assert(chamfer <= r1, "chamfer is larger than the r1 radius of the cylinder.")
assert(chamfer <= r2, "chamfer is larger than the r2 radius of the cylinder.");
} }
if (cham1 != undef) { if (cham1 != undef) {
assert(cham1 <= r1, "chamfer1 is larger than the r1 radius of the cylinder."); check = assert(cham1 <= r1, "chamfer1 is larger than the r1 radius of the cylinder.");
} }
if (cham2 != undef) { if (cham2 != undef) {
assert(cham2 <= r2, "chamfer2 is larger than the r2 radius of the cylinder."); check = assert(cham2 <= r2, "chamfer2 is larger than the r2 radius of the cylinder.");
} }
if (rounding != undef) { if (rounding != undef) {
assert(rounding <= r1, "rounding is larger than the r1 radius of the cylinder."); checks =
assert(rounding <= r2, "rounding is larger than the r2 radius of the cylinder."); assert(rounding <= r1, "rounding is larger than the r1 radius of the cylinder.")
assert(rounding <= r2, "rounding is larger than the r2 radius of the cylinder.");
} }
if (fil1 != undef) { if (fil1 != undef) {
assert(fil1 <= r1, "rounding1 is larger than the r1 radius of the cylinder."); check = assert(fil1 <= r1, "rounding1 is larger than the r1 radius of the cylinder.");
} }
if (fil2 != undef) { if (fil2 != undef) {
assert(fil2 <= r2, "rounding2 is larger than the r1 radius of the cylinder."); check = assert(fil2 <= r2, "rounding2 is larger than the r1 radius of the cylinder.");
} }
dy1 = abs(first_defined([cham1, fil1, 0])); dy1 = abs(first_defined([cham1, fil1, 0]));
dy2 = abs(first_defined([cham2, fil2, 0])); dy2 = abs(first_defined([cham2, fil2, 0]));
assert(dy1+dy2 <= l, "Sum of fillets and chamfer sizes must be less than the length of the cylinder."); check = assert(dy1+dy2 <= l, "Sum of fillets and chamfer sizes must be less than the length of the cylinder.");
path = concat( path = concat(
[[0,l/2]], [[0,l/2]],
@ -1540,9 +1547,10 @@ module tube(
r2 = default(orr2, u_add(irr2,wall)); r2 = default(orr2, u_add(irr2,wall));
ir1 = default(irr1, u_sub(orr1,wall)); ir1 = default(irr1, u_sub(orr1,wall));
ir2 = default(irr2, u_sub(orr2,wall)); ir2 = default(irr2, u_sub(orr2,wall));
assert(all_defined([r1, r2, ir1, ir2]), "Must specify two of inner radius/diam, outer radius/diam, and wall width."); checks =
assert(ir1 <= r1, "Inner radius is larger than outer radius."); assert(all_defined([r1, r2, ir1, ir2]), "Must specify two of inner radius/diam, outer radius/diam, and wall width.")
assert(ir2 <= r2, "Inner radius is larger than outer radius."); assert(ir1 <= r1, "Inner radius is larger than outer radius.")
assert(ir2 <= r2, "Inner radius is larger than outer radius.");
sides = segs(max(r1,r2)); sides = segs(max(r1,r2));
anchor = get_anchor(anchor, center, BOT, CENTER); anchor = get_anchor(anchor, center, BOT, CENTER);
attachable(anchor,spin,orient, r1=r1, r2=r2, l=h) { attachable(anchor,spin,orient, r1=r1, r2=r2, l=h) {
@ -2608,7 +2616,6 @@ function _cut_interp(pathcut, path, data) =
// color("red")stroke(path, width=.3); // color("red")stroke(path, width=.3);
// kern = [1,1.2,1,1,.3,-.2,1,0,.8,1,1.1,1]; // kern = [1,1.2,1,1,.3,-.2,1,0,.8,1,1.1,1];
// path_text(path, "Example text", font="Courier", size=5, lettersize = 5/1.2, kern=kern, normal=UP); // path_text(path, "Example text", font="Courier", size=5, lettersize = 5/1.2, kern=kern, normal=UP);
module path_text(path, text, font, size, thickness, lettersize, offset=0, reverse=false, normal, top, center=false, textmetrics=false, kern=0) module path_text(path, text, font, size, thickness, lettersize, offset=0, reverse=false, normal, top, center=false, textmetrics=false, kern=0)
{ {
no_children($children); no_children($children);
@ -2653,29 +2660,37 @@ module path_text(path, text, font, size, thickness, lettersize, offset=0, revers
normpts = is_undef(normal) ? (reverse?1:-1)*column(pts,3) : _cut_interp(pts,path, normal); normpts = is_undef(normal) ? (reverse?1:-1)*column(pts,3) : _cut_interp(pts,path, normal);
toppts = is_undef(top) ? undef : _cut_interp(pts,path,top); toppts = is_undef(top) ? undef : _cut_interp(pts,path,top);
for(i=idx(text)) for (i = idx(text)) {
let( tangent = pts[i][2] ) tangent = pts[i][2];
assert(!usetop || !approx(tangent*toppts[i],norm(top[i])*norm(tangent)), checks =
str("Specified top direction parallel to path at character ",i)) assert(!usetop || !approx(tangent*toppts[i],norm(top[i])*norm(tangent)),
assert(usetop || !approx(tangent*normpts[i],norm(normpts[i])*norm(tangent)), str("Specified top direction parallel to path at character ",i))
str("Specified normal direction parallel to path at character ",i)) assert(usetop || !approx(tangent*normpts[i],norm(normpts[i])*norm(tangent)),
let( str("Specified normal direction parallel to path at character ",i));
adjustment = usetop ? (tangent*toppts[i])*toppts[i]/(toppts[i]*toppts[i]) adjustment = usetop ? (tangent*toppts[i])*toppts[i]/(toppts[i]*toppts[i])
: usernorm ? (tangent*normpts[i])*normpts[i]/(normpts[i]*normpts[i]) : usernorm ? (tangent*normpts[i])*normpts[i]/(normpts[i]*normpts[i])
: [0,0,0] : [0,0,0];
) move(pts[i][0]) {
move(pts[i][0]) if (dim==3) {
if(dim==3){ frame_map(
frame_map(x=tangent-adjustment, x=tangent-adjustment,
z=usetop ? undef : normpts[i], z=usetop ? undef : normpts[i],
y=usetop ? toppts[i] : undef) y=usetop ? toppts[i] : undef
up(offset-thickness/2) ) up(offset-thickness/2) {
linear_extrude(height=thickness) linear_extrude(height=thickness)
left(lsize[0]/2)text(text[i], font=font, size=size); left(lsize[0]/2)
} else { text(text[i], font=font, size=size);
frame_map(x=point3d(tangent-adjustment), y=point3d(usetop ? toppts[i] : -normpts[i])) }
left(lsize[0]/2)text(text[i], font=font, size=size); } else {
} frame_map(
x=point3d(tangent-adjustment),
y=point3d(usetop ? toppts[i] : -normpts[i])
) left(lsize[0]/2) {
text(text[i], font=font, size=size);
}
}
}
}
} }
@ -2912,8 +2927,9 @@ module ruler(length=100, width, thickness=1, depth=3, labels=false, pipscale=1/3
colors=["black","white"], alpha=1.0, unit=1, inch=false, anchor=LEFT+BACK+TOP, spin=0, orient=UP) colors=["black","white"], alpha=1.0, unit=1, inch=false, anchor=LEFT+BACK+TOP, spin=0, orient=UP)
{ {
inchfactor = 25.4; inchfactor = 25.4;
assert(depth<=5, "Cannot render scales smaller than depth=5"); checks =
assert(len(colors)==2, "colors must contain a list of exactly two colors."); assert(depth<=5, "Cannot render scales smaller than depth=5")
assert(len(colors)==2, "colors must contain a list of exactly two colors.");
length = inch ? inchfactor * length : length; length = inch ? inchfactor * length : length;
unit = inch ? inchfactor*unit : unit; unit = inch ? inchfactor*unit : unit;
maxscale = is_def(maxscale)? maxscale : floor(log(length/unit-EPSILON)); maxscale = is_def(maxscale)? maxscale : floor(log(length/unit-EPSILON));