Debian Codegraph @

src/ImathTest/testInvert.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <ImathMatrix.h>
11 #include <ImathMatrixAlgo.h>
12 #include <assert.h>
13 #include <iostream>
14 #include "testInvert.h"
15
16 using namespace std;
17 using namespace IMATH_INTERNAL_NAMESPACE;
18
19 namespace
20 {
21
22 void
23 invertM44f (const M44f &m, float e)
24 {
25 M44f inv1 = m.inverse();
26 M44f inv2 = m.gjInverse();
27 M44f ident1 = m * inv1;
28 M44f ident2 = m * inv2;
29
30 //cout << "m\n" << m << endl;
31 //cout << "inv1\n" << inv1 << "ident1\n" << ident1 << endl;
32 //cout << "inv2\n" << inv2 << "ident2\n" << ident2 << endl;
33
34 assert (ident1.equalWithAbsError (identity44f, e));
35 assert (ident2.equalWithAbsError (identity44f, e));
36 }
37
38
39 void
40 invertM33f (const M33f &m, float e)
41 {
42 M33f inv1 = m.inverse();
43 M33f inv2 = m.gjInverse();
44 M33f ident1 = m * inv1;
45 M33f ident2 = m * inv2;
46
47 //cout << "m\n" << m << endl;
48 //cout << "inv1\n" << inv1 << "ident1\n" << ident1 << endl;
49 //cout << "inv2\n" << inv2 << "ident2\n" << ident2 << endl;
50
51 assert (ident1.equalWithAbsError (identity33f, e));
52 assert (ident2.equalWithAbsError (identity33f, e));
53 }
54
55
56 } // namespace
57
58
59 void
60 testInvert ()
61 {
62 cout << "Testing 4x4 and 3x3 matrix inversion:" << endl;
63
64 {
65 cout << "M44f" << endl;
66
67 // clang-format off
68
69 M44f m1 ( 1, 0, 0, 0,
70 0, 1, 0, 0,
71 0, 0, 1, 0,
72 0, 0, 0, 1);
73
74 M44f m2 ( 0, 1, 0, 0,
75 -1, 0, 0, 0,
76 0, 0, 1, 0,
77 0, 0, 0, 1);
78
79 M44f m3 ( 1, 0, 0, 0,
80 0, 2, 0, 0,
81 0, 0, 0, -1,
82 0, 0, 1, 0);
83
84 M44f m4 ( 4.683281e-01f, -8.749647e-01f, 1.229049e-01f, 0.000000e+00f,
85 1.251189e-02f, 1.456563e-01f, 9.892561e-01f, 0.000000e+00f,
86 -8.834660e-01f, -4.617587e-01f, 7.916244e-02f, 0.000000e+00f,
87 -4.726541e+00f, 3.044795e+00f, -6.737138e+00f, 1.000000e+00f);
88
89 M44f m5 ( 4.683281e-01f, -8.749647e-01f, 1.229049e-01f, 1.000000e+00f,
90 1.251189e-02f, 1.456563e-01f, 9.892561e-01f, 2.000000e+00f,
91 -8.834660e-01f, -4.617587e-01f, 7.916244e-02f, 3.000000e+00f,
92 -4.726541e+00f, 3.044795e+00f, -6.737138e+00f, 4.000000e+00f);
93
94 // clang-format on
95
96 invertM44f (m1, 0);
97 invertM44f (m2, 0);
98 invertM44f (m3, 0);
99 invertM44f (m4, 1e-6f);
100 invertM44f (m5, 1e-6f);
101 }
102
103 {
104 cout << "M33f" << endl;
105
106 // clang-format off
107
108 M33f m1 ( 1, 0, 0,
109 0, 1, 0,
110 0, 0, 1);
111
112 M33f m2 ( 0, 1, 0,
113 -1, 0, 0,
114 0, 0, 1);
115
116 M33f m3 ( 2, 0, 0,
117 0, 0, -1,
118 0, 1, 0);
119
120 M33f m4 ( 4.683281e-01f, -8.749647e-01f, 0.000000e+00f,
121 1.251189e-02f, 1.456563e-01f, 0.000000e+00f,
122 0.000000e+00f, 0.000000e+00f, 1.000000e+00f);
123
124 M33f m5 ( 4.683281e-01f, -8.749647e-01f, 1.229049e-01f,
125 1.251189e-02f, 1.456563e-01f, 9.892561e-01f,
126 -8.834660e-01f, -4.617587e-01f, 7.916244e-02f);
127
128 // clang-format on
129
130 invertM33f (m1, 0);
131 invertM33f (m2, 0);
132 invertM33f (m3, 0);
133 invertM33f (m4, 1e-6f);
134 invertM33f (m5, 1e-6f);
135 }
136
137 cout << "ok\n" << endl;
138 }