-
Notifications
You must be signed in to change notification settings - Fork 1
/
quaternion.lua
126 lines (105 loc) · 2.57 KB
/
quaternion.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local vect = require 'dokidoki.vect'
local mt
local function make(w, x, y, z)
return setmetatable({w, x, y, z}, mt)
end
local function from_rotation(axis, angle)
local s = math.sin(angle/2)
local c = math.cos(angle/2)
return make(c, s * axis[1], s * axis[2], s * axis[3])
end
-- http://www.flipcode.com/documents/matrfaq.html#Q55
-- https://github.com/henkboom/rhizome/blob/master/quaternion.c#L39
local function from_ijk(axis, angle)
assert(false)
end
local function look_at(direction, up)
assert(false)
end
local function mul(a, b)
return make(
a[1]*b[1] - a[2]*b[2] - a[3]*b[3] - a[4]*b[4],
a[1]*b[2] + a[2]*b[1] + a[3]*b[4] - a[4]*b[3],
a[1]*b[3] + a[3]*b[1] + a[4]*b[2] - a[2]*b[4],
a[1]*b[4] + a[4]*b[1] + a[2]*b[3] - a[3]*b[2])
end
local function sqrmag(q)
return q[1]*q[1] + q[2]*q[2] + q[3]*q[3] + q[4]*q[4]
end
local function mag(q)
return math.sqrt(sqrmag(q))
end
local function norm(q)
local sm = sqrmag(q)
if(math.abs(sm - 1) > 0.000001) then
local mag = math.sqrt(sm)
return make(q[1]/mag, q[2]/mag, q[3]/mag, q[4]/mag)
end
end
local function conjugate(q)
return make(q[1], -q[2], -q[3], -q[4])
end
local function rotate_vect(q, v)
local result = q * make(0, v[1], v[2], v[3]) * conjugate(q)
return vect(result[2], result[3], result[4])
end
local function rotated_i(q)
return vect(
1 - 2*(q[3]*q[3] + q[4]*q[4]),
2*(q[2]*q[3] + q[4]*q[1]),
2*(q[2]*q[4] - q[3]*q[1]))
end
local function rotated_j(q)
return vect(
2*(q[2]*q[3] - q[4]*q[1]),
1 - 2*(q[2]*q[2] + q[4]*q[4]),
2*(q[3]*q[4] + q[2]*q[1]))
end
local function rotated_k(q)
return vect(
2*(q[2]*q[4] + q[3]*q[1]),
2*(q[3]*q[4] - q[2]*q[1]),
1 - 2*(q[2]*q[2] + q[3]*q[3]))
end
local function to_ijk_string(q)
return
'[i = ' .. rotated_i(q) ..
', j = ' .. rotated_j(q) ..
', k = ' .. rotated_k(q) .. ']'
end
local function to_string(q)
return
'q(' .. q[1] .. ' + ' ..
q[2] .. 'i + ' ..
q[3] .. 'j + ' ..
q[4] .. 'k)'
end
mt = {
__mul = mul,
__tostring = to_string
}
local identity = make(1, 0, 0, 0)
return setmetatable(
{
make = make,
from_rotation = from_rotation,
from_ijk = from_ijk,
look_at = look_at,
mul = mul,
sqrmag = sqrmag,
mag = mag,
norm = norm,
conjugate = conjugate,
rotate_vect = rotate_vect,
rotated_i = rotated_i,
rotated_j = rotated_j,
rotated_k = rotated_k,
to_matrix = to_matrix,
identity = identity,
to_ijk_string = to_ijk_string,
to_string = to_string,
},
{
__call = function (_, ...) return make(...) end
}
)