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