Skip to content

Commit

Permalink
Refactored the vec3 class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBlanchard committed Dec 20, 2015
1 parent 3893afc commit 1531bf4
Showing 1 changed file with 76 additions and 216 deletions.
292 changes: 76 additions & 216 deletions modules/vec3.lua
Original file line number Diff line number Diff line change
@@ -1,86 +1,50 @@
--[[
Copyright (c) 2010-2013 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--

-- Modified to include 3D capabilities by Bill Shillito, April 2014
-- Various bug fixes by Colby Klein, October 2014

--- 3 dimensional vectors.
-- @module vec3
-- @alias vector

local assert = assert
local sqrt, cos, sin, atan2, acos = math.sqrt, math.cos, math.sin, math.atan2, math.acos

local vector = {}
vector.__index = vector

--- Instance a new vec3.
-- @param x X value, table containing 3 elements, or another vector.
-- @param y Y value
-- @param z Z value
-- @return vec3
local 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
local sqrt= math.sqrt
local ffi = require "ffi"

local function isvector(v)
return getmetatable(v) == vector or type(v.x and v.y and v.z) == "number"
end
ffi.cdef[[
typedef struct {
double x, y, z;
} cpml_vec3;
]]

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)
local vec3 = {}
local cpml_vec3 = ffi.typeof("cpml_vec3")
vec3.new = cpml_vec3

--- Create a new vector containing the same data.
-- @return vec3
function vector:clone()
return new(self.x, self.y, self.z)
function vec3.clone(a)
ffi.copy(vec3.new(), a, ffi.sizeof(out))
end

--- Unpack the vector into its components.
-- @return number
-- @return number
-- @return number
function vector:unpack()
return self.x, self.y, self.z
function vec3.add(out, a, b)
out.x = a.x + b.x
out.y = a.y + b.y
out.z = a.z + b.z
end

function vector:__tostring()
return string.format("(%+0.3f,%+0.3f,%+0.3f)", self.x, self.y, self.z)
function vec3.sub(out, a, b)
out.x = a.x - b.x
out.y = a.y - b.y
out.z = a.z - b.z
end

function vector.__unm(a)
return new(-a.x, -a.y, -a.z)
function vec3.mul(out, a, b)
out.x = a.x * b
out.y = a.y * b
out.z = a.z * b
end

<<<<<<< HEAD
function vec3.div(out, a, b)
out.x = a.x / b
out.y = a.y / b
out.z = a.z / b
end

function vec3.cross(out, a, b)
out.x = a.y*b.z - a.z*b.y
out.y = a.z*b.x - a.x*b.z
out.z = a.x*b.y - a.y*b.x
=======
function vector.__add(a,b)
if type(a) == "number" then
return new(a+b.x, a+b.y, a+b.z)
Expand Down Expand Up @@ -127,190 +91,86 @@ end

function vector.__eq(a,b)
return a.x == b.x and a.y == b.y and a.z == b.z
>>>>>>> refs/remotes/excessive/master
end

function vector.__lt(a,b)
-- This is a lexicographical order.
return a.x < b.x or (a.x == b.x and a.y < b.y) or (a.x == b.x and a.y == b.y and a.z < b.z)
end

function vector.__le(a,b)
-- This is a lexicographical order.
return a.x <= b.x and a.y <= b.y and a.z <= b.z
function vec3.dot(a, b)
return a.x * b.x + a.y * b.y + a.z * b.z
end

--- Dot product.
-- @param a first vec3 to dot with
-- @param b second vec3 to dot with
-- @return number
function vector.dot(a,b)
assert(isvector(a) and isvector(b), "dot: wrong argument types (<vector> expected)")
return a.x*b.x + a.y*b.y + a.z*b.z
function vec3.normalize(out, a)
local l = vec3.len(a)
out.x, out.y, out.z = a.x / l, a.y / l, a.z / l
end

function vector:len2()
return self.x * self.x + self.y * self.y + self.z * self.z
function vec3.len2(a)
return a.x * a.x + a.y * a.y + a.z * a.z
end

--- Vector length/magnitude.
-- @return number
function vector:len()
return sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
function vec3.len(a)
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z)
end

