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 "VspKdTree.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "GlRenderer.h"
|
---|
14 |
|
---|
15 | Preprocessor *preprocessor;
|
---|
16 |
|
---|
17 | Preprocessor::Preprocessor():
|
---|
18 | mKdTree(NULL),
|
---|
19 | mBspTree(NULL),
|
---|
20 | mVspKdTree(NULL),
|
---|
21 | mVspBspTree(NULL),
|
---|
22 | mViewCellsManager(NULL)
|
---|
23 | {
|
---|
24 | environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
25 |
|
---|
26 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
27 | renderer = NULL;
|
---|
28 |
|
---|
29 | environment->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
30 | environment->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes);
|
---|
31 | environment->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
32 | Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | Preprocessor::~Preprocessor()
|
---|
38 | {
|
---|
39 | Debug<<"Deleting view cells manager...\n";
|
---|
40 | DEL_PTR(mViewCellsManager);
|
---|
41 | Debug<<"done.\n";
|
---|
42 | Debug<<"Deleting bsp tree...\n";
|
---|
43 | DEL_PTR(mBspTree);
|
---|
44 | Debug<<"done.\n";
|
---|
45 | Debug<<"Deleting kd tree...\n";
|
---|
46 | DEL_PTR(mKdTree);
|
---|
47 | Debug<<"done.\n";
|
---|
48 | Debug<<"Deleting vspkd tree...\n";
|
---|
49 | DEL_PTR(mVspKdTree);
|
---|
50 | Debug<<"done.\n";
|
---|
51 | Debug<<"Deleting vspbsp tree...\n";
|
---|
52 | DEL_PTR(mVspBspTree);
|
---|
53 | Debug<<"done.\n";
|
---|
54 | }
|
---|
55 |
|
---|
56 | int
|
---|
57 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
58 | {
|
---|
59 | int pos = 0;
|
---|
60 |
|
---|
61 | while(1) {
|
---|
62 | int npos = (int)str.find(';', pos);
|
---|
63 |
|
---|
64 | if (npos < 0 || npos - pos < 1)
|
---|
65 | break;
|
---|
66 | filenames.push_back(string(str, pos, npos - pos));
|
---|
67 | pos = npos + 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
71 | return (int)filenames.size();
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool
|
---|
75 | Preprocessor::LoadScene(const string filename)
|
---|
76 | {
|
---|
77 | // use leaf nodes of the original spatial hiearrchy as occludees
|
---|
78 | mSceneGraph = new SceneGraph;
|
---|
79 |
|
---|
80 | Parser *parser;
|
---|
81 | vector<string> filenames;
|
---|
82 | int files = SplitFilenames(filename, filenames);
|
---|
83 | cout<<files<<endl;
|
---|
84 | bool result = false;
|
---|
85 | if (files == 1) {
|
---|
86 |
|
---|
87 | if (strstr(filename.c_str(), ".x3d"))
|
---|
88 | parser = new X3dParser;
|
---|
89 | else
|
---|
90 | parser = new UnigraphicsParser;
|
---|
91 |
|
---|
92 | cout<<filename<<endl;
|
---|
93 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
|
---|
94 |
|
---|
95 | delete parser;
|
---|
96 |
|
---|
97 | } else {
|
---|
98 | // root for different files
|
---|
99 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
100 | for (int i= 0; i < filenames.size(); i++) {
|
---|
101 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
102 | parser = new X3dParser;
|
---|
103 | else
|
---|
104 | parser = new UnigraphicsParser;
|
---|
105 |
|
---|
106 | SceneGraphNode *node;
|
---|
107 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
108 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
109 | // at least one file parsed
|
---|
110 | result = true;
|
---|
111 | }
|
---|
112 | delete parser;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | if (result) {
|
---|
118 |
|
---|
119 | mSceneGraph->AssignObjectIds();
|
---|
120 | int intersectables, faces;
|
---|
121 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
122 | cout<<filename<<" parsed successfully."<<endl;
|
---|
123 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
124 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
125 | mSceneGraph->CollectObjects(&mObjects);
|
---|
126 | mSceneGraph->mRoot->UpdateBox();
|
---|
127 |
|
---|
128 | /* Exporter *exporter = Exporter::GetExporter("testload.x3d");
|
---|
129 |
|
---|
130 | if (exporter)
|
---|
131 | {
|
---|
132 | exporter->ExportGeometry(mObjects);
|
---|
133 | delete exporter;
|
---|
134 | }*/
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | return result;
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool
|
---|
143 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
144 | {
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool
|
---|
149 | Preprocessor::BuildKdTree()
|
---|
150 | {
|
---|
151 | mKdTree = new KdTree;
|
---|
152 | // add mesh instances of the scene graph to the root of the tree
|
---|
153 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
154 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
155 |
|
---|
156 | mKdTree->Construct();
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void
|
---|
161 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
162 | {
|
---|
163 | s<<mKdTree->GetStatistics();
|
---|
164 | }
|
---|
165 |
|
---|
166 | void
|
---|
167 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
168 | {
|
---|
169 | s << mBspTree->GetStatistics();
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool
|
---|
173 | Preprocessor::Export( const string filename,
|
---|
174 | const bool scene,
|
---|
175 | const bool kdtree,
|
---|
176 | const bool bsptree
|
---|
177 | )
|
---|
178 | {
|
---|
179 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
180 |
|
---|
181 | if (exporter) {
|
---|
182 | if (scene)
|
---|
183 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
184 |
|
---|
185 | if (kdtree) {
|
---|
186 | exporter->SetWireframe();
|
---|
187 | exporter->ExportKdTree(*mKdTree);
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (bsptree) {
|
---|
191 | //exporter->SetWireframe();
|
---|
192 | exporter->ExportBspTree(*mBspTree);
|
---|
193 | }
|
---|
194 |
|
---|
195 | delete exporter;
|
---|
196 | return true;
|
---|
197 | }
|
---|
198 |
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | bool Preprocessor::PrepareViewCells()
|
---|
204 | {
|
---|
205 | //-- parse view cells construction method
|
---|
206 | environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
207 | char buf[100];
|
---|
208 |
|
---|
209 | if (mLoadViewCells)
|
---|
210 | {
|
---|
211 | environment->GetStringValue("ViewCells.filename", buf);
|
---|
212 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects);
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | //-- parse type of view cell container
|
---|
217 | char viewCellsStr[64];
|
---|
218 | environment->GetStringValue("ViewCells.type", viewCellsStr);
|
---|
219 | mViewCellsManager = CreateViewCellsManager(viewCellsStr);
|
---|
220 | }
|
---|
221 |
|
---|
222 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
223 |
|
---|
224 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
225 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
226 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
227 |
|
---|
228 | mRenderSimulator =
|
---|
229 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
230 |
|
---|
231 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
232 |
|
---|
233 |
|
---|
234 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
235 | {
|
---|
236 | // NOTE: render texture should be power of 2 and square
|
---|
237 | // renderer must be initialised
|
---|
238 | renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
239 | // renderer->makeCurrent();
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
244 |
|
---|
245 | return true;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
250 | {
|
---|
251 | if (strcmp(name, "kdTree") == 0)
|
---|
252 | {
|
---|
253 | mViewCellsManager = new KdViewCellsManager(mKdTree);
|
---|
254 | }
|
---|
255 | else if (strcmp(name, "bspTree") == 0)
|
---|
256 | {
|
---|
257 | Debug << "view cell type: Bsp" << endl;
|
---|
258 |
|
---|
259 | mBspTree = new BspTree();
|
---|
260 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
261 | }
|
---|
262 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
263 | {
|
---|
264 | Debug << "view cell type: VspBsp" << endl;
|
---|
265 |
|
---|
266 | mVspBspTree = new VspBspTree();
|
---|
267 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
268 | }
|
---|
269 | else if (strcmp(name, "vspKdTree") == 0)
|
---|
270 | {
|
---|
271 | mVspKdTree = new VspKdTree();
|
---|
272 |
|
---|
273 | mViewCellsManager = new VspKdViewCellsManager(mVspKdTree);
|
---|
274 | }
|
---|
275 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
276 | {
|
---|
277 | //TODO
|
---|
278 | mBspTree = new BspTree();
|
---|
279 |
|
---|
280 | Debug << "view cell type: Bsp" << endl;
|
---|
281 |
|
---|
282 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
283 | }
|
---|
284 | else
|
---|
285 | {
|
---|
286 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
287 | exit(1);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return mViewCellsManager;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | // use ascii format to store rays
|
---|
295 | #define USE_ASCII 0
|
---|
296 |
|
---|
297 |
|
---|
298 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
299 | {
|
---|
300 | return obj1->mId < obj2->mId;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
305 | ObjectContainer &objects) const
|
---|
306 | {
|
---|
307 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
308 | char fileName[100];
|
---|
309 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
310 |
|
---|
311 | Vector3 origin, termination;
|
---|
312 | // HACK: needed only for lower_bound algorithm to find the
|
---|
313 | // intersected objects
|
---|
314 | MeshInstance sObj(NULL);
|
---|
315 | MeshInstance tObj(NULL);
|
---|
316 |
|
---|
317 | #if USE_ASCII
|
---|
318 | ifstream samplesIn(fileName);
|
---|
319 | if (!samplesIn.is_open())
|
---|
320 | return false;
|
---|
321 |
|
---|
322 | string buf;
|
---|
323 | while (!(getline(samplesIn, buf)).eof())
|
---|
324 | {
|
---|
325 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
326 | &origin.x, &origin.y, &origin.z,
|
---|
327 | &termination.x, &termination.y, &termination.z,
|
---|
328 | &(sObj.mId), &(tObj.mId));
|
---|
329 |
|
---|
330 | Intersectable *sourceObj = NULL;
|
---|
331 | Intersectable *termObj = NULL;
|
---|
332 |
|
---|
333 | if (sObj.mId >= 0)
|
---|
334 | {
|
---|
335 | ObjectContainer::iterator oit =
|
---|
336 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
337 | sourceObj = *oit;
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (tObj.mId >= 0)
|
---|
341 | {
|
---|
342 | ObjectContainer::iterator oit =
|
---|
343 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
344 | termObj = *oit;
|
---|
345 | }
|
---|
346 |
|
---|
347 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
348 | }
|
---|
349 | #else
|
---|
350 | ifstream samplesIn(fileName, ios::binary);
|
---|
351 | if (!samplesIn.is_open())
|
---|
352 | return false;
|
---|
353 |
|
---|
354 | while (1)
|
---|
355 | {
|
---|
356 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
357 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
358 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
359 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
360 |
|
---|
361 | if (samplesIn.eof())
|
---|
362 | break;
|
---|
363 |
|
---|
364 | Intersectable *sourceObj = NULL;
|
---|
365 | Intersectable *termObj = NULL;
|
---|
366 |
|
---|
367 | if (sObj.mId >= 0)
|
---|
368 | {
|
---|
369 | ObjectContainer::iterator oit =
|
---|
370 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
371 | sourceObj = *oit;
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (tObj.mId >= 0)
|
---|
375 | {
|
---|
376 | ObjectContainer::iterator oit =
|
---|
377 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
378 | termObj = *oit;
|
---|
379 | }
|
---|
380 |
|
---|
381 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
382 | }
|
---|
383 |
|
---|
384 | #endif
|
---|
385 | samplesIn.close();
|
---|
386 |
|
---|
387 | return true;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
392 | {
|
---|
393 | char fileName[100];
|
---|
394 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
395 |
|
---|
396 |
|
---|
397 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
398 |
|
---|
399 | #if USE_ASCII
|
---|
400 | ofstream samplesOut(fileName);
|
---|
401 | if (!samplesOut.is_open())
|
---|
402 | return false;
|
---|
403 |
|
---|
404 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
405 | {
|
---|
406 | VssRay *ray = *it;
|
---|
407 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
408 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
409 |
|
---|
410 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
411 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
412 | << sourceid << " " << termid << "\n";
|
---|
413 | }
|
---|
414 | #else
|
---|
415 | ofstream samplesOut(fileName, ios::binary);
|
---|
416 | if (!samplesOut.is_open())
|
---|
417 | return false;
|
---|
418 |
|
---|
419 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
420 | {
|
---|
421 | VssRay *ray = *it;
|
---|
422 | Vector3 origin(ray->GetOrigin());
|
---|
423 | Vector3 termination(ray->GetTermination());
|
---|
424 |
|
---|
425 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
426 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
427 |
|
---|
428 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
429 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
430 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
431 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
432 | }
|
---|
433 | #endif
|
---|
434 | samplesOut.close();
|
---|
435 | return true;
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 |
|
---|
440 | bool
|
---|
441 | Preprocessor::GenerateRays(
|
---|
442 | const int number,
|
---|
443 | const int sampleType,
|
---|
444 | SimpleRayContainer &rays
|
---|
445 | )
|
---|
446 | {
|
---|
447 | Vector3 origin, direction;
|
---|
448 | int startSize = rays.size();
|
---|
449 | for (int i=0; rays.size() - startSize < number; i++) {
|
---|
450 | // now get the direction
|
---|
451 | switch (sampleType) {
|
---|
452 | case OBJECT_BASED_DISTRIBUTION: {
|
---|
453 | mViewCellsManager->GetViewPoint(origin);
|
---|
454 | Vector3 point;
|
---|
455 | Vector3 normal;
|
---|
456 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
457 | Intersectable *object = mObjects[i];
|
---|
458 | object->GetRandomSurfacePoint(point, normal);
|
---|
459 | direction = point - origin;
|
---|
460 | }
|
---|
461 | break;
|
---|
462 | case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
|
---|
463 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
464 | Intersectable *object = mObjects[i];
|
---|
465 | Vector3 normal;
|
---|
466 | object->GetRandomSurfacePoint(origin, normal);
|
---|
467 | direction = UniformRandomVector(normal);
|
---|
468 | origin += 0.1f*direction;
|
---|
469 | }
|
---|
470 | break;
|
---|
471 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
472 | mViewCellsManager->GetViewPoint(origin);
|
---|
473 | direction = UniformRandomVector();
|
---|
474 | break;
|
---|
475 | case DIRECTION_BOX_BASED_DISTRIBUTION: {
|
---|
476 | mViewCellsManager->GetViewPoint(origin);
|
---|
477 | float alpha = RandomValue(0.0f, 2*M_PI);
|
---|
478 | float beta = RandomValue(-M_PI/2, M_PI/2);
|
---|
479 | direction = VssRay::GetDirection(alpha, beta);
|
---|
480 | break;
|
---|
481 | }
|
---|
482 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
483 | mViewCellsManager->GetViewPoint(origin);
|
---|
484 | direction = mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
485 | break;
|
---|
486 | default:
|
---|
487 | // unsuported distribution type
|
---|
488 | return false;
|
---|
489 | }
|
---|
490 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
491 | float pdf = 1.0f;
|
---|
492 | float c = Magnitude(direction);
|
---|
493 | if (c > Limits::Small) {
|
---|
494 | direction*=1.0f/c;
|
---|
495 | rays.AddRay(SimpleRay(origin, direction, pdf));
|
---|
496 | }
|
---|
497 | }
|
---|
498 | return true;
|
---|
499 | }
|
---|