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

Revision 7597, 19.1 kB (checked in by robert, 6 years ago)

Added testPolytope entry

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