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