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

Revision 6941, 17.6 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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