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

Revision 410, 5.6 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 "RenderSimulator.h"
9
10Preprocessor::Preprocessor():
11mKdTree(NULL),
12mBspTree(NULL),
13mRenderSimulator(NULL)
14{
15}
16
17
18Preprocessor::~Preprocessor()
19{
20        DeleteViewCells();
21               
22        DEL_PTR(mBspTree);
23        DEL_PTR(mKdTree);
24
25        DEL_PTR(mRenderSimulator);
26}
27
28bool
29Preprocessor::LoadViewCells(const string filename)
30{
31        X3dParser parser;
32        int maxViewCells = 0;
33        environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
34        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
35
36        bool loaded = parser.ParseFile(filename, mViewCells);
37
38        if (maxViewCells > 0)
39        {
40                while (mViewCells.size() > maxViewCells)
41                {
42                        ViewCell *vc = mViewCells.back();
43                        DEL_PTR(vc);
44                        mViewCells.pop_back();
45                }
46        }
47        return loaded;
48}
49
50bool
51Preprocessor::ParseViewCellsOptions()
52{
53        // parse type of view cells
54        char viewCellsStr[64];
55        environment->GetStringValue("ViewCells.hierarchy", viewCellsStr);
56
57        if (strcmp(viewCellsStr, "bspTree") == 0)
58        {
59                ViewCell::sHierarchy = ViewCell::BSP;
60        }
61        else if (strcmp(viewCellsStr, "kdTree") == 0)
62        {
63                ViewCell::sHierarchy = ViewCell::KD;
64        }
65        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
66        {
67                //TODO
68        }
69        else
70        {
71                cerr<<"Wrong view cells type" << viewCellsStr << endl;
72                exit(1);
73        }
74
75        return true;
76}
77
78RenderSimulator *Preprocessor::GetRenderSimulator()
79{
80        if (mRenderSimulator)
81                return mRenderSimulator;
82
83    float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
84
85        environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
86        environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
87        environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
88
89        Debug << "render simulator using render cost=" << objRenderCost << ", vc overhead=" << vcOverhead << ", move speed=" << moveSpeed << endl;
90        if (ViewCell::sHierarchy = ViewCell::BSP)
91                mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
92        else // KD view cells
93                mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
94
95        return mRenderSimulator;
96}
97
98void Preprocessor::DeleteViewCells()
99{
100        for (int i = 0; i < (int)mViewCells.size(); ++ i)
101        {
102                Mesh *mesh = mViewCells[i]->GetMesh();
103                DEL_PTR(mesh);
104        }
105        CLEAR_CONTAINER(mViewCells);
106}
107
108bool
109Preprocessor::GenerateViewCells()
110{
111        // TODO
112        // HACK: derive view cells from the scene objects
113        ObjectContainer objects;
114
115        int maxViewCells = 0;
116        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
117
118        mSceneGraph->CollectObjects(&objects);
119        ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
120
121        return true;
122}
123
124int
125SplitFilenames(const string str,
126                                                         vector<string> &filenames)
127{
128        int pos = 0;
129
130        while(1) {
131                int npos = str.find(';', pos);
132               
133                if (npos < 0 || npos - pos < 1)
134                        break;
135                filenames.push_back(string(str, pos, npos - pos));
136                pos = npos + 1;
137        }
138       
139        filenames.push_back(string(str, pos, str.size() - pos));
140        return filenames.size();
141}
142
143bool
144Preprocessor::LoadScene(const string filename)
145{
146  // use leaf nodes of the original spatial hiearrchy as occludees
147
148  mSceneGraph = new SceneGraph;
149
150 
151  Parser *parser;
152        vector<string> filenames;
153        int files = SplitFilenames(filename, filenames);
154        cout<<files<<endl;
155        bool result = false;
156        if (files == 1) {
157               
158                if (strstr(filename.c_str(), ".x3d"))
159                        parser = new X3dParser;
160                else
161                        parser = new UnigraphicsParser;
162
163                cout<<filename<<endl;
164                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
165
166                delete parser;
167
168        } else {
169                // root for different files
170                mSceneGraph->mRoot = new SceneGraphNode;
171                for (int i= 0; i < filenames.size(); i++) {
172                        if (strstr(filenames[i].c_str(), ".x3d"))
173                                parser = new X3dParser;
174                        else
175                                parser = new UnigraphicsParser;
176                       
177                        SceneGraphNode *node;
178                        if (parser->ParseFile(filenames[i], &node)) {
179                                mSceneGraph->mRoot->mChildren.push_back(node);
180                                // at least one file parsed
181                                result = true;
182                        }
183                        delete parser;
184                }
185        }
186       
187
188        if (result) {
189                mSceneGraph->AssignObjectIds();
190                int intersectables, faces;
191                mSceneGraph->GetStatistics(intersectables, faces);
192                cout<<filename<<" parsed successfully."<<endl;
193                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
194                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
195        }
196
197       
198  return result;
199}
200
201bool
202Preprocessor::ExportPreprocessedData(const string filename)
203{
204  return false;
205}
206
207bool
208Preprocessor::BuildKdTree()
209{
210  mKdTree = new KdTree;
211  // add mesh instances of the scene graph to the root of the tree
212  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
213  mSceneGraph->CollectObjects(&root->mObjects);
214 
215  mKdTree->Construct();
216  return true;
217}
218
219void
220Preprocessor::KdTreeStatistics(ostream &s)
221{
222  s<<mKdTree->GetStatistics();
223}
224
225void
226Preprocessor::BspTreeStatistics(ostream &s)
227{
228        s << mBspTree->GetStatistics();
229}
230
231bool
232Preprocessor::Export( const string filename,
233                                                                                        const bool scene,
234                                                                                        const bool kdtree,
235                                                                                        const bool bsptree
236                                                                                        )
237{
238  Exporter *exporter = Exporter::GetExporter(filename);
239       
240  if (exporter) {
241    if (scene)
242      exporter->ExportScene(mSceneGraph->mRoot);
243
244    if (kdtree) {
245      exporter->SetWireframe();
246      exporter->ExportKdTree(*mKdTree);
247    }
248
249        if (bsptree) {
250                //exporter->SetWireframe();
251                exporter->ExportBspTree(*mBspTree);
252        }
253
254    delete exporter;
255    return true;
256  }
257
258  return false;
259}
Note: See TracBrowser for help on using the repository browser.