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

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