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

Revision 4058, 9.2 kB (checked in by robert, 10 years ago)

Fixed typo in testLookAt debug info.

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