blob: 3ff4089a98c33d4277a315fbb1bdc47f91029c7f [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030024#include <float.h>
Pekka Paalanen668ca372012-01-12 14:30:47 +020025#include <string.h>
26#include <stdlib.h>
Pekka Paalanen75b47ec2012-01-16 14:27:00 +020027#include <math.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050028
29#ifdef IN_WESTON
Pekka Paalanen668ca372012-01-12 14:30:47 +020030#include <wayland-server.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050031#else
32#define WL_EXPORT
33#endif
Pekka Paalanen668ca372012-01-12 14:30:47 +020034
35#include "matrix.h"
36
Pekka Paalanend1f0ab62012-01-20 10:47:57 +020037
Pekka Paalanen668ca372012-01-12 14:30:47 +020038/*
39 * Matrices are stored in column-major order, that is the array indices are:
40 * 0 4 8 12
41 * 1 5 9 13
42 * 2 6 10 14
43 * 3 7 11 15
44 */
45
46WL_EXPORT void
47weston_matrix_init(struct weston_matrix *matrix)
48{
49 static const struct weston_matrix identity = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030050 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
51 .type = 0,
Pekka Paalanen668ca372012-01-12 14:30:47 +020052 };
53
54 memcpy(matrix, &identity, sizeof identity);
55}
56
57/* m <- n * m, that is, m is multiplied on the LEFT. */
58WL_EXPORT void
59weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
60{
61 struct weston_matrix tmp;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020062 const float *row, *column;
Pekka Paalanen668ca372012-01-12 14:30:47 +020063 div_t d;
64 int i, j;
65
66 for (i = 0; i < 16; i++) {
67 tmp.d[i] = 0;
68 d = div(i, 4);
69 row = m->d + d.quot * 4;
70 column = n->d + d.rem;
71 for (j = 0; j < 4; j++)
72 tmp.d[i] += row[j] * column[j * 4];
73 }
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030074 tmp.type = m->type | n->type;
Pekka Paalanen668ca372012-01-12 14:30:47 +020075 memcpy(m, &tmp, sizeof tmp);
76}
77
78WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020079weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020080{
81 struct weston_matrix translate = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030082 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 },
83 .type = WESTON_MATRIX_TRANSFORM_TRANSLATE,
Pekka Paalanen668ca372012-01-12 14:30:47 +020084 };
85
86 weston_matrix_multiply(matrix, &translate);
87}
88
89WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020090weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020091{
92 struct weston_matrix scale = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030093 .d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 },
94 .type = WESTON_MATRIX_TRANSFORM_SCALE,
Pekka Paalanen668ca372012-01-12 14:30:47 +020095 };
96
97 weston_matrix_multiply(matrix, &scale);
98}
99
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300100WL_EXPORT void
101weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin)
102{
103 struct weston_matrix translate = {
104 .d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
105 .type = WESTON_MATRIX_TRANSFORM_ROTATE,
106 };
107
108 weston_matrix_multiply(matrix, &translate);
109}
110
Pekka Paalanen668ca372012-01-12 14:30:47 +0200111/* v <- m * v */
112WL_EXPORT void
113weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
114{
115 int i, j;
116 struct weston_vector t;
117
118 for (i = 0; i < 4; i++) {
119 t.f[i] = 0;
120 for (j = 0; j < 4; j++)
121 t.f[i] += v->f[j] * matrix->d[i + j * 4];
122 }
123
124 *v = t;
125}
Pekka Paalanen061b7472012-01-12 15:00:57 +0200126
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200127static inline void
128swap_rows(double *a, double *b)
129{
130 unsigned k;
131 double tmp;
132
133 for (k = 0; k < 13; k += 4) {
134 tmp = a[k];
135 a[k] = b[k];
136 b[k] = tmp;
137 }
138}
139
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200140static inline void
141swap_unsigned(unsigned *a, unsigned *b)
142{
143 unsigned tmp;
144
145 tmp = *a;
146 *a = *b;
147 *b = tmp;
148}
149
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200150static inline unsigned
151find_pivot(double *column, unsigned k)
152{
153 unsigned p = k;
154 for (++k; k < 4; ++k)
155 if (fabs(column[p]) < fabs(column[k]))
156 p = k;
157
158 return p;
159}
160
161/*
162 * reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
163 * 3rd ed. The Johns Hopkins University Press. 1996.
164 * LU decomposition, forward and back substitution: Chapter 3.
165 */
166
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200167MATRIX_TEST_EXPORT inline int
168matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
Pekka Paalanen061b7472012-01-12 15:00:57 +0200169{
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200170 unsigned i, j, k;
171 unsigned pivot;
172 double pv;
173
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200174 for (i = 0; i < 4; ++i)
175 p[i] = i;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200176 for (i = 16; i--; )
177 A[i] = matrix->d[i];
178
179 /* LU decomposition with partial pivoting */
180 for (k = 0; k < 4; ++k) {
181 pivot = find_pivot(&A[k * 4], k);
182 if (pivot != k) {
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200183 swap_unsigned(&p[k], &p[pivot]);
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200184 swap_rows(&A[k], &A[pivot]);
185 }
186
187 pv = A[k * 4 + k];
188 if (fabs(pv) < 1e-9)
189 return -1; /* zero pivot, not invertible */
190
191 for (i = k + 1; i < 4; ++i) {
192 A[i + k * 4] /= pv;
193
194 for (j = k + 1; j < 4; ++j)
195 A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
196 }
197 }
198
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200199 return 0;
200}
201
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200202MATRIX_TEST_EXPORT inline void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200203inverse_transform(const double *LU, const unsigned *p, float *v)
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200204{
205 /* Solve A * x = v, when we have P * A = L * U.
206 * P * A * x = P * v => L * U * x = P * v
207 * Let U * x = b, then L * b = P * v.
208 */
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200209 double b[4];
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200210 unsigned j;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200211
212 /* Forward substitution, column version, solves L * b = P * v */
213 /* The diagonal of L is all ones, and not explicitly stored. */
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200214 b[0] = v[p[0]];
215 b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
216 b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
217 b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200218 b[2] -= b[1] * LU[2 + 1 * 4];
219 b[3] -= b[1] * LU[3 + 1 * 4];
220 b[3] -= b[2] * LU[3 + 2 * 4];
221
222 /* backward substitution, column version, solves U * y = b */
223#if 1
224 /* hand-unrolled, 25% faster for whole function */
225 b[3] /= LU[3 + 3 * 4];
226 b[0] -= b[3] * LU[0 + 3 * 4];
227 b[1] -= b[3] * LU[1 + 3 * 4];
228 b[2] -= b[3] * LU[2 + 3 * 4];
229
230 b[2] /= LU[2 + 2 * 4];
231 b[0] -= b[2] * LU[0 + 2 * 4];
232 b[1] -= b[2] * LU[1 + 2 * 4];
233
234 b[1] /= LU[1 + 1 * 4];
235 b[0] -= b[1] * LU[0 + 1 * 4];
236
237 b[0] /= LU[0 + 0 * 4];
238#else
239 for (j = 3; j > 0; --j) {
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200240 unsigned k;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200241 b[j] /= LU[j + j * 4];
242 for (k = 0; k < j; ++k)
243 b[k] -= b[j] * LU[k + j * 4];
244 }
245
246 b[0] /= LU[0 + 0 * 4];
247#endif
248
249 /* the result */
250 for (j = 0; j < 4; ++j)
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200251 v[j] = b[j];
252}
253
254WL_EXPORT int
255weston_matrix_invert(struct weston_matrix *inverse,
256 const struct weston_matrix *matrix)
257{
258 double LU[16]; /* column-major */
259 unsigned perm[4]; /* permutation */
260 unsigned c;
261
262 if (matrix_invert(LU, perm, matrix) < 0)
263 return -1;
264
265 weston_matrix_init(inverse);
266 for (c = 0; c < 4; ++c)
267 inverse_transform(LU, perm, &inverse->d[c * 4]);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300268 inverse->type = matrix->type;
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200269
270 return 0;
Pekka Paalanen061b7472012-01-12 15:00:57 +0200271}