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