blob: 7feea431050a16f50c10e1a5d74d2fb4ca915ba6 [file] [log] [blame]
Pekka Paalanen668ca372012-01-12 14:30:47 +02001/*
2 * Copyright © 2011 Intel Corporation
Pekka Paalanen75b47ec2012-01-16 14:27:00 +02003 * Copyright © 2012 Collabora, Ltd.
Pekka Paalanen668ca372012-01-12 14:30:47 +02004 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Pekka Paalanen668ca372012-01-12 14:30:47 +020012 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Pekka Paalanen668ca372012-01-12 14:30:47 +020025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
28
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030029#include <float.h>
Pekka Paalanen668ca372012-01-12 14:30:47 +020030#include <string.h>
31#include <stdlib.h>
Pekka Paalanen75b47ec2012-01-16 14:27:00 +020032#include <math.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050033
34#ifdef IN_WESTON
Pekka Paalanen668ca372012-01-12 14:30:47 +020035#include <wayland-server.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050036#else
37#define WL_EXPORT
38#endif
Pekka Paalanen668ca372012-01-12 14:30:47 +020039
40#include "matrix.h"
41
Pekka Paalanend1f0ab62012-01-20 10:47:57 +020042
Pekka Paalanen668ca372012-01-12 14:30:47 +020043/*
44 * Matrices are stored in column-major order, that is the array indices are:
45 * 0 4 8 12
46 * 1 5 9 13
47 * 2 6 10 14
48 * 3 7 11 15
49 */
50
51WL_EXPORT void
52weston_matrix_init(struct weston_matrix *matrix)
53{
54 static const struct weston_matrix identity = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030055 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
56 .type = 0,
Pekka Paalanen668ca372012-01-12 14:30:47 +020057 };
58
59 memcpy(matrix, &identity, sizeof identity);
60}
61
62/* m <- n * m, that is, m is multiplied on the LEFT. */
63WL_EXPORT void
64weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
65{
66 struct weston_matrix tmp;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020067 const float *row, *column;
Pekka Paalanen668ca372012-01-12 14:30:47 +020068 div_t d;
69 int i, j;
70
71 for (i = 0; i < 16; i++) {
72 tmp.d[i] = 0;
73 d = div(i, 4);
74 row = m->d + d.quot * 4;
75 column = n->d + d.rem;
76 for (j = 0; j < 4; j++)
77 tmp.d[i] += row[j] * column[j * 4];
78 }
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030079 tmp.type = m->type | n->type;
Pekka Paalanen668ca372012-01-12 14:30:47 +020080 memcpy(m, &tmp, sizeof tmp);
81}
82
83WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020084weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020085{
86 struct weston_matrix translate = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030087 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 },
88 .type = WESTON_MATRIX_TRANSFORM_TRANSLATE,
Pekka Paalanen668ca372012-01-12 14:30:47 +020089 };
90
91 weston_matrix_multiply(matrix, &translate);
92}
93
94WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020095weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020096{
97 struct weston_matrix scale = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030098 .d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 },
99 .type = WESTON_MATRIX_TRANSFORM_SCALE,
Pekka Paalanen668ca372012-01-12 14:30:47 +0200100 };
101
102 weston_matrix_multiply(matrix, &scale);
103}
104
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300105WL_EXPORT void
106weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin)
107{
108 struct weston_matrix translate = {
109 .d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
110 .type = WESTON_MATRIX_TRANSFORM_ROTATE,
111 };
112
113 weston_matrix_multiply(matrix, &translate);
114}
115
Pekka Paalanen668ca372012-01-12 14:30:47 +0200116/* v <- m * v */
117WL_EXPORT void
118weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
119{
120 int i, j;
121 struct weston_vector t;
122
123 for (i = 0; i < 4; i++) {
124 t.f[i] = 0;
125 for (j = 0; j < 4; j++)
126 t.f[i] += v->f[j] * matrix->d[i + j * 4];
127 }
128
129 *v = t;
130}
Pekka Paalanen061b7472012-01-12 15:00:57 +0200131
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200132static inline void
133swap_rows(double *a, double *b)
134{
135 unsigned k;
136 double tmp;
137
138 for (k = 0; k < 13; k += 4) {
139 tmp = a[k];
140 a[k] = b[k];
141 b[k] = tmp;
142 }
143}
144
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200145static inline void
146swap_unsigned(unsigned *a, unsigned *b)
147{
148 unsigned tmp;
149
150 tmp = *a;
151 *a = *b;
152 *b = tmp;
153}
154
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200155static inline unsigned
156find_pivot(double *column, unsigned k)
157{
158 unsigned p = k;
159 for (++k; k < 4; ++k)
160 if (fabs(column[p]) < fabs(column[k]))
161 p = k;
162
163 return p;
164}
165
166/*
167 * reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
168 * 3rd ed. The Johns Hopkins University Press. 1996.
169 * LU decomposition, forward and back substitution: Chapter 3.
170 */
171
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200172MATRIX_TEST_EXPORT inline int
173matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
Pekka Paalanen061b7472012-01-12 15:00:57 +0200174{
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200175 unsigned i, j, k;
176 unsigned pivot;
177 double pv;
178
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200179 for (i = 0; i < 4; ++i)
180 p[i] = i;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200181 for (i = 16; i--; )
182 A[i] = matrix->d[i];
183
184 /* LU decomposition with partial pivoting */
185 for (k = 0; k < 4; ++k) {
186 pivot = find_pivot(&A[k * 4], k);
187 if (pivot != k) {
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200188 swap_unsigned(&p[k], &p[pivot]);
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200189 swap_rows(&A[k], &A[pivot]);
190 }
191
192 pv = A[k * 4 + k];
193 if (fabs(pv) < 1e-9)
194 return -1; /* zero pivot, not invertible */
195
196 for (i = k + 1; i < 4; ++i) {
197 A[i + k * 4] /= pv;
198
199 for (j = k + 1; j < 4; ++j)
200 A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
201 }
202 }
203
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200204 return 0;
205}
206
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200207MATRIX_TEST_EXPORT inline void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200208inverse_transform(const double *LU, const unsigned *p, float *v)
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200209{
210 /* Solve A * x = v, when we have P * A = L * U.
211 * P * A * x = P * v => L * U * x = P * v
212 * Let U * x = b, then L * b = P * v.
213 */
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200214 double b[4];
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200215 unsigned j;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200216
217 /* Forward substitution, column version, solves L * b = P * v */
218 /* The diagonal of L is all ones, and not explicitly stored. */
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200219 b[0] = v[p[0]];
220 b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
221 b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
222 b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200223 b[2] -= b[1] * LU[2 + 1 * 4];
224 b[3] -= b[1] * LU[3 + 1 * 4];
225 b[3] -= b[2] * LU[3 + 2 * 4];
226
227 /* backward substitution, column version, solves U * y = b */
228#if 1
229 /* hand-unrolled, 25% faster for whole function */
230 b[3] /= LU[3 + 3 * 4];
231 b[0] -= b[3] * LU[0 + 3 * 4];
232 b[1] -= b[3] * LU[1 + 3 * 4];
233 b[2] -= b[3] * LU[2 + 3 * 4];
234
235 b[2] /= LU[2 + 2 * 4];
236 b[0] -= b[2] * LU[0 + 2 * 4];
237 b[1] -= b[2] * LU[1 + 2 * 4];
238
239 b[1] /= LU[1 + 1 * 4];
240 b[0] -= b[1] * LU[0 + 1 * 4];
241
242 b[0] /= LU[0 + 0 * 4];
243#else
244 for (j = 3; j > 0; --j) {
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200245 unsigned k;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200246 b[j] /= LU[j + j * 4];
247 for (k = 0; k < j; ++k)
248 b[k] -= b[j] * LU[k + j * 4];
249 }
250
251 b[0] /= LU[0 + 0 * 4];
252#endif
253
254 /* the result */
255 for (j = 0; j < 4; ++j)
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200256 v[j] = b[j];
257}
258
259WL_EXPORT int
260weston_matrix_invert(struct weston_matrix *inverse,
261 const struct weston_matrix *matrix)
262{
263 double LU[16]; /* column-major */
264 unsigned perm[4]; /* permutation */
265 unsigned c;
266
267 if (matrix_invert(LU, perm, matrix) < 0)
268 return -1;
269
270 weston_matrix_init(inverse);
271 for (c = 0; c < 4; ++c)
272 inverse_transform(LU, perm, &inverse->d[c * 4]);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300273 inverse->type = matrix->type;
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200274
275 return 0;
Pekka Paalanen061b7472012-01-12 15:00:57 +0200276}