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

Revision 448, 5.7 KB checked in by mattausch, 19 years ago (diff)

fixed bug in VspBspTree?
view cells in VssPreprocessor?
bounding rays for vspkdtree

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