Debian Codegraph @

src/ImathTest/testMiscMatrixAlgo.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 <ImathMatrixAlgo.h>
11 #include <ImathRandom.h>
12 #include <assert.h>
13 #include <exception>
14 #include <iostream>
15 #include <stdio.h>
16 #include "testMiscMatrixAlgo.h"
17
18 #if 0
19 # define debug(x) (printf x, fflush (stdout))
20 #else
21 # define debug(x)
22 #endif
23
24 using namespace std;
25 using namespace IMATH_INTERNAL_NAMESPACE;
26
27 namespace
28 {
29
30 float
31 rad (float deg)
32 {
33 return deg * float(M_PI / 180);
34 }
35
36 void
37 testComputeLocalFrame()
38 {
39 float eps = 0.00005f;
40 Rand48 random (0);
41 for (int i = 0; i < 100000; ++i)
42 {
43 debug (("iteration: %d\n", i));
44
45 // Random pos
46 V3f p (
47 float(random.nextf (-10, 10)),
48 float(random.nextf (-10, 10)),
49 float(random.nextf (-10, 10)));
50
51 // Random xDir
52 V3f xDir (
53 float(random.nextf (-10, 10)),
54 float(random.nextf (-10, 10)),
55 float(random.nextf (-10, 10)));
56
57 // Random normalDir
58 V3f normalDir (
59 float(random.nextf (-10, 10)),
60 float(random.nextf (-10, 10)),
61 float(random.nextf (-10, 10)));
62
63 // Run computeLocalFrame we want to test
64 M44f L = computeLocalFrame (p, xDir, normalDir);
65
66 // test position
67 for (int j = 0; j < 3; j++)
68 {
69 if (abs (L[3][j] - p[j]) > eps)
70 assert (false);
71 }
72 if (abs (L[3][3] - 1.0) > eps)
73 assert (false);
74
75 // check that xAxis has the same dir as xDir and that is is normalized
76 V3f x (L[0][0], L[0][1], L[0][2]);
77 assert ((x % xDir).length() < eps);
78 if (abs (L[0][3]) > eps)
79 assert (false);
80 assert ((abs (x.length() - 1.f) < eps));
81
82 // Check that y is normal to x and to normalDir, and is normalized
83 V3f y (L[1][0], L[1][1], L[1][2]);
84 if (abs (L[1][3]) > eps)
85 assert (false);
86 assert (abs (x ^ y) < eps);
87 /*std::cout<<y<<"\n";
88 std::cout<<normalDir<<"\n";
89 std::cout<<(y^normalDir)<<"\n";*/
90 assert (abs (y ^ normalDir) < eps);
91 assert ((abs (y.length() - 1.f) < eps));
92
93 // check that z is normalized, normal to x and y, and direct
94 V3f z (L[2][0], L[2][1], L[2][2]);
95 if (abs (L[2][3]) > eps)
96 assert (false);
97 assert ((abs (z.length() - 1.f) < eps));
98 assert (abs (x ^ z) < eps);
99 assert (abs (y ^ z) < eps);
100 assert (((x % y) ^ z) > 0);
101 }
102 }
103
104 void
105 getRandTRS (Rand48& random, V3f& trans, V3f& rot, V3f& scale)
106 {
107 // Translate
108 trans = V3f (
109 float(random.nextf (-10, 10)),
110 float(random.nextf (-10, 10)),
111 float(random.nextf (-10, 10)));
112 // Rotate
113 rot = V3f (
114 rad (float(random.nextf (-180, 180))),
115 rad (float(random.nextf (-180, 180))),
116 rad (float(random.nextf (-180, 180))));
117
118 // Scale
119 V3f s (
120 float(random.nextf (0.000001, 2.0)),
121 float(random.nextf (0.000001, 2.0)),
122 float(random.nextf (0.000001, 2.0)));
123 for (int j = 0; j < 3; j++)
124 if (random.nextf (0.0, 1.0) >= 0.5)
125 s[j] *= -1;
126 scale = s;
127 }
128
129 M44f
130 createRandomMat (Rand48& random, V3f& trans, V3f& rot, V3f& scale)
131 {
132
133 M44f M;
134 V3f t, r, s;
135 getRandTRS (random, t, r, s);
136
137 M.translate (t);
138 M.rotate (r);
139
140 // Shear M.
141 V3f h (
142 float(random.nextf (0.000001, 2.0)),
143 float(random.nextf (0.000001, 2.0)),
144 float(random.nextf (0.000001, 2.0)));
145
146 for (int j = 0; j < 3; j++)
147 if (random.nextf (0.0, 1.0) >= 0.5)
148 h[j] *= -1;
149 M.shear (h);
150
151 M.scale (s);
152
153 //
154 // Add a small random error to the elements of M
155 //
156 for (int j = 0; j < 4; ++j)
157 for (int k = 0; k < 3; ++k)
158 M[j][k] += float(random.nextf (-1e-7, 1e-7));
159
160 V3f sh;
161 extractSHRT (M, scale, sh, rot, trans);
162
163 debug (("Scale : %f %f %f\n", s[0], s[1], s[2]));
164 debug (("Shear : %f %f %f\n", h[0], h[1], h[2]));
165 debug (("Rot : %f %f %f\n", r[0], r[1], r[2]));
166 debug (("Trans : %f %f %f\n", t[0], t[1], t[2]));
167
168 return M;
169 }
170
171 void
172 compareMat (M44f& M, M44f& N)
173 {
174 float eps = 0.0001f;
175
176 /// Verify that the entries in M and N do not
177 // differ too much.
178
179 M44f D (M - N);
180
181 for (int j = 0; j < 4; ++j)
182 {
183 for (int k = 0; k < 4; ++k)
184 {
185 //cout << "diff="<<D[j][k] << endl;
186 if (abs (D[j][k]) > eps)
187 {
188 cout << "unexpectedly diff " << D[j][k] << endl;
189
190 cout << j << " " << k << endl;
191
192 cout << "M\n" << M << endl;
193 cout << "N\n" << N << endl;
194 cout << "D\n" << D << endl;
195
196 assert (false);
197 }
198 }
199 }
200 }
201
202 void
203 testAddOffset()
204 {
205 Rand48 random (0);
206
207 for (int i = 0; i < 100000; ++i)
208 {
209 debug (("iteration: %d\n", i));
210
211 V3f transA, transB, rotA, rotB, scaleA, scaleB;
212 V3f tOffset, rOffset, sOffset;
213 M44f inMat = createRandomMat (random, transA, rotA, scaleA);
214 M44f refMat = createRandomMat (random, transB, rotB, scaleB);
215 getRandTRS (random, tOffset, rOffset, sOffset);
216
217 // addOffset : function to test
218 M44f outMat = addOffset (inMat, tOffset, rOffset, sOffset, refMat);
219
220 // add the inverse offset
221 M44f invO;
222 invO.rotate (V3f (rad (rOffset[0]), rad (rOffset[1]), rad (rOffset[2])));
223 invO[3][0] = tOffset[0];
224 invO[3][1] = tOffset[1];
225 invO[3][2] = tOffset[2];
226 invO.invert();
227
228 M44f invS;
229 invS.scale (sOffset);
230 invS.invert(); // zero scale is avoided in getRandTRS
231
232 // in ref mat from the function result
233 M44f outInRefMat = invO * invS * outMat;
234
235 // in ref mat from the inputs
236 M44f inRefMat = inMat * refMat;
237
238 // compare the mat
239 compareMat (outInRefMat, inRefMat);
240 }
241 }
242
243 void
244 testRSMatrix (M44f& M, V3f& t, V3f& r, V3f& s)
245 {
246 M44f N;
247 N.makeIdentity();
248 N.translate (t); // ... matrix compositions
249 N.rotate (r);
250 N.scale (s);
251
252 compareMat (M, N);
253 }
254
255 void
256 testComputeRSMatrix()
257 {
258 Rand48 random (0);
259
260 for (int i = 0; i < 100000; ++i)
261 {
262 debug (("iteration: %d\n", i));
263
264 V3f transA, transB, rotA, rotB, scaleA, scaleB;
265
266 M44f A = createRandomMat (random, transA, rotA, scaleA);
267 M44f B = createRandomMat (random, transB, rotB, scaleB);
268
269 M44f ArAsA = computeRSMatrix (true, true, A, B);
270 M44f ArBsB = computeRSMatrix (false, false, A, B);
271 M44f ArAsB = computeRSMatrix (true, false, A, B);
272 M44f ArBsA = computeRSMatrix (false, true, A, B);
273
274 testRSMatrix (ArAsA, transA, rotA, scaleA);
275 testRSMatrix (ArBsB, transA, rotB, scaleB);
276 testRSMatrix (ArAsB, transA, rotA, scaleB);
277 testRSMatrix (ArBsA, transA, rotB, scaleA);
278
279 debug (("\n"));
280 }
281 }
282
283 } // namespace
284
285 void
286 testMiscMatrixAlgo()
287 {
288 try
289 {
290 cout << "Testing misc functions in ImathMatrixAlgo.h" << endl;
291
292 cout << "Testing the building of an orthonormal direct frame from : a position, "
293 << "an x axis direction and a normal to the y axis" << endl;
294 cout << "IMATH_INTERNAL_NAMESPACE::computeLocalFrame()" << endl;
295
296 testComputeLocalFrame();
297
298 cout << "ok\n" << endl;
299
300 cout << "Add a translate/rotate/scale offset to an input frame "
301 << "and put it in another frame of reference" << endl;
302 cout << "IMATH_INTERNAL_NAMESPACE::addOffset()" << endl;
303
304 testAddOffset();
305
306 cout << "ok\n" << endl;
307
308 cout << "Compute Translate/Rotate/Scale matrix from matrix A " << endl;
309 cout << "with the Rotate/Scale of Matrix B" << endl;
310 cout << "IMATH_INTERNAL_NAMESPACE::computeRSMatrix()" << endl;
311
312 testComputeRSMatrix();
313
314 cout << "ok\n" << endl;
315 }
316 catch (std::exception& e)
317 {
318 cerr << " Caught exception: " << e.what() << endl;
319 }
320 }