source: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp @ 534

Revision 534, 10.4 KB checked in by bittner, 18 years ago (diff)

vss preprocessor void space identification fixed - by default it is off

Line 
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
15Preprocessor *preprocessor;
16
17Preprocessor::Preprocessor():
18mKdTree(NULL),
19mBspTree(NULL),
20mVspKdTree(NULL),
21mVspBspTree(NULL),
22mViewCellsManager(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
31Preprocessor::~Preprocessor()
32{
33  DEL_PTR(mViewCellsManager);
34  DEL_PTR(mBspTree);
35  DEL_PTR(mKdTree);
36  DEL_PTR(mVspKdTree);
37  DEL_PTR(mVspBspTree);
38}
39
40int
41SplitFilenames(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
58bool
59Preprocessor::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
117bool
118Preprocessor::ExportPreprocessedData(const string filename)
119{
120  return false;
121}
122
123bool
124Preprocessor::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
135void
136Preprocessor::KdTreeStatistics(ostream &s)
137{
138  s<<mKdTree->GetStatistics();
139}
140
141void
142Preprocessor::BspTreeStatistics(ostream &s)
143{
144        s << mBspTree->GetStatistics();
145}
146
147bool
148Preprocessor::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
178bool 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       
250        //-- parse view cells construction method
251        environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
252        char buf[100];
253        if (mLoadViewCells)
254        {
255                environment->GetStringValue("ViewCells.filename", buf);
256                mViewCellsFilename = buf;
257        }
258        if (mUseGlRenderer)
259          renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
260
261
262        environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
263       
264
265
266        return true;
267}
268
269
270// use ascii format to store rays
271#define USE_ASCII 0
272
273
274inline bool ilt(Intersectable *obj1, Intersectable *obj2)
275{
276        return obj1->mId < obj2->mId;
277}
278
279
280bool Preprocessor::LoadSamples(VssRayContainer &samples,
281                                                           ObjectContainer &objects) const
282{
283        std::stable_sort(objects.begin(), objects.end(), ilt);
284        char fileName[100];
285        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
286       
287    Vector3 origin, termination;
288        // HACK: needed only for lower_bound algorithm to find the
289        // intersected objects
290        MeshInstance sObj(NULL);
291        MeshInstance tObj(NULL);
292
293#if USE_ASCII
294        ifstream samplesIn(fileName, ios::binary);
295        if (!samplesIn.is_open())
296                return false;
297
298        string buf;
299        while (!(getline(samplesIn, buf)).eof())
300        {
301                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
302                           &origin.x, &origin.y, &origin.z,
303                           &termination.x, &termination.y, &termination.z,
304                           &(sObj.mId), &(tObj.mId));
305               
306                Intersectable *sourceObj = NULL;
307                Intersectable *termObj = NULL;
308               
309                if (sObj.mId >= 0)
310                {
311                        ObjectContainer::iterator oit =
312                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
313                        sourceObj = *oit;
314                }
315               
316                if (tObj.mId >= 0)
317                {
318                        ObjectContainer::iterator oit =
319                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
320                        termObj = *oit;
321                }
322
323                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
324        }
325#else
326        ifstream samplesIn(fileName, ios::binary);
327        if (!samplesIn.is_open())
328                return false;
329
330        while (1)
331        {
332                 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
333                 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
334                 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
335                 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
336               
337                 if (samplesIn.eof())
338                        break;
339
340                Intersectable *sourceObj = NULL;
341                Intersectable *termObj = NULL;
342               
343                if (sObj.mId >= 0)
344                {
345                        ObjectContainer::iterator oit =
346                                lower_bound(objects.begin(), objects.end(), &sObj, ilt);
347                        sourceObj = *oit;
348                }
349               
350                if (tObj.mId >= 0)
351                {
352                        ObjectContainer::iterator oit =
353                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
354                        termObj = *oit;
355                }
356
357                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
358        }
359
360#endif
361        samplesIn.close();
362
363        return true;
364}
365
366
367bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
368{
369        char fileName[100];
370        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
371       
372
373        VssRayContainer::const_iterator it, it_end = samples.end();
374       
375#if USE_ASCII
376        ofstream samplesOut(fileName);
377        if (!samplesOut.is_open())
378                return false;
379
380        for (it = samples.begin(); it != it_end; ++ it)
381        {
382                VssRay *ray = *it;
383                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
384                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
385
386                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
387                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
388                                   << sourceid << " " << termid << "\n";
389        }
390#else
391        ofstream samplesOut(fileName, ios::binary);
392        if (!samplesOut.is_open())
393                return false;
394
395        for (it = samples.begin(); it != it_end; ++ it)
396        {       
397                VssRay *ray = *it;
398                Vector3 origin(ray->GetOrigin());
399                Vector3 termination(ray->GetTermination());
400               
401                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
402                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
403
404                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
405                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
406                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
407                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
408    }
409#endif
410        samplesOut.close();
411        return true;
412}
Note: See TracBrowser for help on using the repository browser.