mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
Merge pull request #210 from revarbat/revarbat_dev
Added zero=undef/true/false arg to is_vector. Bugfix for vector_axis()
This commit is contained in:
commit
1d03a6aa93
3 changed files with 39 additions and 18 deletions
|
@ -9,6 +9,10 @@ module test_is_vector() {
|
||||||
assert(is_vector(1) == false);
|
assert(is_vector(1) == false);
|
||||||
assert(is_vector("foo") == false);
|
assert(is_vector("foo") == false);
|
||||||
assert(is_vector(true) == 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);
|
||||||
|
assert(is_vector([0,0,1],zero=false) == true);
|
||||||
}
|
}
|
||||||
test_is_vector();
|
test_is_vector();
|
||||||
|
|
||||||
|
|
51
vectors.scad
51
vectors.scad
|
@ -19,6 +19,8 @@
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// v = The value to test to see if it is a vector.
|
// v = The value to test to see if it is a vector.
|
||||||
// length = If given, make sure the vector is `length` items long.
|
// 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:
|
// Example:
|
||||||
// is_vector(4); // Returns false
|
// is_vector(4); // Returns false
|
||||||
// is_vector([4,true,false]); // Returns false
|
// is_vector([4,true,false]); // Returns false
|
||||||
|
@ -28,8 +30,14 @@
|
||||||
// is_vector([3,4,5],3); // Returns true
|
// is_vector([3,4,5],3); // Returns true
|
||||||
// is_vector([3,4,5],4); // Returns true
|
// is_vector([3,4,5],4); // Returns true
|
||||||
// is_vector([]); // Returns false
|
// is_vector([]); // Returns false
|
||||||
function is_vector(v,length) =
|
// is_vector([0,0,0],zero=true); // Returns true
|
||||||
is_list(v) && is_num(0*(v*v)) && (is_undef(length)||len(v)==length);
|
// 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
|
||||||
|
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()
|
// Function: add_scalar()
|
||||||
|
@ -188,22 +196,31 @@ function vector_angle(v1,v2,v3) =
|
||||||
// vector_axis([10,0,10], [0,0,0], [-10,10,0]); // Returns: [-0.57735, -0.57735, 0.57735]
|
// vector_axis([10,0,10], [0,0,0], [-10,10,0]); // Returns: [-0.57735, -0.57735, 0.57735]
|
||||||
// vector_axis([[10,0,10], [0,0,0], [-10,10,0]]); // Returns: [-0.57735, -0.57735, 0.57735]
|
// vector_axis([[10,0,10], [0,0,0], [-10,10,0]]); // Returns: [-0.57735, -0.57735, 0.57735]
|
||||||
function vector_axis(v1,v2=undef,v3=undef) =
|
function vector_axis(v1,v2=undef,v3=undef) =
|
||||||
(is_list(v1) && is_list(v1[0]) && is_undef(v2) && is_undef(v3))? (
|
is_vector(v3)
|
||||||
assert(is_vector(v1.x))
|
? assert(is_consistent([v3,v2,v1]), "Bad arguments.")
|
||||||
assert(is_vector(v1.y))
|
vector_axis(v1-v2, v3-v2)
|
||||||
len(v1)==3? assert(is_vector(v1.z)) vector_axis(v1.x, v1.y, v1.z) :
|
:
|
||||||
len(v1)==2? vector_axis(v1.x, v1.y) :
|
assert( is_undef(v3), "Bad arguments.")
|
||||||
assert(false, "Bad arguments.")
|
is_undef(v2)
|
||||||
) :
|
? assert( is_list(v1), "Bad arguments.")
|
||||||
(is_vector(v1) && is_vector(v2) && is_vector(v3))? vector_axis(v1-v2, v3-v2) :
|
len(v1) == 2
|
||||||
(is_vector(v1) && is_vector(v2) && is_undef(v3))? let(
|
? 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,
|
eps = 1e-6,
|
||||||
v1 = point3d(v1/norm(v1)),
|
w1 = point3d(v1/norm(v1)),
|
||||||
v2 = point3d(v2/norm(v2)),
|
w2 = point3d(v2/norm(v2)),
|
||||||
v3 = (norm(v1-v2) > eps && norm(v1+v2) > eps)? v2 :
|
w3 = (norm(w1-w2) > eps && norm(w1+w2) > eps) ? w2
|
||||||
(norm(vabs(v2)-UP) > eps)? UP :
|
: (norm(vabs(w2)-UP) > eps) ? UP
|
||||||
RIGHT
|
: RIGHT
|
||||||
) unit(cross(v1,v3)) : assert(false, "Bad arguments.");
|
) unit(cross(w1,w3));
|
||||||
|
|
||||||
|
|
||||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
BOSL_VERSION = [2,0,396];
|
BOSL_VERSION = [2,0,397];
|
||||||
|
|
||||||
|
|
||||||
// Section: BOSL Library Version Functions
|
// Section: BOSL Library Version Functions
|
||||||
|
|
Loading…
Reference in a new issue