vec: Add nan checking functions
Add: * vec2.has_nan + test * vec3.has_nan + test * quat.has_nan * mat4.has_nan * utils.is_nan + test
This commit is contained in:
parent
a3aacc354c
commit
29954aa60e
@ -9,4 +9,8 @@ function private.round(value, precision)
|
||||
return value >= 0 and floor(value+0.5) or ceil(value-0.5)
|
||||
end
|
||||
|
||||
function private.is_nan(a)
|
||||
return a ~= a
|
||||
end
|
||||
|
||||
return private
|
||||
|
@ -6,6 +6,7 @@ local vec2 = require(modules .. "vec2")
|
||||
local vec3 = require(modules .. "vec3")
|
||||
local quat = require(modules .. "quat")
|
||||
local utils = require(modules .. "utils")
|
||||
local private = require(modules .. "_private_utils")
|
||||
local sqrt = math.sqrt
|
||||
local cos = math.cos
|
||||
local sin = math.sin
|
||||
@ -650,6 +651,18 @@ function mat4.is_mat4(a)
|
||||
return true
|
||||
end
|
||||
|
||||
--- Return whether any component is NaN
|
||||
-- @tparam mat4 a Matrix to be tested
|
||||
-- @treturn boolean if any component is NaN
|
||||
function vec2.has_nan(a)
|
||||
for i=1, 16 do
|
||||
if private.is_nan(a[i]) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
--- Return a formatted string.
|
||||
-- @tparam mat4 a Matrix to be turned into a string
|
||||
-- @treturn string formatted
|
||||
|
@ -4,6 +4,7 @@
|
||||
local modules = (...):gsub('%.[^%.]+$', '') .. "."
|
||||
local constants = require(modules .. "constants")
|
||||
local vec3 = require(modules .. "vec3")
|
||||
local private = require(modules .. "_private_utils")
|
||||
local DOT_THRESHOLD = constants.DOT_THRESHOLD
|
||||
local DBL_EPSILON = constants.DBL_EPSILON
|
||||
local acos = math.acos
|
||||
@ -362,6 +363,16 @@ function quat.is_imaginary(a)
|
||||
return a.w == 0
|
||||
end
|
||||
|
||||
--- Return whether any component is NaN
|
||||
-- @tparam quat a Quaternion to be tested
|
||||
-- @treturn boolean if x,y,z, or w is NaN
|
||||
function quat.has_nan(a)
|
||||
return private.is_nan(a.x) or
|
||||
private.is_nan(a.y) or
|
||||
private.is_nan(a.z) or
|
||||
private.is_nan(a.w)
|
||||
end
|
||||
|
||||
--- Convert a quaternion into an angle plus axis components.
|
||||
-- @tparam quat a Quaternion to convert
|
||||
-- @tparam identityAxis vec3 of axis to use on identity/degenerate quaternions (optional, default returns 0,0,0,1)
|
||||
|
@ -128,6 +128,12 @@ function utils.is_pot(value)
|
||||
return (frexp(value)) == 0.5
|
||||
end
|
||||
|
||||
--- Check if a value is NaN
|
||||
-- Returns true if a number is not a valid number
|
||||
-- @param value
|
||||
-- @return boolean
|
||||
utils.is_nan = private.is_nan
|
||||
|
||||
-- Originally from vec3
|
||||
function utils.project_on(a, b)
|
||||
local s =
|
||||
|
@ -320,6 +320,14 @@ function vec2.is_zero(a)
|
||||
return a.x == 0 and a.y == 0
|
||||
end
|
||||
|
||||
--- Return whether either value is NaN
|
||||
-- @tparam vec2 a Vector to be tested
|
||||
-- @treturn boolean if x or y is nan
|
||||
function vec2.has_nan(a)
|
||||
return private.is_nan(a.x) or
|
||||
private.is_nan(a.y)
|
||||
end
|
||||
|
||||
--- Convert point from cartesian to polar.
|
||||
-- @tparam vec2 a Vector to convert
|
||||
-- @treturn number radius
|
||||
|
@ -332,6 +332,15 @@ function vec3.is_zero(a)
|
||||
return a.x == 0 and a.y == 0 and a.z == 0
|
||||
end
|
||||
|
||||
--- Return whether any component is NaN
|
||||
-- @tparam vec3 a Vector to be tested
|
||||
-- @treturn boolean if x,y, or z are nan
|
||||
function vec3.has_nan(a)
|
||||
return private.is_nan(a.x) or
|
||||
private.is_nan(a.y) or
|
||||
private.is_nan(a.z)
|
||||
end
|
||||
|
||||
--- Return a formatted string.
|
||||
-- @tparam vec3 a Vector to be turned into a string
|
||||
-- @treturn string formatted
|
||||
|
@ -28,6 +28,11 @@ describe("utils:", function()
|
||||
assert.is_true(tolerance(v, 0.39346934028737))
|
||||
end)
|
||||
|
||||
it("checks a nan", function()
|
||||
local a = 0/0
|
||||
assert.is_true(utils.is_nan(a))
|
||||
end)
|
||||
|
||||
it("rounds a number", function()
|
||||
-- round up
|
||||
local v = utils.round(1.3252525, 0.01)
|
||||
|
@ -35,6 +35,11 @@ describe("vec2:", function()
|
||||
assert.is.equal(5, a.y)
|
||||
end)
|
||||
|
||||
it("creates a vector from nan", function()
|
||||
local a = vec2(0/0)
|
||||
assert.is_true(a:has_nan())
|
||||
end)
|
||||
|
||||
it("clones a vector", function()
|
||||
local a = vec2(3, 5)
|
||||
local b = a:clone()
|
||||
|
@ -40,6 +40,11 @@ describe("vec3:", function()
|
||||
assert.is.equal(7, a.z)
|
||||
end)
|
||||
|
||||
it("creates a vector from nan", function()
|
||||
local a = vec3(0/0)
|
||||
assert.is_true(a:has_nan())
|
||||
end)
|
||||
|
||||
it("clones a vector", function()
|
||||
local a = vec3(3, 5, 7)
|
||||
local b = a:clone()
|
||||
|
Loading…
x
Reference in New Issue
Block a user