root/OpenSceneGraph/trunk/examples/osgunittests/osgunittests.cpp @ 6879

Revision 6879, 16.7 kB (checked in by robert, 8 years ago)

Added support for using quat_scaled sx sy sz for testing getting quats from scaled
matrices. Removed broken Matrixd/quat test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/ArgumentParser>
2#include <osg/ApplicationUsage>
3
4#include <osg/Vec3>
5#include <osg/Matrix>
6#include <osg/Timer>
7#include <osg/io_utils>
8
9#include "UnitTestFramework.h"
10#include "performance.h"
11
12#include <iostream>
13
14void testFrustum(double left,double right,double bottom,double top,double zNear,double zFar)
15{
16    osg::Matrix f;
17    f.makeFrustum(left,right,bottom,top,zNear,zFar);
18
19    double c_left=0;
20    double c_right=0;
21    double c_top=0;
22    double c_bottom=0;
23    double c_zNear=0;
24    double c_zFar=0;
25   
26   
27    std::cout << "testFrustum"<<f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar)<<std::endl;
28    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
29    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
30
31    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
32    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
33
34    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
35    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
36   
37    std::cout << std::endl;
38}
39
40void testOrtho(double left,double right,double bottom,double top,double zNear,double zFar)
41{
42    osg::Matrix f;
43    f.makeOrtho(left,right,bottom,top,zNear,zFar);
44
45    double c_left=0;
46    double c_right=0;
47    double c_top=0;
48    double c_bottom=0;
49    double c_zNear=0;
50    double c_zFar=0;
51
52    std::cout << "testOrtho "<< f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar) << std::endl;
53    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
54    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
55
56    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
57    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
58
59    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
60    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
61   
62    std::cout << std::endl;
63}
64
65void testPerspective(double fovy,double aspect,double zNear,double zFar)
66{
67    osg::Matrix f;
68    f.makePerspective(fovy,aspect,zNear,zFar);
69
70    double c_fovy=0;
71    double c_aspect=0;
72    double c_zNear=0;
73    double c_zFar=0;
74
75    std::cout << "testPerspective "<< f.getPerspective(c_fovy,c_aspect,c_zNear,c_zFar) << std::endl;
76    std::cout << "  fovy = "<<fovy<<" compute "<<c_fovy<<std::endl;
77    std::cout << "  aspect = "<<aspect<<" compute "<<c_aspect<<std::endl;
78
79    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
80    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
81   
82    std::cout << std::endl;
83}
84
85void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
86{
87    osg::Matrix mv;
88    mv.makeLookAt(eye,center,up);
89   
90    osg::Vec3 c_eye,c_center,c_up;
91    mv.getLookAt(c_eye,c_center,c_up);
92   
93    std::cout << "testLookAt"<<std::endl;
94    std::cout << "  eye "<<eye<< " compute "<<c_eye<<std::endl;
95    std::cout << "  center "<<center<< " compute "<<c_center<<std::endl;
96    std::cout << "  up "<<up<< " compute "<<c_up<<std::endl;
97   
98    std::cout << std::endl;
99   
100}
101
102
103void testMatrixInvert(const osg::Matrix& matrix)
104{
105    //Invert it twice using the two inversion functions and view the results
106    osg::notify(osg::NOTICE)<<"testMatrixInvert("<<std::endl;
107    osg::notify(osg::NOTICE)<<matrix<<std::endl;
108    osg::notify(osg::NOTICE)<<")"<<std::endl;
109
110    osg::Matrix invM1_0;
111    invM1_0.invert(matrix);
112    osg::notify(osg::NOTICE)<<"Matrix::invert"<<std::endl;
113    osg::notify(osg::NOTICE)<<invM1_0<<std::endl;
114    osg::Matrix default_result = matrix*invM1_0;
115    osg::notify(osg::NOTICE)<<"matrix * invert="<<std::endl;
116    osg::notify(osg::NOTICE)<<default_result<<std::endl;;
117
118}
119
120void sizeOfTest()
121{
122  std::cout<<"sizeof(bool)=="<<sizeof(bool)<<std::endl;
123  std::cout<<"sizeof(char)=="<<sizeof(char)<<std::endl;
124  std::cout<<"sizeof(short)=="<<sizeof(short)<<std::endl;
125  std::cout<<"sizeof(short int)=="<<sizeof(short int)<<std::endl;
126  std::cout<<"sizeof(int)=="<<sizeof(int)<<std::endl;
127  std::cout<<"sizeof(long)=="<<sizeof(long)<<std::endl;
128  std::cout<<"sizeof(long int)=="<<sizeof(long int)<<std::endl;
129
130#if defined(_MSC_VER)
131  // long long isn't supported on VS6.0...
132  std::cout<<"sizeof(__int64)=="<<sizeof(__int64)<<std::endl;
133#else
134  std::cout<<"sizeof(long long)=="<<sizeof(long long)<<std::endl;
135#endif
136  std::cout<<"sizeof(float)=="<<sizeof(float)<<std::endl;
137  std::cout<<"sizeof(double)=="<<sizeof(double)<<std::endl;
138
139  std::cout<<"sizeof(std::istream::pos_type)=="<<sizeof(std::istream::pos_type)<<std::endl;
140  std::cout<<"sizeof(std::istream::off_type)=="<<sizeof(std::istream::off_type)<<std::endl;
141  std::cout<<"sizeof(OpenThreads::Mutex)=="<<sizeof(OpenThreads::Mutex)<<std::endl;
142
143  std::cout<<"sizeof(std::string)=="<<sizeof(std::string)<<std::endl;
144
145}
146
147/// Exercise the Matrix.getRotate function.
148/// Compare the output of:
149///  q1 * q2
150/// versus
151///  (mat(q1)*mat(q2)*scale).getRotate()
152/// for a range of rotations
153void testGetQuatFromMatrix(const osg::Vec3d& scale)
154{
155   
156    // Options
157   
158    // acceptable error range
159    double eps=1e-6;
160
161    // scale matrix
162    // To not test with scale, use 1,1,1
163    // Not sure if 0's or negative values are acceptable
164    osg::Matrixd scalemat;
165    scalemat.makeScale(scale);
166   
167    // range of rotations
168#if 1
169    // wide range
170    double rol1start = 0.0;
171    double rol1stop = 360.0;
172    double rol1step = 20.0;
173
174    double pit1start = 0.0;
175    double pit1stop = 90.0;
176    double pit1step = 20.0;
177
178    double yaw1start = 0.0;
179    double yaw1stop = 360.0;
180    double yaw1step = 20.0;
181
182    double rol2start = 0.0;
183    double rol2stop = 360.0;
184    double rol2step = 20.0;
185
186    double pit2start = 0.0;
187    double pit2stop = 90.0;
188    double pit2step = 20.0;
189
190    double yaw2start = 0.0;
191    double yaw2stop = 360.0;
192    double yaw2step = 20.0;
193#else
194    // focussed range
195    double rol1start = 0.0;
196    double rol1stop = 0.0;
197    double rol1step = 0.1;
198
199    double pit1start = 0.0;
200    double pit1stop = 5.0;
201    double pit1step = 5.0;
202
203    double yaw1start = 89.0;
204    double yaw1stop = 91.0;
205    double yaw1step = 0.1;
206
207    double rol2start = 0.0;
208    double rol2stop = 0.0;
209    double rol2step = 0.1;
210
211    double pit2start = 0.0;
212    double pit2stop = 0.0;
213    double pit2step = 0.1;
214
215    double yaw2start = 89.0;
216    double yaw2stop = 91.0;
217    double yaw2step = 0.1;
218#endif
219   
220    std::cout << std::endl << "Starting " << __FUNCTION__ << ", it can take a while ..." << std::endl;
221
222    osg::Timer_t tstart, tstop;
223    tstart = osg::Timer::instance()->tick();
224    int count=0;
225    for (double rol1 = rol1start; rol1 <= rol1stop; rol1 += rol1step) {
226    for (double pit1 = pit1start; pit1 <= pit1stop; pit1 += pit1step) {
227        for (double yaw1 = yaw1start; yaw1 <= yaw1stop; yaw1 += yaw1step) {
228        for (double rol2 = rol2start; rol2 <= rol2stop; rol2 += rol2step) {
229            for (double pit2 = pit2start; pit2 <= pit2stop; pit2 += pit2step) {
230            for (double yaw2 = yaw2start; yaw2 <= yaw2stop; yaw2 += yaw2step) {
231                count++;
232                // create two quats based on the roll, pitch and yaw values
233                osg::Quat rot_quat1 =
234                osg::Quat(osg::DegreesToRadians(rol1),osg::Vec3d(1,0,0),
235                      osg::DegreesToRadians(pit1),osg::Vec3d(0,1,0),
236                      osg::DegreesToRadians(yaw1),osg::Vec3d(0,0,1));
237               
238                osg::Quat rot_quat2 =
239                osg::Quat(osg::DegreesToRadians(rol2),osg::Vec3d(1,0,0),
240                      osg::DegreesToRadians(pit2),osg::Vec3d(0,1,0),
241                      osg::DegreesToRadians(yaw2),osg::Vec3d(0,0,1));
242               
243                // create an output quat using quaternion math
244                osg::Quat out_quat1;
245                out_quat1 = rot_quat2 * rot_quat1;
246               
247                // create two matrices based on the input quats
248                osg::Matrixd mat1,mat2;
249                mat1.makeRotate(rot_quat1);
250                mat2.makeRotate(rot_quat2);
251       
252                // create an output quat by matrix multiplication and getRotate
253                osg::Matrixd out_mat;
254                out_mat = mat2 * mat1;
255                // add matrix scale for even more nastiness
256                out_mat = out_mat * scalemat;
257                osg::Quat out_quat2;
258                out_quat2 = out_mat.getRotate();
259               
260                // if the output quat length is not one
261                // or if the component magnitudes do not match,
262                // something is amiss
263                if (fabs(1.0-out_quat2.length()) > eps ||
264                (fabs(out_quat1.x())-fabs(out_quat2.x())) > eps ||
265                (fabs(out_quat1.y())-fabs(out_quat2.y())) > eps ||
266                (fabs(out_quat1.z())-fabs(out_quat2.z())) > eps ||
267                (fabs(out_quat1.w())-fabs(out_quat2.w())) > eps) {
268                std::cout << __FUNCTION__ << " problem at: \n"
269                      << " r1=" << rol1
270                      << " p1=" << pit1
271                      << " y1=" << yaw1
272                      << " r2=" << rol2
273                      << " p2=" << pit2
274                      << " y2=" << yaw2 << "\n";
275                std::cout << "quats:        " << out_quat1 << " length: " << out_quat1.length() << "\n";
276                std::cout << "mats and get: " << out_quat2 << " length: " << out_quat2.length() << "\n\n";
277                }
278            }
279            }
280        }
281        }
282    }
283    }
284    tstop = osg::Timer::instance()->tick();
285    double duration = osg::Timer::instance()->delta_s(tstart,tstop);
286    std::cout << "Time for " << __FUNCTION__ << " with " << count << " iterations: " << duration << std::endl << std::endl;
287}
288
289void testQuatRotate(const osg::Vec3d& from, const osg::Vec3d& to)
290{
291    osg::Quat q_nicolas;
292    q_nicolas.makeRotate(from,to);
293   
294    osg::Quat q_original;
295    q_original.makeRotate_original(from,to);
296   
297    std::cout<<"osg::Quat::makeRotate("<<from<<", "<<to<<")"<<std::endl;
298    std::cout<<"  q_nicolas = "<<q_nicolas<<std::endl;
299    std::cout<<"  q_original = "<<q_original<<std::endl;
300    std::cout<<"  from * M4x4(q_nicolas) = "<<from * osg::Matrixd::rotate(q_nicolas)<<std::endl;
301    std::cout<<"  from * M4x4(q_original) = "<<from * osg::Matrixd::rotate(q_original)<<std::endl;
302}
303
304void testQuat(const osg::Vec3d& quat_scale)
305{
306    osg::Quat q1;
307    q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f);
308
309    osg::Quat q2;
310    q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f);
311
312    osg::Quat q1_2 = q1*q2;
313    osg::Quat q2_1 = q2*q1;
314
315    osg::Matrix m1 = osg::Matrix::rotate(q1);
316    osg::Matrix m2 = osg::Matrix::rotate(q2);
317   
318    osg::Matrix m1_2 = m1*m2;
319    osg::Matrix m2_1 = m2*m1;
320   
321    osg::Quat qm1_2;
322    qm1_2.set(m1_2);
323   
324    osg::Quat qm2_1;
325    qm2_1.set(m2_1);
326   
327    std::cout<<"q1*q2 = "<<q1_2<<std::endl;
328    std::cout<<"q2*q1 = "<<q2_1<<std::endl;
329    std::cout<<"m1*m2 = "<<qm1_2<<std::endl;
330    std::cout<<"m2*m1 = "<<qm2_1<<std::endl;
331
332
333    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(0.0,1.0,0.0));
334    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(1.0,0.0,0.0));
335    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,1.0,0.0));
336    testQuatRotate(osg::Vec3d(1.0,1.0,1.0),osg::Vec3d(1.0,0.0,0.0));
337    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
338    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(-1.0,0.0,0.0));
339    testQuatRotate(osg::Vec3d(-1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
340    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(0.0,-1.0,0.0));
341    testQuatRotate(osg::Vec3d(0.0,-1.0,0.0),osg::Vec3d(0.0,1.0,0.0));
342    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,0.0,-1.0));
343    testQuatRotate(osg::Vec3d(0.0,0.0,-1.0),osg::Vec3d(0.0,0.0,1.0));
344
345    // Test a range of rotations
346    testGetQuatFromMatrix(quat_scale);
347
348    // This is a specific test case for a matrix containing scale and rotation
349    osg::Matrix matrix(0.5, 0.0, 0.0, 0.0,
350                       0.0, 0.5, 0.0, 0.0,
351                       0.0, 0.0, 0.5, 0.0,
352                       1.0, 1.0, 1.0, 1.0);
353                       
354    osg::Quat quat;
355    matrix.get(quat);
356   
357    osg::notify(osg::NOTICE)<<"Matrix = "<<matrix<<"rotation = "<<quat<<", expected quat = (0,0,0,1)"<<std::endl;
358}
359
360
361int main( int argc, char** argv )
362{
363    osg::ArgumentParser arguments(&argc,argv);
364
365    // set up the usage document, in case we need to print out how to use this program.
366    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
367    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
368    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
369    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
370    arguments.getApplicationUsage()->addCommandLineOption("quat","Display extended quaternion tests.");
371    arguments.getApplicationUsage()->addCommandLineOption("quat_scaled sx sy sz","Display extended quaternion tests of pre scaled matrix.");
372    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
373    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
374    arguments.getApplicationUsage()->addCommandLineOption("performance","Display qualified tests.");
375 
376
377    if (arguments.argc()<=1)
378    {
379        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
380        return 1;
381    }
382
383    bool printQualifiedTest = false;
384    while (arguments.read("qt")) printQualifiedTest = true;
385
386    bool printMatrixTest = false;
387    while (arguments.read("matrix")) printMatrixTest = true;
388
389    bool printSizeOfTest = false;
390    while (arguments.read("sizeof")) printSizeOfTest = true;
391
392    bool printQuatTest = false;
393    while (arguments.read("quat")) printQuatTest = true;
394
395    osg::Vec3d quat_scale(1.0,1.0,1.0);
396    while (arguments.read("quat_scaled", quat_scale.x(), quat_scale.y(), quat_scale.z() )) printQuatTest = true;
397
398    bool performanceTest = false;
399    while (arguments.read("p") || arguments.read("performance")) performanceTest = true;
400
401    // if user request help write it out to cout.
402    if (arguments.read("-h") || arguments.read("--help"))
403    {
404        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
405        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
406        return 1;
407    }
408
409    // any option left unread are converted into errors to write out later.
410    arguments.reportRemainingOptionsAsUnrecognized();
411
412    // report any errors if they have occured when parsing the program aguments.
413    if (arguments.errors())
414    {
415        arguments.writeErrorMessages(std::cout);
416        return 1;
417    }
418   
419    if (printQuatTest)
420    {
421        testQuat(quat_scale);
422    }
423
424
425    if (printMatrixTest)
426    {
427        std::cout<<"******   Running matrix tests   ******"<<std::endl;
428
429        testFrustum(-1,1,-1,1,1,1000);
430        testFrustum(0,1,1,2,2.5,100000);
431
432        testOrtho(0,1,1,2,2.1,1000);
433        testOrtho(-1,10,1,20,2.5,100000);
434
435        testPerspective(20,1,1,1000);
436        testPerspective(90,2,1,1000);
437
438        testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(0.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));
439        testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(1.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));
440       
441        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242, -0.1715,
442                                     0,         0.987960,   0.154710,  0.207295,
443                                     -0.017452, -0.154687,  0.987809, -0.98239,
444                                     0,         0,          0,         1));
445
446        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242,   0.0,
447                                     0.0,        0.987960,   0.154710,   0.0,
448                                     -0.017452, -0.154687,  0.987809,   0.0,
449                                     -0.1715,    0.207295,  -0.98239,   1.0));
450
451    }
452   
453    if (printSizeOfTest)
454    {
455        std::cout<<"**** sizeof() tests  ******"<<std::endl;
456       
457        sizeOfTest();
458
459        std::cout<<std::endl;
460    }
461
462
463    if (performanceTest)
464    {
465        std::cout<<"**** performance tests  ******"<<std::endl;
466       
467        runPerformanceTests();
468    }
469
470
471    if (printQualifiedTest)
472    {
473         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
474
475         osgUtx::QualifiedTestPrinter printer;
476         osgUtx::TestGraph::instance().root()->accept( printer );   
477         std::cout<<std::endl;
478    }
479
480    std::cout<<"******   Running tests   ******"<<std::endl;
481
482    // Global Data or Context
483    osgUtx::TestContext ctx;
484    osgUtx::TestRunner runner( ctx );
485    runner.specify("root");
486
487    osgUtx::TestGraph::instance().root()->accept( runner );
488
489    return 0;
490}
Note: See TracBrowser for help on using the browser.