| 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 | |
|---|
| 10 | void 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 | |
|---|
| 36 | void 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 | |
|---|
| 61 | void 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 | |
|---|
| 81 | void 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 | |
|---|
| 98 | void 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 | |
|---|
| 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 | std::cout<<"sizeof(OpenThreads::Mutex)=="<<sizeof(OpenThreads::Mutex)<<std::endl; |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | void testQuatRotate(const osg::Vec3d& from, const osg::Vec3d& to) |
|---|
| 124 | { |
|---|
| 125 | osg::Quat q_nicolas; |
|---|
| 126 | q_nicolas.makeRotate(from,to); |
|---|
| 127 | |
|---|
| 128 | osg::Quat q_original; |
|---|
| 129 | q_original.makeRotate_original(from,to); |
|---|
| 130 | |
|---|
| 131 | std::cout<<"osg::Quat::makeRotate("<<from<<", "<<to<<")"<<std::endl; |
|---|
| 132 | std::cout<<" q_nicolas = "<<q_nicolas<<std::endl; |
|---|
| 133 | std::cout<<" q_original = "<<q_original<<std::endl; |
|---|
| 134 | std::cout<<" from * M4x4(q_nicolas) = "<<from * osg::Matrixd::rotate(q_nicolas)<<std::endl; |
|---|
| 135 | std::cout<<" from * M4x4(q_original) = "<<from * osg::Matrixd::rotate(q_original)<<std::endl; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void testQuat() |
|---|
| 139 | { |
|---|
| 140 | osg::Quat q1; |
|---|
| 141 | q1.makeRotate(osg::DegreesToRadians(30.0),0.0f,0.0f,1.0f); |
|---|
| 142 | |
|---|
| 143 | osg::Quat q2; |
|---|
| 144 | q2.makeRotate(osg::DegreesToRadians(133.0),0.0f,1.0f,1.0f); |
|---|
| 145 | |
|---|
| 146 | osg::Quat q1_2 = q1*q2; |
|---|
| 147 | osg::Quat q2_1 = q2*q1; |
|---|
| 148 | |
|---|
| 149 | osg::Matrix m1 = osg::Matrix::rotate(q1); |
|---|
| 150 | osg::Matrix m2 = osg::Matrix::rotate(q2); |
|---|
| 151 | |
|---|
| 152 | osg::Matrix m1_2 = m1*m2; |
|---|
| 153 | osg::Matrix m2_1 = m2*m1; |
|---|
| 154 | |
|---|
| 155 | osg::Quat qm1_2; |
|---|
| 156 | qm1_2.set(m1_2); |
|---|
| 157 | |
|---|
| 158 | osg::Quat qm2_1; |
|---|
| 159 | qm2_1.set(m2_1); |
|---|
| 160 | |
|---|
| 161 | std::cout<<"q1*q2 = "<<q1_2<<std::endl; |
|---|
| 162 | std::cout<<"q2*q1 = "<<q2_1<<std::endl; |
|---|
| 163 | std::cout<<"m1*m2 = "<<qm1_2<<std::endl; |
|---|
| 164 | std::cout<<"m2*m1 = "<<qm2_1<<std::endl; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | testQuatRotate(osg::Vec3d(1.0,0.0,0.0),osg::Vec3d(0.0,1.0,0.0)); |
|---|
| 168 | testQuatRotate(osg::Vec3d(0.0,1.0,0.0),osg::Vec3d(1.0,0.0,0.0)); |
|---|
| 169 | testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,1.0,0.0)); |
|---|
| 170 | testQuatRotate(osg::Vec3d(1.0,1.0,1.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(-1.0,0.0,0.0),osg::Vec3d(1.0,0.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,-1.0,0.0),osg::Vec3d(0.0,1.0,0.0)); |
|---|
| 176 | testQuatRotate(osg::Vec3d(0.0,0.0,1.0),osg::Vec3d(0.0,0.0,-1.0)); |
|---|
| 177 | testQuatRotate(osg::Vec3d(0.0,0.0,-1.0),osg::Vec3d(0.0,0.0,1.0)); |
|---|
| 178 | |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | int main( int argc, char** argv ) |
|---|
| 182 | { |
|---|
| 183 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests."); |
|---|
| 187 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]"); |
|---|
| 188 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 189 | arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests."); |
|---|
| 190 | arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests."); |
|---|
| 191 | arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests."); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | if (arguments.argc()<=1) |
|---|
| 195 | { |
|---|
| 196 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 197 | return 1; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | bool printQualifiedTest = false; |
|---|
| 201 | while (arguments.read("qt")) printQualifiedTest = true; |
|---|
| 202 | |
|---|
| 203 | bool printMatrixTest = false; |
|---|
| 204 | while (arguments.read("matrix")) printMatrixTest = true; |
|---|
| 205 | |
|---|
| 206 | bool printSizeOfTest = false; |
|---|
| 207 | while (arguments.read("sizeof")) printSizeOfTest = true; |
|---|
| 208 | |
|---|
| 209 | bool printQuatTest = false; |
|---|
| 210 | while (arguments.read("quat")) printQuatTest = true; |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 214 | { |
|---|
| 215 | std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl; |
|---|
| 216 | arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions()); |
|---|
| 217 | return 1; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | if (arguments.errors()) |
|---|
| 225 | { |
|---|
| 226 | arguments.writeErrorMessages(std::cout); |
|---|
| 227 | return 1; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | if (printQuatTest) |
|---|
| 231 | { |
|---|
| 232 | testQuat(); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | if (printMatrixTest) |
|---|
| 237 | { |
|---|
| 238 | std::cout<<"****** Running matrix tests ******"<<std::endl; |
|---|
| 239 | |
|---|
| 240 | testFrustum(-1,1,-1,1,1,1000); |
|---|
| 241 | testFrustum(0,1,1,2,2.5,100000); |
|---|
| 242 | |
|---|
| 243 | testOrtho(0,1,1,2,2.1,1000); |
|---|
| 244 | testOrtho(-1,10,1,20,2.5,100000); |
|---|
| 245 | |
|---|
| 246 | testPerspective(20,1,1,1000); |
|---|
| 247 | testPerspective(90,2,1,1000); |
|---|
| 248 | |
|---|
| 249 | 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)); |
|---|
| 250 | 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)); |
|---|
| 251 | |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | if (printSizeOfTest) |
|---|
| 255 | { |
|---|
| 256 | std::cout<<"**** sizeof() tests ******"<<std::endl; |
|---|
| 257 | |
|---|
| 258 | sizeOfTest(); |
|---|
| 259 | |
|---|
| 260 | std::cout<<std::endl; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | if (printQualifiedTest) |
|---|
| 265 | { |
|---|
| 266 | std::cout<<"***** Qualified Tests ******"<<std::endl; |
|---|
| 267 | |
|---|
| 268 | osgUtx::QualifiedTestPrinter printer; |
|---|
| 269 | osgUtx::TestGraph::instance().root()->accept( printer ); |
|---|
| 270 | std::cout<<std::endl; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | std::cout<<"****** Running tests ******"<<std::endl; |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | osgUtx::TestContext ctx; |
|---|
| 277 | osgUtx::TestRunner runner( ctx ); |
|---|
| 278 | runner.specify("root"); |
|---|
| 279 | |
|---|
| 280 | osgUtx::TestGraph::instance().root()->accept( runner ); |
|---|
| 281 | |
|---|
| 282 | return 0; |
|---|
| 283 | } |
|---|