From 9cd858a0effd81162dfd4d5d48e5a693833abcf4 Mon Sep 17 00:00:00 2001 From: mcc Date: Sat, 25 Apr 2020 18:32:48 -0400 Subject: [PATCH] Add tests to identity-quat patch and also take an axis not a full value for the identity fallback --- modules/quat.lua | 13 +++++++------ spec/quat_spec.lua | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/quat.lua b/modules/quat.lua index 8b01421..0e1bace 100644 --- a/modules/quat.lua +++ b/modules/quat.lua @@ -364,12 +364,12 @@ end --- Convert a quaternion into an angle plus axis components. -- @tparam quat a Quaternion to convert --- @tparam identity an optional table to return on identity/degenerate quaternions (by default returns 0,0,0,1) +-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,0,0,1) -- @treturn number angle -- @treturn x axis-x -- @treturn y axis-y -- @treturn z axis-z -function quat.to_angle_axis_unpack(a, identity) +function quat.to_angle_axis_unpack(a, identityAxis) if a.w > 1 or a.w < -1 then a = a:normalize() end @@ -378,8 +378,8 @@ function quat.to_angle_axis_unpack(a, identity) -- Normally an identity quat would return a nonsense answer, so we return an arbitrary zero rotation early. -- FIXME: Is it safe to assume there are *no* valid quaternions with nonzero degenerate lengths? if a.x*a.x + a.y*a.y + a.z*a.z < constants.DBL_EPSILON*constants.DBL_EPSILON then - if identity then - return unpack(identity) + if identityAxis then + return 0,identityAxis:unpack() else return 0,0,0,1 end @@ -404,10 +404,11 @@ end --- Convert a quaternion into an angle/axis pair. -- @tparam quat a Quaternion to convert +-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,vec3(0,0,1)) -- @treturn number angle -- @treturn vec3 axis -function quat.to_angle_axis(a, default) - local angle, x, y, z = a:to_angle_axis_unpack(default) +function quat.to_angle_axis(a, identityAxis) + local angle, x, y, z = a:to_angle_axis_unpack(identityAxis) return angle, vec3(x, y, z) end diff --git a/spec/quat_spec.lua b/spec/quat_spec.lua index ecb2b8a..9867dc8 100644 --- a/spec/quat_spec.lua +++ b/spec/quat_spec.lua @@ -310,6 +310,22 @@ describe("quat:", function() assert.is.equal(3, axis.z) end) + it("converts between a quaternion and angle/axis (identity quaternion) (by component)", function() + local angle, x,y,z = quat():to_angle_axis_unpack() + assert.is.equal(0, angle) + assert.is.equal(0, x) + assert.is.equal(0, y) + assert.is.equal(1, z) + end) + + it("converts between a quaternion and angle/axis (identity quaternion with fallback)", function() + local angle, axis = quat():to_angle_axis(vec3(2,3,4)) + assert.is.equal(0, angle) + assert.is.equal(2, axis.x) + assert.is.equal(3, axis.y) + assert.is.equal(4, axis.z) + end) + it("gets a string representation of a quaternion", function() local a = quat():to_string() assert.is.equal("(+0.000,+0.000,+0.000,+1.000)", a)