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

Revision 422, 5.3 KB checked in by mattausch, 19 years ago (diff)

worded on vspkdtree

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