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

Revision 2039, 5.3 kB (checked in by robert, 11 years ago)

Added osg::Matrix::getOtho,getFrustum and getLookAt() methods.

Added test for new matrix methods into unit tests example, but these really
should go in their own lib...

  • 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/Matrix>
6
7
8void testFrustum(double left,double right,double bottom,double top,double zNear,double zFar)
9{
10    osg::Matrix f;
11    f.makeFrustum(left,right,bottom,top,zNear,zFar);
12
13   
14    double c_zNear = f(3,2) / (f(2,2)-1.0f);
15    double c_zFar = f(3,2) / (1.0f+f(2,2));
16   
17    double c_left = c_zNear * (f(2,0)-1.0f) / f(0,0);
18    double c_right = c_zNear * (1.0f+f(2,0)) / f(0,0);
19
20    double c_top = c_zNear * (1+f(2,1)) / f(1,1);
21    double c_bottom = c_zNear * (f(2,1)-1.0f) / f(1,1);
22   
23    f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);
24
25    std::cout << "testFrustum"<<std::endl;
26    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
27    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
28
29    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
30    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
31
32    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
33    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
34   
35    std::cout << std::endl;
36}
37
38void testOrtho(double left,double right,double bottom,double top,double zNear,double zFar)
39{
40    osg::Matrix f;
41    f.makeOrtho(left,right,bottom,top,zNear,zFar);
42
43    double c_zNear = (f(3,2)+1.0f) / f(2,2);
44    double c_zFar = (f(3,2)-1.0f) / f(2,2);
45   
46    double c_left = -(1.0f+f(3,0)) / f(0,0);
47    double c_right = (1.0f-f(3,0)) / f(0,0);
48
49    double c_bottom = -(1.0f+f(3,1)) / f(1,1);
50    double c_top = (1.0f-f(3,1)) / f(1,1);
51   
52    f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);
53
54
55    std::cout << "testOrtho"<<std::endl;
56    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
57    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
58
59    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
60    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
61
62    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
63    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
64   
65    std::cout << std::endl;
66}
67
68void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
69{
70    osg::Matrix mv;
71    mv.makeLookAt(eye,center,up);
72   
73    osg::Vec3 c_eye,c_center,c_up;
74    mv.getLookAt(c_eye,c_center,c_up);
75   
76    std::cout << "testLookAt"<<std::endl;
77    std::cout << "  eye "<<eye<< " compute "<<c_eye<<std::endl;
78    std::cout << "  eye "<<center<< " compute "<<c_center<<std::endl;
79    std::cout << "  eye "<<up<< " compute "<<c_up<<std::endl;
80   
81    std::cout << std::endl;
82   
83}
84
85int main( int argc, char** argv )
86{
87    osg::ArgumentParser arguments(&argc,argv);
88
89    // set up the usage document, in case we need to print out how to use this program.
90    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
91    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
92    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
93    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
94    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
95 
96
97    if (arguments.argc()<=1)
98    {
99        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
100        return 1;
101    }
102
103    bool printQualifiedTest = false;
104    while (arguments.read("qt")) printQualifiedTest = true;
105
106    bool displayMatrixTest = false;
107    while (arguments.read("matrix")) displayMatrixTest = true;
108
109    // if user request help write it out to cout.
110    if (arguments.read("-h") || arguments.read("--help"))
111    {
112        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
113        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
114        return 1;
115    }
116
117    // any option left unread are converted into errors to write out later.
118    arguments.reportRemainingOptionsAsUnrecognized();
119
120    // report any errors if they have occured when parsing the program aguments.
121    if (arguments.errors())
122    {
123        arguments.writeErrorMessages(std::cout);
124        return 1;
125    }
126   
127
128    if (displayMatrixTest)
129    {
130        std::cout<<"******   Running matrix tests   ******"<<std::endl;
131
132        testFrustum(-1,1,-1,1,1,1000);
133        testFrustum(0,1,1,2,2.5,100000);
134
135        testOrtho(0,1,1,2,2.1,1000);
136        testOrtho(-1,10,1,20,2.5,100000);
137
138        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));
139        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));
140
141    }
142
143
144    if (printQualifiedTest)
145    {
146         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
147
148         osgUtx::QualifiedTestPrinter printer;
149         osgUtx::TestGraph::instance().root()->accept( printer );   
150         std::cout<<std::endl;
151    }
152
153    std::cout<<"******   Running tests   ******"<<std::endl;
154
155    // Global Data or Context
156    osgUtx::TestContext ctx;
157    osgUtx::TestRunner runner( ctx );
158    runner.specify("root");
159
160    osgUtx::TestGraph::instance().root()->accept( runner );
161
162    return 0;
163}
Note: See TracBrowser for help on using the browser.