-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloc.lua
69 lines (67 loc) · 2.19 KB
/
loc.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- Rigid body transform class based on CPML
-- Assumes PL, CPML in namespace
-- IMPORTS ALL ON REQUIRE
namespace "standard"
local plMetatable = {__tostring = function(o) return o:to_string() end}
class.Loc(nil, nil, plMetatable)
function Loc:_init(at, rotate, scale) -- Warning treat contents as const
self.at = at or vec3()
self.rotate = rotate or quat()
self.scale = scale or 1
end
function Loc.fromPose(x,y,z, angle,ax,ay,az)
return Loc(vec3(x,y,z), quat.from_angle_axis(angle,ax,ay,az))
end
function Loc:toPose()
return self.at.x, self.at.y, self.at.z, self.rotate:to_angle_axis_unpack()
end
function Loc:unpack()
return self.at, self.rotate, self.scale
end
function Loc:clone()
return Loc(self.at, self.rotate, self.scale)
end
function Loc:assign(loc)
self.at = loc.at
self.rotate = loc.rotate
self.scale = loc.scale
end
function Loc:scaleUnpack() -- Return scale in 3-component form
return self.scale, self.scale, self.scale
end
function Loc:apply(v) -- The following methods are untested
return self.rotate * (v * self.scale) + self.at
end
function Loc:applyToVector(v)
return self.rotate * (v * self.scale)
end
function Loc:compose(v) -- Return Loc equivalent to "apply self, then v"
return Loc(v:apply(self.at), v.rotate * self.rotate, self.scale * v.scale)
end
function Loc:precompose(v) -- Return Loc equivalent to "apply v, then self"
return v:compose(self)
end
function Loc:recenter(recenterVector)
return Loc(-recenterVector):compose(self):compose(Loc(recenterVector))
end
function Loc:inverse(v)
local unrotate = self.rotate:inverse()
local unscale = self.scale ~= 0 and 1/self.scale or 0
return Loc(unrotate * -self.at * unscale, unrotate, unscale)
end
function Loc:pow(s)
return Loc(self.at * s, self.rotate:pow(s), math.pow(self.scale, s))
end
function Loc:lerp(v, s)
return Loc(self.at:lerp(v.at, s), self.rotate:slerp(v.rotate, s), self.scale + (v.scale - self.scale) * s)
end
function Loc:to_string()
local s = "(at:" .. tostring(self.at) .. " rot:" .. tostring(self.rotate)
if self.scale ~= 1 then s = s .. " scale:" .. tostring(self.scale) end
s = s .. ")"
return s
end
function Loc:to_mat4()
return mat4.from_transform(self.at, self.rotate, self.scale)
end
Loc.zero = Loc()