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

Revision 7300, 17.8 kB (checked in by robert, 7 years ago)

From David Spilling, tweaked quat tests to reproduce getRotate errors.

  • 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 quaternion W is <0, then we should reflect
279                // to get it into the positive W
280                if(out_quat1.w()<0) out_quat1 = out_quat1 * -1.0;
281                if(out_quat2.w()<0) out_quat2 = out_quat2 * -1.0;
282
283
284                // if the output quat length is not one
285                // or if the components do not match,
286                // something is amiss
287                if (fabs(1.0-out_quat2.length()) > eps ||
288                (fabs(out_quat1.x()-out_quat2.x())) > eps ||
289                (fabs(out_quat1.y()-out_quat2.y())) > eps ||
290                (fabs(out_quat1.z()-out_quat2.z())) > eps ||
291                (fabs(out_quat1.w()-out_quat2.w())) > eps) {
292                std::cout << __FUNCTION__ << " problem at: \n"
293                      << " r1=" << rol1
294                      << " p1=" << pit1
295                      << " y1=" << yaw1
296                      << " r2=" << rol2
297                      << " p2=" << pit2
298                      << " y2=" << yaw2 << "\n";
299                std::cout << "quats:        " << out_quat1 << " length: " << out_quat1.length() << "\n";
300                std::cout << "mats and get: " << out_quat2 << " length: " << out_quat2.length() << "\n\n";
301                }
302            }
303            }
304        }
305        }
306    }
307    }
308    tstop = osg::Timer::instance()->tick();
309    double duration = osg::Timer::instance()->delta_s(tstart,tstop);
310    std::cout << "Time for " << __FUNCTION__ << " with " << count << " iterations: " << duration << std::endl << std::endl;
311}
312
313void testQuatRotate(const osg::Vec3d& from, const osg::Vec3d& to)
314{
315    osg::Quat q_nicolas;
316    q_nicolas.makeRotate(from,to);
317   
318    osg::Quat q_original;
319    q_original.makeRotate_original(from,to);
320   
321    std::cout<<"osg::Quat::makeRotate("<<from<<", "<<to<<")"<<std::endl;
322    std::cout<<"  q_nicolas = "<<q_nicolas<<std::endl;
323    std::cout<<"  q_original = "<<q_original<<std::endl;
324    std::cout<<"  from * M4x4(q_nicolas) = "<<from * osg::Matrixd::rotate(q_nicolas)<<std::endl;
325    std::cout<<"  from * M4x4(q_original) = "<<from * osg::Matrixd::rotate(q_original)<<std::endl;
326}
327
328void testQuat(const osg::Vec3d& quat_scale)
329{
330    osg::Quat q1;
331    q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f);
332
333    osg::Quat q2;
334    q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f);
335
336    osg::Quat q1_2 = q1*q2;
337    osg::Quat q2_1 = q2*q1;
338
339    osg::Matrix m1 = osg::Matrix::rotate(q1);
340    osg::Matrix m2 = osg::Matrix::rotate(q2);
341   
342    osg::Matrix m1_2 = m1*m2;
343    osg::Matrix m2_1 = m2*m1;
344   
345    osg::Quat qm1_2;
346    qm1_2.set(m1_2);
347   
348    osg::Quat qm2_1;
349    qm2_1.set(m2_1);
350   
351    std::cout<<"q1*q2 = "<<q1_2<<std::endl;
352    std::cout<<"q2*q1 = "<<q2_1<<std::endl;
353    std::cout<<"m1*m2 = "<<qm1_2<<std::endl;
354    std::cout<<"m2*m1 = "<<qm2_1<<std::endl;
355
356
357    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(0.0,1.0,0.0));
358    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(1.0,0.0,0.0));
359    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,1.0,0.0));
360    testQuatRotate(osg::Vec3d(1.0,1.0,1.0),osg::Vec3d(1.0,0.0,0.0));
361    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
362    testQuatRotate(osg::Vec3d(1.0,0.0,0.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(0.0,1.0,0.0),osg::Vec3d(0.0,-1.0,0.0));
365    testQuatRotate(osg::Vec3d(0.0,-1.0,0.0),osg::Vec3d(0.0,1.0,0.0));
366    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,0.0,-1.0));
367    testQuatRotate(osg::Vec3d(0.0,0.0,-1.0),osg::Vec3d(0.0,0.0,1.0));
368
369    // Test a range of rotations
370    testGetQuatFromMatrix(quat_scale);
371
372    // This is a specific test case for a matrix containing scale and rotation
373    osg::Matrix matrix(0.5, 0.0, 0.0, 0.0,
374                       0.0, 0.5, 0.0, 0.0,
375                       0.0, 0.0, 0.5, 0.0,
376                       1.0, 1.0, 1.0, 1.0);
377                       
378    osg::Quat quat;
379    matrix.get(quat);
380   
381    osg::notify(osg::NOTICE)<<"Matrix = "<<matrix<<"rotation = "<<quat<<", expected quat = (0,0,0,1)"<<std::endl;
382}
383
384
385int main( int argc, char** argv )
386{
387    osg::ArgumentParser arguments(&argc,argv);
388
389    // set up the usage document, in case we need to print out how to use this program.
390    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
391    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
392    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
393    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
394    arguments.getApplicationUsage()->addCommandLineOption("quat","Display extended quaternion tests.");
395    arguments.getApplicationUsage()->addCommandLineOption("quat_scaled sx sy sz","Display extended quaternion tests of pre scaled matrix.");
396    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
397    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
398    arguments.getApplicationUsage()->addCommandLineOption("performance","Display qualified tests.");
399 
400
401    if (arguments.argc()<=1)
402    {
403        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
404        return 1;
405    }
406
407    bool printQualifiedTest = false;
408    while (arguments.read("qt")) printQualifiedTest = true;
409
410    bool printMatrixTest = false;
411    while (arguments.read("matrix")) printMatrixTest = true;
412
413    bool printSizeOfTest = false;
414    while (arguments.read("sizeof")) printSizeOfTest = true;
415
416    bool printQuatTest = false;
417    while (arguments.read("quat")) printQuatTest = true;
418
419    osg::Vec3d quat_scale(1.0,1.0,1.0);
420    while (arguments.read("quat_scaled", quat_scale.x(), quat_scale.y(), quat_scale.z() )) printQuatTest = true;
421
422    bool performanceTest = false;
423    while (arguments.read("p") || arguments.read("performance")) performanceTest = true;
424
425    // if user request help write it out to cout.
426    if (arguments.read("-h") || arguments.read("--help"))
427    {
428        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
429        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
430        return 1;
431    }
432
433    // any option left unread are converted into errors to write out later.
434    arguments.reportRemainingOptionsAsUnrecognized();
435
436    // report any errors if they have occured when parsing the program aguments.
437    if (arguments.errors())
438    {
439        arguments.writeErrorMessages(std::cout);
440        return 1;
441    }
442   
443    if (printQuatTest)
444    {
445        testQuat(quat_scale);
446    }
447
448
449    if (printMatrixTest)
450    {
451        std::cout<<"******   Running matrix tests   ******"<<std::endl;
452
453        testFrustum(-1,1,-1,1,1,1000);
454        testFrustum(0,1,1,2,2.5,100000);
455
456        testOrtho(0,1,1,2,2.1,1000);
457        testOrtho(-1,10,1,20,2.5,100000);
458
459        testPerspective(20,1,1,1000);
460        testPerspective(90,2,1,1000);
461
462        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));
463        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));
464       
465        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242, -0.1715,
466                                     0,         0.987960,   0.154710,  0.207295,
467                                     -0.017452, -0.154687,  0.987809, -0.98239,
468                                     0,         0,          0,         1));
469
470        testMatrixInvert(osg::Matrix(0.999848,  -0.002700,  0.017242,   0.0,
471                                     0.0,        0.987960,   0.154710,   0.0,
472                                     -0.017452, -0.154687,  0.987809,   0.0,
473                                     -0.1715,    0.207295,  -0.98239,   1.0));
474
475    }
476   
477    if (printSizeOfTest)
478    {
479        std::cout<<"**** sizeof() tests  ******"<<std::endl;
480       
481        sizeOfTest();
482
483        std::cout<<std::endl;
484    }
485
486
487    if (performanceTest)
488    {
489        std::cout<<"**** performance tests  ******"<<std::endl;
490       
491        runPerformanceTests();
492    }
493
494
495    if (printQualifiedTest)
496    {
497         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
498
499         osgUtx::QualifiedTestPrinter printer;
500         osgUtx::TestGraph::instance().root()->accept( printer );   
501         std::cout<<std::endl;
502    }
503
504    std::cout<<"******   Running tests   ******"<<std::endl;
505
506    // Global Data or Context
507    osgUtx::TestContext ctx;
508    osgUtx::TestRunner runner( ctx );
509    runner.specify("root");
510
511    osgUtx::TestGraph::instance().root()->accept( runner );
512
513    return 0;
514}
Note: See TracBrowser for help on using the browser.