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