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