blob: 4f0b6b79809d3ce213f831ed5a057559b58dcaa2 [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
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
25
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030026#include <float.h>
Pekka Paalanen668ca372012-01-12 14:30:47 +020027#include <string.h>
28#include <stdlib.h>
Pekka Paalanen75b47ec2012-01-16 14:27:00 +020029#include <math.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050030
31#ifdef IN_WESTON
Pekka Paalanen668ca372012-01-12 14:30:47 +020032#include <wayland-server.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050033#else
34#define WL_EXPORT
35#endif
Pekka Paalanen668ca372012-01-12 14:30:47 +020036
37#include "matrix.h"
38
Pekka Paalanend1f0ab62012-01-20 10:47:57 +020039
Pekka Paalanen668ca372012-01-12 14:30:47 +020040/*
41 * Matrices are stored in column-major order, that is the array indices are:
42 * 0 4 8 12
43 * 1 5 9 13
44 * 2 6 10 14
45 * 3 7 11 15
46 */
47
48WL_EXPORT void
49weston_matrix_init(struct weston_matrix *matrix)
50{
51 static const struct weston_matrix identity = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030052 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
53 .type = 0,
Pekka Paalanen668ca372012-01-12 14:30:47 +020054 };
55
56 memcpy(matrix, &identity, sizeof identity);
57}
58
59/* m <- n * m, that is, m is multiplied on the LEFT. */
60WL_EXPORT void
61weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
62{
63 struct weston_matrix tmp;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020064 const float *row, *column;
Pekka Paalanen668ca372012-01-12 14:30:47 +020065 div_t d;
66 int i, j;
67
68 for (i = 0; i < 16; i++) {
69 tmp.d[i] = 0;
70 d = div(i, 4);
71 row = m->d + d.quot * 4;
72 column = n->d + d.rem;
73 for (j = 0; j < 4; j++)
74 tmp.d[i] += row[j] * column[j * 4];
75 }
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030076 tmp.type = m->type | n->type;
Pekka Paalanen668ca372012-01-12 14:30:47 +020077 memcpy(m, &tmp, sizeof tmp);
78}
79
80WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020081weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020082{
83 struct weston_matrix translate = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030084 .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 },
85 .type = WESTON_MATRIX_TRANSFORM_TRANSLATE,
Pekka Paalanen668ca372012-01-12 14:30:47 +020086 };
87
88 weston_matrix_multiply(matrix, &translate);
89}
90
91WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020092weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020093{
94 struct weston_matrix scale = {
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +030095 .d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 },
96 .type = WESTON_MATRIX_TRANSFORM_SCALE,
Pekka Paalanen668ca372012-01-12 14:30:47 +020097 };
98
99 weston_matrix_multiply(matrix, &scale);
100}
101
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300102WL_EXPORT void
103weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin)
104{
105 struct weston_matrix translate = {
106 .d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
107 .type = WESTON_MATRIX_TRANSFORM_ROTATE,
108 };
109
110 weston_matrix_multiply(matrix, &translate);
111}
112
Pekka Paalanen668ca372012-01-12 14:30:47 +0200113/* v <- m * v */
114WL_EXPORT void
115weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
116{
117 int i, j;
118 struct weston_vector t;
119
120 for (i = 0; i < 4; i++) {
121 t.f[i] = 0;
122 for (j = 0; j < 4; j++)
123 t.f[i] += v->f[j] * matrix->d[i + j * 4];
124 }
125
126 *v = t;
127}
Pekka Paalanen061b7472012-01-12 15:00:57 +0200128
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200129static inline void
130swap_rows(double *a, double *b)
131{
132 unsigned k;
133 double tmp;
134
135 for (k = 0; k < 13; k += 4) {
136 tmp = a[k];
137 a[k] = b[k];
138 b[k] = tmp;
139 }
140}
141
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200142static inline void
143swap_unsigned(unsigned *a, unsigned *b)
144{
145 unsigned tmp;
146
147 tmp = *a;
148 *a = *b;
149 *b = tmp;
150}
151
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200152static inline unsigned
153find_pivot(double *column, unsigned k)
154{
155 unsigned p = k;
156 for (++k; k < 4; ++k)
157 if (fabs(column[p]) < fabs(column[k]))
158 p = k;
159
160 return p;
161}
162
163/*
164 * reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
165 * 3rd ed. The Johns Hopkins University Press. 1996.
166 * LU decomposition, forward and back substitution: Chapter 3.
167 */
168
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200169MATRIX_TEST_EXPORT inline int
170matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
Pekka Paalanen061b7472012-01-12 15:00:57 +0200171{
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200172 unsigned i, j, k;
173 unsigned pivot;
174 double pv;
175
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200176 for (i = 0; i < 4; ++i)
177 p[i] = i;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200178 for (i = 16; i--; )
179 A[i] = matrix->d[i];
180
181 /* LU decomposition with partial pivoting */
182 for (k = 0; k < 4; ++k) {
183 pivot = find_pivot(&A[k * 4], k);
184 if (pivot != k) {
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200185 swap_unsigned(&p[k], &p[pivot]);
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200186 swap_rows(&A[k], &A[pivot]);
187 }
188
189 pv = A[k * 4 + k];
190 if (fabs(pv) < 1e-9)
191 return -1; /* zero pivot, not invertible */
192
193 for (i = k + 1; i < 4; ++i) {
194 A[i + k * 4] /= pv;
195
196 for (j = k + 1; j < 4; ++j)
197 A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
198 }
199 }
200
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200201 return 0;
202}
203
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200204MATRIX_TEST_EXPORT inline void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200205inverse_transform(const double *LU, const unsigned *p, float *v)
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200206{
207 /* Solve A * x = v, when we have P * A = L * U.
208 * P * A * x = P * v => L * U * x = P * v
209 * Let U * x = b, then L * b = P * v.
210 */
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200211 double b[4];
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200212 unsigned j;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200213
214 /* Forward substitution, column version, solves L * b = P * v */
215 /* The diagonal of L is all ones, and not explicitly stored. */
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200216 b[0] = v[p[0]];
217 b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
218 b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
219 b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200220 b[2] -= b[1] * LU[2 + 1 * 4];
221 b[3] -= b[1] * LU[3 + 1 * 4];
222 b[3] -= b[2] * LU[3 + 2 * 4];
223
224 /* backward substitution, column version, solves U * y = b */
225#if 1
226 /* hand-unrolled, 25% faster for whole function */
227 b[3] /= LU[3 + 3 * 4];
228 b[0] -= b[3] * LU[0 + 3 * 4];
229 b[1] -= b[3] * LU[1 + 3 * 4];
230 b[2] -= b[3] * LU[2 + 3 * 4];
231
232 b[2] /= LU[2 + 2 * 4];
233 b[0] -= b[2] * LU[0 + 2 * 4];
234 b[1] -= b[2] * LU[1 + 2 * 4];
235
236 b[1] /= LU[1 + 1 * 4];
237 b[0] -= b[1] * LU[0 + 1 * 4];
238
239 b[0] /= LU[0 + 0 * 4];
240#else
241 for (j = 3; j > 0; --j) {
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200242 unsigned k;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200243 b[j] /= LU[j + j * 4];
244 for (k = 0; k < j; ++k)
245 b[k] -= b[j] * LU[k + j * 4];
246 }
247
248 b[0] /= LU[0 + 0 * 4];
249#endif
250
251 /* the result */
252 for (j = 0; j < 4; ++j)
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200253 v[j] = b[j];
254}
255
256WL_EXPORT int
257weston_matrix_invert(struct weston_matrix *inverse,
258 const struct weston_matrix *matrix)
259{
260 double LU[16]; /* column-major */
261 unsigned perm[4]; /* permutation */
262 unsigned c;
263
264 if (matrix_invert(LU, perm, matrix) < 0)
265 return -1;
266
267 weston_matrix_init(inverse);
268 for (c = 0; c < 4; ++c)
269 inverse_transform(LU, perm, &inverse->d[c * 4]);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300270 inverse->type = matrix->type;
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200271
272 return 0;
Pekka Paalanen061b7472012-01-12 15:00:57 +0200273}