1include <BOSL/constants.scad>
2include <BOSL/math.scad>
3
4eps = 1e-9;
5
6// Simple Calculations
7
8module test_quant() {
9 assert(quant(-4,3) == -3);
10 assert(quant(-3,3) == -3);
11 assert(quant(-2,3) == -3);
12 assert(quant(-1,3) == 0);
13 assert(quant(0,3) == 0);
14 assert(quant(1,3) == 0);
15 assert(quant(2,3) == 3);
16 assert(quant(3,3) == 3);
17 assert(quant(4,3) == 3);
18 assert(quant(7,3) == 6);
19}
20test_quant();
21
22
23module test_quantdn() {
24 assert(quantdn(-4,3) == -6);
25 assert(quantdn(-3,3) == -3);
26 assert(quantdn(-2,3) == -3);
27 assert(quantdn(-1,3) == -3);
28 assert(quantdn(0,3) == 0);
29 assert(quantdn(1,3) == 0);
30 assert(quantdn(2,3) == 0);
31 assert(quantdn(3,3) == 3);
32 assert(quantdn(4,3) == 3);
33 assert(quantdn(7,3) == 6);
34}
35test_quantdn();
36
37
38module test_quantup() {
39 assert(quantup(-4,3) == -3);
40 assert(quantup(-3,3) == -3);
41 assert(quantup(-2,3) == 0);
42 assert(quantup(-1,3) == 0);
43 assert(quantup(0,3) == 0);
44 assert(quantup(1,3) == 3);
45 assert(quantup(2,3) == 3);
46 assert(quantup(3,3) == 3);
47 assert(quantup(4,3) == 6);
48 assert(quantup(7,3) == 9);
49}
50test_quantup();
51
52
53module test_constrain() {
54 assert(constrain(-2,-1,1) == -1);
55 assert(constrain(-1.75,-1,1) == -1);
56 assert(constrain(-1,-1,1) == -1);
57 assert(constrain(-0.75,-1,1) == -0.75);
58 assert(constrain(0,-1,1) == 0);
59 assert(constrain(0.75,-1,1) == 0.75);
60 assert(constrain(1,-1,1) == 1);
61 assert(constrain(1.75,-1,1) == 1);
62 assert(constrain(2,-1,1) == 1);
63}
64test_constrain();
65
66
67module test_posmod() {
68 assert(posmod(-5,3) == 1);
69 assert(posmod(-4,3) == 2);
70 assert(posmod(-3,3) == 0);
71 assert(posmod(-2,3) == 1);
72 assert(posmod(-1,3) == 2);
73 assert(posmod(0,3) == 0);
74 assert(posmod(1,3) == 1);
75 assert(posmod(2,3) == 2);
76 assert(posmod(3,3) == 0);
77}
78test_posmod();
79
80
81module test_modrange() {
82 assert(modrange(-5,5,3) == [1,2]);
83 assert(modrange(-1,4,3) == [2,0,1]);
84 assert(modrange(1,8,10,step=2) == [1,3,5,7]);
85 assert(modrange(5,12,10,step=2) == [5,7,9,1]);
86}
87test_modrange();
88
89
90module test_segs() {
91 assert(segs(50,$fn=8) == 8);
92 assert(segs(50,$fa=2,$fs=2) == 158);
93}
94test_segs();
95
96
97module test_lerp() {
98 assert(lerp(-20,20,0) == -20);
99 assert(lerp(-20,20,0.25) == -10);
100 assert(lerp(-20,20,0.5) == 0);
101 assert(lerp(-20,20,0.75) == 10);
102 assert(lerp(-20,20,1) == 20);
103 assert(lerp([10,10],[30,-10],0.5) == [20,0]);
104}
105test_lerp();
106
107
108module test_hypot() {
109 assert(hypot(20,30) == norm([20,30]));
110}
111test_hypot();
112
113
114module test_sinh() {
115 assert(abs(sinh(-2)+3.6268604078) < eps);
116 assert(abs(sinh(-1)+1.1752011936) < eps);
117 assert(abs(sinh(0)) < eps);
118 assert(abs(sinh(1)-1.1752011936) < eps);
119 assert(abs(sinh(2)-3.6268604078) < eps);
120}
121test_sinh();
122
123
124module test_cosh() {
125 assert(abs(cosh(-2)-3.7621956911) < eps);
126 assert(abs(cosh(-1)-1.5430806348) < eps);
127 assert(abs(cosh(0)-1) < eps);
128 assert(abs(cosh(1)-1.5430806348) < eps);
129 assert(abs(cosh(2)-3.7621956911) < eps);
130}
131test_cosh();
132
133
134module test_tanh() {
135 assert(abs(tanh(-2)+0.9640275801) < eps);
136 assert(abs(tanh(-1)+0.761594156) < eps);
137 assert(abs(tanh(0)) < eps);
138 assert(abs(tanh(1)-0.761594156) < eps);
139 assert(abs(tanh(2)-0.9640275801) < eps);
140}
141test_tanh();
142
143
144module test_asinh() {
145 assert(abs(asinh(sinh(-2))+2) < eps);
146 assert(abs(asinh(sinh(-1))+1) < eps);
147 assert(abs(asinh(sinh(0))) < eps);
148 assert(abs(asinh(sinh(1))-1) < eps);
149 assert(abs(asinh(sinh(2))-2) < eps);
150}
151test_asinh();
152
153
154module test_acosh() {
155 assert(abs(acosh(cosh(-2))-2) < eps);
156 assert(abs(acosh(cosh(-1))-1) < eps);
157 assert(abs(acosh(cosh(0))) < eps);
158 assert(abs(acosh(cosh(1))-1) < eps);
159 assert(abs(acosh(cosh(2))-2) < eps);
160}
161test_acosh();
162
163
164module test_atanh() {
165 assert(abs(atanh(tanh(-2))+2) < eps);
166 assert(abs(atanh(tanh(-1))+1) < eps);
167 assert(abs(atanh(tanh(0))) < eps);
168 assert(abs(atanh(tanh(1))-1) < eps);
169 assert(abs(atanh(tanh(2))-2) < eps);
170}
171test_atanh();
172
173
174module test_sum() {
175 assert(sum([1,2,3]) == 6);
176 assert(sum([-2,-1,0,1,2]) == 0);
177 assert(sum([[1,2,3], [3,4,5], [5,6,7]]) == [9,12,15]);
178}
179test_sum();
180
181
182module test_sum_of_squares() {
183 assert(sum_of_squares([1,2,3]) == 14);
184 assert(sum_of_squares([1,2,4]) == 21);
185 assert(sum_of_squares([-3,-2,-1]) == 14);
186}
187test_sum_of_squares();
188
189
190module test_sum_of_sines() {
191 assert(sum_of_sines(0, [[3,4,0],[2,2,0]]) == 0);
192 assert(sum_of_sines(45, [[3,4,0],[2,2,0]]) == 2);
193 assert(sum_of_sines(90, [[3,4,0],[2,2,0]]) == 0);
194 assert(sum_of_sines(135, [[3,4,0],[2,2,0]]) == -2);
195 assert(sum_of_sines(180, [[3,4,0],[2,2,0]]) == 0);
196}
197test_sum_of_sines();
198
199
200module test_mean() {
201 assert(mean([2,3,4]) == 3);
202 assert(mean([[1,2,3], [3,4,5], [5,6,7]]) == [3,4,5]);
203}
204test_mean();
205
206
207// Logic
208
209
210module test_compare_vals() {
211 assert(compare_vals(-10,0) == -1);
212 assert(compare_vals(10,0) == 1);
213 assert(compare_vals(10,10) == 0);
214
215 assert(compare_vals("abc","abcd") == -1);
216 assert(compare_vals("abcd","abc") == 1);
217 assert(compare_vals("abcd","abcd") == 0);
218
219 assert(compare_vals(false,false) == 0);
220 assert(compare_vals(true,false) == 1);
221 assert(compare_vals(false,true) == -1);
222 assert(compare_vals(true,true) == 0);
223
224 assert(compare_vals([2,3,4], [2,3,4,5]) == -1);
225 assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0);
226 assert(compare_vals([2,3,4,5], [2,3,4]) == 1);
227 assert(compare_vals([2,3,4,5], [2,3,5,5]) == -1);
228 assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) == -1);
229
230 assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
231 assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) == -1);
232 assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) == -1);
233 assert(compare_vals([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) == 1);
234 assert(compare_vals([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) == 1);
235 assert(compare_vals([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) == 1);
236 assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) == -1);
237}
238test_compare_vals();
239
240
241module test_compare_lists() {
242 assert(compare_lists([2,3,4], [2,3,4,5]) == -1);
243 assert(compare_lists([2,3,4,5], [2,3,4,5]) == 0);
244 assert(compare_lists([2,3,4,5], [2,3,4]) == 1);
245 assert(compare_lists([2,3,4,5], [2,3,5,5]) == -1);
246
247 assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
248 assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) == -1);
249 assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) == -1);
250 assert(compare_lists([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) == 1);
251 assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) == 1);
252 assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) == 1);
253 assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) == -1);
254
255 assert(compare_lists("cat", "bat") == 1);
256 assert(compare_lists(["cat"], ["bat"]) == 1);
257}
258test_compare_lists();
259
260
261module test_any() {
262 assert(any([0,false,undef]) == false);
263 assert(any([1,false,undef]) == true);
264 assert(any([1,5,true]) == true);
265 assert(any([[0,0], [0,0]]) == false);
266 assert(any([[0,0], [1,0]]) == true);
267}
268test_any();
269
270
271module test_all() {
272 assert(all([0,false,undef]) == false);
273 assert(all([1,false,undef]) == false);
274 assert(all([1,5,true]) == true);
275 assert(all([[0,0], [0,0]]) == false);
276 assert(all([[0,0], [1,0]]) == false);
277 assert(all([[1,1], [1,1]]) == true);
278}
279test_all();
280
281
282module test_count_true() {
283 assert(count_true([0,false,undef]) == 0);
284 assert(count_true([1,false,undef]) == 1);
285 assert(count_true([1,5,false]) == 2);
286 assert(count_true([1,5,true]) == 3);
287 assert(count_true([[0,0], [0,0]]) == 0);
288 assert(count_true([[0,0], [1,0]]) == 1);
289 assert(count_true([[1,1], [1,1]]) == 4);
290 assert(count_true([[1,1], [1,1]], nmax=3) == 3);
291}
292test_count_true();
293
294
295
296// List/Array Ops
297
298module test_cdr() {
299 assert(cdr([]) == []);
300 assert(cdr([88]) == []);
301 assert(cdr([1,2,3]) == [2,3]);
302 assert(cdr(["a","b","c"]) == ["b","c"]);
303}
304test_cdr();
305
306
307module test_replist() {
308 assert(replist(1, 4) == [1,1,1,1]);
309 assert(replist(8, [2,3]) == [[8,8,8], [8,8,8]]);
310 assert(replist(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
311 assert(replist([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
312}
313test_replist();
314
315
316module test_in_list() {
317 assert(in_list("bar", ["foo", "bar", "baz"]));
318 assert(!in_list("bee", ["foo", "bar", "baz"]));
319 assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
320}
321test_in_list();
322
323
324module test_slice() {
325 assert(slice([3,4,5,6,7,8,9], 3, 5) == [6,7]);
326 assert(slice([3,4,5,6,7,8,9], 2, -1) == [5,6,7,8,9]);
327 assert(slice([3,4,5,6,7,8,9], 1, 1) == []);
328 assert(slice([3,4,5,6,7,8,9], 6, -1) == [9]);
329 assert(slice([3,4,5,6,7,8,9], 2, -2) == [5,6,7,8]);
330}
331test_slice();
332
333
334module test_select() {
335 l = [3,4,5,6,7,8,9];
336 assert(select(l, 5, 6) == [8,9]);
337 assert(select(l, 5, 8) == [8,9,3,4]);
338 assert(select(l, 5, 2) == [8,9,3,4,5]);
339 assert(select(l, -3, -1) == [7,8,9]);
340 assert(select(l, 3, 3) == [6]);
341 assert(select(l, 4) == 7);
342 assert(select(l, -2) == 8);
343 assert(select(l, [1:3]) == [4,5,6]);
344 assert(select(l, [1,3]) == [4,6]);
345}
346test_select();
347
348
349module test_reverse() {
350 assert(reverse([3,4,5,6]) == [6,5,4,3]);
351}
352test_reverse();
353
354
355module test_array_subindex() {
356 v = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
357 assert(array_subindex(v,2) == [3, 7, 11, 15]);
358 assert(array_subindex(v,[2,1]) == [[3, 2], [7, 6], [11, 10], [15, 14]]);
359 assert(array_subindex(v,[1:3]) == [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]]);
360}
361test_array_subindex();
362
363
364module test_list_range() {
365 assert(list_range(4) == [0,1,2,3]);
366 assert(list_range(n=4, step=2) == [0,2,4,6]);
367 assert(list_range(n=4, s=3, step=3) == [3,6,9,12]);
368 assert(list_range(n=4, s=3, e=9, step=3) == [3,6,9]);
369 assert(list_range(e=3) == [0,1,2,3]);
370 assert(list_range(e=6, step=2) == [0,2,4,6]);
371 assert(list_range(s=3, e=5) == [3,4,5]);
372 assert(list_range(s=3, e=8, step=2) == [3,5,7]);
373 assert(list_range(s=4, e=8, step=2) == [4,6,8]);
374 assert(list_range(n=4, s=[3,4], step=[2,3]) == [[3,4], [5,7], [7,10], [9,13]]);
375}
376test_list_range();
377
378
379module test_array_shortest() {
380 assert(array_shortest(["foobar", "bazquxx", "abcd"]) == 4);
381}
382test_array_shortest();
383
384
385module test_array_longest() {
386 assert(array_longest(["foobar", "bazquxx", "abcd"]) == 7);
387}
388test_array_longest();
389
390
391module test_array_pad() {
392 assert(array_pad([4,5,6], 5, 8) == [4,5,6,8,8]);
393 assert(array_pad([4,5,6,7,8], 5, 8) == [4,5,6,7,8]);
394 assert(array_pad([4,5,6,7,8,9], 5, 8) == [4,5,6,7,8,9]);
395}
396test_array_pad();
397
398
399module test_array_trim() {
400 assert(array_trim([4,5,6], 5) == [4,5,6]);
401 assert(array_trim([4,5,6,7,8], 5) == [4,5,6,7,8]);
402 assert(array_trim([3,4,5,6,7,8,9], 5) == [3,4,5,6,7]);
403}
404test_array_trim();
405
406
407module test_array_fit() {
408 assert(array_fit([4,5,6], 5, 8) == [4,5,6,8,8]);
409 assert(array_fit([4,5,6,7,8], 5, 8) == [4,5,6,7,8]);
410 assert(array_fit([3,4,5,6,7,8,9], 5, 8) == [3,4,5,6,7]);
411}
412test_array_fit();
413
414
415module test_enumerate() {
416 assert(enumerate(["a","b","c"]) == [[0,"a"], [1,"b"], [2,"c"]]);
417 assert(enumerate([[88,"a"],[76,"b"],[21,"c"]], idx=1) == [[0,"a"], [1,"b"], [2,"c"]]);
418 assert(enumerate([["cat","a",12],["dog","b",10],["log","c",14]], idx=[1:2]) == [[0,"a",12], [1,"b",10], [2,"c",14]]);
419}
420test_enumerate();
421
422
423module test_array_zip() {
424 v1 = [1,2,3,4];
425 v2 = [5,6,7];
426 v3 = [8,9,10,11];
427 assert(array_zip(v1,v3) == [[1,8],[2,9],[3,10],[4,11]]);
428 assert(array_zip([v1,v3]) == [[1,8],[2,9],[3,10],[4,11]]);
429 assert(array_zip([v1,v2],fit="short") == [[1,5],[2,6],[3,7]]);
430 assert(array_zip([v1,v2],fit="long") == [[1,5],[2,6],[3,7],[4,undef]]);
431 assert(array_zip([v1,v2],fit="long", fill=0) == [[1,5],[2,6],[3,7],[4,0]]);
432 assert(array_zip([v1,v2,v3],fit="long") == [[1,5,8],[2,6,9],[3,7,10],[4,undef,11]]);
433}
434test_array_zip();
435
436
437module test_array_group() {
438 v = [1,2,3,4,5,6];
439 assert(array_group(v,2) == [[1,2], [3,4], [5,6]]);
440 assert(array_group(v,3) == [[1,2,3], [4,5,6]]);
441 assert(array_group(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
442}
443test_array_group();
444
445
446module test_flatten() {
447 assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
448}
449test_flatten();
450
451
452module test_sort() {
453 assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]);
454 assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"]) == ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]);
455 assert(sort(enumerate([[2,3,4],[1,2,3],[2,4,3]]),idx=1)==[[1,[1,2,3]], [0,[2,3,4]], [2,[2,4,3]]]);
456}
457test_sort();
458
459
460module test_sortidx() {
461 lst1 = ["d","b","e","c"];
462 assert(sortidx(lst1) == [1,3,0,2]);
463 lst2 = [
464 ["foo", 88, [0,0,1], false],
465 ["bar", 90, [0,1,0], true],
466 ["baz", 89, [1,0,0], false],
467 ["qux", 23, [1,1,1], true]
468 ];
469 assert(sortidx(lst2, idx=1) == [3,0,2,1]);
470 assert(sortidx(lst2, idx=0) == [1,2,0,3]);
471 assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]);
472 lst3 = [[-4, 0, 0], [0, 0, -4], [0, -4, 0], [-4, 0, 0], [0, -4, 0], [0, 0, 4], [0, 0, -4], [0, 4, 0], [4, 0, 0], [0, 0, 4], [0, 4, 0], [4, 0, 0]];
473 assert(sortidx(lst3)==[0,3,2,4,1,6,5,9,7,10,8,11]);
474}
475test_sortidx();
476
477
478module test_unique() {
479 assert(unique([]) == []);
480 assert(unique([8]) == [8]);
481 assert(unique([7,3,9,4,3,1,8]) == [1,3,4,7,8,9]);
482}
483test_unique();
484
485
486module test_array_dim() {
487 assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
488 assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);
489 assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 2) == 3);
490 assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9]]]) == [2,undef,3]);
491}
492test_array_dim();
493
494
495module test_vmul() {
496 assert(vmul([3,4,5], [8,7,6]) == [24,28,30]);
497 assert(vmul([1,2,3], [4,5,6]) == [4,10,18]);
498}
499test_vmul();
500
501
502module test_vdiv() {
503 assert(vdiv([24,28,30], [8,7,6]) == [3, 4, 5]);
504}
505test_vdiv();
506
507
508module test_vabs() {
509 assert(vabs([2,4,8]) == [2,4,8]);
510 assert(vabs([-2,-4,-8]) == [2,4,8]);
511 assert(vabs([-2,4,8]) == [2,4,8]);
512 assert(vabs([2,-4,8]) == [2,4,8]);
513 assert(vabs([2,4,-8]) == [2,4,8]);
514}
515test_vabs();
516
517
518module test_normalize() {
519 assert(normalize([10,0,0]) == [1,0,0]);
520 assert(normalize([0,10,0]) == [0,1,0]);
521 assert(normalize([0,0,10]) == [0,0,1]);
522 assert(abs(norm(normalize([10,10,10]))-1) < eps);
523 assert(abs(norm(normalize([-10,-10,-10]))-1) < eps);
524 assert(abs(norm(normalize([-10,0,0]))-1) < eps);
525 assert(abs(norm(normalize([0,-10,0]))-1) < eps);
526 assert(abs(norm(normalize([0,0,-10]))-1) < eps);
527}
528test_normalize();
529
530
531module test_vector_angle() {
532 vecs = [[10,0,0], [-10,0,0], [0,10,0], [0,-10,0], [0,0,10], [0,0,-10]];
533 for (a=vecs, b=vecs) {
534 if(a==b) {
535 assert(vector_angle(a,b)==0);
536 } else if(a==-b) {
537 assert(vector_angle(a,b)==180);
538 } else {
539 assert(vector_angle(a,b)==90);
540 }
541 }
542 assert(abs(vector_angle([10,10,0],[10,0,0])-45) < eps);
543}
544test_vector_angle();
545
546
547module test_vector_axis() {
548 assert(norm(vector_axis([10,0,0],[10,10,0]) - [0,0,1]) < eps);
549 assert(norm(vector_axis([10,0,0],[0,10,0]) - [0,0,1]) < eps);
550 assert(norm(vector_axis([0,10,0],[10,0,0]) - [0,0,-1]) < eps);
551 assert(norm(vector_axis([0,0,10],[10,0,0]) - [0,1,0]) < eps);
552 assert(norm(vector_axis([10,0,0],[0,0,10]) - [0,-1,0]) < eps);
553 assert(norm(vector_axis([10,0,10],[0,-10,0]) - [sin(45),0,-sin(45)]) < eps);
554}
555test_vector_axis();
556
557
558module test_point2d() {
559 assert(point2d([1,2,3])==[1,2]);
560 assert(point2d([2,3])==[2,3]);
561 assert(point2d([1])==[1,0]);
562}
563test_point2d();
564
565
566module test_path2d() {
567 assert(path2d([[1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5]])==[[1,0],[1,2],[1,2],[1,2],[1,2]]);
568}
569test_path2d();
570
571
572module test_point3d() {
573 assert(point3d([1,2,3,4,5])==[1,2,3]);
574 assert(point3d([1,2,3,4])==[1,2,3]);
575 assert(point3d([1,2,3])==[1,2,3]);
576 assert(point3d([2,3])==[2,3,0]);
577 assert(point3d([1])==[1,0,0]);
578}
579test_point3d();
580
581
582module test_path3d() {
583 assert(path3d([[1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5]])==[[1,0,0],[1,2,0],[1,2,3],[1,2,3],[1,2,3]]);
584}
585test_path3d();
586
587
588module test_translate_points() {
589 pts = [[0,0,1], [0,1,0], [1,0,0], [0,0,-1], [0,-1,0], [-1,0,0]];
590 assert(translate_points(pts, v=[1,2,3]) == [[1,2,4], [1,3,3], [2,2,3], [1,2,2], [1,1,3], [0,2,3]]);
591 assert(translate_points(pts, v=[-1,-2,-3]) == [[-1,-2,-2], [-1,-1,-3], [0,-2,-3], [-1,-2,-4], [-1,-3,-3], [-2,-2,-3]]);
592}
593test_translate_points();
594
595
596module test_scale_points() {
597 pts = [[0,0,1], [0,1,0], [1,0,0], [0,0,-1], [0,-1,0], [-1,0,0]];
598 assert(scale_points(pts, v=[2,3,4]) == [[0,0,4], [0,3,0], [2,0,0], [0,0,-4], [0,-3,0], [-2,0,0]]);
599 assert(scale_points(pts, v=[-2,-3,-4]) == [[0,0,-4], [0,-3,0], [-2,0,0], [0,0,4], [0,3,0], [2,0,0]]);
600 assert(scale_points(pts, v=[1,1,1]) == [[0,0,1], [0,1,0], [1,0,0], [0,0,-1], [0,-1,0], [-1,0,0]]);
601 assert(scale_points(pts, v=[-1,-1,-1]) == [[0,0,-1], [0,-1,0], [-1,0,0], [0,0,1], [0,1,0], [1,0,0]]);
602}
603test_scale_points();
604
605
606module test_rotate_points2d() {
607 pts = [[0,1], [1,0], [0,-1], [-1,0]];
608 s = sin(45);
609 assert(rotate_points2d(pts,45) == [[-s,s],[s,s],[s,-s],[-s,-s]]);
610 assert(rotate_points2d(pts,90) == [[-1,0],[0,1],[1,0],[0,-1]]);
611 assert(rotate_points2d(pts,90,cp=[1,0]) == [[0,-1],[1,0],[2,-1],[1,-2]]);
612}
613test_rotate_points2d();
614
615
616module test_rotate_points3d() {
617 pts = [[0,0,1], [0,1,0], [1,0,0], [0,0,-1], [0,-1,0], [-1,0,0]];
618 assert(rotate_points3d(pts, [90,0,0]) == [[0,-1,0], [0,0,1], [1,0,0], [0,1,0], [0,0,-1], [-1,0,0]]);
619 assert(rotate_points3d(pts, [0,90,0]) == [[1,0,0], [0,1,0], [0,0,-1], [-1,0,0], [0,-1,0], [0,0,1]]);
620 assert(rotate_points3d(pts, [0,0,90]) == [[0,0,1], [-1,0,0], [0,1,0], [0,0,-1], [1,0,0], [0,-1,0]]);
621 assert(rotate_points3d(pts, [0,0,90],cp=[2,0,0]) == [[2,-2,1], [1,-2,0], [2,-1,0], [2,-2,-1], [3,-2,0], [2,-3,0]]);
622 assert(rotate_points3d(pts, 90, axis=V_UP) == [[0,0,1], [-1,0,0], [0,1,0], [0,0,-1], [1,0,0], [0,-1,0]]);
623 assert(rotate_points3d(pts, 90, axis=V_DOWN) == [[0,0,1], [1,0,0], [0,-1,0], [0,0,-1], [-1,0,0], [0,1,0]]);
624 assert(rotate_points3d(pts, 90, axis=V_RIGHT) == [[0,-1,0], [0,0,1], [1,0,0], [0,1,0], [0,0,-1], [-1,0,0]]);
625 assert(rotate_points3d(pts, from=V_UP, to=V_BACK) == [[0,1,0], [0,0,-1], [1,0,0], [0,-1,0], [0,0,1], [-1,0,0]]);
626 assert(rotate_points3d(pts, 90, from=V_UP, to=V_BACK), [[0,1,0], [-1,0,0], [0,0,-1], [0,-1,0], [1,0,0], [0,0,1]]);
627 assert(rotate_points3d(pts, from=V_UP, to=V_UP*2) == [[0,0,1], [0,1,0], [1,0,0], [0,0,-1], [0,-1,0], [-1,0,0]]);
628 assert(rotate_points3d(pts, from=V_UP, to=V_DOWN*2) == [[0,0,-1], [0,1,0], [-1,0,0], [0,0,1], [0,-1,0], [1,0,0]]);
629}
630test_rotate_points3d();
631
632
633module test_simplify_path()
634{
635 path = [[-20,10],[-10,0],[-5,0],[0,0],[5,0],[10,0], [10,10]];
636 assert(simplify_path(path) == [[-20,10],[-10,0],[10,0], [10,10]]);
637}
638test_simplify_path();
639
640
641module test_simplify_path_indexed()
642{
643 points = [[-20,10],[-10,0],[-5,0],[0,0],[5,0],[10,0], [10,10]];
644 path = list_range(len(points));
645 assert(simplify_path_indexed(points, path) == [0,1,5,6]);
646}
647test_simplify_path_indexed();
648
649
650// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap