Fix vec3 test failures, add unit vectors.
This commit is contained in:
parent
6f050bd1c8
commit
54daab8ce2
@ -43,18 +43,25 @@ if hasffi and jitenabled then
|
||||
ffi.cdef "struct cpml_vec3 { double x, y, z; };"
|
||||
new = ffi.typeof("struct cpml_vec3")
|
||||
function isvector(v)
|
||||
return ffi.istype(new, v)
|
||||
return ffi.istype(new, v) or type(v.x and v.y and v.z) == "number"
|
||||
end
|
||||
else
|
||||
function new(x,y,z)
|
||||
-- allow construction via vec3(a, b, c), vec3 { a, b, c } or vec3 { x = a, y = b, z = c }
|
||||
if type(x) == "table" then
|
||||
return setmetatable({x=x.x or x[1] or 0, y=x.y or x[2] or 0, z=x.z or x[3] or 0}, vector)
|
||||
end
|
||||
return setmetatable({x = x or 0, y = y or 0, z = z or 0}, vector)
|
||||
end
|
||||
function isvector(v)
|
||||
return getmetatable(v) == vector
|
||||
return getmetatable(v) == vector or type(v.x and v.y and v.z) == "number"
|
||||
end
|
||||
end
|
||||
|
||||
local zero = new(0,0,0)
|
||||
local unit_x = new(1,0,0)
|
||||
local unit_y = new(0,1,0)
|
||||
local unit_z = new(0,0,1)
|
||||
|
||||
function vector:clone()
|
||||
return new(self.x, self.y, self.z)
|
||||
@ -246,5 +253,15 @@ if hasffi and jitenabled then
|
||||
end
|
||||
|
||||
-- the module
|
||||
return setmetatable({new = new, isvector = isvector, zero = zero},
|
||||
{__call = function(_, ...) return new(...) end})
|
||||
return setmetatable(
|
||||
{
|
||||
new = new,
|
||||
isvector = isvector,
|
||||
zero = zero,
|
||||
unit_x = unit_x,
|
||||
unit_y = unit_y,
|
||||
unit_z = unit_z
|
||||
}, {
|
||||
__call = function(_, ...) return new(...) end
|
||||
}
|
||||
)
|
||||
|
@ -37,10 +37,28 @@ describe("vec3:", function()
|
||||
assert.is_false(vec3(3,3,3) >= vec3(5,5,5))
|
||||
end)
|
||||
|
||||
it("testing misc operators", function()
|
||||
-- tostring
|
||||
assert.has_no.errors(function() return tostring(vec3(1,1,1)) end)
|
||||
it("testing new", function()
|
||||
local v = vec3()
|
||||
assert.is_true(v.x == 0)
|
||||
assert.is_true(v.y == 0)
|
||||
assert.is_true(v.z == 0)
|
||||
|
||||
v = vec3{1, 2, 3}
|
||||
assert.is_true(v.x == 1)
|
||||
assert.is_true(v.y == 2)
|
||||
assert.is_true(v.z == 3)
|
||||
|
||||
v = vec3(4, 5, 6)
|
||||
assert.is_true(v.x == 4)
|
||||
assert.is_true(v.y == 5)
|
||||
assert.is_true(v.z == 6)
|
||||
end)
|
||||
|
||||
it("testing tostring", function()
|
||||
assert.has_no.errors(function() return tostring(vec3(1,1,1)) end)
|
||||
end)
|
||||
|
||||
it("testing isvector", function()
|
||||
assert.is_true(vec3.isvector(vec3()))
|
||||
assert.is_true(vec3.isvector(vec3{1,1}))
|
||||
assert.is_true(vec3.isvector{x=1, y=2, z=3})
|
||||
@ -60,7 +78,7 @@ describe("vec3:", function()
|
||||
end)
|
||||
|
||||
it("testing cross", function()
|
||||
assert.is.equal(vec3(1,0,0):cross(vec3(0,1,0)), vec3(0,0,1))
|
||||
assert.is.equal(vec3.unit_x:cross(vec3.unit_y), vec3.unit_z)
|
||||
end)
|
||||
|
||||
it("testing len and normalize", function()
|
||||
@ -73,8 +91,11 @@ describe("vec3:", function()
|
||||
assert.is.equal(vec3(2,0,2):len2(), 8)
|
||||
end)
|
||||
|
||||
it("testing lerp", function()
|
||||
assert.is.equal(vec3(0, 0, 0):lerp(vec3(1, 1, 1), 0.5), vec3(0.5, 0.5, 0.5))
|
||||
end)
|
||||
|
||||
describe("vec3 pending tests", function()
|
||||
pending "lerp"
|
||||
pending "trim"
|
||||
pending "angle_to"
|
||||
pending "angle_between"
|
||||
|
Loading…
x
Reference in New Issue
Block a user