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

Revision 409, 5.5 KB checked in by mattausch, 19 years ago (diff)

worked on kd view space partitioning structure
worked on render simulation

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        if (ViewCell::sHierarchy = ViewCell::BSP)
90                mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
91        else // KD view cells
92                mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
93
94        return mRenderSimulator;
95}
96
97void Preprocessor::DeleteViewCells()
98{
99        for (int i = 0; i < (int)mViewCells.size(); ++ i)
100        {
101                Mesh *mesh = mViewCells[i]->GetMesh();
102                DEL_PTR(mesh);
103        }
104        CLEAR_CONTAINER(mViewCells);
105}
106
107bool
108Preprocessor::GenerateViewCells()
109{
110        // TODO
111        // HACK: derive view cells from the scene objects
112        ObjectContainer objects;
113
114        int maxViewCells = 0;
115        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
116
117        mSceneGraph->CollectObjects(&objects);
118        ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
119
120        return true;
121}
122
123int
124SplitFilenames(const string str,
125                                                         vector<string> &filenames)
126{
127        int pos = 0;
128
129        while(1) {
130                int npos = str.find(';', pos);
131               
132                if (npos < 0 || npos - pos < 1)
133                        break;
134                filenames.push_back(string(str, pos, npos - pos));
135                pos = npos + 1;
136        }
137       
138        filenames.push_back(string(str, pos, str.size() - pos));
139        return filenames.size();
140}
141
142bool
143Preprocessor::LoadScene(const string filename)
144{
145  // use leaf nodes of the original spatial hiearrchy as occludees
146
147  mSceneGraph = new SceneGraph;
148
149 
150  Parser *parser;
151        vector<string> filenames;
152        int files = SplitFilenames(filename, filenames);
153        cout<<files<<endl;
154        bool result = false;
155        if (files == 1) {
156               
157                if (strstr(filename.c_str(), ".x3d"))
158                        parser = new X3dParser;
159                else
160                        parser = new UnigraphicsParser;
161
162                cout<<filename<<endl;
163                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
164
165                delete parser;
166
167        } else {
168                // root for different files
169                mSceneGraph->mRoot = new SceneGraphNode;
170                for (int i= 0; i < filenames.size(); i++) {
171                        if (strstr(filenames[i].c_str(), ".x3d"))
172                                parser = new X3dParser;
173                        else
174                                parser = new UnigraphicsParser;
175                       
176                        SceneGraphNode *node;
177                        if (parser->ParseFile(filenames[i], &node)) {
178                                mSceneGraph->mRoot->mChildren.push_back(node);
179                                // at least one file parsed
180                                result = true;
181                        }
182                        delete parser;
183                }
184        }
185       
186
187        if (result) {
188                mSceneGraph->AssignObjectIds();
189                int intersectables, faces;
190                mSceneGraph->GetStatistics(intersectables, faces);
191                cout<<filename<<" parsed successfully."<<endl;
192                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
193                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
194        }
195
196       
197  return result;
198}
199
200bool
201Preprocessor::ExportPreprocessedData(const string filename)
202{
203  return false;
204}
205
206bool
207Preprocessor::BuildKdTree()
208{
209  mKdTree = new KdTree;
210  // add mesh instances of the scene graph to the root of the tree
211  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
212  mSceneGraph->CollectObjects(&root->mObjects);
213 
214  mKdTree->Construct();
215  return true;
216}
217
218void
219Preprocessor::KdTreeStatistics(ostream &s)
220{
221  s<<mKdTree->GetStatistics();
222}
223
224void
225Preprocessor::BspTreeStatistics(ostream &s)
226{
227        s << mBspTree->GetStatistics();
228}
229
230bool
231Preprocessor::Export( const string filename,
232                                                                                        const bool scene,
233                                                                                        const bool kdtree,
234                                                                                        const bool bsptree
235                                                                                        )
236{
237  Exporter *exporter = Exporter::GetExporter(filename);
238       
239  if (exporter) {
240    if (scene)
241      exporter->ExportScene(mSceneGraph->mRoot);
242
243    if (kdtree) {
244      exporter->SetWireframe();
245      exporter->ExportKdTree(*mKdTree);
246    }
247
248        if (bsptree) {
249                //exporter->SetWireframe();
250                exporter->ExportBspTree(*mBspTree);
251        }
252
253    delete exporter;
254    return true;
255  }
256
257  return false;
258}
Note: See TracBrowser for help on using the repository browser.