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