Add tests to identity-quat patch and also take an axis not a full value for the identity fallback
This commit is contained in:
parent
f27f5520b1
commit
9cd858a0ef
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user