[AdgMatrix] Added adg_matrix_init_reflection()
[adg.git] / adg / adg-matrix.c
blob8887bed1f9b13300617c5c7b62d51de634510925
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.
20 /**
21 * SECTION:matrix
22 * @title: AdgMatrix
23 * @short_description: A wrapper for the cairo_matrix_t struct
25 * The AdgMatrix is essentially a wrapper for #cairo_matrix_t structures.
28 #include "adg-matrix.h"
30 #include <string.h>
31 #include <math.h>
34 static AdgMatrix null_matrix = { 0., 0., 0., 0., 0., 0. };
35 static AdgMatrix identity_matrix = { 1., 0., 0., 1., 0., 0. };
38 GType
39 adg_matrix_get_type(void)
41 static int matrix_type = 0;
43 if (G_UNLIKELY(matrix_type == 0))
44 matrix_type = g_boxed_type_register_static("AdgMatrix",
45 (GBoxedCopyFunc) adg_matrix_dup,
46 g_free);
48 return matrix_type;
51 /**
52 * adg_matrix_dup:
53 * @matrix: an #AdgMatrix structure
55 * Duplicates @matrix.
57 * Return value: the duplicate of @matrix: must be freed with g_free()
58 * when no longer needed.
60 AdgMatrix *
61 adg_matrix_dup(const AdgMatrix *matrix)
63 g_return_val_if_fail(matrix != NULL, NULL);
65 return g_memdup(matrix, sizeof(AdgMatrix));
68 /**
69 * adg_matrix_get_fallback:
71 * Gets a fallback matrix. The fallback matrix is a statically allocated
72 * identity matrix.
74 * Return value: the fallback matrix
76 const AdgMatrix *
77 adg_matrix_get_fallback(void)
79 return &identity_matrix;
82 /**
83 * adg_matrix_init_null:
84 * @matrix: the #AdgMatrix to nullify
86 * Nullifies a matrix, setting all its components to 0.
88 void
89 adg_matrix_init_null(AdgMatrix *matrix)
91 memcpy(matrix, &null_matrix, sizeof(AdgMatrix));
94 /**
95 * adg_matrix_is_null:
96 * @matrix: the #AdgMatrix to check
98 * Checks if a matrix is a nullified matrix.
100 * Return value: %TRUE if the matrix is a null matrix, %FALSE otherwise
102 gboolean
103 adg_matrix_is_null(const AdgMatrix *matrix)
105 return memcmp(matrix, &null_matrix, sizeof(AdgMatrix)) == 0;
109 * adg_matrix_copy:
110 * @matrix: the destination #AdgMatrix
111 * @src: the source #AdgMatrix
113 * Copies @matrix to @dst.
115 * Return value: @matrix
117 AdgMatrix *
118 adg_matrix_copy(AdgMatrix *matrix, const AdgMatrix *src)
120 g_return_val_if_fail(matrix != NULL, matrix);
121 g_return_val_if_fail(src != NULL, matrix);
123 memcpy(matrix, src, sizeof(AdgMatrix));
125 return matrix;
129 * adg_matrix_equal:
130 * @matrix1: an #AdgMatrix
131 * @matrix2: an #AdgMatrix
133 * Compares @matrix1 and @matrix2 and returns %TRUE if the matrices are equal.
135 * Return value: %TRUE if @matrix1 is equal to @matrix2, %FALSE otherwise
137 gboolean
138 adg_matrix_equal(const AdgMatrix *matrix1, const AdgMatrix *matrix2)
140 g_return_val_if_fail(matrix1 != NULL, FALSE);
141 g_return_val_if_fail(matrix2 != NULL, FALSE);
143 return memcmp(matrix1, matrix2, sizeof(AdgMatrix)) == 0;
147 * adg_matrix_init_reflection:
148 * @matrix: the destination #AdgMatrix
149 * @radians: angle of the mirroring axis, in radians
151 * Initialized @matrix to a transformation that reflects arond an axis
152 * rotated by @radians and passing throught the origin (0, 0).
154 void
155 adg_matrix_init_reflection(AdgMatrix *matrix, double radians)
157 double s2 = sin(2*radians);
158 double c2 = cos(2*radians);
160 cairo_matrix_init(matrix, c2, s2, s2, -c2, 0, 0);