Merge pull request #216 from RonaldoCMP/master

Extensive changes in arrays.scad, vectors.scad, common.scad and their regression test codes
This commit is contained in:
Revar Desmera 2020-07-29 21:44:39 -07:00 committed by GitHub
commit 5e98cbf679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1133 additions and 686 deletions

File diff suppressed because it is too large Load Diff

View File

@ -15,7 +15,8 @@
// Usage:
// typ = typeof(x);
// Description:
// Returns a string representing the type of the value. One of "undef", "boolean", "number", "nan", "string", "list", or "range"
// Returns a string representing the type of the value. One of "undef", "boolean", "number", "nan", "string", "list", "range" or "invalid".
// Some malformed "ranges", like '[0:NAN:INF]' and '[0:"a":INF]', may be classified as "undef" or "invalid".
function typeof(x) =
is_undef(x)? "undef" :
is_bool(x)? "boolean" :
@ -23,7 +24,9 @@ function typeof(x) =
is_nan(x)? "nan" :
is_string(x)? "string" :
is_list(x)? "list" :
"range";
is_range(x) ? "range" :
"invalid";
// Function: is_type()
@ -70,8 +73,8 @@ function is_str(x) = is_string(x);
// is_int(n)
// Description:
// Returns true if the given value is an integer (it is a number and it rounds to itself).
function is_int(n) = is_num(n) && n == round(n);
function is_integer(n) = is_num(n) && n == round(n);
function is_int(n) = is_finite(n) && n == round(n);
function is_integer(n) = is_finite(n) && n == round(n);
// Function: is_nan()
@ -93,7 +96,17 @@ function is_finite(v) = is_num(0*v);
// Function: is_range()
// Description:
// Returns true if its argument is a range
function is_range(x) = is_num(x[0]) && !is_list(x);
function is_range(x) = !is_list(x) && is_finite(x[0]+x[1]+x[2]) ;
// Function: valid_range()
// Description:
// Returns true if its argument is a valid range (deprecated ranges excluded).
function valid_range(x) =
is_range(x)
&& ( x[1]>0
? x[0]<=x[2]
: ( x[1]<0 && x[0]>=x[2] ) );
// Function: is_list_of()
@ -106,13 +119,15 @@ function is_range(x) = is_num(x[0]) && !is_list(x);
// is_list_of([3,4,5], 0); // Returns true
// is_list_of([3,4,undef], 0); // Returns false
// is_list_of([[3,4],[4,5]], [1,1]); // Returns true
// is_list_of([[3,"a"],[4,true]], [1,undef]); // Returns true
// is_list_of([[3,4], 6, [4,5]], [1,1]); // Returns false
// is_list_of([[1,[3,4]], [4,[5,6]]], [1,[2,3]]); // Returne true
// is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]); // Returne false
// is_list_of([[1,[3,4]], [4,[5,6]]], [1,[2,3]]); // Returns true
// is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]); // Returns false
// is_list_of([], [1,[2,3]]); // Returns true
function is_list_of(list,pattern) =
let(pattern = 0*pattern)
is_list(list) &&
[]==[for(entry=list) if (entry*0 != pattern) entry];
[]==[for(entry=0*list) if (entry != pattern) entry];
// Function: is_consistent()
@ -128,7 +143,15 @@ function is_list_of(list,pattern) =
// is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true
// is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false
function is_consistent(list) =
is_list(list) && is_list_of(list, list[0]);
is_list(list) && is_list_of(list, _list_pattern(list[0]));
//Internal function
//Creates a list with the same structure of `list` with each of its elements substituted by 0.
function _list_pattern(list) =
is_list(list)
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
: 0;
// Function: same_shape()
@ -139,7 +162,7 @@ function is_consistent(list) =
// Example:
// same_shape([3,[4,5]],[7,[3,4]]); // Returns true
// same_shape([3,4,5], [7,[3,4]]); // Returns false
function same_shape(a,b) = a*0 == b*0;
function same_shape(a,b) = _list_pattern(a) == b*0;
// Section: Handling `undef`s.
@ -311,9 +334,10 @@ function scalar_vec3(v, dflt=undef) =
// Calculate the standard number of sides OpenSCAD would give a circle based on `$fn`, `$fa`, and `$fs`.
// Arguments:
// r = Radius of circle to get the number of segments for.
function segs(r) =
function segs(r) =
$fn>0? ($fn>3? $fn : 3) :
ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs)));
let( r = is_finite(r)? r: 0 )
ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs))) ;
@ -322,7 +346,7 @@ function segs(r) =
function _valstr(x) =
is_list(x)? str("[",str_join([for (xx=x) _valstr(xx)],","),"]") :
is_num(x)? fmt_float(x,12) : x;
is_finite(x)? fmt_float(x,12) : x;
// Module: assert_approx()

