mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-30 08:21:24 +02:00
input data checks and addition of many new functions
1. Input data check in all functions 2. Definition of new functions for data check, new interpolations functions, quaternion from matrix, rotation path interpolating quaternions, inverse, exp, ln, power.
This commit is contained in:
@@ -8,6 +8,7 @@ function rec_cmp(a,b,eps=1e-9) =
|
||||
is_list(a)? len(a)==len(b) && all([for (i=idx(a)) rec_cmp(a[i],b[i],eps=eps)]) :
|
||||
a == b;
|
||||
|
||||
function Qstandard(q) = sign([for(qi=q) if( ! approx(qi,0)) qi,0 ][0])*q;
|
||||
|
||||
module verify_f(actual,expected) {
|
||||
if (!rec_cmp(actual,expected)) {
|
||||
@@ -22,6 +23,15 @@ module verify_f(actual,expected) {
|
||||
}
|
||||
|
||||
|
||||
module test_is_quat() {
|
||||
verify_f(Q_is_quat([0]),false);
|
||||
verify_f(Q_is_quat([0,0,0,0]),false);
|
||||
verify_f(Q_is_quat([1,0,2,0]),true);
|
||||
verify_f(Q_is_quat([1,0,2,0,0]),false);
|
||||
}
|
||||
test_is_quat();
|
||||
|
||||
|
||||
module test_Quat() {
|
||||
verify_f(Quat(UP,0),[0,0,0,1]);
|
||||
verify_f(Quat(FWD,0),[0,0,0,1]);
|
||||
@@ -92,6 +102,15 @@ module test_QuatXYZ() {
|
||||
test_QuatXYZ();
|
||||
|
||||
|
||||
module test_Q_From_to() {
|
||||
verify_f(Q_Mul(Q_From_to([1,2,3], [4,5,2]),Q_From_to([4,5,2], [1,2,3])), Q_Ident());
|
||||
verify_f(Q_Matrix4(Q_From_to([1,2,3], [4,5,2])), rot(from=[1,2,3],to=[4,5,2]));
|
||||
verify_f(Qrot(Q_From_to([1,2,3], -[1,2,3]),[1,2,3]), -[1,2,3]);
|
||||
verify_f(unit(Qrot(Q_From_to([1,2,3], [4,5,2]),[1,2,3])), unit([4,5,2]));
|
||||
}
|
||||
test_Q_From_to();
|
||||
|
||||
|
||||
module test_Q_Ident() {
|
||||
verify_f(Q_Ident(), [0,0,0,1]);
|
||||
}
|
||||
@@ -207,6 +226,16 @@ module test_Q_Conj() {
|
||||
test_Q_Conj();
|
||||
|
||||
|
||||
module test_Q_Inverse() {
|
||||
|
||||
verify_f(Q_Inverse([1,0,0,1]),[-1,0,0,1]/sqrt(2));
|
||||
verify_f(Q_Inverse([0,1,1,0]),[0,-1,-1,0]/sqrt(2));
|
||||
verify_f(Q_Inverse(QuatXYZ([23,45,67])),Q_Conj(QuatXYZ([23,45,67])));
|
||||
verify_f(Q_Mul(Q_Inverse(QuatXYZ([23,45,67])),QuatXYZ([23,45,67])),Q_Ident());
|
||||
}
|
||||
test_Q_Inverse();
|
||||
|
||||
|
||||
module test_Q_Norm() {
|
||||
verify_f(Q_Norm([1,0,0,1]),1.414213562);
|
||||
verify_f(Q_Norm([0,1,1,0]),1.414213562);
|
||||
@@ -276,6 +305,10 @@ module test_Q_Angle() {
|
||||
verify_f(Q_Angle(QuatY(-37)),37);
|
||||
verify_f(Q_Angle(QuatZ(37)),37);
|
||||
verify_f(Q_Angle(QuatZ(-37)),37);
|
||||
|
||||
verify_f(Q_Angle(QuatZ(-37),QuatZ(-37)), 0);
|
||||
verify_f(Q_Angle(QuatZ( 37.123),QuatZ(-37.123)), 74.246);
|
||||
verify_f(Q_Angle(QuatX( 37),QuatY(-37)), 51.86293283);
|
||||
}
|
||||
test_Q_Angle();
|
||||
|
||||
@@ -288,4 +321,87 @@ module test_Qrot() {
|
||||
test_Qrot();
|
||||
|
||||
|
||||
module test_Q_Rotation() {
|
||||
verify_f(Qstandard(Q_Rotation(Q_Matrix3(Quat([12,34,56],33)))),Qstandard(Quat([12,34,56],33)));
|
||||
verify_f(Q_Matrix3(Q_Rotation(Q_Matrix3(QuatXYZ([12,34,56])))),
|
||||
Q_Matrix3(QuatXYZ([12,34,56])));
|
||||
}
|
||||
test_Q_Rotation();
|
||||
|
||||
|
||||
module test_Q_Rotation_path() {
|
||||
|
||||
verify_f(Q_Rotation_path(QuatX(135), 5, QuatY(13.5))[0] , Q_Matrix4(QuatX(135)));
|
||||
verify_f(Q_Rotation_path(QuatX(135), 11, QuatY(13.5))[11] , yrot(13.5));
|
||||
verify_f(Q_Rotation_path(QuatX(135), 16, QuatY(13.5))[8] , Q_Rotation_path(QuatX(135), 8, QuatY(13.5))[4]);
|
||||
verify_f(Q_Rotation_path(QuatX(135), 16, QuatY(13.5))[7] ,
|
||||
Q_Rotation_path(QuatY(13.5),16, QuatX(135))[9]);
|
||||
|
||||
verify_f(Q_Rotation_path(QuatX(11), 5)[0] , xrot(11));
|
||||
verify_f(Q_Rotation_path(QuatX(11), 5)[4] , xrot(55));
|
||||
|
||||
}
|
||||
test_Q_Rotation_path();
|
||||
|
||||
|
||||
module test_Q_Nlerp() {
|
||||
verify_f(Q_Nlerp(QuatX(45),QuatY(30),0.0),QuatX(45));
|
||||
verify_f(Q_Nlerp(QuatX(45),QuatY(30),0.5),[0.1967063121, 0.1330377423, 0, 0.9713946602]);
|
||||
verify_f(Q_Rotation_path(QuatX(135), 16, QuatY(13.5))[8] , Q_Matrix4(Q_Nlerp(QuatX(135), QuatY(13.5),0.5)));
|
||||
verify_f(Q_Nlerp(QuatX(45),QuatY(30),1.0),QuatY(30));
|
||||
}
|
||||
test_Q_Nlerp();
|
||||
|
||||
|
||||
module test_Q_Squad() {
|
||||
verify_f(Q_Squad(QuatX(45),QuatZ(30),QuatX(90),QuatY(30),0.0),QuatX(45));
|
||||
verify_f(Q_Squad(QuatX(45),QuatZ(30),QuatX(90),QuatY(30),1.0),QuatY(30));
|
||||
verify_f(Q_Squad(QuatX(0),QuatX(30),QuatX(90),QuatX(120),0.5),
|
||||
Q_Slerp(QuatX(0),QuatX(120),0.5));
|
||||
verify_f(Q_Squad(QuatY(0),QuatY(0),QuatX(120),QuatX(120),0.3),
|
||||
Q_Slerp(QuatY(0),QuatX(120),0.3));
|
||||
}
|
||||
test_Q_Squad();
|
||||
|
||||
|
||||
module test_Q_exp() {
|
||||
verify_f(Q_exp(Q_Ident()), exp(1)*Q_Ident());
|
||||
verify_f(Q_exp([0,0,0,33.7]), exp(33.7)*Q_Ident());
|
||||
verify_f(Q_exp(Q_ln(Q_Ident())), Q_Ident());
|
||||
verify_f(Q_exp(Q_ln([1,2,3,0])), [1,2,3,0]);
|
||||
verify_f(Q_exp(Q_ln(QuatXYZ([31,27,34]))), QuatXYZ([31,27,34]));
|
||||
let(q=QuatXYZ([12,23,34]))
|
||||
verify_f(Q_exp(q+Q_Inverse(q)),Q_Mul(Q_exp(q),Q_exp(Q_Inverse(q))));
|
||||
|
||||
}
|
||||
test_Q_exp();
|
||||
|
||||
|
||||
module test_Q_ln() {
|
||||
verify_f(Q_ln([1,2,3,0]), [24.0535117721, 48.1070235442, 72.1605353164, 1.31952866481]);
|
||||
verify_f(Q_ln(Q_Ident()), [0,0,0,0]);
|
||||
verify_f(Q_ln(5.5*Q_Ident()), [0,0,0,ln(5.5)]);
|
||||
verify_f(Q_ln(Q_exp(QuatXYZ([13,37,43]))), QuatXYZ([13,37,43]));
|
||||
verify_f(Q_ln(QuatXYZ([12,23,34]))+Q_ln(Q_Inverse(QuatXYZ([12,23,34]))), [0,0,0,0]);
|
||||
}
|
||||
test_Q_ln();
|
||||
|
||||
|
||||
module test_Q_pow() {
|
||||
q = Quat([1,2,3],77);
|
||||
verify_f(Q_pow(q,1), q);
|
||||
verify_f(Q_pow(q,0), Q_Ident());
|
||||
verify_f(Q_pow(q,-1), Q_Inverse(q));
|
||||
verify_f(Q_pow(q,2), Q_Mul(q,q));
|
||||
verify_f(Q_pow(q,3), Q_Mul(q,Q_pow(q,2)));
|
||||
verify_f(Q_Mul(Q_pow(q,0.456),Q_pow(q,0.544)), q);
|
||||
verify_f(Q_Mul(Q_pow(q,0.335),Q_Mul(Q_pow(q,.552),Q_pow(q,.113))), q);
|
||||
}
|
||||
test_Q_pow();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
Reference in New Issue
Block a user