Merge branch 'master' into master

This commit is contained in:
RonaldoCMP
2021-03-25 13:40:09 +00:00
committed by GitHub
84 changed files with 8808 additions and 5346 deletions

View File

@@ -31,8 +31,21 @@ module test_is_2d_transform() {
test_is_2d_transform();
module test_is_affine() {
assert(is_affine(affine2d_scale([2,3])));
assert(is_affine(affine3d_scale([2,3,4])));
assert(!is_affine(affine3d_scale([2,3,4]),2));
assert(is_affine(affine2d_scale([2,3]),2));
assert(is_affine(affine3d_scale([2,3,4]),3));
assert(!is_affine(affine2d_scale([2,3]),3));
}
test_is_affine();
module test_affine2d_to_3d() {
assert(affine2d_to_3d(affine2d_identity()) == affine3d_identity());
assert(affine2d_to_3d(affine2d_translate([30,40])) == affine3d_translate([30,40,0]));
assert(affine2d_to_3d(affine2d_scale([3,4])) == affine3d_scale([3,4,1]));
assert(affine2d_to_3d(affine2d_zrot(30)) == affine3d_zrot(30));
}
test_affine2d_to_3d();
@@ -88,15 +101,6 @@ module test_affine2d_skew() {
test_affine2d_skew();
module test_affine2d_chain() {
t = affine2d_translate([15,30]);
s = affine2d_scale([1.5,2]);
r = affine2d_zrot(30);
assert(affine2d_chain([t,s,r]) == r * s * t);
}
test_affine2d_chain();
// 3D
module test_affine3d_identity() {
@@ -210,21 +214,12 @@ module test_affine3d_skew_yz() {
test_affine3d_skew_yz();
module test_affine3d_chain() {
t = affine3d_translate([15,30,23]);
s = affine3d_scale([1.5,2,1.8]);
r = affine3d_zrot(30);
assert(affine3d_chain([t,s,r]) == r * s * t);
}
test_affine3d_chain();
////////////////////////////
module test_affine_frame_map() {
assert(approx(affine_frame_map(x=[1,1,0], y=[-1,1,0]), affine3d_zrot(45)));
module test_affine3d_frame_map() {
assert(approx(affine3d_frame_map(x=[1,1,0], y=[-1,1,0]), affine3d_zrot(45)));
}
test_affine_frame_map();
test_affine3d_frame_map();
module test_apply() {
@@ -236,22 +231,27 @@ module test_apply() {
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_apply_list() {
assert(approx(apply_list(25*(BACK+UP), []), 25*(BACK+UP)));
assert(approx(apply_list(25*(BACK+UP), [affine3d_xrot(135)]), 25*sqrt(2)*FWD));
assert(approx(apply_list(25*(RIGHT+UP), [affine3d_yrot(135)]), 25*sqrt(2)*DOWN));
assert(approx(apply_list(25*(BACK+RIGHT), [affine3d_zrot(45)]), 25*sqrt(2)*BACK));
assert(approx(apply_list(25*(BACK+UP), [affine3d_xrot(135), affine3d_translate([30,40,50])]), 25*sqrt(2)*FWD+[30,40,50]));
assert(approx(apply_list(25*(RIGHT+UP), [affine3d_yrot(135), affine3d_translate([30,40,50])]), 25*sqrt(2)*DOWN+[30,40,50]));
assert(approx(apply_list(25*(BACK+RIGHT), [affine3d_zrot(45), affine3d_translate([30,40,50])]), 25*sqrt(2)*BACK+[30,40,50]));
}
test_apply_list();
module test_rot_decode() {
Tlist = [
rot(37),

View File

@@ -167,8 +167,17 @@ test_deduplicate_indexed();
module test_list_set() {
assert(list_set([2,3,4,5], 2, 21) == [2,3,21,5]);
assert(list_set([2,3,4,5], [1,3], [81,47]) == [2,81,4,47]);
assert_equal(list_set([2,3,4,5], 2, 21), [2,3,21,5]);
assert_equal(list_set([2,3,4,5], [1,3], [81,47]), [2,81,4,47]);
assert_equal(list_set([2,3,4,5], [2], [21]), [2,3,21,5]);
assert_equal(list_set([1,2,3], [], []), [1,2,3]);
assert_equal(list_set([1,2,3], [1,5], [4,4]), [1,4,3,0,0,4]);
assert_equal(list_set([1,2,3], [1,5], [4,4],dflt=12), [1,4,3,12,12,4]);
assert_equal(list_set([1,2,3], [1,2], [4,4],dflt=12, minlen=5), [1,4,4,12,12]);
assert_equal(list_set([1,2,3], 1, 4, dflt=12, minlen=5), [1,4,3,12,12]);
assert_equal(list_set([1,2,3], [],[],dflt=12, minlen=5), [1,2,3,12,12]);
assert_equal(list_set([1,2,3], 5,9), [1,2,3,0,0,9]);
assert_equal(list_set([1,2,3], 5,9,dflt=12), [1,2,3,12,12,9]);
}
test_list_set();
@@ -176,6 +185,8 @@ test_list_set();
module test_list_remove() {
assert(list_remove([3,6,9,12],1) == [3,9,12]);
assert(list_remove([3,6,9,12],[1,3]) == [3,9]);
assert(list_remove([3,6,9],[]) == [3,6,9]);
assert(list_remove([],[]) == []);
}
test_list_remove();
@@ -191,8 +202,12 @@ test_list_remove_values();
module test_list_insert() {
assert(list_insert([3,6,9,12],1,5) == [3,5,6,9,12]);
assert(list_insert([3,6,9,12],[1,3],[5,11]) == [3,5,6,9,11,12]);
assert_equal(list_insert([3,6,9,12],1,5),[3,5,6,9,12]);
assert_equal(list_insert([3,6,9,12],[1,3],[5,11]),[3,5,6,9,11,12]);
assert_equal(list_insert([3],1,4), [3,4]);
assert_equal(list_insert([3],[0,1], [1,2]), [1,3,2]);
assert_equal(list_insert([1,2,3],[],[]),[1,2,3]);
assert_equal(list_insert([], 0, 4),[4]);
}
test_list_insert();
@@ -251,9 +266,9 @@ test_list_fit();
module test_idx() {
colors = ["red", "green", "blue", "cyan"];
assert([for (i=idx(colors)) i] == [0,1,2,3]);
assert([for (i=idx(colors,end=-2)) i] == [0,1,2]);
assert([for (i=idx(colors,start=1)) i] == [1,2,3]);
assert([for (i=idx(colors,start=1,end=-2)) i] == [1,2]);
assert([for (i=idx(colors,e=-2)) i] == [0,1,2]);
assert([for (i=idx(colors,s=1)) i] == [1,2,3]);
assert([for (i=idx(colors,s=1,e=-2)) i] == [1,2]);
}
test_idx();
@@ -434,36 +449,30 @@ test_force_list();
module test_pair() {
assert(pair([3,4,5,6]) == [[3,4], [4,5], [5,6]]);
assert(pair("ABCD") == [["A","B"], ["B","C"], ["C","D"]]);
assert(pair([3,4,5,6],true) == [[3,4], [4,5], [5,6], [6,3]]);
assert(pair("ABCD",true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
assert(pair([3,4,5,6],wrap=true) == [[3,4], [4,5], [5,6], [6,3]]);
assert(pair("ABCD",wrap=true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
}
test_pair();
module test_pair_wrap() {
assert(pair_wrap([3,4,5,6]) == [[3,4], [4,5], [5,6], [6,3]]);
assert(pair_wrap("ABCD") == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
}
test_pair_wrap();
module test_triplet() {
assert(triplet([3,4,5,6,7]) == [[3,4,5], [4,5,6], [5,6,7]]);
assert(triplet("ABCDE") == [["A","B","C"], ["B","C","D"], ["C","D","E"]]);
assert(triplet([3,4,5,6],true) == [[3,4,5], [4,5,6], [5,6,3], [6,3,4]]);
assert(triplet("ABCD",true) == [["A","B","C"], ["B","C","D"], ["C","D","A"], ["D","A","B"]]);
assert(triplet([3,4,5,6],wrap=true) == [[3,4,5], [4,5,6], [5,6,3], [6,3,4]]);
assert(triplet("ABCD",wrap=true) == [["A","B","C"], ["B","C","D"], ["C","D","A"], ["D","A","B"]]);
}
test_triplet();
module test_triplet_wrap() {
assert(triplet_wrap([3,4,5,6]) == [[3,4,5], [4,5,6], [5,6,3], [6,3,4]]);
assert(triplet_wrap("ABCD") == [["A","B","C"], ["B","C","D"], ["C","D","A"], ["D","A","B"]]);
module test_combinations() {
assert(combinations([3,4,5,6]) == [[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]);
assert(combinations([3,4,5,6],n=3) == [[3,4,5],[3,4,6],[3,5,6],[4,5,6]]);
}
test_triplet_wrap();
module test_permute() {
assert(permute([3,4,5,6]) == [[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]);
assert(permute([3,4,5,6],n=3) == [[3,4,5],[3,4,6],[3,5,6],[4,5,6]]);
}
test_permute();
test_combinations();
module test_repeat_entries() {
@@ -486,15 +495,36 @@ module test_zip() {
assert(zip([v1,v2],fit="long", fill=0) == [[1,5],[2,6],[3,7],[4,0]]);
assert(zip([v1,v2,v3],fit="long") == [[1,5,8],[2,6,9],[3,7,10],[4,undef,11]]);
}
test_zip();
//test_zip();
module test_hstack() {
M = ident(3);
v1 = [2,3,4];
v2 = [5,6,7];
v3 = [8,9,10];
a = hstack(v1,v2);
b = hstack(v1,v2,v3);
c = hstack([M,v1,M]);
d = hstack(subindex(M,0), subindex(M,[1, 2]));
assert_equal(a,[[2, 5], [3, 6], [4, 7]]);
assert_equal(b,[[2, 5, 8], [3, 6, 9], [4, 7, 10]]);
assert_equal(c,[[1, 0, 0, 2, 1, 0, 0], [0, 1, 0, 3, 0, 1, 0], [0, 0, 1, 4, 0, 0, 1]]);
assert_equal(d,M);
strmat = [["three","four"], ["five","six"]];
assert_equal(hstack(strmat,strmat), [["three", "four", "three", "four"], ["five", "six", "five", "six"]]);
strvec = ["one","two"];
assert_equal(hstack(strvec,strmat),[["o", "n", "e", "three", "four"], ["t", "w", "o", "five", "six"]]);
}
test_hstack();
module test_block_matrix() {
A = [[1,2],[3,4]];
B = ident(2);
assert_equal(block_matrix([[A,B],[B,A],[A,B]]), [[1,2,1,0],[3,4,0,1],[1,0,1,2],[0,1,3,4],[1,2,1,0],[3,4,0,1]]);
assert_equal(block_matrix([[A,B],ident(4)]), [[1,2,1,0],[3,4,0,1],[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
text = [["a","b"],["c","d"]];
assert_equal(block_matrix([[text,B]]), [["a","b",1,0],["c","d",0,1]]);
text = [["aa","bb"],["cc","dd"]];
assert_equal(block_matrix([[text,B]]), [["aa","bb",1,0],["cc","dd",0,1]]);
}
test_block_matrix();

View File

@@ -217,16 +217,6 @@ module test_valid_range() {
}
test_valid_range();
module test_is_list_of() {
assert(is_list_of([3,4,5], 0));
assert(!is_list_of([3,4,undef], 0));
assert(is_list_of([[3,4],[4,5]], [1,1]));
assert(!is_list_of([[3,4], 6, [4,5]], [1,1]));
assert(is_list_of([[1,[3,4]], [4,[5,6]]], [1,[2,3]]));
assert(!is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]));
}
test_is_list_of();
module test_is_consistent() {
assert(is_consistent([]));
assert(is_consistent([[],[]]));
@@ -238,6 +228,13 @@ module test_is_consistent() {
assert(!is_consistent([[3,4,5],[3,4]]));
assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]));
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));
assert(is_consistent([3,4,5], 0));
assert(!is_consistent([3,4,undef], 0));
assert(is_consistent([[3,4],[4,5]], [1,1]));
assert(!is_consistent([[3,4], 6, [4,5]], [1,1]));
assert(is_consistent([[1,[3,4]], [4,[5,6]]], [1,[2,3]]));
assert(!is_consistent([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]));
}
test_is_consistent();
@@ -245,6 +242,12 @@ test_is_consistent();
module test_same_shape() {
assert(same_shape([3,[4,5]],[7,[3,4]]));
assert(!same_shape([3,4,5], [7,[3,4]]));
assert(!same_shape([3,4,5],undef));
assert(!same_shape([5,3],3));
assert(!same_shape(undef,[3,4]));
assert(same_shape(4,5));
assert(!same_shape(5,undef));
}
test_same_shape();
@@ -276,10 +279,14 @@ test_first_defined();
module test_one_defined() {
assert_equal(one_defined([27,undef,undef], "length,L,l") ,27);
assert_equal(one_defined([undef,28,undef], "length,L,l") ,28);
assert_equal(one_defined([undef,undef,29], "length,L,l") ,29);
assert_equal(one_defined([undef,undef,undef], "length,L,l", dflt=undef), undef);
assert_equal(one_defined([27,undef,undef], ["length","L","l"]) ,27);
assert_equal(one_defined([undef,28,undef], ["length","L","l"]) ,28);
assert_equal(one_defined([undef,undef,29], ["length","L","l"]) ,29);
assert_equal(one_defined([undef,undef,undef], ["length","L","l"], required=false), undef);
assert_equal(one_defined([undef,undef,undef], ["length","L","l"], dflt=undef), undef);
}
test_one_defined();
@@ -361,19 +368,6 @@ module test_get_radius() {
test_get_radius();
module test_get_height() {
assert(get_height(h=undef, l=undef, height=undef, dflt=undef) == undef);
assert(get_height(h=undef, l=undef, height=undef, dflt=23) == 23);
assert(get_height(h=undef, l=undef, height=50, dflt=23) == 50);
assert(get_height(h=undef, l=50, height=undef, dflt=23) == 50);
assert(get_height(h=50, l=undef, height=undef, dflt=23) == 50);
assert(get_height(h=undef, l=undef, height=75, dflt=23) == 75);
assert(get_height(h=undef, l=75, height=undef, dflt=23) == 75);
assert(get_height(h=75, l=undef, height=undef, dflt=23) == 75);
}
test_get_height();
module test_scalar_vec3() {
assert(scalar_vec3(undef) == undef);
assert(scalar_vec3(3) == [3,3,3]);

View File

@@ -47,7 +47,6 @@ test_projection_on_plane();
test_plane_point_nearest_origin();
test_distance_from_plane();
test_closest_point_on_plane();
test__general_plane_line_intersection();
test_plane_line_angle();
test_normalize_plane();
@@ -111,15 +110,6 @@ function info_str(list,i=0,string=chr(10)) =
: info_str(list,i+1,str(string,str(list[i][0],_valstr(list[i][1]),chr(10))));
module test_closest_point_on_plane(){
plane = rands(-5,5,4,seed=175)+[10,0,0,0];
point = rands(-1,1,3,seed=477);
point2 = closest_point_on_plane(plane,point);
assert_approx(norm(point-point2), abs(distance_from_plane(plane,point)));
}
*test_closest_point_on_plane();
module test_normalize_plane(){
plane = rands(-5,5,4,seed=333)+[10,0,0,0];
plane2 = normalize_plane(plane);
@@ -394,7 +384,7 @@ module test_line_normal() {
assert(line_normal([[0,0],[0,-10]]) == [1,0]);
assert(approx(line_normal([[0,0],[10,10]]), [-sqrt(2)/2,sqrt(2)/2]));
pts = [for (p=pair(rands(-100,100,1000,seed_value=4312))) p];
for (p = pair_wrap(pts)) {
for (p = pair(pts,true)) {
p1 = p.x;
p2 = p.y;
n = unit(p2-p1);
@@ -606,7 +596,10 @@ module test_circle_point_tangents() {
[[0,0], 50, [50*sqrt(2),0], [polar_to_xy(50,45), polar_to_xy(50,-45)]],
[[5,10], 50, [5+50*sqrt(2),10], [[5,10]+polar_to_xy(50,45), [5,10]+polar_to_xy(50,-45)]],
[[0,0], 50, [0,50*sqrt(2)], [polar_to_xy(50,135), polar_to_xy(50,45)]],
[[5,10], 50, [5,10+50*sqrt(2)], [[5,10]+polar_to_xy(50,135), [5,10]+polar_to_xy(50,45)]]
[[5,10], 50, [5,10+50*sqrt(2)], [[5,10]+polar_to_xy(50,135), [5,10]+polar_to_xy(50,45)]],
[[5,10], 50, [5,10+50*sqrt(2)], [[5,10]+polar_to_xy(50,135), [5,10]+polar_to_xy(50,45)]],
[[5,10], 50, [5, 60], [[5, 60]]],
[[5,10], 50, [5, 59], []],
];
for (v = testvals) {
cp = v[0]; r = v[1]; pt = v[2]; expect = v[3];
@@ -619,7 +612,7 @@ module test_circle_point_tangents() {
module test_tri_calc() {
sides = rands(1,100,100,seed_value=8888);
for (p=pair_wrap(sides)) {
for (p=pair(sides,true)) {
opp = p[0];
adj = p[1];
hyp = norm([opp,adj]);
@@ -642,7 +635,7 @@ module test_tri_calc() {
module test_tri_functions() {
sides = rands(1,100,100,seed_value=8181);
for (p = pair_wrap(sides)) {
for (p = pair(sides,true)) {
adj = p.x;
opp = p.y;
hyp = norm([opp,adj]);

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

View File

@@ -83,6 +83,12 @@ module test_is_matrix() {
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));
@@ -824,19 +830,78 @@ module test_lcm() {
test_lcm();
module test_C_times() {
assert_equal(C_times([4,5],[9,-4]), [56,29]);
assert_equal(C_times([-7,2],[24,3]), [-174, 27]);
module test_c_mul() {
assert_equal(c_mul([4,5],[9,-4]), [56,29]);
assert_equal(c_mul([-7,2],[24,3]), [-174, 27]);
assert_equal(c_mul([3,4], [[3,-7], [4,9], [4,8]]), [[37,-9],[-24,43], [-20,40]]);
assert_equal(c_mul([[3,-7], [4,9], [4,8]], [[1,1],[3,4],[-3,4]]), [-58,31]);
M = [
[ [3,4], [9,-1], [4,3] ],
[ [2,9], [4,9], [3,-1] ]
];
assert_equal(c_mul(M, [ [3,4], [4,4],[5,5]]), [[38,91], [-30, 97]]);
assert_equal(c_mul([[4,4],[9,1]], M), [[5,111],[67,117], [32,22]]);
assert_equal(c_mul(M,transpose(M)), [ [[80,30], [30, 117]], [[30,117], [-134, 102]]]);
assert_equal(c_mul(transpose(M),M), [ [[-84,60],[-42,87],[15,50]], [[-42,87],[15,54],[60,46]], [[15,50],[60,46],[15,18]]]);
}
test_C_times();
test_c_mul();
module test_C_div() {
assert_equal(C_div([56,29],[9,-4]), [4,5]);
assert_equal(C_div([-174,27],[-7,2]), [24,3]);
module test_c_div() {
assert_equal(c_div([56,29],[9,-4]), [4,5]);
assert_equal(c_div([-174,27],[-7,2]), [24,3]);
}
test_C_div();
test_c_div();
module test_c_conj(){
assert_equal(c_conj([3,4]), [3,-4]);
assert_equal(c_conj( [ [2,9], [4,9], [3,-1] ]), [ [2,-9], [4,-9], [3,1] ]);
M = [
[ [3,4], [9,-1], [4,3] ],
[ [2,9], [4,9], [3,-1] ]
];
Mc = [
[ [3,-4], [9,1], [4,-3] ],
[ [2,-9], [4,-9], [3,1] ]
];
assert_equal(c_conj(M), Mc);
}
test_c_conj();
module test_c_real(){
M = [
[ [3,4], [9,-1], [4,3] ],
[ [2,9], [4,9], [3,-1] ]
];
assert_equal(c_real(M), [[3,9,4],[2,4,3]]);
assert_equal(c_real( [ [3,4], [9,-1], [4,3] ]), [3,9,4]);
assert_equal(c_real([3,4]),3);
}
test_c_real();
module test_c_imag(){
M = [
[ [3,4], [9,-1], [4,3] ],
[ [2,9], [4,9], [3,-1] ]
];
assert_equal(c_imag(M), [[4,-1,3],[9,9,-1]]);
assert_equal(c_imag( [ [3,4], [9,-1], [4,3] ]), [4,-1,3]);
assert_equal(c_imag([3,4]),4);
}
test_c_imag();
module test_c_ident(){
assert_equal(c_ident(3), [[[1, 0], [0, 0], [0, 0]], [[0, 0], [1, 0], [0, 0]], [[0, 0], [0, 0], [1, 0]]]);
}
test_c_ident();
module test_c_norm(){
assert_equal(c_norm([3,4]), 5);
assert_approx(c_norm([[3,4],[5,6]]), 9.273618495495704);
}
test_c_norm();
module test_back_substitute(){
R = [[12,4,3,2],

View File

@@ -39,5 +39,37 @@ module test_spheroid() {
test_spheroid();
module test_cyl() {
$fn=12;
shape_compare() {
cyl(r=50,l=10,circum=true,anchor=BOTTOM);
cylinder(r=50/cos(180/12),l=10);
}
shape_compare() {
cyl(r=50,l=10,circum=false,anchor=BOTTOM);
cylinder(r=50,l=10);
}
shape_compare() {
cyl(r=50,l=10,chamfer=1,circum=true,anchor=BOTTOM);
union() {
r=50/cos(180/12);
cylinder(r1=r-1,r2=r,l=1);
up(1) cylinder(r=r,l=8);
up(9) cylinder(r1=r,r2=r-1,l=1);
}
}
shape_compare() {
cyl(r=50,l=10,chamfer=1,circum=false,anchor=BOTTOM);
union() {
r=50;
cylinder(r1=r-1,r2=r,l=1);
up(1) cylinder(r=r,l=8);
up(9) cylinder(r1=r,r2=r-1,l=1);
}
}
}
test_cyl();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View File

@@ -142,6 +142,15 @@ module test_supershape() {
test_supershape();
module test_reuleaux_polygon() {
$fn=36;
assert_approx(reuleaux_polygon(N=3, r=50),[[50,0],[45.5443467787,-6.93313174371],[40.449833029,-13.4113329645],[34.7625954562,-19.375936069],[28.5341385645,-24.7729246878],[21.8208682239,-29.5534228563],[14.6835808504,-33.6741376427],[7.18691282348,-37.0977512159],[-0.601244870218,-39.7932588011],[-8.61036146861,-41.7362494642],[-16.7679051715,-42.909127181],[-25,-43.3012701892],[-28.7764416072,-35.9759954373],[-31.8394715604,-28.3249164997],[-34.161350586,-20.417322732],[-35.7210513879,-12.324826528],[-36.5044490743,-4.12071478641],[-36.5044490743,4.12071478641],[-35.7210513879,12.324826528],[-34.161350586,20.417322732],[-31.8394715604,28.3249164997],[-28.7764416072,35.9759954373],[-25,43.3012701892],[-16.7679051715,42.909127181],[-8.61036146861,41.7362494642],[-0.601244870218,39.7932588011],[7.18691282348,37.0977512159],[14.6835808504,33.6741376427],[21.8208682239,29.5534228563],[28.5341385645,24.7729246878],[34.7625954562,19.375936069],[40.449833029,13.4113329645],[45.5443467787,6.93313174371]]);
assert_approx(reuleaux_polygon(N=3, d=100),[[50,0],[45.5443467787,-6.93313174371],[40.449833029,-13.4113329645],[34.7625954562,-19.375936069],[28.5341385645,-24.7729246878],[21.8208682239,-29.5534228563],[14.6835808504,-33.6741376427],[7.18691282348,-37.0977512159],[-0.601244870218,-39.7932588011],[-8.61036146861,-41.7362494642],[-16.7679051715,-42.909127181],[-25,-43.3012701892],[-28.7764416072,-35.9759954373],[-31.8394715604,-28.3249164997],[-34.161350586,-20.417322732],[-35.7210513879,-12.324826528],[-36.5044490743,-4.12071478641],[-36.5044490743,4.12071478641],[-35.7210513879,12.324826528],[-34.161350586,20.417322732],[-31.8394715604,28.3249164997],[-28.7764416072,35.9759954373],[-25,43.3012701892],[-16.7679051715,42.909127181],[-8.61036146861,41.7362494642],[-0.601244870218,39.7932588011],[7.18691282348,37.0977512159],[14.6835808504,33.6741376427],[21.8208682239,29.5534228563],[28.5341385645,24.7729246878],[34.7625954562,19.375936069],[40.449833029,13.4113329645],[45.5443467787,6.93313174371]]);
assert_approx(reuleaux_polygon(N=5, d=100),[[50,0],[47.0014382812,-7.98963912753],[43.2987621605,-15.6783253167],[38.921783409,-23.0041537871],[33.9057428858,-29.9081412755],[28.2910268,-36.3347009341],[22.122841544,-42.2320898832],[15.4508497187,-47.5528258148],[6.92564483387,-47.1699584219],[-1.53092011402,-46.0244388665],[-9.85075783633,-44.1254901984],[-17.9668818917,-41.4884016357],[-25.8139460215,-38.134405465],[-33.3287702792,-34.0905060913],[-40.4508497187,-29.3892626146],[-42.7211543799,-21.1629984251],[-44.2449228251,-12.7663422159],[-45.0098865668,-4.26689892573],[-45.0098865668,4.26689892573],[-44.2449228251,12.7663422159],[-42.7211543799,21.1629984251],[-40.4508497187,29.3892626146],[-33.3287702792,34.0905060913],[-25.8139460215,38.134405465],[-17.9668818917,41.4884016357],[-9.85075783633,44.1254901984],[-1.53092011402,46.0244388665],[6.92564483387,47.1699584219],[15.4508497187,47.5528258148],[22.122841544,42.2320898832],[28.2910268,36.3347009341],[33.9057428858,29.9081412755],[38.921783409,23.0041537871],[43.2987621605,15.6783253167],[47.0014382812,7.98963912753]]);
}
test_reuleaux_polygon();
module test_mask2d_chamfer() {
assert_approx(mask2d_chamfer(x=10),[[10,-0.01],[-0.01,-0.01],[-0.01,10],[0,10],[10,0]]);
assert_approx(mask2d_chamfer(y=10),[[10,-0.01],[-0.01,-0.01],[-0.01,10],[0,10],[10,0]]);

View File

@@ -368,5 +368,23 @@ test_echofmt();
*/
module test_str_pad() {
assert_equal(str_pad("abc",5,"x"), "abcxx");
assert_equal(str_pad("abc",5), "abc ");
assert_equal(str_pad("abc",5,"x",left=true), "xxabc");
assert_equal(str_pad("", 5, "x"), "xxxxx");
assert_equal(str_pad("", 5, "x", left=true), "xxxxx");
}
test_str_pad();
module test_str_replace_char() {
assert_equal(str_replace_char("abcabc", "b", "xyz"), "axyzcaxyzc");
assert_equal(str_replace_char("abcabc", "b", ""), "acac");
assert_equal(str_replace_char("", "b", "xyz"), "");
assert_equal(str_replace_char("acdacd", "b", "xyz"), "acdacd");
}
test_str_replace_char();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View File

@@ -259,21 +259,13 @@ module test_rot() {
for (xa=angs, ya=angs, za=angs) {
assert_equal(
rot([xa,ya,za]),
affine3d_chain([
affine3d_xrot(xa),
affine3d_yrot(ya),
affine3d_zrot(za)
]),
affine3d_zrot(za) * affine3d_yrot(ya) * affine3d_xrot(xa),
info=str("[X,Y,Z] = ",[xa,ya,za])
);
assert_equal(
rot([xa,ya,za],p=pts3d),
apply(
affine3d_chain([
affine3d_xrot(xa),
affine3d_yrot(ya),
affine3d_zrot(za)
]),
affine3d_zrot(za) * affine3d_yrot(ya) * affine3d_xrot(xa),
pts3d
),
info=str("[X,Y,Z] = ",[xa,ya,za], ", p=...")
@@ -312,10 +304,7 @@ module test_rot() {
for (a = angs) {
assert_equal(
rot(from=vec1, to=vec2, a=a),
affine3d_chain([
affine3d_zrot(a),
affine3d_rot_from_to(vec1,vec2)
]),
affine3d_rot_from_to(vec1,vec2) * affine3d_rot_by_axis(vec1,a),
info=str(
"from = ", vec1, ", ",
"to = ", vec2, ", ",
@@ -325,10 +314,7 @@ module test_rot() {
assert_equal(
rot(from=vec1, to=vec2, a=a, p=pts3d),
apply(
affine3d_chain([
affine3d_zrot(a),
affine3d_rot_from_to(vec1,vec2)
]),
affine3d_rot_from_to(vec1,vec2) * affine3d_rot_by_axis(vec1,a),
pts3d
),
info=str(
@@ -395,7 +381,7 @@ module test_skew() {
assert_equal(skew(sxy=2, sxz=3, syx=4, syz=5, szx=6, szy=7), m);
assert_equal(skew(sxy=2, sxz=3, syx=4, syz=5, szx=6, szy=7, p=[1,2,3]), apply(m,[1,2,3]));
// Verify that module at least doesn't crash.
skew(2,3,4,5,6,7) nil();
skew(undef,2,3,4,5,6,7) nil();
}
test_skew();

View File

@@ -11,6 +11,10 @@ module test_is_vector() {
assert(is_vector(1) == false);
assert(is_vector("foo") == false);
assert(is_vector(true) == false);
assert(is_vector([3,4,"foo"]) == false);
assert(is_vector([3,4,[4,5]]) == false);
assert(is_vector([3,4,undef]) == false);
assert(is_vector(["foo","bar"]) == false);
assert(is_vector([0,0,0],zero=true) == true);
assert(is_vector([0,0,0],zero=false) == false);

View File

@@ -36,8 +36,9 @@ test_vnf_faces();
module test_vnf_get_vertex() {
vnf = [[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]];
assert(vnf_get_vertex(vnf,[0,1,-1]) == [[2],vnf]);
assert(vnf_get_vertex(vnf,[0,1,2]) == [[4],[concat(vnf[0],[[0,1,2]]),vnf[1]]]);
assert(vnf_get_vertex(vnf,[0,1,-1]) == [2,vnf]);
assert(vnf_get_vertex(vnf,[0,1,2]) == [4,[concat(vnf[0],[[0,1,2]]),vnf[1]]]);
assert(vnf_get_vertex(vnf,[[0,1,-1],[0,1,2]]) == [[2,4],[concat(vnf[0],[[0,1,2]]),vnf[1]]]);
}
test_vnf_get_vertex();