blob: 11b5b959a6b9eb31b72882ea1f2ae29c58a5eb2b [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
24#include <string.h>
25#include <stdlib.h>
Pekka Paalanen75b47ec2012-01-16 14:27:00 +020026#include <math.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050027
28#ifdef IN_WESTON
Pekka Paalanen668ca372012-01-12 14:30:47 +020029#include <wayland-server.h>
Kristian Høgsberg3a8d3f22012-12-07 15:00:32 -050030#else
31#define WL_EXPORT
32#endif
Pekka Paalanen668ca372012-01-12 14:30:47 +020033
34#include "matrix.h"
35
Pekka Paalanend1f0ab62012-01-20 10:47:57 +020036
Pekka Paalanen668ca372012-01-12 14:30:47 +020037/*
38 * Matrices are stored in column-major order, that is the array indices are:
39 * 0 4 8 12
40 * 1 5 9 13
41 * 2 6 10 14
42 * 3 7 11 15
43 */
44
45WL_EXPORT void
46weston_matrix_init(struct weston_matrix *matrix)
47{
48 static const struct weston_matrix identity = {
49 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
50 };
51
52 memcpy(matrix, &identity, sizeof identity);
53}
54
55/* m <- n * m, that is, m is multiplied on the LEFT. */
56WL_EXPORT void
57weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
58{
59 struct weston_matrix tmp;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020060 const float *row, *column;
Pekka Paalanen668ca372012-01-12 14:30:47 +020061 div_t d;
62 int i, j;
63
64 for (i = 0; i < 16; i++) {
65 tmp.d[i] = 0;
66 d = div(i, 4);
67 row = m->d + d.quot * 4;
68 column = n->d + d.rem;
69 for (j = 0; j < 4; j++)
70 tmp.d[i] += row[j] * column[j * 4];
71 }
72 memcpy(m, &tmp, sizeof tmp);
73}
74
75WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020076weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020077{
78 struct weston_matrix translate = {
79 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
80 };
81
82 weston_matrix_multiply(matrix, &translate);
83}
84
85WL_EXPORT void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +020086weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
Pekka Paalanen668ca372012-01-12 14:30:47 +020087{
88 struct weston_matrix scale = {
89 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
90 };
91
92 weston_matrix_multiply(matrix, &scale);
93}
94
95/* v <- m * v */
96WL_EXPORT void
97weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
98{
99 int i, j;
100 struct weston_vector t;
101
102 for (i = 0; i < 4; i++) {
103 t.f[i] = 0;
104 for (j = 0; j < 4; j++)
105 t.f[i] += v->f[j] * matrix->d[i + j * 4];
106 }
107
108 *v = t;
109}
Pekka Paalanen061b7472012-01-12 15:00:57 +0200110
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200111static inline void
112swap_rows(double *a, double *b)
113{
114 unsigned k;
115 double tmp;
116
117 for (k = 0; k < 13; k += 4) {
118 tmp = a[k];
119 a[k] = b[k];
120 b[k] = tmp;
121 }
122}
123
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200124static inline void
125swap_unsigned(unsigned *a, unsigned *b)
126{
127 unsigned tmp;
128
129 tmp = *a;
130 *a = *b;
131 *b = tmp;
132}
133
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200134static inline unsigned
135find_pivot(double *column, unsigned k)
136{
137 unsigned p = k;
138 for (++k; k < 4; ++k)
139 if (fabs(column[p]) < fabs(column[k]))
140 p = k;
141
142 return p;
143}
144
145/*
146 * reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
147 * 3rd ed. The Johns Hopkins University Press. 1996.
148 * LU decomposition, forward and back substitution: Chapter 3.
149 */
150
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200151MATRIX_TEST_EXPORT inline int
152matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
Pekka Paalanen061b7472012-01-12 15:00:57 +0200153{
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200154 unsigned i, j, k;
155 unsigned pivot;
156 double pv;
157
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200158 for (i = 0; i < 4; ++i)
159 p[i] = i;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200160 for (i = 16; i--; )
161 A[i] = matrix->d[i];
162
163 /* LU decomposition with partial pivoting */
164 for (k = 0; k < 4; ++k) {
165 pivot = find_pivot(&A[k * 4], k);
166 if (pivot != k) {
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200167 swap_unsigned(&p[k], &p[pivot]);
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200168 swap_rows(&A[k], &A[pivot]);
169 }
170
171 pv = A[k * 4 + k];
172 if (fabs(pv) < 1e-9)
173 return -1; /* zero pivot, not invertible */
174
175 for (i = k + 1; i < 4; ++i) {
176 A[i + k * 4] /= pv;
177
178 for (j = k + 1; j < 4; ++j)
179 A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
180 }
181 }
182
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200183 return 0;
184}
185
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200186MATRIX_TEST_EXPORT inline void
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200187inverse_transform(const double *LU, const unsigned *p, float *v)
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200188{
189 /* Solve A * x = v, when we have P * A = L * U.
190 * P * A * x = P * v => L * U * x = P * v
191 * Let U * x = b, then L * b = P * v.
192 */
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200193 double b[4];
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200194 unsigned j;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200195
196 /* Forward substitution, column version, solves L * b = P * v */
197 /* The diagonal of L is all ones, and not explicitly stored. */
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200198 b[0] = v[p[0]];
199 b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
200 b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
201 b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200202 b[2] -= b[1] * LU[2 + 1 * 4];
203 b[3] -= b[1] * LU[3 + 1 * 4];
204 b[3] -= b[2] * LU[3 + 2 * 4];
205
206 /* backward substitution, column version, solves U * y = b */
207#if 1
208 /* hand-unrolled, 25% faster for whole function */
209 b[3] /= LU[3 + 3 * 4];
210 b[0] -= b[3] * LU[0 + 3 * 4];
211 b[1] -= b[3] * LU[1 + 3 * 4];
212 b[2] -= b[3] * LU[2 + 3 * 4];
213
214 b[2] /= LU[2 + 2 * 4];
215 b[0] -= b[2] * LU[0 + 2 * 4];
216 b[1] -= b[2] * LU[1 + 2 * 4];
217
218 b[1] /= LU[1 + 1 * 4];
219 b[0] -= b[1] * LU[0 + 1 * 4];
220
221 b[0] /= LU[0 + 0 * 4];
222#else
223 for (j = 3; j > 0; --j) {
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200224 unsigned k;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200225 b[j] /= LU[j + j * 4];
226 for (k = 0; k < j; ++k)
227 b[k] -= b[j] * LU[k + j * 4];
228 }
229
230 b[0] /= LU[0 + 0 * 4];
231#endif
232
233 /* the result */
234 for (j = 0; j < 4; ++j)
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200235 v[j] = b[j];
236}
237
238WL_EXPORT int
239weston_matrix_invert(struct weston_matrix *inverse,
240 const struct weston_matrix *matrix)
241{
242 double LU[16]; /* column-major */
243 unsigned perm[4]; /* permutation */
244 unsigned c;
245
246 if (matrix_invert(LU, perm, matrix) < 0)
247 return -1;
248
249 weston_matrix_init(inverse);
250 for (c = 0; c < 4; ++c)
251 inverse_transform(LU, perm, &inverse->d[c * 4]);
252
253 return 0;
Pekka Paalanen061b7472012-01-12 15:00:57 +0200254}