Updated tests, fixed couple bugs
This commit is contained in:
parent
a7f9247838
commit
08b23e394b
@ -7,7 +7,7 @@ local vec2 = require(modules .. "vec2")
|
||||
local vec3 = require(modules .. "vec3")
|
||||
local quat = require(modules .. "quat")
|
||||
local utils = require(modules .. "utils")
|
||||
local DBL_EPSILON = constants.DBL.EPSILON
|
||||
local DBL_EPSILON = constants.DBL_EPSILON
|
||||
local sqrt = math.sqrt
|
||||
local cos = math.cos
|
||||
local sin = math.sin
|
||||
@ -602,7 +602,7 @@ end
|
||||
-- @tparam mat4 a Matrix to be converted
|
||||
-- @treturn quat out
|
||||
function mat4.to_quat(a)
|
||||
tmp = identity(tmp):transpose(a)
|
||||
tmp = a:transpose()
|
||||
|
||||
local w = sqrt(1 + tmp[1] + tmp[6] + tmp[11]) / 2
|
||||
local scale = w * 4
|
||||
|
@ -25,10 +25,6 @@ local function new(x, y, z, w)
|
||||
}, quat_mt)
|
||||
end
|
||||
|
||||
-- Statically allocate a temporary variable used in some of our functions.
|
||||
local tmp = new()
|
||||
local qv, uv, uuv = vec3(), vec3(), vec3()
|
||||
|
||||
-- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
|
||||
local status, ffi
|
||||
if type(jit) == "table" and jit.status() then
|
||||
@ -39,6 +35,10 @@ if type(jit) == "table" and jit.status() then
|
||||
end
|
||||
end
|
||||
|
||||
-- Statically allocate a temporary variable used in some of our functions.
|
||||
local tmp = new()
|
||||
local qv, uv, uuv = vec3(), vec3(), vec3()
|
||||
|
||||
--- Constants
|
||||
-- @table quat
|
||||
-- @field unit Unit quaternion
|
||||
@ -75,7 +75,7 @@ function quat.new(x, y, z, w)
|
||||
return new(xx, yy, zz, ww)
|
||||
end
|
||||
|
||||
return new()
|
||||
return new(0, 0, 0, 1)
|
||||
end
|
||||
|
||||
--- Create a quaternion from an angle/axis pair.
|
||||
@ -165,7 +165,7 @@ end
|
||||
-- @treturn quat out
|
||||
function quat.pow(a, n)
|
||||
if n == 0 then
|
||||
return new()
|
||||
return new(0, 0, 0, 1)
|
||||
end
|
||||
|
||||
if n > 0 then
|
||||
|
@ -61,7 +61,7 @@ function vec3.new(x, y, z)
|
||||
assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")
|
||||
assert(type(zz) == "number", "new: Wrong argument type for z (<number> expected)")
|
||||
|
||||
return new(x, y, z)
|
||||
return new(xx, yy, zz)
|
||||
|
||||
-- number
|
||||
elseif type(x) == "number" then
|
||||
|
@ -25,11 +25,11 @@ describe("intersect:", function()
|
||||
assert.is_true(intersect.point_aabb(a, c))
|
||||
assert.is_not_true(intersect.point_aabb(b, c))
|
||||
end)
|
||||
--[[
|
||||
it("intersects a point with a frustum", function()
|
||||
|
||||
it("intersects a point with a frustum", function()
|
||||
pending("TODO")
|
||||
end)
|
||||
--]]
|
||||
|
||||
it("intersects a ray with a triangle", function()
|
||||
local a = {
|
||||
position = vec3(0.5, 0.5, -1),
|
||||
@ -170,8 +170,7 @@ describe("intersect:", function()
|
||||
end)
|
||||
|
||||
it("intersects an aabb with an obb", function()
|
||||
local r = mat4()
|
||||
r:rotate(r, math.pi / 4, vec3.unit_z)
|
||||
local r = mat4():rotate(math.pi / 4, vec3.unit_z)
|
||||
|
||||
local a = {
|
||||
position = vec3(),
|
||||
@ -207,11 +206,11 @@ describe("intersect:", function()
|
||||
assert.is_true(intersect.aabb_sphere(a, c))
|
||||
assert.is_not_true(intersect.aabb_sphere(b, c))
|
||||
end)
|
||||
--[[
|
||||
it("intersects an aabb with a frustum", function()
|
||||
|
||||
it("intersects an aabb with a frustum", function()
|
||||
pending("TODO")
|
||||
end)
|
||||
--]]
|
||||
|
||||
it("encapsulates an aabb", function()
|
||||
local a = {
|
||||
min = vec3(-1),
|
||||
@ -267,9 +266,12 @@ describe("intersect:", function()
|
||||
assert.is_true(intersect.sphere_sphere(a, c))
|
||||
assert.is_not_true(intersect.sphere_sphere(b, c))
|
||||
end)
|
||||
--[[
|
||||
it("intersects a sphere with a frustum", function()
|
||||
|
||||
it("intersects a sphere with a frustum", function()
|
||||
pending("TODO")
|
||||
end)
|
||||
|
||||
it("intersects a capsule with another capsule", function()
|
||||
pending("TODO")
|
||||
end)
|
||||
--]]
|
||||
end)
|
||||
|
@ -142,7 +142,7 @@ describe("mat4:", function()
|
||||
3, 7, 11, 15,
|
||||
4, 8, 12, 16
|
||||
}
|
||||
local c = mat4():mul(a, b)
|
||||
local c = a:mul(b)
|
||||
local d = a * b
|
||||
assert.is.equal(30, c[1])
|
||||
assert.is.equal(70, c[2])
|
||||
@ -171,7 +171,7 @@ describe("mat4:", function()
|
||||
13, 14, 15, 16
|
||||
}
|
||||
local b = { 10, 20, 30, 40 }
|
||||
local c = mat4.mul_vec4({}, a, b)
|
||||
local c = mat4.mul_vec4(a, b)
|
||||
local d = a * b
|
||||
assert.is.equal(900, c[1])
|
||||
assert.is.equal(1000, c[2])
|
||||
@ -185,84 +185,79 @@ describe("mat4:", function()
|
||||
end)
|
||||
|
||||
it("scales a matrix", function()
|
||||
local a = mat4()
|
||||
local b = mat4():scale(a, vec3(5, 5, 5))
|
||||
assert.is.equal(5, b[1])
|
||||
assert.is.equal(5, b[6])
|
||||
assert.is.equal(5, b[11])
|
||||
local a = mat4():scale(vec3(5, 5, 5))
|
||||
assert.is.equal(5, a[1])
|
||||
assert.is.equal(5, a[6])
|
||||
assert.is.equal(5, a[11])
|
||||
end)
|
||||
|
||||
it("rotates a matrix", function()
|
||||
local a = mat4()
|
||||
local b = mat4():rotate(a, math.rad(45), vec3.unit_z)
|
||||
assert.is_true(utils.tolerance( 0.7071-b[1], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.7071-b[2], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.7071-b[5], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.7071-b[6], 0.001))
|
||||
local a = mat4():rotate(math.rad(45), vec3.unit_z)
|
||||
assert.is_true(utils.tolerance( 0.7071-a[1], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.7071-a[2], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.7071-a[5], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.7071-a[6], 0.001))
|
||||
end)
|
||||
|
||||
it("translates a matrix", function()
|
||||
local a = mat4()
|
||||
local b = mat4():translate(a, vec3(5, 5, 5))
|
||||
assert.is.equal(5, b[13])
|
||||
assert.is.equal(5, b[14])
|
||||
assert.is.equal(5, b[15])
|
||||
local a = mat4():translate(vec3(5, 5, 5))
|
||||
assert.is.equal(5, a[13])
|
||||
assert.is.equal(5, a[14])
|
||||
assert.is.equal(5, a[15])
|
||||
end)
|
||||
|
||||
it("inverts a matrix", function()
|
||||
local a = mat4()
|
||||
a:rotate(a, math.pi/4, vec3.unit_y)
|
||||
a:translate(a, vec3(4, 5, 6))
|
||||
:rotate(math.pi/4, vec3.unit_y)
|
||||
:translate(vec3(4, 5, 6))
|
||||
|
||||
local b = mat4():invert(a)
|
||||
local c = mat4():mul(a, b)
|
||||
local b = a:invert()
|
||||
local c = a * b
|
||||
assert.is.equal(mat4(), c)
|
||||
|
||||
local d = mat4()
|
||||
d:rotate(d, math.pi/4, vec3.unit_y)
|
||||
d:translate(d, vec3(4, 5, 6))
|
||||
:rotate(math.pi/4, vec3.unit_y)
|
||||
:translate(vec3(4, 5, 6))
|
||||
|
||||
local e = -d
|
||||
local f = mat4():mul(d, e)
|
||||
local f = d * e
|
||||
assert.is.equal(mat4(), f)
|
||||
end)
|
||||
|
||||
it("transposes a matrix", function()
|
||||
local a = mat4 {
|
||||
local a = mat4({
|
||||
1, 1, 1, 1,
|
||||
2, 2, 2, 2,
|
||||
3, 3, 3, 3,
|
||||
4, 4, 4, 4
|
||||
}
|
||||
local b = mat4():transpose(a)
|
||||
assert.is.equal(1, b[1])
|
||||
assert.is.equal(2, b[2])
|
||||
assert.is.equal(3, b[3])
|
||||
assert.is.equal(4, b[4])
|
||||
assert.is.equal(1, b[5])
|
||||
assert.is.equal(2, b[6])
|
||||
assert.is.equal(3, b[7])
|
||||
assert.is.equal(4, b[8])
|
||||
assert.is.equal(1, b[9])
|
||||
assert.is.equal(2, b[10])
|
||||
assert.is.equal(3, b[11])
|
||||
assert.is.equal(4, b[12])
|
||||
assert.is.equal(1, b[13])
|
||||
assert.is.equal(2, b[14])
|
||||
assert.is.equal(3, b[15])
|
||||
assert.is.equal(4, b[16])
|
||||
}):transpose()
|
||||
assert.is.equal(1, a[1])
|
||||
assert.is.equal(2, a[2])
|
||||
assert.is.equal(3, a[3])
|
||||
assert.is.equal(4, a[4])
|
||||
assert.is.equal(1, a[5])
|
||||
assert.is.equal(2, a[6])
|
||||
assert.is.equal(3, a[7])
|
||||
assert.is.equal(4, a[8])
|
||||
assert.is.equal(1, a[9])
|
||||
assert.is.equal(2, a[10])
|
||||
assert.is.equal(3, a[11])
|
||||
assert.is.equal(4, a[12])
|
||||
assert.is.equal(1, a[13])
|
||||
assert.is.equal(2, a[14])
|
||||
assert.is.equal(3, a[15])
|
||||
assert.is.equal(4, a[16])
|
||||
end)
|
||||
|
||||
it("shears a matrix", function()
|
||||
local a = mat4()
|
||||
local yx, zx, xy, zy, xz, yz = 1, 1, 1, -1, -1, -1
|
||||
local b = mat4():shear(a, yx, zx, xy, zy, xz, yz)
|
||||
assert.is.equal( 1, b[2])
|
||||
assert.is.equal( 1, b[3])
|
||||
assert.is.equal( 1, b[5])
|
||||
assert.is.equal(-1, b[7])
|
||||
assert.is.equal(-1, b[9])
|
||||
assert.is.equal(-1, b[10])
|
||||
local a = mat4():shear(yx, zx, xy, zy, xz, yz)
|
||||
assert.is.equal( 1, a[2])
|
||||
assert.is.equal( 1, a[3])
|
||||
assert.is.equal( 1, a[5])
|
||||
assert.is.equal(-1, a[7])
|
||||
assert.is.equal(-1, a[9])
|
||||
assert.is.equal(-1, a[10])
|
||||
end)
|
||||
|
||||
it("projects a matrix into screen space", function()
|
||||
@ -289,31 +284,30 @@ describe("mat4:", function()
|
||||
end)
|
||||
|
||||
it("transforms a matrix to look at a point", function()
|
||||
local a = mat4()
|
||||
local e = vec3(0, 0, 1.55)
|
||||
local c = vec3(4, 7, 1)
|
||||
local u = vec3(0, 0, 1)
|
||||
local b = mat4():look_at(a, e, c, u)
|
||||
local a = mat4().look_at(e, c, u)
|
||||
|
||||
assert.is_true(utils.tolerance( 0.868-b[1], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.034-b[2], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.495-b[3], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -b[4], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.868-a[1], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.034-a[2], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.495-a[3], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -a[4], 0.001))
|
||||
|
||||
assert.is_true(utils.tolerance(-0.496-b[5], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.059-b[6], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.866-b[7], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -b[8], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.496-a[5], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.059-a[6], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.866-a[7], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -a[8], 0.001))
|
||||
|
||||
assert.is_true(utils.tolerance( 0 -b[9], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.998-b[10], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.068-b[11], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -b[12], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -a[9], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.998-a[10], 0.001))
|
||||
assert.is_true(utils.tolerance( 0.068-a[11], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -a[12], 0.001))
|
||||
|
||||
assert.is_true(utils.tolerance( 0 -b[13], 0.001))
|
||||
assert.is_true(utils.tolerance(-1.546-b[14], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.106-b[15], 0.001))
|
||||
assert.is_true(utils.tolerance( 1 -b[16], 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -a[13], 0.001))
|
||||
assert.is_true(utils.tolerance(-1.546-a[14], 0.001))
|
||||
assert.is_true(utils.tolerance(-0.106-a[15], 0.001))
|
||||
assert.is_true(utils.tolerance( 1 -a[16], 0.001))
|
||||
end)
|
||||
|
||||
it("converts a matrix to vec4s", function()
|
||||
@ -347,14 +341,12 @@ describe("mat4:", function()
|
||||
end)
|
||||
|
||||
it("converts a matrix to a quaternion", function()
|
||||
local a = mat4()
|
||||
local b = mat4 {
|
||||
local q = mat4({
|
||||
0, 0, 1, 0,
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 0, 0
|
||||
}
|
||||
local q = b:to_quat()
|
||||
}):to_quat()
|
||||
assert.is.equal(-0.5, q.x)
|
||||
assert.is.equal(-0.5, q.y)
|
||||
assert.is.equal(-0.5, q.z)
|
||||
@ -364,7 +356,7 @@ describe("mat4:", function()
|
||||
it("converts a matrix to a frustum", function()
|
||||
local a = mat4()
|
||||
local b = mat4.from_perspective(45, 1, 0.1, 1000)
|
||||
local f = mat4():mul(b, a):to_frustum()
|
||||
local f = (b * a):to_frustum()
|
||||
|
||||
assert.is_true(utils.tolerance( 0.9239-f.left.a, 0.001))
|
||||
assert.is_true(utils.tolerance( 0 -f.left.b, 0.001))
|
||||
@ -406,15 +398,14 @@ describe("mat4:", function()
|
||||
end)
|
||||
|
||||
it("gets a string representation of a matrix", function()
|
||||
local a = mat4()
|
||||
local b = a:to_string()
|
||||
local a = mat4():to_string()
|
||||
local z = "+0.000"
|
||||
local o = "+1.000"
|
||||
local s = string.format(
|
||||
"[ %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s ]",
|
||||
o, z, z, z, z, o, z, z, z, z, o, z, z, z ,z, o
|
||||
)
|
||||
assert.is.equal(s, b)
|
||||
assert.is.equal(s, a)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
@ -40,7 +40,7 @@ describe("quat:", function()
|
||||
end)
|
||||
|
||||
it("creates a quaternion from a direction", function()
|
||||
local v = vec3():normalize(vec3(-80, 80, -80))
|
||||
local v = vec3(-80, 80, -80):normalize()
|
||||
local a = quat.from_direction(v, vec3.unit_z)
|
||||
assert.is_true(utils.tolerance(-0.577-a.x, 0.001))
|
||||
assert.is_true(utils.tolerance(-0.577-a.y, 0.001))
|
||||
@ -60,7 +60,7 @@ describe("quat:", function()
|
||||
it("adds a quaternion to another", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local b = quat(3, 6, 9, 1)
|
||||
local c = quat():add(a, b)
|
||||
local c = a:add(b)
|
||||
local d = a + b
|
||||
assert.is.equal(5, c.x)
|
||||
assert.is.equal(9, c.y)
|
||||
@ -72,7 +72,7 @@ describe("quat:", function()
|
||||
it("subtracts a quaternion from another", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local b = quat(3, 6, 9, 1)
|
||||
local c = quat():sub(a, b)
|
||||
local c = a:sub(b)
|
||||
local d = a - b
|
||||
assert.is.equal(-1, c.x)
|
||||
assert.is.equal(-3, c.y)
|
||||
@ -84,7 +84,7 @@ describe("quat:", function()
|
||||
it("multiplies a quaternion by another", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local b = quat(3, 6, 9, 1)
|
||||
local c = quat():mul(a, b)
|
||||
local c = a:mul(b)
|
||||
local d = a * b
|
||||
assert.is.equal( 8, c.x)
|
||||
assert.is.equal( 3, c.y)
|
||||
@ -96,7 +96,7 @@ describe("quat:", function()
|
||||
it("multiplies a quaternion by a scale factor", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local s = 3
|
||||
local b = quat():scale(a, s)
|
||||
local b = a:scale(s)
|
||||
local c = a * s
|
||||
assert.is.equal(6, b.x)
|
||||
assert.is.equal(9, b.y)
|
||||
@ -117,7 +117,7 @@ describe("quat:", function()
|
||||
it("multiplies a quaternion by a vec3", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local v = vec3(3, 4, 5)
|
||||
local b = quat.mul_vec3(vec3(), a, v)
|
||||
local b = a:mul_vec3(v)
|
||||
local c = a * v
|
||||
assert.is.equal(-21, c.x)
|
||||
assert.is.equal( 4, c.y)
|
||||
@ -128,7 +128,7 @@ describe("quat:", function()
|
||||
it("multiplies a quaternion by an exponent of 0", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local e = 0
|
||||
local b = quat():pow(a, e)
|
||||
local b = a:pow(e)
|
||||
local c = a^e
|
||||
assert.is.equal(0, b.x)
|
||||
assert.is.equal(0, b.y)
|
||||
@ -140,43 +140,42 @@ describe("quat:", function()
|
||||
it("multiplies a quaternion by a positive exponent", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local e = 2
|
||||
local b = quat():pow(a, e)
|
||||
local b = a:pow(e)
|
||||
local c = a^e
|
||||
assert.is.equal( 4, b.x)
|
||||
assert.is.equal( 14, b.y)
|
||||
assert.is.equal( 24, b.z)
|
||||
assert.is.equal(-145, b.w)
|
||||
assert.is.equal( b, c)
|
||||
|
||||
assert.is.equal( 4, b.x)
|
||||
assert.is.equal( 6, b.y)
|
||||
assert.is.equal( 8, b.z)
|
||||
assert.is.equal(-28, b.w)
|
||||
assert.is.equal( b, c)
|
||||
end)
|
||||
|
||||
it("multiplies a quaternion by a negative exponent", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local e = -2
|
||||
local b = quat():pow(a, e)
|
||||
local b = a:pow(e)
|
||||
local c = a^e
|
||||
assert.is_true(utils.tolerance(0.004-b.x, 0.001))
|
||||
assert.is_true(utils.tolerance(0.01 -b.y, 0.001))
|
||||
assert.is_true(utils.tolerance(0.018-b.z, 0.001))
|
||||
assert.is_true(utils.tolerance(0.001-b.w, 0.001))
|
||||
assert.is_true(utils.tolerance(0.0044+b.x, 0.0001))
|
||||
assert.is_true(utils.tolerance(0.0067+b.y, 0.0001))
|
||||
assert.is_true(utils.tolerance(0.0089+b.z, 0.0001))
|
||||
assert.is_true(utils.tolerance(0.0311+b.w, 0.0001))
|
||||
assert.is.equal(b, c)
|
||||
end)
|
||||
|
||||
it("inverts a quaternion", function()
|
||||
local a = quat(1, 1, 1, 1)
|
||||
local b = quat():inverse(a)
|
||||
assert.is.equal(-0.5, b.x)
|
||||
assert.is.equal(-0.5, b.y)
|
||||
assert.is.equal(-0.5, b.z)
|
||||
assert.is.equal( 0.5, b.w)
|
||||
local a = quat(1, 1, 1, 1):inverse()
|
||||
assert.is.equal(-0.5, a.x)
|
||||
assert.is.equal(-0.5, a.y)
|
||||
assert.is.equal(-0.5, a.z)
|
||||
assert.is.equal( 0.5, a.w)
|
||||
end)
|
||||
|
||||
it("normalizes a quaternion", function()
|
||||
local a = quat(1, 1, 1, 1)
|
||||
local b = quat():normalize(a)
|
||||
assert.is.equal(0.5, b.x)
|
||||
assert.is.equal(0.5, b.y)
|
||||
assert.is.equal(0.5, b.z)
|
||||
assert.is.equal(0.5, b.w)
|
||||
local a = quat(1, 1, 1, 1):normalize()
|
||||
assert.is.equal(0.5, a.x)
|
||||
assert.is.equal(0.5, a.y)
|
||||
assert.is.equal(0.5, a.z)
|
||||
assert.is.equal(0.5, a.w)
|
||||
end)
|
||||
|
||||
it("dots two quaternions", function()
|
||||
@ -201,22 +200,20 @@ describe("quat:", function()
|
||||
end)
|
||||
|
||||
it("gets the length of a quaternion", function()
|
||||
local a = quat(2, 3, 4, 5)
|
||||
local b = a:len()
|
||||
assert.is.equal(math.sqrt(54), b)
|
||||
local a = quat(2, 3, 4, 5):len()
|
||||
assert.is.equal(math.sqrt(54), a)
|
||||
end)
|
||||
|
||||
it("gets the square length of a quaternion", function()
|
||||
local a = quat(2, 3, 4, 5)
|
||||
local b = a:len2()
|
||||
assert.is.equal(54, b)
|
||||
local a = quat(2, 3, 4, 5):len2()
|
||||
assert.is.equal(54, a)
|
||||
end)
|
||||
|
||||
it("interpolates between two quaternions", function()
|
||||
local a = quat(3, 3, 3, 3)
|
||||
local b = quat(6, 6, 6, 6)
|
||||
local s = 0.1
|
||||
local c = quat():lerp(a, b, s)
|
||||
local c = a:lerp(b, s)
|
||||
assert.is.equal(0.5, c.x)
|
||||
assert.is.equal(0.5, c.y)
|
||||
assert.is.equal(0.5, c.z)
|
||||
@ -227,7 +224,7 @@ describe("quat:", function()
|
||||
local a = quat(3, 3, 3, 3)
|
||||
local b = quat(6, 6, 6, 6)
|
||||
local s = 0.1
|
||||
local c = quat():slerp(a, b, s)
|
||||
local c = a:slerp(b, s)
|
||||
assert.is.equal(0.5, c.x)
|
||||
assert.is.equal(0.5, c.y)
|
||||
assert.is.equal(0.5, c.z)
|
||||
@ -235,8 +232,7 @@ describe("quat:", function()
|
||||
end)
|
||||
|
||||
it("unpacks a quaternion", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local x, y, z, w = a:unpack()
|
||||
local x, y, z, w = quat(2, 3, 4, 1):unpack()
|
||||
assert.is.equal(2, x)
|
||||
assert.is.equal(3, y)
|
||||
assert.is.equal(4, z)
|
||||
@ -244,26 +240,24 @@ describe("quat:", function()
|
||||
end)
|
||||
|
||||
it("converts quaternion to a vec3", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local v = a:to_vec3()
|
||||
local v = quat(2, 3, 4, 1):to_vec3()
|
||||
assert.is.equal(2, v.x)
|
||||
assert.is.equal(3, v.y)
|
||||
assert.is.equal(4, v.z)
|
||||
end)
|
||||
|
||||
it("gets the conjugate quaternion", function()
|
||||
local a = quat(2, 3, 4, 1)
|
||||
local b = quat():conjugate(a)
|
||||
assert.is.equal(-2, b.x)
|
||||
assert.is.equal(-3, b.y)
|
||||
assert.is.equal(-4, b.z)
|
||||
assert.is.equal( 1, b.w)
|
||||
local a = quat(2, 3, 4, 1):conjugate()
|
||||
assert.is.equal(-2, a.x)
|
||||
assert.is.equal(-3, a.y)
|
||||
assert.is.equal(-4, a.z)
|
||||
assert.is.equal( 1, a.w)
|
||||
end)
|
||||
|
||||
it("gets the reciprocal quaternion", function()
|
||||
local a = quat(1, 1, 1, 1)
|
||||
local b = quat():reciprocal(a)
|
||||
local c = quat():reciprocal(b)
|
||||
local b = a:reciprocal()
|
||||
local c = b:reciprocal()
|
||||
|
||||
assert.is_not.equal(a.x, b.x)
|
||||
assert.is_not.equal(a.y, b.y)
|
||||
@ -284,25 +278,23 @@ describe("quat:", function()
|
||||
end)
|
||||
|
||||
it("converts between a quaternion and angle/axis (w=2)", function()
|
||||
local a = quat(1, 1, 1, 2)
|
||||
local _, axis = a:to_angle_axis()
|
||||
assert.is_true(utils.tolerance(0.378-a.x, 0.001))
|
||||
assert.is_true(utils.tolerance(0.378-a.y, 0.001))
|
||||
assert.is_true(utils.tolerance(0.378-a.z, 0.001))
|
||||
assert.is_true(utils.tolerance(0.756-a.w, 0.001))
|
||||
local angle, axis = quat(1, 1, 1, 2):to_angle_axis()
|
||||
assert.is_true(utils.tolerance(1.427-angle, 0.001))
|
||||
assert.is_true(utils.tolerance(0.577-axis.x, 0.001))
|
||||
assert.is_true(utils.tolerance(0.577-axis.y, 0.001))
|
||||
assert.is_true(utils.tolerance(0.577-axis.z, 0.001))
|
||||
end)
|
||||
|
||||
it("converts between a quaternion and angle/axis (w=1)", function()
|
||||
local a = quat(1, 2, 3, 1)
|
||||
local _, axis = a:to_angle_axis()
|
||||
local angle, axis = quat(1, 2, 3, 1):to_angle_axis()
|
||||
assert.is.equal(0, angle)
|
||||
assert.is.equal(1, axis.x)
|
||||
assert.is.equal(2, axis.y)
|
||||
assert.is.equal(3, axis.z)
|
||||
end)
|
||||
|
||||
it("gets a string representation of a quaternion", function()
|
||||
local a = quat()
|
||||
local b = a:to_string()
|
||||
assert.is.equal("(+0.000,+0.000,+0.000,+1.000)", b)
|
||||
local a = quat():to_string()
|
||||
assert.is.equal("(+0.000,+0.000,+0.000,+1.000)", a)
|
||||
end)
|
||||
end)
|
||||
|
@ -44,7 +44,7 @@ describe("vec2:", function()
|
||||
it("adds a vector to another", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2(7, 4)
|
||||
local c = vec2():add(a, b)
|
||||
local c = a:add(b)
|
||||
local d = a + b
|
||||
assert.is.equal(10, c.x)
|
||||
assert.is.equal(9, c.y)
|
||||
@ -54,7 +54,7 @@ describe("vec2:", function()
|
||||
it("subracts a vector from another", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2(7, 4)
|
||||
local c = vec2():sub(a, b)
|
||||
local c = a:sub(b)
|
||||
local d = a - b
|
||||
assert.is.equal(-4, c.x)
|
||||
assert.is.equal( 1, c.y)
|
||||
@ -64,7 +64,7 @@ describe("vec2:", function()
|
||||
it("multiplies a vector by a scale factor", function()
|
||||
local a = vec2(3, 5)
|
||||
local s = 2
|
||||
local c = vec2():scale(a, s)
|
||||
local c = a:scale(s)
|
||||
local d = a * s
|
||||
assert.is.equal(6, c.x)
|
||||
assert.is.equal(10, c.y)
|
||||
@ -74,7 +74,7 @@ describe("vec2:", function()
|
||||
it("divides a vector by another vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local s = vec2(2, 2)
|
||||
local c = vec2():div(a, s)
|
||||
local c = a:div(s)
|
||||
local d = a / s
|
||||
assert.is.equal(1.5, c.x)
|
||||
assert.is.equal(2.5, c.y)
|
||||
@ -100,13 +100,13 @@ describe("vec2:", function()
|
||||
|
||||
it("normalizes a vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2():normalize(a)
|
||||
local b = a:normalize()
|
||||
assert.is_true(abs(b:len()-1) < DBL_EPSILON)
|
||||
end)
|
||||
|
||||
it("trims the length of a vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2():trim(a, 0.5)
|
||||
local b = a:trim(0.5)
|
||||
assert.is_true(abs(b:len()-0.5) < DBL_EPSILON)
|
||||
end)
|
||||
|
||||
@ -142,7 +142,7 @@ describe("vec2:", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2(7, 4)
|
||||
local s = 0.1
|
||||
local c = vec2():lerp(a, b, s)
|
||||
local c = a:lerp(b, s)
|
||||
assert.is.equal(3.4, c.x)
|
||||
assert.is.equal(4.9, c.y)
|
||||
end)
|
||||
@ -156,8 +156,8 @@ describe("vec2:", function()
|
||||
|
||||
it("rotates a vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2():rotate(a, math.pi)
|
||||
local c = vec2():rotate(b, -math.pi)
|
||||
local b = a:rotate( math.pi)
|
||||
local c = b:rotate(-math.pi)
|
||||
assert.is_not.equal(a, b)
|
||||
assert.is.equal(a, c)
|
||||
end)
|
||||
@ -172,7 +172,7 @@ describe("vec2:", function()
|
||||
|
||||
it("gets a perpendicular vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = vec2():perpendicular(a)
|
||||
local b = a:perpendicular()
|
||||
assert.is.equal(-5, b.x)
|
||||
assert.is.equal( 3, b.y)
|
||||
end)
|
||||
|
@ -49,7 +49,7 @@ describe("vec3:", function()
|
||||
it("adds a vector to another", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3(7, 4, 1)
|
||||
local c = vec3():add(a, b)
|
||||
local c = a:add(b)
|
||||
local d = a + b
|
||||
assert.is.equal(10, c.x)
|
||||
assert.is.equal(9, c.y)
|
||||
@ -60,7 +60,7 @@ describe("vec3:", function()
|
||||
it("subracts a vector from another", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3(7, 4, 1)
|
||||
local c = vec3():sub(a, b)
|
||||
local c = a:sub(b)
|
||||
local d = a - b
|
||||
assert.is.equal(-4, c.x)
|
||||
assert.is.equal( 1, c.y)
|
||||
@ -71,7 +71,7 @@ describe("vec3:", function()
|
||||
it("multiplies a vector by a scale factor", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local s = 2
|
||||
local c = vec3():scale(a, s)
|
||||
local c = a:scale(s)
|
||||
local d = a * s
|
||||
assert.is.equal(6, c.x)
|
||||
assert.is.equal(10, c.y)
|
||||
@ -82,7 +82,7 @@ describe("vec3:", function()
|
||||
it("divides a vector by another vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local s = vec3(2, 2, 2)
|
||||
local c = vec3():div(a, s)
|
||||
local c = a:div(s)
|
||||
local d = a / s
|
||||
assert.is.equal(1.5, c.x)
|
||||
assert.is.equal(2.5, c.y)
|
||||
@ -110,13 +110,13 @@ describe("vec3:", function()
|
||||
|
||||
it("normalizes a vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3():normalize(a)
|
||||
local b = a:normalize()
|
||||
assert.is_true(abs(b:len()-1) < DBL_EPSILON)
|
||||
end)
|
||||
|
||||
it("trims the length of a vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3():trim(a, 0.5)
|
||||
local b = a:trim(0.5)
|
||||
assert.is_true(abs(b:len()-0.5) < DBL_EPSILON)
|
||||
end)
|
||||
|
||||
@ -137,7 +137,7 @@ describe("vec3:", function()
|
||||
it("crosses two vectors", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3(7, 4, 1)
|
||||
local c = vec3():cross(a, b)
|
||||
local c = a:cross(b)
|
||||
assert.is.equal(-23, c.x)
|
||||
assert.is.equal( 46, c.y)
|
||||
assert.is.equal(-23, c.z)
|
||||
@ -154,7 +154,7 @@ describe("vec3:", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3(7, 4, 1)
|
||||
local s = 0.1
|
||||
local c = vec3():lerp(a, b, s)
|
||||
local c = a:lerp(b, s)
|
||||
assert.is.equal(3.4, c.x)
|
||||
assert.is.equal(4.9, c.y)
|
||||
assert.is.equal(6.4, c.z)
|
||||
@ -170,8 +170,8 @@ describe("vec3:", function()
|
||||
|
||||
it("rotates a vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3():rotate(a, math.pi, vec3.unit_z)
|
||||
local c = vec3():rotate(b, -math.pi, vec3.unit_z)
|
||||
local b = a:rotate( math.pi, vec3.unit_z)
|
||||
local c = b:rotate(-math.pi, vec3.unit_z)
|
||||
assert.is_not.equal(a, b)
|
||||
assert.is.equal(7, b.z)
|
||||
assert.is.equal(a, c)
|
||||
@ -179,13 +179,13 @@ describe("vec3:", function()
|
||||
|
||||
it("cannot rotate a vector without a valis axis", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3():rotate(a, math.pi, 0)
|
||||
local b = a:rotate(math.pi, 0)
|
||||
assert.is_equal(a, b)
|
||||
end)
|
||||
|
||||
it("gets a perpendicular vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = vec3():perpendicular(a)
|
||||
local b = a:perpendicular()
|
||||
assert.is.equal(-5, b.x)
|
||||
assert.is.equal( 3, b.y)
|
||||
assert.is.equal( 0, b.z)
|
||||
|
Loading…
x
Reference in New Issue
Block a user