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

Revision 520, 10.3 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[372]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"
[439]8#include "ViewCellsManager.h"
[445]9#include "ViewCellBsp.h"
10#include "VspBspTree.h"
11#include "VspKdTree.h"
[469]12#include "RenderSimulator.h"
[496]13#include "GlRenderer.h"
[372]14
[492]15Preprocessor *preprocessor;
16
[372]17Preprocessor::Preprocessor():
18mKdTree(NULL),
[409]19mBspTree(NULL),
[422]20mVspKdTree(NULL),
[445]21mVspBspTree(NULL),
[439]22mViewCellsManager(NULL)
[308]23{
[492]24  environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
[496]25  // renderer will be constructed when the scene graph and viewcell manager will be known
26  renderer = NULL;
27 
[372]28}
29
30
31Preprocessor::~Preprocessor()
32{
[496]33  DEL_PTR(mViewCellsManager);
34  DEL_PTR(mBspTree);
35  DEL_PTR(mKdTree);
36  DEL_PTR(mVspKdTree);
37  DEL_PTR(mVspBspTree);
[372]38}
39
[387]40int
[419]41SplitFilenames(const string str, vector<string> &filenames)
[387]42{
43        int pos = 0;
44
45        while(1) {
[469]46                int npos = (int)str.find(';', pos);
[387]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));
[440]55        return (int)filenames.size();
[387]56}
57
[372]58bool
59Preprocessor::LoadScene(const string filename)
60{
[508]61        // use leaf nodes of the original spatial hiearrchy as occludees
62        mSceneGraph = new SceneGraph;
[372]63 
[508]64        Parser *parser;
[387]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;
[372]75
[387]76                cout<<filename<<endl;
77                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
[372]78
[387]79                delete parser;
[372]80
[387]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        }
[372]99       
100
[387]101        if (result) {
[492]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();
[387]111        }
[372]112       
[492]113       
114        return result;
[372]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,
[492]149                                          const bool scene,
150                                          const bool kdtree,
151                                          const bool bsptree
152                                          )
[372]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}
[429]176
[508]177
[463]178bool Preprocessor::PrepareViewCells()
179{
[439]180        //-- parse type of view cell container
181        char viewCellsStr[64];
182        environment->GetStringValue("ViewCells.type", viewCellsStr);
183
[445]184        int constructionSamples = 0;
[469]185       
[439]186        if (strcmp(viewCellsStr, "kdTree") == 0)
187        {
[440]188                mViewCellsManager = new KdViewCellsManager(mKdTree);
[439]189        }
[448]190        else if (strcmp(viewCellsStr, "bspTree") == 0)
[439]191        {
[441]192                mBspTree = new BspTree();
193
[448]194                Debug << "view cell type: Bsp" << endl;
195
[445]196                environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
197                mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
[439]198        }
[448]199        else if (strcmp(viewCellsStr, "vspBspTree") == 0)
[439]200        {
[445]201                mVspBspTree = new VspBspTree();
202
[448]203                Debug << "view cell type: VspBsp" << endl;
204
[445]205                environment->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
206                mViewCellsManager = new VspBspViewCellsManager(mVspBspTree, constructionSamples);
[439]207        }
[445]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        }
[439]215        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
216        {
217                //TODO
[445]218                mBspTree = new BspTree();
[440]219
[448]220                Debug << "view cell type: Bsp" << endl;
[445]221                environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
222                mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
[439]223        }
224        else
225        {
226                cerr<<"Wrong view cells type" << viewCellsStr << endl;
227                exit(1);
228        }
229
[473]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       
[440]239        int postProcessSamples = 0;
240        int visSamples = 0;
241
[478]242        environment->GetIntValue("ViewCells.PostProcess.samples", postProcessSamples);
[445]243        environment->GetIntValue("ViewCells.Visualization.samples", visSamples);
[440]244
245        mViewCellsManager->SetPostProcessSamples(postProcessSamples);
246        mViewCellsManager->SetVisualizationSamples(visSamples);
[480]247        mViewCellsManager->SetRenderer(mRenderSimulator);
[440]248
[463]249        //-- parse view cells construction method
[518]250        environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
[520]251        char buf[100];
252        if (mLoadViewCells)
253        {
254                environment->GetStringValue("ViewCells.filename", buf);
255                mViewCellsFilename = buf;
256        }
[496]257        if (mUseGlRenderer)
258          renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager);
259       
[463]260        return true;
[490]261}
262
263
[491]264// use ascii format to store rays
265#define USE_ASCII 0
266
267
[490]268inline bool ilt(Intersectable *obj1, Intersectable *obj2)
269{
270        return obj1->mId < obj2->mId;
271}
272
273
274bool 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       
[491]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);
[490]286
[491]287#if USE_ASCII
288        ifstream samplesIn(fileName, ios::binary);
[490]289        if (!samplesIn.is_open())
290                return false;
291
292        string buf;
293        while (!(getline(samplesIn, buf)).eof())
294        {
[491]295                sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
[490]296                           &origin.x, &origin.y, &origin.z,
[491]297                           &termination.x, &termination.y, &termination.z,
298                           &(sObj.mId), &(tObj.mId));
[490]299               
[491]300                Intersectable *sourceObj = NULL;
301                Intersectable *termObj = NULL;
302               
303                if (sObj.mId >= 0)
[490]304                {
305                        ObjectContainer::iterator oit =
[491]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                }
[490]316
[491]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;
[490]342                }
[491]343               
344                if (tObj.mId >= 0)
[490]345                {
[491]346                        ObjectContainer::iterator oit =
347                                lower_bound(objects.begin(), objects.end(), &tObj, ilt);
348                        termObj = *oit;
[490]349                }
[491]350
351                samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
[490]352        }
[491]353
354#endif
[490]355        samplesIn.close();
356
357        return true;
358}
359
[508]360
361bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
[490]362{
[491]363        char fileName[100];
364        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
365       
[490]366
367        VssRayContainer::const_iterator it, it_end = samples.end();
368       
[491]369#if USE_ASCII
370        ofstream samplesOut(fileName);
[490]371        if (!samplesOut.is_open())
372                return false;
373
374        for (it = samples.begin(); it != it_end; ++ it)
375        {
376                VssRay *ray = *it;
[491]377                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
378                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
379
[490]380                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
381                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
[491]382                                   << sourceid << " " << termid << "\n";
[490]383        }
[491]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
[490]404        samplesOut.close();
405        return true;
406}
Note: See TracBrowser for help on using the repository browser.