Pekka Paalanen | 668ca37 | 2012-01-12 14:30:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Collabora, Ltd. |
Pekka Paalanen | 668ca37 | 2012-01-12 14:30:47 +0200 | [diff] [blame] | 4 | * |
| 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 Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 26 | #include <math.h> |
Pekka Paalanen | 668ca37 | 2012-01-12 14:30:47 +0200 | [diff] [blame] | 27 | #include <GLES2/gl2.h> |
| 28 | #include <wayland-server.h> |
| 29 | |
| 30 | #include "matrix.h" |
| 31 | |
| 32 | /* |
| 33 | * Matrices are stored in column-major order, that is the array indices are: |
| 34 | * 0 4 8 12 |
| 35 | * 1 5 9 13 |
| 36 | * 2 6 10 14 |
| 37 | * 3 7 11 15 |
| 38 | */ |
| 39 | |
| 40 | WL_EXPORT void |
| 41 | weston_matrix_init(struct weston_matrix *matrix) |
| 42 | { |
| 43 | static const struct weston_matrix identity = { |
| 44 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } |
| 45 | }; |
| 46 | |
| 47 | memcpy(matrix, &identity, sizeof identity); |
| 48 | } |
| 49 | |
| 50 | /* m <- n * m, that is, m is multiplied on the LEFT. */ |
| 51 | WL_EXPORT void |
| 52 | weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n) |
| 53 | { |
| 54 | struct weston_matrix tmp; |
| 55 | const GLfloat *row, *column; |
| 56 | div_t d; |
| 57 | int i, j; |
| 58 | |
| 59 | for (i = 0; i < 16; i++) { |
| 60 | tmp.d[i] = 0; |
| 61 | d = div(i, 4); |
| 62 | row = m->d + d.quot * 4; |
| 63 | column = n->d + d.rem; |
| 64 | for (j = 0; j < 4; j++) |
| 65 | tmp.d[i] += row[j] * column[j * 4]; |
| 66 | } |
| 67 | memcpy(m, &tmp, sizeof tmp); |
| 68 | } |
| 69 | |
| 70 | WL_EXPORT void |
| 71 | weston_matrix_translate(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
| 72 | { |
| 73 | struct weston_matrix translate = { |
| 74 | { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 } |
| 75 | }; |
| 76 | |
| 77 | weston_matrix_multiply(matrix, &translate); |
| 78 | } |
| 79 | |
| 80 | WL_EXPORT void |
| 81 | weston_matrix_scale(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z) |
| 82 | { |
| 83 | struct weston_matrix scale = { |
| 84 | { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 } |
| 85 | }; |
| 86 | |
| 87 | weston_matrix_multiply(matrix, &scale); |
| 88 | } |
| 89 | |
| 90 | /* v <- m * v */ |
| 91 | WL_EXPORT void |
| 92 | weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v) |
| 93 | { |
| 94 | int i, j; |
| 95 | struct weston_vector t; |
| 96 | |
| 97 | for (i = 0; i < 4; i++) { |
| 98 | t.f[i] = 0; |
| 99 | for (j = 0; j < 4; j++) |
| 100 | t.f[i] += v->f[j] * matrix->d[i + j * 4]; |
| 101 | } |
| 102 | |
| 103 | *v = t; |
| 104 | } |
Pekka Paalanen | 061b747 | 2012-01-12 15:00:57 +0200 | [diff] [blame] | 105 | |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 106 | static inline void |
| 107 | swap_rows(double *a, double *b) |
| 108 | { |
| 109 | unsigned k; |
| 110 | double tmp; |
| 111 | |
| 112 | for (k = 0; k < 13; k += 4) { |
| 113 | tmp = a[k]; |
| 114 | a[k] = b[k]; |
| 115 | b[k] = tmp; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static inline unsigned |
| 120 | find_pivot(double *column, unsigned k) |
| 121 | { |
| 122 | unsigned p = k; |
| 123 | for (++k; k < 4; ++k) |
| 124 | if (fabs(column[p]) < fabs(column[k])) |
| 125 | p = k; |
| 126 | |
| 127 | return p; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * reference: Gene H. Golub and Charles F. van Loan. Matrix computations. |
| 132 | * 3rd ed. The Johns Hopkins University Press. 1996. |
| 133 | * LU decomposition, forward and back substitution: Chapter 3. |
| 134 | */ |
| 135 | |
Pekka Paalanen | 061b747 | 2012-01-12 15:00:57 +0200 | [diff] [blame] | 136 | WL_EXPORT int |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 137 | weston_matrix_invert(struct weston_inverse_matrix *inverse, |
Pekka Paalanen | 061b747 | 2012-01-12 15:00:57 +0200 | [diff] [blame] | 138 | const struct weston_matrix *matrix) |
| 139 | { |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 140 | double A[16]; |
| 141 | unsigned p[4] = { 0, 1, 2, 3 }; |
| 142 | unsigned i, j, k; |
| 143 | unsigned pivot; |
| 144 | double pv; |
| 145 | |
| 146 | for (i = 16; i--; ) |
| 147 | A[i] = matrix->d[i]; |
| 148 | |
| 149 | /* LU decomposition with partial pivoting */ |
| 150 | for (k = 0; k < 4; ++k) { |
| 151 | pivot = find_pivot(&A[k * 4], k); |
| 152 | if (pivot != k) { |
| 153 | unsigned tmp = p[k]; |
| 154 | p[k] = p[pivot]; |
| 155 | p[pivot] = tmp; |
| 156 | swap_rows(&A[k], &A[pivot]); |
| 157 | } |
| 158 | |
| 159 | pv = A[k * 4 + k]; |
| 160 | if (fabs(pv) < 1e-9) |
| 161 | return -1; /* zero pivot, not invertible */ |
| 162 | |
| 163 | for (i = k + 1; i < 4; ++i) { |
| 164 | A[i + k * 4] /= pv; |
| 165 | |
| 166 | for (j = k + 1; j < 4; ++j) |
| 167 | A[i + j * 4] -= A[i + k * 4] * A[k + j * 4]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | memcpy(inverse->LU, A, sizeof(A)); |
| 172 | memcpy(inverse->p, p, sizeof(p)); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | WL_EXPORT void |
| 177 | weston_matrix_inverse_transform(struct weston_inverse_matrix *inverse, |
| 178 | struct weston_vector *v) |
| 179 | { |
| 180 | /* Solve A * x = v, when we have P * A = L * U. |
| 181 | * P * A * x = P * v => L * U * x = P * v |
| 182 | * Let U * x = b, then L * b = P * v. |
| 183 | */ |
| 184 | unsigned *p = inverse->p; |
| 185 | double *LU = inverse->LU; |
| 186 | double b[4]; |
Pekka Paalanen | 4520d5c | 2012-01-16 15:04:28 +0200 | [diff] [blame] | 187 | unsigned j; |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 188 | |
| 189 | /* Forward substitution, column version, solves L * b = P * v */ |
| 190 | /* The diagonal of L is all ones, and not explicitly stored. */ |
| 191 | b[0] = v->f[p[0]]; |
| 192 | b[1] = (double)v->f[p[1]] - b[0] * LU[1 + 0 * 4]; |
| 193 | b[2] = (double)v->f[p[2]] - b[0] * LU[2 + 0 * 4]; |
| 194 | b[3] = (double)v->f[p[3]] - b[0] * LU[3 + 0 * 4]; |
| 195 | b[2] -= b[1] * LU[2 + 1 * 4]; |
| 196 | b[3] -= b[1] * LU[3 + 1 * 4]; |
| 197 | b[3] -= b[2] * LU[3 + 2 * 4]; |
| 198 | |
| 199 | /* backward substitution, column version, solves U * y = b */ |
| 200 | #if 1 |
| 201 | /* hand-unrolled, 25% faster for whole function */ |
| 202 | b[3] /= LU[3 + 3 * 4]; |
| 203 | b[0] -= b[3] * LU[0 + 3 * 4]; |
| 204 | b[1] -= b[3] * LU[1 + 3 * 4]; |
| 205 | b[2] -= b[3] * LU[2 + 3 * 4]; |
| 206 | |
| 207 | b[2] /= LU[2 + 2 * 4]; |
| 208 | b[0] -= b[2] * LU[0 + 2 * 4]; |
| 209 | b[1] -= b[2] * LU[1 + 2 * 4]; |
| 210 | |
| 211 | b[1] /= LU[1 + 1 * 4]; |
| 212 | b[0] -= b[1] * LU[0 + 1 * 4]; |
| 213 | |
| 214 | b[0] /= LU[0 + 0 * 4]; |
| 215 | #else |
| 216 | for (j = 3; j > 0; --j) { |
Pekka Paalanen | 4520d5c | 2012-01-16 15:04:28 +0200 | [diff] [blame] | 217 | unsigned k; |
Pekka Paalanen | 75b47ec | 2012-01-16 14:27:00 +0200 | [diff] [blame] | 218 | b[j] /= LU[j + j * 4]; |
| 219 | for (k = 0; k < j; ++k) |
| 220 | b[k] -= b[j] * LU[k + j * 4]; |
| 221 | } |
| 222 | |
| 223 | b[0] /= LU[0 + 0 * 4]; |
| 224 | #endif |
| 225 | |
| 226 | /* the result */ |
| 227 | for (j = 0; j < 4; ++j) |
| 228 | v->f[j] = b[j]; |
Pekka Paalanen | 061b747 | 2012-01-12 15:00:57 +0200 | [diff] [blame] | 229 | } |