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

Revision 3790, 9.1 kB (checked in by robert, 9 years ago)

From Nicolas Brodu, new faster osg::Quat::makeRotate(Vec3d,Vec3d) implmentation.

From Robert Osfield, modes to osg::Quat to keep the original implmentation around
as makeRotate_original(,) and added tests into osgunittest to test the new
methods provide equivilant results to the original implemementation. The
orignal implementation will be removed once the new method is more widely tested.

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