Advertisement
Krutoy242

3D Vector Class

Feb 26th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. -- ********************************************************************************** --
  2. -- **   3D Vector                                                                  ** --
  3. -- **                                                                              ** --
  4. -- **   Modified version of 2d vector from                                         ** --
  5. -- **   https://github.com/vrld/hump/blob/master/vector.lua                        ** --
  6. -- **                                                                              ** --
  7. -- ********************************************************************************** --
  8.  
  9. vec3 = (function()
  10.   local assert = assert
  11.   local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
  12.  
  13.   local vector = {}
  14.   vector.__index = vector
  15.  
  16.   local function new(x,y,z)
  17.     return setmetatable({x = x or 0, y = y or 0, z = z or 0}, vector)
  18.   end
  19.   local zero = new(0,0,0)
  20.  
  21.   local function isvector(v)
  22.     return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number' and type(v.z) == 'number'
  23.   end
  24.  
  25.   function vector:clone()
  26.     return new(self.x, self.y, self.z)
  27.   end
  28.  
  29.   function vector:unpack()
  30.     return self.x, self.y, self.z
  31.   end
  32.  
  33.   function vector:__tostring()
  34.     return "("..tonumber(self.x)..","..tonumber(self.y)..","..tonumber(self.z)..")"
  35.   end
  36.  
  37.   function vector.__unm(a)
  38.     return new(-a.x, -a.y, -a.z)
  39.   end
  40.  
  41.   function vector.__add(a,b)
  42.     assert(isvector(a) and isvector(b), "Add: wrong argument types (<vector> expected)")
  43.     return new(a.x+b.x, a.y+b.y, a.z+b.z)
  44.   end
  45.  
  46.   function vector.__sub(a,b)
  47.     assert(isvector(a) and isvector(b), "Sub: wrong argument types (<vector> expected)")
  48.     return new(a.x-b.x, a.y-b.y, a.z-b.z)
  49.   end
  50.  
  51.   function vector.__mul(a,b)
  52.     if type(a) == "number" then
  53.       return new(a*b.x, a*b.y, a*b.z)
  54.     elseif type(b) == "number" then
  55.       return new(b*a.x, b*a.y, b*a.z)
  56.     else
  57.       assert(isvector(a) and isvector(b), "Mul: wrong argument types (<vector> or <number> expected)")
  58.       return a.x*b.x + a.y*b.y + a.z*b.z
  59.     end
  60.   end
  61.  
  62.   function vector.__div(a,b)
  63.     assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
  64.     return new(a.x / b, a.y / b, a.z / b)
  65.   end
  66.  
  67.   function vector.__eq(a,b)
  68.     return a.x == b.x and a.y == b.y and a.z == b.z
  69.   end
  70.  
  71.   function vector.__lt(a,b)
  72.     return a.x < b.x and a.y < b.y and a.z < b.z
  73.   end
  74.  
  75.   function vector.__le(a,b)
  76.     return a.x <= b.x and a.y <= b.y and a.z <= b.z
  77.   end
  78.  
  79.   function vector.permul(a,b)
  80.     assert(isvector(a) and isvector(b), "permul: wrong argument types (<vector> expected)")
  81.     return new(a.x*b.x, a.y*b.y, a.z*b.z)
  82.   end
  83.  
  84.   function vector:len2()
  85.     return self.x * self.x + self.y * self.y + self.z * self.z
  86.   end
  87.  
  88.   function vector:len()
  89.     return sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  90.   end
  91.  
  92.   function vector.dist(a, b)
  93.     assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
  94.     local dx = a.x - b.x
  95.     local dy = a.y - b.y
  96.     local dz = a.z - b.z
  97.     return sqrt(dx * dx + dy * dy + dz * dz)
  98.   end
  99.  
  100.   function vector.dist2(a, b)
  101.     assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
  102.     local dx = a.x - b.x
  103.     local dy = a.y - b.y
  104.     local dz = a.z - b.z
  105.     return (dx * dx + dy * dy + dz * dz)
  106.   end
  107.  
  108.   function vector:normalize_inplace()
  109.     local l = self:len()
  110.     if l > 0 then
  111.       self.x, self.y, self.z = self.x / l, self.y / l, self.z / l
  112.     end
  113.     return self
  114.   end
  115.  
  116.   function vector:normalized()
  117.     return self:clone():normalize_inplace()
  118.   end
  119.  
  120.   function vector:rotate_inplace_z(phi)
  121.     local c, s = cos(phi), sin(phi)
  122.     self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
  123.     return self
  124.   end  
  125.   function vector:rotate_inplace_x(phi)
  126.     local c, s = cos(phi), sin(phi)
  127.     self.y, self.z = c * self.y - s * self.z, s * self.y + c * self.z
  128.     return self
  129.   end  
  130.   function vector:rotate_inplace_y(phi)
  131.     local c, s = cos(phi), sin(phi)
  132.     self.z, self.x = c * self.z - s * self.x, s * self.z + c * self.x
  133.     return self
  134.   end
  135.    
  136.   function vector:rotated_z(phi)
  137.     local c, s = cos(phi), sin(phi)
  138.     return new(c * self.x - s * self.y, s * self.x + c * self.y, self.z)
  139.   end  
  140.   function vector:rotated_x(phi)
  141.     local c, s = cos(phi), sin(phi)
  142.     return new(self.x, c * self.y - s * self.z, s * self.y + c * self.z)
  143.   end  
  144.   function vector:rotated_y(phi)
  145.     local c, s = cos(phi), sin(phi)
  146.     return new(s * self.z + c * self.x, self.y, c * self.z - s * self.x)
  147.   end
  148.  
  149.   function vector:cross(v)
  150.     assert(isvector(v), "cross: wrong argument types (<vector> expected)")
  151.     return new(self.Y * v.Z - self.Z * v.Y,
  152.                self.Z * v.X - self.X * v.Z,
  153.                self.X * v.Y - self.Y * v.X)
  154.   end
  155.  
  156.   -- ref.: http://blog.signalsondisplay.com/?p=336
  157.   function vector:trim_inplace(maxLen)
  158.     local s = maxLen * maxLen / self:len2()
  159.     s = (s > 1 and 1) or math.sqrt(s)
  160.     self.x, self.y, self.z = self.x * s, self.y * s, self.z * s
  161.     return self
  162.   end
  163.  
  164.   function vector:trimmed(maxLen)
  165.     return self:clone():trim_inplace(maxLen)
  166.   end
  167.  
  168.  
  169.   -- the module
  170.   return setmetatable({new = new, isvector = isvector, zero = zero},
  171.   {__call = function(_, ...) return new(...) end})
  172. end)()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement