1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 /* Simple manipulations with affine transformations */
22 #include <math.h>
23 #include <stdio.h> /* for sprintf */
24 #include <string.h> /* for strcpy */
25 #include "art_misc.h"
26 #include "art_point.h"
27 #include "art_affine.h"
30 /* According to a strict interpretation of the libart structure, this
31 routine should go into its own module, art_point_affine. However,
32 it's only two lines of code, and it can be argued that it is one of
33 the natural basic functions of an affine transformation.
34 */
36 /**
37 * art_affine_point: Do an affine transformation of a point.
38 * @dst: Where the result point is stored.
39 * @src: The original point.
40 @ @affine: The affine transformation.
41 **/
42 void
43 art_affine_point (ArtPoint *dst, const ArtPoint *src,
44 const double affine[6])
45 {
46 double x, y;
48 x = src->x;
49 y = src->y;
50 dst->x = x * affine[0] + y * affine[2] + affine[4];
51 dst->y = x * affine[1] + y * affine[3] + affine[5];
52 }
54 /**
55 * art_affine_invert: Find the inverse of an affine transformation.
56 * @dst: Where the resulting affine is stored.
57 * @src: The original affine transformation.
58 *
59 * All non-degenerate affine transforms are invertible. If the original
60 * affine is degenerate or nearly so, expect numerical instability and
61 * very likely core dumps on Alpha and other fp-picky architectures.
62 * Otherwise, @dst multiplied with @src, or @src multiplied with @dst
63 * will be (to within roundoff error) the identity affine.
64 **/
65 void
66 art_affine_invert (double dst[6], const double src[6])
67 {
68 double r_det;
70 r_det = 1.0 / (src[0] * src[3] - src[1] * src[2]);
71 dst[0] = src[3] * r_det;
72 dst[1] = -src[1] * r_det;
73 dst[2] = -src[2] * r_det;
74 dst[3] = src[0] * r_det;
75 dst[4] = -src[4] * dst[0] - src[5] * dst[2];
76 dst[5] = -src[4] * dst[1] - src[5] * dst[3];
77 }
79 /**
80 * art_affine_flip: Flip an affine transformation horizontally and/or vertically.
81 * @dst_affine: Where the resulting affine is stored.
82 * @src_affine: The original affine transformation.
83 * @horiz: Whether or not to flip horizontally.
84 * @vert: Whether or not to flip horizontally.
85 *
86 * Flips the affine transform. FALSE for both @horiz and @vert implements
87 * a simple copy operation. TRUE for both @horiz and @vert is a
88 * 180 degree rotation. It is ok for @src_affine and @dst_affine to
89 * be equal pointers.
90 **/
91 void
92 art_affine_flip (double dst_affine[6], const double src_affine[6], int horz, int vert)
93 {
94 dst_affine[0] = horz ? - src_affine[0] : src_affine[0];
95 dst_affine[1] = horz ? - src_affine[1] : src_affine[1];
96 dst_affine[2] = vert ? - src_affine[2] : src_affine[2];
97 dst_affine[3] = vert ? - src_affine[3] : src_affine[3];
98 dst_affine[4] = horz ? - src_affine[4] : src_affine[4];
99 dst_affine[5] = vert ? - src_affine[5] : src_affine[5];
100 }
102 #define EPSILON 1e-6
104 /* It's ridiculous I have to write this myself. This is hardcoded to
105 six digits of precision, which is good enough for PostScript.
107 The return value is the number of characters (i.e. strlen (str)).
108 It is no more than 12. */
109 static int
110 art_ftoa (char str[80], double x)
111 {
112 char *p = str;
113 int i, j;
115 p = str;
116 if (fabs (x) < EPSILON / 2)
117 {
118 strcpy (str, "0");
119 return 1;
120 }
121 if (x < 0)
122 {
123 *p++ = '-';
124 x = -x;
125 }
126 if ((int)floor ((x + EPSILON / 2) < 1))
127 {
128 *p++ = '0';
129 *p++ = '.';
130 i = sprintf (p, "%06d", (int)floor ((x + EPSILON / 2) * 1e6));
131 while (i && p[i - 1] == '0')
132 i--;
133 if (i == 0)
134 i--;
135 p += i;
136 }
137 else if (x < 1e6)
138 {
139 i = sprintf (p, "%d", (int)floor (x + EPSILON / 2));
140 p += i;
141 if (i < 6)
142 {
143 int ix;
145 *p++ = '.';
146 x -= floor (x + EPSILON / 2);
147 for (j = i; j < 6; j++)
148 x *= 10;
149 ix = floor (x + 0.5);
151 for (j = 0; j < i; j++)
152 ix *= 10;
154 /* A cheap hack, this routine can round wrong for fractions
155 near one. */
156 if (ix == 1000000)
157 ix = 999999;
159 sprintf (p, "%06d", ix);
160 i = 6 - i;
161 while (i && p[i - 1] == '0')
162 i--;
163 if (i == 0)
164 i--;
165 p += i;
166 }
167 }
168 else
169 p += sprintf (p, "%g", x);
171 *p = '\0';
172 return p - str;
173 }
177 #include <stdlib.h>
178 /**
179 * art_affine_to_string: Convert affine transformation to concise PostScript string representation.
180 * @str: Where to store the resulting string.
181 * @src: The affine transform.
182 *
183 * Converts an affine transform into a bit of PostScript code that
184 * implements the transform. Special cases of scaling, rotation, and
185 * translation are detected, and the corresponding PostScript
186 * operators used (this greatly aids understanding the output
187 * generated). The identity transform is mapped to the null string.
188 **/
189 void
190 art_affine_to_string (char str[128], const double src[6])
191 {
192 char tmp[80];
193 int i, ix;
195 #if 0
196 for (i = 0; i < 1000; i++)
197 {
198 double d = rand () * .1 / RAND_MAX;
199 art_ftoa (tmp, d);
200 printf ("%g %f %s\n", d, d, tmp);
201 }
202 #endif
203 if (fabs (src[4]) < EPSILON && fabs (src[5]) < EPSILON)
204 {
205 /* could be scale or rotate */
206 if (fabs (src[1]) < EPSILON && fabs (src[2]) < EPSILON)
207 {
208 /* scale */
209 if (fabs (src[0] - 1) < EPSILON && fabs (src[3] - 1) < EPSILON)
210 {
211 /* identity transform */
212 str[0] = '\0';
213 return;
214 }
215 else
216 {
217 ix = 0;
218 ix += art_ftoa (str + ix, src[0]);
219 str[ix++] = ' ';
220 ix += art_ftoa (str + ix, src[3]);
221 strcpy (str + ix, " scale");
222 return;
223 }
224 }
225 else
226 {
227 /* could be rotate */
228 if (fabs (src[0] - src[3]) < EPSILON &&
229 fabs (src[1] + src[2]) < EPSILON &&
230 fabs (src[0] * src[0] + src[1] * src[1] - 1) < 2 * EPSILON)
231 {
232 double theta;
234 theta = (180 / M_PI) * atan2 (src[1], src[0]);
235 art_ftoa (tmp, theta);
236 sprintf (str, "%s rotate", tmp);
237 return;
238 }
239 }
240 }
241 else
242 {
243 /* could be translate */
244 if (fabs (src[0] - 1) < EPSILON && fabs (src[1]) < EPSILON &&
245 fabs (src[2]) < EPSILON && fabs (src[3] - 1) < EPSILON)
246 {
247 ix = 0;
248 ix += art_ftoa (str + ix, src[4]);
249 str[ix++] = ' ';
250 ix += art_ftoa (str + ix, src[5]);
251 strcpy (str + ix, " translate");
252 return;
253 }
254 }
256 ix = 0;
257 str[ix++] = '[';
258 str[ix++] = ' ';
259 for (i = 0; i < 6; i++)
260 {
261 ix += art_ftoa (str + ix, src[i]);
262 str[ix++] = ' ';
263 }
264 strcpy (str + ix, "] concat");
265 }
267 /**
268 * art_affine_multiply: Multiply two affine transformation matrices.
269 * @dst: Where to store the result.
270 * @src1: The first affine transform to multiply.
271 * @src2: The second affine transform to multiply.
272 *
273 * Multiplies two affine transforms together, i.e. the resulting @dst
274 * is equivalent to doing first @src1 then @src2. Note that the
275 * PostScript concat operator multiplies on the left, i.e. "M concat"
276 * is equivalent to "CTM = multiply (M, CTM)";
277 *
278 * It is safe to call this function with @dst equal to @src1 or @src2.
279 **/
280 void
281 art_affine_multiply (double dst[6], const double src1[6], const double src2[6])
282 {
283 double d0, d1, d2, d3, d4, d5;
285 d0 = src1[0] * src2[0] + src1[1] * src2[2];
286 d1 = src1[0] * src2[1] + src1[1] * src2[3];
287 d2 = src1[2] * src2[0] + src1[3] * src2[2];
288 d3 = src1[2] * src2[1] + src1[3] * src2[3];
289 d4 = src1[4] * src2[0] + src1[5] * src2[2] + src2[4];
290 d5 = src1[4] * src2[1] + src1[5] * src2[3] + src2[5];
291 dst[0] = d0;
292 dst[1] = d1;
293 dst[2] = d2;
294 dst[3] = d3;
295 dst[4] = d4;
296 dst[5] = d5;
297 }
299 /**
300 * art_affine_identity: Set up the identity matrix.
301 * @dst: Where to store the resulting affine transform.
302 *
303 * Sets up an identity matrix.
304 **/
305 void
306 art_affine_identity (double dst[6])
307 {
308 dst[0] = 1;
309 dst[1] = 0;
310 dst[2] = 0;
311 dst[3] = 1;
312 dst[4] = 0;
313 dst[5] = 0;
314 }
317 /**
318 * art_affine_scale: Set up a scaling matrix.
319 * @dst: Where to store the resulting affine transform.
320 * @sx: X scale factor.
321 * @sy: Y scale factor.
322 *
323 * Sets up a scaling matrix.
324 **/
325 void
326 art_affine_scale (double dst[6], double sx, double sy)
327 {
328 dst[0] = sx;
329 dst[1] = 0;
330 dst[2] = 0;
331 dst[3] = sy;
332 dst[4] = 0;
333 dst[5] = 0;
334 }
336 /**
337 * art_affine_rotate: Set up a rotation affine transform.
338 * @dst: Where to store the resulting affine transform.
339 * @theta: Rotation angle in degrees.
340 *
341 * Sets up a rotation matrix. In the standard libart coordinate
342 * system, in which increasing y moves downward, this is a
343 * counterclockwise rotation. In the standard PostScript coordinate
344 * system, which is reversed in the y direction, it is a clockwise
345 * rotation.
346 **/
347 void
348 art_affine_rotate (double dst[6], double theta)
349 {
350 double s, c;
352 s = sin (theta * M_PI / 180.0);
353 c = cos (theta * M_PI / 180.0);
354 dst[0] = c;
355 dst[1] = s;
356 dst[2] = -s;
357 dst[3] = c;
358 dst[4] = 0;
359 dst[5] = 0;
360 }
362 /**
363 * art_affine_shear: Set up a shearing matrix.
364 * @dst: Where to store the resulting affine transform.
365 * @theta: Shear angle in degrees.
366 *
367 * Sets up a shearing matrix. In the standard libart coordinate system
368 * and a small value for theta, || becomes \\. Horizontal lines remain
369 * unchanged.
370 **/
371 void
372 art_affine_shear (double dst[6], double theta)
373 {
374 double t;
376 t = tan (theta * M_PI / 180.0);
377 dst[0] = 1;
378 dst[1] = 0;
379 dst[2] = t;
380 dst[3] = 1;
381 dst[4] = 0;
382 dst[5] = 0;
383 }
385 /**
386 * art_affine_translate: Set up a translation matrix.
387 * @dst: Where to store the resulting affine transform.
388 * @tx: X translation amount.
389 * @tx: Y translation amount.
390 *
391 * Sets up a translation matrix.
392 **/
393 void
394 art_affine_translate (double dst[6], double tx, double ty)
395 {
396 dst[0] = 1;
397 dst[1] = 0;
398 dst[2] = 0;
399 dst[3] = 1;
400 dst[4] = tx;
401 dst[5] = ty;
402 }
404 /**
405 * art_affine_expansion: Find the affine's expansion factor.
406 * @src: The affine transformation.
407 *
408 * Finds the expansion factor, i.e. the square root of the factor
409 * by which the affine transform affects area. In an affine transform
410 * composed of scaling, rotation, shearing, and translation, returns
411 * the amount of scaling.
412 *
413 * Return value: the expansion factor.
414 **/
415 double
416 art_affine_expansion (const double src[6])
417 {
418 return sqrt (fabs (src[0] * src[3] - src[1] * src[2]));
419 }
421 /**
422 * art_affine_rectilinear: Determine whether the affine transformation is rectilinear.
423 * @src: The original affine transformation.
424 *
425 * Determines whether @src is rectilinear, i.e. grid-aligned
426 * rectangles are transformed to other grid-aligned rectangles. The
427 * implementation has epsilon-tolerance for roundoff errors.
428 *
429 * Return value: TRUE if @src is rectilinear.
430 **/
431 int
432 art_affine_rectilinear (const double src[6])
433 {
434 return ((fabs (src[1]) < EPSILON && fabs (src[2]) < EPSILON) ||
435 (fabs (src[0]) < EPSILON && fabs (src[3]) < EPSILON));
436 }
438 /**
439 * art_affine_equal: Determine whether two affine transformations are equal.
440 * @matrix1: An affine transformation.
441 * @matrix2: Another affine transformation.
442 *
443 * Determines whether @matrix1 and @matrix2 are equal, with
444 * epsilon-tolerance for roundoff errors.
445 *
446 * Return value: TRUE if @matrix1 and @matrix2 are equal.
447 **/
448 int
449 art_affine_equal (double matrix1[6], double matrix2[6])
450 {
451 return (fabs (matrix1[0] - matrix2[0]) < EPSILON &&
452 fabs (matrix1[1] - matrix2[1]) < EPSILON &&
453 fabs (matrix1[2] - matrix2[2]) < EPSILON &&
454 fabs (matrix1[3] - matrix2[3]) < EPSILON &&
455 fabs (matrix1[4] - matrix2[4]) < EPSILON &&
456 fabs (matrix1[5] - matrix2[5]) < EPSILON);
457 }