1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
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.
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.
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.
20 /* Simple manipulations with affine transformations */
23 #include <stdio.h> /* for sprintf */
24 #include <string.h> /* for strcpy */
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.
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.
43 art_affine_point (ArtPoint *dst, const ArtPoint *src,
44 const double affine[6])
50 dst->x = x * affine[0] + y * affine[2] + affine[4];
51 dst->y = x * affine[1] + y * affine[3] + affine[5];
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.
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.
66 art_affine_invert (double dst[6], const double src[6])
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];
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.
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
92 art_affine_flip (double dst_affine[6], const double src_affine[6], int horz, int vert)
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];
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. */
110 art_ftoa (char str[80], double x)
116 if (fabs (x) < EPSILON / 2)
126 if ((int)floor ((x + EPSILON / 2) < 1))
130 i = sprintf (p, "%06d", (int)floor ((x + EPSILON / 2) * 1e6));
131 while (i && p[i - 1] == '0')
139 i = sprintf (p, "%d", (int)floor (x + EPSILON / 2));
146 x -= floor (x + EPSILON / 2);
147 for (j = i; j < 6; j++)
149 ix = floor (x + 0.5);
151 for (j = 0; j < i; j++)
154 /* A cheap hack, this routine can round wrong for fractions
159 sprintf (p, "%06d", ix);
161 while (i && p[i - 1] == '0')
169 p += sprintf (p, "%g", x);
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.
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.
190 art_affine_to_string (char str[128], const double src[6])
196 for (i = 0; i < 1000; i++)
198 double d = rand () * .1 / RAND_MAX;
200 printf ("%g %f %s\n", d, d, tmp);
203 if (fabs (src[4]) < EPSILON && fabs (src[5]) < EPSILON)
205 /* could be scale or rotate */
206 if (fabs (src[1]) < EPSILON && fabs (src[2]) < EPSILON)
209 if (fabs (src[0] - 1) < EPSILON && fabs (src[3] - 1) < EPSILON)
211 /* identity transform */
218 ix += art_ftoa (str + ix, src[0]);
220 ix += art_ftoa (str + ix, src[3]);
221 strcpy (str + ix, " scale");
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)
234 theta = (180 / M_PI) * atan2 (src[1], src[0]);
235 art_ftoa (tmp, theta);
236 sprintf (str, "%s rotate", tmp);
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)
248 ix += art_ftoa (str + ix, src[4]);
250 ix += art_ftoa (str + ix, src[5]);
251 strcpy (str + ix, " translate");
259 for (i = 0; i < 6; i++)
261 ix += art_ftoa (str + ix, src[i]);
264 strcpy (str + ix, "] concat");
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.
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)";
278 * It is safe to call this function with @dst equal to @src1 or @src2.
281 art_affine_multiply (double dst[6], const double src1[6], const double src2[6])
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];
300 * art_affine_identity: Set up the identity matrix.
301 * @dst: Where to store the resulting affine transform.
303 * Sets up an identity matrix.
306 art_affine_identity (double dst[6])
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.
323 * Sets up a scaling matrix.
326 art_affine_scale (double dst[6], double sx, double sy)
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.
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
348 art_affine_rotate (double dst[6], double theta)
352 s = sin (theta * M_PI / 180.0);
353 c = cos (theta * M_PI / 180.0);
363 * art_affine_shear: Set up a shearing matrix.
364 * @dst: Where to store the resulting affine transform.
365 * @theta: Shear angle in degrees.
367 * Sets up a shearing matrix. In the standard libart coordinate system
368 * and a small value for theta, || becomes \\. Horizontal lines remain
372 art_affine_shear (double dst[6], double theta)
376 t = tan (theta * M_PI / 180.0);
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.
391 * Sets up a translation matrix.
394 art_affine_translate (double dst[6], double tx, double ty)
405 * art_affine_expansion: Find the affine's expansion factor.
406 * @src: The affine transformation.
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.
413 * Return value: the expansion factor.
416 art_affine_expansion (const double src[6])
418 return sqrt (fabs (src[0] * src[3] - src[1] * src[2]));
422 * art_affine_rectilinear: Determine whether the affine transformation is rectilinear.
423 * @src: The original affine transformation.
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.
429 * Return value: TRUE if @src is rectilinear.
432 art_affine_rectilinear (const double src[6])
434 return ((fabs (src[1]) < EPSILON && fabs (src[2]) < EPSILON) ||
435 (fabs (src[0]) < EPSILON && fabs (src[3]) < EPSILON));
439 * art_affine_equal: Determine whether two affine transformations are equal.
440 * @matrix1: An affine transformation.
441 * @matrix2: Another affine transformation.
443 * Determines whether @matrix1 and @matrix2 are equal, with
444 * epsilon-tolerance for roundoff errors.
446 * Return value: TRUE if @matrix1 and @matrix2 are equal.
449 art_affine_equal (double matrix1[6], double matrix2[6])
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);