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

Revision 5219, 9.6 kB (checked in by robert, 8 years ago)

Added a performace test section to osgunitests, currently just does basic C/C++ tests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/UnitTestFramework>
2#include <osg/ArgumentParser>
3#include <osg/ApplicationUsage>
4
5#include <osg/Vec3>
6#include <osg/Matrix>
7#include <osg/io_utils>
8
9#include "performance.h"
10
11#include <iostream>
12
13void testFrustum(double left,double right,double bottom,double top,double zNear,double zFar)
14{
15    osg::Matrix f;
16    f.makeFrustum(left,right,bottom,top,zNear,zFar);
17
18    double c_left=0;
19    double c_right=0;
20    double c_top=0;
21    double c_bottom=0;
22    double c_zNear=0;
23    double c_zFar=0;
24   
25   
26    std::cout << "testFrustum"<<f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar)<<std::endl;
27    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
28    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
29
30    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
31    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
32
33    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
34    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
35   
36    std::cout << std::endl;
37}
38
39void testOrtho(double left,double right,double bottom,double top,double zNear,double zFar)
40{
41    osg::Matrix f;
42    f.makeOrtho(left,right,bottom,top,zNear,zFar);
43
44    double c_left=0;
45    double c_right=0;
46    double c_top=0;
47    double c_bottom=0;
48    double c_zNear=0;
49    double c_zFar=0;
50
51    std::cout << "testOrtho "<< f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar) << std::endl;
52    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
53    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
54
55    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
56    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
57
58    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
59    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
60   
61    std::cout << std::endl;
62}
63
64void testPerspective(double fovy,double aspect,double zNear,double zFar)
65{
66    osg::Matrix f;
67    f.makePerspective(fovy,aspect,zNear,zFar);
68
69    double c_fovy=0;
70    double c_aspect=0;
71    double c_zNear=0;
72    double c_zFar=0;
73
74    std::cout << "testPerspective "<< f.getPerspective(c_fovy,c_aspect,c_zNear,c_zFar) << std::endl;
75    std::cout << "  fovy = "<<fovy<<" compute "<<c_fovy<<std::endl;
76    std::cout << "  aspect = "<<aspect<<" compute "<<c_aspect<<std::endl;
77
78    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
79    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
80   
81    std::cout << std::endl;
82}
83
84void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
85{
86    osg::Matrix mv;
87    mv.makeLookAt(eye,center,up);
88   
89    osg::Vec3 c_eye,c_center,c_up;
90    mv.getLookAt(c_eye,c_center,c_up);
91   
92    std::cout << "testLookAt"<<std::endl;
93    std::cout << "  eye "<<eye<< " compute "<<c_eye<<std::endl;
94    std::cout << "  center "<<center<< " compute "<<c_center<<std::endl;
95    std::cout << "  up "<<up<< " compute "<<c_up<<std::endl;
96   
97    std::cout << std::endl;
98   
99}
100
101void sizeOfTest()
102{
103  std::cout<<"sizeof(bool)=="<<sizeof(bool)<<std::endl;
104  std::cout<<"sizeof(char)=="<<sizeof(char)<<std::endl;
105  std::cout<<"sizeof(short)=="<<sizeof(short)<<std::endl;
106  std::cout<<"sizeof(int)=="<<sizeof(int)<<std::endl;
107  std::cout<<"sizeof(long)=="<<sizeof(long)<<std::endl;
108  std::cout<<"sizeof(long int)=="<<sizeof(long int)<<std::endl;
109
110#if defined(_MSC_VER)
111  // long long isn't supported on VS6.0...
112  std::cout<<"sizeof(__int64)=="<<sizeof(__int64)<<std::endl;
113#else
114  std::cout<<"sizeof(long long)=="<<sizeof(long long)<<std::endl;
115#endif
116  std::cout<<"sizeof(float)=="<<sizeof(float)<<std::endl;
117  std::cout<<"sizeof(double)=="<<sizeof(double)<<std::endl;
118
119  std::cout<<"sizeof(std::istream::pos_type)=="<<sizeof(std::istream::pos_type)<<std::endl;
120  std::cout<<"sizeof(std::istream::off_type)=="<<sizeof(std::istream::off_type)<<std::endl;
121  std::cout<<"sizeof(OpenThreads::Mutex)=="<<sizeof(OpenThreads::Mutex)<<std::endl;
122
123  std::cout<<"sizeof(std::string)=="<<sizeof(std::string)<<std::endl;
124
125}
126
127
128void testQuatRotate(const osg::Vec3d& from, const osg::Vec3d& to)
129{
130    osg::Quat q_nicolas;
131    q_nicolas.makeRotate(from,to);
132   
133    osg::Quat q_original;
134    q_original.makeRotate_original(from,to);
135   
136    std::cout<<"osg::Quat::makeRotate("<<from<<", "<<to<<")"<<std::endl;
137    std::cout<<"  q_nicolas = "<<q_nicolas<<std::endl;
138    std::cout<<"  q_original = "<<q_original<<std::endl;
139    std::cout<<"  from * M4x4(q_nicolas) = "<<from * osg::Matrixd::rotate(q_nicolas)<<std::endl;
140    std::cout<<"  from * M4x4(q_original) = "<<from * osg::Matrixd::rotate(q_original)<<std::endl;
141}
142
143void testQuat()
144{
145    osg::Quat q1;
146    q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f);
147
148    osg::Quat q2;
149    q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f);
150
151    osg::Quat q1_2 = q1*q2;
152    osg::Quat q2_1 = q2*q1;
153
154    osg::Matrix m1 = osg::Matrix::rotate(q1);
155    osg::Matrix m2 = osg::Matrix::rotate(q2);
156   
157    osg::Matrix m1_2 = m1*m2;
158    osg::Matrix m2_1 = m2*m1;
159   
160    osg::Quat qm1_2;
161    qm1_2.set(m1_2);
162   
163    osg::Quat qm2_1;
164    qm2_1.set(m2_1);
165   
166    std::cout<<"q1*q2 = "<<q1_2<<std::endl;
167    std::cout<<"q2*q1 = "<<q2_1<<std::endl;
168    std::cout<<"m1*m2 = "<<qm1_2<<std::endl;
169    std::cout<<"m2*m1 = "<<qm2_1<<std::endl;
170
171
172    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(0.0,1.0,0.0));
173    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(1.0,0.0,0.0));
174    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,1.0,0.0));
175    testQuatRotate(osg::Vec3d(1.0,1.0,1.0),osg::Vec3d(1.0,0.0,0.0));
176    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
177    testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(-1.0,0.0,0.0));
178    testQuatRotate(osg::Vec3d(-1.0,0.0,0.0),osg::Vec3d(1.0,0.0,0.0));
179    testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(0.0,-1.0,0.0));
180    testQuatRotate(osg::Vec3d(0.0,-1.0,0.0),osg::Vec3d(0.0,1.0,0.0));
181    testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,0.0,-1.0));
182    testQuatRotate(osg::Vec3d(0.0,0.0,-1.0),osg::Vec3d(0.0,0.0,1.0));
183   
184}
185
186int main( int argc, char** argv )
187{
188    osg::ArgumentParser arguments(&argc,argv);
189
190    // set up the usage document, in case we need to print out how to use this program.
191    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
192    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
193    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
194    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
195    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
196    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
197    arguments.getApplicationUsage()->addCommandLineOption("performance","Display qualified tests.");
198 
199
200    if (arguments.argc()<=1)
201    {
202        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
203        return 1;
204    }
205
206    bool printQualifiedTest = false;
207    while (arguments.read("qt")) printQualifiedTest = true;
208
209    bool printMatrixTest = false;
210    while (arguments.read("matrix")) printMatrixTest = true;
211
212    bool printSizeOfTest = false;
213    while (arguments.read("sizeof")) printSizeOfTest = true;
214
215    bool printQuatTest = false;
216    while (arguments.read("quat")) printQuatTest = true;
217
218    bool performanceTest = false;
219    while (arguments.read("p") || arguments.read("performance")) performanceTest = true;
220
221    // if user request help write it out to cout.
222    if (arguments.read("-h") || arguments.read("--help"))
223    {
224        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
225        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
226        return 1;
227    }
228
229    // any option left unread are converted into errors to write out later.
230    arguments.reportRemainingOptionsAsUnrecognized();
231
232    // report any errors if they have occured when parsing the program aguments.
233    if (arguments.errors())
234    {
235        arguments.writeErrorMessages(std::cout);
236        return 1;
237    }
238   
239    if (printQuatTest)
240    {
241        testQuat();
242    }
243
244
245    if (printMatrixTest)
246    {
247        std::cout<<"******   Running matrix tests   ******"<<std::endl;
248
249        testFrustum(-1,1,-1,1,1,1000);
250        testFrustum(0,1,1,2,2.5,100000);
251
252        testOrtho(0,1,1,2,2.1,1000);
253        testOrtho(-1,10,1,20,2.5,100000);
254
255        testPerspective(20,1,1,1000);
256        testPerspective(90,2,1,1000);
257
258        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));
259        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));
260
261    }
262   
263    if (printSizeOfTest)
264    {
265        std::cout<<"**** sizeof() tests  ******"<<std::endl;
266       
267        sizeOfTest();
268
269        std::cout<<std::endl;
270    }
271
272
273    if (performanceTest)
274    {
275        std::cout<<"**** performance tests  ******"<<std::endl;
276       
277        runPerformanceTests();
278    }
279
280
281    if (printQualifiedTest)
282    {
283         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
284
285         osgUtx::QualifiedTestPrinter printer;
286         osgUtx::TestGraph::instance().root()->accept( printer );   
287         std::cout<<std::endl;
288    }
289
290    std::cout<<"******   Running tests   ******"<<std::endl;
291
292    // Global Data or Context
293    osgUtx::TestContext ctx;
294    osgUtx::TestRunner runner( ctx );
295    runner.specify("root");
296
297    osgUtx::TestGraph::instance().root()->accept( runner );
298
299    return 0;
300}
Note: See TracBrowser for help on using the browser.