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 |
|
---|
22 |
|
---|
23 | #ifdef GTP_INTERNAL
|
---|
24 | #include "ArchModeler2MLRT.hxx"
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #define DEBUG_RAYCAST 0
|
---|
28 |
|
---|
29 |
|
---|
30 | namespace GtpVisibilityPreprocessor {
|
---|
31 |
|
---|
32 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
33 |
|
---|
34 | //Preprocessor *preprocessor;
|
---|
35 |
|
---|
36 |
|
---|
37 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
38 | static void AddGeometry(SceneGraph *scene)
|
---|
39 | {
|
---|
40 | scene->GetRoot()->UpdateBox();
|
---|
41 |
|
---|
42 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
43 |
|
---|
44 | int n = 200;
|
---|
45 |
|
---|
46 | if (0)
|
---|
47 | {
|
---|
48 | // form grid of boxes
|
---|
49 | for (int i = 0; i < n; ++ i)
|
---|
50 | {
|
---|
51 | for (int j = 0; j < n; ++ j)
|
---|
52 | {
|
---|
53 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
54 |
|
---|
55 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
56 |
|
---|
57 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
58 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
59 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
60 |
|
---|
61 | mesh->Preprocess();
|
---|
62 |
|
---|
63 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
64 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | for (int i = 0; i < n; ++ i)
|
---|
69 | {
|
---|
70 | for (int j = 0; j < n; ++ j)
|
---|
71 | {
|
---|
72 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
73 |
|
---|
74 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
75 |
|
---|
76 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
77 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
78 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
79 |
|
---|
80 | mesh->Preprocess();
|
---|
81 |
|
---|
82 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
83 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | for (int i = 0; i < n; ++ i)
|
---|
88 | {
|
---|
89 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
90 |
|
---|
91 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
92 |
|
---|
93 | //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
|
---|
94 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
95 |
|
---|
96 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
97 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
98 |
|
---|
99 | mesh->Preprocess();
|
---|
100 |
|
---|
101 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
102 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
103 | }
|
---|
104 |
|
---|
105 | scene->GetRoot()->UpdateBox();
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (1)
|
---|
109 | {
|
---|
110 | // plane separating view space regions
|
---|
111 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
112 |
|
---|
113 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
114 |
|
---|
115 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
116 | Mesh *planeMesh = new Mesh();
|
---|
117 |
|
---|
118 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
119 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
120 |
|
---|
121 | planeMesh->Preprocess();
|
---|
122 |
|
---|
123 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
124 | scene->GetRoot()->mGeometry.push_back(planeMi);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | Preprocessor::Preprocessor():
|
---|
130 | mKdTree(NULL),
|
---|
131 | mBspTree(NULL),
|
---|
132 | mVspBspTree(NULL),
|
---|
133 | mVspTree(NULL),
|
---|
134 | mHierarchyManager(NULL),
|
---|
135 | mViewCellsManager(NULL),
|
---|
136 | mRenderSimulator(NULL),
|
---|
137 | mPass(0),
|
---|
138 | mRayCastMethod(0),
|
---|
139 | mSceneGraph(NULL)
|
---|
140 | {
|
---|
141 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
142 |
|
---|
143 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
144 | renderer = NULL;
|
---|
145 |
|
---|
146 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
147 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
|
---|
148 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
149 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
150 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
151 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
152 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", mRayCastMethod);
|
---|
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 | if (mRayCastMethod == 0)
|
---|
167 | cout << "ray cast method: internal" << endl;
|
---|
168 | else
|
---|
169 | cout << "ray cast method: intel" << endl;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | Preprocessor::~Preprocessor()
|
---|
174 | {
|
---|
175 | cout << "cleaning up" << endl;
|
---|
176 |
|
---|
177 | cout << "Deleting view cells manager ... \n";
|
---|
178 | DEL_PTR(mViewCellsManager);
|
---|
179 | cout << "done.\n";
|
---|
180 |
|
---|
181 | cout << "Deleting bsp tree ... \n";
|
---|
182 | DEL_PTR(mBspTree);
|
---|
183 | cout << "done.\n";
|
---|
184 |
|
---|
185 | cout << "Deleting kd tree...\n";
|
---|
186 | DEL_PTR(mKdTree);
|
---|
187 | cout << "done.\n";
|
---|
188 |
|
---|
189 | cout << "Deleting vsp tree...\n";
|
---|
190 | DEL_PTR(mVspTree);
|
---|
191 | cout << "done.\n";
|
---|
192 |
|
---|
193 | cout << "Deleting hierarchy manager...\n";
|
---|
194 | DEL_PTR(mHierarchyManager);
|
---|
195 | cout << "done.\n";
|
---|
196 |
|
---|
197 | cout << "Deleting vspbsp tree...\n";
|
---|
198 | DEL_PTR(mVspBspTree);
|
---|
199 | cout << "done.\n";
|
---|
200 |
|
---|
201 | cout << "Deleting scene graph...\n";
|
---|
202 | DEL_PTR(mSceneGraph);
|
---|
203 | cout << "done.\n";
|
---|
204 |
|
---|
205 | DEL_PTR(mRenderSimulator);
|
---|
206 | DEL_PTR(renderer);
|
---|
207 | }
|
---|
208 |
|
---|
209 | int
|
---|
210 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
211 | {
|
---|
212 | int pos = 0;
|
---|
213 |
|
---|
214 | while(1) {
|
---|
215 | int npos = (int)str.find(';', pos);
|
---|
216 |
|
---|
217 | if (npos < 0 || npos - pos < 1)
|
---|
218 | break;
|
---|
219 | filenames.push_back(string(str, pos, npos - pos));
|
---|
220 | pos = npos + 1;
|
---|
221 | }
|
---|
222 |
|
---|
223 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
224 | return (int)filenames.size();
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | bool
|
---|
229 | Preprocessor::LoadScene(const string filename)
|
---|
230 | {
|
---|
231 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
232 | mSceneGraph = new SceneGraph;
|
---|
233 |
|
---|
234 | Parser *parser;
|
---|
235 | vector<string> filenames;
|
---|
236 | const int files = SplitFilenames(filename, filenames);
|
---|
237 | cout << "number of input files: " << files << endl;
|
---|
238 | bool result = false;
|
---|
239 |
|
---|
240 | // root for different files
|
---|
241 | mSceneGraph->SetRoot(new SceneGraphNode());
|
---|
242 |
|
---|
243 | // intel ray caster can only trace triangles
|
---|
244 | vector<FaceParentInfo> *fi = (mRayCastMethod == Preprocessor::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 |
|
---|
335 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
336 |
|
---|
337 | return true;
|
---|
338 | }
|
---|
339 |
|
---|
340 | bool
|
---|
341 | Preprocessor::PostProcessVisibility()
|
---|
342 | {
|
---|
343 |
|
---|
344 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
345 | cout<<"Applying visibility filter ...";
|
---|
346 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
347 |
|
---|
348 | if (!mViewCellsManager)
|
---|
349 | return false;
|
---|
350 |
|
---|
351 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
352 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
353 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
354 | cout << "done." << endl;
|
---|
355 | }
|
---|
356 |
|
---|
357 | // export the preprocessed information to a file
|
---|
358 | if (mExportVisibility)
|
---|
359 | ExportPreprocessedData(mVisibilityFileName);
|
---|
360 |
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | bool
|
---|
366 | Preprocessor::BuildKdTree()
|
---|
367 | {
|
---|
368 | mKdTree = new KdTree;
|
---|
369 |
|
---|
370 | // add mesh instances of the scene graph to the root of the tree
|
---|
371 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
372 |
|
---|
373 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
374 |
|
---|
375 | const long startTime = GetTime();
|
---|
376 | cout << "building kd tree ... " << endl;
|
---|
377 |
|
---|
378 | mKdTree->Construct();
|
---|
379 |
|
---|
380 | cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
381 | << " secs " << endl;
|
---|
382 |
|
---|
383 | return true;
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | void
|
---|
388 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
389 | {
|
---|
390 | s<<mKdTree->GetStatistics();
|
---|
391 | }
|
---|
392 |
|
---|
393 | void
|
---|
394 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
395 | {
|
---|
396 | s << mBspTree->GetStatistics();
|
---|
397 | }
|
---|
398 |
|
---|
399 | bool
|
---|
400 | Preprocessor::Export( const string filename,
|
---|
401 | const bool scene,
|
---|
402 | const bool kdtree,
|
---|
403 | const bool bsptree
|
---|
404 | )
|
---|
405 | {
|
---|
406 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
407 |
|
---|
408 | if (exporter) {
|
---|
409 | if (0 && scene)
|
---|
410 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
411 |
|
---|
412 | if (0 && kdtree) {
|
---|
413 | exporter->SetWireframe();
|
---|
414 | exporter->ExportKdTree(*mKdTree);
|
---|
415 | }
|
---|
416 |
|
---|
417 | if (0 && bsptree) {
|
---|
418 | //exporter->SetWireframe();
|
---|
419 | exporter->ExportBspTree(*mBspTree);
|
---|
420 | }
|
---|
421 |
|
---|
422 | delete exporter;
|
---|
423 | return true;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return false;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | bool Preprocessor::PrepareViewCells()
|
---|
431 | {
|
---|
432 | //-- parse view cells construction method
|
---|
433 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
434 | char buf[100];
|
---|
435 |
|
---|
436 | if (mLoadViewCells)
|
---|
437 | {
|
---|
438 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
439 | cout << "loading view cells from " << buf << endl;
|
---|
440 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
|
---|
441 | }
|
---|
442 | else
|
---|
443 | {
|
---|
444 | // parse type of view cell container
|
---|
445 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
446 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
447 |
|
---|
448 | // default view space is the extent of the scene
|
---|
449 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
450 | }
|
---|
451 |
|
---|
452 | //-- parameters for render heuristics evaluation
|
---|
453 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
454 |
|
---|
455 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
456 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
457 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
458 |
|
---|
459 | mRenderSimulator =
|
---|
460 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
461 |
|
---|
462 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
463 |
|
---|
464 |
|
---|
465 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
466 | {
|
---|
467 | // NOTE: render texture should be power of 2 and square
|
---|
468 | // renderer must be initialised
|
---|
469 | // $$matt
|
---|
470 | // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
471 | // renderer->makeCurrent();
|
---|
472 |
|
---|
473 | }
|
---|
474 |
|
---|
475 | return true;
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | bool
|
---|
480 | Preprocessor::ConstructViewCells()
|
---|
481 | {
|
---|
482 | // if not already loaded, construct view cells from file
|
---|
483 | if (!mLoadViewCells) {
|
---|
484 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
485 |
|
---|
486 | // construct view cells using it's own set of samples
|
---|
487 | mViewCellsManager->Construct(this);
|
---|
488 |
|
---|
489 | //-- several visualizations and statistics
|
---|
490 | Debug << "view cells construction finished: " << endl;
|
---|
491 | mViewCellsManager->PrintStatistics(Debug);
|
---|
492 | }
|
---|
493 | return true;
|
---|
494 | }
|
---|
495 |
|
---|
496 |
|
---|
497 | HierarchyManager *Preprocessor::CreateHierarchyManager(const char *name)
|
---|
498 | {
|
---|
499 | HierarchyManager *hierarchyManager;
|
---|
500 |
|
---|
501 | if (strcmp(name, "osp") == 0)
|
---|
502 | {
|
---|
503 | Debug << "hierarchy manager: osp" << endl;
|
---|
504 | // HACK for testing if per kd evaluation works!!
|
---|
505 | const bool ishack = false;
|
---|
506 | if (ishack)
|
---|
507 | hierarchyManager = new HierarchyManager(mVspTree, mKdTree);
|
---|
508 | else
|
---|
509 | hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
|
---|
510 | }
|
---|
511 | else if (strcmp(name, "bvh") == 0)
|
---|
512 | {
|
---|
513 | Debug << "hierarchy manager: bvh" << endl;
|
---|
514 | hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
|
---|
515 | }
|
---|
516 | else // only view space partition
|
---|
517 | {
|
---|
518 | Debug << "hierarchy manager: obj" << endl;
|
---|
519 | hierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::NO_OBJ_SUBDIV);
|
---|
520 | }
|
---|
521 |
|
---|
522 | return hierarchyManager;
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
527 | {
|
---|
528 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
529 |
|
---|
530 | if (strcmp(name, "kdTree") == 0)
|
---|
531 | {
|
---|
532 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
533 | }
|
---|
534 | else if (strcmp(name, "bspTree") == 0)
|
---|
535 | {
|
---|
536 | Debug << "view cell type: Bsp" << endl;
|
---|
537 |
|
---|
538 | mBspTree = new BspTree();
|
---|
539 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
540 | }
|
---|
541 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
542 | {
|
---|
543 | Debug << "view cell type: VspBsp" << endl;
|
---|
544 |
|
---|
545 | mVspBspTree = new VspBspTree();
|
---|
546 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
547 | }
|
---|
548 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
549 | {
|
---|
550 | mVspTree = new VspTree();
|
---|
551 | char buf[100];
|
---|
552 | Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);
|
---|
553 |
|
---|
554 | HierarchyManager *mHierarchyManager = CreateHierarchyManager(buf);
|
---|
555 | mViewCellsManager = new VspOspViewCellsManager(vcTree, mHierarchyManager);
|
---|
556 | }
|
---|
557 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
558 | {
|
---|
559 | Debug << "view cell type: Bsp" << endl;
|
---|
560 |
|
---|
561 | //TODO
|
---|
562 | mBspTree = new BspTree();
|
---|
563 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
564 | }
|
---|
565 | else
|
---|
566 | {
|
---|
567 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
568 | exit(1);
|
---|
569 | }
|
---|
570 |
|
---|
571 | return mViewCellsManager;
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 | // use ascii format to store rays
|
---|
576 | #define USE_ASCII 0
|
---|
577 |
|
---|
578 |
|
---|
579 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
580 | {
|
---|
581 | return obj1->mId < obj2->mId;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | bool Preprocessor::LoadKdTree(const string filename)
|
---|
586 | {
|
---|
587 | mKdTree = new KdTree();
|
---|
588 | return mKdTree->LoadBinTree(filename.c_str(), mObjects);
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | bool Preprocessor::ExportKdTree(const string filename)
|
---|
593 | {
|
---|
594 | return mKdTree->ExportBinTree(filename.c_str());
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
599 | ObjectContainer &objects) const
|
---|
600 | {
|
---|
601 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
602 | char fileName[100];
|
---|
603 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
604 |
|
---|
605 | Vector3 origin, termination;
|
---|
606 | // HACK: needed only for lower_bound algorithm to find the
|
---|
607 | // intersected objects
|
---|
608 | MeshInstance sObj(NULL);
|
---|
609 | MeshInstance tObj(NULL);
|
---|
610 |
|
---|
611 | #if USE_ASCII
|
---|
612 | ifstream samplesIn(fileName);
|
---|
613 | if (!samplesIn.is_open())
|
---|
614 | return false;
|
---|
615 |
|
---|
616 | string buf;
|
---|
617 | while (!(getline(samplesIn, buf)).eof())
|
---|
618 | {
|
---|
619 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
620 | &origin.x, &origin.y, &origin.z,
|
---|
621 | &termination.x, &termination.y, &termination.z,
|
---|
622 | &(sObj.mId), &(tObj.mId));
|
---|
623 |
|
---|
624 | Intersectable *sourceObj = NULL;
|
---|
625 | Intersectable *termObj = NULL;
|
---|
626 |
|
---|
627 | if (sObj.mId >= 0)
|
---|
628 | {
|
---|
629 | ObjectContainer::iterator oit =
|
---|
630 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
631 | sourceObj = *oit;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (tObj.mId >= 0)
|
---|
635 | {
|
---|
636 | ObjectContainer::iterator oit =
|
---|
637 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
638 | termObj = *oit;
|
---|
639 | }
|
---|
640 |
|
---|
641 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
642 | }
|
---|
643 | #else
|
---|
644 | ifstream samplesIn(fileName, ios::binary);
|
---|
645 | if (!samplesIn.is_open())
|
---|
646 | return false;
|
---|
647 |
|
---|
648 | while (1)
|
---|
649 | {
|
---|
650 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
651 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
652 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
653 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
654 |
|
---|
655 | if (samplesIn.eof())
|
---|
656 | break;
|
---|
657 |
|
---|
658 | Intersectable *sourceObj = NULL;
|
---|
659 | Intersectable *termObj = NULL;
|
---|
660 |
|
---|
661 | if (sObj.mId >= 0)
|
---|
662 | {
|
---|
663 | ObjectContainer::iterator oit =
|
---|
664 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
665 | sourceObj = *oit;
|
---|
666 | }
|
---|
667 |
|
---|
668 | if (tObj.mId >= 0)
|
---|
669 | {
|
---|
670 | ObjectContainer::iterator oit =
|
---|
671 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
672 | termObj = *oit;
|
---|
673 | }
|
---|
674 |
|
---|
675 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
676 | }
|
---|
677 |
|
---|
678 | #endif
|
---|
679 | samplesIn.close();
|
---|
680 |
|
---|
681 | return true;
|
---|
682 | }
|
---|
683 |
|
---|
684 |
|
---|
685 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
686 | {
|
---|
687 | char fileName[100];
|
---|
688 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
689 |
|
---|
690 |
|
---|
691 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
692 |
|
---|
693 | #if USE_ASCII
|
---|
694 | ofstream samplesOut(fileName);
|
---|
695 | if (!samplesOut.is_open())
|
---|
696 | return false;
|
---|
697 |
|
---|
698 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
699 | {
|
---|
700 | VssRay *ray = *it;
|
---|
701 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
702 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
703 |
|
---|
704 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
705 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
706 | << sourceid << " " << termid << "\n";
|
---|
707 | }
|
---|
708 | #else
|
---|
709 | ofstream samplesOut(fileName, ios::binary);
|
---|
710 | if (!samplesOut.is_open())
|
---|
711 | return false;
|
---|
712 |
|
---|
713 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
714 | {
|
---|
715 | VssRay *ray = *it;
|
---|
716 | Vector3 origin(ray->GetOrigin());
|
---|
717 | Vector3 termination(ray->GetTermination());
|
---|
718 |
|
---|
719 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
720 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
721 |
|
---|
722 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
723 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
724 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
725 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
726 | }
|
---|
727 | #endif
|
---|
728 | samplesOut.close();
|
---|
729 |
|
---|
730 | return true;
|
---|
731 | }
|
---|
732 |
|
---|
733 | bool Preprocessor::GenerateRays(const int number,
|
---|
734 | const int sampleType,
|
---|
735 | SimpleRayContainer &rays)
|
---|
736 | {
|
---|
737 | const int startSize = (int)rays.size();
|
---|
738 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
739 |
|
---|
740 | if (!strategy)
|
---|
741 | {
|
---|
742 | return false;
|
---|
743 | }
|
---|
744 |
|
---|
745 | for (int i=0; (int)rays.size() - startSize < number; ++ i)
|
---|
746 | {
|
---|
747 | SimpleRay newRay;
|
---|
748 |
|
---|
749 | if (strategy->GenerateSample(newRay))
|
---|
750 | {
|
---|
751 | #if 1
|
---|
752 | rays.AddRay(newRay);
|
---|
753 | #else
|
---|
754 | GenerateRayBundle(rays, newRay, 16, 0);
|
---|
755 | #endif
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | delete strategy;
|
---|
760 | return true;
|
---|
761 | }
|
---|
762 |
|
---|
763 |
|
---|
764 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
|
---|
765 | {
|
---|
766 | switch (strategyId)
|
---|
767 | {
|
---|
768 | case OBJECT_BASED_DISTRIBUTION:
|
---|
769 | return new ObjectBasedDistribution(*this);
|
---|
770 | case OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
771 | return new ObjectDirectionBasedDistribution(*this);
|
---|
772 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
773 | return new DirectionBasedDistribution(*this);
|
---|
774 | case DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
775 | return new DirectionBoxBasedDistribution(*this);
|
---|
776 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
777 | return new SpatialBoxBasedDistribution(*this);
|
---|
778 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
779 | // return new ObjectsInteriorDistribution(*this);
|
---|
780 | default: // no valid strategy
|
---|
781 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
782 | return NULL;
|
---|
783 | }
|
---|
784 |
|
---|
785 | // should never come here
|
---|
786 | return NULL;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | bool Preprocessor::InitRayCast(const string externKdTree)
|
---|
791 | {
|
---|
792 | bool loadKdTree, exportKdTree;
|
---|
793 |
|
---|
794 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadKdTree", loadKdTree);
|
---|
795 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
|
---|
796 |
|
---|
797 | char kdtreename[100];
|
---|
798 | Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", kdtreename);
|
---|
799 |
|
---|
800 |
|
---|
801 | if (!loadKdTree)
|
---|
802 | {
|
---|
803 | /////////////////
|
---|
804 | //-- build new kd tree from scene geometry
|
---|
805 |
|
---|
806 | BuildKdTree();
|
---|
807 | KdTreeStatistics(cout);
|
---|
808 | }
|
---|
809 | else
|
---|
810 | {
|
---|
811 | const long startTime = GetTime();
|
---|
812 | cout << "loading kd tree file " << kdtreename << " ... ";
|
---|
813 |
|
---|
814 | if (!LoadKdTree(kdtreename))
|
---|
815 | {
|
---|
816 | cout << "error loading kd tree with filename " << kdtreename << endl;
|
---|
817 | return false;
|
---|
818 | }
|
---|
819 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
820 |
|
---|
821 | if (0)
|
---|
822 | {
|
---|
823 | Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
|
---|
824 |
|
---|
825 | if (exporter)
|
---|
826 | {
|
---|
827 | exporter->ExportKdTree(*mKdTree, true);
|
---|
828 | delete exporter;
|
---|
829 | }
|
---|
830 | }
|
---|
831 | }
|
---|
832 |
|
---|
833 | if (exportKdTree)
|
---|
834 | {
|
---|
835 | const long startTime = GetTime();
|
---|
836 | cout << "exporting kd tree ... ";
|
---|
837 | if (!ExportKdTree(kdtreename))
|
---|
838 | {
|
---|
839 | cout << " error exporting kd tree with filename " << kdtreename << endl;
|
---|
840 | }
|
---|
841 | else
|
---|
842 | {
|
---|
843 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | switch (mRayCastMethod) // use intel ray tracing
|
---|
848 | {
|
---|
849 | case INTEL_RAYCASTER:
|
---|
850 | #ifdef GTP_INTERNAL
|
---|
851 | cout<<"Ray Cast file: " << externKdTree << endl;
|
---|
852 | return mlrtaLoadAS(externKdTree.c_str());
|
---|
853 | #endif
|
---|
854 | case INTERNAL_RAYCASTER:
|
---|
855 | default:
|
---|
856 | break;
|
---|
857 | }
|
---|
858 |
|
---|
859 | return true;
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | int Preprocessor::CastIntelDoubleRay(
|
---|
864 | const Vector3 &viewPoint,
|
---|
865 | const Vector3 &direction,
|
---|
866 | const float probability,
|
---|
867 | VssRayContainer &vssRays,
|
---|
868 | const AxisAlignedBox3 &box
|
---|
869 | )
|
---|
870 | {
|
---|
871 | #ifdef GTP_INTERNAL
|
---|
872 | //cout << "intel ray" << endl;
|
---|
873 | VssRay *vssRay = NULL;
|
---|
874 | int hits = 0;
|
---|
875 | int hittriangle;
|
---|
876 | Vector3 pointA, pointB;
|
---|
877 | Vector3 normalA, normalB;
|
---|
878 | Intersectable *objectA = NULL, *objectB = NULL;
|
---|
879 | float dist;
|
---|
880 | double normal[3];
|
---|
881 |
|
---|
882 | hittriangle = mlrtaIntersectAS(&viewPoint.x,
|
---|
883 | &direction.x,
|
---|
884 | normal,
|
---|
885 | dist);
|
---|
886 |
|
---|
887 | if (hittriangle != -1 ) {
|
---|
888 | if (hittriangle >= mFaceParents.size())
|
---|
889 | cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
|
---|
890 | else {
|
---|
891 | objectA = mFaceParents[hittriangle].mObject;
|
---|
892 | normalA = Vector3(normal[0], normal[1], normal[2]);
|
---|
893 | // Get the normal of that face
|
---|
894 | // Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
|
---|
895 | // normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
896 | //-rays[index+i].mDirection; // $$ temporary
|
---|
897 | pointA = viewPoint + direction*dist;
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | Vector3 dir = -direction;
|
---|
903 | hittriangle = mlrtaIntersectAS(&viewPoint.x,
|
---|
904 | &dir.x,
|
---|
905 | normal,
|
---|
906 | dist);
|
---|
907 |
|
---|
908 | if (hittriangle != -1 ) {
|
---|
909 | if (hittriangle >= mFaceParents.size())
|
---|
910 | cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
|
---|
911 | else {
|
---|
912 | objectB = mFaceParents[hittriangle].mObject;
|
---|
913 | normalB = Vector3(normal[0], normal[1], normal[2]);
|
---|
914 | // Get the normal of that face
|
---|
915 | // Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
|
---|
916 | // normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
917 | //-rays[index+i].mDirection; // $$ temporary
|
---|
918 | pointB = viewPoint + dir * dist;
|
---|
919 | }
|
---|
920 | }
|
---|
921 |
|
---|
922 | return ProcessRay(viewPoint,
|
---|
923 | direction,
|
---|
924 | objectA, pointA, normalA,
|
---|
925 | objectB, pointB, normalB,
|
---|
926 | probability,
|
---|
927 | vssRays,
|
---|
928 | box
|
---|
929 | );
|
---|
930 | #else
|
---|
931 | return -1;
|
---|
932 | #endif
|
---|
933 | }
|
---|
934 |
|
---|
935 |
|
---|
936 | Intersectable *Preprocessor::CastIntelSingleRay(const Vector3 &viewPoint,
|
---|
937 | const Vector3 &direction,
|
---|
938 | //const float probability,
|
---|
939 | Vector3 &tPoint,
|
---|
940 | const AxisAlignedBox3 &box
|
---|
941 | )
|
---|
942 | {
|
---|
943 | AxisAlignedBox3 sbox = box;
|
---|
944 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
945 |
|
---|
946 | if (!sbox.IsInside(viewPoint))
|
---|
947 | return 0;
|
---|
948 |
|
---|
949 | #ifdef GTP_INTERNAL
|
---|
950 | float pforg[3];
|
---|
951 | float pfdir[3];
|
---|
952 | double pfnorm[3];
|
---|
953 |
|
---|
954 | pforg[0] = viewPoint[0]; pforg[1] = viewPoint[1]; pforg[2] = viewPoint[2];
|
---|
955 | pfdir[0] = direction[0]; pfdir[1] = direction[1]; pfdir[2] = direction[2];
|
---|
956 |
|
---|
957 | float dist;
|
---|
958 | const int hittriangle = mlrtaIntersectAS(pforg, pfdir, pfnorm, dist);
|
---|
959 |
|
---|
960 | if (hittriangle == -1)
|
---|
961 | {
|
---|
962 | static Ray ray;
|
---|
963 | SetupRay(ray, viewPoint, direction);
|
---|
964 |
|
---|
965 | float tmin = 0, tmax;
|
---|
966 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
967 | {
|
---|
968 | tPoint = ray.Extrap(tmax);
|
---|
969 | }
|
---|
970 |
|
---|
971 | return NULL;
|
---|
972 | }
|
---|
973 | else {
|
---|
974 | if (hittriangle >= mFaceParents.size()) {
|
---|
975 | cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
|
---|
976 | return NULL;
|
---|
977 | }
|
---|
978 | else {
|
---|
979 |
|
---|
980 | tPoint[0] = pforg[0] + pfdir[0] * dist;
|
---|
981 | tPoint[1] = pforg[1] + pfdir[1] * dist;
|
---|
982 | tPoint[2] = pforg[2] + pfdir[2] * dist;
|
---|
983 |
|
---|
984 | return mFaceParents[hittriangle].mObject;
|
---|
985 | }
|
---|
986 | }
|
---|
987 |
|
---|
988 | #else
|
---|
989 | const int hittriangle = -1;
|
---|
990 | return NULL;
|
---|
991 | #endif
|
---|
992 |
|
---|
993 | }
|
---|
994 |
|
---|
995 |
|
---|
996 | int Preprocessor::CastInternalRay(
|
---|
997 | const Vector3 &viewPoint,
|
---|
998 | const Vector3 &direction,
|
---|
999 | const float probability,
|
---|
1000 | VssRayContainer &vssRays,
|
---|
1001 | const AxisAlignedBox3 &box
|
---|
1002 | )
|
---|
1003 | {
|
---|
1004 | //cout << "internal ray" << endl;
|
---|
1005 | int hits = 0;
|
---|
1006 | static Ray ray;
|
---|
1007 | Intersectable *objectA, *objectB;
|
---|
1008 | Vector3 pointA, pointB;
|
---|
1009 |
|
---|
1010 | // AxisAlignedBox3 box = Union(mKdTree->GetBox(), mViewCellsManager->GetViewSpaceBox());
|
---|
1011 | AxisAlignedBox3 sbox = box;
|
---|
1012 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
1013 | if (!sbox.IsInside(viewPoint))
|
---|
1014 | return 0;
|
---|
1015 |
|
---|
1016 | SetupRay(ray, viewPoint, direction);
|
---|
1017 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
1018 |
|
---|
1019 | // cast ray to KD tree to find intersection with other objects
|
---|
1020 | float bsize = Magnitude(box.Size());
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | if (mKdTree->CastRay(ray)) {
|
---|
1024 | objectA = ray.intersections[0].mObject;
|
---|
1025 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
1026 | if (mDetectEmptyViewSpace)
|
---|
1027 | if (DotProd(ray.intersections[0].mNormal, direction) >= 0) {
|
---|
1028 | // discard the sample
|
---|
1029 | return 0;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | } else {
|
---|
1033 | objectA = NULL;
|
---|
1034 | // compute intersection with the scene bounding box
|
---|
1035 | float tmin, tmax;
|
---|
1036 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
1037 | pointA = ray.Extrap(tmax);
|
---|
1038 | else
|
---|
1039 | return 0;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | SetupRay(ray, viewPoint, -direction);
|
---|
1043 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
1044 |
|
---|
1045 | if (mKdTree->CastRay(ray)) {
|
---|
1046 | objectB = ray.intersections[0].mObject;
|
---|
1047 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
1048 | if (mDetectEmptyViewSpace)
|
---|
1049 | if (DotProd(ray.intersections[0].mNormal, direction) <= 0) {
|
---|
1050 | // discard the sample
|
---|
1051 | return 0;
|
---|
1052 | }
|
---|
1053 | } else {
|
---|
1054 | objectB = NULL;
|
---|
1055 | float tmin, tmax;
|
---|
1056 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
1057 | pointB = ray.Extrap(tmax);
|
---|
1058 | else
|
---|
1059 | return 0;
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 |
|
---|
1063 | VssRay *vssRay = NULL;
|
---|
1064 | bool validSample = (objectA != objectB);
|
---|
1065 | if (validSample) {
|
---|
1066 | if (objectA) {
|
---|
1067 | vssRay = new VssRay(pointB,
|
---|
1068 | pointA,
|
---|
1069 | objectB,
|
---|
1070 | objectA,
|
---|
1071 | mPass,
|
---|
1072 | probability
|
---|
1073 | );
|
---|
1074 | vssRays.push_back(vssRay);
|
---|
1075 | //cout << "ray: " << *vssRay << endl;
|
---|
1076 | hits ++;
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | if (objectB) {
|
---|
1080 | vssRay = new VssRay(pointA,
|
---|
1081 | pointB,
|
---|
1082 | objectA,
|
---|
1083 | objectB,
|
---|
1084 | mPass,
|
---|
1085 | probability
|
---|
1086 | );
|
---|
1087 | vssRays.push_back(vssRay);
|
---|
1088 | //cout << "ray: " << *vssRay << endl;
|
---|
1089 | hits ++;
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | return hits;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 |
|
---|
1097 | int
|
---|
1098 | Preprocessor::ProcessRay(
|
---|
1099 | const Vector3 &viewPoint,
|
---|
1100 | const Vector3 &direction,
|
---|
1101 | Intersectable *objectA,
|
---|
1102 | Vector3 &pointA,
|
---|
1103 | const Vector3 &normalA,
|
---|
1104 | Intersectable *objectB,
|
---|
1105 | Vector3 &pointB,
|
---|
1106 | const Vector3 &normalB,
|
---|
1107 | const float probability,
|
---|
1108 | VssRayContainer &vssRays,
|
---|
1109 | const AxisAlignedBox3 &box
|
---|
1110 | )
|
---|
1111 | {
|
---|
1112 | int hits=0;
|
---|
1113 | #if DEBUG_RAYCAST
|
---|
1114 | Debug<<"PR ";
|
---|
1115 | #endif
|
---|
1116 | if (objectA == NULL && objectB == NULL)
|
---|
1117 | return 0;
|
---|
1118 |
|
---|
1119 | AxisAlignedBox3 sbox = box;
|
---|
1120 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
1121 |
|
---|
1122 | if (!sbox.IsInside(viewPoint))
|
---|
1123 | return 0;
|
---|
1124 |
|
---|
1125 | if (objectA == NULL) {
|
---|
1126 | // compute intersection with the scene bounding box
|
---|
1127 | static Ray ray;
|
---|
1128 | SetupRay(ray, viewPoint, direction);
|
---|
1129 |
|
---|
1130 | float tmin, tmax;
|
---|
1131 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
1132 | pointA = ray.Extrap(tmax);
|
---|
1133 | else
|
---|
1134 | return 0;
|
---|
1135 | } else {
|
---|
1136 | if (mDetectEmptyViewSpace)
|
---|
1137 | if (DotProd(normalA, direction) >= 0) {
|
---|
1138 | // discard the sample
|
---|
1139 | return 0;
|
---|
1140 | }
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | if (objectB == NULL) {
|
---|
1144 | // compute intersection with the scene bounding box
|
---|
1145 | static Ray ray;
|
---|
1146 | SetupRay(ray, viewPoint, -direction);
|
---|
1147 |
|
---|
1148 | float tmin, tmax;
|
---|
1149 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
1150 | pointB = ray.Extrap(tmax);
|
---|
1151 | else
|
---|
1152 | return 0;
|
---|
1153 | } else {
|
---|
1154 | if (mDetectEmptyViewSpace)
|
---|
1155 | if (DotProd(normalB, direction) <= 0) {
|
---|
1156 | // discard the sample
|
---|
1157 | return 0;
|
---|
1158 | }
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | VssRay *vssRay = NULL;
|
---|
1162 | bool validSample = (objectA != objectB);
|
---|
1163 | if (validSample) {
|
---|
1164 | if (objectA) {
|
---|
1165 | vssRay = new VssRay(pointB,
|
---|
1166 | pointA,
|
---|
1167 | objectB,
|
---|
1168 | objectA,
|
---|
1169 | mPass,
|
---|
1170 | probability
|
---|
1171 | );
|
---|
1172 | vssRays.push_back(vssRay);
|
---|
1173 | //cout << "ray: " << *vssRay << endl;
|
---|
1174 | hits ++;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | if (objectB) {
|
---|
1178 | vssRay = new VssRay(pointA,
|
---|
1179 | pointB,
|
---|
1180 | objectA,
|
---|
1181 | objectB,
|
---|
1182 | mPass,
|
---|
1183 | probability
|
---|
1184 | );
|
---|
1185 | vssRays.push_back(vssRay);
|
---|
1186 | //cout << "ray: " << *vssRay << endl;
|
---|
1187 | hits ++;
|
---|
1188 | }
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | return hits;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | void
|
---|
1196 | Preprocessor::CastRays16(const int index,
|
---|
1197 | SimpleRayContainer &rays,
|
---|
1198 | VssRayContainer &vssRays,
|
---|
1199 | const AxisAlignedBox3 &sbox)
|
---|
1200 | {
|
---|
1201 | int i;
|
---|
1202 | const int num = 16;
|
---|
1203 |
|
---|
1204 | #if DEBUG_RAYCAST
|
---|
1205 | Debug<<"C16 "<<flush;
|
---|
1206 | #endif
|
---|
1207 | if (mRayCastMethod == INTEL_RAYCASTER) {
|
---|
1208 | #ifdef GTP_INTERNAL
|
---|
1209 |
|
---|
1210 | int forward_hit_triangles[16];
|
---|
1211 | float forward_dist[16];
|
---|
1212 |
|
---|
1213 | int backward_hit_triangles[16];
|
---|
1214 | float backward_dist[16];
|
---|
1215 |
|
---|
1216 |
|
---|
1217 | Vector3 min = sbox.Min();
|
---|
1218 | Vector3 max = sbox.Max();
|
---|
1219 |
|
---|
1220 | for (i=0; i < num; i++) {
|
---|
1221 | mlrtaStoreRayAS16(&rays[index + i].mOrigin.x,
|
---|
1222 | &rays[index + i].mDirection.x,
|
---|
1223 | i);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | mlrtaTraverseGroupAS16(&min.x,
|
---|
1227 | &max.x,
|
---|
1228 | forward_hit_triangles,
|
---|
1229 | forward_dist);
|
---|
1230 |
|
---|
1231 | for (i=0; i < num; i++) {
|
---|
1232 | Vector3 dir = -rays[index + i].mDirection;
|
---|
1233 | mlrtaStoreRayAS16(&rays[index+i].mOrigin.x,
|
---|
1234 | &dir.x,
|
---|
1235 | i);
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | mlrtaTraverseGroupAS16(&min.x,
|
---|
1239 | &max.x,
|
---|
1240 | backward_hit_triangles,
|
---|
1241 | backward_dist);
|
---|
1242 |
|
---|
1243 |
|
---|
1244 | for (i=0; i < num; i++) {
|
---|
1245 | Intersectable *objectA = NULL, *objectB = NULL;
|
---|
1246 | Vector3 pointA, pointB;
|
---|
1247 | Vector3 normalA, normalB;
|
---|
1248 |
|
---|
1249 | if (forward_hit_triangles[i] != -1 ) {
|
---|
1250 | if (forward_hit_triangles[i] >= mFaceParents.size())
|
---|
1251 | cerr<<"Warning: triangle index out of range! "<<forward_hit_triangles[i]<<endl;
|
---|
1252 | else {
|
---|
1253 | objectA = mFaceParents[forward_hit_triangles[i]].mObject;
|
---|
1254 | // Get the normal of that face
|
---|
1255 | normalA = objectA->GetNormal(mFaceParents[forward_hit_triangles[i]].mFaceIndex);
|
---|
1256 | //-rays[index+i].mDirection; // $$ temporary
|
---|
1257 | pointA = rays[index+i].Extrap(forward_dist[i]);
|
---|
1258 | }
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | if (backward_hit_triangles[i]!=-1) {
|
---|
1262 | if (backward_hit_triangles[i] >= mFaceParents.size())
|
---|
1263 | cerr<<"Warning: triangle index out of range! "<<backward_hit_triangles[i]<<endl;
|
---|
1264 | else {
|
---|
1265 | objectB = mFaceParents[backward_hit_triangles[i]].mObject;
|
---|
1266 | normalB = objectB->GetNormal(mFaceParents[forward_hit_triangles[i]].mFaceIndex);
|
---|
1267 |
|
---|
1268 | // normalB = rays[index+i].mDirection; // $$ temporary
|
---|
1269 | pointB = rays[index+i].Extrap(-backward_dist[i]);
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | ProcessRay(rays[index+i].mOrigin,
|
---|
1274 | rays[index+i].mDirection,
|
---|
1275 | objectA, pointA, normalA,
|
---|
1276 | objectB, pointB, normalB,
|
---|
1277 | rays[index+i].mPdf,
|
---|
1278 | vssRays,
|
---|
1279 | sbox
|
---|
1280 | );
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | #endif
|
---|
1284 |
|
---|
1285 | } else {
|
---|
1286 |
|
---|
1287 | for (i=index; i < index + num; i++) {
|
---|
1288 | CastRay(rays[i].mOrigin,
|
---|
1289 | rays[i].mDirection,
|
---|
1290 | rays[i].mPdf,
|
---|
1291 | vssRays,
|
---|
1292 | sbox);
|
---|
1293 | }
|
---|
1294 | }
|
---|
1295 | #if DEBUG_RAYCAST
|
---|
1296 | Debug<<"C16F\n"<<flush;
|
---|
1297 | #endif
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | void
|
---|
1301 | Preprocessor::CastRays(
|
---|
1302 | SimpleRayContainer &rays,
|
---|
1303 | VssRayContainer &vssRays
|
---|
1304 | )
|
---|
1305 | {
|
---|
1306 | long t1 = GetTime();
|
---|
1307 |
|
---|
1308 | for (int i = 0; i < (int)rays.size(); ) {
|
---|
1309 | // method only available for intel raycaster yet
|
---|
1310 | if (i + 16 < (int)rays.size()) {
|
---|
1311 |
|
---|
1312 | CastRays16(
|
---|
1313 | i,
|
---|
1314 | rays,
|
---|
1315 | vssRays,
|
---|
1316 | mViewCellsManager->GetViewSpaceBox());
|
---|
1317 | i += 16;
|
---|
1318 | } else {
|
---|
1319 | CastRay(rays[i].mOrigin,
|
---|
1320 | rays[i].mDirection,
|
---|
1321 | rays[i].mPdf,
|
---|
1322 | vssRays,
|
---|
1323 | mViewCellsManager->GetViewSpaceBox());
|
---|
1324 | i++;
|
---|
1325 | }
|
---|
1326 | if (i % 10000 == 0)
|
---|
1327 | cout<<".";
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | long t2 = GetTime();
|
---|
1331 | cout<<2*rays.size()/(1e3*TimeDiff(t1, t2))<<"M rays/s"<<endl;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | Intersectable *
|
---|
1335 | Preprocessor::CastSimpleRay(
|
---|
1336 | const Vector3 &viewPoint,
|
---|
1337 | const Vector3 &direction,
|
---|
1338 | const AxisAlignedBox3 &box,
|
---|
1339 | Vector3 &point,
|
---|
1340 | Vector3 &normal
|
---|
1341 | )
|
---|
1342 | {
|
---|
1343 | Intersectable *result = NULL;
|
---|
1344 | switch (mRayCastMethod)
|
---|
1345 | {
|
---|
1346 | case INTEL_RAYCASTER: {
|
---|
1347 |
|
---|
1348 | int hittriangle;
|
---|
1349 |
|
---|
1350 | #ifdef GTP_INTERNAL
|
---|
1351 | float dist;
|
---|
1352 | double n[3];
|
---|
1353 |
|
---|
1354 | hittriangle = mlrtaIntersectAS(&viewPoint.x,
|
---|
1355 | &direction.x,
|
---|
1356 | n,
|
---|
1357 | dist);
|
---|
1358 |
|
---|
1359 | if (hittriangle !=-1 ) {
|
---|
1360 | if (hittriangle >= mFaceParents.size())
|
---|
1361 | cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
|
---|
1362 | else {
|
---|
1363 | result = mFaceParents[hittriangle].mObject;
|
---|
1364 | normal = Vector3(n[0], n[1], n[2]);
|
---|
1365 | point = viewPoint + direction*dist;
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 | #else
|
---|
1369 | hittriangle = -1;
|
---|
1370 | #endif
|
---|
1371 |
|
---|
1372 | break;
|
---|
1373 | }
|
---|
1374 | case INTERNAL_RAYCASTER:
|
---|
1375 | default: {
|
---|
1376 | static Ray ray;
|
---|
1377 |
|
---|
1378 | ray.intersections.clear();
|
---|
1379 | // do not store anything else then intersections at the ray
|
---|
1380 | ray.Init(viewPoint, direction, Ray::LOCAL_RAY);
|
---|
1381 |
|
---|
1382 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
1383 |
|
---|
1384 | if (mKdTree->CastRay(ray)) {
|
---|
1385 | result = ray.intersections[0].mObject;
|
---|
1386 | point = ray.Extrap(ray.intersections[0].mT);
|
---|
1387 | normal = ray.intersections[0].mNormal;
|
---|
1388 | }
|
---|
1389 | break;
|
---|
1390 | }
|
---|
1391 | }
|
---|
1392 | return result;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | int
|
---|
1396 | Preprocessor::CastRay(
|
---|
1397 | const Vector3 &viewPoint,
|
---|
1398 | const Vector3 &direction,
|
---|
1399 | const float probability,
|
---|
1400 | VssRayContainer &vssRays,
|
---|
1401 | const AxisAlignedBox3 &box
|
---|
1402 | )
|
---|
1403 | {
|
---|
1404 | #if DEBUG_RAYCAST
|
---|
1405 | Debug<<"CR "<<flush;
|
---|
1406 | #endif
|
---|
1407 | switch (mRayCastMethod)
|
---|
1408 | {
|
---|
1409 | case INTEL_RAYCASTER:
|
---|
1410 | return CastIntelDoubleRay(viewPoint, direction, probability, vssRays, box);
|
---|
1411 | case INTERNAL_RAYCASTER:
|
---|
1412 | default:
|
---|
1413 | return CastInternalRay(viewPoint, direction, probability, vssRays, box);
|
---|
1414 | }
|
---|
1415 | #if DEBUG_RAYCAST
|
---|
1416 | Debug<<"CRF "<<flush;
|
---|
1417 | #endif
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,
|
---|
1422 | const SimpleRay &mainRay,
|
---|
1423 | const int number,
|
---|
1424 | const int pertubType) const
|
---|
1425 | {
|
---|
1426 | rayBundle.push_back(mainRay);
|
---|
1427 |
|
---|
1428 | const float pertubOrigin = 10.0f;
|
---|
1429 | const float pertubDir = 0.0f;
|
---|
1430 |
|
---|
1431 | for (int i = 0; i < number - 1; ++ i)
|
---|
1432 | {
|
---|
1433 | Vector3 pertub;
|
---|
1434 |
|
---|
1435 | pertub.x = RandomValue(0.0f, pertubDir);
|
---|
1436 | pertub.y = RandomValue(0.0f, pertubDir);
|
---|
1437 | pertub.z = RandomValue(0.0f, pertubDir);
|
---|
1438 | const Vector3 newDir = mainRay.mDirection + pertub;
|
---|
1439 | //const Vector3 newDir = mainRay.mDirection;
|
---|
1440 |
|
---|
1441 | pertub.x = RandomValue(0.0f, pertubOrigin);
|
---|
1442 | pertub.y = RandomValue(0.0f, pertubOrigin);
|
---|
1443 | pertub.z = RandomValue(0.0f, pertubOrigin);
|
---|
1444 | const Vector3 newOrigin = mainRay.mOrigin + pertub;
|
---|
1445 | //const Vector3 newOrigin = mainRay.mOrigin;
|
---|
1446 |
|
---|
1447 | rayBundle.push_back(SimpleRay(newOrigin, newDir, 0));
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | return true;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 |
|
---|
1454 | void Preprocessor::SetupRay(Ray &ray,
|
---|
1455 | const Vector3 &point,
|
---|
1456 | const Vector3 &direction
|
---|
1457 | )
|
---|
1458 | {
|
---|
1459 | ray.Clear();
|
---|
1460 | // do not store anything else then intersections at the ray
|
---|
1461 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | }
|
---|