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