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