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 "VspOspTree.h"
|
---|
16 | #include "ObjParser.h"
|
---|
17 | #ifdef INTEL_COMPILER
|
---|
18 | #include "ArchModeler2MLRT.hxx"
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | namespace GtpVisibilityPreprocessor {
|
---|
22 |
|
---|
23 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
24 |
|
---|
25 | //Preprocessor *preprocessor;
|
---|
26 |
|
---|
27 |
|
---|
28 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
29 | static void AddGeometry(SceneGraph *scene)
|
---|
30 | {
|
---|
31 | scene->mRoot->UpdateBox();
|
---|
32 |
|
---|
33 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
34 |
|
---|
35 | int n = 200;
|
---|
36 |
|
---|
37 | if (0){
|
---|
38 | // form grid of boxes
|
---|
39 | for (int i = 0; i < n; ++ i)
|
---|
40 | {
|
---|
41 | for (int j = 0; j < n; ++ j)
|
---|
42 | {
|
---|
43 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
44 |
|
---|
45 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
46 |
|
---|
47 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
48 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
49 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
50 |
|
---|
51 | mesh->Preprocess();
|
---|
52 |
|
---|
53 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
54 | scene->mRoot->mGeometry.push_back(mi);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | for (int i = 0; i < n; ++ i)
|
---|
59 | {
|
---|
60 | for (int j = 0; j < n; ++ j)
|
---|
61 | {
|
---|
62 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
63 |
|
---|
64 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
65 |
|
---|
66 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
67 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
68 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
69 |
|
---|
70 | mesh->Preprocess();
|
---|
71 |
|
---|
72 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
73 | scene->mRoot->mGeometry.push_back(mi);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | for (int i = 0; i < n; ++ i)
|
---|
78 | {
|
---|
79 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
80 |
|
---|
81 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
82 |
|
---|
83 | //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
|
---|
84 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
85 |
|
---|
86 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
87 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
88 |
|
---|
89 | mesh->Preprocess();
|
---|
90 |
|
---|
91 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
92 | scene->mRoot->mGeometry.push_back(mi);
|
---|
93 | }
|
---|
94 |
|
---|
95 | scene->mRoot->UpdateBox();
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (1)
|
---|
99 | {
|
---|
100 | // plane separating view space regions
|
---|
101 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
102 |
|
---|
103 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
104 |
|
---|
105 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
106 | Mesh *planeMesh = new Mesh();
|
---|
107 |
|
---|
108 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
109 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
110 |
|
---|
111 | planeMesh->Preprocess();
|
---|
112 |
|
---|
113 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
114 | scene->mRoot->mGeometry.push_back(planeMi);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | Preprocessor::Preprocessor():
|
---|
120 | mKdTree(NULL),
|
---|
121 | mBspTree(NULL),
|
---|
122 | mVspBspTree(NULL),
|
---|
123 | mViewCellsManager(NULL),
|
---|
124 | mRenderSimulator(NULL)
|
---|
125 | {
|
---|
126 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
127 |
|
---|
128 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
129 | renderer = NULL;
|
---|
130 |
|
---|
131 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
132 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes);
|
---|
133 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
134 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
135 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
136 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
137 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", mRayCastMethod);
|
---|
138 |
|
---|
139 | char buffer[256];
|
---|
140 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
141 | mVisibilityFileName = buffer;
|
---|
142 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
|
---|
143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
144 | mApplyVisibilitySpatialFilter );
|
---|
145 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
146 |
|
---|
147 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
148 | Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | Preprocessor::~Preprocessor()
|
---|
153 | {
|
---|
154 | cout << "cleaning up" << endl;
|
---|
155 |
|
---|
156 | cout << "Deleting view cells manager ... \n";
|
---|
157 | DEL_PTR(mViewCellsManager);
|
---|
158 | cout << "done.\n";
|
---|
159 |
|
---|
160 | cout << "Deleting bsp tree ... \n";
|
---|
161 | DEL_PTR(mBspTree);
|
---|
162 | cout << "done.\n";
|
---|
163 |
|
---|
164 | cout << "Deleting kd tree...\n";
|
---|
165 | DEL_PTR(mKdTree);
|
---|
166 | cout << "done.\n";
|
---|
167 |
|
---|
168 | cout << "Deleting vsp tree...\n";
|
---|
169 | DEL_PTR(mVspTree);
|
---|
170 | cout << "done.\n";
|
---|
171 |
|
---|
172 | cout << "Deleting osp tree...\n";
|
---|
173 | DEL_PTR(mOspTree);
|
---|
174 | cout << "done.\n";
|
---|
175 |
|
---|
176 | cout << "Deleting vspbsp tree...\n";
|
---|
177 | DEL_PTR(mVspBspTree);
|
---|
178 | cout << "done.\n";
|
---|
179 |
|
---|
180 | cout << "Deleting scene graph...\n";
|
---|
181 | DEL_PTR(mSceneGraph);
|
---|
182 | cout << "done.\n";
|
---|
183 |
|
---|
184 | DEL_PTR(mRenderSimulator);
|
---|
185 | DEL_PTR(renderer);
|
---|
186 | }
|
---|
187 |
|
---|
188 | int
|
---|
189 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
190 | {
|
---|
191 | int pos = 0;
|
---|
192 |
|
---|
193 | while(1) {
|
---|
194 | int npos = (int)str.find(';', pos);
|
---|
195 |
|
---|
196 | if (npos < 0 || npos - pos < 1)
|
---|
197 | break;
|
---|
198 | filenames.push_back(string(str, pos, npos - pos));
|
---|
199 | pos = npos + 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
203 | return (int)filenames.size();
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | bool
|
---|
208 | Preprocessor::LoadScene(const string filename)
|
---|
209 | {
|
---|
210 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
211 | mSceneGraph = new SceneGraph;
|
---|
212 |
|
---|
213 | Parser *parser;
|
---|
214 | vector<string> filenames;
|
---|
215 | int files = SplitFilenames(filename, filenames);
|
---|
216 | cout << "number of input files: " << files << endl;
|
---|
217 | bool result = false;
|
---|
218 | if (files == 1) {
|
---|
219 |
|
---|
220 | if (strstr(filename.c_str(), ".x3d"))
|
---|
221 | parser = new X3dParser;
|
---|
222 | else
|
---|
223 | if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
224 | parser = new PlyParser;
|
---|
225 | else if (strstr(filename.c_str(), ".obj"))
|
---|
226 | parser = new ObjParser;
|
---|
227 | else
|
---|
228 | parser = new UnigraphicsParser;
|
---|
229 |
|
---|
230 | cout<<filename<<endl;
|
---|
231 |
|
---|
232 | if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
|
---|
233 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes, &mFaceParents);
|
---|
234 | else
|
---|
235 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
|
---|
236 |
|
---|
237 | delete parser;
|
---|
238 |
|
---|
239 | } else {
|
---|
240 | // root for different files
|
---|
241 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
242 | for (int i= 0; i < filenames.size(); i++) {
|
---|
243 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
244 | parser = new X3dParser;
|
---|
245 | else
|
---|
246 | parser = new UnigraphicsParser;
|
---|
247 |
|
---|
248 | SceneGraphNode *node;
|
---|
249 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
250 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
251 | // at least one file parsed
|
---|
252 | result = true;
|
---|
253 | }
|
---|
254 | delete parser;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | if (result)
|
---|
260 | {
|
---|
261 | // HACK
|
---|
262 | if (ADDITIONAL_GEOMETRY_HACK)
|
---|
263 | AddGeometry(mSceneGraph);
|
---|
264 |
|
---|
265 | mSceneGraph->AssignObjectIds();
|
---|
266 |
|
---|
267 | int intersectables, faces;
|
---|
268 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
269 |
|
---|
270 | cout<<filename<<" parsed successfully."<<endl;
|
---|
271 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
272 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
273 | mSceneGraph->CollectObjects(&mObjects);
|
---|
274 | mSceneGraph->mRoot->UpdateBox();
|
---|
275 |
|
---|
276 | if (0)
|
---|
277 | {
|
---|
278 | Exporter *exporter = Exporter::GetExporter("testload.x3d");
|
---|
279 |
|
---|
280 | if (exporter)
|
---|
281 | {
|
---|
282 | exporter->ExportGeometry(mObjects);
|
---|
283 | delete exporter;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | return result;
|
---|
290 | }
|
---|
291 |
|
---|
292 | bool
|
---|
293 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
294 | {
|
---|
295 |
|
---|
296 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
297 |
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | bool
|
---|
302 | Preprocessor::PostProcessVisibility()
|
---|
303 | {
|
---|
304 |
|
---|
305 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
306 | cout<<"Applying visibility filter ...";
|
---|
307 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
308 |
|
---|
309 | if (!mViewCellsManager)
|
---|
310 | return false;
|
---|
311 |
|
---|
312 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
313 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
314 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
315 | cout << "done." << endl;
|
---|
316 | }
|
---|
317 |
|
---|
318 | // export the preprocessed information to a file
|
---|
319 | if (mExportVisibility)
|
---|
320 | ExportPreprocessedData(mVisibilityFileName);
|
---|
321 |
|
---|
322 | return true;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | bool
|
---|
327 | Preprocessor::BuildKdTree()
|
---|
328 | {
|
---|
329 | mKdTree = new KdTree;
|
---|
330 | // add mesh instances of the scene graph to the root of the tree
|
---|
331 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
332 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
333 |
|
---|
334 | long startTime = GetTime();
|
---|
335 |
|
---|
336 | cout << "building kd tree ... " << endl;
|
---|
337 | mKdTree->Construct();
|
---|
338 | cout << "construction finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs " << endl;
|
---|
339 | return true;
|
---|
340 | }
|
---|
341 |
|
---|
342 | void
|
---|
343 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
344 | {
|
---|
345 | s<<mKdTree->GetStatistics();
|
---|
346 | }
|
---|
347 |
|
---|
348 | void
|
---|
349 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
350 | {
|
---|
351 | s << mBspTree->GetStatistics();
|
---|
352 | }
|
---|
353 |
|
---|
354 | bool
|
---|
355 | Preprocessor::Export( const string filename,
|
---|
356 | const bool scene,
|
---|
357 | const bool kdtree,
|
---|
358 | const bool bsptree
|
---|
359 | )
|
---|
360 | {
|
---|
361 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
362 |
|
---|
363 | if (exporter) {
|
---|
364 | if (scene)
|
---|
365 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
366 |
|
---|
367 | if (kdtree) {
|
---|
368 | exporter->SetWireframe();
|
---|
369 | exporter->ExportKdTree(*mKdTree);
|
---|
370 | }
|
---|
371 |
|
---|
372 | if (bsptree) {
|
---|
373 | //exporter->SetWireframe();
|
---|
374 | exporter->ExportBspTree(*mBspTree);
|
---|
375 | }
|
---|
376 |
|
---|
377 | delete exporter;
|
---|
378 | return true;
|
---|
379 | }
|
---|
380 |
|
---|
381 | return false;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | bool Preprocessor::PrepareViewCells()
|
---|
386 | {
|
---|
387 | //-- parse view cells construction method
|
---|
388 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
389 | char buf[100];
|
---|
390 |
|
---|
391 | if (mLoadViewCells)
|
---|
392 | {
|
---|
393 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
394 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
|
---|
395 | }
|
---|
396 | else
|
---|
397 | {
|
---|
398 | //-- parse type of view cell container
|
---|
399 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
400 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
401 |
|
---|
402 | // default view space is the extent of the scene
|
---|
403 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
404 |
|
---|
405 |
|
---|
406 | }
|
---|
407 |
|
---|
408 | //-- parameters for render heuristics evaluation
|
---|
409 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
410 |
|
---|
411 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
412 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
413 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
414 |
|
---|
415 | mRenderSimulator =
|
---|
416 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
417 |
|
---|
418 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
419 |
|
---|
420 |
|
---|
421 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
422 | {
|
---|
423 | // NOTE: render texture should be power of 2 and square
|
---|
424 | // renderer must be initialised
|
---|
425 | // $$matt
|
---|
426 | // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
427 | // renderer->makeCurrent();
|
---|
428 |
|
---|
429 | }
|
---|
430 |
|
---|
431 | return true;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
436 | {
|
---|
437 | if (strcmp(name, "kdTree") == 0)
|
---|
438 | {
|
---|
439 | mViewCellsManager = new KdViewCellsManager(mKdTree);
|
---|
440 | }
|
---|
441 | else if (strcmp(name, "bspTree") == 0)
|
---|
442 | {
|
---|
443 | Debug << "view cell type: Bsp" << endl;
|
---|
444 |
|
---|
445 | mBspTree = new BspTree();
|
---|
446 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
447 | }
|
---|
448 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
449 | {
|
---|
450 | Debug << "view cell type: VspBsp" << endl;
|
---|
451 |
|
---|
452 | mVspBspTree = new VspBspTree();
|
---|
453 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
454 | }
|
---|
455 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
456 | {
|
---|
457 | mVspTree = new VspTree();
|
---|
458 | mOspTree = new OspTree();
|
---|
459 |
|
---|
460 | // HACK for testing if per kd evaluation works!!
|
---|
461 | mOspTree = new OspTree(*mKdTree);
|
---|
462 |
|
---|
463 | mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
|
---|
464 | }
|
---|
465 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
466 | {
|
---|
467 | Debug << "view cell type: Bsp" << endl;
|
---|
468 |
|
---|
469 | //TODO
|
---|
470 | mBspTree = new BspTree();
|
---|
471 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
472 | }
|
---|
473 | else
|
---|
474 | {
|
---|
475 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
476 | exit(1);
|
---|
477 | }
|
---|
478 |
|
---|
479 | return mViewCellsManager;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | // use ascii format to store rays
|
---|
484 | #define USE_ASCII 0
|
---|
485 |
|
---|
486 |
|
---|
487 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
488 | {
|
---|
489 | return obj1->mId < obj2->mId;
|
---|
490 | }
|
---|
491 |
|
---|
492 |
|
---|
493 | bool Preprocessor::LoadKdTree()
|
---|
494 | {
|
---|
495 | return true;
|
---|
496 | }
|
---|
497 |
|
---|
498 | bool Preprocessor::ExportKdTree()
|
---|
499 | {
|
---|
500 | return true;
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
505 | ObjectContainer &objects) const
|
---|
506 | {
|
---|
507 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
508 | char fileName[100];
|
---|
509 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
510 |
|
---|
511 | Vector3 origin, termination;
|
---|
512 | // HACK: needed only for lower_bound algorithm to find the
|
---|
513 | // intersected objects
|
---|
514 | MeshInstance sObj(NULL);
|
---|
515 | MeshInstance tObj(NULL);
|
---|
516 |
|
---|
517 | #if USE_ASCII
|
---|
518 | ifstream samplesIn(fileName);
|
---|
519 | if (!samplesIn.is_open())
|
---|
520 | return false;
|
---|
521 |
|
---|
522 | string buf;
|
---|
523 | while (!(getline(samplesIn, buf)).eof())
|
---|
524 | {
|
---|
525 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
526 | &origin.x, &origin.y, &origin.z,
|
---|
527 | &termination.x, &termination.y, &termination.z,
|
---|
528 | &(sObj.mId), &(tObj.mId));
|
---|
529 |
|
---|
530 | Intersectable *sourceObj = NULL;
|
---|
531 | Intersectable *termObj = NULL;
|
---|
532 |
|
---|
533 | if (sObj.mId >= 0)
|
---|
534 | {
|
---|
535 | ObjectContainer::iterator oit =
|
---|
536 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
537 | sourceObj = *oit;
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (tObj.mId >= 0)
|
---|
541 | {
|
---|
542 | ObjectContainer::iterator oit =
|
---|
543 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
544 | termObj = *oit;
|
---|
545 | }
|
---|
546 |
|
---|
547 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
548 | }
|
---|
549 | #else
|
---|
550 | ifstream samplesIn(fileName, ios::binary);
|
---|
551 | if (!samplesIn.is_open())
|
---|
552 | return false;
|
---|
553 |
|
---|
554 | while (1)
|
---|
555 | {
|
---|
556 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
557 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
558 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
559 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
560 |
|
---|
561 | if (samplesIn.eof())
|
---|
562 | break;
|
---|
563 |
|
---|
564 | Intersectable *sourceObj = NULL;
|
---|
565 | Intersectable *termObj = NULL;
|
---|
566 |
|
---|
567 | if (sObj.mId >= 0)
|
---|
568 | {
|
---|
569 | ObjectContainer::iterator oit =
|
---|
570 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
571 | sourceObj = *oit;
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (tObj.mId >= 0)
|
---|
575 | {
|
---|
576 | ObjectContainer::iterator oit =
|
---|
577 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
578 | termObj = *oit;
|
---|
579 | }
|
---|
580 |
|
---|
581 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
582 | }
|
---|
583 |
|
---|
584 | #endif
|
---|
585 | samplesIn.close();
|
---|
586 |
|
---|
587 | return true;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
592 | {
|
---|
593 | char fileName[100];
|
---|
594 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
595 |
|
---|
596 |
|
---|
597 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
598 |
|
---|
599 | #if USE_ASCII
|
---|
600 | ofstream samplesOut(fileName);
|
---|
601 | if (!samplesOut.is_open())
|
---|
602 | return false;
|
---|
603 |
|
---|
604 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
605 | {
|
---|
606 | VssRay *ray = *it;
|
---|
607 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
608 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
609 |
|
---|
610 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
611 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
612 | << sourceid << " " << termid << "\n";
|
---|
613 | }
|
---|
614 | #else
|
---|
615 | ofstream samplesOut(fileName, ios::binary);
|
---|
616 | if (!samplesOut.is_open())
|
---|
617 | return false;
|
---|
618 |
|
---|
619 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
620 | {
|
---|
621 | VssRay *ray = *it;
|
---|
622 | Vector3 origin(ray->GetOrigin());
|
---|
623 | Vector3 termination(ray->GetTermination());
|
---|
624 |
|
---|
625 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
626 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
627 |
|
---|
628 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
629 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
630 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
631 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
632 | }
|
---|
633 | #endif
|
---|
634 | samplesOut.close();
|
---|
635 |
|
---|
636 | return true;
|
---|
637 | }
|
---|
638 |
|
---|
639 | #if 0 // matt: implemented interface samplestrategy
|
---|
640 | bool
|
---|
641 | Preprocessor::GenerateRays(
|
---|
642 | const int number,
|
---|
643 | const int sampleType,
|
---|
644 | SimpleRayContainer &rays
|
---|
645 | )
|
---|
646 | {
|
---|
647 | Vector3 origin, direction;
|
---|
648 | int startSize = (int)rays.size();
|
---|
649 | for (int i=0; (int)rays.size() - startSize < number; i ++) {
|
---|
650 | // now get the direction
|
---|
651 | switch (sampleType) {
|
---|
652 | case OBJECT_BASED_DISTRIBUTION: {
|
---|
653 | mViewCellsManager->GetViewPoint(origin);
|
---|
654 | Vector3 point;
|
---|
655 | Vector3 normal;
|
---|
656 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
657 | Intersectable *object = mObjects[i];
|
---|
658 | object->GetRandomSurfacePoint(point, normal);
|
---|
659 | direction = point - origin;
|
---|
660 | }
|
---|
661 | break;
|
---|
662 | case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
|
---|
663 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
664 | Intersectable *object = mObjects[i];
|
---|
665 | Vector3 normal;
|
---|
666 | object->GetRandomSurfacePoint(origin, normal);
|
---|
667 | direction = UniformRandomVector(normal);
|
---|
668 | origin += 0.1f*direction;
|
---|
669 | }
|
---|
670 | break;
|
---|
671 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
672 | mViewCellsManager->GetViewPoint(origin);
|
---|
673 | direction = UniformRandomVector();
|
---|
674 | break;
|
---|
675 | case DIRECTION_BOX_BASED_DISTRIBUTION: {
|
---|
676 | mViewCellsManager->GetViewPoint(origin);
|
---|
677 | float alpha = RandomValue(0.0f, 2*M_PI);
|
---|
678 | float beta = RandomValue(-M_PI/2, M_PI/2);
|
---|
679 | direction = VssRay::GetDirection(alpha, beta);
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
683 | mViewCellsManager->GetViewPoint(origin);
|
---|
684 | direction = mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
685 | break;
|
---|
686 | default:
|
---|
687 | // unsuported distribution type
|
---|
688 | return false;
|
---|
689 | }
|
---|
690 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
691 | float pdf = 1.0f;
|
---|
692 | float c = Magnitude(direction);
|
---|
693 | if (c > Limits::Small) {
|
---|
694 | direction*=1.0f/c;
|
---|
695 | rays.AddRay(SimpleRay(origin, direction, pdf));
|
---|
696 | }
|
---|
697 | }
|
---|
698 | return true;
|
---|
699 | }
|
---|
700 | #endif
|
---|
701 | bool Preprocessor::GenerateRays(const int number,
|
---|
702 | const int sampleType,
|
---|
703 | SimpleRayContainer &rays)
|
---|
704 | {
|
---|
705 | Vector3 origin, direction;
|
---|
706 |
|
---|
707 | const int startSize = (int)rays.size();
|
---|
708 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
709 |
|
---|
710 | if (!strategy)
|
---|
711 | return false;
|
---|
712 |
|
---|
713 | for (int i=0; (int)rays.size() - startSize < number; ++ i)
|
---|
714 | {
|
---|
715 | SimpleRay newRay;
|
---|
716 | bool success = strategy->GenerateSample(newRay);
|
---|
717 |
|
---|
718 | if (success)
|
---|
719 | rays.AddRay(newRay);
|
---|
720 | }
|
---|
721 |
|
---|
722 | delete strategy;
|
---|
723 |
|
---|
724 | return true;
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
|
---|
729 | {
|
---|
730 | switch (strategyId)
|
---|
731 | {
|
---|
732 | case OBJECT_BASED_DISTRIBUTION:
|
---|
733 | return new ObjectBasedDistribution(*this);
|
---|
734 | case OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
735 | return new ObjectDirectionBasedDistribution(*this);
|
---|
736 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
737 | return new DirectionBasedDistribution(*this);
|
---|
738 | case DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
739 | return new DirectionBoxBasedDistribution(*this);
|
---|
740 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
741 | return new SpatialBoxBasedDistribution(*this);
|
---|
742 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
743 | // return new ObjectsInteriorDistribution(*this);
|
---|
744 | default: // no valid strategy
|
---|
745 | return NULL;
|
---|
746 | }
|
---|
747 |
|
---|
748 | // should never come here
|
---|
749 | return NULL;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | bool Preprocessor::InitRayCast(const string externKdTree)
|
---|
754 | {
|
---|
755 | switch (mRayCastMethod) // use intel ray tracing
|
---|
756 | {
|
---|
757 | case INTEL_RAYCASTER:
|
---|
758 | #ifdef INTEL_COMPILER
|
---|
759 | return mlrtaLoadAS(externKdTree.c_str());
|
---|
760 | #endif
|
---|
761 | case INTERNAL_RAYCASTER:
|
---|
762 | default:
|
---|
763 | break;
|
---|
764 | }
|
---|
765 |
|
---|
766 | return true;
|
---|
767 | }
|
---|
768 |
|
---|
769 | }
|
---|