Looks like we don't need quat.div after all.

This commit is contained in:
Landon Manning 2015-12-28 12:40:52 -04:00
parent e9b00c2b71
commit 2347bd9706
2 changed files with 27 additions and 61 deletions

View File

@ -80,22 +80,20 @@ end
-- http://www.lighthouse3d.com/tutorials/maths/ray-triangle-intersection/
local h, s, q, e1, e2 = vec3(), vec3(), vec3(), vec3(), vec3()
function intersect.ray_triangle(ray, triangle)
local a, f, u, v
vec3.sub(e1, triangle[2], triangle[1])
vec3.sub(e2, triangle[3], triangle[1])
vec3.cross(h, ray.direction, e2)
a = vec3.dot(h, e1)
local a = vec3.dot(h, e1)
-- if a is too close to 0, ray does not intersect triangle
if abs(a) <= FLT_EPSILON then
return false
end
f = 1 / a
local f = 1 / a
vec3.sub(s, ray.position, triangle[1])
u = vec3.dot(s, h) * f
local u = vec3.dot(s, h) * f
-- ray does not intersect triangle
if u < 0 or u > 1 then
@ -103,7 +101,7 @@ function intersect.ray_triangle(ray, triangle)
end
vec3.cross(q, s, e1)
v = vec3.dot(ray.direction, q) * f
local v = vec3.dot(ray.direction, q) * f
-- ray does not intersect triangle
if v < 0 or u + v > 1 then
@ -135,8 +133,6 @@ end
local q, r, s = vec3(), vec3(), vec3()
local n, u, v = vec3(), vec3(), vec3()
function intersect.ray_triangle2(ray, triangle)
local b1, b2, b3, a, t
-- Edge vectors
vec3.sub(u, triangle[2], triangle[1])
vec3.sub(v, triangle[3], triangle[1])
@ -145,7 +141,7 @@ function intersect.ray_triangle2(ray, triangle)
vec3.cross(n, u, v)
vec3.cross(q, ray.direction, v)
a = vec3.dot(q, u)
local a = vec3.dot(q, u)
-- Backfacing or nearly parallel?
if vec3.dot(n, ray.direction) >= FLT_EPSILON or
@ -157,9 +153,9 @@ function intersect.ray_triangle2(ray, triangle)
vec3.div(s, s, a)
vec3.cross(r, s, u)
b1 = vec3.dot(s, q))
b2 = vec3.dot(r, ray.direction))
b3 = 1 - b[1] - b[2])
local b1 = vec3.dot(s, q))
local b2 = vec3.dot(r, ray.direction))
local b3 = 1 - b[1] - b[2])
-- Intersected outside triangle?
if b1 < FLT_EPSILON or
@ -168,7 +164,7 @@ function intersect.ray_triangle2(ray, triangle)
return false
end
t = vec3.dot(r, v)
local t = vec3.dot(r, v)
if t >= FLT_EPSILON then
local out = vec3()

View File

@ -126,46 +126,19 @@ end
-- @tparam quat b
-- @treturn quat out
function quat.mul(out, a, b)
-- quat * quat
if type(b) == "table" and b.x and b.y and b.z and b.w then
out.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y
out.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z
out.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x
out.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
-- quat * vec3
elseif type(b) == "table" and b.x and b.y and b.z then
local qv = vec3(a.x, a.y, a.z)
local uv, uuv = vec3(), vec3()
vec3.cross(uv, qv, b)
vec3.cross(uuv, qv, uv)
vec3.mul(out, uv, a.w)
vec3.add(out, out, uuv)
vec3.mul(out, out, 2)
vec3.add(out, b, out)
end
out.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y
out.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z
out.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x
out.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
return out
end
--- Perform a quaternion division.
--- Perform a quaternion and vec3 multiplication.
-- @tparam quat out
-- @tparam quat a
-- @tparam quat b
-- @treturn quat out
function quat.div(out, a, b)
-- quat / quat
if type(b) == "table" and b.x and b.y and b.z and b.w then
quat.reciprocal(out, b)
quat.mul(out, a, out)
-- quat / number
elseif type(b) == "number" then
quat.scale(out, a, 1 / b)
end
return out
end
local uv, uuv = vec3.new(), vec3.new()
-- @tparam vec3 b
-- @treturn vec3 out
local uv, uuv = vec3(), vec3()
function quat.mul_vec3(out, a, b)
vec3.cross(uv, a, b)
vec3.cross(uuv, a, uv)
@ -415,20 +388,17 @@ end
function quat_mt.__mul(a, b)
assert(quat.isquat(a), "__mul: Wrong argument type for left hand operant. (<cpml.quat> expected)")
assert(quat.isquat(b), "__mul: Wrong argument type for right hand operant. (<cpml.quat> expected)")
assert(quat.isquat(b) or vec3.isvec3(b), "__mul: Wrong argument type for right hand operant. (<cpml.quat> or <cpml.vec3> expected)")
local temp = quat.new()
quat.mul(temp, a, b)
return temp
end
function quat_mt.__div(a, b)
assert(quat.isquat(a), "__div: Wrong argument type for left hand operant. (<cpml.quat> expected)")
assert(quat.isquat(b), "__div: Wrong argument type for right hand operant. (<cpml.quat> expected)")
local temp = quat.new()
quat.div(temp, a, b)
return temp
if quat.isquat(b) then
local temp = quat.new()
quat.mul(temp, a, b)
return temp
elseif vec3.isvec3(b) then
local temp = vec3()
quat.mul_vec3(temp, a, b)
return temp
end
end
function quat_mt.__pow(a, n)