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

Revision 10324, 24.5 kB (checked in by robert, 5 years ago)

Added basic Matrix::decompose() test, based on a test progrem wrttien by Paul Obermeier

  • 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#include "MultiThreadRead.h"
33
34#include <iostream>
35
36void testFrustum(double left,double right,double bottom,double top,double zNear,double zFar)
37{
38    osg::Matrix f;
39    f.makeFrustum(left,right,bottom,top,zNear,zFar);
40
41    double c_left=0;
42    double c_right=0;
43    double c_top=0;
44    double c_bottom=0;
45    double c_zNear=0;
46    double c_zFar=0;
47   
48   
49    std::cout << "testFrustum"<<f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar)<<std::endl;
50    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
51    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
52
53    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
54    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
55
56    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
57    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
58   
59    std::cout << std::endl;
60}
61
62void testOrtho(double left,double right,double bottom,double top,double zNear,double zFar)
63{
64    osg::Matrix f;
65    f.makeOrtho(left,right,bottom,top,zNear,zFar);
66
67    double c_left=0;
68    double c_right=0;
69    double c_top=0;
70    double c_bottom=0;
71    double c_zNear=0;
72    double c_zFar=0;
73
74    std::cout << "testOrtho "<< f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar) << std::endl;
75    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
76    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
77
78    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
79    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
80
81    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
82    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
83   
84    std::cout << std::endl;
85}
86
87void testPerspective(double fovy,double aspect,double zNear,double zFar)
88{
89    osg::Matrix f;
90    f.makePerspective(fovy,aspect,zNear,zFar);
91
92    double c_fovy=0;
93    double c_aspect=0;
94    double c_zNear=0;
95    double c_zFar=0;
96
97    std::cout << "testPerspective "<< f.getPerspective(c_fovy,c_aspect,c_zNear,c_zFar) << std::endl;
98    std::cout << "  fovy = "<<fovy<<" compute "<<c_fovy<<std::endl;
99    std::cout << "  aspect = "<<aspect<<" compute "<<c_aspect<<std::endl;
100
101    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
102    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
103   
104    std::cout << std::endl;
105}
106
107void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
108{
109    osg::Matrix mv;
110    mv.makeLookAt(eye,center,up);
111   
112    osg::Vec3 c_eye,c_center,c_up;
113    mv.getLookAt(c_eye,c_center,c_up);
114   
115    std::cout << "testLookAt"<<std::endl;
116    std::cout << "  eye "<<eye<< " compute "<<c_eye<<std::endl;
117    std::cout << "  center "<<center<< " compute "<<c_center<<std::endl;
118    std::cout << "  up "<<up<< " compute "<<c_up<<std::endl;
119   
120    std::cout << std::endl;
121   
122}
123
124
125void testMatrixInvert(const osg::Matrix& matrix)
126{
127    //Invert it twice using the two inversion functions and view the results
128    osg::notify(osg::NOTICE)<<"testMatrixInvert("<<std::endl;
129    osg::notify(osg::NOTICE)<<matrix<<std::endl;
130    osg::notify(osg::NOTICE)<<")"<<std::endl;
131
132    osg::Matrix invM1_0;
133    invM1_0.invert(matrix);
134    osg::notify(osg::NOTICE)<<"Matrix::invert"<<std::endl;
135    osg::notify(osg::NOTICE)<<invM1_0<<std::endl;
136    osg::Matrix default_result = matrix*invM1_0;
137    osg::notify(osg::NOTICE)<<"matrix * invert="<<std::endl;
138    osg::notify(osg::NOTICE)<<default_result<<std::endl;;
139
140}
141
142void sizeOfTest()
143{
144  std::cout<<"sizeof(bool)=="<<sizeof(bool)<<std::endl;
145  std::cout<<"sizeof(char)=="<<sizeof(char)<<std::endl;
146  std::cout<<"sizeof(short)=="<<sizeof(short)<<std::endl;
147  std::cout<<"sizeof(short int)=="<<sizeof(short int)<<std::endl;
148  std::cout<<"sizeof(int)=="<<sizeof(int)<<std::endl;
149  std::cout<<"sizeof(long)=="<<sizeof(long)<<std::endl;
150  std::cout<<"sizeof(long int)=="<<sizeof(long int)<<std::endl;
151
152#if defined(_MSC_VER)
153  // long long isn't supported on VS6.0...
154  std::cout<<"sizeof(__int64)=="<<sizeof(__int64)<<std::endl;
155#else
156  std::cout<<"sizeof(long long)=="<<sizeof(long long)<<std::endl;
157#endif
158  std::cout<<"sizeof(float)=="<<sizeof(float)<<std::endl;
159  std::cout<<"sizeof(double)=="<<sizeof(double)<<std::endl;
160
161  std::cout<<"sizeof(std::istream::pos_type)=="<<sizeof(std::istream::pos_type)<<std::endl;
162  std::cout<<"sizeof(std::istream::off_type)=="<<sizeof(std::istream::off_type)<<std::endl;
163  std::cout<<"sizeof(OpenThreads::Mutex)=="<<sizeof(OpenThreads::Mutex)<<std::endl;
164
165  std::cout<<"sizeof(std::string)=="<<sizeof(std::string)<<std::endl;
166
167}
168
169/// Exercise the Matrix.getRotate function.
170/// Compare the output of:
171///  q1 * q2
172/// versus
173///  (mat(q1)*mat(q2)*scale).getRotate()
174/// for a range of rotations
175void testGetQuatFromMatrix(const osg::Vec3d& scale)
176{
177   
178    // Options
179   
180    // acceptable error range
181    double eps=1e-6;
182
183    // scale matrix
184    // To not test with scale, use 1,1,1
185    // Not sure if 0's or negative values are acceptable
186    osg::Matrixd scalemat;
187    scalemat.makeScale(scale);
188   
189    // range of rotations
190#if 1
191    // wide range
192    double rol1start = 0.0;
193    double rol1stop = 360.0;
194    double rol1step = 20.0;
195
196    double pit1start = 0.0;
197    double pit1stop = 90.0;
198    double pit1step = 20.0;
199
200    double yaw1start = 0.0;
201    double yaw1stop = 360.0;
202    double yaw1step = 20.0;
203
204    double rol2start = 0.0;
205    double rol2stop = 360.0;
206    double rol2step = 20.0;
207
208    double pit2start = 0.0;
209    double pit2stop = 90.0;
210    double pit2step = 20.0;
211
212    double yaw2start = 0.0;
213    double yaw2stop = 360.0;
214    double yaw2step = 20.0;
215#else
216    // focussed range
217    double rol1start = 0.0;
218    double rol1stop = 0.0;
219    double rol1step = 0.1;
220
221    double pit1start = 0.0;
222    double pit1stop = 5.0;
223    double pit1step = 5.0;
224
225    double yaw1start = 89.0;
226    double yaw1stop = 91.0;
227    double yaw1step = 0.1;
228
229    double rol2start = 0.0;
230    double rol2stop = 0.0;
231    double rol2step = 0.1;
232
233    double pit2start = 0.0;
234    double pit2stop = 0.0;
235    double pit2step = 0.1;
236
237    double yaw2start = 89.0;
238    double yaw2stop = 91.0;
239    double yaw2step = 0.1;
240#endif
241
242    std::cout << std::endl << "Starting testGetQuatFromMatrix, it can take a while ..." << std::endl;
243
244    osg::Timer_t tstart, tstop;
245    tstart = osg::Timer::instance()->tick();
246    int count=0;
247    for (double rol1 = rol1start; rol1 <= rol1stop; rol1 += rol1step) {
248        for (double pit1 = pit1start; pit1 <= pit1stop; pit1 += pit1step) {
249            for (double yaw1 = yaw1start; yaw1 <= yaw1stop; yaw1 += yaw1step) {
250                for (double rol2 = rol2start; rol2 <= rol2stop; rol2 += rol2step) {
251                    for (double pit2 = pit2start; pit2 <= pit2stop; pit2 += pit2step) {
252                        for (double yaw2 = yaw2start; yaw2 <= yaw2stop; yaw2 += yaw2step)
253                        {
254                            count++;
255                            // create two quats based on the roll, pitch and yaw values
256                            osg::Quat rot_quat1 =
257                            osg::Quat(osg::DegreesToRadians(rol1),osg::Vec3d(1,0,0),
258                                  osg::DegreesToRadians(pit1),osg::Vec3d(0,1,0),
259                                  osg::DegreesToRadians(yaw1),osg::Vec3d(0,0,1));
260
261                            osg::Quat rot_quat2 =
262                            osg::Quat(osg::DegreesToRadians(rol2),osg::Vec3d(1,0,0),
263                                  osg::DegreesToRadians(pit2),osg::Vec3d(0,1,0),
264                                  osg::DegreesToRadians(yaw2),osg::Vec3d(0,0,1));
265
266                            // create an output quat using quaternion math
267                            osg::Quat out_quat1;
268                            out_quat1 = rot_quat2 * rot_quat1;
269
270                            // create two matrices based on the input quats
271                            osg::Matrixd mat1,mat2;
272                            mat1.makeRotate(rot_quat1);
273                            mat2.makeRotate(rot_quat2);
274
275                            // create an output quat by matrix multiplication and getRotate
276                            osg::Matrixd out_mat;
277                            out_mat = mat2 * mat1;
278                            // add matrix scale for even more nastiness
279                            out_mat = out_mat * scalemat;
280                            osg::Quat out_quat2;
281                            out_quat2 = out_mat.getRotate();
282
283                            // If the quaternion W is <0, then we should reflect
284                            // to get it into the positive W.
285                            // Unfortunately, when W is very small (close to 0), the sign
286                            // does not really make sense because of precision problems
287                            // and the reflection might not work.
288                            if(out_quat1.w()<0) out_quat1 = out_quat1 * -1.0;
289                            if(out_quat2.w()<0) out_quat2 = out_quat2 * -1.0;
290
291                            // if the output quat length is not one
292                            // or if the components do not match,
293                            // something is amiss
294
295                            bool componentsOK = false;
296                            if ( ((fabs(out_quat1.x()-out_quat2.x())) < eps) &&
297                                 ((fabs(out_quat1.y()-out_quat2.y())) < eps) &&
298                                 ((fabs(out_quat1.z()-out_quat2.z())) < eps) &&
299                                 ((fabs(out_quat1.w()-out_quat2.w())) < eps) )
300                                    {
301                                componentsOK = true;
302                            }
303                            // We should also test for q = -q which is valid, so reflect
304                            // one quat.
305                            out_quat2 = out_quat2 * -1.0;
306                            if ( ((fabs(out_quat1.x()-out_quat2.x())) < eps) &&
307                                 ((fabs(out_quat1.y()-out_quat2.y())) < eps) &&
308                                 ((fabs(out_quat1.z()-out_quat2.z())) < eps) &&
309                                 ((fabs(out_quat1.w()-out_quat2.w())) < eps) )
310                            {
311                                componentsOK = true;
312                            }
313
314                            bool lengthOK = false;
315                            if (fabs(1.0-out_quat2.length()) < eps)
316                            {
317                                lengthOK = true;
318                            }
319
320                            if (!lengthOK || !componentsOK)
321                            {
322                                std::cout << "testGetQuatFromMatrix problem at: \n"
323                                      << " r1=" << rol1
324                                      << " p1=" << pit1
325                                      << " y1=" << yaw1
326                                      << " r2=" << rol2
327                                      << " p2=" << pit2
328                                      << " y2=" << yaw2 << "\n";
329                                std::cout << "quats:        " << out_quat1 << " length: " << out_quat1.length() << "\n";
330                                std::cout << "mats and get: " << out_quat2 << " length: " << out_quat2.length() << "\n\n";
331                            }
332                        }
333                    }
334                }
335            }
336        }
337    }
338    tstop = osg::Timer::instance()->tick();
339    double duration = osg::Timer::instance()->delta_s(tstart,tstop);
340    std::cout << "Time for testGetQuatFromMatrix with " << count << " iterations: " << duration << std::endl << std::endl;
341}
342
343void testQuatRotate(const osg::Vec3d& from, const osg::Vec3d& to)
344{
345    osg::Quat q_nicolas;
346    q_nicolas.makeRotate(from,to);
347   
348    osg::Quat q_original;
349    q_original.makeRotate_original(from,to);
350   
351    std::cout<<"osg::Quat::makeRotate("<<from<<", "<<to<<")"<<std::endl;
352    std::cout<<"  q_nicolas = "<<q_nicolas<<std::endl;
353    std::cout<<"  q_original = "<<q_original<<std::endl;
354    std::cout<<"  from * M4x4(q_nicolas) = "<<from * osg::Matrixd::rotate(q_nicolas)<<std::endl;
355    std::cout<<"  from * M4x4(q_original) = "<<from * osg::Matrixd::rotate(q_original)<<std::endl;
356}
357
358void testQuat(const osg::Vec3d& quat_scale)
359{
360    osg::Quat q1;
361    q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f);
362
363    osg::Quat q2;
364    q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f);
365
366    osg::Quat q1_2 = q1*q2;
367    osg::Quat q2_1 = q2*q1;
368
369    osg::Matrix m1 = osg::Matrix::rotate(q1);
370    osg::Matrix m2 = osg::Matrix::rotate(q2);
371   
372    osg::Matrix m1_2 = m1*m2;
373    osg::Matrix m2_1 = m2*m1;
374   
375    osg::Quat qm1_2;
376    qm1_2.set(m1_2);
377   
378    osg::Quat qm2_1;
379    qm2_1.set(m2_1);
380   
381    std::cout<<"q1*q2 = "<<q1_2<<std::endl;
382    std::cout<<"q2*q1 = "<<q2_1<<std::endl;
383    std::cout<<"m1*m2 = "<<qm1_2<<std::endl;
384    std::cout<<"m2*m1 = "<<qm2_1<<std::endl;
385
386
387    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(0.0,1.0,0.0));
388    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(1.0,0.0,0.0));
389    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,1.0,0.0));
390    testQuatRotate(osg::Vec3d(1.0,1.0,1.0),osg::Vec3d(1.0,0.0,0.0));
391    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
392    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(-1.0,0.0,0.0));
393    testQuatRotate(osg::Vec3d(-1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
394    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(0.0,-1.0,0.0));
395    testQuatRotate(osg::Vec3d(0.0,-1.0,0.0),osg::Vec3d(0.0,1.0,0.0));
396    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,0.0,-1.0));
397    testQuatRotate(osg::Vec3d(0.0,0.0,-1.0),osg::Vec3d(0.0,0.0,1.0));
398
399    // Test a range of rotations
400    testGetQuatFromMatrix(quat_scale);
401
402    // This is a specific test case for a matrix containing scale and rotation
403    osg::Matrix matrix(0.5, 0.0, 0.0, 0.0,
404                       0.0, 0.5, 0.0, 0.0,
405                       0.0, 0.0, 0.5, 0.0,
406                       1.0, 1.0, 1.0, 1.0);
407                       
408    osg::Quat quat;
409    matrix.get(quat);
410   
411    osg::notify(osg::NOTICE)<<"Matrix = "<<matrix<<"rotation = "<<quat<<", expected quat = (0,0,0,1)"<<std::endl;
412}
413
414void testDecompose()
415{
416    double angx = osg::DegreesToRadians(30.0);
417    double angy = osg::DegreesToRadians(30.0);
418    double angz = osg::DegreesToRadians(30.0);
419
420    osg::Quat qx, qy, qz;
421    qx.makeRotate(angx, osg::Vec3f (1.0f, 0.0f, 0.0f));
422    qy.makeRotate(angy, osg::Vec3f (0.0f, 1.0f, 0.0f));
423    qz.makeRotate(angz, osg::Vec3f (0.0f, 0.0f, 1.0f));
424
425    osg::Quat rotation = qx * qy * qz;
426
427    osg::Matrixf matf;
428    matf.makeRotate(rotation);
429
430    printf ("Test - Matrix::decompos(), input rotation  : %f %f %f %f\n", rotation._v[0], rotation._v[1], rotation._v[2], rotation._v[3]);
431
432    osg::Vec3f transf;
433    osg::Quat  rotf;
434    osg::Vec3f sclf;
435    osg::Quat  sof;
436    matf.decompose (transf, rotf, sclf, sof);
437    printf ("Matrixf::decomposef\n");
438    printf ("Translation      : %f %f %f\n", transf.x(), transf.y(), transf.z());
439    printf ("Rotation         : %f %f %f %f\n", rotf._v[0], rotf._v[1], rotf._v[2], rotf._v[3]);
440    printf ("Scale            : %f %f %f\n", sclf.x(), sclf.y(), sclf.z());
441    printf ("Scale Orientation: %f %f %f %f\n", sof._v[0], sof._v[1], sof._v[2], sof._v[3]);
442
443    osg::Matrixd matd;
444    matd.makeRotate(rotation);
445
446    osg::Vec3f transd;
447    osg::Quat  rotd;
448    osg::Vec3f scld;
449    osg::Quat  sod;
450    matd.decompose (transd, rotd, scld, sod);
451    printf ("Matrixd::decompose\n");
452    printf ("Translation      : %f %f %f\n", transd.x(), transd.y(), transd.z());
453    printf ("Rotation         : %f %f %f %f\n", rotd._v[0], rotd._v[1], rotd._v[2], rotd._v[3]);
454    printf ("Scale            : %f %f %f\n", scld.x(), scld.y(), scld.z());
455    printf ("Scale Orientation: %f %f %f %f\n", sod._v[0], sod._v[1], sod._v[2], sod._v[3]);
456
457    osg::notify(osg::NOTICE)<<std::endl;
458}
459
460class MyThread : public OpenThreads::Thread {
461public:
462    void run(void) { }
463};
464
465class NotifyThread : public OpenThreads::Thread {
466public:
467
468    NotifyThread(osg::NotifySeverity level, const std::string& message):
469    _done(false),
470    _level(level),
471    _message(message) {}
472
473    ~NotifyThread()
474    {
475        _done = true;
476        while(isRunning())
477        {
478            OpenThreads::Thread::YieldCurrentThread();
479        }
480    }
481
482    void run(void)
483    {
484        std::cout << "Entering thread ..." <<_message<< std::endl;
485
486        unsigned int count=0;
487
488        while(!_done)
489        {
490            ++count;
491#if 1
492            osg::notify(_level)<<_message<<this<<"\n";
493#else
494            osg::notify(_level)<<_message<<this<<std::endl;
495#endif
496        }
497
498        std::cout << "Leaving thread ..." <<_message<< " count="<<count<<std::endl;
499    }
500
501    bool                  _done;
502    osg::NotifySeverity   _level;
503    std::string           _message;
504 
505};
506
507void testThreadInitAndExit()
508{
509    std::cout<<"******   Running thread start and delete test   ****** "<<std::endl;
510
511    {
512        MyThread thread;
513        thread.startThread();
514    }
515   
516    // add a sleep to allow the thread start to fall over it its going to.
517    OpenThreads::Thread::microSleep(500000);
518   
519    std::cout<<"pass    thread start and delete test"<<std::endl<<std::endl;
520
521
522    std::cout<<"******   Running notify thread test   ****** "<<std::endl;
523
524    {
525        NotifyThread thread1(osg::INFO,"thread one:");
526        NotifyThread thread2(osg::INFO,"thread two:");
527        NotifyThread thread3(osg::INFO,"thread three:");
528        NotifyThread thread4(osg::INFO,"thread four:");
529        thread1.startThread();
530        thread2.startThread();
531        thread3.startThread();
532        thread4.startThread();
533
534        // add a sleep to allow the thread start to fall over it its going to.
535        OpenThreads::Thread::microSleep(5000000);
536    }
537
538    std::cout<<"pass    noitfy thread test."<<std::endl<<std::endl;
539}
540
541void testPolytope()
542{
543    osg::Polytope pt;
544    pt.setToBoundingBox(osg::BoundingBox(-1000, -1000, -1000, 1000, 1000, 1000));
545    bool bContains = pt.contains(osg::Vec3(0, 0, 0));
546    if (bContains)
547    {
548        std::cout<<"Polytope pt.contains(osg::Vec3(0, 0, 0)) has succeeded."<<std::endl;
549    }
550    else
551    {
552        std::cout<<"Polytope pt.contains(osg::Vec3(0, 0, 0)) has failed."<<std::endl;
553    }
554
555}
556
557
558int main( int argc, char** argv )
559{
560    osg::ArgumentParser arguments(&argc,argv);
561
562    // set up the usage document, in case we need to print out how to use this program.
563    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
564    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
565    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
566    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
567    arguments.getApplicationUsage()->addCommandLineOption("quat","Display extended quaternion tests.");
568    arguments.getApplicationUsage()->addCommandLineOption("quat_scaled sx sy sz","Display extended quaternion tests of pre scaled matrix.");
569    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
570    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
571    arguments.getApplicationUsage()->addCommandLineOption("performance","Display qualified tests.");
572    arguments.getApplicationUsage()->addCommandLineOption("read-threads <numthreads>","Run multi-thread reading test.");
573 
574
575    if (arguments.argc()<=1)
576    {
577        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
578        return 1;
579    }
580
581    bool printQualifiedTest = false;
582    while (arguments.read("qt")) printQualifiedTest = true;
583
584    bool printMatrixTest = false;
585    while (arguments.read("matrix")) printMatrixTest = true;
586
587    bool printSizeOfTest = false;
588    while (arguments.read("sizeof")) printSizeOfTest = true;
589
590    bool printQuatTest = false;
591    while (arguments.read("quat")) printQuatTest = true;
592
593    int numReadThreads = 0;
594    while (arguments.read("read-threads", numReadThreads)) {}
595
596    bool printPolytopeTest = false;
597    while (arguments.read("polytope")) printPolytopeTest = true;
598   
599    bool doTestThreadInitAndExit = false;
600    while (arguments.read("thread")) doTestThreadInitAndExit = true;
601
602    osg::Vec3d quat_scale(1.0,1.0,1.0);
603    while (arguments.read("quat_scaled", quat_scale.x(), quat_scale.y(), quat_scale.z() )) printQuatTest = true;
604
605    bool performanceTest = false;
606    while (arguments.read("p") || arguments.read("performance")) performanceTest = true;
607
608    // if user request help write it out to cout.
609    if (arguments.read("-h") || arguments.read("--help"))
610    {
611        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
612        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
613        return 1;
614    }
615
616    // any option left unread are converted into errors to write out later.
617    arguments.reportRemainingOptionsAsUnrecognized();
618
619    // report any errors if they have occurred when parsing the program arguments.
620    if (arguments.errors())
621    {
622        arguments.writeErrorMessages(std::cout);
623        return 1;
624    }
625   
626    if (printQuatTest)
627    {
628        testQuat(quat_scale);
629    }
630
631    if (printMatrixTest)
632    {
633        std::cout<<"******   Running matrix tests   ******"<<std::endl;
634
635        testFrustum(-1,1,-1,1,1,1000);
636        testFrustum(0,1,1,2,2.5,100000);
637
638        testOrtho(0,1,1,2,2.1,1000);
639        testOrtho(-1,10,1,20,2.5,100000);
640
641        testPerspective(20,1,1,1000);
642        testPerspective(90,2,1,1000);
643
644        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));
645        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));
646       
647        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242, -0.1715,
648                                     0,         0.987960,   0.154710,  0.207295,
649                                     -0.017452, -0.154687,  0.987809, -0.98239,
650                                     0,         0,          0,         1));
651
652        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242,   0.0,
653                                     0.0,        0.987960,   0.154710,   0.0,
654                                     -0.017452, -0.154687,  0.987809,   0.0,
655                                     -0.1715,    0.207295,  -0.98239,   1.0));
656
657        testDecompose();
658
659    }
660   
661    if (printSizeOfTest)
662    {
663        std::cout<<"**** sizeof() tests  ******"<<std::endl;
664       
665        sizeOfTest();
666
667        std::cout<<std::endl;
668    }
669
670
671    if (performanceTest)
672    {
673        std::cout<<"**** performance tests  ******"<<std::endl;
674       
675        runPerformanceTests();
676    }
677
678    if (numReadThreads>0)
679    {
680        runMultiThreadReadTests(numReadThreads, arguments);
681        return 0;
682    }
683
684
685    if (printPolytopeTest)
686    {
687        testPolytope();
688    }
689
690
691    if (printQualifiedTest)
692    {
693         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
694
695         osgUtx::QualifiedTestPrinter printer;
696         osgUtx::TestGraph::instance().root()->accept( printer );   
697         std::cout<<std::endl;
698    }
699
700    if (doTestThreadInitAndExit)
701    {
702        testThreadInitAndExit();
703    }
704
705    std::cout<<"******   Running tests   ******"<<std::endl;
706
707    // Global Data or Context
708    osgUtx::TestContext ctx;
709    osgUtx::TestRunner runner( ctx );
710    runner.specify("root");
711
712    osgUtx::TestGraph::instance().root()->accept( runner );
713
714    return 0;
715}
Note: See TracBrowser for help on using the browser.