1 | #include "SceneGraph.h"
|
---|
2 | #include "Exporter.h"
|
---|
3 | #include "UnigraphicsParser.h"
|
---|
4 | #include "X3dParser.h"
|
---|
5 | #include "Preprocessor.h"
|
---|
6 | #include "ViewCell.h"
|
---|
7 | #include "Environment.h"
|
---|
8 | #include "RenderSimulator.h"
|
---|
9 |
|
---|
10 | Preprocessor::Preprocessor():
|
---|
11 | mKdTree(NULL),
|
---|
12 | mBspTree(NULL),
|
---|
13 | mVspKdTree(NULL),
|
---|
14 | mRenderSimulator(NULL)
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | Preprocessor::~Preprocessor()
|
---|
20 | {
|
---|
21 | DEL_PTR(mBspTree);
|
---|
22 | DEL_PTR(mKdTree);
|
---|
23 |
|
---|
24 | DEL_PTR(mRenderSimulator);
|
---|
25 |
|
---|
26 | DeleteViewCells();
|
---|
27 | }
|
---|
28 |
|
---|
29 | bool
|
---|
30 | Preprocessor::LoadViewCells(const string filename)
|
---|
31 | {
|
---|
32 | X3dParser parser;
|
---|
33 |
|
---|
34 | environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
35 |
|
---|
36 | return parser.ParseFile(filename, mViewCells);
|
---|
37 | }
|
---|
38 |
|
---|
39 | bool
|
---|
40 | Preprocessor::ParseViewCellsOptions()
|
---|
41 | {
|
---|
42 | // parse type of view cells
|
---|
43 | char viewCellsStr[64];
|
---|
44 | environment->GetStringValue("ViewCells.hierarchy", viewCellsStr);
|
---|
45 |
|
---|
46 | if (strcmp(viewCellsStr, "bspTree") == 0)
|
---|
47 | {
|
---|
48 | ViewCell::sHierarchy = ViewCell::BSP;
|
---|
49 | }
|
---|
50 | else if (strcmp(viewCellsStr, "kdTree") == 0)
|
---|
51 | {
|
---|
52 | ViewCell::sHierarchy = ViewCell::KD;
|
---|
53 | }
|
---|
54 | else if (strcmp(viewCellsStr, "vspTree") == 0)
|
---|
55 | {
|
---|
56 | ViewCell::sHierarchy = ViewCell::VSP;
|
---|
57 | }
|
---|
58 | else if (strcmp(viewCellsStr, "sceneDependent") == 0)
|
---|
59 | {
|
---|
60 | //TODO
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | cerr<<"Wrong view cells type" << viewCellsStr << endl;
|
---|
65 | exit(1);
|
---|
66 | }
|
---|
67 |
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | RenderSimulator *Preprocessor::GetRenderSimulator()
|
---|
72 | {
|
---|
73 | if (mRenderSimulator)
|
---|
74 | return mRenderSimulator;
|
---|
75 |
|
---|
76 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
77 |
|
---|
78 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
79 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
80 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
81 |
|
---|
82 | Debug << "render simulator using render cost=" << objRenderCost << ", vc overhead=" << vcOverhead << ", move speed=" << moveSpeed << endl;
|
---|
83 | if (ViewCell::sHierarchy == ViewCell::BSP)
|
---|
84 | {
|
---|
85 | mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
|
---|
86 | Debug << "creating bsp render simulator" << endl;
|
---|
87 | }
|
---|
88 | else if (ViewCell::sHierarchy == ViewCell::KD)// KD view cells
|
---|
89 | {
|
---|
90 | mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
|
---|
91 | Debug << "creating kd render simulator" << endl;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | Debug << "not implemented yet" << endl;
|
---|
96 | return NULL;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return mRenderSimulator;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void Preprocessor::DeleteViewCells()
|
---|
103 | {
|
---|
104 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
105 | {
|
---|
106 | Mesh *mesh = mViewCells[i]->GetMesh();
|
---|
107 | DEL_PTR(mesh);
|
---|
108 | }
|
---|
109 | CLEAR_CONTAINER(mViewCells);
|
---|
110 | }
|
---|
111 |
|
---|
112 | int
|
---|
113 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
114 | {
|
---|
115 | int pos = 0;
|
---|
116 |
|
---|
117 | while(1) {
|
---|
118 | int npos = str.find(';', pos);
|
---|
119 |
|
---|
120 | if (npos < 0 || npos - pos < 1)
|
---|
121 | break;
|
---|
122 | filenames.push_back(string(str, pos, npos - pos));
|
---|
123 | pos = npos + 1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
127 | return filenames.size();
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool
|
---|
131 | Preprocessor::LoadScene(const string filename)
|
---|
132 | {
|
---|
133 | // use leaf nodes of the original spatial hiearrchy as occludees
|
---|
134 |
|
---|
135 | mSceneGraph = new SceneGraph;
|
---|
136 |
|
---|
137 |
|
---|
138 | Parser *parser;
|
---|
139 | vector<string> filenames;
|
---|
140 | int files = SplitFilenames(filename, filenames);
|
---|
141 | cout<<files<<endl;
|
---|
142 | bool result = false;
|
---|
143 | if (files == 1) {
|
---|
144 |
|
---|
145 | if (strstr(filename.c_str(), ".x3d"))
|
---|
146 | parser = new X3dParser;
|
---|
147 | else
|
---|
148 | parser = new UnigraphicsParser;
|
---|
149 |
|
---|
150 | cout<<filename<<endl;
|
---|
151 | result = parser->ParseFile(filename, &mSceneGraph->mRoot);
|
---|
152 |
|
---|
153 | delete parser;
|
---|
154 |
|
---|
155 | } else {
|
---|
156 | // root for different files
|
---|
157 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
158 | for (int i= 0; i < filenames.size(); i++) {
|
---|
159 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
160 | parser = new X3dParser;
|
---|
161 | else
|
---|
162 | parser = new UnigraphicsParser;
|
---|
163 |
|
---|
164 | SceneGraphNode *node;
|
---|
165 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
166 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
167 | // at least one file parsed
|
---|
168 | result = true;
|
---|
169 | }
|
---|
170 | delete parser;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | if (result) {
|
---|
176 | mSceneGraph->AssignObjectIds();
|
---|
177 | int intersectables, faces;
|
---|
178 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
179 | cout<<filename<<" parsed successfully."<<endl;
|
---|
180 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
181 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool
|
---|
189 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
190 | {
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 |
|
---|
194 | bool
|
---|
195 | Preprocessor::BuildKdTree()
|
---|
196 | {
|
---|
197 | mKdTree = new KdTree;
|
---|
198 | // add mesh instances of the scene graph to the root of the tree
|
---|
199 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
200 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
201 |
|
---|
202 | mKdTree->Construct();
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 |
|
---|
206 | void
|
---|
207 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
208 | {
|
---|
209 | s<<mKdTree->GetStatistics();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void
|
---|
213 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
214 | {
|
---|
215 | s << mBspTree->GetStatistics();
|
---|
216 | }
|
---|
217 |
|
---|
218 | bool
|
---|
219 | Preprocessor::Export( const string filename,
|
---|
220 | const bool scene,
|
---|
221 | const bool kdtree,
|
---|
222 | const bool bsptree
|
---|
223 | )
|
---|
224 | {
|
---|
225 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
226 |
|
---|
227 | if (exporter) {
|
---|
228 | if (scene)
|
---|
229 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
230 |
|
---|
231 | if (kdtree) {
|
---|
232 | exporter->SetWireframe();
|
---|
233 | exporter->ExportKdTree(*mKdTree);
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (bsptree) {
|
---|
237 | //exporter->SetWireframe();
|
---|
238 | exporter->ExportBspTree(*mBspTree);
|
---|
239 | }
|
---|
240 |
|
---|
241 | delete exporter;
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 |
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | void Preprocessor::ExportSplits(const ObjectContainer &objects,
|
---|
250 | const RayContainer &sampleRays,
|
---|
251 | const int visSamples)
|
---|
252 | {
|
---|
253 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
254 |
|
---|
255 | if (exporter)
|
---|
256 | {
|
---|
257 | Material m;
|
---|
258 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
259 | exporter->SetForcedMaterial(m);
|
---|
260 | exporter->SetWireframe();
|
---|
261 | exporter->ExportBspSplits(*mBspTree, true);
|
---|
262 |
|
---|
263 | // take forced material, else big scenes cannot be viewed
|
---|
264 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
265 | exporter->SetForcedMaterial(m);
|
---|
266 | exporter->SetFilled();
|
---|
267 |
|
---|
268 | exporter->ResetForcedMaterial();
|
---|
269 |
|
---|
270 | // export rays
|
---|
271 | if (0)
|
---|
272 | {
|
---|
273 | RayContainer outRays;
|
---|
274 |
|
---|
275 | for (int i = 0; i < sampleRays.size(); ++ i)
|
---|
276 | {
|
---|
277 | // only rays piercing geometry
|
---|
278 | if (!sampleRays[i]->intersections.empty())
|
---|
279 | outRays.push_back(sampleRays[i]);
|
---|
280 | }
|
---|
281 | if (BspTree::sConstructionMethod == BspTree::FROM_SAMPLES)
|
---|
282 | {
|
---|
283 | // export rays
|
---|
284 | exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (1)
|
---|
289 | ExportSceneGeometry(exporter, objects);
|
---|
290 |
|
---|
291 | delete exporter;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | inline bool vc_gt(ViewCell *a, ViewCell *b)
|
---|
296 | {
|
---|
297 | return a->GetPvs().GetSize() > b->GetPvs().GetSize();
|
---|
298 | }
|
---|
299 |
|
---|
300 | void Preprocessor::ExportBspPvs(const ObjectContainer &objects,
|
---|
301 | const RayContainer &sampleRays,
|
---|
302 | const int visSamples)
|
---|
303 | {
|
---|
304 | const int leafOut = 10;
|
---|
305 |
|
---|
306 | ViewCell::NewMail();
|
---|
307 |
|
---|
308 | //-- some rays for output
|
---|
309 | const int raysOut = min((int)sampleRays.size(), visSamples);
|
---|
310 | cout << "visualization using " << visSamples << " samples" << endl;
|
---|
311 | vector<Ray *> vcRays[leafOut];
|
---|
312 |
|
---|
313 | if (0)
|
---|
314 | {
|
---|
315 | //-- some random view cells and rays for output
|
---|
316 | vector<BspLeaf *> bspLeaves;
|
---|
317 |
|
---|
318 | for (int i = 0; i < leafOut; ++ i)
|
---|
319 | bspLeaves.push_back(mBspTree->GetRandomLeaf());
|
---|
320 |
|
---|
321 | for (int i = 0; i < bspLeaves.size(); ++ i)
|
---|
322 | {
|
---|
323 | cout << "creating output for view cell " << i << " ... ";
|
---|
324 | // check whether we can add the current ray to the output rays
|
---|
325 | for (int k = 0; k < raysOut; ++ k)
|
---|
326 | {
|
---|
327 | Ray *ray = sampleRays[k];
|
---|
328 |
|
---|
329 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
330 | {
|
---|
331 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
|
---|
332 |
|
---|
333 | if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell())
|
---|
334 | {
|
---|
335 | vcRays[i].push_back(ray);
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | Intersectable::NewMail();
|
---|
341 |
|
---|
342 | BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
|
---|
343 |
|
---|
344 | //bspLeaves[j]->Mail();
|
---|
345 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
346 |
|
---|
347 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
348 | exporter->SetFilled();
|
---|
349 |
|
---|
350 | ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
|
---|
351 |
|
---|
352 | exporter->SetWireframe();
|
---|
353 | //exporter->SetFilled();
|
---|
354 |
|
---|
355 | Material m;//= RandomMaterial();
|
---|
356 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
357 | exporter->SetForcedMaterial(m);
|
---|
358 |
|
---|
359 | if (vc->GetMesh())
|
---|
360 | exporter->ExportViewCell(vc);
|
---|
361 | else
|
---|
362 | {
|
---|
363 | PolygonContainer cell;
|
---|
364 | // export view cell geometry
|
---|
365 | mBspTree->ConstructGeometry(vc, cell);
|
---|
366 | exporter->ExportPolygons(cell);
|
---|
367 | CLEAR_CONTAINER(cell);
|
---|
368 | }
|
---|
369 |
|
---|
370 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
371 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
372 |
|
---|
373 | // export rays piercing this view cell
|
---|
374 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
375 |
|
---|
376 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
377 | exporter->SetForcedMaterial(m);
|
---|
378 |
|
---|
379 | // exporter->SetWireframe();
|
---|
380 | exporter->SetFilled();
|
---|
381 |
|
---|
382 | // output PVS of view cell
|
---|
383 | for (; it != vc->GetPvs().mEntries.end(); ++ it)
|
---|
384 | {
|
---|
385 | Intersectable *intersect = (*it).first;
|
---|
386 | if (!intersect->Mailed())
|
---|
387 | {
|
---|
388 | exporter->ExportIntersectable(intersect);
|
---|
389 | intersect->Mail();
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | // output rest of the objects
|
---|
394 | if (0)
|
---|
395 | {
|
---|
396 | Material m;//= RandomMaterial();
|
---|
397 | m.mDiffuseColor = RgbColor(0, 0, 1);
|
---|
398 | exporter->SetForcedMaterial(m);
|
---|
399 |
|
---|
400 | for (int j = 0; j < objects.size(); ++ j)
|
---|
401 | if (!objects[j]->Mailed())
|
---|
402 | {
|
---|
403 | exporter->SetForcedMaterial(m);
|
---|
404 | exporter->ExportIntersectable(objects[j]);
|
---|
405 | objects[j]->Mail();
|
---|
406 | }
|
---|
407 | }
|
---|
408 | DEL_PTR(exporter);
|
---|
409 | cout << "finished" << endl;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | ViewCellContainer viewCells;
|
---|
415 |
|
---|
416 | mBspTree->CollectViewCells(viewCells);
|
---|
417 | stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
|
---|
418 |
|
---|
419 | int limit = min(leafOut, (int)viewCells.size());
|
---|
420 |
|
---|
421 | for (int i = 0; i < limit; ++ i)
|
---|
422 | {
|
---|
423 | cout << "creating output for view cell " << i << " ... ";
|
---|
424 |
|
---|
425 | Intersectable::NewMail();
|
---|
426 | BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
|
---|
427 |
|
---|
428 | cout << "creating output for view cell " << i << " ... ";
|
---|
429 | // check whether we can add the current ray to the output rays
|
---|
430 | for (int k = 0; k < raysOut; ++ k)
|
---|
431 | {
|
---|
432 | Ray *ray = sampleRays[k];
|
---|
433 |
|
---|
434 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
435 | {
|
---|
436 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
|
---|
437 |
|
---|
438 | if (vc == leaf->GetViewCell())
|
---|
439 | {
|
---|
440 | vcRays[i].push_back(ray);
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | //bspLeaves[j]->Mail();
|
---|
446 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
447 |
|
---|
448 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
449 |
|
---|
450 | exporter->SetWireframe();
|
---|
451 |
|
---|
452 | Material m;//= RandomMaterial();
|
---|
453 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
454 | exporter->SetForcedMaterial(m);
|
---|
455 |
|
---|
456 | if (vc->GetMesh())
|
---|
457 | exporter->ExportViewCell(vc);
|
---|
458 | else
|
---|
459 | {
|
---|
460 | PolygonContainer cell;
|
---|
461 | // export view cell
|
---|
462 | mBspTree->ConstructGeometry(vc, cell);
|
---|
463 | exporter->ExportPolygons(cell);
|
---|
464 | CLEAR_CONTAINER(cell);
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
469 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
470 |
|
---|
471 |
|
---|
472 | // export rays piercing this view cell
|
---|
473 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
474 |
|
---|
475 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
476 | exporter->SetForcedMaterial(m);
|
---|
477 |
|
---|
478 | ViewCellPvsMap::const_iterator it,
|
---|
479 | it_end = vc->GetPvs().mEntries.end();
|
---|
480 |
|
---|
481 | // output PVS of view cell
|
---|
482 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
483 | {
|
---|
484 | Intersectable *intersect = (*it).first;
|
---|
485 | if (!intersect->Mailed())
|
---|
486 | {
|
---|
487 | Material m = RandomMaterial();
|
---|
488 |
|
---|
489 | exporter->SetForcedMaterial(m);
|
---|
490 |
|
---|
491 | exporter->ExportIntersectable(intersect);
|
---|
492 | intersect->Mail();
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | DEL_PTR(exporter);
|
---|
497 | cout << "finished" << endl;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | void Preprocessor::ExportSceneGeometry(Exporter *exporter,
|
---|
504 | const ObjectContainer &objects)
|
---|
505 | {
|
---|
506 | Material m;//= RandomMaterial();
|
---|
507 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
508 | exporter->SetForcedMaterial(m);
|
---|
509 | exporter->SetWireframe();
|
---|
510 |
|
---|
511 | for (int j = 0; j < objects.size(); ++ j)
|
---|
512 | exporter->ExportIntersectable(objects[j]);
|
---|
513 | } |
---|