mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-01-16 13:50:23 +01:00
add path.scad tests, shift tests to correct files
path_extrude2d bugfix
This commit is contained in:
parent
662f6c458d
commit
3a367c3faf
@ -192,7 +192,7 @@ function lerp(a,b,u) =
|
||||
// l = lerpn(0,1,6); // Returns: [0, 0.2, 0.4, 0.6, 0.8, 1]
|
||||
// l = lerpn(0,1,5,false); // Returns: [0, 0.2, 0.4, 0.6, 0.8]
|
||||
function lerpn(a,b,n,endpoint=true) =
|
||||
assert(same_shape(a,b), "Bad or inconsistent inputs to lerp")
|
||||
assert(same_shape(a,b), "Bad or inconsistent inputs to lerpn")
|
||||
assert(is_int(n))
|
||||
assert(is_bool(endpoint))
|
||||
let( d = n - (endpoint? 1 : 0) )
|
||||
|
@ -504,8 +504,8 @@ module chain_hull()
|
||||
// path_extrude2d(path, caps=false)
|
||||
// trapezoid(w1=10, w2=1, h=5, anchor=BACK);
|
||||
module path_extrude2d(path, caps=false, closed=false) {
|
||||
extra_ang = 0.1; // Extra angle for overlap of joints
|
||||
assert(caps==false || closed==false, "Cannot have caps on a closed extrusion");
|
||||
thin = 0.01;
|
||||
path = deduplicate(path);
|
||||
for (p=pair(path,wrap=closed))
|
||||
extrude_from_to(p[0],p[1]) xflip()rot(-90)children();
|
||||
@ -515,7 +515,8 @@ module path_extrude2d(path, caps=false, closed=false) {
|
||||
if (ang!=0)
|
||||
translate(t[1]) {
|
||||
frame_map(y=delt, z=UP)
|
||||
rotate_extrude(angle=ang)
|
||||
rotate(-sign(ang)*extra_ang/2)
|
||||
rotate_extrude(angle=ang+sign(ang)*extra_ang)
|
||||
if (ang<0)
|
||||
right_half(planar=true) children();
|
||||
else
|
||||
@ -523,7 +524,7 @@ module path_extrude2d(path, caps=false, closed=false) {
|
||||
}
|
||||
|
||||
}
|
||||
if (caps) {
|
||||
if (false && caps) {
|
||||
move_copies([path[0],last(path)])
|
||||
rotate_extrude()
|
||||
right_half(planar=true) children();
|
||||
|
59
paths.scad
59
paths.scad
@ -290,10 +290,8 @@ function _path_self_intersections(path, closed=true, eps=EPSILON) =
|
||||
isect = _general_line_intersection([a1,a2],[b1,b2],eps=eps)
|
||||
)
|
||||
if (isect
|
||||
// && isect[1]> (i==0 && !closed? -eps: 0) // Apparently too strict
|
||||
&& isect[1]>=-eps
|
||||
&& isect[1]<= 1+eps
|
||||
// && isect[2]> 0
|
||||
&& isect[2]>= -eps
|
||||
&& isect[2]<= 1+eps)
|
||||
[isect[0], i, isect[1], j, isect[2]]
|
||||
@ -390,32 +388,28 @@ function subdivide_path(path, N, refine, closed=true, exact=true, method="length
|
||||
assert((is_num(N) && N>0) || is_vector(N),"Parameter N to subdivide_path must be postive number or vector")
|
||||
let(
|
||||
count = len(path) - (closed?0:1),
|
||||
add_guess = method=="segment"? (
|
||||
is_list(N)? (
|
||||
assert(len(N)==count,"Vector parameter N to subdivide_path has the wrong length")
|
||||
add_guess = method=="segment"?
|
||||
(
|
||||
is_list(N)
|
||||
? assert(len(N)==count,"Vector parameter N to subdivide_path has the wrong length")
|
||||
add_scalar(N,-1)
|
||||
) : repeat((N-len(path)) / count, count)
|
||||
) : // method=="length"
|
||||
: repeat((N-len(path)) / count, count)
|
||||
)
|
||||
: // method=="length"
|
||||
assert(is_num(N),"Parameter N to subdivide path must be a number when method=\"length\"")
|
||||
let(
|
||||
path_lens = concat(
|
||||
[ for (i = [0:1:len(path)-2]) norm(path[i+1]-path[i]) ],
|
||||
closed? [norm(path[len(path)-1]-path[0])] : []
|
||||
),
|
||||
path_lens = path_segment_lengths(path,closed),
|
||||
add_density = (N - len(path)) / sum(path_lens)
|
||||
)
|
||||
path_lens * add_density,
|
||||
add = exact? _sum_preserving_round(add_guess) :
|
||||
[for (val=add_guess) round(val)]
|
||||
) concat(
|
||||
add = exact? _sum_preserving_round(add_guess)
|
||||
: [for (val=add_guess) round(val)]
|
||||
)
|
||||
[
|
||||
for (i=[0:1:count]) each [
|
||||
for(j=[0:1:add[i]])
|
||||
lerp(path[i],select(path,i+1), j/(add[i]+1))
|
||||
]
|
||||
],
|
||||
closed? [] : [last(path)]
|
||||
);
|
||||
for (i=[0:1:count-1])
|
||||
each lerpn(path[i],select(path,i+1), 1+add[i],endpoint=false),
|
||||
if (!closed) last(path)
|
||||
];
|
||||
|
||||
|
||||
|
||||
@ -465,8 +459,31 @@ function subdivide_long_segments(path, maxlen, closed=true) =
|
||||
// Arguments:
|
||||
// path = path in any dimension or a 1-region
|
||||
// N = Number of points in output
|
||||
// ---
|
||||
// spacing = Approximate spacing desired
|
||||
// closed = set to true if path is closed. Default: true
|
||||
// Example(2D): Subsampling lots of points from a smooth curve
|
||||
// path = xscale(2,circle($fn=250, r=10));
|
||||
// sampled = resample_path(path, 16);
|
||||
// stroke(path);
|
||||
// color("red")move_copies(sampled) circle($fn=16);
|
||||
// Example(2D): Specified spacing is rounded to make a uniform sampling
|
||||
// path = xscale(2,circle($fn=250, r=10));
|
||||
// sampled = resample_path(path, spacing=17);
|
||||
// stroke(path);
|
||||
// color("red")move_copies(sampled) circle($fn=16);
|
||||
// Example(2D): Notice that the corners are excluded
|
||||
// path = square(20);
|
||||
// sampled = resample_path(path, spacing=6);
|
||||
// stroke(path,closed=true);
|
||||
// color("red")move_copies(sampled) circle($fn=16);
|
||||
// Example(2D): Closed set to false
|
||||
// path = square(20);
|
||||
// sampled = resample_path(path, spacing=6,closed=false);
|
||||
// stroke(path);
|
||||
// color("red")move_copies(sampled) circle($fn=16);
|
||||
|
||||
|
||||
function resample_path(path, N, spacing, closed=true) =
|
||||
let(path = force_path(path))
|
||||
assert(is_path(path))
|
||||
|
@ -1,37 +1,6 @@
|
||||
include <../std.scad>
|
||||
|
||||
|
||||
module test_ident() {
|
||||
assert(ident(3) == [[1,0,0],[0,1,0],[0,0,1]]);
|
||||
assert(ident(4) == [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
|
||||
}
|
||||
test_ident();
|
||||
|
||||
|
||||
module test_is_2d_transform() {
|
||||
assert(!is_2d_transform(affine2d_identity()));
|
||||
assert(!is_2d_transform(affine2d_translate([5,8])));
|
||||
assert(!is_2d_transform(affine2d_scale([3,4])));
|
||||
assert(!is_2d_transform(affine2d_zrot(30)));
|
||||
assert(!is_2d_transform(affine2d_mirror([-1,1])));
|
||||
assert(!is_2d_transform(affine2d_skew(30,15)));
|
||||
|
||||
assert(is_2d_transform(affine3d_identity()));
|
||||
assert(is_2d_transform(affine3d_translate([30,40,0])));
|
||||
assert(!is_2d_transform(affine3d_translate([30,40,50])));
|
||||
assert(is_2d_transform(affine3d_scale([3,4,1])));
|
||||
assert(!is_2d_transform(affine3d_xrot(30)));
|
||||
assert(!is_2d_transform(affine3d_yrot(30)));
|
||||
assert(is_2d_transform(affine3d_zrot(30)));
|
||||
assert(is_2d_transform(affine3d_skew(sxy=2)));
|
||||
assert(is_2d_transform(affine3d_skew(syx=2)));
|
||||
assert(!is_2d_transform(affine3d_skew(szx=2)));
|
||||
assert(!is_2d_transform(affine3d_skew(szy=2)));
|
||||
}
|
||||
test_is_2d_transform();
|
||||
|
||||
|
||||
|
||||
// 2D
|
||||
|
||||
module test_affine2d_identity() {
|
||||
@ -197,64 +166,5 @@ test_affine3d_skew_yz();
|
||||
|
||||
////////////////////////////
|
||||
|
||||
module test_apply() {
|
||||
assert(approx(apply(affine3d_xrot(90),2*UP),2*FRONT));
|
||||
assert(approx(apply(affine3d_yrot(90),2*UP),2*RIGHT));
|
||||
assert(approx(apply(affine3d_zrot(90),2*UP),2*UP));
|
||||
assert(approx(apply(affine3d_zrot(90),2*RIGHT),2*BACK));
|
||||
assert(approx(apply(affine3d_zrot(90),2*BACK+2*RIGHT),2*BACK+2*LEFT));
|
||||
assert(approx(apply(affine3d_xrot(135),2*BACK+2*UP),2*sqrt(2)*FWD));
|
||||
assert(approx(apply(affine3d_yrot(135),2*RIGHT+2*UP),2*sqrt(2)*DOWN));
|
||||
assert(approx(apply(affine3d_zrot(45),2*BACK+2*RIGHT),2*sqrt(2)*BACK));
|
||||
|
||||
module check_path_apply(mat,path)
|
||||
assert_approx(apply(mat,path),path3d([for (p=path) mat*concat(p,1)]));
|
||||
|
||||
check_path_apply(xrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(yrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(zrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(rot([20,30,40])*scale([0.9,1.1,1])*move([10,20,30]), path3d(rect(100,center=true)));
|
||||
|
||||
module check_patch_apply(mat,patch)
|
||||
assert_approx(apply(mat,patch), [for (path=patch) path3d([for (p=path) mat*concat(p,1)])]);
|
||||
|
||||
flat = [for (x=[-50:25:50]) [for (y=[-50:25:50]) [x,y,0]]];
|
||||
check_patch_apply(xrot(45), flat);
|
||||
check_patch_apply(yrot(45), flat);
|
||||
check_patch_apply(zrot(45), flat);
|
||||
check_patch_apply(rot([20,30,40])*scale([0.9,1.1,1])*move([10,20,30]), flat);
|
||||
}
|
||||
test_apply();
|
||||
|
||||
|
||||
module test_rot_decode() {
|
||||
Tlist = [
|
||||
rot(37),
|
||||
xrot(49),
|
||||
yrot(88),
|
||||
rot(37,v=[1,3,3]),
|
||||
rot(41,v=[2,-3,4]),
|
||||
rot(180),
|
||||
xrot(180),
|
||||
yrot(180),
|
||||
rot(180, v=[3,2,-5], cp=[3,5,18]),
|
||||
rot(0.1, v=[1,2,3]),
|
||||
rot(-47,v=[3,4,5],cp=[9,3,4]),
|
||||
rot(197,v=[13,4,5],cp=[9,-3,4]),
|
||||
move([3,4,5]),
|
||||
move([3,4,5]) * rot(a=56, v=[5,3,-3], cp=[2,3,4]),
|
||||
ident(4)
|
||||
];
|
||||
errlist = [for(T = Tlist)
|
||||
let(
|
||||
parm = rot_decode(T),
|
||||
restore = move(parm[3])*rot(a=parm[0],v=parm[1],cp=parm[2])
|
||||
)
|
||||
norm_fro(restore-T)];
|
||||
assert(max(errlist)<1e-13);
|
||||
}
|
||||
test_rot_decode();
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
@ -280,3 +280,114 @@ module test_approx() {
|
||||
assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true);
|
||||
}
|
||||
test_approx();
|
||||
|
||||
|
||||
|
||||
module test_group_data() {
|
||||
assert_equal(group_data([1,2,0], ["A","B","C"]), [["C"],["A"],["B"]]);
|
||||
assert_equal(group_data([1,3,0], ["A","B","C"]), [["C"],["A"],[],["B"]]);
|
||||
assert_equal(group_data([5,3,1], ["A","B","C"]), [[],["C"],[],["B"],[],["A"]]);
|
||||
assert_equal(group_data([1,3,1], ["A","B","C"]), [[],["A","C"],[],["B"]]);
|
||||
}
|
||||
test_group_data();
|
||||
|
||||
|
||||
module test_compare_vals() {
|
||||
assert(compare_vals(-10,0) < 0);
|
||||
assert(compare_vals(10,0) > 0);
|
||||
assert(compare_vals(10,10) == 0);
|
||||
|
||||
assert(compare_vals("abc","abcd") < 0);
|
||||
assert(compare_vals("abcd","abc") > 0);
|
||||
assert(compare_vals("abcd","abcd") == 0);
|
||||
|
||||
assert(compare_vals(false,false) == 0);
|
||||
assert(compare_vals(true,false) > 0);
|
||||
assert(compare_vals(false,true) < 0);
|
||||
assert(compare_vals(true,true) == 0);
|
||||
|
||||
assert(compare_vals([2,3,4], [2,3,4,5]) < 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,4]) > 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,5,5]) < 0);
|
||||
assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) < 0);
|
||||
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
|
||||
assert(compare_vals([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
|
||||
|
||||
assert(compare_vals(undef, undef) == 0);
|
||||
assert(compare_vals(undef, true) < 0);
|
||||
assert(compare_vals(undef, 0) < 0);
|
||||
assert(compare_vals(undef, "foo") < 0);
|
||||
assert(compare_vals(undef, [2,3,4]) < 0);
|
||||
assert(compare_vals(undef, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(true, undef) > 0);
|
||||
assert(compare_vals(true, true) == 0);
|
||||
assert(compare_vals(true, 0) < 0);
|
||||
assert(compare_vals(true, "foo") < 0);
|
||||
assert(compare_vals(true, [2,3,4]) < 0);
|
||||
assert(compare_vals(true, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(0, undef) > 0);
|
||||
assert(compare_vals(0, true) > 0);
|
||||
assert(compare_vals(0, 0) == 0);
|
||||
assert(compare_vals(0, "foo") < 0);
|
||||
assert(compare_vals(0, [2,3,4]) < 0);
|
||||
assert(compare_vals(0, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(1, undef) > 0);
|
||||
assert(compare_vals(1, true) > 0);
|
||||
assert(compare_vals(1, 1) == 0);
|
||||
assert(compare_vals(1, "foo") < 0);
|
||||
assert(compare_vals(1, [2,3,4]) < 0);
|
||||
assert(compare_vals(1, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals("foo", undef) > 0);
|
||||
assert(compare_vals("foo", true) > 0);
|
||||
assert(compare_vals("foo", 1) > 0);
|
||||
assert(compare_vals("foo", "foo") == 0);
|
||||
assert(compare_vals("foo", [2,3,4]) < 0);
|
||||
assert(compare_vals("foo", [0:3]) < 0);
|
||||
|
||||
assert(compare_vals([2,3,4], undef) > 0);
|
||||
assert(compare_vals([2,3,4], true) > 0);
|
||||
assert(compare_vals([2,3,4], 1) > 0);
|
||||
assert(compare_vals([2,3,4], "foo") > 0);
|
||||
assert(compare_vals([2,3,4], [2,3,4]) == 0);
|
||||
assert(compare_vals([2,3,4], [0:3]) < 0);
|
||||
|
||||
assert(compare_vals([0:3], undef) > 0);
|
||||
assert(compare_vals([0:3], true) > 0);
|
||||
assert(compare_vals([0:3], 1) > 0);
|
||||
assert(compare_vals([0:3], "foo") > 0);
|
||||
assert(compare_vals([0:3], [2,3,4]) > 0);
|
||||
assert(compare_vals([0:3], [0:3]) == 0);
|
||||
}
|
||||
test_compare_vals();
|
||||
|
||||
|
||||
module test_compare_lists() {
|
||||
assert(compare_lists([2,3,4], [2,3,4,5]) < 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,4,5]) == 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,4]) > 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,5,5]) < 0);
|
||||
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
|
||||
assert(compare_lists([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
|
||||
|
||||
assert(compare_lists("cat", "bat") > 0);
|
||||
assert(compare_lists(["cat"], ["bat"]) > 0);
|
||||
}
|
||||
test_compare_lists();
|
||||
|
||||
|
@ -49,3 +49,13 @@ module test_arc() {
|
||||
assert_approx(arc($fn=24, cp=[10,10], points=[[45,45],[-25,45]], long=true), [[45,45],[53.2421021636,34.0856928585],[58.1827254512,21.3324740498],[59.4446596304,7.71403542491],[56.9315576496,-5.72987274525],[50.8352916125,-17.9728253654],[41.6213035891,-28.0800887515],[29.9930697126,-35.2799863457],[16.8383906815,-39.0228152281],[3.16160931847,-39.0228152281],[-9.9930697126,-35.2799863457],[-21.6213035891,-28.0800887515],[-30.8352916125,-17.9728253654],[-36.9315576496,-5.72987274525],[-39.4446596304,7.71403542491],[-38.1827254512,21.3324740498],[-33.2421021636,34.0856928585],[-25,45]]);
|
||||
}
|
||||
test_arc();
|
||||
|
||||
|
||||
|
||||
module test_dashed_stroke() {
|
||||
segs = dashed_stroke([[0,0],[10,0]], dashpat=[3,2], closed=false);
|
||||
assert_equal(segs,[[[0,0],[3,0]], [[5,0],[8,0]]]);
|
||||
}
|
||||
test_dashed_stroke();
|
||||
|
||||
|
||||
|
@ -55,6 +55,7 @@ test_ccw_polygon();
|
||||
test_reverse_polygon();
|
||||
|
||||
test_polygon_normal();
|
||||
test_rot_decode();
|
||||
|
||||
//tests to migrate to other files
|
||||
test_convex_distance();
|
||||
@ -966,4 +967,37 @@ module test_convex_collision() {
|
||||
}
|
||||
*test_convex_distance();
|
||||
|
||||
|
||||
|
||||
module test_rot_decode() {
|
||||
Tlist = [
|
||||
rot(37),
|
||||
xrot(49),
|
||||
yrot(88),
|
||||
rot(37,v=[1,3,3]),
|
||||
rot(41,v=[2,-3,4]),
|
||||
rot(180),
|
||||
xrot(180),
|
||||
yrot(180),
|
||||
rot(180, v=[3,2,-5], cp=[3,5,18]),
|
||||
rot(0.1, v=[1,2,3]),
|
||||
rot(-47,v=[3,4,5],cp=[9,3,4]),
|
||||
rot(197,v=[13,4,5],cp=[9,-3,4]),
|
||||
move([3,4,5]),
|
||||
move([3,4,5]) * rot(a=56, v=[5,3,-3], cp=[2,3,4]),
|
||||
ident(4)
|
||||
];
|
||||
errlist = [for(T = Tlist)
|
||||
let(
|
||||
parm = rot_decode(T),
|
||||
restore = move(parm[3])*rot(a=parm[0],v=parm[1],cp=parm[2])
|
||||
)
|
||||
norm_fro(restore-T)];
|
||||
assert(max(errlist)<1e-13);
|
||||
}
|
||||
*test_rot_decode();
|
||||
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
@ -1,5 +1,51 @@
|
||||
include <../std.scad>
|
||||
|
||||
|
||||
module test_is_matrix() {
|
||||
assert(is_matrix([[2,3,4],[5,6,7],[8,9,10]]));
|
||||
assert(is_matrix([[2,3],[5,6],[8,9]],3,2));
|
||||
assert(is_matrix([[2,3],[5,6],[8,9]],m=3,n=2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],m=2,n=3));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],2,3));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],m=2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],n=3));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],m=4));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],n=5));
|
||||
assert(!is_matrix([[2,3],[5,6],[8,9]],m=2,n=3));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],m=3,n=2));
|
||||
assert(!is_matrix([ [2,[3,4]],
|
||||
[4,[5,6]]]));
|
||||
assert(!is_matrix([[3,4],[undef,3]]));
|
||||
assert(!is_matrix([[3,4],[3,"foo"]]));
|
||||
assert(!is_matrix([[3,4],[3,3,2]]));
|
||||
assert(!is_matrix([ [3,4],6]));
|
||||
assert(!is_matrix(undef));
|
||||
assert(!is_matrix(NAN));
|
||||
assert(!is_matrix(INF));
|
||||
assert(!is_matrix(-5));
|
||||
assert(!is_matrix(0));
|
||||
assert(!is_matrix(5));
|
||||
assert(!is_matrix(""));
|
||||
assert(!is_matrix("foo"));
|
||||
assert(!is_matrix([3,4,5]));
|
||||
assert(!is_matrix([]));
|
||||
}
|
||||
test_is_matrix();
|
||||
|
||||
|
||||
|
||||
|
||||
module test_ident() {
|
||||
assert(ident(3) == [[1,0,0],[0,1,0],[0,0,1]]);
|
||||
assert(ident(4) == [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
|
||||
}
|
||||
test_ident();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module test_qr_factor() {
|
||||
// Check that R is upper triangular
|
||||
function is_ut(R) =
|
||||
|
@ -401,15 +401,6 @@ module test_list_to_matrix() {
|
||||
test_list_to_matrix();
|
||||
|
||||
|
||||
module test_group_data() {
|
||||
assert_equal(group_data([1,2,0], ["A","B","C"]), [["C"],["A"],["B"]]);
|
||||
assert_equal(group_data([1,3,0], ["A","B","C"]), [["C"],["A"],[],["B"]]);
|
||||
assert_equal(group_data([5,3,1], ["A","B","C"]), [[],["C"],[],["B"],[],["A"]]);
|
||||
assert_equal(group_data([1,3,1], ["A","B","C"]), [[],["A","C"],[],["B"]]);
|
||||
}
|
||||
test_group_data();
|
||||
|
||||
|
||||
module test_flatten() {
|
||||
assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
|
||||
assert(flatten([]) == []);
|
||||
|
@ -76,40 +76,6 @@ module test_constrain() {
|
||||
test_constrain();
|
||||
|
||||
|
||||
module test_is_matrix() {
|
||||
assert(is_matrix([[2,3,4],[5,6,7],[8,9,10]]));
|
||||
assert(is_matrix([[2,3],[5,6],[8,9]],3,2));
|
||||
assert(is_matrix([[2,3],[5,6],[8,9]],m=3,n=2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],m=2,n=3));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],2,3));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],m=2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],2));
|
||||
assert(is_matrix([[2,3,4],[5,6,7]],n=3));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],m=4));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],n=5));
|
||||
assert(!is_matrix([[2,3],[5,6],[8,9]],m=2,n=3));
|
||||
assert(!is_matrix([[2,3,4],[5,6,7]],m=3,n=2));
|
||||
assert(!is_matrix([ [2,[3,4]],
|
||||
[4,[5,6]]]));
|
||||
assert(!is_matrix([[3,4],[undef,3]]));
|
||||
assert(!is_matrix([[3,4],[3,"foo"]]));
|
||||
assert(!is_matrix([[3,4],[3,3,2]]));
|
||||
assert(!is_matrix([ [3,4],6]));
|
||||
assert(!is_matrix(undef));
|
||||
assert(!is_matrix(NAN));
|
||||
assert(!is_matrix(INF));
|
||||
assert(!is_matrix(-5));
|
||||
assert(!is_matrix(0));
|
||||
assert(!is_matrix(5));
|
||||
assert(!is_matrix(""));
|
||||
assert(!is_matrix("foo"));
|
||||
assert(!is_matrix([3,4,5]));
|
||||
assert(!is_matrix([]));
|
||||
}
|
||||
test_is_matrix();
|
||||
|
||||
|
||||
|
||||
|
||||
module test_all_integer() {
|
||||
assert(!all_integer(undef));
|
||||
@ -133,37 +99,6 @@ test_all_integer();
|
||||
|
||||
|
||||
|
||||
module test_min_index() {
|
||||
vals = rands(-100,100,100,seed=75);
|
||||
minval = min(vals);
|
||||
minidx = min_index(vals);
|
||||
assert_equal(vals[minidx], minval);
|
||||
assert_equal(min_index([3,4,5,6]), 0);
|
||||
assert_equal(min_index([4,3,5,6]), 1);
|
||||
assert_equal(min_index([4,5,3,6]), 2);
|
||||
assert_equal(min_index([4,5,6,3]), 3);
|
||||
assert_equal(min_index([6,5,4,3]), 3);
|
||||
assert_equal(min_index([6,3,4,5]), 1);
|
||||
assert_equal(min_index([-56,72,-874,5]), 2);
|
||||
}
|
||||
test_min_index();
|
||||
|
||||
|
||||
module test_max_index() {
|
||||
vals = rands(-100,100,100,seed=97);
|
||||
maxval = max(vals);
|
||||
maxidx = max_index(vals);
|
||||
assert_equal(vals[maxidx], maxval);
|
||||
assert_equal(max_index([3,4,5,6]), 3);
|
||||
assert_equal(max_index([3,4,6,5]), 2);
|
||||
assert_equal(max_index([3,6,4,5]), 1);
|
||||
assert_equal(max_index([6,3,4,5]), 0);
|
||||
assert_equal(max_index([5,6,4,3]), 1);
|
||||
assert_equal(max_index([-56,72,-874,5]), 1);
|
||||
}
|
||||
test_max_index();
|
||||
|
||||
|
||||
module test_posmod() {
|
||||
assert_equal(posmod(-5,3), 1);
|
||||
assert_equal(posmod(-4,3), 2);
|
||||
@ -248,13 +183,6 @@ test_gaussian_rands();
|
||||
|
||||
|
||||
|
||||
module test_segs() {
|
||||
assert_equal(segs(50,$fn=8), 8);
|
||||
assert_equal(segs(50,$fa=2,$fs=2), 158);
|
||||
}
|
||||
test_segs();
|
||||
|
||||
|
||||
module test_lerp() {
|
||||
assert_equal(lerp(-20,20,0), -20);
|
||||
assert_equal(lerp(-20,20,0.25), -10);
|
||||
@ -486,105 +414,6 @@ test_convolve();
|
||||
// Logic
|
||||
|
||||
|
||||
module test_compare_vals() {
|
||||
assert(compare_vals(-10,0) < 0);
|
||||
assert(compare_vals(10,0) > 0);
|
||||
assert(compare_vals(10,10) == 0);
|
||||
|
||||
assert(compare_vals("abc","abcd") < 0);
|
||||
assert(compare_vals("abcd","abc") > 0);
|
||||
assert(compare_vals("abcd","abcd") == 0);
|
||||
|
||||
assert(compare_vals(false,false) == 0);
|
||||
assert(compare_vals(true,false) > 0);
|
||||
assert(compare_vals(false,true) < 0);
|
||||
assert(compare_vals(true,true) == 0);
|
||||
|
||||
assert(compare_vals([2,3,4], [2,3,4,5]) < 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,4]) > 0);
|
||||
assert(compare_vals([2,3,4,5], [2,3,5,5]) < 0);
|
||||
assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) < 0);
|
||||
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
|
||||
assert(compare_vals([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
|
||||
|
||||
assert(compare_vals(undef, undef) == 0);
|
||||
assert(compare_vals(undef, true) < 0);
|
||||
assert(compare_vals(undef, 0) < 0);
|
||||
assert(compare_vals(undef, "foo") < 0);
|
||||
assert(compare_vals(undef, [2,3,4]) < 0);
|
||||
assert(compare_vals(undef, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(true, undef) > 0);
|
||||
assert(compare_vals(true, true) == 0);
|
||||
assert(compare_vals(true, 0) < 0);
|
||||
assert(compare_vals(true, "foo") < 0);
|
||||
assert(compare_vals(true, [2,3,4]) < 0);
|
||||
assert(compare_vals(true, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(0, undef) > 0);
|
||||
assert(compare_vals(0, true) > 0);
|
||||
assert(compare_vals(0, 0) == 0);
|
||||
assert(compare_vals(0, "foo") < 0);
|
||||
assert(compare_vals(0, [2,3,4]) < 0);
|
||||
assert(compare_vals(0, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals(1, undef) > 0);
|
||||
assert(compare_vals(1, true) > 0);
|
||||
assert(compare_vals(1, 1) == 0);
|
||||
assert(compare_vals(1, "foo") < 0);
|
||||
assert(compare_vals(1, [2,3,4]) < 0);
|
||||
assert(compare_vals(1, [0:3]) < 0);
|
||||
|
||||
assert(compare_vals("foo", undef) > 0);
|
||||
assert(compare_vals("foo", true) > 0);
|
||||
assert(compare_vals("foo", 1) > 0);
|
||||
assert(compare_vals("foo", "foo") == 0);
|
||||
assert(compare_vals("foo", [2,3,4]) < 0);
|
||||
assert(compare_vals("foo", [0:3]) < 0);
|
||||
|
||||
assert(compare_vals([2,3,4], undef) > 0);
|
||||
assert(compare_vals([2,3,4], true) > 0);
|
||||
assert(compare_vals([2,3,4], 1) > 0);
|
||||
assert(compare_vals([2,3,4], "foo") > 0);
|
||||
assert(compare_vals([2,3,4], [2,3,4]) == 0);
|
||||
assert(compare_vals([2,3,4], [0:3]) < 0);
|
||||
|
||||
assert(compare_vals([0:3], undef) > 0);
|
||||
assert(compare_vals([0:3], true) > 0);
|
||||
assert(compare_vals([0:3], 1) > 0);
|
||||
assert(compare_vals([0:3], "foo") > 0);
|
||||
assert(compare_vals([0:3], [2,3,4]) > 0);
|
||||
assert(compare_vals([0:3], [0:3]) == 0);
|
||||
}
|
||||
test_compare_vals();
|
||||
|
||||
|
||||
module test_compare_lists() {
|
||||
assert(compare_lists([2,3,4], [2,3,4,5]) < 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,4,5]) == 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,4]) > 0);
|
||||
assert(compare_lists([2,3,4,5], [2,3,5,5]) < 0);
|
||||
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
|
||||
assert(compare_lists([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
|
||||
assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
|
||||
|
||||
assert(compare_lists("cat", "bat") > 0);
|
||||
assert(compare_lists(["cat"], ["bat"]) > 0);
|
||||
}
|
||||
test_compare_lists();
|
||||
|
||||
|
||||
module test_any() {
|
||||
assert_equal(any([0,false,undef]), false);
|
||||
|
@ -1,5 +1,6 @@
|
||||
include<../std.scad>
|
||||
|
||||
|
||||
module test_is_path() {
|
||||
assert(is_path([[1,2,3],[4,5,6]]));
|
||||
assert(is_path([[1,2,3],[4,5,6],[7,8,9]]));
|
||||
@ -15,6 +16,23 @@ module test_is_path() {
|
||||
test_is_path();
|
||||
|
||||
|
||||
module test_is_1region() {
|
||||
assert(!is_1region([[3,4],[5,6],[7,8]]));
|
||||
assert(is_1region([[[3,4],[5,6],[7,8]]]));
|
||||
}
|
||||
test_is_1region();
|
||||
|
||||
|
||||
module force_path() {
|
||||
assert_equal(force_path([[3,4],[5,6],[7,8]]), [[3,4],[5,6],[7,8]]);
|
||||
assert_equal(force_path([[[3,4],[5,6],[7,8]]]), [[3,4],[5,6],[7,8]]);
|
||||
assert_equal(force_path("abc"), "abc");
|
||||
assert_equal(force_path(13), 13);
|
||||
}
|
||||
test_is_1region();
|
||||
|
||||
|
||||
|
||||
module test_is_closed_path() {
|
||||
assert(!is_closed_path([[1,2,3],[4,5,6],[1,8,9]]));
|
||||
assert(is_closed_path([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]));
|
||||
@ -40,7 +58,218 @@ module test_path_merge_collinear() {
|
||||
path = [[-20,-20], [-10,-20], [0,-10], [10,0], [20,10], [20,20], [15,30]];
|
||||
assert(path_merge_collinear(path) == [[-20,-20], [-10,-20], [20,10], [20,20], [15,30]]);
|
||||
assert(path_merge_collinear([path]) == [[-20,-20], [-10,-20], [20,10], [20,20], [15,30]]);
|
||||
sq=square(10);
|
||||
assert_equal(path_merge_collinear(subdivide_path(square(10), refine=25),closed=true), sq);
|
||||
}
|
||||
test_path_merge_collinear();
|
||||
|
||||
|
||||
module test_path_length(){
|
||||
sq = square(10);
|
||||
assert_equal(path_length(sq),30);
|
||||
assert_equal(path_length(sq,true),40);
|
||||
c = circle($fn=1000, r=1);
|
||||
assert(approx(path_length(c,closed=true), 2*PI,eps=.0001));
|
||||
}
|
||||
test_path_length();
|
||||
|
||||
|
||||
module test_path_segment_lengths(){
|
||||
sq = square(10);
|
||||
assert_equal(path_segment_lengths(sq), [10,10,10]);
|
||||
assert_equal(path_segment_lengths(sq,true), [10,10,10,10]);
|
||||
c = circle($fn=1000, r=1);
|
||||
assert(approx(path_segment_lengths(c,closed=true), repeat(2*PI/1000,1000),eps=1e-7));
|
||||
}
|
||||
test_path_segment_lengths();
|
||||
|
||||
|
||||
module test_path_length_fractions(){
|
||||
sq = square(10);
|
||||
assert_approx(path_length_fractions(sq), [0,1/3, 2/3, 1]);
|
||||
assert_approx(path_length_fractions(sq,true), [0,1/4, 2/4,3/4, 1]);
|
||||
}
|
||||
test_path_length_fractions();
|
||||
|
||||
|
||||
|
||||
module test_subdivide_path(){
|
||||
assert(approx(subdivide_path(square([2,2],center=true), 12), [[1, -1], [1/3, -1], [-1/3, -1], [-1, -1], [-1, -1/3], [-1, 1/3], [-1, 1], [-1/3, 1], [1/3, 1], [1, 1], [1, 1/3], [1, -1/3]]));
|
||||
assert_equal(subdivide_path(square([8,2],center=true), 12), [[4, -1], [2, -1], [0, -1], [-2, -1], [-4, -1], [-4, 0], [-4, 1], [-2, 1], [0, 1], [2, 1], [4, 1], [4, 0]]);
|
||||
assert_approx(subdivide_path(square([8,2],center=true), 12, method="segment"), [[4, -1], [4/3, -1], [-4/3, -1], [-4, -1], [-4, -1/3], [-4, 1/3], [-4, 1], [-4/3, 1], [4/3, 1], [4, 1], [4, 1/3], [4, -1/3]]);
|
||||
assert_approx(subdivide_path(square([2,2],center=true), 17, closed=false), [[1, -1], [0.6, -1], [0.2, -1], [-0.2, -1], [-0.6, -1], [-1, -1], [-1, -2/3], [-1, -1/3], [-1, 0], [-1, 1/3], [-1, 2/3], [-1, 1], [-0.6, 1], [-0.2, 1], [0.2, 1], [0.6, 1], [1, 1]]);
|
||||
assert_approx(subdivide_path(hexagon(side=2), [2,3,4,5,6,7], method="segment"),
|
||||
[[2, 0], [1.5, -0.866025403784], [1, -1.73205080757],
|
||||
[0.333333333333, -1.73205080757], [-0.333333333333,
|
||||
-1.73205080757], [-1, -1.73205080757], [-1.25,
|
||||
-1.29903810568], [-1.5, -0.866025403784], [-1.75,
|
||||
-0.433012701892], [-2, 0], [-1.8, 0.346410161514],
|
||||
[-1.6, 0.692820323028], [-1.4, 1.03923048454], [-1.2,
|
||||
1.38564064606], [-1, 1.73205080757], [-0.666666666667,
|
||||
1.73205080757], [-0.333333333333, 1.73205080757], [0,
|
||||
1.73205080757], [0.333333333333, 1.73205080757],
|
||||
[0.666666666667, 1.73205080757], [1, 1.73205080757],
|
||||
[1.14285714286, 1.48461497792], [1.28571428571,
|
||||
1.23717914826], [1.42857142857, 0.989743318611],
|
||||
[1.57142857143, 0.742307488958], [1.71428571429,
|
||||
0.494871659305], [1.85714285714, 0.247435829653]]);
|
||||
assert_approx(subdivide_path(pentagon(side=2), [3,4,3,4], method="segment", closed=false),
|
||||
[[1.7013016167, 0], [1.30944478184, -0.539344662917],
|
||||
[0.917587946981, -1.07868932583], [0.525731112119,
|
||||
-1.61803398875], [0.0502028539716, -1.46352549156],
|
||||
[-0.425325404176, -1.30901699437], [-0.900853662324,
|
||||
-1.15450849719], [-1.37638192047, -1], [-1.37638192047,
|
||||
-0.333333333333], [-1.37638192047, 0.333333333333],
|
||||
[-1.37638192047, 1], [-0.900853662324, 1.15450849719],
|
||||
[-0.425325404176, 1.30901699437], [0.0502028539716,
|
||||
1.46352549156], [0.525731112119, 1.61803398875]]);
|
||||
assert_approx(subdivide_path(pentagon(side=2), 17),
|
||||
[[1.7013016167, 0], [1.30944478184,
|
||||
-0.539344662917], [0.917587946981, -1.07868932583],
|
||||
[0.525731112119, -1.61803398875], [0.0502028539716,
|
||||
-1.46352549156], [-0.425325404176, -1.30901699437],
|
||||
[-0.900853662324, -1.15450849719], [-1.37638192047,
|
||||
-1], [-1.37638192047, -0.333333333333],
|
||||
[-1.37638192047, 0.333333333333], [-1.37638192047,
|
||||
1], [-0.900853662324, 1.15450849719],
|
||||
[-0.425325404176, 1.30901699437], [0.0502028539716,
|
||||
1.46352549156], [0.525731112119, 1.61803398875],
|
||||
[0.917587946981, 1.07868932583], [1.30944478184,
|
||||
0.539344662917]]);
|
||||
assert_approx(subdivide_path(pentagon(side=2), 17, exact=false),
|
||||
[[1.7013016167, 0], [1.30944478184,
|
||||
-0.539344662917], [0.917587946981, -1.07868932583],
|
||||
[0.525731112119, -1.61803398875], [-0.108306565411,
|
||||
-1.41202265917], [-0.742344242941, -1.20601132958],
|
||||
[-1.37638192047, -1], [-1.37638192047,
|
||||
-0.333333333333], [-1.37638192047, 0.333333333333],
|
||||
[-1.37638192047, 1], [-0.742344242941,
|
||||
1.20601132958], [-0.108306565411, 1.41202265917],
|
||||
[0.525731112119, 1.61803398875], [0.917587946981,
|
||||
1.07868932583], [1.30944478184, 0.539344662917]]);
|
||||
assert_approx(subdivide_path(pentagon(side=2), 18, exact=false),
|
||||
[[1.7013016167, 0], [1.40740899056,
|
||||
-0.404508497187], [1.11351636441,
|
||||
-0.809016994375], [0.819623738265,
|
||||
-1.21352549156], [0.525731112119, -1.61803398875],
|
||||
[0.0502028539716, -1.46352549156],
|
||||
[-0.425325404176, -1.30901699437],
|
||||
[-0.900853662324, -1.15450849719],
|
||||
[-1.37638192047, -1], [-1.37638192047, -0.5],
|
||||
[-1.37638192047, 0], [-1.37638192047, 0.5],
|
||||
[-1.37638192047, 1], [-0.900853662324,
|
||||
1.15450849719], [-0.425325404176, 1.30901699437],
|
||||
[0.0502028539716, 1.46352549156], [0.525731112119,
|
||||
1.61803398875], [0.819623738265, 1.21352549156],
|
||||
[1.11351636441, 0.809016994375], [1.40740899056,
|
||||
0.404508497187]]);
|
||||
assert_approx(subdivide_path([[0,0,0],[2,0,1],[2,3,2]], 12),
|
||||
[[0, 0, 0], [2/3, 0, 1/3], [4/3, 0, 2/3], [2, 0, 1], [2, 0.75, 1.25], [2, 1.5, 1.5], [2, 2.25, 1.75], [2, 3, 2], [1.6, 2.4, 1.6], [1.2, 1.8, 1.2], [0.8, 1.2, 0.8], [0.4, 0.6, 0.4]]);
|
||||
}
|
||||
test_subdivide_path();
|
||||
|
||||
|
||||
module test_subdivide_long_segments(){
|
||||
path = pentagon(d=100);
|
||||
spath = subdivide_long_segments(path, 10, closed=true);
|
||||
assert_approx(spath,
|
||||
[[50, 0], [44.2418082865, -7.92547096913], [38.4836165729,
|
||||
-15.8509419383], [32.7254248594, -23.7764129074], [26.9672331458,
|
||||
-31.7018838765], [21.2090414323, -39.6273548456], [15.4508497187,
|
||||
-47.5528258148], [6.1338998125, -44.5255652814], [-3.18305009375,
|
||||
-41.498304748], [-12.5, -38.4710442147], [-21.8169499062,
|
||||
-35.4437836813], [-31.1338998125, -32.416523148], [-40.4508497187,
|
||||
-29.3892626146], [-40.4508497187, -19.5928417431], [-40.4508497187,
|
||||
-9.79642087154], [-40.4508497187, 0], [-40.4508497187, 9.79642087154],
|
||||
[-40.4508497187, 19.5928417431], [-40.4508497187, 29.3892626146],
|
||||
[-31.1338998125, 32.416523148], [-21.8169499062, 35.4437836813],
|
||||
[-12.5, 38.4710442147], [-3.18305009375, 41.498304748], [6.1338998125,
|
||||
44.5255652814], [15.4508497187, 47.5528258148], [21.2090414323,
|
||||
39.6273548456], [26.9672331458, 31.7018838765], [32.7254248594,
|
||||
23.7764129074], [38.4836165729, 15.8509419383], [44.2418082865,
|
||||
7.92547096913]]);
|
||||
}
|
||||
test_subdivide_long_segments();
|
||||
|
||||
|
||||
module test_resample_path(){
|
||||
path = xscale(2,circle($fn=250, r=10));
|
||||
sampled = resample_path(path, 16);
|
||||
assert_approx(sampled,
|
||||
[[20, 0], [17.1657142861, -5.13020769642],
|
||||
[11.8890531315, -8.04075246881], [6.03095737128,
|
||||
-9.53380030092], [1.72917236085e-14, -9.99921044204],
|
||||
[-6.03095737128, -9.53380030092], [-11.8890531315,
|
||||
-8.04075246881], [-17.1657142861, -5.13020769642], [-20,
|
||||
-3.19176120946e-14], [-17.1657142861, 5.13020769642],
|
||||
[-11.8890531315, 8.04075246881], [-6.03095737128,
|
||||
9.53380030092], [-4.20219414821e-14, 9.99921044204],
|
||||
[6.03095737128, 9.53380030092], [11.8890531315,
|
||||
8.04075246881], [17.1657142861, 5.13020769642]]);
|
||||
path2 = square(20);
|
||||
assert_approx(resample_path(path2, spacing=6),
|
||||
[[20, 0], [13.8461538462, 0], [7.69230769231, 0], [1.53846153846, 0],
|
||||
[0, 4.61538461538], [0, 10.7692307692], [0, 16.9230769231], [3.07692307692, 20],
|
||||
[9.23076923077, 20], [15.3846153846, 20], [20, 18.4615384615], [20, 12.3076923077], [20, 6.15384615385]]);
|
||||
assert_equal(resample_path(path2, spacing=6,closed=false),[[20, 0], [14, 0], [8, 0], [2, 0], [0, 4], [0, 10], [0, 16], [2, 20], [8, 20], [14, 20], [20, 20]]);
|
||||
assert_approx(resample_path(path, spacing=17),
|
||||
[[20, 0], [8.01443073309, -9.16170407964],
|
||||
[-8.01443073309, -9.16170407964], [-20,
|
||||
-1.59309060367e-14], [-8.01443073309, 9.16170407964],
|
||||
[8.01443073309, 9.16170407964]]);
|
||||
}
|
||||
test_resample_path();
|
||||
|
||||
|
||||
module test_path_closest_point(){
|
||||
path = circle(d=100,$fn=6);
|
||||
pt = [20,10];
|
||||
closest = path_closest_point(path, pt);
|
||||
assert_approx(closest, [5, [38.1698729811, 20.4903810568]]);
|
||||
}
|
||||
test_path_closest_point();
|
||||
|
||||
|
||||
|
||||
module test_path_tangents(){
|
||||
path = circle(r=1, $fn=200);
|
||||
path_t = path_tangents(path,closed=true);
|
||||
assert_approx(path_t, hstack(column(path,1), -column(path,0)));
|
||||
rect = square([10,3]);
|
||||
tr1 = path_tangents(rect,closed=true);
|
||||
tr2 = path_tangents(rect,closed=true,uniform=false);
|
||||
tr3 = path_tangents(rect,closed=false);
|
||||
tr4 = path_tangents(rect,closed=false,uniform=false);
|
||||
assert_approx(tr1, [[-0.957826285221, -0.287347885566], [-0.957826285221, 0.287347885566], [0.957826285221, 0.287347885566], [0.957826285221, -0.287347885566]]);
|
||||
assert_approx(tr2, [[-0.707106781187, -0.707106781187], [-0.707106781187, 0.707106781187], [0.707106781187, 0.707106781187], [0.707106781187, -0.707106781187]]);
|
||||
assert_approx(tr3, [[-0.99503719021, -0.099503719021], [-0.957826285221, 0.287347885566], [0.957826285221, 0.287347885566], [0.99503719021, -0.099503719021]]);
|
||||
assert_approx(tr4, [[-1, 0], [-0.707106781187, 0.707106781187], [0.707106781187, 0.707106781187], [1, 0]]);
|
||||
}
|
||||
test_path_tangents();
|
||||
|
||||
|
||||
|
||||
module test_path_curvature(){
|
||||
c8 = path3d(circle(r=8, $fn=100));
|
||||
c28 = path3d(circle(r=28, $fn=100));
|
||||
assert(approx(path_curvature(c8,closed=true), repeat(1/8, 100), 4e-4));
|
||||
assert(approx(path_curvature(c28,closed=true), repeat(1/28, 100), 4e-4));
|
||||
}
|
||||
test_path_curvature();
|
||||
|
||||
|
||||
module test_path_torsion(){
|
||||
c = path3d(circle(r=1, $fn=100));
|
||||
tc = path_torsion(c, closed=true);
|
||||
assert(all_zero(tc));
|
||||
a=3;b=7;
|
||||
helix = [for(t=[0:1:20]) [a*cos(t), a*sin(t), b*t*PI/180]];
|
||||
th = path_torsion(helix, closed=false);
|
||||
assert(approx(th[5], b/(a*a+b*b), 1e-5));
|
||||
}
|
||||
test_path_torsion();
|
||||
|
||||
|
||||
|
||||
//echo(fmt_float(sampled));
|
||||
|
||||
|
@ -216,11 +216,4 @@ module test_mask2d_ogee() {
|
||||
test_mask2d_ogee();
|
||||
|
||||
|
||||
module test_dashed_stroke() {
|
||||
segs = dashed_stroke([[0,0],[10,0]], dashpat=[3,2], closed=false);
|
||||
assert_equal(segs,[[[0,0],[3,0]], [[5,0],[8,0]]]);
|
||||
}
|
||||
test_dashed_stroke();
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
@ -395,4 +395,61 @@ module test_skew() {
|
||||
test_skew();
|
||||
|
||||
|
||||
module test_apply() {
|
||||
assert(approx(apply(affine3d_xrot(90),2*UP),2*FRONT));
|
||||
assert(approx(apply(affine3d_yrot(90),2*UP),2*RIGHT));
|
||||
assert(approx(apply(affine3d_zrot(90),2*UP),2*UP));
|
||||
assert(approx(apply(affine3d_zrot(90),2*RIGHT),2*BACK));
|
||||
assert(approx(apply(affine3d_zrot(90),2*BACK+2*RIGHT),2*BACK+2*LEFT));
|
||||
assert(approx(apply(affine3d_xrot(135),2*BACK+2*UP),2*sqrt(2)*FWD));
|
||||
assert(approx(apply(affine3d_yrot(135),2*RIGHT+2*UP),2*sqrt(2)*DOWN));
|
||||
assert(approx(apply(affine3d_zrot(45),2*BACK+2*RIGHT),2*sqrt(2)*BACK));
|
||||
|
||||
module check_path_apply(mat,path)
|
||||
assert_approx(apply(mat,path),path3d([for (p=path) mat*concat(p,1)]));
|
||||
|
||||
check_path_apply(xrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(yrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(zrot(45), path3d(rect(100,center=true)));
|
||||
check_path_apply(rot([20,30,40])*scale([0.9,1.1,1])*move([10,20,30]), path3d(rect(100,center=true)));
|
||||
|
||||
module check_patch_apply(mat,patch)
|
||||
assert_approx(apply(mat,patch), [for (path=patch) path3d([for (p=path) mat*concat(p,1)])]);
|
||||
|
||||
flat = [for (x=[-50:25:50]) [for (y=[-50:25:50]) [x,y,0]]];
|
||||
check_patch_apply(xrot(45), flat);
|
||||
check_patch_apply(yrot(45), flat);
|
||||
check_patch_apply(zrot(45), flat);
|
||||
check_patch_apply(rot([20,30,40])*scale([0.9,1.1,1])*move([10,20,30]), flat);
|
||||
}
|
||||
test_apply();
|
||||
|
||||
|
||||
module test_is_2d_transform() {
|
||||
assert(!is_2d_transform(affine2d_identity()));
|
||||
assert(!is_2d_transform(affine2d_translate([5,8])));
|
||||
assert(!is_2d_transform(affine2d_scale([3,4])));
|
||||
assert(!is_2d_transform(affine2d_zrot(30)));
|
||||
assert(!is_2d_transform(affine2d_mirror([-1,1])));
|
||||
assert(!is_2d_transform(affine2d_skew(30,15)));
|
||||
|
||||
assert(is_2d_transform(affine3d_identity()));
|
||||
assert(is_2d_transform(affine3d_translate([30,40,0])));
|
||||
assert(!is_2d_transform(affine3d_translate([30,40,50])));
|
||||
assert(is_2d_transform(affine3d_scale([3,4,1])));
|
||||
assert(!is_2d_transform(affine3d_xrot(30)));
|
||||
assert(!is_2d_transform(affine3d_yrot(30)));
|
||||
assert(is_2d_transform(affine3d_zrot(30)));
|
||||
assert(is_2d_transform(affine3d_skew(sxy=2)));
|
||||
assert(is_2d_transform(affine3d_skew(syx=2)));
|
||||
assert(!is_2d_transform(affine3d_skew(szx=2)));
|
||||
assert(!is_2d_transform(affine3d_skew(szy=2)));
|
||||
}
|
||||
test_is_2d_transform();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
@ -76,6 +76,15 @@ module test_is_def() {
|
||||
test_is_def();
|
||||
|
||||
|
||||
module test_segs() {
|
||||
assert_equal(segs(50,$fn=8), 8);
|
||||
assert_equal(segs(50,$fa=2,$fs=2), 158);
|
||||
}
|
||||
test_segs();
|
||||
|
||||
|
||||
|
||||
|
||||
module test_is_str() {
|
||||
assert(!is_str(undef));
|
||||
assert(!is_str(true));
|
||||
|
@ -113,6 +113,40 @@ module test_v_theta() {
|
||||
test_v_theta();
|
||||
|
||||
|
||||
module test_min_index() {
|
||||
vals = rands(-100,100,100,seed=75);
|
||||
minval = min(vals);
|
||||
minidx = min_index(vals);
|
||||
assert_equal(vals[minidx], minval);
|
||||
assert_equal(min_index([3,4,5,6]), 0);
|
||||
assert_equal(min_index([4,3,5,6]), 1);
|
||||
assert_equal(min_index([4,5,3,6]), 2);
|
||||
assert_equal(min_index([4,5,6,3]), 3);
|
||||
assert_equal(min_index([6,5,4,3]), 3);
|
||||
assert_equal(min_index([6,3,4,5]), 1);
|
||||
assert_equal(min_index([-56,72,-874,5]), 2);
|
||||
}
|
||||
test_min_index();
|
||||
|
||||
|
||||
module test_max_index() {
|
||||
vals = rands(-100,100,100,seed=97);
|
||||
maxval = max(vals);
|
||||
maxidx = max_index(vals);
|
||||
assert_equal(vals[maxidx], maxval);
|
||||
assert_equal(max_index([3,4,5,6]), 3);
|
||||
assert_equal(max_index([3,4,6,5]), 2);
|
||||
assert_equal(max_index([3,6,4,5]), 1);
|
||||
assert_equal(max_index([6,3,4,5]), 0);
|
||||
assert_equal(max_index([5,6,4,3]), 1);
|
||||
assert_equal(max_index([-56,72,-874,5]), 1);
|
||||
}
|
||||
test_max_index();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module test_unit() {
|
||||
assert(unit([10,0,0]) == [1,0,0]);
|
||||
assert(unit([0,10,0]) == [0,1,0]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user