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 "ViewCellsManager.h"
|
---|
9 | #include "ViewCellBsp.h"
|
---|
10 | #include "VspBspTree.h"
|
---|
11 | #include "RenderSimulator.h"
|
---|
12 | #include "GlRenderer.h"
|
---|
13 | #include "PlyParser.h"
|
---|
14 | #include "SamplingStrategy.h"
|
---|
15 | #include "VspTree.h"
|
---|
16 | #include "OspTree.h"
|
---|
17 | #include "ObjParser.h"
|
---|
18 | #include "BvHierarchy.h"
|
---|
19 | #include "HierarchyManager.h"
|
---|
20 | #include "VssRay.h"
|
---|
21 | #include "IntelRayCaster.h"
|
---|
22 | #include "InternalRayCaster.h"
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | #define DEBUG_RAYCAST 0
|
---|
27 |
|
---|
28 |
|
---|
29 | namespace GtpVisibilityPreprocessor {
|
---|
30 |
|
---|
31 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
32 |
|
---|
33 |
|
---|
34 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
35 | static void AddGeometry(SceneGraph *scene)
|
---|
36 | {
|
---|
37 | scene->GetRoot()->UpdateBox();
|
---|
38 |
|
---|
39 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
40 |
|
---|
41 | int n = 200;
|
---|
42 |
|
---|
43 | if (0)
|
---|
44 | {
|
---|
45 | // form grid of boxes
|
---|
46 | for (int i = 0; i < n; ++ i)
|
---|
47 | {
|
---|
48 | for (int j = 0; j < n; ++ j)
|
---|
49 | {
|
---|
50 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
51 |
|
---|
52 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
53 |
|
---|
54 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
55 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
56 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
57 |
|
---|
58 | mesh->Preprocess();
|
---|
59 |
|
---|
60 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
61 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | for (int i = 0; i < n; ++ i)
|
---|
66 | {
|
---|
67 | for (int j = 0; j < n; ++ j)
|
---|
68 | {
|
---|
69 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
70 |
|
---|
71 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
72 |
|
---|
73 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
74 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
75 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
76 |
|
---|
77 | mesh->Preprocess();
|
---|
78 |
|
---|
79 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
80 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | for (int i = 0; i < n; ++ i)
|
---|
85 | {
|
---|
86 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
87 |
|
---|
88 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
89 |
|
---|
90 | //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
|
---|
91 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
92 |
|
---|
93 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
94 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
95 |
|
---|
96 | mesh->Preprocess();
|
---|
97 |
|
---|
98 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
99 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
100 | }
|
---|
101 |
|
---|
102 | scene->GetRoot()->UpdateBox();
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (1)
|
---|
106 | {
|
---|
107 | // plane separating view space regions
|
---|
108 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
109 |
|
---|
110 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
111 |
|
---|
112 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
113 | Mesh *planeMesh = new Mesh();
|
---|
114 |
|
---|
115 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
116 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
117 |
|
---|
118 | planeMesh->Preprocess();
|
---|
119 |
|
---|
120 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
121 | scene->GetRoot()->mGeometry.push_back(planeMi);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | Preprocessor::Preprocessor():
|
---|
127 | mKdTree(NULL),
|
---|
128 | mBspTree(NULL),
|
---|
129 | mVspBspTree(NULL),
|
---|
130 | mHierarchyManager(NULL),
|
---|
131 | mViewCellsManager(NULL),
|
---|
132 | mRenderSimulator(NULL),
|
---|
133 | mPass(0),
|
---|
134 | mSceneGraph(NULL),
|
---|
135 | mRayCaster(NULL)
|
---|
136 | {
|
---|
137 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
138 |
|
---|
139 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
140 | renderer = NULL;
|
---|
141 |
|
---|
142 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
|
---|
144 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
145 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
146 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
147 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
148 |
|
---|
149 | char buffer[256];
|
---|
150 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
151 |
|
---|
152 | mVisibilityFileName = buffer;
|
---|
153 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
|
---|
154 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
155 | mApplyVisibilitySpatialFilter );
|
---|
156 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
157 |
|
---|
158 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
159 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | Preprocessor::~Preprocessor()
|
---|
164 | {
|
---|
165 | cout << "cleaning up" << endl;
|
---|
166 |
|
---|
167 | cout << "Deleting view cells manager ... \n";
|
---|
168 | DEL_PTR(mViewCellsManager);
|
---|
169 | cout << "done.\n";
|
---|
170 |
|
---|
171 | cout << "Deleting bsp tree ... \n";
|
---|
172 | DEL_PTR(mBspTree);
|
---|
173 | cout << "done.\n";
|
---|
174 |
|
---|
175 | cout << "Deleting kd tree...\n";
|
---|
176 | DEL_PTR(mKdTree);
|
---|
177 | cout << "done.\n";
|
---|
178 |
|
---|
179 | cout << "Deleting hierarchy manager...\n";
|
---|
180 | DEL_PTR(mHierarchyManager);
|
---|
181 | cout << "done.\n";
|
---|
182 |
|
---|
183 | cout << "Deleting vspbsp tree...\n";
|
---|
184 | DEL_PTR(mVspBspTree);
|
---|
185 | cout << "done.\n";
|
---|
186 |
|
---|
187 | cout << "Deleting scene graph...\n";
|
---|
188 | DEL_PTR(mSceneGraph);
|
---|
189 | cout << "done.\n";
|
---|
190 |
|
---|
191 | DEL_PTR(mRenderSimulator);
|
---|
192 | DEL_PTR(renderer);
|
---|
193 | DEL_PTR(mRayCaster);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | GlRendererBuffer *Preprocessor::GetRenderer()
|
---|
198 | {
|
---|
199 | return renderer;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
204 | {
|
---|
205 | int pos = 0;
|
---|
206 |
|
---|
207 | while(1) {
|
---|
208 | int npos = (int)str.find(';', pos);
|
---|
209 |
|
---|
210 | if (npos < 0 || npos - pos < 1)
|
---|
211 | break;
|
---|
212 | filenames.push_back(string(str, pos, npos - pos));
|
---|
213 | pos = npos + 1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
217 | return (int)filenames.size();
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | bool
|
---|
222 | Preprocessor::LoadScene(const string filename)
|
---|
223 | {
|
---|
224 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
225 | mSceneGraph = new SceneGraph;
|
---|
226 |
|
---|
227 | Parser *parser;
|
---|
228 | vector<string> filenames;
|
---|
229 | const int files = SplitFilenames(filename, filenames);
|
---|
230 | cout << "number of input files: " << files << endl;
|
---|
231 | bool result = false;
|
---|
232 |
|
---|
233 | // root for different files
|
---|
234 | mSceneGraph->SetRoot(new SceneGraphNode());
|
---|
235 |
|
---|
236 | // intel ray caster can only trace triangles
|
---|
237 | int rayCastMethod;
|
---|
238 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
239 | vector<FaceParentInfo> *fi = (rayCastMethod == RayCaster::INTEL_RAYCASTER) ?
|
---|
240 | &mFaceParents : NULL;
|
---|
241 |
|
---|
242 | if (files == 1) {
|
---|
243 |
|
---|
244 | if (strstr(filename.c_str(), ".x3d"))
|
---|
245 | parser = new X3dParser;
|
---|
246 | else
|
---|
247 | if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
248 | parser = new PlyParser;
|
---|
249 | else if (strstr(filename.c_str(), ".obj"))
|
---|
250 | parser = new ObjParser;
|
---|
251 | else
|
---|
252 | parser = new UnigraphicsParser;
|
---|
253 |
|
---|
254 | cout << filename << endl;
|
---|
255 |
|
---|
256 | result = parser->ParseFile(
|
---|
257 | filename,
|
---|
258 | mSceneGraph->GetRoot(),
|
---|
259 | mLoadMeshes,
|
---|
260 | fi);
|
---|
261 |
|
---|
262 | delete parser;
|
---|
263 |
|
---|
264 | }
|
---|
265 | else {
|
---|
266 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
267 |
|
---|
268 | for (fit = filenames.begin(); fit != fit_end; ++ fit)
|
---|
269 | {
|
---|
270 | const string filename = *fit;
|
---|
271 |
|
---|
272 | cout << "parsing file " << filename.c_str() << endl;
|
---|
273 | if (strstr(filename.c_str(), ".x3d"))
|
---|
274 | parser = new X3dParser;
|
---|
275 | else
|
---|
276 | parser = new UnigraphicsParser;
|
---|
277 |
|
---|
278 | SceneGraphNode *node = new SceneGraphNode();
|
---|
279 | const bool success = parser->ParseFile(
|
---|
280 | filename,
|
---|
281 | node,
|
---|
282 | mLoadMeshes,
|
---|
283 | fi);
|
---|
284 |
|
---|
285 | if (success)
|
---|
286 | {
|
---|
287 | mSceneGraph->GetRoot()->mChildren.push_back(node);
|
---|
288 | result = true; // at least one file parsed
|
---|
289 | }
|
---|
290 |
|
---|
291 | delete parser;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | if (result)
|
---|
296 | {
|
---|
297 | // HACK
|
---|
298 | if (ADDITIONAL_GEOMETRY_HACK)
|
---|
299 | AddGeometry(mSceneGraph);
|
---|
300 |
|
---|
301 | mSceneGraph->AssignObjectIds();
|
---|
302 |
|
---|
303 | int intersectables, faces;
|
---|
304 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
305 |
|
---|
306 | cout<<filename<<" parsed successfully."<<endl;
|
---|
307 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
308 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
309 | mSceneGraph->CollectObjects(&mObjects);
|
---|
310 | mSceneGraph->GetRoot()->UpdateBox();
|
---|
311 |
|
---|
312 | if (0)
|
---|
313 | {
|
---|
314 | Exporter *exporter = Exporter::GetExporter("testload.x3d");
|
---|
315 | if (exporter)
|
---|
316 | {
|
---|
317 | exporter->ExportGeometry(mObjects);
|
---|
318 | delete exporter;
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | return result;
|
---|
324 | }
|
---|
325 |
|
---|
326 | bool
|
---|
327 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
328 | {
|
---|
329 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
330 | return true;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | bool
|
---|
335 | Preprocessor::PostProcessVisibility()
|
---|
336 | {
|
---|
337 |
|
---|
338 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
339 | cout<<"Applying visibility filter ...";
|
---|
340 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
341 |
|
---|
342 | if (!mViewCellsManager)
|
---|
343 | return false;
|
---|
344 |
|
---|
345 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
346 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
347 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
348 | cout << "done." << endl;
|
---|
349 | }
|
---|
350 |
|
---|
351 | // export the preprocessed information to a file
|
---|
352 | if (mExportVisibility)
|
---|
353 | {
|
---|
354 | ExportPreprocessedData(mVisibilityFileName);
|
---|
355 | }
|
---|
356 |
|
---|
357 | return true;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | bool
|
---|
362 | Preprocessor::BuildKdTree()
|
---|
363 | {
|
---|
364 | mKdTree = new KdTree;
|
---|
365 |
|
---|
366 | // add mesh instances of the scene graph to the root of the tree
|
---|
367 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
368 |
|
---|
369 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
370 |
|
---|
371 | const long startTime = GetTime();
|
---|
372 | cout << "building kd tree ... " << endl;
|
---|
373 |
|
---|
374 | mKdTree->Construct();
|
---|
375 |
|
---|
376 | cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
377 | << " secs " << endl;
|
---|
378 |
|
---|
379 | return true;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | void
|
---|
384 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
385 | {
|
---|
386 | s<<mKdTree->GetStatistics();
|
---|
387 | }
|
---|
388 |
|
---|
389 | void
|
---|
390 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
391 | {
|
---|
392 | s << mBspTree->GetStatistics();
|
---|
393 | }
|
---|
394 |
|
---|
395 | bool
|
---|
396 | Preprocessor::Export( const string filename,
|
---|
397 | const bool scene,
|
---|
398 | const bool kdtree
|
---|
399 | )
|
---|
400 | {
|
---|
401 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
402 |
|
---|
403 | if (exporter) {
|
---|
404 | if (2 && scene)
|
---|
405 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
406 |
|
---|
407 | if (1 && kdtree) {
|
---|
408 | exporter->SetWireframe();
|
---|
409 | exporter->ExportKdTree(*mKdTree);
|
---|
410 | }
|
---|
411 |
|
---|
412 | delete exporter;
|
---|
413 | return true;
|
---|
414 | }
|
---|
415 |
|
---|
416 | return false;
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | bool Preprocessor::PrepareViewCells()
|
---|
421 | {
|
---|
422 | ///////
|
---|
423 | //-- parse view cells construction method
|
---|
424 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
425 | char buf[100];
|
---|
426 |
|
---|
427 | if (mLoadViewCells)
|
---|
428 | {
|
---|
429 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
430 | cout << "loading view cells from " << buf << endl;
|
---|
431 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
|
---|
432 | if (!mViewCellsManager)
|
---|
433 | return false;
|
---|
434 | }
|
---|
435 | else
|
---|
436 | {
|
---|
437 | // parse type of view cell container
|
---|
438 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
439 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
440 |
|
---|
441 | // default view space is the extent of the scene
|
---|
442 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
443 |
|
---|
444 | bool loadVcGeometry;
|
---|
445 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry);
|
---|
446 |
|
---|
447 | bool extrudeBaseTriangles;
|
---|
448 | Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles);
|
---|
449 |
|
---|
450 | char vcGeomFilename[100];
|
---|
451 | Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename);
|
---|
452 |
|
---|
453 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
454 | {
|
---|
455 | if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles))
|
---|
456 | {
|
---|
457 | cerr << "loading view cells geometry failed" << endl;
|
---|
458 | }
|
---|
459 | }
|
---|
460 | else
|
---|
461 | {
|
---|
462 | cerr << "loading view cells geometry is not implemented for this manager" << endl;
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | ////////
|
---|
467 | //-- evaluation of render cost heuristics
|
---|
468 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
469 |
|
---|
470 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
471 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
472 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
473 |
|
---|
474 | mRenderSimulator =
|
---|
475 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
476 |
|
---|
477 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
478 |
|
---|
479 |
|
---|
480 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
481 | {
|
---|
482 | // NOTE: render texture should be power of 2 and square
|
---|
483 | // renderer must be initialised
|
---|
484 | // $$matt
|
---|
485 | // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
486 | // renderer->makeCurrent();
|
---|
487 |
|
---|
488 | }
|
---|
489 |
|
---|
490 | return true;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | bool Preprocessor::ConstructViewCells(const AxisAlignedBox3 &viewSpaceBox)
|
---|
495 | {
|
---|
496 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
497 |
|
---|
498 | // construct view cells using it's own set of samples
|
---|
499 | mViewCellsManager->Construct(this);
|
---|
500 |
|
---|
501 | // visualizations and statistics
|
---|
502 | Debug << "finished view cells:" << endl;
|
---|
503 | mViewCellsManager->PrintStatistics(Debug);
|
---|
504 |
|
---|
505 | return true;
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | HierarchyManager *Preprocessor::CreateHierarchyManager(const char *name)
|
---|
510 | {
|
---|
511 | HierarchyManager *hierarchyManager;
|
---|
512 |
|
---|
513 | if (strcmp(name, "osp") == 0)
|
---|
514 | {
|
---|
515 | Debug << "hierarchy manager: osp" << endl;
|
---|
516 | // HACK for testing if per kd evaluation works!!
|
---|
517 | const bool ishack = false;
|
---|
518 | if (ishack)
|
---|
519 | hierarchyManager = new HierarchyManager(mKdTree);
|
---|
520 | else
|
---|
521 | hierarchyManager = new HierarchyManager(HierarchyManager::KD_BASED_OBJ_SUBDIV);
|
---|
522 | }
|
---|
523 | else if (strcmp(name, "bvh") == 0)
|
---|
524 | {
|
---|
525 | Debug << "hierarchy manager: bvh" << endl;
|
---|
526 | hierarchyManager = new HierarchyManager(HierarchyManager::BV_BASED_OBJ_SUBDIV);
|
---|
527 | }
|
---|
528 | else // only view space partition
|
---|
529 | {
|
---|
530 | Debug << "hierarchy manager: obj" << endl;
|
---|
531 | hierarchyManager = new HierarchyManager(HierarchyManager::NO_OBJ_SUBDIV);
|
---|
532 | }
|
---|
533 |
|
---|
534 | return hierarchyManager;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
539 | {
|
---|
540 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
541 |
|
---|
542 | if (strcmp(name, "kdTree") == 0)
|
---|
543 | {
|
---|
544 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
545 | }
|
---|
546 | else if (strcmp(name, "bspTree") == 0)
|
---|
547 | {
|
---|
548 | Debug << "view cell type: Bsp" << endl;
|
---|
549 |
|
---|
550 | mBspTree = new BspTree();
|
---|
551 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
552 | }
|
---|
553 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
554 | {
|
---|
555 | Debug << "view cell type: VspBsp" << endl;
|
---|
556 |
|
---|
557 | mVspBspTree = new VspBspTree();
|
---|
558 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
559 | }
|
---|
560 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
561 | {
|
---|
562 | Debug << "view cell type: VspOsp" << endl;
|
---|
563 | char buf[100];
|
---|
564 | Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);
|
---|
565 |
|
---|
566 | HierarchyManager *mHierarchyManager = CreateHierarchyManager(buf);
|
---|
567 | mViewCellsManager = new VspOspViewCellsManager(vcTree, mHierarchyManager);
|
---|
568 | }
|
---|
569 | else if (strcmp(name, "sceneDependent") == 0) //TODO
|
---|
570 | {
|
---|
571 | Debug << "view cell type: Bsp" << endl;
|
---|
572 |
|
---|
573 | mBspTree = new BspTree();
|
---|
574 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
575 | }
|
---|
576 | else
|
---|
577 | {
|
---|
578 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
579 | exit(1);
|
---|
580 | }
|
---|
581 |
|
---|
582 | return mViewCellsManager;
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | // use ascii format to store rays
|
---|
587 | #define USE_ASCII 0
|
---|
588 |
|
---|
589 |
|
---|
590 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
591 | {
|
---|
592 | return obj1->mId < obj2->mId;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | bool Preprocessor::LoadKdTree(const string filename)
|
---|
597 | {
|
---|
598 | mKdTree = new KdTree();
|
---|
599 | return mKdTree->LoadBinTree(filename.c_str(), mObjects);
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | bool Preprocessor::ExportKdTree(const string filename)
|
---|
604 | {
|
---|
605 | return mKdTree->ExportBinTree(filename.c_str());
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
610 | ObjectContainer &objects) const
|
---|
611 | {
|
---|
612 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
613 | char fileName[100];
|
---|
614 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
615 |
|
---|
616 | Vector3 origin, termination;
|
---|
617 | // HACK: needed only for lower_bound algorithm to find the
|
---|
618 | // intersected objects
|
---|
619 | MeshInstance sObj(NULL);
|
---|
620 | MeshInstance tObj(NULL);
|
---|
621 |
|
---|
622 | #if USE_ASCII
|
---|
623 | ifstream samplesIn(fileName);
|
---|
624 | if (!samplesIn.is_open())
|
---|
625 | return false;
|
---|
626 |
|
---|
627 | string buf;
|
---|
628 | while (!(getline(samplesIn, buf)).eof())
|
---|
629 | {
|
---|
630 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
631 | &origin.x, &origin.y, &origin.z,
|
---|
632 | &termination.x, &termination.y, &termination.z,
|
---|
633 | &(sObj.mId), &(tObj.mId));
|
---|
634 |
|
---|
635 | Intersectable *sourceObj = NULL;
|
---|
636 | Intersectable *termObj = NULL;
|
---|
637 |
|
---|
638 | if (sObj.mId >= 0)
|
---|
639 | {
|
---|
640 | ObjectContainer::iterator oit =
|
---|
641 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
642 | sourceObj = *oit;
|
---|
643 | }
|
---|
644 |
|
---|
645 | if (tObj.mId >= 0)
|
---|
646 | {
|
---|
647 | ObjectContainer::iterator oit =
|
---|
648 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
649 | termObj = *oit;
|
---|
650 | }
|
---|
651 |
|
---|
652 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
653 | }
|
---|
654 | #else
|
---|
655 | ifstream samplesIn(fileName, ios::binary);
|
---|
656 | if (!samplesIn.is_open())
|
---|
657 | return false;
|
---|
658 |
|
---|
659 | while (1)
|
---|
660 | {
|
---|
661 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
662 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
663 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
664 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
665 |
|
---|
666 | if (samplesIn.eof())
|
---|
667 | break;
|
---|
668 |
|
---|
669 | Intersectable *sourceObj = NULL;
|
---|
670 | Intersectable *termObj = NULL;
|
---|
671 |
|
---|
672 | if (sObj.mId >= 0)
|
---|
673 | {
|
---|
674 | ObjectContainer::iterator oit =
|
---|
675 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
676 | sourceObj = *oit;
|
---|
677 | }
|
---|
678 |
|
---|
679 | if (tObj.mId >= 0)
|
---|
680 | {
|
---|
681 | ObjectContainer::iterator oit =
|
---|
682 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
683 | termObj = *oit;
|
---|
684 | }
|
---|
685 |
|
---|
686 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
687 | }
|
---|
688 |
|
---|
689 | #endif
|
---|
690 | samplesIn.close();
|
---|
691 |
|
---|
692 | return true;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
697 | {
|
---|
698 | char fileName[100];
|
---|
699 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
700 |
|
---|
701 |
|
---|
702 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
703 |
|
---|
704 | #if USE_ASCII
|
---|
705 | ofstream samplesOut(fileName);
|
---|
706 | if (!samplesOut.is_open())
|
---|
707 | return false;
|
---|
708 |
|
---|
709 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
710 | {
|
---|
711 | VssRay *ray = *it;
|
---|
712 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
713 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
714 |
|
---|
715 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
716 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
717 | << sourceid << " " << termid << "\n";
|
---|
718 | }
|
---|
719 | #else
|
---|
720 | ofstream samplesOut(fileName, ios::binary);
|
---|
721 | if (!samplesOut.is_open())
|
---|
722 | return false;
|
---|
723 |
|
---|
724 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
725 | {
|
---|
726 | VssRay *ray = *it;
|
---|
727 | Vector3 origin(ray->GetOrigin());
|
---|
728 | Vector3 termination(ray->GetTermination());
|
---|
729 |
|
---|
730 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
731 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
732 |
|
---|
733 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
734 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
735 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
736 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
737 | }
|
---|
738 | #endif
|
---|
739 | samplesOut.close();
|
---|
740 |
|
---|
741 | return true;
|
---|
742 | }
|
---|
743 |
|
---|
744 | bool Preprocessor::GenerateRays(const int number,
|
---|
745 | const int sampleType,
|
---|
746 | SimpleRayContainer &rays)
|
---|
747 | {
|
---|
748 | const int startSize = (int)rays.size();
|
---|
749 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
750 |
|
---|
751 | if (!strategy)
|
---|
752 | {
|
---|
753 | return false;
|
---|
754 | }
|
---|
755 |
|
---|
756 | for (int i=0; (int)rays.size() - startSize < number; ++ i)
|
---|
757 | {
|
---|
758 | SimpleRay newRay;
|
---|
759 |
|
---|
760 | if (strategy->GenerateSample(newRay))
|
---|
761 | {
|
---|
762 | #if 1
|
---|
763 | rays.AddRay(newRay);
|
---|
764 | #else
|
---|
765 | GenerateRayBundle(rays, newRay, 16, 0);
|
---|
766 | #endif
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | delete strategy;
|
---|
771 | return true;
|
---|
772 | }
|
---|
773 |
|
---|
774 |
|
---|
775 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
|
---|
776 | {
|
---|
777 | switch (strategyId)
|
---|
778 | {
|
---|
779 | case SamplingStrategy::OBJECT_BASED_DISTRIBUTION:
|
---|
780 | return new ObjectBasedDistribution(*this);
|
---|
781 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
782 | return new ObjectDirectionBasedDistribution(*this);
|
---|
783 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
784 | return new DirectionBasedDistribution(*this);
|
---|
785 | case SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
786 | return new DirectionBoxBasedDistribution(*this);
|
---|
787 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
788 | return new SpatialBoxBasedDistribution(*this);
|
---|
789 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
790 | // return new ObjectsInteriorDistribution(*this);
|
---|
791 | default: // no valid strategy
|
---|
792 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
793 | return NULL;
|
---|
794 | }
|
---|
795 |
|
---|
796 | // should never come here
|
---|
797 | return NULL;
|
---|
798 | }
|
---|
799 |
|
---|
800 |
|
---|
801 | bool Preprocessor::InitRayCast(const string externKdTree, const string internkdtree)
|
---|
802 | {
|
---|
803 | bool loadKdTree;
|
---|
804 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadKdTree", loadKdTree);
|
---|
805 |
|
---|
806 | if (!loadKdTree)
|
---|
807 | {
|
---|
808 | //-- build new kd tree from scene geometry
|
---|
809 | BuildKdTree();
|
---|
810 | KdTreeStatistics(cout);
|
---|
811 | }
|
---|
812 | else
|
---|
813 | {
|
---|
814 | const long startTime = GetTime();
|
---|
815 | cout << "loading kd tree file " << internkdtree << " ... ";
|
---|
816 |
|
---|
817 | if (!LoadKdTree(internkdtree))
|
---|
818 | {
|
---|
819 | cout << "error loading kd tree with filename " << internkdtree << ", rebuilding it instead ..." << endl;
|
---|
820 |
|
---|
821 | BuildKdTree();
|
---|
822 | KdTreeStatistics(cout);
|
---|
823 | }
|
---|
824 |
|
---|
825 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
826 |
|
---|
827 | if (0)
|
---|
828 | {
|
---|
829 | Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
|
---|
830 |
|
---|
831 | if (exporter)
|
---|
832 | {
|
---|
833 | exporter->ExportKdTree(*mKdTree, true);
|
---|
834 | delete exporter;
|
---|
835 | }
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | int rayCastMethod;
|
---|
840 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
841 |
|
---|
842 | if (rayCastMethod == 0)
|
---|
843 | {
|
---|
844 | mRayCaster = new InternalRayCaster(*this, mKdTree);
|
---|
845 | cout << "ray cast method: internal" << endl;
|
---|
846 | }
|
---|
847 | else
|
---|
848 | {
|
---|
849 | #ifdef GTP_INTERNAL
|
---|
850 | mRayCaster = new IntelRayCaster(*this, externKdTree);
|
---|
851 | cout << "ray cast method: intel" << endl;
|
---|
852 | #endif
|
---|
853 | }
|
---|
854 |
|
---|
855 | return true;
|
---|
856 | }
|
---|
857 |
|
---|
858 |
|
---|
859 | void
|
---|
860 | Preprocessor::CastRays(
|
---|
861 | SimpleRayContainer &rays,
|
---|
862 | VssRayContainer &vssRays,
|
---|
863 | const bool castDoubleRays,
|
---|
864 | const bool pruneInvalidRays
|
---|
865 | )
|
---|
866 | {
|
---|
867 | const long t1 = GetTime();
|
---|
868 |
|
---|
869 | for (int i = 0; i < (int)rays.size();)
|
---|
870 | {
|
---|
871 | if (i + 16 < (int)rays.size())
|
---|
872 | {
|
---|
873 | mRayCaster->CastRays16(
|
---|
874 | i,
|
---|
875 | rays,
|
---|
876 | vssRays,
|
---|
877 | mViewCellsManager->GetViewSpaceBox(),
|
---|
878 | castDoubleRays,
|
---|
879 | pruneInvalidRays);
|
---|
880 | i += 16;
|
---|
881 | }
|
---|
882 | else
|
---|
883 | {
|
---|
884 | mRayCaster->CastRay(
|
---|
885 | rays[i],
|
---|
886 | vssRays,
|
---|
887 | mViewCellsManager->GetViewSpaceBox(),
|
---|
888 | castDoubleRays,
|
---|
889 | pruneInvalidRays);
|
---|
890 | i ++;
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | #if DEBUB_RAYCAST
|
---|
895 | long t2 = GetTime();
|
---|
896 | if (i % 10000 == 0)
|
---|
897 | cout<<".";
|
---|
898 |
|
---|
899 | if (castDoubleRays)
|
---|
900 | cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
901 | else
|
---|
902 | cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
903 | #endif
|
---|
904 | }
|
---|
905 |
|
---|
906 |
|
---|
907 | bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,
|
---|
908 | const SimpleRay &mainRay,
|
---|
909 | const int number,
|
---|
910 | const int pertubType) const
|
---|
911 | {
|
---|
912 | rayBundle.push_back(mainRay);
|
---|
913 |
|
---|
914 | const float pertubOrigin = 10.0f;
|
---|
915 | const float pertubDir = 0.0f;
|
---|
916 |
|
---|
917 | for (int i = 0; i < number - 1; ++ i)
|
---|
918 | {
|
---|
919 | Vector3 pertub;
|
---|
920 |
|
---|
921 | pertub.x = RandomValue(0.0f, pertubDir);
|
---|
922 | pertub.y = RandomValue(0.0f, pertubDir);
|
---|
923 | pertub.z = RandomValue(0.0f, pertubDir);
|
---|
924 | const Vector3 newDir = mainRay.mDirection + pertub;
|
---|
925 | //const Vector3 newDir = mainRay.mDirection;
|
---|
926 |
|
---|
927 | pertub.x = RandomValue(0.0f, pertubOrigin);
|
---|
928 | pertub.y = RandomValue(0.0f, pertubOrigin);
|
---|
929 | pertub.z = RandomValue(0.0f, pertubOrigin);
|
---|
930 | const Vector3 newOrigin = mainRay.mOrigin + pertub;
|
---|
931 | //const Vector3 newOrigin = mainRay.mOrigin;
|
---|
932 |
|
---|
933 | rayBundle.push_back(SimpleRay(newOrigin, newDir, 0));
|
---|
934 | }
|
---|
935 |
|
---|
936 | return true;
|
---|
937 | }
|
---|
938 |
|
---|
939 |
|
---|
940 | void Preprocessor::SetupRay(Ray &ray,
|
---|
941 | const Vector3 &point,
|
---|
942 | const Vector3 &direction
|
---|
943 | ) const
|
---|
944 | {
|
---|
945 | ray.Clear();
|
---|
946 | // do not store anything else then intersections at the ray
|
---|
947 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | }
|
---|