mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2024-12-29 16:29:40 +00:00
Tweaked vector_axis() and vector_angle() to work with lists of points and vectors.
This commit is contained in:
parent
80192c53ab
commit
a63e9aca8e
2 changed files with 46 additions and 11 deletions
|
@ -42,24 +42,38 @@ module test_vector_angle() {
|
||||||
for (a=vecs, b=vecs) {
|
for (a=vecs, b=vecs) {
|
||||||
if(a==b) {
|
if(a==b) {
|
||||||
assert(vector_angle(a,b)==0);
|
assert(vector_angle(a,b)==0);
|
||||||
|
assert(vector_angle([a,b])==0);
|
||||||
} else if(a==-b) {
|
} else if(a==-b) {
|
||||||
assert(vector_angle(a,b)==180);
|
assert(vector_angle(a,b)==180);
|
||||||
|
assert(vector_angle([a,b])==180);
|
||||||
} else {
|
} else {
|
||||||
assert(vector_angle(a,b)==90);
|
assert(vector_angle(a,b)==90);
|
||||||
|
assert(vector_angle([a,b])==90);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(abs(vector_angle([10,10,0],[10,0,0])-45) < EPSILON);
|
assert(abs(vector_angle([10,10,0],[10,0,0])-45) < EPSILON);
|
||||||
|
assert(abs(vector_angle([[10,10,0],[10,0,0]])-45) < EPSILON);
|
||||||
|
assert(abs(vector_angle([11,11,1],[1,1,1],[11,-11,1])-90) < EPSILON);
|
||||||
|
assert(abs(vector_angle([[11,11,1],[1,1,1],[11,-11,1]])-90) < EPSILON);
|
||||||
}
|
}
|
||||||
test_vector_angle();
|
test_vector_angle();
|
||||||
|
|
||||||
|
|
||||||
module test_vector_axis() {
|
module test_vector_axis() {
|
||||||
assert(norm(vector_axis([10,0,0],[10,10,0]) - [0,0,1]) < EPSILON);
|
assert(norm(vector_axis([10,0,0],[10,10,0]) - [0,0,1]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[10,0,0],[10,10,0]]) - [0,0,1]) < EPSILON);
|
||||||
assert(norm(vector_axis([10,0,0],[0,10,0]) - [0,0,1]) < EPSILON);
|
assert(norm(vector_axis([10,0,0],[0,10,0]) - [0,0,1]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[10,0,0],[0,10,0]]) - [0,0,1]) < EPSILON);
|
||||||
assert(norm(vector_axis([0,10,0],[10,0,0]) - [0,0,-1]) < EPSILON);
|
assert(norm(vector_axis([0,10,0],[10,0,0]) - [0,0,-1]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[0,10,0],[10,0,0]]) - [0,0,-1]) < EPSILON);
|
||||||
assert(norm(vector_axis([0,0,10],[10,0,0]) - [0,1,0]) < EPSILON);
|
assert(norm(vector_axis([0,0,10],[10,0,0]) - [0,1,0]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[0,0,10],[10,0,0]]) - [0,1,0]) < EPSILON);
|
||||||
assert(norm(vector_axis([10,0,0],[0,0,10]) - [0,-1,0]) < EPSILON);
|
assert(norm(vector_axis([10,0,0],[0,0,10]) - [0,-1,0]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[10,0,0],[0,0,10]]) - [0,-1,0]) < EPSILON);
|
||||||
assert(norm(vector_axis([10,0,10],[0,-10,0]) - [sin(45),0,-sin(45)]) < EPSILON);
|
assert(norm(vector_axis([10,0,10],[0,-10,0]) - [sin(45),0,-sin(45)]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[10,0,10],[0,-10,0]]) - [sin(45),0,-sin(45)]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([11,1,11],[1,1,1],[1,-9,1]) - [sin(45),0,-sin(45)]) < EPSILON);
|
||||||
|
assert(norm(vector_axis([[11,1,11],[1,1,1],[1,-9,1]]) - [sin(45),0,-sin(45)]) < EPSILON);
|
||||||
}
|
}
|
||||||
test_vector_axis();
|
test_vector_axis();
|
||||||
|
|
||||||
|
|
43
vectors.scad
43
vectors.scad
|
@ -85,35 +85,56 @@ function vquantup(v,m) = [for (x=v) quantup(x,m)];
|
||||||
// Function: vector_angle()
|
// Function: vector_angle()
|
||||||
// Usage:
|
// Usage:
|
||||||
// vector_angle(v1,v2);
|
// vector_angle(v1,v2);
|
||||||
|
// vector_angle([PT1,PT2,PT3]);
|
||||||
|
// vector_angle(PT1,PT2,PT3);
|
||||||
// Description:
|
// Description:
|
||||||
// Returns angle in degrees between two vectors of similar dimensions.
|
// If given a single list of two vectors, like `vector_angle([V1,V2])`, returns the angle between the two vectors V1 and V2.
|
||||||
|
// If given a single list of three points, like `vector_angle([A,B,C])`, returns the angle between the line segments AB and BC.
|
||||||
|
// If given two vectors, like `vector_angle(V1,V1)`, returns the angle between the two vectors V1 and V2.
|
||||||
|
// If given three points, like `vector_angle(A,B,C)`, returns the angle between the line segments AB and BC.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// v1 = First vector.
|
// v1 = First vector or point.
|
||||||
// v2 = Second vector.
|
// v2 = Second vector or point.
|
||||||
|
// v3 = Third point in three point mode.
|
||||||
// NOTE: constrain() corrects crazy FP rounding errors that exceed acos()'s domain.
|
// NOTE: constrain() corrects crazy FP rounding errors that exceed acos()'s domain.
|
||||||
function vector_angle(v1,v2) =
|
function vector_angle(v1,v2=undef,v3=undef) =
|
||||||
assert(is_vector(v1))
|
(is_list(v1) && is_list(v1[0]) && is_undef(v2) && is_undef(v3))? (
|
||||||
assert(is_vector(v2))
|
assert(is_vector(v1.x))
|
||||||
acos(constrain((v1*v2)/(norm(v1)*norm(v2)), -1, 1));
|
assert(is_vector(v1.y))
|
||||||
|
len(v1)==3? assert(is_vector(v1.z)) vector_angle(v1.x, v1.y, v1.z) :
|
||||||
|
len(v1)==2? vector_angle(v1.x, v1.y) :
|
||||||
|
assert(false, "Bad arguments.")
|
||||||
|
) :
|
||||||
|
(is_vector(v1) && is_vector(v2) && is_vector(v3))? vector_angle(v1-v2, v3-v2) :
|
||||||
|
(is_vector(v1) && is_vector(v2) && is_undef(v3))? acos(constrain((v1*v2)/(norm(v1)*norm(v2)), -1, 1)) :
|
||||||
|
assert(false, "Bad arguments.");
|
||||||
|
|
||||||
|
|
||||||
// Function: vector_axis()
|
// Function: vector_axis()
|
||||||
// Usage:
|
// Usage:
|
||||||
// vector_xis(v1,v2);
|
// vector_axis(v1,v2);
|
||||||
// Description:
|
// Description:
|
||||||
// Returns the vector perpendicular to both of the given vectors.
|
// Returns the vector perpendicular to both of the given vectors.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// v1 = First vector.
|
// v1 = First vector.
|
||||||
// v2 = Second vector.
|
// v2 = Second vector.
|
||||||
function vector_axis(v1,v2) =
|
function vector_axis(v1,v2=undef,v3=undef) =
|
||||||
let(
|
(is_list(v1) && is_list(v1[0]) && is_undef(v2) && is_undef(v3))? (
|
||||||
|
assert(is_vector(v1.x))
|
||||||
|
assert(is_vector(v1.y))
|
||||||
|
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(false, "Bad arguments.")
|
||||||
|
) :
|
||||||
|
(is_vector(v1) && is_vector(v2) && is_vector(v3))? vector_axis(v1-v2, v3-v2) :
|
||||||
|
(is_vector(v1) && is_vector(v2) && is_undef(v3))? let(
|
||||||
eps = 1e-6,
|
eps = 1e-6,
|
||||||
v1 = point3d(v1/norm(v1)),
|
v1 = point3d(v1/norm(v1)),
|
||||||
v2 = point3d(v2/norm(v2)),
|
v2 = point3d(v2/norm(v2)),
|
||||||
v3 = (norm(v1-v2) > eps && norm(v1+v2) > eps)? v2 :
|
v3 = (norm(v1-v2) > eps && norm(v1+v2) > eps)? v2 :
|
||||||
(norm(vabs(v2)-UP) > eps)? UP :
|
(norm(vabs(v2)-UP) > eps)? UP :
|
||||||
RIGHT
|
RIGHT
|
||||||
) normalize(cross(v1,v3));
|
) normalize(cross(v1,v3)) : assert(false, "Bad arguments.");
|
||||||
|
|
||||||
|
|
||||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||||
|
|
Loading…
Reference in a new issue