forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.c
More file actions
80 lines (67 loc) · 2.49 KB
/
Copy pathmatrices.c
File metadata and controls
80 lines (67 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
This file is part of darktable,
copyright (c) 2010-2011 Henrik Andersson.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/matrices.h"
/** inverts the given 3x3 matrix */
int
mat3inv (float * const dst, const float *const src)
{
#define A(y, x) src[(y - 1) * 3 + (x - 1)]
#define B(y, x) dst[(y - 1) * 3 + (x - 1)]
const float det =
A(1, 1) * (A(3, 3) * A(2, 2) - A(3, 2) * A(2, 3)) -
A(2, 1) * (A(3, 3) * A(1, 2) - A(3, 2) * A(1, 3)) +
A(3, 1) * (A(2, 3) * A(1, 2) - A(2, 2) * A(1, 3));
const float epsilon = 1e-7f;
if (fabsf(det) < epsilon) return 1;
const float invDet = 1.f / det;
B(1, 1) = invDet * (A(3, 3) * A(2, 2) - A(3, 2) * A(2, 3));
B(1, 2) = -invDet * (A(3, 3) * A(1, 2) - A(3, 2) * A(1, 3));
B(1, 3) = invDet * (A(2, 3) * A(1, 2) - A(2, 2) * A(1, 3));
B(2, 1) = -invDet * (A(3, 3) * A(2, 1) - A(3, 1) * A(2, 3));
B(2, 2) = invDet * (A(3, 3) * A(1, 1) - A(3, 1) * A(1, 3));
B(2, 3) = -invDet * (A(2, 3) * A(1, 1) - A(2, 1) * A(1, 3));
B(3, 1) = invDet * (A(3, 2) * A(2, 1) - A(3, 1) * A(2, 2));
B(3, 2) = -invDet * (A(3, 2) * A(1, 1) - A(3, 1) * A(1, 2));
B(3, 3) = invDet * (A(2, 2) * A(1, 1) - A(2, 1) * A(1, 2));
#undef A
#undef B
return 0;
}
static void
mat3mulv (float *dst, const float *const mat, const float *const v)
{
for(int k=0; k<3; k++)
{
float x=0.0f;
for(int i=0; i<3; i++) x += mat[3*k+i] * v[i];
dst[k] = x;
}
}
static void
mat3mul (float *dst, const float *const m1, const float *const m2)
{
for(int k=0; k<3; k++)
{
for(int i=0; i<3; i++)
{
float x=0.0f;
for(int j=0; j<3; j++) x += m1[3*k+j] * m2[3*j+i];
dst[3*k+i] = x;
}
}
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;