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

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