1include <../std.scad>
  2
  3seed = floor(rands(0,10000,1)[0]);
  4
  5module test_is_vector() {
  6    assert(is_vector([1,2,3]) == true);
  7    assert(is_vector([[1,2,3]]) == false);
  8    assert(is_vector([[1,2,3,4],[5,6,7,8]]) == false);
  9    assert(is_vector([[1,2,3,4],[5,6]]) == false);
 10    assert(is_vector(["foo"]) == false);
 11    assert(is_vector([]) == false);
 12    assert(is_vector(1) == false);
 13    assert(is_vector("foo") == false);
 14    assert(is_vector(true) == false);
 15    assert(is_vector([3,4,"foo"]) == false);
 16    assert(is_vector([3,4,[4,5]]) == false);
 17    assert(is_vector([3,4,undef]) == false);
 18    assert(is_vector(["foo","bar"]) == false);
 19
 20    assert(is_vector([0,0,0],zero=true) == true);
 21    assert(is_vector([0,0,0],zero=false) == false);
 22    assert(is_vector([0,1,0],zero=true) == false);
 23    assert(is_vector([0,0,1],zero=false) == true);
 24    assert(is_vector([1,1,1],zero=false) == true);
 25
 26    assert(is_vector([0,0,0],all_nonzero=true) == false);
 27    assert(is_vector([0,1,0],all_nonzero=true) == false);
 28    assert(is_vector([0,0,1],all_nonzero=true) == false);
 29    assert(is_vector([1,1,1],all_nonzero=true) == true);
 30    assert(is_vector([-1,1,1],all_nonzero=true) == true);
 31    assert(is_vector([-1,-1,-1],all_nonzero=true) == true);
 32    assert(!is_vector([3,INF,4]));
 33    assert(!is_vector([3,NAN,4]));
 34}
 35test_is_vector();
 36
 37
 38module test_v_floor() {
 39    assert_equal(v_floor([2.0, 3.14, 18.9, 7]), [2,3,18,7]);
 40    assert_equal(v_floor([-2.0, -3.14, -18.9, -7]), [-2,-4,-19,-7]);
 41}
 42test_v_floor();
 43
 44
 45module test_v_ceil() {
 46    assert_equal(v_ceil([2.0, 3.14, 18.9, 7]), [2,4,19,7]);
 47    assert_equal(v_ceil([-2.0, -3.14, -18.9, -7]), [-2,-3,-18,-7]);
 48}
 49test_v_ceil();
 50
 51
 52module test_v_lookup() {
 53    lup = [[4, [3,4,5]], [5, [5,6,7]], [6, [4,5,6]]];
 54    assert_equal(v_lookup(3,lup), [3,4,5]);
 55    assert_equal(v_lookup(3.5,lup), [3,4,5]);
 56    assert_equal(v_lookup(4,lup), [3,4,5]);
 57    assert_approx(v_lookup(4.2,lup), [3.4,4.4,5.4]);
 58    assert_equal(v_lookup(4.5,lup), [4,5,6]);
 59    assert_equal(v_lookup(5,lup), [5,6,7]);
 60    assert_approx(v_lookup(5.2,lup), [4.8,5.8,6.8]);
 61    assert_equal(v_lookup(5.5,lup), [4.5,5.5,6.5]);
 62    assert_equal(v_lookup(6,lup), [4,5,6]);
 63    assert_equal(v_lookup(6.5,lup), [4,5,6]);
 64    assert_equal(v_lookup(7,lup), [4,5,6]);
 65}
 66test_v_lookup();
 67
 68
 69module test_v_mul() {
 70    assert_equal(v_mul([3,4,5], [8,7,6]), [24,28,30]);
 71    assert_equal(v_mul([1,2,3], [4,5,6]), [4,10,18]);
 72    assert_equal(v_mul([[1,2,3],[4,5,6],[7,8,9]], [[4,5,6],[3,2,1],[5,9,3]]), [32,28,134]);
 73}
 74test_v_mul();
 75
 76
 77module test_v_div() {
 78    assert(v_div([24,28,30], [8,7,6]) == [3, 4, 5]);
 79}
 80test_v_div();
 81
 82
 83module test_v_abs() {
 84    assert(v_abs([2,4,8]) == [2,4,8]);
 85    assert(v_abs([-2,-4,-8]) == [2,4,8]);
 86    assert(v_abs([-2,4,8]) == [2,4,8]);
 87    assert(v_abs([2,-4,8]) == [2,4,8]);
 88    assert(v_abs([2,4,-8]) == [2,4,8]);
 89}
 90test_v_abs();
 91
 92include <../strings.scad>
 93module test_v_theta() {
 94    assert_approx(v_theta([0,0]), 0);
 95    assert_approx(v_theta([1,0]), 0);
 96    assert_approx(v_theta([0,1]), 90);
 97    assert_approx(v_theta([-1,0]), 180);
 98    assert_approx(v_theta([0,-1]), -90);
 99    assert_approx(v_theta([1,1]), 45);
100    assert_approx(v_theta([-1,1]), 135);
101    assert_approx(v_theta([1,-1]), -45);
102    assert_approx(v_theta([-1,-1]), -135);
103    assert_approx(v_theta([0,0,1]), 0);
104    assert_approx(v_theta([0,1,1]), 90);
105    assert_approx(v_theta([0,1,-1]), 90);
106    assert_approx(v_theta([1,0,0]), 0);
107    assert_approx(v_theta([0,1,0]), 90);
108    assert_approx(v_theta([0,-1,0]), -90);
109    assert_approx(v_theta([-1,0,0]), 180);
110    assert_approx(v_theta([1,0,1]), 0);
111    assert_approx(v_theta([0,1,1]), 90);
112    assert_approx(v_theta([0,-1,1]), -90);
113    assert_approx(v_theta([1,1,1]), 45);
114}
115test_v_theta();
116
117
118module test_min_index() {
119    vals = rands(-100,100,100,seed=75);
120    minval = min(vals);
121    minidx = min_index(vals);
122    assert_equal(vals[minidx], minval);
123    assert_equal(min_index([3,4,5,6]), 0);
124    assert_equal(min_index([4,3,5,6]), 1);
125    assert_equal(min_index([4,5,3,6]), 2);
126    assert_equal(min_index([4,5,6,3]), 3);
127    assert_equal(min_index([6,5,4,3]), 3);
128    assert_equal(min_index([6,3,4,5]), 1);
129    assert_equal(min_index([-56,72,-874,5]), 2);
130}
131test_min_index();
132
133
134module test_max_index() {
135    vals = rands(-100,100,100,seed=97);
136    maxval = max(vals);
137    maxidx = max_index(vals);
138    assert_equal(vals[maxidx], maxval);
139    assert_equal(max_index([3,4,5,6]), 3);
140    assert_equal(max_index([3,4,6,5]), 2);
141    assert_equal(max_index([3,6,4,5]), 1);
142    assert_equal(max_index([6,3,4,5]), 0);
143    assert_equal(max_index([5,6,4,3]), 1);
144    assert_equal(max_index([-56,72,-874,5]), 1);
145}
146test_max_index();
147
148
149
150
151
152module test_unit() {
153    assert(unit([10,0,0]) == [1,0,0]);
154    assert(unit([0,10,0]) == [0,1,0]);
155    assert(unit([0,0,10]) == [0,0,1]);
156    assert(abs(norm(unit([10,10,10]))-1) < EPSILON);
157    assert(abs(norm(unit([-10,-10,-10]))-1) < EPSILON);
158    assert(abs(norm(unit([-10,0,0]))-1) < EPSILON);
159    assert(abs(norm(unit([0,-10,0]))-1) < EPSILON);
160    assert(abs(norm(unit([0,0,-10]))-1) < EPSILON);
161}
162test_unit();
163
164
165module test_vector_angle() {
166    vecs = [[10,0,0], [-10,0,0], [0,10,0], [0,-10,0], [0,0,10], [0,0,-10]];
167    for (a=vecs, b=vecs) {
168        if(a==b) {
169            assert(vector_angle(a,b)==0);
170            assert(vector_angle([a,b])==0);
171        } else if(a==-b) {
172            assert(vector_angle(a,b)==180);
173            assert(vector_angle([a,b])==180);
174        } else {
175            assert(vector_angle(a,b)==90);
176            assert(vector_angle([a,b])==90);
177        }
178    }
179    assert(abs(vector_angle([10,10,0],[10,0,0])-45) < EPSILON);
180    assert(abs(vector_angle([[10,10,0],[10,0,0]])-45) < EPSILON);
181    assert(abs(vector_angle([11,11,1],[1,1,1],[11,-9,1])-90) < EPSILON);
182    assert(abs(vector_angle([[11,11,1],[1,1,1],[11,-9,1]])-90) < EPSILON);
183}
184test_vector_angle();
185
186
187module test_vector_axis() {
188    assert(norm(vector_axis([10,0,0],[10,10,0]) - [0,0,1]) < EPSILON);
189    assert(norm(vector_axis([[10,0,0],[10,10,0]]) - [0,0,1]) < EPSILON);
190    assert(norm(vector_axis([10,0,0],[0,10,0]) - [0,0,1]) < EPSILON);
191    assert(norm(vector_axis([[10,0,0],[0,10,0]]) - [0,0,1]) < EPSILON);
192    assert(norm(vector_axis([0,10,0],[10,0,0]) - [0,0,-1]) < EPSILON);
193    assert(norm(vector_axis([[0,10,0],[10,0,0]]) - [0,0,-1]) < EPSILON);
194    assert(norm(vector_axis([0,0,10],[10,0,0]) - [0,1,0]) < EPSILON);
195    assert(norm(vector_axis([[0,0,10],[10,0,0]]) - [0,1,0]) < EPSILON);
196    assert(norm(vector_axis([10,0,0],[0,0,10]) - [0,-1,0]) < EPSILON);
197    assert(norm(vector_axis([[10,0,0],[0,0,10]]) - [0,-1,0]) < EPSILON);
198    assert(norm(vector_axis([10,0,10],[0,-10,0]) - [sin(45),0,-sin(45)]) < EPSILON);
199    assert(norm(vector_axis([[10,0,10],[0,-10,0]]) - [sin(45),0,-sin(45)]) < EPSILON);
200    assert(norm(vector_axis([11,1,11],[1,1,1],[1,-9,1]) - [sin(45),0,-sin(45)]) < EPSILON);
201    assert(norm(vector_axis([[11,1,11],[1,1,1],[1,-9,1]]) - [sin(45),0,-sin(45)]) < EPSILON);
202}
203test_vector_axis();
204
205module test_vector_search(){
206    points = [for(i=[0:9], j=[0:9], k=[1:5]) [i,j,k] ];
207    ind = vector_search([5,5,1],1,points);
208    assert(ind== [225, 270, 275, 276, 280, 325]);
209    assert([for(i=ind) if(norm(points[i]-[5,5,1])>1) i ]==[]);
210    assert([for(i=idx(points)) if(norm(points[i]-[5,5,1])<=1) i]==sort(ind));
211}
212test_vector_search();
213
214module test_vector_search_tree(){
215    points1 = [ [0,1,2], [1,2,3], [2,3,4] ];
216    tree1 = vector_search_tree(points1);
217    assert(tree1 == points1);
218    points2 = [for(i=[0:9], j=[0:9], k=[1:5]) [i,j,k] ];
219    tree2 = vector_search_tree(points2);
220    assert(tree2[0]==points2);
221    ind = vector_search([5,5,1],1,tree2);
222    assert(ind== [225, 270, 275, 276, 280, 325]);
223    rpts = list_to_matrix(rands(0,10,50*3,seed=seed),3);
224    rtree = vector_search_tree(rpts);
225    radius = 3;
226    found0 = vector_search([0,0,0],radius,rpts);
227    found1 = vector_search([0,0,0],radius,rtree);
228    found2 = [for(i=idx(rpts)) if(norm(rpts[i])<=radius) i];
229    assert(sort(found0)==sort(found1), str("Seed = ",seed));
230    assert(sort(found1)==sort(found2), str("Seed = ",seed));
231}
232test_vector_search_tree();
233
234module test_vector_nearest(){
235    points = [for(i=[0:9], j=[0:9], k=[1:5]) [i,j,k] ];
236    ind1 = vector_nearest([5,5,1], 4, points);
237    assert(ind1==[275, 225, 270, 276]);
238    pts = list_to_matrix(rands(0,10,50*3,seed=seed),3);
239    tree = vector_search_tree(pts);
240    nearest = vector_nearest([0,0,0], 4, tree);
241    closest = select(sortidx([for(p=pts) norm(p)]), [0:3]);
242    assert(closest==nearest,str("Seed = ",seed));
243}
244test_vector_nearest();
245
246
247module test_add_scalar() {
248    assert(add_scalar([1,2,3],3) == [4,5,6]);
249}
250test_add_scalar();
251
252
253
254
255module test_pointlist_bounds() {
256    pts = [
257        [-53,27,12],
258        [-63,97,36],
259        [84,-32,-5],
260        [63,-24,42],
261        [23,57,-42]
262    ];
263    assert(pointlist_bounds(pts) == [[-63,-32,-42], [84,97,42]]);
264    pts2d = [
265        [-53,12],
266        [-63,36],
267        [84,-5],
268        [63,42],
269        [23,-42] 
270    ];
271    assert(pointlist_bounds(pts2d) == [[-63,-42],[84,42]]);
272    pts5d = [
273        [-53, 27, 12,-53, 12],
274        [-63, 97, 36,-63, 36],
275        [ 84,-32, -5, 84, -5], 
276        [ 63,-24, 42, 63, 42], 
277        [ 23, 57,-42, 23,-42]
278    ];
279    assert(pointlist_bounds(pts5d) == [[-63,-32,-42,-63,-42],[84,97,42,84,42]]);
280    assert(pointlist_bounds([[3,4,5,6]]), [[3,4,5,6],[3,4,5,6]]);
281}
282test_pointlist_bounds();
283
284
285module test_closest_point() {
286    ptlist = [for (i=count(100)) rands(-100,100,2,seed_value=8463+i)];
287    testpts = [for (i=count(100)) rands(-100,100,2,seed_value=6834+i)];
288    for (pt = testpts) {
289        pidx = closest_point(pt,ptlist);
290        dists = [for (p=ptlist) norm(pt-p)];
291        mindist = min(dists);
292        assert(mindist == dists[pidx]);
293    }
294}
295test_closest_point();
296
297
298module test_furthest_point() {
299    ptlist = [for (i=count(100)) rands(-100,100,2,seed_value=8463+i)];
300    testpts = [for (i=count(100)) rands(-100,100,2,seed_value=6834+i)];
301    for (pt = testpts) {
302        pidx = furthest_point(pt,ptlist);
303        dists = [for (p=ptlist) norm(pt-p)];
304        mindist = max(dists);
305        assert(mindist == dists[pidx]);
306    }
307}
308test_furthest_point();
309
310
311// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap