2012-01-12 16:30:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Intel Corporation
|
2012-01-16 16:27:00 +04:00
|
|
|
* Copyright © 2012 Collabora, Ltd.
|
2012-01-12 16:30:47 +04:00
|
|
|
*
|
2015-06-12 00:20:17 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
2012-01-12 16:30:47 +04:00
|
|
|
*
|
2015-06-12 00:20:17 +03:00
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2012-01-12 16:30:47 +04:00
|
|
|
*/
|
|
|
|
|
2013-05-22 19:03:19 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-01-28 23:40:28 +04:00
|
|
|
#include <float.h>
|
2012-01-12 16:30:47 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-16 16:27:00 +04:00
|
|
|
#include <math.h>
|
2012-12-08 00:00:32 +04:00
|
|
|
|
2018-12-20 12:53:21 +03:00
|
|
|
#ifdef UNIT_TEST
|
2012-12-08 00:00:32 +04:00
|
|
|
#define WL_EXPORT
|
2018-12-20 12:53:21 +03:00
|
|
|
#else
|
|
|
|
#include <wayland-server.h>
|
2012-12-08 00:00:32 +04:00
|
|
|
#endif
|
2012-01-12 16:30:47 +04:00
|
|
|
|
2019-04-04 13:47:40 +03:00
|
|
|
#include <libweston/matrix.h>
|
2012-01-12 16:30:47 +04:00
|
|
|
|
2012-01-20 12:47:57 +04:00
|
|
|
|
2012-01-12 16:30:47 +04:00
|
|
|
/*
|
|
|
|
* Matrices are stored in column-major order, that is the array indices are:
|
|
|
|
* 0 4 8 12
|
|
|
|
* 1 5 9 13
|
|
|
|
* 2 6 10 14
|
|
|
|
* 3 7 11 15
|
|
|
|
*/
|
|
|
|
|
|
|
|
WL_EXPORT void
|
|
|
|
weston_matrix_init(struct weston_matrix *matrix)
|
|
|
|
{
|
|
|
|
static const struct weston_matrix identity = {
|
2013-01-28 23:40:28 +04:00
|
|
|
.d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
|
|
|
.type = 0,
|
2012-01-12 16:30:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
memcpy(matrix, &identity, sizeof identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* m <- n * m, that is, m is multiplied on the LEFT. */
|
|
|
|
WL_EXPORT void
|
|
|
|
weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
|
|
|
|
{
|
|
|
|
struct weston_matrix tmp;
|
2012-09-30 04:57:21 +04:00
|
|
|
const float *row, *column;
|
2012-01-12 16:30:47 +04:00
|
|
|
div_t d;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
tmp.d[i] = 0;
|
|
|
|
d = div(i, 4);
|
|
|
|
row = m->d + d.quot * 4;
|
|
|
|
column = n->d + d.rem;
|
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
tmp.d[i] += row[j] * column[j * 4];
|
|
|
|
}
|
2013-01-28 23:40:28 +04:00
|
|
|
tmp.type = m->type | n->type;
|
2012-01-12 16:30:47 +04:00
|
|
|
memcpy(m, &tmp, sizeof tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_EXPORT void
|
2012-09-30 04:57:21 +04:00
|
|
|
weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z)
|
2012-01-12 16:30:47 +04:00
|
|
|
{
|
|
|
|
struct weston_matrix translate = {
|
2013-01-28 23:40:28 +04:00
|
|
|
.d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 },
|
|
|
|
.type = WESTON_MATRIX_TRANSFORM_TRANSLATE,
|
2012-01-12 16:30:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
weston_matrix_multiply(matrix, &translate);
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_EXPORT void
|
2012-09-30 04:57:21 +04:00
|
|
|
weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z)
|
2012-01-12 16:30:47 +04:00
|
|
|
{
|
|
|
|
struct weston_matrix scale = {
|
2013-01-28 23:40:28 +04:00
|
|
|
.d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 },
|
|
|
|
.type = WESTON_MATRIX_TRANSFORM_SCALE,
|
2012-01-12 16:30:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
weston_matrix_multiply(matrix, &scale);
|
|
|
|
}
|
|
|
|
|
2013-01-28 23:40:28 +04:00
|
|
|
WL_EXPORT void
|
|
|
|
weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin)
|
|
|
|
{
|
|
|
|
struct weston_matrix translate = {
|
|
|
|
.d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
|
|
|
.type = WESTON_MATRIX_TRANSFORM_ROTATE,
|
|
|
|
};
|
|
|
|
|
|
|
|
weston_matrix_multiply(matrix, &translate);
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:30:47 +04:00
|
|
|
/* v <- m * v */
|
|
|
|
WL_EXPORT void
|
|
|
|
weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct weston_vector t;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
t.f[i] = 0;
|
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
t.f[i] += v->f[j] * matrix->d[i + j * 4];
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = t;
|
|
|
|
}
|
2012-01-12 17:00:57 +04:00
|
|
|
|
2012-01-16 16:27:00 +04:00
|
|
|
static inline void
|
|
|
|
swap_rows(double *a, double *b)
|
|
|
|
{
|
|
|
|
unsigned k;
|
|
|
|
double tmp;
|
|
|
|
|
|
|
|
for (k = 0; k < 13; k += 4) {
|
|
|
|
tmp = a[k];
|
|
|
|
a[k] = b[k];
|
|
|
|
b[k] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 12:47:57 +04:00
|
|
|
static inline void
|
|
|
|
swap_unsigned(unsigned *a, unsigned *b)
|
|
|
|
{
|
|
|
|
unsigned tmp;
|
|
|
|
|
|
|
|
tmp = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = tmp;
|
|
|
|
}
|
|
|
|
|
2012-01-16 16:27:00 +04:00
|
|
|
static inline unsigned
|
|
|
|
find_pivot(double *column, unsigned k)
|
|
|
|
{
|
|
|
|
unsigned p = k;
|
|
|
|
for (++k; k < 4; ++k)
|
|
|
|
if (fabs(column[p]) < fabs(column[k]))
|
|
|
|
p = k;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reference: Gene H. Golub and Charles F. van Loan. Matrix computations.
|
|
|
|
* 3rd ed. The Johns Hopkins University Press. 1996.
|
|
|
|
* LU decomposition, forward and back substitution: Chapter 3.
|
|
|
|
*/
|
|
|
|
|
2012-01-20 12:47:57 +04:00
|
|
|
MATRIX_TEST_EXPORT inline int
|
|
|
|
matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix)
|
2012-01-12 17:00:57 +04:00
|
|
|
{
|
2012-01-16 16:27:00 +04:00
|
|
|
unsigned i, j, k;
|
|
|
|
unsigned pivot;
|
|
|
|
double pv;
|
|
|
|
|
2012-01-20 12:47:57 +04:00
|
|
|
for (i = 0; i < 4; ++i)
|
|
|
|
p[i] = i;
|
2012-01-16 16:27:00 +04:00
|
|
|
for (i = 16; i--; )
|
|
|
|
A[i] = matrix->d[i];
|
|
|
|
|
|
|
|
/* LU decomposition with partial pivoting */
|
|
|
|
for (k = 0; k < 4; ++k) {
|
|
|
|
pivot = find_pivot(&A[k * 4], k);
|
|
|
|
if (pivot != k) {
|
2012-01-20 12:47:57 +04:00
|
|
|
swap_unsigned(&p[k], &p[pivot]);
|
2012-01-16 16:27:00 +04:00
|
|
|
swap_rows(&A[k], &A[pivot]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pv = A[k * 4 + k];
|
|
|
|
if (fabs(pv) < 1e-9)
|
|
|
|
return -1; /* zero pivot, not invertible */
|
|
|
|
|
|
|
|
for (i = k + 1; i < 4; ++i) {
|
|
|
|
A[i + k * 4] /= pv;
|
|
|
|
|
|
|
|
for (j = k + 1; j < 4; ++j)
|
|
|
|
A[i + j * 4] -= A[i + k * 4] * A[k + j * 4];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-20 12:47:57 +04:00
|
|
|
MATRIX_TEST_EXPORT inline void
|
2012-09-30 04:57:21 +04:00
|
|
|
inverse_transform(const double *LU, const unsigned *p, float *v)
|
2012-01-16 16:27:00 +04:00
|
|
|
{
|
|
|
|
/* Solve A * x = v, when we have P * A = L * U.
|
|
|
|
* P * A * x = P * v => L * U * x = P * v
|
|
|
|
* Let U * x = b, then L * b = P * v.
|
|
|
|
*/
|
|
|
|
double b[4];
|
tests: add matrix-test
Add a new directory tests/ for unit test applications. This directory
will be built only if --enable-tests is given to ./configure.
Add matrix-test application. It excercises especially the
weston_matrix_invert() and weston_matrix_inverse_transform() functions.
It has one test for correctness and precision, and other tests for
measuring the speed of various matrix operations.
For the record, the correctness test prints:
a random matrix:
1.112418e-02 2.628150e+00 8.205844e+02 -1.147526e-04
4.943677e-04 -1.117819e-04 -9.158849e-06 3.678122e-02
7.915063e-03 -3.093254e-04 -4.376583e+02 3.424706e-02
-2.504038e+02 2.481788e+03 -7.545445e+01 1.752909e-03
The matrix multiplied by its inverse, error:
0.000000e+00 -0.000000e+00 -0.000000e+00 -0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
-0.000000e+00 -0.000000e+00 0.000000e+00 -0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
max abs error: 0, original determinant 11595.2
Running a test loop for 10 seconds...
test fail, det: -0.00464805, error sup: inf
test fail, det: -0.0424053, error sup: 1.30787e-06
test fail, det: 5.15191, error sup: 1.15956e-06
tests: 6791767 ok, 1 not invertible but ok, 3 failed.
Total: 6791771 iterations.
These results are expected with the current precision thresholds in
src/matrix.c and tests/matrix-test.c. The random number generator is
seeded with a constant, so the random numbers should be the same on
every run. Machine speed and scheduling affect how many iterations are
run.
Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
2012-01-16 17:04:28 +04:00
|
|
|
unsigned j;
|
2012-01-16 16:27:00 +04:00
|
|
|
|
|
|
|
/* Forward substitution, column version, solves L * b = P * v */
|
|
|
|
/* The diagonal of L is all ones, and not explicitly stored. */
|
2012-01-20 12:47:57 +04:00
|
|
|
b[0] = v[p[0]];
|
|
|
|
b[1] = (double)v[p[1]] - b[0] * LU[1 + 0 * 4];
|
|
|
|
b[2] = (double)v[p[2]] - b[0] * LU[2 + 0 * 4];
|
|
|
|
b[3] = (double)v[p[3]] - b[0] * LU[3 + 0 * 4];
|
2012-01-16 16:27:00 +04:00
|
|
|
b[2] -= b[1] * LU[2 + 1 * 4];
|
|
|
|
b[3] -= b[1] * LU[3 + 1 * 4];
|
|
|
|
b[3] -= b[2] * LU[3 + 2 * 4];
|
|
|
|
|
|
|
|
/* backward substitution, column version, solves U * y = b */
|
|
|
|
#if 1
|
|
|
|
/* hand-unrolled, 25% faster for whole function */
|
|
|
|
b[3] /= LU[3 + 3 * 4];
|
|
|
|
b[0] -= b[3] * LU[0 + 3 * 4];
|
|
|
|
b[1] -= b[3] * LU[1 + 3 * 4];
|
|
|
|
b[2] -= b[3] * LU[2 + 3 * 4];
|
|
|
|
|
|
|
|
b[2] /= LU[2 + 2 * 4];
|
|
|
|
b[0] -= b[2] * LU[0 + 2 * 4];
|
|
|
|
b[1] -= b[2] * LU[1 + 2 * 4];
|
|
|
|
|
|
|
|
b[1] /= LU[1 + 1 * 4];
|
|
|
|
b[0] -= b[1] * LU[0 + 1 * 4];
|
|
|
|
|
|
|
|
b[0] /= LU[0 + 0 * 4];
|
|
|
|
#else
|
|
|
|
for (j = 3; j > 0; --j) {
|
tests: add matrix-test
Add a new directory tests/ for unit test applications. This directory
will be built only if --enable-tests is given to ./configure.
Add matrix-test application. It excercises especially the
weston_matrix_invert() and weston_matrix_inverse_transform() functions.
It has one test for correctness and precision, and other tests for
measuring the speed of various matrix operations.
For the record, the correctness test prints:
a random matrix:
1.112418e-02 2.628150e+00 8.205844e+02 -1.147526e-04
4.943677e-04 -1.117819e-04 -9.158849e-06 3.678122e-02
7.915063e-03 -3.093254e-04 -4.376583e+02 3.424706e-02
-2.504038e+02 2.481788e+03 -7.545445e+01 1.752909e-03
The matrix multiplied by its inverse, error:
0.000000e+00 -0.000000e+00 -0.000000e+00 -0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
-0.000000e+00 -0.000000e+00 0.000000e+00 -0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
max abs error: 0, original determinant 11595.2
Running a test loop for 10 seconds...
test fail, det: -0.00464805, error sup: inf
test fail, det: -0.0424053, error sup: 1.30787e-06
test fail, det: 5.15191, error sup: 1.15956e-06
tests: 6791767 ok, 1 not invertible but ok, 3 failed.
Total: 6791771 iterations.
These results are expected with the current precision thresholds in
src/matrix.c and tests/matrix-test.c. The random number generator is
seeded with a constant, so the random numbers should be the same on
every run. Machine speed and scheduling affect how many iterations are
run.
Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
2012-01-16 17:04:28 +04:00
|
|
|
unsigned k;
|
2012-01-16 16:27:00 +04:00
|
|
|
b[j] /= LU[j + j * 4];
|
|
|
|
for (k = 0; k < j; ++k)
|
|
|
|
b[k] -= b[j] * LU[k + j * 4];
|
|
|
|
}
|
|
|
|
|
|
|
|
b[0] /= LU[0 + 0 * 4];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* the result */
|
|
|
|
for (j = 0; j < 4; ++j)
|
2012-01-20 12:47:57 +04:00
|
|
|
v[j] = b[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
WL_EXPORT int
|
|
|
|
weston_matrix_invert(struct weston_matrix *inverse,
|
|
|
|
const struct weston_matrix *matrix)
|
|
|
|
{
|
|
|
|
double LU[16]; /* column-major */
|
|
|
|
unsigned perm[4]; /* permutation */
|
|
|
|
unsigned c;
|
|
|
|
|
|
|
|
if (matrix_invert(LU, perm, matrix) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
weston_matrix_init(inverse);
|
|
|
|
for (c = 0; c < 4; ++c)
|
|
|
|
inverse_transform(LU, perm, &inverse->d[c * 4]);
|
2013-01-28 23:40:28 +04:00
|
|
|
inverse->type = matrix->type;
|
2012-01-20 12:47:57 +04:00
|
|
|
|
|
|
|
return 0;
|
2012-01-12 17:00:57 +04:00
|
|
|
}
|