Included 2009 in the copyright
[adg.git] / adg / adg-matrix.c
blob77635b33562496ae8b730471797e8c1a2c8d2147
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"
29 #include <string.h>
32 static AdgMatrix null_matrix = { 0., 0., 0., 0., 0., 0. };
33 static AdgMatrix identity_matrix = { 1., 0., 0., 1., 0., 0. };
36 GType
37 adg_matrix_get_type(void)
39 static int matrix_type = 0;
41 if (G_UNLIKELY(matrix_type == 0))
42 matrix_type = g_boxed_type_register_static("AdgMatrix",
43 (GBoxedCopyFunc) adg_matrix_dup,
44 g_free);
46 return matrix_type;
49 /**
50 * adg_matrix_dup:
51 * @matrix: an #AdgMatrix structure
53 * Duplicates @matrix.
55 * Return value: the duplicate of @matrix: must be freed with g_free()
56 * when no longer needed.
58 AdgMatrix *
59 adg_matrix_dup(const AdgMatrix * matrix)
61 g_return_val_if_fail(matrix != NULL, NULL);
63 return g_memdup(matrix, sizeof(AdgMatrix));
66 /**
67 * adg_matrix_get_fallback:
69 * Gets a fallback matrix. The fallback matrix is a statically allocated
70 * identity matrix.
72 * Return value: the fallback matrix
74 const AdgMatrix *
75 adg_matrix_get_fallback(void)
77 return &identity_matrix;
80 /**
81 * adg_matrix_init_null:
82 * @matrix: the #AdgMatrix to nullify
84 * Nullifies a matrix, setting all its components to 0.
86 void
87 adg_matrix_init_null(AdgMatrix * matrix)
89 memcpy(matrix, &null_matrix, sizeof(AdgMatrix));
92 /**
93 * adg_matrix_is_null:
94 * @matrix: the #AdgMatrix to check
96 * Checks if a matrix is a nullified matrix.
98 * Return value: %TRUE if the matrix is a null matrix, %FALSE otherwise
100 gboolean
101 adg_matrix_is_null(const AdgMatrix * matrix)
103 return memcmp(matrix, &null_matrix, sizeof(AdgMatrix)) == 0;
107 * adg_matrix_copy:
108 * @matrix: the destination #AdgMatrix
109 * @src: the source #AdgMatrix
111 * Copies @matrix to @dst.
113 * Return value: @matrix
115 AdgMatrix *
116 adg_matrix_copy(AdgMatrix *matrix, const AdgMatrix *src)
118 g_return_val_if_fail(matrix != NULL, matrix);
119 g_return_val_if_fail(src != NULL, matrix);
121 memcpy(matrix, src, sizeof(AdgMatrix));
123 return matrix;
127 * adg_matrix_equal:
128 * @matrix1: an #AdgMatrix
129 * @matrix2: an #AdgMatrix
131 * Compares @matrix1 and @matrix2 and returns %TRUE if the matrices are equal.
133 * Return value: %TRUE if @matrix1 is equal to @matrix2, %FALSE otherwise
135 gboolean
136 adg_matrix_equal(const AdgMatrix * matrix1, const AdgMatrix * matrix2)
138 g_return_val_if_fail(matrix1 != NULL, FALSE);
139 g_return_val_if_fail(matrix2 != NULL, FALSE);
141 return memcmp(matrix1, matrix2, sizeof(AdgMatrix)) == 0;