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