621
math.scad

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,10 @@ include<../std.scad>
include<../polyhedra.scad>
$fn=96;
if (true) {
$fn=96;
// Display of all solids with insphere, midsphere and circumsphere
for(i=[0:len(_polyhedra_)-1]) {

View File

@ -1,37 +1,14 @@
include <../std.scad>
// List/Array Ops
module test_repeat() {
assert(repeat(1, 4) == [1,1,1,1]);
assert(repeat(8, [2,3]) == [[8,8,8], [8,8,8]]);
assert(repeat(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
assert(repeat([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
}
test_repeat();
// Section: List Query Operations
module test_in_list() {
assert(in_list("bar", ["foo", "bar", "baz"]));
assert(!in_list("bee", ["foo", "bar", "baz"]));
assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
assert(!in_list(undef, [3,4,5]));
assert(in_list(undef,[3,4,undef,5]));
assert(!in_list(3,[]));
assert(!in_list(3,[4,5,[3]]));
}
test_in_list();
module test_slice() {
assert(slice([3,4,5,6,7,8,9], 3, 5) == [6,7]);
assert(slice([3,4,5,6,7,8,9], 2, -1) == [5,6,7,8,9]);
assert(slice([3,4,5,6,7,8,9], 1, 1) == []);
assert(slice([3,4,5,6,7,8,9], 6, -1) == [9]);
assert(slice([3,4,5,6,7,8,9], 2, -2) == [5,6,7,8]);
}
test_slice();
module test_is_simple_list() {
assert(is_simple_list([1,2,3,4]));
assert(is_simple_list([]));
assert(!is_simple_list([1,2,[3,4]]));
}
test_is_simple_list();
module test_select() {
@ -49,6 +26,74 @@ module test_select() {
test_select();
module test_slice() {
assert(slice([3,4,5,6,7,8,9], 3, 5) == [6,7]);
assert(slice([3,4,5,6,7,8,9], 2, -1) == [5,6,7,8,9]);
assert(slice([3,4,5,6,7,8,9], 1, 1) == []);
assert(slice([3,4,5,6,7,8,9], 6, -1) == [9]);
assert(slice([3,4,5,6,7,8,9], 2, -2) == [5,6,7,8]);
assert(slice([], 2, -2) == []);
}
test_slice();
module test_in_list() {
assert(in_list("bar", ["foo", "bar", "baz"]));
assert(!in_list("bee", ["foo", "bar", "baz"]));
assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
assert(!in_list("bee", ["foo", "bar", ["bee"]]));
assert(in_list(NAN, [NAN])==false);
assert(!in_list(undef, [3,4,5]));
assert(in_list(undef,[3,4,undef,5]));
assert(!in_list(3,[]));
assert(!in_list(3,[4,5,[3]]));
}
test_in_list();
module test_min_index() {
assert(min_index([5,3,9,6,2,7,8,2,1])==8);
assert(min_index([5,3,9,6,2,7,8,2,7],all=true)==[4,7]);
// assert(min_index([],all=true)==[]);
}
test_min_index();
module test_max_index() {
assert(max_index([5,3,9,6,2,7,8,9,1])==2);
assert(max_index([5,3,9,6,2,7,8,9,7],all=true)==[2,7]);
// assert(max_index([],all=true)==[]);
}
test_max_index();
module test_list_increasing() {
assert(list_increasing([1,2,3,4]) == true);
assert(list_increasing([1,3,2,4]) == false);
assert(list_increasing([4,3,2,1]) == false);
}
test_list_increasing();
module test_list_decreasing() {
assert(list_decreasing([1,2,3,4]) == false);
assert(list_decreasing([4,2,3,1]) == false);
assert(list_decreasing([4,3,2,1]) == true);
}
test_list_decreasing();
// Section: Basic List Generation
module test_repeat() {
assert(repeat(1, 4) == [1,1,1,1]);
assert(repeat(8, [2,3]) == [[8,8,8], [8,8,8]]);
assert(repeat(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
assert(repeat([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
assert(repeat(4, [2,-1]) == [[], []]);
}
test_repeat();
module test_list_range() {
assert(list_range(4) == [0,1,2,3]);
assert(list_range(n=4, step=2) == [0,2,4,6]);
@ -66,6 +111,8 @@ test_list_range();
module test_reverse() {
assert(reverse([3,4,5,6]) == [6,5,4,3]);
assert(reverse("abcd") == ["d","c","b","a"]);
assert(reverse([]) == []);
}
test_reverse();
@ -90,6 +137,8 @@ module test_deduplicate() {
assert(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]) == [8,3,4,8,2,3]);
assert(deduplicate("Hello") == ["H","e","l","o"]);
assert(deduplicate([[3,4],[7,1.99],[7,2],[1,4]],eps=0.1) == [[3,4],[7,2],[1,4]]);
assert(deduplicate([], closed=true) == []);
assert(deduplicate([[1,[1,[undef]]],[1,[1,[undef]]],[1,[2]],[1,[2,[0]]]])==[[1, [1,[undef]]],[1,[2]],[1,[2,[0]]]]);
}
test_deduplicate();
@ -148,22 +197,6 @@ module test_list_bset() {
test_list_bset();
module test_list_increasing() {
assert(list_increasing([1,2,3,4]) == true);
assert(list_increasing([1,3,2,4]) == false);
assert(list_increasing([4,3,2,1]) == false);
}
test_list_increasing();
module test_list_decreasing() {
assert(list_decreasing([1,2,3,4]) == false);
assert(list_decreasing([4,2,3,1]) == false);
assert(list_decreasing([4,3,2,1]) == true);
}
test_list_decreasing();
module test_list_shortest() {
assert(list_shortest(["foobar", "bazquxx", "abcd"]) == 4);
}
@ -315,6 +348,13 @@ test_set_intersection();
// Arrays
module test_add_scalar() {
assert(add_scalar([1,2,3],3) == [4,5,6]);
assert(add_scalar([[1,2,3],[3,4,5]],3) == [[4,5,6],[6,7,8]]);
}
test_add_scalar();
module test_subindex() {
v = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
assert(subindex(v,2) == [3, 7, 11, 15]);
@ -402,10 +442,18 @@ test_array_group();
module test_flatten() {
assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
assert(flatten([]) == []);
}
test_flatten();
module test_full_flatten() {
assert(full_flatten([[1,2,3], [4,5,[6,[7],8]]]) == [1,2,3,4,5,6,7,8]);
assert(full_flatten([]) == []);
}
test_full_flatten();
module test_array_dim() {
assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);

View File

@ -18,6 +18,10 @@ module test_typeof() {
assert(typeof([0:1:5]) == "range");
assert(typeof([-3:2:5]) == "range");
assert(typeof([10:-2:-10]) == "range");
assert(typeof([0:NAN:INF]) == "invalid");
assert(typeof([0:"a":INF]) == "undef");
assert(typeof([0:[]:INF]) == "undef");
assert(typeof([true:1:INF]) == "undef");
}
test_typeof();
@ -102,6 +106,8 @@ module test_is_int() {
assert(!is_int(-99.1));
assert(!is_int(99.1));
assert(!is_int(undef));
assert(!is_int(INF));
assert(!is_int(NAN));
assert(!is_int(false));
assert(!is_int(true));
assert(!is_int("foo"));
@ -124,6 +130,8 @@ module test_is_integer() {
assert(!is_integer(-99.1));
assert(!is_integer(99.1));
assert(!is_integer(undef));
assert(!is_integer(INF));
assert(!is_integer(NAN));
assert(!is_integer(false));
assert(!is_integer(true));
assert(!is_integer("foo"));
@ -161,16 +169,33 @@ module test_is_range() {
assert(!is_range(5));
assert(!is_range(INF));
assert(!is_range(-INF));
assert(!is_nan(NAN));
assert(!is_range(""));
assert(!is_range("foo"));
assert(!is_range([]));
assert(!is_range([3,4,5]));
assert(!is_range([INF:4:5]));
assert(!is_range([3:NAN:5]));
assert(!is_range([3:4:"a"]));
assert(is_range([3:1:5]));
}
test_is_nan();
test_is_range();
module test_valid_range() {
assert(valid_range([0:0]));
assert(valid_range([0:1:0]));
assert(valid_range([0:1:10]));
assert(valid_range([0.1:1.1:2.1]));
assert(valid_range([0:-1:0]));
assert(valid_range([10:-1:0]));
assert(valid_range([2.1:-1.1:0.1]));
assert(!valid_range([10:1:0]));
assert(!valid_range([2.1:1.1:0.1]));
assert(!valid_range([0:-1:10]));
assert(!valid_range([0.1:-1.1:2.1]));
}
test_valid_range();
module test_is_list_of() {
assert(is_list_of([3,4,5], 0));
assert(!is_list_of([3,4,undef], 0));
@ -181,10 +206,14 @@ module test_is_list_of() {
}
test_is_list_of();
module test_is_consistent() {
assert(is_consistent([]));
assert(is_consistent([[],[]]));
assert(is_consistent([3,4,5]));
assert(is_consistent([[3,4],[4,5],[6,7]]));
assert(is_consistent([[[3],4],[[4],5]]));
assert(!is_consistent(5));
assert(!is_consistent(undef));
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]]]));
@ -331,11 +360,25 @@ module test_scalar_vec3() {
assert(scalar_vec3([3]) == [3,0,0]);
assert(scalar_vec3([3,4]) == [3,4,0]);
assert(scalar_vec3([3,4],dflt=1) == [3,4,1]);
assert(scalar_vec3([3,"a"],dflt=1) == [3,"a",1]);
assert(scalar_vec3([3,[2]],dflt=1) == [3,[2],1]);
assert(scalar_vec3([3],dflt=1) == [3,1,1]);
assert(scalar_vec3([3,4,5]) == [3,4,5]);
assert(scalar_vec3([3,4,5,6]) == [3,4,5]);
assert(scalar_vec3([3,4,5,6]) == [3,4,5]);
}
test_scalar_vec3();
module test_segs() {
assert_equal(segs(50,$fn=8), 8);
assert_equal(segs(50,$fa=2,$fs=2), 158);
assert(segs(1)==5);
assert(segs(11)==30);
// assert(segs(1/0)==5);
// assert(segs(0/0)==5);
// assert(segs(undef)==5);
}
test_segs();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View File

@ -110,6 +110,8 @@ module test_approx() {
assert_equal(approx(1/3, 0.3333333333), true);
assert_equal(approx(-1/3, -0.3333333333), true);
assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true);
assert_equal(approx([1,[1,undef]], [1+1e-12,[1,true]]), false);
assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true);
}
test_approx();
@ -389,7 +391,6 @@ module test_mean() {
}
test_mean();
module test_median() {
assert_equal(median([2,3,7]), 4.5);
assert_equal(median([[1,2,3], [3,4,5], [8,9,10]]), [4.5,5.5,6.5]);
@ -397,6 +398,16 @@ module test_median() {
test_median();
module test_convolve() {
assert_equal(convolve([],[1,2,1]), []);
assert_equal(convolve([1,1],[]), []);
assert_equal(convolve([1,1],[1,2,1]), [1,3,3,1]);
assert_equal(convolve([1,2,3],[1,2,1]), [1,4,8,8,3]);
}
test_convolve();
module test_matrix_inverse() {
assert_approx(matrix_inverse(rot([20,30,40])), [[0.663413948169,0.556670399226,-0.5,0],[-0.47302145844,0.829769465589,0.296198132726,0],[0.579769465589,0.0400087565481,0.813797681349,0],[0,0,0,1]]);
}
@ -583,6 +594,24 @@ module test_factorial() {
}
test_factorial();
module test_binomial() {
assert_equal(binomial(1), [1,1]);
assert_equal(binomial(2), [1,2,1]);
assert_equal(binomial(3), [1,3,3,1]);
assert_equal(binomial(5), [1,5,10,10,5,1]);
}
test_binomial();
module test_binomial_coefficient() {
assert_equal(binomial_coefficient(2,1), 2);
assert_equal(binomial_coefficient(3,2), 3);
assert_equal(binomial_coefficient(4,2), 6);
assert_equal(binomial_coefficient(10,7), 120);
assert_equal(binomial_coefficient(10,7), binomial(10)[7]);
assert_equal(binomial_coefficient(15,4), binomial(15)[4]);
}
test_binomial_coefficient();
module test_gcd() {
assert_equal(gcd(15,25), 5);
@ -682,6 +711,7 @@ test_linear_solve();
module test_outer_product(){
assert_equal(outer_product([1,2,3],[4,5,6]), [[4,5,6],[8,10,12],[12,15,18]]);
assert_equal(outer_product([1,2],[4,5,6]), [[4,5,6],[8,10,12]]);
assert_equal(outer_product([9],[7]), [[63]]);
}
test_outer_product();
@ -782,8 +812,10 @@ test_deriv3();
module test_polynomial(){
assert_equal(polynomial([],12),0);
assert_equal(polynomial([],[12,4]),[0,0]);
assert_equal(polynomial([0],12),0);
assert_equal(polynomial([0],[12,4]),[0,0]);
// assert_equal(polynomial([],12),0);
// assert_equal(polynomial([],[12,4]),[0,0]);
assert_equal(polynomial([1,2,3,4],3),58);
assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]);
assert_equal(polynomial([0,0,2],4),2);
@ -879,16 +911,20 @@ test_qr_factor();
module test_poly_mult(){
assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]);
assert_equal(poly_mult([3,2,1],[]),[]);
assert_equal(poly_mult([3,2,1],[0]),[0]);
// assert_equal(poly_mult([3,2,1],[]),[]);
assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]);
assert_equal(poly_mult([[1,2],[],[5,6]]), []);
assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]);
assert_equal(poly_mult([[1,2],[0],[5,6]]), [0]);
// assert_equal(poly_mult([[1,2],[],[5,6]]), []);
assert_equal(poly_mult([[3,4,5],[0,0,0]]),[0]);
// assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]);
}
test_poly_mult();
module test_poly_div(){
assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]);
assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[0]]);
// assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]);
assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[], [1,2,3,4]]);
assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]);
assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]);
@ -899,7 +935,8 @@ test_poly_div();
module test_poly_add(){
assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]);
assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]);
assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
assert_equal(poly_add([1,2,3],-[1,2,3]),[0]);
// assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
}
test_poly_add();

