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

Revision 473, 6.1 KB checked in by mattausch, 19 years ago (diff)

worked on new features,
removed Random Bug (took only 32000 values),
removed bug when choosing new candidates (totally wrong)
introduced new candidate plane method
implemented priority queue for vsp bsp

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