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 |
|
---|
14 | Preprocessor *preprocessor;
|
---|
15 |
|
---|
16 | Preprocessor::Preprocessor():
|
---|
17 | mKdTree(NULL),
|
---|
18 | mBspTree(NULL),
|
---|
19 | mVspKdTree(NULL),
|
---|
20 | mVspBspTree(NULL),
|
---|
21 | mViewCellsManager(NULL)
|
---|
22 | {
|
---|
23 | environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | Preprocessor::~Preprocessor()
|
---|
28 | {
|
---|
29 | DEL_PTR(mViewCellsManager);
|
---|
30 | DEL_PTR(mBspTree);
|
---|
31 | DEL_PTR(mKdTree);
|
---|
32 | DEL_PTR(mVspKdTree);
|
---|
33 | DEL_PTR(mVspBspTree);
|
---|
34 | }
|
---|
35 |
|
---|
36 | int
|
---|
37 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
38 | {
|
---|
39 | int pos = 0;
|
---|
40 |
|
---|
41 | while(1) {
|
---|
42 | int npos = (int)str.find(';', pos);
|
---|
43 |
|
---|
44 | if (npos < 0 || npos - pos < 1)
|
---|
45 | break;
|
---|
46 | filenames.push_back(string(str, pos, npos - pos));
|
---|
47 | pos = npos + 1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
51 | return (int)filenames.size();
|
---|
52 | }
|
---|
53 |
|
---|
54 | bool
|
---|
55 | Preprocessor::LoadScene(const string filename)
|
---|
56 | {
|
---|
57 | // use leaf nodes of the original spatial hiearrchy as occludees
|
---|
58 |
|
---|
59 | mSceneGraph = new SceneGraph;
|
---|
60 |
|
---|
61 | Parser *parser;
|
---|
62 | vector<string> filenames;
|
---|
63 | int files = SplitFilenames(filename, filenames);
|
---|
64 | cout<<files<<endl;
|
---|
65 | bool result = false;
|
---|
66 | if (files == 1) {
|
---|
67 |
|
---|
68 | if (strstr(filename.c_str(), ".x3d"))
|
---|
69 | parser = new X3dParser;
|
---|
70 | else
|
---|
71 | parser = new UnigraphicsParser;
|
---|
72 |
|
---|
73 | cout<<filename<<endl;
|
---|
74 | result = parser->ParseFile(filename, &mSceneGraph->mRoot);
|
---|
75 |
|
---|
76 | delete parser;
|
---|
77 |
|
---|
78 | } else {
|
---|
79 | // root for different files
|
---|
80 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
81 | for (int i= 0; i < filenames.size(); i++) {
|
---|
82 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
83 | parser = new X3dParser;
|
---|
84 | else
|
---|
85 | parser = new UnigraphicsParser;
|
---|
86 |
|
---|
87 | SceneGraphNode *node;
|
---|
88 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
89 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
90 | // at least one file parsed
|
---|
91 | result = true;
|
---|
92 | }
|
---|
93 | delete parser;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | if (result) {
|
---|
99 |
|
---|
100 | mSceneGraph->AssignObjectIds();
|
---|
101 | int intersectables, faces;
|
---|
102 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
103 | cout<<filename<<" parsed successfully."<<endl;
|
---|
104 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
105 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
106 | mSceneGraph->CollectObjects(&mObjects);
|
---|
107 | mSceneGraph->mRoot->UpdateBox();
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 | bool
|
---|
115 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
116 | {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | bool
|
---|
121 | Preprocessor::BuildKdTree()
|
---|
122 | {
|
---|
123 | mKdTree = new KdTree;
|
---|
124 | // add mesh instances of the scene graph to the root of the tree
|
---|
125 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
126 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
127 |
|
---|
128 | mKdTree->Construct();
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void
|
---|
133 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
134 | {
|
---|
135 | s<<mKdTree->GetStatistics();
|
---|
136 | }
|
---|
137 |
|
---|
138 | void
|
---|
139 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
140 | {
|
---|
141 | s << mBspTree->GetStatistics();
|
---|
142 | }
|
---|
143 |
|
---|
144 | bool
|
---|
145 | Preprocessor::Export( const string filename,
|
---|
146 | const bool scene,
|
---|
147 | const bool kdtree,
|
---|
148 | const bool bsptree
|
---|
149 | )
|
---|
150 | {
|
---|
151 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
152 |
|
---|
153 | if (exporter) {
|
---|
154 | if (scene)
|
---|
155 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
156 |
|
---|
157 | if (kdtree) {
|
---|
158 | exporter->SetWireframe();
|
---|
159 | exporter->ExportKdTree(*mKdTree);
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (bsptree) {
|
---|
163 | //exporter->SetWireframe();
|
---|
164 | exporter->ExportBspTree(*mBspTree);
|
---|
165 | }
|
---|
166 |
|
---|
167 | delete exporter;
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool Preprocessor::PrepareViewCells()
|
---|
175 | {
|
---|
176 | //-- parse type of view cell container
|
---|
177 | char viewCellsStr[64];
|
---|
178 | environment->GetStringValue("ViewCells.type", viewCellsStr);
|
---|
179 |
|
---|
180 | int constructionSamples = 0;
|
---|
181 |
|
---|
182 | if (strcmp(viewCellsStr, "kdTree") == 0)
|
---|
183 | {
|
---|
184 | mViewCellsManager = new KdViewCellsManager(mKdTree);
|
---|
185 | }
|
---|
186 | else if (strcmp(viewCellsStr, "bspTree") == 0)
|
---|
187 | {
|
---|
188 | mBspTree = new BspTree();
|
---|
189 |
|
---|
190 | Debug << "view cell type: Bsp" << endl;
|
---|
191 |
|
---|
192 | environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
|
---|
193 | mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
|
---|
194 | }
|
---|
195 | else if (strcmp(viewCellsStr, "vspBspTree") == 0)
|
---|
196 | {
|
---|
197 | mVspBspTree = new VspBspTree();
|
---|
198 |
|
---|
199 | Debug << "view cell type: VspBsp" << endl;
|
---|
200 |
|
---|
201 | environment->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
|
---|
202 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree, constructionSamples);
|
---|
203 | }
|
---|
204 | else if (strcmp(viewCellsStr, "vspKdTree") == 0)
|
---|
205 | {
|
---|
206 | mVspKdTree = new VspKdTree();
|
---|
207 |
|
---|
208 | environment->GetIntValue("VspKdTree.Construction.samples", constructionSamples);
|
---|
209 | mViewCellsManager = new VspKdViewCellsManager(mVspKdTree, constructionSamples);
|
---|
210 | }
|
---|
211 | else if (strcmp(viewCellsStr, "sceneDependent") == 0)
|
---|
212 | {
|
---|
213 | //TODO
|
---|
214 | mBspTree = new BspTree();
|
---|
215 |
|
---|
216 | Debug << "view cell type: Bsp" << endl;
|
---|
217 | environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
|
---|
218 | mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
|
---|
219 | }
|
---|
220 | else
|
---|
221 | {
|
---|
222 | cerr<<"Wrong view cells type" << viewCellsStr << endl;
|
---|
223 | exit(1);
|
---|
224 | }
|
---|
225 |
|
---|
226 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
227 |
|
---|
228 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
229 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
230 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
231 |
|
---|
232 | mRenderSimulator =
|
---|
233 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
234 |
|
---|
235 | int postProcessSamples = 0;
|
---|
236 | int visSamples = 0;
|
---|
237 |
|
---|
238 | environment->GetIntValue("ViewCells.PostProcess.samples", postProcessSamples);
|
---|
239 | environment->GetIntValue("ViewCells.Visualization.samples", visSamples);
|
---|
240 |
|
---|
241 | mViewCellsManager->SetPostProcessSamples(postProcessSamples);
|
---|
242 | mViewCellsManager->SetVisualizationSamples(visSamples);
|
---|
243 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
244 |
|
---|
245 | //-- parse view cells construction method
|
---|
246 | bool loadViewCells = false;
|
---|
247 | environment->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
|
---|
248 |
|
---|
249 | //-- load view cells from file if requested
|
---|
250 | if (loadViewCells)
|
---|
251 | {
|
---|
252 | char buff[100];
|
---|
253 | environment->GetStringValue("ViewCells.filename", buff);
|
---|
254 | string vcFilename(buff);
|
---|
255 | mViewCellsManager->LoadViewCells(vcFilename);
|
---|
256 | }
|
---|
257 |
|
---|
258 | return true;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | // use ascii format to store rays
|
---|
263 | #define USE_ASCII 0
|
---|
264 |
|
---|
265 |
|
---|
266 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
267 | {
|
---|
268 | return obj1->mId < obj2->mId;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
273 | ObjectContainer &objects) const
|
---|
274 | {
|
---|
275 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
276 | char fileName[100];
|
---|
277 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
278 |
|
---|
279 | Vector3 origin, termination;
|
---|
280 | // HACK: needed only for lower_bound algorithm to find the
|
---|
281 | // intersected objects
|
---|
282 | MeshInstance sObj(NULL);
|
---|
283 | MeshInstance tObj(NULL);
|
---|
284 |
|
---|
285 | #if USE_ASCII
|
---|
286 | ifstream samplesIn(fileName, ios::binary);
|
---|
287 | if (!samplesIn.is_open())
|
---|
288 | return false;
|
---|
289 |
|
---|
290 | string buf;
|
---|
291 | while (!(getline(samplesIn, buf)).eof())
|
---|
292 | {
|
---|
293 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
294 | &origin.x, &origin.y, &origin.z,
|
---|
295 | &termination.x, &termination.y, &termination.z,
|
---|
296 | &(sObj.mId), &(tObj.mId));
|
---|
297 |
|
---|
298 | Intersectable *sourceObj = NULL;
|
---|
299 | Intersectable *termObj = NULL;
|
---|
300 |
|
---|
301 | if (sObj.mId >= 0)
|
---|
302 | {
|
---|
303 | ObjectContainer::iterator oit =
|
---|
304 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
305 | sourceObj = *oit;
|
---|
306 | }
|
---|
307 |
|
---|
308 | if (tObj.mId >= 0)
|
---|
309 | {
|
---|
310 | ObjectContainer::iterator oit =
|
---|
311 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
312 | termObj = *oit;
|
---|
313 | }
|
---|
314 |
|
---|
315 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
316 | }
|
---|
317 | #else
|
---|
318 | ifstream samplesIn(fileName, ios::binary);
|
---|
319 | if (!samplesIn.is_open())
|
---|
320 | return false;
|
---|
321 |
|
---|
322 | while (1)
|
---|
323 | {
|
---|
324 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
325 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
326 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
327 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
328 |
|
---|
329 | if (samplesIn.eof())
|
---|
330 | break;
|
---|
331 |
|
---|
332 | Intersectable *sourceObj = NULL;
|
---|
333 | Intersectable *termObj = NULL;
|
---|
334 |
|
---|
335 | if (sObj.mId >= 0)
|
---|
336 | {
|
---|
337 | ObjectContainer::iterator oit =
|
---|
338 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
339 | sourceObj = *oit;
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (tObj.mId >= 0)
|
---|
343 | {
|
---|
344 | ObjectContainer::iterator oit =
|
---|
345 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
346 | termObj = *oit;
|
---|
347 | }
|
---|
348 |
|
---|
349 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
350 | }
|
---|
351 |
|
---|
352 | #endif
|
---|
353 | samplesIn.close();
|
---|
354 |
|
---|
355 | return true;
|
---|
356 | }
|
---|
357 |
|
---|
358 | bool Preprocessor::WriteSamples(const VssRayContainer &samples) const
|
---|
359 | {
|
---|
360 | char fileName[100];
|
---|
361 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
362 |
|
---|
363 |
|
---|
364 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
365 |
|
---|
366 | #if USE_ASCII
|
---|
367 | ofstream samplesOut(fileName);
|
---|
368 | if (!samplesOut.is_open())
|
---|
369 | return false;
|
---|
370 |
|
---|
371 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
372 | {
|
---|
373 | VssRay *ray = *it;
|
---|
374 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
375 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
376 |
|
---|
377 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
378 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
379 | << sourceid << " " << termid << "\n";
|
---|
380 | }
|
---|
381 | #else
|
---|
382 | ofstream samplesOut(fileName, ios::binary);
|
---|
383 | if (!samplesOut.is_open())
|
---|
384 | return false;
|
---|
385 |
|
---|
386 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
387 | {
|
---|
388 | VssRay *ray = *it;
|
---|
389 | Vector3 origin(ray->GetOrigin());
|
---|
390 | Vector3 termination(ray->GetTermination());
|
---|
391 |
|
---|
392 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
393 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
394 |
|
---|
395 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
396 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
397 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
398 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
399 | }
|
---|
400 | #endif
|
---|
401 | samplesOut.close();
|
---|
402 | return true;
|
---|
403 | }
|
---|