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

Revision 496, 10.4 KB checked in by bittner, 19 years ago (diff)

glrenderer split into widget and buffer, buffer moved to the same thread as the preprocessor

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
63  mSceneGraph = new SceneGraph;
64 
65  Parser *parser;
66        vector<string> filenames;
67        int files = SplitFilenames(filename, filenames);
68        cout<<files<<endl;
69        bool result = false;
70        if (files == 1) {
71               
72                if (strstr(filename.c_str(), ".x3d"))
73                        parser = new X3dParser;
74                else
75                        parser = new UnigraphicsParser;
76
77                cout<<filename<<endl;
78                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
79
80                delete parser;
81
82        } else {
83                // root for different files
84                mSceneGraph->mRoot = new SceneGraphNode;
85                for (int i= 0; i < filenames.size(); i++) {
86                        if (strstr(filenames[i].c_str(), ".x3d"))
87                                parser = new X3dParser;
88                        else
89                                parser = new UnigraphicsParser;
90                       
91                        SceneGraphNode *node;
92                        if (parser->ParseFile(filenames[i], &node)) {
93                                mSceneGraph->mRoot->mChildren.push_back(node);
94                                // at least one file parsed
95                                result = true;
96                        }
97                        delete parser;
98                }
99        }
100       
101
102        if (result) {
103         
104          mSceneGraph->AssignObjectIds();
105          int intersectables, faces;
106          mSceneGraph->GetStatistics(intersectables, faces);
107          cout<<filename<<" parsed successfully."<<endl;
108          cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
109          cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
110          mSceneGraph->CollectObjects(&mObjects);
111          mSceneGraph->mRoot->UpdateBox();
112        }
113       
114       
115        return result;
116}
117
118bool
119Preprocessor::ExportPreprocessedData(const string filename)
120{
121  return false;
122}
123
124bool
125Preprocessor::BuildKdTree()
126{
127  mKdTree = new KdTree;
128  // add mesh instances of the scene graph to the root of the tree
129  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
130  mSceneGraph->CollectObjects(&root->mObjects);
131 
132  mKdTree->Construct();
133  return true;
134}
135
136void
137Preprocessor::KdTreeStatistics(ostream &s)
138{
139  s<<mKdTree->GetStatistics();
140}
141
142void
143Preprocessor::BspTreeStatistics(ostream &s)
144{
145        s << mBspTree->GetStatistics();
146}
147
148bool
149Preprocessor::Export( const string filename,
150                                          const bool scene,
151                                          const bool kdtree,
152                                          const bool bsptree
153                                          )
154{
155  Exporter *exporter = Exporter::GetExporter(filename);
156       
157  if (exporter) {
158    if (scene)
159      exporter->ExportScene(mSceneGraph->mRoot);
160
161    if (kdtree) {
162      exporter->SetWireframe();
163      exporter->ExportKdTree(*mKdTree);
164    }
165
166        if (bsptree) {
167                //exporter->SetWireframe();
168                exporter->ExportBspTree(*mBspTree);
169        }
170
171    delete exporter;
172    return true;
173  }
174
175  return false;
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        //-- parse view cells construction method
250        bool loadViewCells = false;
251        environment->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
252
253        //-- load view cells from file if requested
254        if (loadViewCells)
255        {
256                char buff[100];
257                environment->GetStringValue("ViewCells.filename", buff);
258                string vcFilename(buff);
259                mViewCellsManager->LoadViewCells(vcFilename);
260        }
261
262
263        if (mUseGlRenderer)
264          renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager);
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
366bool Preprocessor::WriteSamples(const VssRayContainer &samples) const
367{
368        char fileName[100];
369        environment->GetStringValue("Preprocessor.samplesFilename", fileName);
370       
371
372        VssRayContainer::const_iterator it, it_end = samples.end();
373       
374#if USE_ASCII
375        ofstream samplesOut(fileName);
376        if (!samplesOut.is_open())
377                return false;
378
379        for (it = samples.begin(); it != it_end; ++ it)
380        {
381                VssRay *ray = *it;
382                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
383                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;       
384
385                samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
386                                   << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
387                                   << sourceid << " " << termid << "\n";
388        }
389#else
390        ofstream samplesOut(fileName, ios::binary);
391        if (!samplesOut.is_open())
392                return false;
393
394        for (it = samples.begin(); it != it_end; ++ it)
395        {       
396                VssRay *ray = *it;
397                Vector3 origin(ray->GetOrigin());
398                Vector3 termination(ray->GetTermination());
399               
400                int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;               
401                int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;               
402
403                samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
404                samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
405                samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
406                samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
407    }
408#endif
409        samplesOut.close();
410        return true;
411}
Note: See TracBrowser for help on using the repository browser.