Updated to work outside of a sanitized environment.
This commit is contained in:
parent
20f9625ae2
commit
799e1607bc
@ -433,7 +433,7 @@ end
|
||||
-- @tparam vec3 axis Axis to rotate on
|
||||
-- @treturn mat4 out
|
||||
function mat4.rotate(out, a, angle, axis)
|
||||
if type(angle) == "table" then
|
||||
if type(angle) == "table" or type(angle) == "cdata" then
|
||||
angle, axis = angle:to_angle_axis()
|
||||
end
|
||||
|
||||
@ -804,8 +804,9 @@ function mat4_mt.__unm(a)
|
||||
end
|
||||
|
||||
function mat4_mt.__eq(a, b)
|
||||
assert(mat4.is_mat4(a), "__eq: Wrong argument type for left hand operant. (<cpml.mat4> expected)")
|
||||
assert(mat4.is_mat4(b), "__eq: Wrong argument type for right hand operant. (<cpml.mat4> expected)")
|
||||
if not mat4.is_mat4(a) or not mat4.is_mat4(b) then
|
||||
return false
|
||||
end
|
||||
|
||||
for i = 1, 16 do
|
||||
if not utils.tolerance(b[i]-a[i], constants.FLT_EPSILON) then
|
||||
|
@ -18,11 +18,11 @@ end
|
||||
-- triangle[2] is a vec3
|
||||
-- triangle[3] is a vec3
|
||||
function mesh.normal(triangle)
|
||||
local ca = vec3():sub(triangle[3], triangle[1])
|
||||
local ba = vec3():sub(triangle[2], triangle[1])
|
||||
local ca = vec3():sub(triangle[3], triangle[1])
|
||||
local out = vec3()
|
||||
return out
|
||||
:cross(ca, ba)
|
||||
:cross(ba, ca)
|
||||
:normalize(out)
|
||||
end
|
||||
|
||||
@ -30,24 +30,17 @@ end
|
||||
-- triangle[2] is a vec3
|
||||
-- triangle[3] is a vec3
|
||||
function mesh.plane_from_triangle(triangle)
|
||||
local out = {}
|
||||
local ca = vec3():sub(triangle[3], triangle[1])
|
||||
local ba = vec3():sub(triangle[2], triangle[1])
|
||||
|
||||
out.origin = triangle[1]
|
||||
out.normal = vec3()
|
||||
:cross(ba, ca)
|
||||
:normalize(out)
|
||||
out.dot = -out.normal:dot(out.origin)
|
||||
|
||||
return out
|
||||
return {
|
||||
origin = triangle[1],
|
||||
normal = mesh.normal(triangle)
|
||||
}
|
||||
end
|
||||
|
||||
-- plane.origin is a vec3
|
||||
-- plane.normal is a vec3
|
||||
-- direction is a vec3
|
||||
function mesh.is_front_facing(plane, direction)
|
||||
return plane.normal:dot(direction) <= 0
|
||||
return plane.normal:dot(direction) >= 0
|
||||
end
|
||||
|
||||
-- point is a vec3
|
||||
@ -55,8 +48,7 @@ end
|
||||
-- plane.normal is a vec3
|
||||
-- plane.dot is a number
|
||||
function mesh.signed_distance(point, plane)
|
||||
dot = plane.dot or -plane.normal:dot(plane.origin)
|
||||
return point:dot(plane.normal) + dot
|
||||
return point:dot(plane.normal) - plane.normal:dot(plane.origin)
|
||||
end
|
||||
|
||||
return mesh
|
||||
|
@ -155,10 +155,10 @@ function quat.mul_vec3(out, a, b)
|
||||
uuv:cross(a, uv)
|
||||
|
||||
return out
|
||||
:mul(uv, a.w)
|
||||
:scale(uv, a.w)
|
||||
:add(out, uuv)
|
||||
:mul(out, 2 )
|
||||
:add(b, out)
|
||||
:scale(out, 2)
|
||||
:add(b, out)
|
||||
end
|
||||
|
||||
--- Multiply a quaternion by an exponent.
|
||||
@ -232,6 +232,14 @@ function quat.scale(out, a, s)
|
||||
return out
|
||||
end
|
||||
|
||||
--- Alias of from_angle_axis.
|
||||
-- @tparam number angle Angle (in radians)
|
||||
-- @tparam vec3 axis
|
||||
-- @treturn quat out
|
||||
function quat.rotate(angle, axis)
|
||||
return quat.from_angle_axis(angle, axis)
|
||||
end
|
||||
|
||||
--- Return the conjugate of a quaternion.
|
||||
-- @tparam quat out Quaternion to store the result
|
||||
-- @tparam quat a Quaternion to conjugate
|
||||
@ -420,8 +428,9 @@ function quat_mt.__unm(a)
|
||||
end
|
||||
|
||||
function quat_mt.__eq(a,b)
|
||||
assert(quat.is_quat(a), "__eq: Wrong argument type for left hand operant. (<cpml.quat> expected)")
|
||||
assert(quat.is_quat(b), "__eq: Wrong argument type for right hand operant. (<cpml.quat> expected)")
|
||||
if not quat.is_quat(a) or not quat.is_quat(b) then
|
||||
return false
|
||||
end
|
||||
return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w
|
||||
end
|
||||
|
||||
|
@ -187,26 +187,4 @@ function utils.refract(out, i, n, ior)
|
||||
return out
|
||||
end
|
||||
|
||||
-- Originally from vec3
|
||||
function utils.angle_to(a, b)
|
||||
if b then
|
||||
return atan2(a.y - b.y, a.x - b.x)
|
||||
end
|
||||
|
||||
return atan2(a.y, a.x)
|
||||
end
|
||||
|
||||
-- Originally from vec3
|
||||
function utils.angle_between(a, b)
|
||||
if b then
|
||||
if vec2.is_vec2(a) then
|
||||
return acos(vec2.dot(a, b) / (vec2.len(a) * vec2.len(b)))
|
||||
end
|
||||
|
||||
return acos(vec3.dot(a, b) / (vec3.len(a) * vec3.len(b)))
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
return utils
|
||||
|
@ -230,6 +230,34 @@ function vec2.perpendicular(out, a)
|
||||
return out
|
||||
end
|
||||
|
||||
--- Angle from one vector to another.
|
||||
-- @tparam vec2 a Vector
|
||||
-- @tparam vec2 b Vector
|
||||
-- @treturn number angle
|
||||
function vec2.angle_to(a, b)
|
||||
if b then
|
||||
return atan2(a.y - b.y, a.x - b.x)
|
||||
end
|
||||
|
||||
return atan2(a.y, a.x)
|
||||
end
|
||||
|
||||
--- Angle between two vectors.
|
||||
-- @tparam vec2 a Vector
|
||||
-- @tparam vec2 b Vector
|
||||
-- @treturn number angle
|
||||
function vec2.angle_between(a, b)
|
||||
if b then
|
||||
if vec2.is_vec2(a) then
|
||||
return acos(vec2.dot(a, b) / (vec2.len(a) * vec2.len(b)))
|
||||
end
|
||||
|
||||
return acos(vec3.dot(a, b) / (vec3.len(a) * vec3.len(b)))
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
--- Lerp between two vectors.
|
||||
-- @tparam vec2 out Vector to store the result
|
||||
-- @tparam vec2 a Left hand operant
|
||||
@ -301,9 +329,10 @@ function vec2_mt.__unm(a)
|
||||
return new(-a.x, -a.y)
|
||||
end
|
||||
|
||||
function vec2_mt.__eq(a,b)
|
||||
assert(vec2.is_vec2(a), "__eq: Wrong argument type for left hand operant. (<cpml.vec2> expected)")
|
||||
assert(vec2.is_vec2(b), "__eq: Wrong argument type for right hand operant. (<cpml.vec2> expected)")
|
||||
function vec2_mt.__eq(a, b)
|
||||
if not vec2.is_vec2(a) or not vec2.is_vec2(b) then
|
||||
return false
|
||||
end
|
||||
return a.x == b.x and a.y == b.y
|
||||
end
|
||||
|
||||
|
@ -147,7 +147,7 @@ end
|
||||
function vec3.trim(out, a, len)
|
||||
return out
|
||||
:normalize(a)
|
||||
:mul(out, math.min(vec3.len(a), len))
|
||||
:scale(out, math.min(vec3.len(a), len))
|
||||
end
|
||||
|
||||
--- Get the cross product of two vectors.
|
||||
@ -260,7 +260,7 @@ end
|
||||
function vec3.lerp(out, a, b, s)
|
||||
return out
|
||||
:sub(b, a)
|
||||
:mul(out, s)
|
||||
:scale(out, s)
|
||||
:add(out, a)
|
||||
end
|
||||
|
||||
@ -317,8 +317,9 @@ function vec3_mt.__unm(a)
|
||||
end
|
||||
|
||||
function vec3_mt.__eq(a, b)
|
||||
assert(vec3.is_vec3(a), "__eq: Wrong argument type for left hand operant. (<cpml.vec3> expected)")
|
||||
assert(vec3.is_vec3(b), "__eq: Wrong argument type for right hand operant. (<cpml.vec3> expected)")
|
||||
if not vec3.is_vec3(a) or not vec3.is_vec3(b) then
|
||||
return false
|
||||
end
|
||||
return a.x == b.x and a.y == b.y and a.z == b.z
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user