1 /*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013-2015 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #pragma once
27
28 #include "config.h"
29
30 #include <string.h>
31 #include <stdbool.h>
32 #include <math.h>
33
34 struct matrix {
35 float val[3][3]; /* [row][col] */
36 };
37
38 static inline double
deg2rad(int degree)39 deg2rad(int degree)
40 {
41 return M_PI * degree / 180.0;
42 }
43
44 static inline void
matrix_init_identity(struct matrix * m)45 matrix_init_identity(struct matrix *m)
46 {
47 memset(m, 0, sizeof(*m));
48 m->val[0][0] = 1;
49 m->val[1][1] = 1;
50 m->val[2][2] = 1;
51 }
52
53 static inline void
matrix_from_farray6(struct matrix * m,const float values[6])54 matrix_from_farray6(struct matrix *m, const float values[6])
55 {
56 matrix_init_identity(m);
57 m->val[0][0] = values[0];
58 m->val[0][1] = values[1];
59 m->val[0][2] = values[2];
60 m->val[1][0] = values[3];
61 m->val[1][1] = values[4];
62 m->val[1][2] = values[5];
63 }
64
65 static inline void
matrix_init_scale(struct matrix * m,float sx,float sy)66 matrix_init_scale(struct matrix *m, float sx, float sy)
67 {
68 matrix_init_identity(m);
69 m->val[0][0] = sx;
70 m->val[1][1] = sy;
71 }
72
73 static inline void
matrix_init_translate(struct matrix * m,float x,float y)74 matrix_init_translate(struct matrix *m, float x, float y)
75 {
76 matrix_init_identity(m);
77 m->val[0][2] = x;
78 m->val[1][2] = y;
79 }
80
81 static inline void
matrix_init_rotate(struct matrix * m,int degrees)82 matrix_init_rotate(struct matrix *m, int degrees)
83 {
84 double s, c;
85
86 s = sin(deg2rad(degrees));
87 c = cos(deg2rad(degrees));
88
89 matrix_init_identity(m);
90 m->val[0][0] = c;
91 m->val[0][1] = -s;
92 m->val[1][0] = s;
93 m->val[1][1] = c;
94 }
95
96 static inline bool
matrix_is_identity(const struct matrix * m)97 matrix_is_identity(const struct matrix *m)
98 {
99 return (m->val[0][0] == 1 &&
100 m->val[0][1] == 0 &&
101 m->val[0][2] == 0 &&
102 m->val[1][0] == 0 &&
103 m->val[1][1] == 1 &&
104 m->val[1][2] == 0 &&
105 m->val[2][0] == 0 &&
106 m->val[2][1] == 0 &&
107 m->val[2][2] == 1);
108 }
109
110 static inline void
matrix_mult(struct matrix * dest,const struct matrix * m1,const struct matrix * m2)111 matrix_mult(struct matrix *dest,
112 const struct matrix *m1,
113 const struct matrix *m2)
114 {
115 struct matrix m; /* allow for dest == m1 or dest == m2 */
116 int row, col, i;
117
118 for (row = 0; row < 3; row++) {
119 for (col = 0; col < 3; col++) {
120 double v = 0;
121 for (i = 0; i < 3; i++) {
122 v += m1->val[row][i] * m2->val[i][col];
123 }
124 m.val[row][col] = v;
125 }
126 }
127
128 memcpy(dest, &m, sizeof(m));
129 }
130
131 static inline void
matrix_mult_vec(const struct matrix * m,int * x,int * y)132 matrix_mult_vec(const struct matrix *m, int *x, int *y)
133 {
134 int tx, ty;
135
136 tx = *x * m->val[0][0] + *y * m->val[0][1] + m->val[0][2];
137 ty = *x * m->val[1][0] + *y * m->val[1][1] + m->val[1][2];
138
139 *x = tx;
140 *y = ty;
141 }
142
143 static inline void
matrix_to_farray6(const struct matrix * m,float out[6])144 matrix_to_farray6(const struct matrix *m, float out[6])
145 {
146 out[0] = m->val[0][0];
147 out[1] = m->val[0][1];
148 out[2] = m->val[0][2];
149 out[3] = m->val[1][0];
150 out[4] = m->val[1][1];
151 out[5] = m->val[1][2];
152 }
153
154 static inline void
matrix_to_relative(struct matrix * dest,const struct matrix * src)155 matrix_to_relative(struct matrix *dest, const struct matrix *src)
156 {
157 matrix_init_identity(dest);
158 dest->val[0][0] = src->val[0][0];
159 dest->val[0][1] = src->val[0][1];
160 dest->val[1][0] = src->val[1][0];
161 dest->val[1][1] = src->val[1][1];
162 }
163