blob: 98ccf4caea4abc7d6a8add29265361f437712ee7 [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>
Pekka Paalanen668ca372012-01-12 14:30:47 +020027#include <GLES2/gl2.h>
28#include <wayland-server.h>
29
30#include "matrix.h"
31
Pekka Paalanend1f0ab62012-01-20 10:47:57 +020032
Pekka Paalanen668ca372012-01-12 14:30:47 +020033/*
34 * Matrices are stored in column-major order, that is the array indices are:
35 * 0 4 8 12
36 * 1 5 9 13
37 * 2 6 10 14
38 * 3 7 11 15
39 */
40
41WL_EXPORT void
42weston_matrix_init(struct weston_matrix *matrix)
43{
44 static const struct weston_matrix identity = {
45 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
46 };
47
48 memcpy(matrix, &identity, sizeof identity);
49}
50
51/* m <- n * m, that is, m is multiplied on the LEFT. */
52WL_EXPORT void
53weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
54{
55 struct weston_matrix tmp;
56 const GLfloat *row, *column;
57 div_t d;
58 int i, j;
59
60 for (i = 0; i < 16; i++) {
61 tmp.d[i] = 0;
62 d = div(i, 4);
63 row = m->d + d.quot * 4;
64 column = n->d + d.rem;
65 for (j = 0; j < 4; j++)
66 tmp.d[i] += row[j] * column[j * 4];
67 }
68 memcpy(m, &tmp, sizeof tmp);
69}
70
71WL_EXPORT void
72weston_matrix_translate(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
73{
74 struct weston_matrix translate = {
75 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
76 };
77
78 weston_matrix_multiply(matrix, &translate);
79}
80
81WL_EXPORT void
82weston_matrix_scale(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
83{
84 struct weston_matrix scale = {
85 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
86 };
87
88 weston_matrix_multiply(matrix, &scale);
89}
90
91/* v <- m * v */
92WL_EXPORT void
93weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
94{
95 int i, j;
96 struct weston_vector t;
97
98 for (i = 0; i < 4; i++) {
99 t.f[i] = 0;
100 for (j = 0; j < 4; j++)
101 t.f[i] += v->f[j] * matrix->d[i + j * 4];
102 }
103
104 *v = t;
105}
Pekka Paalanen061b7472012-01-12 15:00:57 +0200106
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200107static inline void
108swap_rows(double *a, double *b)
109{
110 unsigned k;
111 double tmp;
112
113 for (k = 0; k < 13; k += 4) {
114 tmp = a[k];
115 a[k] = b[k];
116 b[k] = tmp;
117 }
118}
119
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200120static inline void
121swap_unsigned(unsigned *a, unsigned *b)
122{
123 unsigned tmp;
124
125 tmp = *a;
126 *a = *b;
127 *b = tmp;
128}
129
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200130static inline unsigned
131find_pivot(double *column, unsigned k)
132{
133 unsigned p = k;
134 for (++k; k < 4; ++k)
135 if (fabs(column[p]) < fabs(column[k]))
136 p = k;
137
138 return p;
139}
140
141/*
142 * reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
143 * 3rd ed. The Johns Hopkins University Press. 1996.
144 * LU decomposition, forward and back substitution: Chapter 3.
145 */
146
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200147MATRIX_TEST_EXPORT inline int
148matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
Pekka Paalanen061b7472012-01-12 15:00:57 +0200149{
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200150 unsigned i, j, k;
151 unsigned pivot;
152 double pv;
153
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200154 for (i = 0; i < 4; ++i)
155 p[i] = i;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200156 for (i = 16; i--; )
157 A[i] = matrix->d[i];
158
159 /* LU decomposition with partial pivoting */
160 for (k = 0; k < 4; ++k) {
161 pivot = find_pivot(&A[k * 4], k);
162 if (pivot != k) {
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200163 swap_unsigned(&p[k], &p[pivot]);
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200164 swap_rows(&A[k], &A[pivot]);
165 }
166
167 pv = A[k * 4 + k];
168 if (fabs(pv) < 1e-9)
169 return -1; /* zero pivot, not invertible */
170
171 for (i = k + 1; i < 4; ++i) {
172 A[i + k * 4] /= pv;
173
174 for (j = k + 1; j < 4; ++j)
175 A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
176 }
177 }
178
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200179 return 0;
180}
181
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200182MATRIX_TEST_EXPORT inline void
183inverse_transform(const double *LU, const unsigned *p, GLfloat *v)
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200184{
185 /* Solve A * x = v, when we have P * A = L * U.
186 * P * A * x = P * v => L * U * x = P * v
187 * Let U * x = b, then L * b = P * v.
188 */
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200189 double b[4];
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200190 unsigned j;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200191
192 /* Forward substitution, column version, solves L * b = P * v */
193 /* The diagonal of L is all ones, and not explicitly stored. */
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200194 b[0] = v[p[0]];
195 b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
196 b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
197 b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200198 b[2] -= b[1] * LU[2 + 1 * 4];
199 b[3] -= b[1] * LU[3 + 1 * 4];
200 b[3] -= b[2] * LU[3 + 2 * 4];
201
202 /* backward substitution, column version, solves U * y = b */
203#if 1
204 /* hand-unrolled, 25% faster for whole function */
205 b[3] /= LU[3 + 3 * 4];
206 b[0] -= b[3] * LU[0 + 3 * 4];
207 b[1] -= b[3] * LU[1 + 3 * 4];
208 b[2] -= b[3] * LU[2 + 3 * 4];
209
210 b[2] /= LU[2 + 2 * 4];
211 b[0] -= b[2] * LU[0 + 2 * 4];
212 b[1] -= b[2] * LU[1 + 2 * 4];
213
214 b[1] /= LU[1 + 1 * 4];
215 b[0] -= b[1] * LU[0 + 1 * 4];
216
217 b[0] /= LU[0 + 0 * 4];
218#else
219 for (j = 3; j > 0; --j) {
Pekka Paalanen4520d5c2012-01-16 15:04:28 +0200220 unsigned k;
Pekka Paalanen75b47ec2012-01-16 14:27:00 +0200221 b[j] /= LU[j + j * 4];
222 for (k = 0; k < j; ++k)
223 b[k] -= b[j] * LU[k + j * 4];
224 }
225
226 b[0] /= LU[0 + 0 * 4];
227#endif
228
229 /* the result */
230 for (j = 0; j < 4; ++j)
Pekka Paalanend1f0ab62012-01-20 10:47:57 +0200231 v[j] = b[j];
232}
233
234WL_EXPORT int
235weston_matrix_invert(struct weston_matrix *inverse,
236 const struct weston_matrix *matrix)
237{
238 double LU[16]; /* column-major */
239 unsigned perm[4]; /* permutation */
240 unsigned c;
241
242 if (matrix_invert(LU, perm, matrix) < 0)
243 return -1;
244
245 weston_matrix_init(inverse);
246 for (c = 0; c < 4; ++c)
247 inverse_transform(LU, perm, &inverse->d[c * 4]);
248
249 return 0;
Pekka Paalanen061b7472012-01-12 15:00:57 +0200250}