-
Notifications
You must be signed in to change notification settings - Fork 0
/
xform.lua
executable file
·412 lines (323 loc) · 11.9 KB
/
xform.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
-- Code to compute transformations
local _M = {}
local abs = math.abs
local rad = math.rad
local cos = math.cos
local sin = math.sin
local unpack = unpack or table.unpack
-- Loads transformations
local transformations = require"facade".driver()
_M.identity = transformations.identity()
----------------------------------------------------------
-- Move Euclidean points (x, y) to Projective points (x, y, w)
-- @param x
-- @param y
-- @return P(x,y) in Projective points (x, y, w)
----------------------------------------------------------
function e2p(x, y)
return {x, y, 1}
end
----------------------------------------------------------
-- Move Projective points (x, y, w) to Euclidean points (x, y)
-- @param x
-- @param y
-- @param w
-- @return P(x,y) in Euclidean points
----------------------------------------------------------
function p2e(x, y, w)
return {x/w, y/w}
end
----------------------------------------------------------
-- Compute the rotation of point P(x, y) by angle
-- @param angle : Angle for rotation
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after rotation transformation
----------------------------------------------------------
local function rotate_by_angle(angle, x, y)
local rotate = transformations.rotation(angle)
local x, y, w = rotate:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix of rotate
-- @param angle
-- @return : Matrix for rotate
----------------------------------------------------------
local function matrix_rotate(angle)
local rotate = transformations.rotation(angle)
return rotate
end
----------------------------------------------------------
-- Compute the rotation of point P(x, y) by cosine and sine
-- @param cos, sin
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after rotation transformation
----------------------------------------------------------
local function rotate_by_cos_sin(cos, sin, x, y)
local rotate = transformations.rotation(cos, sin)
local x, y, w = rotate:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the translation of point P(x, y)
-- @param tx : Coefficient x in translate
-- @param ty : Coefficient y in translate
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after translation transformation
----------------------------------------------------------
local function translate(tx, ty, x, y)
local translate = transformations.translation(tx, ty)
local x, y, w = translate:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix of translation
-- @param tx : Coefficient x in translation
-- @param ty : Coefficient y in translation
-- @return : Matrix for translation transformation
----------------------------------------------------------
local function matrix_translation (tx, ty)
local translate = transformations.translation(tx, ty)
return translate
end
----------------------------------------------------------
-- Compute the anisotropic scale of point P(x, y)
-- @param sx : Coefficient x in scale
-- @param sy : Coefficient y in scale
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after scale transformation
----------------------------------------------------------
local function anisotropic_scale(sx, sy, x, y)
local scale = transformations.scaling(sx, sy)
local x, y, w = scale:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix of anisotropic scale
-- @param sx : Coefficient x in scale
-- @param sy : Coefficient y in scale
-- @return : Matrix for scale transformation
----------------------------------------------------------
local function matrix_anisotropic_scale(sx, sy)
local scale = transformations.scaling(sx, sy)
return scale
end
----------------------------------------------------------
-- Compute the isotropic scale of point P(x, y)
-- @param s : Coefficient for scale
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after scale transformation
----------------------------------------------------------
local function isotropic_scale(s, x, y)
local scale = transformations.scaling(s)
local x, y, w = scale:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix of isotropic scale
-- @param s : Coefficient for scale
-- @return : Matrix for scale transformation
----------------------------------------------------------
local function matrix_isotropic_scale(s)
local scale = transformations.scaling(s)
return scale
end
----------------------------------------------------------
-- Compute the affinity
-- @param a, b, tx, c, d, ty
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after affinity transformation
----------------------------------------------------------
local function affinity(a, b, tx, c, d, ty, x, y)
local affinity = transformations.affinity(a, b, tx, c, d, ty)
local x, y, w = affinity:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix affinity
-- @param a, b, tx, c, d, ty
-- @return : Matrix affinity transformation
----------------------------------------------------------
local function matrix_affinity(a, b, tx, c, d, ty)
local affinity = transformations.affinity(a, b, tx, c, d, ty)
return affinity
end
----------------------------------------------------------
-- Compute the linear
-- @param a11, a12, a21, a22
-- @param x : Position x of the rotation point
-- @param y : Position y of the rotation point
-- @return : Point after linear transformation
----------------------------------------------------------
local function linear(a11, a12, a21, a22, x, y)
local linear = transformations.linear(a11, a12, a21, a22)
local x, y, w = linear:apply(x, y)
return p2e(x, y, w)
end
----------------------------------------------------------
-- Compute the matrix linear
-- @param a11, a12, a21, a22
-- @return : Matrix for linear transformation
----------------------------------------------------------
local function matrix_linear(a11, a12, a21, a22)
local linear = transformations.linear(a11, a12, a21, a22)
return linear
end
----------------------------------------------------------
-- Compute the anisotropic scale
-- @param data : Points for update values
-- @param sx, sy
-- @return : Points after anisotropic scale transformation
----------------------------------------------------------
function _M.compute_anisotropic_scale(data, sx, sy)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(anisotropic_scale(sx, sy, data[i], data[i+1]))
end
return n_data
end
function _M.matrix_anisotropic_scale(sx, sy)
return transformations.scaling(sx, sy)
end
----------------------------------------------------------
-- Compute the matrix anisotropic scale
-- @param sx, sy
-- @return : Matrix for anisotropic scale transformation
----------------------------------------------------------
function _M.compute_matrix_anisotropic_scale(sx, sy)
return matrix_anisotropic_scale(sx, sy)
end
----------------------------------------------------------
-- Compute the isotropic scale
-- @param data : Points for update values
-- @param s
-- @return : Points after anisotropic scale transformation
----------------------------------------------------------
function _M.compute_isotropic_scale(data, s)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(isotropic_scale(s, data[i], data[i+1]))
end
return n_data
end
----------------------------------------------------------
-- Compute the matrix of isotropic scale
-- @param s
-- @return : Matrix for isotropic scale transformation
----------------------------------------------------------
function _M.compute_matrix_isotropic_scale(s)
return matrix_isotropic_scale(s)
end
----------------------------------------------------------
-- Compute the rotate by angle
-- @param data : Points for update values
-- @param angle
-- @return : Points after rotate transformation
----------------------------------------------------------
function _M.compute_rotate_by_angle(data, angle)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(rotate_by_angle(angle, data[i], data[i+1]))
end
return n_data
end
function _M.compute_rotate_by_cos_sin(data, cos, sin)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = (rotate_by_cos_sin(cos, sin, data[i], data[i+1]))
end
return n_data
end
function _M.matrix_rotate_by_angle(angle)
return transformations.rotation(angle)
end
----
----------------------------------------------------------
-- Compute the matrix of rotate
-- @param angle
-- @return : Matrix for rotate transformation
----------------------------------------------------------
function _M.compute_matrix_rotate (angle)
return matrix_rotate(angle)
end
----------------------------------------------------------
-- Compute the translation
-- @param data : Points for update values
-- @param tx, ty
-- @return : Points after translate transformation
----------------------------------------------------------
function _M.compute_translation(data, tx, ty)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(translate(tx, ty, data[i], data[i+1]))
end
return n_data
end
function _M.matrix_translation(tx,ty)
return transformations.translation(tx, ty)
end
----------------------------------------------------------
-- Compute the matrix of translation
-- @param tx, ty
-- @return : Matrix for translation transformation
----------------------------------------------------------
function _M.compute_matrix_translation(tx, ty)
return matrix_translation(tx, ty)
end
----------------------------------------------------------
-- Compute the affinity
-- @param data : Points for update values
-- @param a, b, tx, c, d, ty
-- @return : Points after translate transformation
----------------------------------------------------------
function _M.compute_affinity(data, a, b, tx, c, d, ty)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(affinity(a, b, tx, c, d, ty, data[i], data[i+1]))
end
return n_data
end
----------------------------------------------------------
-- Compute the matrix affinity
-- @param a, b, tx, c, d, ty
-- @return : Matrix for affinity transformation
----------------------------------------------------------
function _M.compute_matrix_affinity(a, b, tx, c, d, ty)
return matrix_affinity(a, b, tx, c, d, ty)
end
----------------------------------------------------------
-- Compute the linear
-- @param data : Points for update values
-- @param a11, a12, a21, a22
-- @return : Points after linear transformation
----------------------------------------------------------
function _M.compute_linear(data, a11, a12, a21, a22)
local n_data = {}
for i = 1, #data, 2 do
n_data[i], n_data[i + 1] = unpack(linear(a11, a12, a21, a22, data[i], data[i+1]))
end
return n_data
end
----------------------------------------------------------
-- Compute the matrix linear
-- @param a11, a12, a21, a22
-- @return : Matrix for linear transformation
----------------------------------------------------------
function _M.compute_matrix_linear(a11, a12, a21, a22)
return matrix_linear(a11, a12, a21, a22)
end
function _M.compute_det2(M)
local a, b, c, d = unpack(M, 1, 4)
return a*d - b*c
end
function _M.compute_det3(M)
local a, b, c, d, e, f, g, h, i = unpack(M, 1, 9)
return -c*e*g + b*f*g + c*d*h - a*f*h - b*d*i + a*e*i
end
return _M