[AdgToyText] Refactored using maps
[adg.git] / adg / adg-matrix.c
blobc490e77aac7955c1ce552f54cc10f599688ec448
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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:AdgMatrix-GBoxed
24 * @title: AdgMatrix
25 * @short_description: A wrapper for #cairo_matrix_t
27 * AdgMatrix is a wrapper in #GType syntax of the #cairo_matrix_t struct.
30 /**
31 * AdgMatrix:
33 * Another name for #cairo_matrix_t: check its documentation for the
34 * fields description and visibility details.
35 **/
38 #include "adg-matrix.h"
40 #include <string.h>
41 #include <math.h>
44 static AdgMatrix null_matrix = { 0., 0., 0., 0., 0., 0. };
45 static AdgMatrix identity_matrix = { 1., 0., 0., 1., 0., 0. };
48 GType
49 adg_matrix_get_type(void)
51 static int matrix_type = 0;
53 if (G_UNLIKELY(matrix_type == 0))
54 matrix_type = g_boxed_type_register_static("AdgMatrix",
55 (GBoxedCopyFunc) adg_matrix_dup,
56 g_free);
58 return matrix_type;
61 /**
62 * adg_matrix_dup:
63 * @matrix: an #AdgMatrix structure
65 * Duplicates @matrix.
67 * Return value: the duplicate of @matrix: must be freed with g_free()
68 * when no longer needed.
70 AdgMatrix *
71 adg_matrix_dup(const AdgMatrix *matrix)
73 g_return_val_if_fail(matrix != NULL, NULL);
75 return g_memdup(matrix, sizeof(AdgMatrix));
78 /**
79 * adg_matrix_get_fallback:
81 * Gets a fallback matrix. The fallback matrix is a statically allocated
82 * identity matrix.
84 * Return value: the fallback matrix
86 const AdgMatrix *
87 adg_matrix_get_fallback(void)
89 return &identity_matrix;
92 /**
93 * adg_matrix_init_null:
94 * @matrix: the #AdgMatrix to nullify
96 * Nullifies a matrix, setting all its components to 0.
98 void
99 adg_matrix_init_null(AdgMatrix *matrix)
101 memcpy(matrix, &null_matrix, sizeof(AdgMatrix));
105 * adg_matrix_is_null:
106 * @matrix: the #AdgMatrix to check
108 * Checks if a matrix is a nullified matrix.
110 * Return value: %TRUE if the matrix is a null matrix, %FALSE otherwise
112 gboolean
113 adg_matrix_is_null(const AdgMatrix *matrix)
115 return memcmp(matrix, &null_matrix, sizeof(AdgMatrix)) == 0;
119 * adg_matrix_copy:
120 * @matrix: the destination #AdgMatrix
121 * @src: the source #AdgMatrix
123 * Copies @matrix to @dst.
125 * Return value: @matrix
127 AdgMatrix *
128 adg_matrix_copy(AdgMatrix *matrix, const AdgMatrix *src)
130 g_return_val_if_fail(matrix != NULL, matrix);
131 g_return_val_if_fail(src != NULL, matrix);
133 memcpy(matrix, src, sizeof(AdgMatrix));
135 return matrix;
139 * adg_matrix_equal:
140 * @matrix1: an #AdgMatrix
141 * @matrix2: an #AdgMatrix
143 * Compares @matrix1 and @matrix2 and returns %TRUE if the matrices are equal.
145 * Return value: %TRUE if @matrix1 is equal to @matrix2, %FALSE otherwise
147 gboolean
148 adg_matrix_equal(const AdgMatrix *matrix1, const AdgMatrix *matrix2)
150 g_return_val_if_fail(matrix1 != NULL, FALSE);
151 g_return_val_if_fail(matrix2 != NULL, FALSE);
153 return memcmp(matrix1, matrix2, sizeof(AdgMatrix)) == 0;
157 * adg_matrix_init_reflection:
158 * @matrix: the destination #AdgMatrix
159 * @radians: angle of the mirroring axis, in radians
161 * Initialized @matrix to a transformation that reflects arond an axis
162 * rotated by @radians and passing throught the origin (0, 0).
164 void
165 adg_matrix_init_reflection(AdgMatrix *matrix, double radians)
167 double s2 = sin(2*radians);
168 double c2 = cos(2*radians);
170 cairo_matrix_init(matrix, c2, s2, s2, -c2, 0, 0);