--- Distance between two points.
-- @param a first point
-- @param b second point
-- @return number
function vector.dist(a, b)
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
function vec3.dist(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return sqrt(dx * dx + dy * dy + dz * dz)
end

--- Squared distance between two points.
-- @param a first point
-- @param b second point
-- @return number
function vector.dist2(a, b)
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
function vec3.dist2(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return (dx * dx + dy * dy + dz * dz)
end

--- Normalize vector.
-- Scales the vector in place such that its length is 1.
-- @return vec3
function vector:normalize_inplace()
local l = self:len()
if l > 0 then
self.x, self.y, self.z = self.x / l, self.y / l, self.z / l
end
return self
end

--- Normalize vector.
-- Returns a copy of the vector scaled such that its length is 1.
-- @return vec3
function vector:normalize()
return self:clone():normalize_inplace()
end

--- Rotate vector about an axis.
-- @param phi Amount to rotate, in radians
-- @param axis Axis to rotate by
-- @return vec3
function vector:rotate(phi, axis)
if axis == nil then return self end

local u = axis:normalize() or Vector(0,0,1) -- default is to rotate in the xy plane
local c, s = cos(phi), sin(phi)

-- Calculate generalized rotation matrix
local m1 = new((c + u.x * u.x * (1-c)), (u.x * u.y * (1-c) - u.z * s), (u.x * u.z * (1-c) + u.y * s))
local m2 = new((u.y * u.x * (1-c) + u.z * s), (c + u.y * u.y * (1-c)), (u.y * u.z * (1-c) - u.x * s))
local m3 = new((u.z * u.x * (1-c) - u.y * s), (u.z * u.y * (1-c) + u.x * s), (c + u.z * u.z * (1-c)) )

-- Return rotated vector
return new( m1:dot(self), m2:dot(self), m3:dot(self) )
end

function vector:rotate_inplace(phi, axis)
self = self:rotated(phi, axis)
function vec3.lerp(a, b, s)
return a + s * (b - a)
end

function vector:perpendicular()
return new(-self.y, self.x, 0)
end

function vector:project_on(v)
assert(isvector(v), "invalid argument: cannot project vector on " .. type(v))
-- (self * v) * v / v:len2()
local s = (self.x * v.x + self.y * v.y + self.z * v.z) / (v.x * v.x + v.y * v.y + v.z * v.z)
return new(s * v.x, s * v.y, s * v.z)
function vec3.unpack(a)
return a.x, a.y, a.z
end

function vector:project_from(v)
assert(isvector(v), "invalid argument: cannot project vector on " .. type(v))
-- Does the reverse of projectOn.
local s = (v.x * v.x + v.y * v.y + v.z * v.z) / (self.x * v.x + self.y * v.y + self.z * v.z)
return new(s * v.x, s * v.y, s * v.z)
end

function vector:mirror_on(v)
assert(isvector(v), "invalid argument: cannot mirror vector on " .. type(v))
local s = 2 * (self.x * v.x + self.y * v.y + self.z * v.z) / (v.x * v.x + v.y * v.y + v.z * v.z)
return new(s * v.x - self.x, s * v.y - self.y, s * v.z - self.z)
function vec3.tostring(a)
return string.format("(%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z)
end

--- Cross product.
-- @param v vec3 to cross with
-- @return vec3
function vector:cross(v)
assert(isvector(v), "cross: wrong argument types (<vector> expected)")
return new(self.y*v.z - self.z*v.y, self.z*v.x - self.x*v.z, self.x*v.y - self.y*v.x)
end
local vec3_mt = {}

-- @return vec3
function vector:trim_inplace(maxLen)
-- ref.: http://blog.signalsondisplay.com/?p=336
local s = maxLen * maxLen / self:len2()
s = (s > 1 and 1) or math.sqrt(s)
self.x, self.y, self.z = self.x * s, self.y * s, self.z * s
return self
end
vec3_mt.__index = vec3
vec3_mt.__call = vec3.new
vec3_mt.__tostring = vec3.tostring

-- @return vec3
function vector:trim(maxLen)
return self:clone():trim_inplace(maxLen)
function vec3_mt.__unm(a)
return vec3.new(-a.x, -a.y, -a.z)
end

-- @return number
function vector:angle_to(other)
-- Only makes sense in 2D.
if other then
return atan2(self.y-other.y, self.x-other.x)
end
return atan2(self.y, self.x)
function vec3_mt.__eq(a,b)
return a.x == b.x and a.y == b.y and a.z == b.z
end

-- @return number
function vector:angle_between(other)
if other then
return acos(self:dot(other) / (self:len() * other:len()))
end
return 0
function vec3_mt.__add(a, b)
local temp = vec3.new()
vec3.add(temp, a, b)
return temp
end

-- @return vec3
function vector:orientation_to_direction(orientation)
orientation = orientation or new(0, 1, 0)
return orientation
:rotated(self.z, unit_z)
:rotated(self.y, unit_y)
:rotated(self.x, unit_x)
function vec3_mt.__mul(a, b)
local temp = vec3.new()
vec3.mul(temp, a, b)
return temp
end

-- http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/
function vector.lerp(a, b, s)
return a + s * (b - a)
function vec3_mt.__div(a, b)
local temp = vec3.new()
vec3.div(temp, a, b)
return temp
end

-- the module
return setmetatable(
{
new = new,
lerp = lerp,
isvector = isvector,
zero = zero,
unit_x = unit_x,
unit_y = unit_y,
unit_z = unit_z
}, {
__call = function(_, ...) return new(...) end
}
)
ffi.metatype(cpml_vec3, vec3_mt)
return setmetatable({}, vec3_mt)

0 comments on commit 1531bf4

Please sign in to comment.