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

Revision 7471, 18.5 kB (checked in by robert, 7 years ago)

Added thread unit test to test the create, start and destruction of threads

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