-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix2.lua
220 lines (148 loc) · 4.38 KB
/
Matrix2.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
local ffi
Matrix2 = {}
setmetatable(Matrix2, Matrix2)
Matrix2.keys = {
r1c1 = 1, r1c2 = 2,
r2c1 = 3, r2c2 = 4,
}
if jit and jit.status() then
ffi = require"ffi"
ffi.cdef([[
typedef union Matrix2
{
float components[4];
} Matrix2;
]])
ffi.metatype("Matrix2", Matrix2)
end
function isMatrix2(matrix)
if ffi then
return ffi.istype("Matrix2", matrix)
else
return type(matrix) == "table" and matrix._isclass and matrix._isclass(Matrix2)
end
end
function Matrix2._isclass(c)
return c == Matrix2
end
local function getComponents(args, components)
for i=1,#args do
if type(args[i]) == "number" then
table.insert(components, args[i])
elseif type(args[i]) == "table" or isVector2(args[i]) or isVector3(args[i]) or isVector4(args[i]) then
getComponents(args[i], components)
else
error("Invalid component #"..(#components+1).." \""..tostring(args[i]).."\"")
end
end
end
------------------------------------------------------------------------
-- CONSTRUCTORS
------------------------------------------------------------------------
function Matrix2.new(...)
local args = {...}
local components = {}
getComponents(args, components)
if #components == 0 then
components = {
1,0,
0,1
}
end
assert(#components == 4, "4 components expected, got "..#components)
local matrix
if ffi then
matrix = ffi.new("Matrix2")
else
matrix = setmetatable({components={}}, Matrix2)
end
for i=1,#components do
matrix.components[i-1] = components[i]
end
return matrix
end
function Matrix2.fromRotation(angle)
return Matrix2(
math.cos(angle), math.sin(angle),
-math.sin(angle), math.cos(angle)
)
end
------------------------------------------------------------------------
-- METHODS
------------------------------------------------------------------------
function Matrix2.get(matrix, row, column)
return matrix[(row-1)*2+column]
end
function Matrix2.set(matrix, row, column, value)
matrix[(row-1)*2+column] = value
end
function Matrix2.unpack(m)
return m[1], m[2],
m[3], m[4]
end
local tmp = {}
function Matrix2.send(mat)
for i=1,4 do
tmp[i] = mat[i]
end
return tmp
end
------------------------------------------------------------------------
-- META
------------------------------------------------------------------------
function Matrix2:__len()
return 4
end
function Matrix2:__index(key)
if rawget(Matrix2, key) then
return rawget(Matrix2, key)
elseif type(key) == "number" or Matrix2.keys[key] then
if Matrix2.keys[key] then key = Matrix2.keys[key] end
return self.components[key-1]
else
return rawget(self, key)
end
end
function Matrix2:__newindex(key, value)
if type(key) == "number" or Matrix2.keys[key] then
if Matrix2.keys[key] then key = Matrix2.keys[key] end
self.components[key-1] = value
elseif self == Matrix2 then
rawset(self, key, value)
else
error("Matrix2 has no attribute \""..tostring(key).."\"")
end
end
function Matrix2:__tostring()
local out = ""
for r=1,2 do
if r > 1 then out = out .. "\n" end
out = out .. "|"
for c=1,2 do
if c > 1 then out = out .. "," end
out = out .. self:get(r,c)
end
out = out .. "|"
end
return out
end
function Matrix2.__concat(a,b)
return tostring(a) .. tostring(b)
end
function Matrix2:__call(...)
return Matrix2.new(...)
end
------------------------------------------------------------------------
-- OPERATORS
------------------------------------------------------------------------
function Matrix2.__eq(a,b)
if not (isMatrix2(a) and isMatrix2(b)) then
return false
end
for i=1,4 do
if not (a[i] == b[i]) then
return false
end
end
return true
end