View File

@ -9,6 +9,7 @@ module test_is_vector() {
assert(is_vector(1) == false);
assert(is_vector("foo") == false);
assert(is_vector(true) == false);
assert(is_vector([0,0,0],zero=true) == true);
assert(is_vector([0,0,0],zero=false) == false);
assert(is_vector([0,1,0],zero=true) == false);
@ -17,13 +18,6 @@ module test_is_vector() {
test_is_vector();
module test_add_scalar() {
assert(add_scalar([1,2,3],3) == [4,5,6]);
assert(add_scalar([[1,2,3],[3,4,5]],3) == [[4,5,6],[6,7,8]]);
}
test_add_scalar();
module test_vfloor() {
assert_equal(vfloor([2.0, 3.14, 18.9, 7]), [2,3,18,7]);
assert_equal(vfloor([-2.0, -3.14, -18.9, -7]), [-2,-4,-19,-7]);

View File

@ -19,42 +19,25 @@
// Arguments:
// v = The value to test to see if it is a vector.
// length = If given, make sure the vector is `length` items long.
// zero = If false, require that the length of the vector is not approximately zero. If true, require the length of the vector to be approx zero-length. Default: `undef` (don't check vector length.)
// eps = The minimum vector length that is considered non-zero. Default: `EPSILON` (`1e-9`)
// Example:
// is_vector(4); // Returns false
// is_vector([4,true,false]); // Returns false
// is_vector([3,4,INF,5]); // Returns false
// is_vector([3,4,5,6]); // Returns true
// is_vector([3,4,undef,5]); // Returns false
// is_vector([3,4,5],3); // Returns true
// is_vector([3,4,5],4); // Returns true
// is_vector([]); // Returns false
// is_vector([0,0,0],zero=true); // Returns true
// is_vector([0,0,0],zero=false); // Returns false
// is_vector([0,1,0],zero=true); // Returns false
// is_vector([0,0,1],zero=false); // Returns true
// is_vector(4); // Returns false
// is_vector([4,true,false]); // Returns false
// is_vector([3,4,INF,5]); // Returns false
// is_vector([3,4,5,6]); // Returns true
// is_vector([3,4,undef,5]); // Returns false
// is_vector([3,4,5],3); // Returns true
// is_vector([3,4,5],4); // Returns true
// is_vector([]); // Returns false
// is_vector([0,4,0],3,zero=false); // Returns true
// is_vector([0,0,0],zero=false); // Returns false
// is_vector([0,0,1e-12],zero=false); // Returns false
// is_vector([],zero=false); // Returns false
function is_vector(v,length,zero,eps=EPSILON) =
is_list(v) && is_num(0*(v*v))
&& (is_undef(length) || len(v)==length)
&& (is_undef(zero) || ((norm(v) >= eps) == !zero));
// Function: add_scalar()
// Usage:
// add_scalar(v,s);
// Description:
// Given a vector and a scalar, returns the vector with the scalar added to each item in it.
// If given a list of vectors, recursively adds the scalar to the each vector.
// Arguments:
// v = The initial list of values.
// s = A scalar value to add to every item in the vector.
// Example:
// add_scalar([1,2,3],3); // Returns: [4,5,6]
// add_scalar([[1,2,3],[3,4,5]],3); // Returns: [[4,5,6],[6,7,8]]
function add_scalar(v,s) = [for (x=v) is_list(x)? add_scalar(x,s) : x+s];
// Function: vang()
// Usage:
// theta = vang([X,Y]);
@ -63,6 +46,7 @@ function add_scalar(v,s) = [for (x=v) is_list(x)? add_scalar(x,s) : x+s];
// Given a 2D vector, returns the angle in degrees counter-clockwise from X+ on the XY plane.
// Given a 3D vector, returns [THETA,PHI] where THETA is the number of degrees counter-clockwise from X+ on the XY plane, and PHI is the number of degrees up from the X+ axis along the XZ plane.
function vang(v) =
assert( is_vector(v,2) || is_vector(v,3) , "Invalid vector")
len(v)==2? atan2(v.y,v.x) :
let(res=xyz_to_spherical(v)) [res[1], 90-res[2]];
@ -70,13 +54,19 @@ function vang(v) =
// Function: vmul()
// Description:
// Element-wise vector multiplication. Multiplies each element of vector `v1` by
// the corresponding element of vector `v2`. Returns a vector of the products.
// the corresponding element of vector `v2`. The vectors should have the same dimension.
// Returns a vector of the products.
// Arguments:
// v1 = The first vector.
// v2 = The second vector.
// Example:
// vmul([3,4,5], [8,7,6]); // Returns [24, 28, 30]
function vmul(v1, v2) = [for (i = [0:1:len(v1)-1]) v1[i]*v2[i]];
function vmul(v1, v2) =
// this thighter check can be done yet because it would break other codes in the library
// assert( is_vector(v1) && is_vector(v2,len(v1)), "Incompatible vectors")
assert( is_vector(v1) && is_vector(v2), "Invalid vector(s)")
[for (i = [0:1:len(v1)-1]) v1[i]*v2[i]];
// Function: vdiv()
@ -88,7 +78,9 @@ function vmul(v1, v2) = [for (i = [0:1:len(v1)-1]) v1[i]*v2[i]];
// v2 = The second vector.
// Example:
// vdiv([24,28,30], [8,7,6]); // Returns [3, 4, 5]
function vdiv(v1, v2) = [for (i = [0:1:len(v1)-1]) v1[i]/v2[i]];
function vdiv(v1, v2) =
assert( is_vector(v1) && is_vector(v2,len(v1)), "Incompatible vectors")
[for (i = [0:1:len(v1)-1]) v1[i]/v2[i]];
// Function: vabs()
@ -97,19 +89,25 @@ function vdiv(v1, v2) = [for (i = [0:1:len(v1)-1]) v1[i]/v2[i]];
// v = The vector to get the absolute values of.
// Example:
// vabs([-1,3,-9]); // Returns: [1,3,9]
function vabs(v) = [for (x=v) abs(x)];
function vabs(v) =
assert( is_vector(v), "Invalid vector" )
[for (x=v) abs(x)];
// Function: vfloor()
// Description:
// Returns the given vector after performing a `floor()` on all items.
function vfloor(v) = [for (x=v) floor(x)];
function vfloor(v) =
assert( is_vector(v), "Invalid vector" )
[for (x=v) floor(x)];
// Function: vceil()
// Description:
// Returns the given vector after performing a `ceil()` on all items.
function vceil(v) = [for (x=v) ceil(x)];
function vceil(v) =
assert( is_vector(v), "Invalid vector" )
[for (x=v) ceil(x)];
// Function: unit()
@ -137,6 +135,7 @@ function unit(v, error=[[["ASSERT"]]]) =
// Function: vector_angle()
// Usage:
// vector_angle(v1,v2);
// vector_angle([v1,v2]);
// vector_angle(PT1,PT2,PT3);
// vector_angle([PT1,PT2,PT3]);
// Description:
@ -156,34 +155,36 @@ function unit(v, error=[[["ASSERT"]]]) =
// vector_angle([10,0,10], [0,0,0], [-10,10,0]); // Returns: 120
// vector_angle([[10,0,10], [0,0,0], [-10,10,0]]); // Returns: 120
function vector_angle(v1,v2,v3) =
let(
vecs = !is_undef(v3)? [v1-v2,v3-v2] :
!is_undef(v2)? [v1,v2] :
len(v1) == 3? [v1[0]-v1[1],v1[2]-v1[1]] :
len(v1) == 2? v1 :
assert(false, "Bad arguments to vector_angle()"),
is_valid = is_vector(vecs[0]) && is_vector(vecs[1]) && vecs[0]*0 == vecs[1]*0
assert( ( is_undef(v3) && ( is_undef(v2) || same_shape(v1,v2) ) )
|| is_consistent([v1,v2,v3]) ,
"Bad arguments.")
assert( is_vector(v1) || is_consistent(v1), "Bad arguments.")
let( vecs = ! is_undef(v3) ? [v1-v2,v3-v2] :
! is_undef(v2) ? [v1,v2] :
len(v1) == 3 ? [v1[0]-v1[1], v1[2]-v1[1]]
: v1
)
assert(is_valid, "Bad arguments to vector_angle()")
assert(is_vector(vecs[0],2) || is_vector(vecs[0],3), "Bad arguments.")
let(
norm0 = norm(vecs[0]),
norm1 = norm(vecs[1])
)
assert(norm0>0 && norm1>0,"Zero length vector given to vector_angle()")
assert(norm0>0 && norm1>0, "Zero length vector.")
// NOTE: constrain() corrects crazy FP rounding errors that exceed acos()'s domain.
acos(constrain((vecs[0]*vecs[1])/(norm0*norm1), -1, 1));
// Function: vector_axis()
// Usage:
// vector_axis(v1,v2);
// vector_axis([v1,v2]);
// vector_axis(PT1,PT2,PT3);
// vector_axis([PT1,PT2,PT3]);
// Description:
// If given a single list of two vectors, like `vector_axis([V1,V2])`, returns the vector perpendicular the two vectors V1 and V2.
// If given a single list of three points, like `vector_axis([A,B,C])`, returns the vector perpendicular the line segments AB and BC.
// If given two vectors, like `vector_axis(V1,V1)`, returns the vector perpendicular the two vectors V1 and V2.
// If given three points, like `vector_axis(A,B,C)`, returns the vector perpendicular the line segments AB and BC.
// If given a single list of three points, like `vector_axis([A,B,C])`, returns the vector perpendicular to the plane through a, B and C.
// If given two vectors, like `vector_axis(V1,V2)`, returns the vector perpendicular to the two vectors V1 and V2.
// If given three points, like `vector_axis(A,B,C)`, returns the vector perpendicular to the plane through a, B and C.
// Arguments:
// v1 = First vector or point.
// v2 = Second vector or point.
@ -199,28 +200,23 @@ function vector_axis(v1,v2=undef,v3=undef) =
is_vector(v3)
? assert(is_consistent([v3,v2,v1]), "Bad arguments.")
vector_axis(v1-v2, v3-v2)
:
assert( is_undef(v3), "Bad arguments.")
is_undef(v2)
? assert( is_list(v1), "Bad arguments.")
len(v1) == 2
? vector_axis(v1[0],v1[1])
: vector_axis(v1[0],v1[1],v1[2])
:
assert(
is_vector(v1,zero=false) &&
is_vector(v2,zero=false) &&
is_consistent([v1,v2]),
"Bad arguments."
)
let(
eps = 1e-6,
w1 = point3d(v1/norm(v1)),
w2 = point3d(v2/norm(v2)),
w3 = (norm(w1-w2) > eps && norm(w1+w2) > eps) ? w2
: (norm(vabs(w2)-UP) > eps) ? UP
: RIGHT
) unit(cross(w1,w3));
: assert( is_undef(v3), "Bad arguments.")
is_undef(v2)
? assert( is_list(v1), "Bad arguments.")
len(v1) == 2
? vector_axis(v1[0],v1[1])
: vector_axis(v1[0],v1[1],v1[2])
: assert( is_vector(v1,zero=false) && is_vector(v2,zero=false) && is_consistent([v1,v2])
, "Bad arguments.")
let(
eps = 1e-6,
w1 = point3d(v1/norm(v1)),
w2 = point3d(v2/norm(v2)),
w3 = (norm(w1-w2) > eps && norm(w1+w2) > eps) ? w2
: (norm(vabs(w2)-UP) > eps)? UP
: RIGHT
) unit(cross(w1,w3));
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap