-
Notifications
You must be signed in to change notification settings - Fork 138
/
dmtxmatrix3.c
355 lines (327 loc) · 7.89 KB
/
dmtxmatrix3.c
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
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2008, 2009 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtxmatrix3.c
* \brief 2D Matrix (3x3) math
*/
/**
* \brief Copy matrix contents
* \param m0 Copy target
* \param m1 Copy source
* \return void
*/
extern void
dmtxMatrix3Copy(DmtxMatrix3 m0, DmtxMatrix3 m1)
{
memcpy(m0, m1, sizeof(DmtxMatrix3));
}
/**
* \brief Generate identity transformation matrix
* \param m Generated matrix
* \return void
*
* | 1 0 0 |
* m = | 0 1 0 |
* | 0 0 1 |
*
* Transform "m"
* (doesn't change anything)
* |\
* (0,1) x----o +--+ \ (0,1) x----o
* | | | \ | |
* | | | / | |
* +----* +--+ / +----*
* (0,0) (1,0) |/ (0,0) (1,0)
*
*/
extern void
dmtxMatrix3Identity(DmtxMatrix3 m)
{
static DmtxMatrix3 tmp = { {1, 0, 0},
{0, 1, 0},
{0, 0, 1} };
dmtxMatrix3Copy(m, tmp);
}
/**
* \brief Generate translate transformation matrix
* \param m Generated matrix
* \param tx
* \param ty
* \return void
*
* | 1 0 0 |
* m = | 0 1 0 |
* | tx ty 1 |
*
* Transform "m"
* _____ (tx,1+ty) x----o (1+tx,1+ty)
* \ | | |
* (0,1) x----o / | (0,1) +-|--+ |
* | | / /\| | +----* (1+tx,ty)
* | | \ / | |
* +----* ` +----+
* (0,0) (1,0) (0,0) (1,0)
*
*/
void dmtxMatrix3Translate(DmtxMatrix3 m, double tx, double ty)
{
dmtxMatrix3Identity(m);
m[2][0] = tx;
m[2][1] = ty;
}
/**
* \brief Generate rotate transformation
* \param m Generated matrix
* \param angle
* \return void
*
* | cos(a) sin(a) 0 |
* m = | -sin(a) cos(a) 0 |
* | 0 0 1 |
* o
* Transform "m" / `
* ___ / `
* (0,1) x----o |/ \ x * (cos(a),sin(a))
* | | '-- | ` /
* | | ___/ ` / a
* +----* `+ - - - - - -
* (0,0) (1,0) (0,0)
*
*/
extern void
dmtxMatrix3Rotate(DmtxMatrix3 m, double angle)
{
double sinAngle, cosAngle;
sinAngle = sin(angle);
cosAngle = cos(angle);
dmtxMatrix3Identity(m);
m[0][0] = cosAngle;
m[0][1] = sinAngle;
m[1][0] = -sinAngle;
m[1][1] = cosAngle;
}
/**
* \brief Generate scale transformation matrix
* \param m Generated matrix
* \param sx
* \param sy
* \return void
*
* | sx 0 0 |
* m = | 0 sy 0 |
* | 0 0 1 |
*
* Transform "m"
* _____ (0,sy) x-------o (sx,sy)
* \ | | |
* (0,1) x----o / | (0,1) +----+ |
* | | / /\| | | |
* | | \ / | | |
* +----* ` +----+--*
* (0,0) (1,0) (0,0) (sx,0)
*
*/
extern void
dmtxMatrix3Scale(DmtxMatrix3 m, double sx, double sy)
{
dmtxMatrix3Identity(m);
m[0][0] = sx;
m[1][1] = sy;
}
/**
* \brief Generate shear transformation matrix
* \param m Generated matrix
* \param shx
* \param shy
* \return void
*
* | 0 shy 0 |
* m = | shx 0 0 |
* | 0 0 1 |
*/
extern void
dmtxMatrix3Shear(DmtxMatrix3 m, double shx, double shy)
{
dmtxMatrix3Identity(m);
m[1][0] = shx;
m[0][1] = shy;
}
/**
* \brief Generate top line skew transformation
* \param m
* \param b0
* \param b1
* \param sz
* \return void
*
* | b1/b0 0 (b1-b0)/(sz*b0) |
* m = | 0 sz/b0 0 |
* | 0 0 1 |
*
* (sz,b1) o
* /| Transform "m"
* / |
* / | +--+
* / | | |
* (0,b0) x | | |
* | | +-+ +-+
* (0,sz) +----+ \ / (0,sz) x----o
* | | \ / | |
* | | \/ | |
* +----+ +----+
* (0,0) (sz,0) (0,0) (sz,0)
*
*/
extern void
dmtxMatrix3LineSkewTop(DmtxMatrix3 m, double b0, double b1, double sz)
{
assert(b0 >= DmtxAlmostZero);
dmtxMatrix3Identity(m);
m[0][0] = b1/b0;
m[1][1] = sz/b0;
m[0][2] = (b1 - b0)/(sz*b0);
}
/**
* \brief Generate top line skew transformation (inverse)
* \param m
* \param b0
* \param b1
* \param sz
* \return void
*/
extern void
dmtxMatrix3LineSkewTopInv(DmtxMatrix3 m, double b0, double b1, double sz)
{
assert(b1 >= DmtxAlmostZero);
dmtxMatrix3Identity(m);
m[0][0] = b0/b1;
m[1][1] = b0/sz;
m[0][2] = (b0 - b1)/(sz*b1);
}
/**
* \brief Generate side line skew transformation
* \param m
* \param b0
* \param b1
* \param sz
* \return void
*/
extern void
dmtxMatrix3LineSkewSide(DmtxMatrix3 m, double b0, double b1, double sz)
{
assert(b0 >= DmtxAlmostZero);
dmtxMatrix3Identity(m);
m[0][0] = sz/b0;
m[1][1] = b1/b0;
m[1][2] = (b1 - b0)/(sz*b0);
}
/**
* \brief Generate side line skew transformation (inverse)
* \param m
* \param b0
* \param b1
* \param sz
* \return void
*/
extern void
dmtxMatrix3LineSkewSideInv(DmtxMatrix3 m, double b0, double b1, double sz)
{
assert(b1 >= DmtxAlmostZero);
dmtxMatrix3Identity(m);
m[0][0] = b0/sz;
m[1][1] = b0/b1;
m[1][2] = (b0 - b1)/(sz*b1);
}
/**
* \brief Multiply two matrices to create a third
* \param mOut
* \param m0
* \param m1
* \return void
*/
extern void
dmtxMatrix3Multiply(DmtxMatrix3 mOut, DmtxMatrix3 m0, DmtxMatrix3 m1)
{
int i, j, k;
double val;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
val = 0.0;
for(k = 0; k < 3; k++) {
val += m0[i][k] * m1[k][j];
}
mOut[i][j] = val;
}
}
}
/**
* \brief Multiply two matrices in place
* \param m0
* \param m1
* \return void
*/
extern void
dmtxMatrix3MultiplyBy(DmtxMatrix3 m0, DmtxMatrix3 m1)
{
DmtxMatrix3 mTmp;
dmtxMatrix3Copy(mTmp, m0);
dmtxMatrix3Multiply(m0, mTmp, m1);
}
/**
* \brief Multiply vector and matrix
* \param vOut Vector (output)
* \param vIn Vector (input)
* \param m Matrix to be multiplied
* \return DmtxPass | DmtxFail
*/
extern int
dmtxMatrix3VMultiply(DmtxVector2 *vOut, DmtxVector2 *vIn, DmtxMatrix3 m)
{
double w;
w = vIn->X*m[0][2] + vIn->Y*m[1][2] + m[2][2];
if(fabs(w) <= DmtxAlmostZero) {
vOut->X = FLT_MAX;
vOut->Y = FLT_MAX;
return DmtxFail;
}
vOut->X = (vIn->X*m[0][0] + vIn->Y*m[1][0] + m[2][0])/w;
vOut->Y = (vIn->X*m[0][1] + vIn->Y*m[1][1] + m[2][1])/w;
return DmtxPass;
}
/**
* \brief Multiply vector and matrix in place
* \param v Vector (input and output)
* \param m Matrix to be multiplied
* \return DmtxPass | DmtxFail
*/
extern int
dmtxMatrix3VMultiplyBy(DmtxVector2 *v, DmtxMatrix3 m)
{
int success;
DmtxVector2 vOut;
success = dmtxMatrix3VMultiply(&vOut, v, m);
*v = vOut;
return success;
}
/**
* \brief Print matrix contents to STDOUT
* \param m
* \return void
*/
extern void
dmtxMatrix3Print(DmtxMatrix3 m)
{
fprintf(stdout, "%8.8f\t%8.8f\t%8.8f\n", m[0][0], m[0][1], m[0][2]);
fprintf(stdout, "%8.8f\t%8.8f\t%8.8f\n", m[1][0], m[1][1], m[1][2]);
fprintf(stdout, "%8.8f\t%8.8f\t%8.8f\n", m[2][0], m[2][1], m[2][2]);
fprintf(stdout, "\n");
}