~ubuntu-branches/ubuntu/saucy/openexr/saucy

« back to all changes in this revision

Viewing changes to ImathTest/testExtractSHRT.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adeodato Simó
  • Date: 2008-03-24 23:00:21 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080324230021-gnofz9mnvcj1xlv3
Tags: 1.6.1-3
Disable (hopefully temporarily) the test suite on arm and ia64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////
2
 
//
3
 
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
 
// Digital Ltd. LLC
5
 
// 
6
 
// All rights reserved.
7
 
// 
8
 
// Redistribution and use in source and binary forms, with or without
9
 
// modification, are permitted provided that the following conditions are
10
 
// met:
11
 
// *       Redistributions of source code must retain the above copyright
12
 
// notice, this list of conditions and the following disclaimer.
13
 
// *       Redistributions in binary form must reproduce the above
14
 
// copyright notice, this list of conditions and the following disclaimer
15
 
// in the documentation and/or other materials provided with the
16
 
// distribution.
17
 
// *       Neither the name of Industrial Light & Magic nor the names of
18
 
// its contributors may be used to endorse or promote products derived
19
 
// from this software without specific prior written permission. 
20
 
// 
21
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 
//
33
 
///////////////////////////////////////////////////////////////////////////
34
 
 
35
 
 
36
 
#include <testExtractSHRT.h>
37
 
#include <ImathMatrixAlgo.h>
38
 
#include <ImathEuler.h>
39
 
#include <ImathRandom.h>
40
 
#include <ImathFun.h>
41
 
#include <iostream>
42
 
#include <exception>
43
 
#include <stdio.h>
44
 
#include <assert.h>
45
 
 
46
 
 
47
 
#if 0
48
 
    #define debug(x) (printf x, fflush (stdout))
49
 
#else
50
 
    #define debug(x)
51
 
#endif
52
 
 
53
 
 
54
 
using namespace std;
55
 
using namespace Imath;
56
 
 
57
 
