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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
/*
* Transfo.c -- Implementation of transformation routines.
*
* Authors : Patrick Lecoanet.
* Creation date :
*
* $Id$
*/
/*
* Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet --
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this code; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* This package deals with *AFFINE* 3x3 matrices.
* This means that you should not try to feed it with matrices
* containing perspective changes. It is assumed that the third
* column is always [0 0 1] as this is the case for affine matrices.
* Furthermore affine matrices are known to be invertible (non singular).
* Despite this, various tests are done to test the invertibility because
* of numerical precision or limit.
* Any of the operations in this module yield an affine matrix. Composition
* of two affine matrices and inversion of an affine matrix result in an
* affine matrix (Affine matrices Group property). Rotation, translation
* anamorphic scaling, xy shear and yx shear also preserve the property.
*
*/
#include "Item.h"
#include "Geo.h"
#include "Transfo.h"
#include "Types.h"
#include <stdlib.h>
static const char rcsid[] = "$Imagine: Transfo.c,v 1.7 1997/01/24 14:33:37 lecoanet Exp $";
static const char compile_id[]="$Compile: " __FILE__ " " __DATE__ " " __TIME__ " $";
/*
*************************************************************************
*
* The transformation primitives are based on affines matrices retricted
* to the following pattern:
*
* x x 0
* x x 0
* x x 1
*
* It is necessary to only feed such matrices to these primitives as they
* do optimizations based on their properties. Furthermore the package
* stores only the first two columns, the third is constant. There is no
* way to describe perspective transformation with these transformation
* matrices.
*
*************************************************************************
*/
/*
*************************************************************************
*
* ZnTransfoNew --
* Create a new transformation and return it initialized to
* identity.
*
*************************************************************************
*/
ZnTransfo *
ZnTransfoNew()
{
ZnTransfo *t;
t = (ZnTransfo *) ZnMalloc(sizeof(ZnTransfo));
ZnTransfoSetIdentity(t);
return t;
}
/*
*************************************************************************
*
* ZnTransfoDuplicate --
* Create a new transformation identical to the model t.
*
*************************************************************************
*/
ZnTransfo *
ZnTransfoDuplicate(ZnTransfo *t)
{
ZnTransfo *nt;
nt = (ZnTransfo *) ZnMalloc(sizeof(ZnTransfo));
if (t) {
*nt = *t;
}
else {
ZnTransfoSetIdentity(nt);
}
return nt;
}
/*
*************************************************************************
*
* ZnTransfoFree --
* Delete a transformation and free its memory.
*
*************************************************************************
*/
void
ZnTransfoFree(ZnTransfo *t)
{
ZnFree(t);
}
/*
*************************************************************************
*
* ZnPrintTransfo --
* Print the transfo matrix on stdout.
*
*************************************************************************
*/
void
ZnPrintTransfo(ZnTransfo *t)
{
printf("(%5g %5g\n %5g %5g\n %5g %5g)\n",
t->_[0][0], t->_[0][1],
t->_[1][0], t->_[1][1],
t->_[2][0], t->_[2][1]);
}
/*
*************************************************************************
*
* ZnTransfoIsIdentity --
* Tell if the given transfo is (close to) identity.
*
*************************************************************************
*/
ZnBool
ZnTransfoIsIdentity(ZnTransfo *t)
{
ZnReal tmp;
ZnBool res = False;
tmp = t->_[0][0] - 1.0;
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
tmp = t->_[1][1] - 1.0;
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
tmp = t->_[0][1];
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
tmp = t->_[1][0];
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
tmp = t->_[2][0];
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
tmp = t->_[2][1];
res = res & ((tmp < PRECISION_LIMIT) && (tmp > -PRECISION_LIMIT));
return res;
}
/*
*************************************************************************
*
* ZnTransfoSetIdentity --
* Initialize the given transfo to identity.
*
*************************************************************************
*/
void
ZnTransfoSetIdentity(ZnTransfo *t)
{
*t = ((ZnTransfo) {{{1, 0}, {0, 1}, {0, 0}}});
}
/*
*************************************************************************
*
* ZnTransfoCompose --
* Combine two transformations t1 and t2 by post-concatenation.
* Returns the resulting transformation.
* t2 can be NULL, meaning identity transform. This is used in
* the toolkit to optimize some cases.
*
* All the parameters must be distincts transforms.
*
*************************************************************************
*/
ZnTransfo *
ZnTransfoCompose(ZnTransfo *res,
ZnTransfo *t1,
ZnTransfo *t2)
{
if ((t1 != NULL) && (t2 != NULL)) {
register ZnReal tmp;
tmp = t1->_[0][0];
res->_[0][0] = tmp*t2->_[0][0] + t1->_[0][1]*t2->_[1][0];
res->_[0][1] = tmp*t2->_[0][1] + t1->_[0][1]*t2->_[1][1];
tmp = t1->_[1][0];
res->_[1][0] = tmp*t2->_[0][0] + t1->_[1][1]*t2->_[1][0];
res->_[1][1] = tmp*t2->_[0][1] + t1->_[1][1]*t2->_[1][1];
tmp = t1->_[2][0];
res->_[2][0] = tmp*t2->_[0][0] + t1->_[2][1]*t2->_[1][0] + t2->_[2][0];
res->_[2][1] = tmp*t2->_[0][1] + t1->_[2][1]*t2->_[1][1] + t2->_[2][1];
}
else if (t1 == NULL) {
if (res != t2) {
*res = *t2;
}
}
else if (t2 == NULL) {
if (res != t2) {
*res = *t1;
}
}
else {
ZnTransfoSetIdentity(res);
}
return res;
}
/*
*************************************************************************
*
* ZnTransfoInvert --
* Compute the inverse of the given matrix and return it. This
* function makes the assumption that the matrix is affine to
* optimize the job. Do not give it a general matrix, this will
* fail. This code is from Graphics Gems II. Anyway an affine
* matrix is always invertible for affine matrices form a sub
* group of the non-singular matrices.
*
*************************************************************************
*/
ZnTransfo *
ZnTransfoInvert(ZnTransfo *t,
ZnTransfo *inv)
{
ZnReal pos, neg, temp, det_l;
if (t == NULL) {
return NULL;
}
/*
* Compute the determinant of the upper left 2x2 sub matrix to see
* if it is singular.
*/
pos = neg = 0.0;
temp = t->_[0][0] * t->_[1][1];
if (temp >= 0.0) {
pos += temp;
}
else {
neg += temp;
}
temp = - t->_[0][1] * t->_[1][0];
if (temp >= 0.0) {
pos += temp;
}
else {
neg += temp;
}
det_l = pos + neg;
temp = det_l / (pos - neg); /* Why divide by (pos - neg) ?? */
if (ABS(temp) < PRECISION_LIMIT) {
ZnWarning("ZnTransfoInvert : singular matrix\n");
return NULL;
}
det_l = 1.0/ det_l;
inv->_[0][0] = t->_[1][1] * det_l;
inv->_[0][1] = - t->_[0][1] * det_l;
inv->_[1][0] = - t->_[1][0] * det_l;
inv->_[1][1] = t->_[0][0] * det_l;
/*
* The code below is equivalent to:
* inv->_[2][0] = (t->_[1][0] * t->_[2][1] - t->_[1][1] * t->_[2][0]) * det_l;
* inv->_[2][1] = - (t->_[0][0] * t->_[2][1] - t->_[0][1] * t->_[2][0]) * det_l;
*
* with some operations factored (already computed) to increase speed.
*/
inv->_[2][0] = - (inv->_[0][0] * t->_[2][0] + inv->_[1][0] * t->_[2][1]);
inv->_[2][1] = - (inv->_[0][1] * t->_[2][0] + inv->_[1][1] * t->_[2][1]);
return inv;
}
/*
*************************************************************************
*
* ZnTransfoDecompose --
* Decompose an affine matrix into translation, scale, shear and
* rotation. The different values are stored in the locations
* pointed to by the pointer parameters. If some values are not
* needed a NULL pointer can be given instead. The resulting shear
* shears x coordinate when y change.
* This code is taken from Graphics Gems II.
*
*************************************************************************
*/
void
ZnTransfoDecompose(ZnTransfo *t,
ZnPoint *scale,
ZnPoint *trans,
ZnReal *rotation,
ZnReal *shearxy)
{
ZnTransfo local;
ZnReal shear, len, rot, det;
if (t == NULL) {
/* Identity transform */
if (scale) {
scale->x = 1.0;
scale->y = 1.0;
}
if (trans) {
trans->x = 0.0;
trans->y = 0.0;
}
if (rotation) {
*rotation = 0.0;
}
if (shearxy) {
*shearxy = 0.0;
}
//printf("Transfo is identity\n");
return;
}
det = (t->_[0][0]*t->_[1][1] - t->_[0][1]*t->_[1][0]);
if (ABS(det) < PRECISION_LIMIT) {
ZnWarning("ZnTransfoDecompose : singular matrix\n");
return;
}
local = *t;
//ZnPrintTransfo(&local);
/* Get translation part if needed */
if (trans) {
trans->x = ABS(local._[2][0]) < PRECISION_LIMIT ? 0 : local._[2][0];
trans->y = ABS(local._[2][1]) < PRECISION_LIMIT ? 0 : local._[2][1];
}
if (!scale && !shearxy && !rotation) {
return;
}
/* Get scale and shear */
len = sqrt(local._[0][0]*local._[0][0] +
local._[0][1]*local._[0][1]); /* Get x scale from 1st row */
if (scale) {
scale->x = len < PRECISION_LIMIT ? 0.0 : len;
}
local._[0][0] /= len; /* Normalize 1st row */
local._[0][1] /= len;
shear = (local._[0][0]*local._[1][0] +
local._[0][1]*local._[1][1]); /* Shear is dot product of 1st row & 2nd row */
/* Make the 2nd row orthogonal to the 1st row
* by linear combinaison:
* row1.x = row1.x + row0.x*-shear &
* row1.y = row1.y + row0.y*-shear
*/
local._[1][0] -= local._[0][0]*shear;
local._[1][1] -= local._[0][1]*shear;
len = sqrt(local._[1][0]*local._[1][0] +
local._[1][1]*local._[1][1]); /* Get y scale from 2nd row */
if (scale)
scale->y = len < PRECISION_LIMIT ? 0.0 : len;
local._[1][0] /= len; /* Normalize 2nd row */
local._[1][1] /= len;
shear /= len;
if (shearxy) {
*shearxy = ABS(shear) < PRECISION_LIMIT ? 0.0 : shear;
//printf("shear %f\n", *shearxy);
}
//printf("Matrix after scale & shear extracted\n");
//ZnPrintTransfo(&local);
/* Get rotation */
/* Check for a coordinate system flip. If det of upper-left 2x2
* is -1, there is a reflection. If the rotation is < 180° negate
* the y scale. If the rotation is > 180° then negate the x scale
* and report a rotation between 0 and 180°. This dissymetry is
* the result of computing (z) rotation from the first row (x component
* of the axis system basis).
*/
det = (local._[0][0]*local._[1][1]- local._[0][1]*local._[1][0]);
rot = atan2(local._[0][1], local._[0][0]);
if (rot < 0) {
rot = 2*M_PI+rot;
}
rot = rot < PRECISION_LIMIT ? 0.0 : rot;
if (rot >= M_PI) {
/*rot -= M_PI; Why that, I'll have to check Graphic Gems ??? */
if (scale && det < 0) {
scale->x *= -1;
}
}
else if (scale && det < 0) {
scale->y *= -1;
}
//printf("scalex %f\n", scale->x);
//printf("scaley %f\n", scale->y);
//printf("rotation %f\n", rot*180.0/3.1415);
if (rotation) {
*rotation = rot;
}
}
/*
*************************************************************************
*
* ZnTransfoEqual --
* Return True if t1 and t2 are equal (i.e they have the same
* rotation, shear scales and translations). If include_translation
* is True the translations are considered in the test.
*
*************************************************************************
*/
ZnBool
ZnTransfoEqual(ZnTransfo *t1,
ZnTransfo *t2,
ZnBool include_translation)
{
if (include_translation) {
return (t1->_[0][0] == t2->_[0][0] &&
t1->_[0][1] == t2->_[0][1] &&
t1->_[1][0] == t2->_[1][0] &&
t1->_[1][1] == t2->_[1][1] &&
t1->_[2][0] == t2->_[2][0] &&
t1->_[2][1] == t2->_[2][1]);
}
else {
return (t1->_[0][0] == t2->_[0][0] &&
t1->_[0][1] == t2->_[0][1] &&
t1->_[1][0] == t2->_[1][0] &&
t1->_[1][1] == t2->_[1][1]);
}
}
/*
*************************************************************************
*
* ZnTransfoHasShear --
* Return True if t has a shear factor in x or y or describe a
* rotation or both.
*
*************************************************************************
*/
ZnBool
ZnTransfoHasShear(ZnTransfo *t)
{
return t->_[0][1] != 0.0 || t->_[1][0] != 0.0;
}
/*
*************************************************************************
*
* ZnTransfoIsTranslation --
* Return True if t is a pure translation.
*
*************************************************************************
*/
ZnBool
ZnTransfoIsTranslation(ZnTransfo *t)
{
return (t->_[0][0] == 0.0 &&
t->_[0][1] == 0.0 &&
t->_[1][0] == 0.0 &&
t->_[1][1] == 0.0);
}
/*
*************************************************************************
*
* ZnTransformPoint --
* Apply the transformation to the point. The point is
* modified and returned as the value of the function.
* A NULL transformation means identity. This is only used
* in the toolkit to optimize some cases. It should never
* happen in user code.
*
*************************************************************************
*/
ZnPoint *
ZnTransformPoint(ZnTransfo *t,
register ZnPoint *p,
ZnPoint *xp)
{
if (t == NULL) {
xp->x = p->x;
xp->y = p->y;
}
else {
xp->x = t->_[0][0]*p->x + t->_[1][0]*p->y + t->_[2][0];
xp->y = t->_[0][1]*p->x + t->_[1][1]*p->y + t->_[2][1];
}
return xp;
}
/*
*************************************************************************
*
* ZnTransformPoints --
* Apply the transformation to the points in p returning points in xp.
* The number of points is in num.
* A NULL transformation means identity. This is only used
* in the toolkit to optimize some cases. It should never
* happen in user code.
*
*************************************************************************
*/
void
ZnTransformPoints(ZnTransfo *t,
ZnPoint *p,
ZnPoint *xp,
int num)
{
if (t == NULL) {
memcpy(xp, p, sizeof(ZnPoint)*num);
}
else {
int i;
for (i = 0; i < num; i++) {
xp[i].x = t->_[0][0]*p[i].x + t->_[1][0]*p[i].y + t->_[2][0];
xp[i].y = t->_[0][1]*p[i].x + t->_[1][1]*p[i].y + t->_[2][1];
}
}
}
/*
*************************************************************************
*
* ZnTranslate --
* Translate the given transformation by delta_x, delta_y. Returns
* the resulting transformation.
*
* ZnSetTranslation --
* Set the translation instead of combining it into the
* transformation.
*
*************************************************************************
*/
ZnTransfo *
ZnTranslate(ZnTransfo *t,
ZnReal delta_x,
ZnReal delta_y)
{
t->_[2][0] = t->_[2][0] + delta_x;
t->_[2][1] = t->_[2][1] + delta_y;
return t;
}
ZnTransfo *
ZnSetTranslation(ZnTransfo *t,
ZnReal delta_x,
ZnReal delta_y)
{
t->_[2][0] = delta_x;
t->_[2][1] = delta_y;
return t;
}
/*
*************************************************************************
*
* ZnScale --
* Scale the given transformation by scale_x, scale_y. Returns the
* resulting transformation.
*
*************************************************************************
*/
ZnTransfo *
ZnScale(ZnTransfo *t,
register ZnReal scale_x,
register ZnReal scale_y)
{
t->_[0][0] = t->_[0][0]*scale_x;
t->_[0][1] = t->_[0][1]*scale_y;
t->_[1][0] = t->_[1][0]*scale_x;
t->_[1][1] = t->_[1][1]*scale_y;
t->_[2][0] = t->_[2][0]*scale_x;
t->_[2][1] = t->_[2][1]*scale_y;
return t;
}
/*
*************************************************************************
*
* ZnRotateRad --
* Rotate the given transformation by angle radians
* counter-clockwise around the origin. Returns the resulting
* transformation.
*
*************************************************************************
*/
ZnTransfo *
ZnRotateRad(ZnTransfo *t,
ZnReal angle)
{
register ZnReal c = cos(angle);
register ZnReal s = sin(angle);
register ZnReal tmp;
tmp = t->_[0][0];
t->_[0][0] = tmp*c - t->_[0][1]*s;
t->_[0][1] = tmp*s + t->_[0][1]*c;
tmp = t->_[1][0];
t->_[1][0] = tmp*c - t->_[1][1]*s;
t->_[1][1] = tmp*s + t->_[1][1]*c;
tmp = t->_[2][0];
t->_[2][0] = tmp*c - t->_[2][1]*s;
t->_[2][1] = tmp*s + t->_[2][1]*c;
return t;
}
/*
*************************************************************************
*
* ZnRotateDeg --
* Rotate the given transformation by angle degrees
* counter-clockwise around the origin. Returns the resulting
* transformation.
*
*************************************************************************
*/
ZnTransfo *
ZnRotateDeg(ZnTransfo *t,
ZnReal angle)
{
return ZnRotateRad(t, DegreesToRadian(angle));
}
#undef PRECISION_LIMIT
|