469e155312223d61609cc953a90e899fe45e2065
[adg.git] / src / adg / adg-matrix.c
blob469e155312223d61609cc953a90e899fe45e2065
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-matrix
23 * @Section_Id:Matrix
24 * @title: Matrix
25 * @short_description: #cairo_matrix_t enhancements and utilities.
27 * This API provides a #GBoxed wrapper around #cairo_matrix_t
28 * (if not yet provided by cairo-gobject) * and augments its methods
29 * with some useful addition.
31 * Since: 1.0
32 **/
35 #include "adg-internal.h"
36 #include <string.h>
37 #include <math.h>
39 #include "adg-matrix-fallback.h"
42 #ifdef ADG_MISSING_GBOXED_MATRIX
44 GType
45 cairo_gobject_matrix_get_type(void)
47 static GType matrix_type = 0;
49 if (G_UNLIKELY(matrix_type == 0))
50 matrix_type = g_boxed_type_register_static("CairoMatrix",
51 (GBoxedCopyFunc) cairo_gobject_cairo_matrix_copy,
52 g_free);
54 return matrix_type;
57 cairo_matrix_t *
58 cairo_gobject_cairo_matrix_copy(const cairo_matrix_t *matrix)
60 return g_memdup(matrix, sizeof(cairo_matrix_t));
63 #endif /* ADG_MISSING_GBOXED_MATRIX */
66 /**
67 * adg_matrix_identity:
69 * A convenient constant providing an identity matrix.
71 * Returns: (transfer none): a pointer to the identity matrix
73 * Since: 1.0
74 **/
75 const cairo_matrix_t *
76 adg_matrix_identity(void)
78 static cairo_matrix_t *identity_matrix = NULL;
80 if (G_UNLIKELY(identity_matrix == NULL)) {
81 identity_matrix = g_new(cairo_matrix_t, 1);
82 cairo_matrix_init_identity(identity_matrix);
85 return identity_matrix;
88 /**
89 * adg_matrix_null:
91 * A convenient constant providing an null matrix, that is a matrix
92 * where all components are 0.
94 * Returns: (transfer none): a pointer to the null matrix
96 * Since: 1.0
97 **/
98 const cairo_matrix_t *
99 adg_matrix_null(void)
101 static cairo_matrix_t *null_matrix = NULL;
103 if (G_UNLIKELY(null_matrix == NULL))
104 null_matrix = g_new0(cairo_matrix_t, 1);
106 return null_matrix;
110 * adg_matrix_copy:
111 * @matrix: (out caller-allocates): the destination #cairo_matrix_t
112 * @src: the source #cairo_matrix_t
114 * Copies @src to @matrix.
116 * Since: 1.0
118 void
119 adg_matrix_copy(cairo_matrix_t *matrix, const cairo_matrix_t *src)
121 g_return_if_fail(matrix != NULL);
122 g_return_if_fail(src != NULL);
124 memcpy(matrix, src, sizeof(cairo_matrix_t));
128 * adg_matrix_dup:
129 * @matrix: the souce #cairo_matrix_t
131 * Duplicates @matrix.
133 * Returns: (transfer full): a duplicate of @matrix that must be freed with g_free() when no longer needed.
135 * Since: 1.0
137 cairo_matrix_t *
138 adg_matrix_dup(const cairo_matrix_t *matrix)
140 g_return_val_if_fail(matrix != NULL, NULL);
142 return g_memdup(matrix, sizeof(cairo_matrix_t));
146 * adg_matrix_equal:
147 * @matrix1: the first operand
148 * @matrix2: the second operand
150 * Compares @matrix1 and @matrix2 and returns %TRUE if the matrices are equal.
152 * Returns: %TRUE if @matrix1 is equal to @matrix2, %FALSE otherwise
154 * Since: 1.0
156 gboolean
157 adg_matrix_equal(const cairo_matrix_t *matrix1, const cairo_matrix_t *matrix2)
159 g_return_val_if_fail(matrix1 != NULL, FALSE);
160 g_return_val_if_fail(matrix2 != NULL, FALSE);
162 return
163 matrix1->xx == matrix2->xx &&
164 matrix1->yx == matrix2->yx &&
165 matrix1->xy == matrix2->xy &&
166 matrix1->yy == matrix2->yy &&
167 matrix1->x0 == matrix2->x0 &&
168 matrix1->y0 == matrix2->y0;
172 * adg_matrix_normalize:
173 * @matrix: (inout): the source/destination #cairo_matrix_t
175 * Gets rid of the scaling component of a matrix.
177 * Returns: %TRUE on success, %FALSE on errors
179 * Since: 1.0
181 gboolean
182 adg_matrix_normalize(cairo_matrix_t *matrix)
184 gdouble k;
186 g_return_val_if_fail(matrix != NULL, FALSE);
188 if (matrix->xx != matrix->yy || matrix->xy != -matrix->yx) {
189 g_warning(_("%s: normalization of anamorphic matrices not supported"),
190 G_STRLOC);
191 return FALSE;
194 if (matrix->xy == 0) {
195 k = matrix->xx;
196 } else if (matrix->xx == 0) {
197 k = matrix->xy;
198 } else {
199 k = sqrt(matrix->xx * matrix->xx + matrix->xy * matrix->xy);
202 g_return_val_if_fail(k != 0, FALSE);
204 matrix->xx /= k;
205 matrix->xy /= k;
206 matrix->yy /= k;
207 matrix->yx /= k;
209 return TRUE;
213 * adg_matrix_transform:
214 * @matrix: (inout): the source/destination #cairo_matrix_t
215 * @transformation: the transformation to apply
216 * @mode: (in): how @transformation should be applied
218 * Modifies @matrix applying @transformation in the way specified by
219 * @mode.
221 * Since: 1.0
223 void
224 adg_matrix_transform(cairo_matrix_t *matrix,
225 const cairo_matrix_t *transformation, AdgTransformMode mode)
227 cairo_matrix_t normalized;
229 g_return_if_fail(matrix != NULL);
230 g_return_if_fail(transformation != NULL);
232 switch (mode) {
233 case ADG_TRANSFORM_NONE:
234 break;
235 case ADG_TRANSFORM_BEFORE:
236 cairo_matrix_multiply(matrix, transformation, matrix);
237 break;
238 case ADG_TRANSFORM_AFTER:
239 cairo_matrix_multiply(matrix, matrix, transformation);
240 break;
241 case ADG_TRANSFORM_BEFORE_NORMALIZED:
242 adg_matrix_copy(&normalized, transformation);
243 adg_matrix_normalize(&normalized);
244 cairo_matrix_multiply(matrix, &normalized, matrix);
245 break;
246 case ADG_TRANSFORM_AFTER_NORMALIZED:
247 adg_matrix_copy(&normalized, transformation);
248 adg_matrix_normalize(&normalized);
249 cairo_matrix_multiply(matrix, matrix, &normalized);
250 break;
251 default:
252 g_return_if_reached();
257 * adg_matrix_dump:
258 * @matrix: an #cairo_matrix_t
260 * Dumps the specified @matrix to stdout. Useful for debugging purposes.
262 * Since: 1.0
264 void
265 adg_matrix_dump(const cairo_matrix_t *matrix)
267 g_return_if_fail(matrix != NULL);
269 g_print("[%8.3lf %8.3lf] [%8.3lf]\n"
270 "[%8.3lf %8.3lf] [%8.3lf]\n",
271 matrix->xx, matrix->xy, matrix->x0,
272 matrix->yx, matrix->yy, matrix->y0);