fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Drawables / Geometry / Util / testGeoOptimization.cpp
blob9aaa10e7ddb3b15e69b255b6ab0bd3c6078bd84e
2 #include "OSGAction.h"
3 #include "OSGGLUT.h"
4 #include "OSGConfig.h"
5 #include "OSGSimpleGeometry.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGSimpleSceneManager.h"
8 #include "OSGSceneFileHandler.h"
9 #include "OSGGeoOptimization.h"
11 #include <deque>
13 OSG::SimpleSceneManagerRefPtr mgr;
14 OSG::NodeUnrecPtr scene;
15 std::string fileName;
16 OSG::Time startT;
17 OSG::Time endT;
18 std::deque<OSG::Time> timeHistory;
19 OSG::Time avgT = 0.0;
20 bool showFrameTime = false;
22 int setupGLUT(int *argc, char *argv[]);
23 void doInit(int argc, char *argv[]);
25 void doPrintGeoInfo (OSG::Node *scene);
26 void doMakeSingleIndexed (OSG::Node *scene);
27 void doMakeIndexedTriangles (OSG::Node *scene);
28 void doMakeOptimizedIndex (OSG::Node *scene);
29 void doMakeOptimizedProperties(OSG::Node *scene);
31 std::ostream &
32 printTypes(std::ostream &os, OSG::GeoIntegralProperty *types);
33 std::ostream &
34 printGeoInfo(std::ostream &os, OSG::Geometry *geo);
37 int main(int argc, char *argv[])
39 doInit(argc, argv);
41 glutMainLoop();
43 return 0;
47 // GLUT callback functions
50 // redraw the window
51 void display(void)
53 startT = OSG::getSystemTime();
55 mgr->idle();
56 mgr->redraw();
57 OSG::Thread::getCurrentChangeList()->clear();
59 endT = OSG::getSystemTime();
61 avgT -= timeHistory.front();
62 while(timeHistory.size() > 20)
63 timeHistory.pop_front();
65 timeHistory.push_back(endT - startT);
66 avgT += timeHistory.back();
68 if(showFrameTime == true)
70 SLOG << "frame time " << (avgT / timeHistory.size())
71 << std::endl;
75 // react to size changes
76 void reshape(int w, int h)
78 mgr->resize(w, h);
79 glutPostRedisplay();
82 // react to mouse button presses
83 void mouse(int button, int state, int x, int y)
85 if (state)
86 mgr->mouseButtonRelease(button, x, y);
87 else
88 mgr->mouseButtonPress(button, x, y);
90 glutPostRedisplay();
93 // react to mouse motions with pressed buttons
94 void motion(int x, int y)
96 mgr->mouseMove(x, y);
97 glutPostRedisplay();
100 // react to keys
101 void keyboard(unsigned char k, int , int )
103 switch(k)
105 case 27:
106 mgr = NULL;
107 scene = NULL;
109 OSG::osgExit();
110 exit(0);
111 break;
113 case '1':
114 std::cout << "printGeoInfo" << std::endl;
115 doPrintGeoInfo(scene);
116 break;
118 case '2':
119 std::cout << "makeSingleIndexed" << std::endl;
120 doMakeSingleIndexed(scene);
121 //doPrintGeoInfo(scene);
122 break;
124 case '3':
125 std::cout << "makeIndexedTrianles" << std::endl;
126 doMakeIndexedTriangles(scene);
127 //doPrintGeoInfo(scene);
128 break;
130 case '4':
131 std::cout << "makeOptimizedIndex" << std::endl;
132 doMakeOptimizedIndex(scene);
133 //doPrintGeoInfo(scene);
134 break;
136 case '5':
137 std::cout << "makeOptimizedProperties" << std::endl;
138 doMakeOptimizedProperties(scene);
139 //doPrintGeoInfo(scene);
140 break;
142 case 'a':
143 std::cout << "makeSphere(5, 5.f)" << std::endl;
144 scene = OSG::makeSphere(5, 5.f);
145 mgr->setRoot(scene);
146 break;
147 case 'b':
148 std::cout << "makeTorus(2.f, 4.f, 16, 16)" << std::endl;
149 scene = OSG::makeTorus(2.f, 4.f, 16, 16);
150 mgr->setRoot(scene);
151 break;
152 case 'c':
153 std::cout << "makeBox(2.f, 2.f, 2.f, 5, 5, 5)" << std::endl;
154 scene = OSG::makeBox(2.f, 2.f, 2.f, 5, 5, 5);
155 mgr->setRoot(scene);
156 break;
157 case 'd':
158 std::cout << "file '" << fileName << "'" << std::endl;
159 scene = OSG::SceneFileHandler::the()->read(fileName.c_str(), NULL);
160 mgr->setRoot(scene);
161 break;
162 case 'e':
163 std::cout << "file '" << fileName << "' with GraphOps" << std::endl;
164 scene = OSG::SceneFileHandler::the()->read(fileName.c_str());
165 mgr->setRoot(scene);
166 break;
168 case '.':
169 std::cout << "mgr->showAll" << std::endl;
170 mgr->showAll();
171 break;
173 case ',':
174 showFrameTime = !showFrameTime;
175 std::cout << "showFrameTime " << showFrameTime
176 << std::endl;
177 break;
180 glutPostRedisplay();
184 // setup the GLUT library which handles the windows for us
185 int setupGLUT(int *argc, char *argv[])
187 glutInit(argc, argv);
188 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
190 int winid = glutCreateWindow("OpenSG");
192 glutReshapeFunc(reshape);
193 glutDisplayFunc(display);
194 glutIdleFunc(display);
195 glutMouseFunc(mouse);
196 glutMotionFunc(motion);
197 glutKeyboardFunc(keyboard);
199 return winid;
202 void
203 doInit(int argc, char *argv[])
205 OSG::osgInit(argc,argv);
207 int winid = setupGLUT(&argc, argv);
209 OSG::GLUTWindowUnrecPtr gwin = OSG::GLUTWindow::create();
210 gwin->setGlutId(winid);
211 gwin->init();
213 if(argc < 2)
215 FWARNING(("No file given!\n"));
216 FWARNING(("Supported file formats:\n"));
217 OSG::SceneFileHandler::the()->print();
219 std::cout << "makeSphere(5, 5.f)" << std::endl;
220 scene = OSG::makeSphere(5, 5.f);
222 else
224 fileName = argv[1];
226 scene = OSG::SceneFileHandler::the()->read(fileName.c_str(), NULL);
229 OSG::commitChanges();
231 mgr = OSG::SimpleSceneManager::create();
232 mgr->setWindow(gwin );
233 mgr->setRoot (scene);
235 mgr->showAll();
237 timeHistory.push_back(0.0);
240 OSG::Action::ResultE
241 doPrintGeoInfoFunc(OSG::Node *node)
243 OSG::NodeCore *core = node->getCore();
244 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
246 if(geo != NULL)
248 std::cout << "== geo info ==" << std::endl;
249 printGeoInfo(std::cout, geo) << std::endl;
252 return OSG::Action::Continue;
255 void
256 doPrintGeoInfo(OSG::Node *pScene)
258 OSG::traverse(pScene,
259 boost::bind(&doPrintGeoInfoFunc, _1));
262 OSG::Action::ResultE
263 doMakeSingleIndexedFunc(OSG::Node *node)
265 OSG::NodeCore *core = node->getCore();
266 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
268 if(geo != NULL)
270 std::cout << "== geo make single idx ==" << std::endl;
271 OSG::makeSingleIndexed(geo);
274 return OSG::Action::Continue;
277 void
278 doMakeSingleIndexed(OSG::Node *pScene)
280 OSG::Time start = OSG::getSystemTime();
282 OSG::traverse(pScene,
283 boost::bind(&doMakeSingleIndexedFunc, _1));
285 std::cout << "time for doMakeSingleIndexed on scene: " << (OSG::getSystemTime() - start)
286 << std::endl;
289 OSG::Action::ResultE
290 doMakeIndexedTrianglesFunc(OSG::Node *node)
292 OSG::NodeCore *core = node->getCore();
293 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
295 if(geo != NULL)
297 std::cout << "== geo make indexed tris ==" << std::endl;
298 OSG::makeIndexedTriangles(geo);
301 return OSG::Action::Continue;
304 void
305 doMakeIndexedTriangles(OSG::Node *pScene)
307 OSG::Time start = OSG::getSystemTime();
309 OSG::traverse(pScene,
310 boost::bind(&doMakeIndexedTrianglesFunc, _1));
312 std::cout << "time for doMakeIndexedTriangles on scene: " << (OSG::getSystemTime() - start)
313 << std::endl;
316 OSG::Action::ResultE
317 doMakeOptimizedIndexFunc(OSG::Node *node)
319 OSG::NodeCore *core = node->getCore();
320 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
322 if(geo != NULL)
324 std::cout << "== geo make optimized index ==" << std::endl;
325 OSG::makeOptimizedIndex(geo);
328 return OSG::Action::Continue;
331 void
332 doMakeOptimizedIndex(OSG::Node *pScene)
334 OSG::Time start = OSG::getSystemTime();
336 OSG::traverse(pScene,
337 boost::bind(&doMakeOptimizedIndexFunc, _1));
339 std::cout << "time for doMakeOptimizedIndex on scene: " << (OSG::getSystemTime() - start)
340 << std::endl;
343 OSG::Action::ResultE
344 doMakeOptimizedPropertiesFunc(OSG::Node *node)
346 OSG::NodeCore *core = node->getCore();
347 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
349 if(geo != NULL)
351 std::cout << "== geo make optimized properties ==" << std::endl;
352 OSG::makeOptimizedProperties(geo);
355 return OSG::Action::Continue;
358 void
359 doMakeOptimizedProperties(OSG::Node *pScene)
361 OSG::Time start = OSG::getSystemTime();
363 OSG::traverse(pScene,
364 boost::bind(&doMakeOptimizedPropertiesFunc, _1));
366 std::cout << "time for doMakeOptimizedProperties on scene: " << (OSG::getSystemTime() - start)
367 << std::endl;
370 std::ostream &
371 printTypes(std::ostream &os, OSG::GeoIntegralProperty *types)
373 os << "#types " << types->size();
375 for(OSG::UInt32 i = 0; i < types->size(); ++i)
377 OSG::UInt32 val;
378 types->getValue(val, i);
380 os << " (" << i << ": ";
381 switch(val)
383 case GL_TRIANGLES: os << "GL_TRIANGLES"; break;
384 case GL_TRIANGLE_STRIP: os << "GL_TRIANGLE_STRIP"; break;
385 case GL_TRIANGLE_FAN: os << "GL_TRIANGLE_FAN"; break;
386 case GL_QUADS: os << "GL_QUADS"; break;
387 case GL_QUAD_STRIP: os << "GL_QUAD_STRIP"; break;
388 case GL_LINES: os << "GL_LINES"; break;
389 case GL_LINE_STRIP: os << "GL_LINE_STRIP"; break;
390 case GL_POINTS: os << "GL_POINTS"; break;
391 case GL_POLYGON: os << "GL_POLYGON"; break;
392 default: os << "?? " << val; break;
394 os << ")";
397 return os;
400 std::ostream &
401 printGeoInfo(std::ostream &os, OSG::Geometry *geo)
403 if(geo->getTypes() != NULL)
404 printTypes(os, geo->getTypes()) << std::endl;
406 if(geo->getLengths() != NULL)
408 os << "#lengths " << geo->getLengths()->size()
409 << std::endl;
412 OSG::Geometry::IndexBag ib = geo->getUniqueIndexBag();
414 for(OSG::UInt32 i = 0; i < ib.size(); ++i)
416 os << "ib " << i << ": ";
418 for(OSG::UInt32 j = 0; j < ib[i].second.size(); ++j)
420 os << ib[i].second[j] << ", ";
423 os << std::endl;
426 for(OSG::UInt16 i = 0; i <= OSG::Geometry::LastIndex; ++i)
428 OSG::GeoVectorProperty *prop = geo->getProperty(i);
429 OSG::GeoIntegralProperty *idx = geo->getIndex (i);
431 if(prop != NULL)
433 os << "prop " << i
434 << " @" << prop
435 << " size " << prop->size()
436 << " type " << prop->getType().getName();
438 if(idx != NULL)
440 os << " idx @" << idx
441 << " size " << idx->size()
442 << " type " << idx->getType().getName();
445 os << std::endl;
447 else if(idx != NULL)
449 os << "prop " << i << " NULL idx @" << idx
450 << " size " << idx->size()
451 << " type " << idx->getType().getName()
452 << std::endl;
456 os << "single indexed? " << (geo->isSingleIndex() ? "yes" : "no")
457 << std::endl;
459 return os;