namespace {
58
 
 
59
 
float rad (float deg) {return deg * (M_PI / 180);}
60
 
float deg (float rad) {return rad * (180 / M_PI);}
61
 
 
62
 
 
63
 
void
64
 
testMatrix (const M33f M)
65
 
{
66
 
    //
67
 
    // Extract the rotation angle from M, and convert the
68
 
    // angle back to a matrix, N.
69
 
    //
70
 
 
71
 
    V2f s, t;
72
 
    float h, r;
73
 
  
74
 
    extractSHRT (M, s, h, r, t, true);
75
 
 
76
 
    M33f N;
77
 
 
78
 
    N *= M33f().setScale (s);
79
 
    N *= M33f().setShear (h);
80
 
    N *= M33f().setRotation (r);
81
 
    N *= M33f().setTranslation (t);
82
 
 
83
 
    debug (("Re-scale: %f %f\n", s[0], s[1]));
84
 
    debug (("Re-shear: %f\n", h));
85
 
    debug (("Re-rot  : %f\n", r));
86
 
    debug (("Re-trans: %f %f\n", t[0], t[1]));
87
 
 
88
 
    //
89
 
    // Verify that the entries in M and N do not
90
 
    // differ too much.
91
 
    //
92
 
 
93
 
    M33f D (M - N);
94
 
 
95
 
    for (int j = 0; j < 3; ++j)
96
 
    {
97
 
        for (int k = 0; k < 3; ++k)
98
 
        {
99
 
            if (abs (D[j][k]) > 0.00001)
100
 
            {
101
 
                cout << "unexpectedly large matrix to "
102
 
                        "euler angles conversion error: " <<
103
 
                        D[j][k] << endl;
104
 
 
105
 
                cout << j << " " << k << endl;
106
 
 
107
 
                cout << "M\n" << M << endl;
108
 
                cout << "N\n" << N << endl;
109
 
                cout << "D\n" << D << endl;
110
 
 
111
 
                assert (false);
112
 
            }
113
 
        }
114
 
    }
115
 
}
116
 
 
117
 
 
118
 
void
119
 
testRandomAngles33 ()
120
 
{
121
 
    Rand48 random(0);
122
 
 
123
 
    for (int i = 0; i < 100000; ++i)
124
 
    {
125
 
        debug (("iteration: %d\n", i));
126
 
 
127
 
        M33f M;
128
 
 
129
 
        //
130
 
        // Scale M.
131
 
        //
132
 
 
133
 
        V2f s (random.nextf (0.000001, 2.0),
134
 
               random.nextf (0.000001, 2.0));
135
 
 
136
 
        for (int j=0; j < 2; j++)
137
 
            if (random.nextf (0.0, 1.0) >= 0.5)
138
 
                s[j] *= -1;
139
 
 
140
 
        M *= M33f().setScale (s);
141
 
 
142
 
        //
143
 
        // Shear M.
144
 
        //
145
 
 
146
 
        float h = random.nextf (0.000001, 2.);
147
 
        if (random.nextf (0.0, 1.0) >= 0.5)
148
 
            h *= -1;
149
 
 
150
 
        M *= M33f().setShear (h);
151
 
 
152
 
        //
153
 
        // Rotate M.
154
 
        //
155
 
 
156
 
        float r = rad (random.nextf (-180, 180));
157
 
        
158
 
        M *= M33f().setRotation (r);
159
 
 
160
 
        //
161
 
        // Translate M.
162
 
        //
163
 
 
164
 
        V2f t (random.nextf (-10, 10), 
165
 
               random.nextf (-10, 10));
166
 
        
167
 
        M *= M33f().setTranslation (t);
168
 
 
169
 
        //
170
 
        // Add a small random error to the elements of M
171
 
        //
172
 
 
173
 
        for (int j = 0; j < 3; ++j)
174
 
            for (int k = 0; k < 2; ++k)
175
 
                M[j][k] += random.nextf (-1e-7, 1e-7);
176
 
 
177
 
 
178
 
        debug (("Scale   : %f %f\n", s[0], s[1]));
179
 
        debug (("Shear   : %f\n", h));
180
 
        debug (("Rot     : %f\n", r));
181
 
        debug (("Trans   : %f %f\n", t[0], t[1]));
182
 
 
183
 
 
184
 
        //
185
 
        // Extract Euler angles from M, convert the Euler angles
186
 
        // back to a matrix, N, and verify that the entries in M
187
 
        // and N do not differ too much.
188
 
        //
189
 
 
190
 
        testMatrix (M);
191
 
 
192
 
        debug (("\n"));
193
 
    }
194
 
}
195
 
 
196
 
 
197
 
void
198
 
testAngles33 (float angle)
199
 
{
200
 
    M33f M;
201
 
    M.setRotation (rad (angle));
202
 
 
203
 
    //
204
 
    // With rounding errors from e.toMatrix.
205
 
    //
206
 
 
207
 
    testMatrix (M);
208
 
 
209
 
    //
210
 
    // Without rounding errors (assuming that
211
 
    // all angles are multiples of 90 degrees).
212
 
    //
213
 
 
214
 
    for (int i = 0; i < 2; ++i)
215
 
        for (int j = 0; j < 2; ++j)
216
 
            if (M[i][j] < -0.5)
217
 
                M[i][j] = -1;
218
 
            else if (M[i][j] > 0.5)
219
 
                M[i][j] = 1;
220
 
            else
221
 
                M[i][j] = 0;
222
 
 
223
 
    testMatrix (M);
224
 
}
225
 
 
226
 
 
227
 
void
228
 
testMatrix (const M44f M)
229
 
{
230
 
    //
231
 
    // Extract Euler angles from M, and convert the
232
 
    // Euler angles back to a matrix, N.
233
 
    //
234
 
 
235
 
    V3f s, h, r, t;
236
 
  
237
 
    extractSHRT (M, s, h, r, t, true);
238
 
 
239
 
    M44f N;
240
 
 
241
 
    N.translate (t); // ... matrix compositions
242
 
    N.rotate (r);
243
 
    N.shear (h);
244
 
    N.scale (s);
245
 
 
246
 
    debug (("Re-scale: %f %f %f\n", s[0], s[1], s[2]));
247
 
    debug (("Re-shear: %f %f %f\n", h[0], h[1], h[2]));
248
 
    debug (("Re-rot  : %f %f %f\n", r[0], r[1], r[2]));
249
 
    debug (("Re-trans: %f %f %f\n", t[0], t[1], t[2]));
250
 
 
251
 
    //
252
 
    // Verify that the entries in M and N do not
253
 
    // differ too much.
254
 
    //
255
 
 
256
 
    M44f D (M - N);
257
 
 
258
 
    for (int j = 0; j < 4; ++j)
259
 
    {
260
 
        for (int k = 0; k < 4; ++k)
261
 
        {
262
 
            if (abs (D[j][k]) > 0.00001)
263
 
            {
264
 
                cout << "unexpectedly large matrix to "
265
 
                        "euler angles conversion error: " <<
266
 
                        D[j][k] << endl;
267
 
 
268
 
                cout << j << " " << k << endl;
269
 
 
270
 
                cout << "M\n" << M << endl;
271
 
                cout << "N\n" << N << endl;
272
 
                cout << "D\n" << D << endl;
273
 
 
274
 
                assert (false);
275
 
            }
276
 
        }
277
 
    }
278
 
}
279
 
 
280
 
 
281
 
void
282
 
testRandomAngles44 ()
283
 
{
284
 
    Rand48 random(0);
285
 
 
286
 
    for (int i = 0; i < 100000; ++i)
287
 
    {
288
 
        debug (("iteration: %d\n", i));
289
 
 
290
 
        M44f M;
291
 
 
292
 
        //
293
 
        // Translate M.
294
 
        //
295
 
 
296
 
        V3f t (random.nextf (-10, 10), 
297
 
               random.nextf (-10, 10), 
298
 
               random.nextf (-10, 10));
299
 
        
300
 
        M.translate (t);
301
 
 
302
 
        //
303
 
        // Rotate M.
304
 
        //
305
 
 
306
 
        V3f r (rad (random.nextf (-180, 180)),
307
 
               rad (random.nextf (-180, 180)),
308
 
               rad (random.nextf (-180, 180)));
309
 
        
310
 
        M.rotate (r);
311
 
 
312
 
        //
313
 
        // Shear M.
314
 
        //
315
 
 
316
 
        V3f h (random.nextf (0.000001, 2.0), 
317
 
               random.nextf (0.000001, 2.0), 
318
 
               random.nextf (0.000001, 2.0));
319
 
        
320
 
        for (int j=0; j < 3; j++)
321
 
            if (random.nextf (0.0, 1.0) >= 0.5)
322
 
                h[j] *= -1;
323
 
 
324
 
        M.shear (h);
325
 
 
326
 
        //
327
 
        // Scale M.
328
 
        //
329
 
 
330
 
        V3f s (random.nextf (0.000001, 2.0),
331
 
               random.nextf (0.000001, 2.0),
332
 
               random.nextf (0.000001, 2.0));
333
 
 
334
 
        for (int j=0; j < 3; j++)
335
 
            if (random.nextf (0.0, 1.0) >= 0.5)
336
 
                s[j] *= -1;
337
 
 
338
 
        M.scale (s);
339
 
 
340
 
        //
341
 
        // Add a small random error to the elements of M
342
 
        //
343
 
 
344
 
        for (int j = 0; j < 4; ++j)
345
 
            for (int k = 0; k < 3; ++k)
346
 
                M[j][k] += random.nextf (-1e-7, 1e-7);
347
 
 
348
 
 
349
 
        debug (("Scale   : %f %f %f\n", s[0], s[1], s[2]));
350
 
        debug (("Shear   : %f %f %f\n", h[0], h[1], h[2]));
351
 
        debug (("Rot     : %f %f %f\n", r[0], r[1], r[2]));
352
 
        debug (("Trans   : %f %f %f\n", t[0], t[1], t[2]));
353
 
 
354
 
 
355
 
        //
356
 
        // Extract Euler angles from M, convert the Euler angles
357
 
        // back to a matrix, N, and verify that the entries in M
358
 
        // and N do not differ too much.
359
 
        //
360
 
 
361
 
        testMatrix (M);
362
 
 
363
 
        debug (("\n"));
364
 
    }
365
 
}
366
 
 
367
 
 
368
 
void
369
 
testAngles44 (V3f angles)
370
 
{
371
 
    Eulerf e (rad (angles.x),
372
 
              rad (angles.y),
373
 
              rad (angles.z));
374
 
 
375
 
    M44f M (e.toMatrix44());
376
 
 
377
 
    //
378
 
    // With rounding errors from e.toMatrix.
379
 
    //
380
 
 
381
 
    testMatrix (M);
382
 
 
383
 
    //
384
 
    // Without rounding errors (assuming that
385
 
    // all angles are multiples of 90 degrees).
386
 
    //
387
 
 
388
 
    for (int i = 0; i < 3; ++i)
389
 
        for (int j = 0; j < 3; ++j)
390
 
            if (M[i][j] < -0.5)
391
 
                M[i][j] = -1;
392
 
            else if (M[i][j] > 0.5)
393
 
                M[i][j] = 1;
394
 
            else
395
 
                M[i][j] = 0;
396
 
 
397
 
    testMatrix (M);
398
 
}
399
 
 
400
 
 
401
 
void
402
 
test ()
403
 
{
404
 
    cout << "  random angles" << endl;
405
 
 
406
 
    cout << "    3x3" << endl;
407
 
    testRandomAngles33 ();
408
 
 
409
 
    cout << "    4x4" << endl;
410
 
    testRandomAngles44 ();
411
 
 
412
 
    cout << "  special angles" << endl;
413
 
 
414
 
    cout << "    3x3" << endl;
415
 
    for (int i = 0; i < 360; i += 90)
416
 
        testAngles33 (float (i));
417
 
 
418
 
    cout << "    4x4" << endl;
419
 
    for (int i = 0; i < 360; i += 90)
420
 
        for (int j = 0; j < 360; j += 90)
421
 
            for (int k = 0; k < 360; k += 90)
422
 
                testAngles44 (V3f (i, j, k));
423
 
}
424
 
 
425
 
 
426
 
} // namespace
427
 
 
428
 
 
429
 
void
430
 
testExtractSHRT ()
431
 
{
432
 
    try
433
 
    {
434
 
        cout << "Testing extraction of scale, shear, rotation, translation " 
435
 
             << "from matrices" << endl;
436
 
        
437
 
        cout << "Imath::extractSHRT()" << endl;
438
 
        test ();
439
 
        
440
 
        cout << "ok\n" << endl;
441
 
    }
442
 
    catch (std::exception &e)
443
 
    {
444
 
        cerr << "  Caught exception: " << e.what () << endl;
445
 
    }
446
 
}
447