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

Revision 3225, 7.5 kB (checked in by robert, 10 years ago)

Added test for quat multiplication ordering.

  • 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
117
118void testQuat()
119{
120    osg::Quat q1;
121    q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f);
122
123    osg::Quat q2;
124    q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f);
125
126    osg::Quat q1_2 = q1*q2;
127    osg::Quat q2_1 = q2*q1;
128
129    osg::Matrix m1 = osg::Matrix::rotate(q1);
130    osg::Matrix m2 = osg::Matrix::rotate(q2);
131   
132    osg::Matrix m1_2 = m1*m2;
133    osg::Matrix m2_1 = m2*m1;
134   
135    osg::Quat qm1_2;
136    qm1_2.set(m1_2);
137   
138    osg::Quat qm2_1;
139    qm2_1.set(m2_1);
140   
141    std::cout<<"q1*q2 = "<<q1_2<<std::endl;
142    std::cout<<"q2*q1 = "<<q2_1<<std::endl;
143    std::cout<<"m1*m2 = "<<qm1_2<<std::endl;
144    std::cout<<"m2*m1 = "<<qm2_1<<std::endl;
145
146   
147}
148
149int main( int argc, char** argv )
150{
151    osg::ArgumentParser arguments(&argc,argv);
152
153    // set up the usage document, in case we need to print out how to use this program.
154    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
155    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
156    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
157    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
158    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
159    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
160 
161
162    if (arguments.argc()<=1)
163    {
164        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
165        return 1;
166    }
167
168    bool printQualifiedTest = false;
169    while (arguments.read("qt")) printQualifiedTest = true;
170
171    bool printMatrixTest = false;
172    while (arguments.read("matrix")) printMatrixTest = true;
173
174    bool printSizeOfTest = false;
175    while (arguments.read("sizeof")) printSizeOfTest = true;
176
177    bool printQuatTest = false;
178    while (arguments.read("quat")) printQuatTest = true;
179
180    // if user request help write it out to cout.
181    if (arguments.read("-h") || arguments.read("--help"))
182    {
183        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
184        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
185        return 1;
186    }
187
188    // any option left unread are converted into errors to write out later.
189    arguments.reportRemainingOptionsAsUnrecognized();
190
191    // report any errors if they have occured when parsing the program aguments.
192    if (arguments.errors())
193    {
194        arguments.writeErrorMessages(std::cout);
195        return 1;
196    }
197   
198    if (printQuatTest)
199    {
200        testQuat();
201    }
202
203
204    if (printMatrixTest)
205    {
206        std::cout<<"******   Running matrix tests   ******"<<std::endl;
207
208        testFrustum(-1,1,-1,1,1,1000);
209        testFrustum(0,1,1,2,2.5,100000);
210
211        testOrtho(0,1,1,2,2.1,1000);
212        testOrtho(-1,10,1,20,2.5,100000);
213
214        testPerspective(20,1,1,1000);
215        testPerspective(90,2,1,1000);
216
217        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));
218        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));
219
220    }
221   
222    if (printSizeOfTest)
223    {
224        std::cout<<"**** sizeof() tests  ******"<<std::endl;
225       
226        sizeOfTest();
227
228        std::cout<<std::endl;
229    }
230
231
232    if (printQualifiedTest)
233    {
234         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
235
236         osgUtx::QualifiedTestPrinter printer;
237         osgUtx::TestGraph::instance().root()->accept( printer );   
238         std::cout<<std::endl;
239    }
240
241    std::cout<<"******   Running tests   ******"<<std::endl;
242
243    // Global Data or Context
244    osgUtx::TestContext ctx;
245    osgUtx::TestRunner runner( ctx );
246    runner.specify("root");
247
248    osgUtx::TestGraph::instance().root()->accept( runner );
249
250    return 0;
251}
Note: See TracBrowser for help on using the browser.