-
Notifications
You must be signed in to change notification settings - Fork 5
/
matrix.lua
314 lines (264 loc) · 8.7 KB
/
matrix.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
local argcheck = require 'argcheck'
local class = require 'class'
local ffi = require 'ffi'
local cairo = require 'cairo.env'
local C = cairo.C
local doc = require 'argcheck.doc'
doc[[
### Transformation matrices
]]
local Matrix = class.new('cairo.Matrix')
cairo.Matrix = Matrix
Matrix.__init = argcheck{
nonamed = true,
{name="self", type="cairo.Matrix"},
call =
function(self)
self.C = ffi.new('cairo_matrix_t')
return self
end
}
Matrix.__init = argcheck{
{name="self", type="cairo.Matrix"},
{name="xx", type="number"},
{name="yx", type="number"},
{name="xy", type="number"},
{name="yy", type="number"},
{name="x0", type="number"},
{name="y0", type="number"},
overload = Matrix.__init,
call =
function(self, xx, yx, xy, yy, x0, y0)
self.C = ffi.new('cairo_matrix_t')
self:set(xx, yx, xy, yy, x0, y0)
return self
end
}
Matrix.__init = argcheck{
{name="self", type="cairo.Matrix"},
{name="values", type="table"},
overload = Matrix.__init,
call =
function(self, values)
self.C = ffi.new('cairo_matrix_t')
self:set(values)
return self
end
}
Matrix.totable = argcheck{
{name="self", type="cairo.Matrix"},
call =
function(self)
return {xx=self.C.xx, yx=self.C.yx, yy=self.C.xy, yy=self.C.yy, x0=self.C.x0, y0=self.C.y0}
end
}
Matrix.set = argcheck{
{name="self", type="cairo.Matrix"},
{name="xx", type="number"},
{name="yx", type="number"},
{name="xy", type="number"},
{name="yy", type="number"},
{name="x0", type="number"},
{name="y0", type="number"},
call =
function(self, xx, yx, xy, yy, x0, y0)
cairo.C.cairo_matrix_init(self.C, xx, yx, xy, yy, x0, y0)
end
}
Matrix.set = argcheck{
{name="self", type="cairo.Matrix"},
{name="values", type="table"},
overload = Matrix.set,
call =
function(self, values)
assert(values.xx and values.yx and values.xy and values.yy and values.x0 and values.y0, 'missing table fields')
cairo.C.cairo_matrix_init(self.C, values.xx, values.yx, values.xy, values.yy, values.x0, values.y0)
end
}
local IdentityMatrix = class.new('cairo.IdentityMatrix', 'cairo.Matrix')
cairo.IdentityMatrix = IdentityMatrix
IdentityMatrix.__init = argcheck{
{name="self", type="cairo.IdentityMatrix"},
call =
function(self)
self.C = ffi.new('cairo_matrix_t')
cairo.C.cairo_matrix_init_identity(self.C)
return self
end
}
local TranslateMatrix = class.new('cairo.TranslateMatrix', 'cairo.Matrix')
cairo.TranslateMatrix = TranslateMatrix
TranslateMatrix.__init = argcheck{
{name="self", type="cairo.TranslateMatrix"},
{name="tx", type="number"},
{name="ty", type="number"},
call =
function(self, tx, ty)
self.C = ffi.new('cairo_matrix_t')
cairo.C.cairo_matrix_init_translate(self.C, tx, ty)
return self
end
}
local ScaleMatrix = class.new('cairo.ScaleMatrix', 'cairo.Matrix')
cairo.ScaleMatrix = ScaleMatrix
ScaleMatrix.__init = argcheck{
{name="self", type="cairo.ScaleMatrix"},
{name="sx", type="number"},
{name="sy", type="number"},
call =
function(self, sx, sy)
self.C = ffi.new('cairo_matrix_t')
cairo.C.cairo_matrix_init_scale(self.C, sx, sy)
return self
end
}
local RotateMatrix = class.new('cairo.RotateMatrix', 'cairo.Matrix')
cairo.RotateMatrix = RotateMatrix
RotateMatrix.__init = argcheck{
{name="self", type="cairo.RotateMatrix"},
{name="radians", type="number"},
call =
function(self, radians)
self.C = ffi.new('cairo_matrix_t')
cairo.C.cairo_matrix_init_rotate(self.C, radians)
return self
end
}
Matrix.translate = argcheck{
doc = [[
<a name="Matrix.translate">
#### Matrix.translate(@ARGP)
@ARGT
Applies a translation by `tx`, `ty` to the transformation in
`matrix`. The effect of the new transformation is to first translate
the coordinates by `tx` and `ty`, then apply the original transformation
to the coordinates.
]],
{name="self", type="cairo.Matrix"},
{name="tx", type="number", doc="amount to translate in the X direction"},
{name="ty", type="number", doc="amount to translate in the Y direction"},
call =
function(self, tx, ty)
cairo.C.cairo_matrix_translate(self.C, tx, ty)
end
}
Matrix.scale = argcheck{
doc = [[
<a name="Matrix.scale">
#### Matrix.scale(@ARGP)
@ARGT
Applies scaling by `sx`, `sy` to the transformation in `matrix`. The
effect of the new transformation is to first scale the coordinates
by `sx` and `sy`, then apply the original transformation to the coordinates.
]],
{name="self", type="cairo.Matrix"},
{name="sx", type="number", doc="scale factor in the X direction"},
{name="sy", type="number", doc="scale factor in the Y direction"},
call =
function(self, sx, sy)
cairo.C.cairo_matrix_scale(self.C, sx, sy)
end
}
Matrix.rotate = argcheck{
doc = [[
<a name="Matrix.rotate">
#### Matrix.rotate(@ARGP)
@ARGT
Applies rotation by `radians` to the transformation in
`matrix`. The effect of the new transformation is to first rotate the
coordinates by `radians`, then apply the original transformation
to the coordinates.
]],
{name="self", type="cairo.Matrix"},
{name="radians", type="number", doc="angle of rotation, in radians. The direction of rotation is defined such that positive angles rotate in the direction from the positive X axis toward the positive Y axis. With the default axis orientation of cairo, positive angles rotate in a clockwise direction."},
call =
function(self, radians)
cairo.C.cairo_matrix_rotate(self.C, radians)
end
}
Matrix.invert = argcheck{
doc = [[
<a name="Matrix.invert">
#### Matrix.invert(@ARGP)
@ARGT
Changes `matrix` to be the inverse of its original value. Not
all transformation matrices have inverses; if the matrix
collapses points together (it is degenerate),
then it has no inverse and this function will fail.
_Returns_: If `matrix` has an inverse, modifies `matrix` to
be the inverse matrix and returns [`"success"`](enums.md#Status). Otherwise,
returns [`"invalid-matrix"`](enums.md#Status).
]],
{name="self", type="cairo.Matrix"},
call =
function(self)
return cairo.C.cairo_matrix_invert(self.C)
end
}
Matrix.multiply = argcheck{
doc = [[
<a name="Matrix.multiply">
#### Matrix.multiply(@ARGP)
@ARGT
Multiplies the affine transformations in `a` and `b` together
and stores the result in `result`. The effect of the resulting
transformation is to first apply the transformation in `a` to the
coordinates and then apply the transformation in `b` to the
coordinates.
It is allowable for `result` to be identical to either `a` or `b`.
]],
{name="self", type="cairo.Matrix"},
{name="a", type="cairo.Matrix", doc="a [`Matrix`](#Matrix)"},
{name="b", type="cairo.Matrix", doc="a [`Matrix`](#Matrix)"},
call =
function(self, a, b)
cairo.C.cairo_matrix_multiply(self.C, a.C, b.C)
end
}
Matrix.transformDistance = argcheck{
doc = [[
<a name="Matrix.transformDistance">
#### Matrix.transformDistance(@ARGP)
@ARGT
Transforms the distance vector (`dx`,`dy`) by `matrix`. This is
similar to [`Matrix.transformPoint()`](#Matrix.transformPoint) except that the translation
components of the transformation are ignored. The calculation of
the returned vector is as follows:
```lua
dx2 = dx1 * a + dy1 * c;
dy2 = dx1 * b + dy1 * d;
```
Affine transformations are position invariant, so the same vector
always transforms to the same vector. If (`x1`,`y1`) transforms
to (`x2`,`y2`) then (`x1`+`dx1`,`y1`+`dy1`) will transform to
(`x1`+`dx2`,`y1`+`dy2`) for all values of `x1` and `x2`.
]],
{name="self", type="cairo.Matrix"},
{name="dx", type="number", doc="X component of a distance vector. An in/out parameter"},
{name="dy", type="number", doc="Y component of a distance vector. An in/out parameter"},
call =
function(self, dx, dy)
local dx_p = ffi.new('double[1]', dx)
local dy_p = ffi.new('double[1]', dy)
cairo.C.cairo_matrix_transform_distance(self.C, dx_p, dy_p)
return dx_p[0], dy_p[0]
end
}
Matrix.transformPoint = argcheck{
doc = [[
<a name="Matrix.transformPoint">
#### Matrix.transformPoint(@ARGP)
@ARGT
Transforms the point (`x`, `y`) by `matrix`.
]],
{name="self", type="cairo.Matrix"},
{name="x", type="number", doc="X position. An in/out parameter"},
{name="y", type="number", doc="Y position. An in/out parameter"},
call =
function(self, x, y)
local x_p = ffi.new('double[1]', x)
local y_p = ffi.new('double[1]', y)
cairo.C.cairo_matrix_transform_point(self.C, x_p, y_p)
return x_p[0], y_p[0]
end
}