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

Revision 445, 5.6 KB checked in by mattausch, 19 years ago (diff)

Added VspBspTree? functionality:
This is a view space partitioning specialised BSPtree.
There are no polygon splits, but we split the sample rays.
The candidates for the next split plane are evaluated only
by checking the sampled visibility information.
The polygons are employed merely as candidates for the next split planes.

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        if (strcmp(viewCellsStr, "bspTree") == 0)
181        {
182                mBspTree = new BspTree();
183
184                environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
185                mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
186        }
187        if (strcmp(viewCellsStr, "vspBspTree") == 0)
188        {
189                mVspBspTree = new VspBspTree();
190
191                environment->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
192                mViewCellsManager = new VspBspViewCellsManager(mVspBspTree, constructionSamples);
193        }
194        else if (strcmp(viewCellsStr, "vspKdTree") == 0)
195        {
196                mVspKdTree = new VspKdTree();           
197       
198                environment->GetIntValue("VspKdTree.Construction.samples", constructionSamples);
199        mViewCellsManager = new VspKdViewCellsManager(mVspKdTree, constructionSamples);
200        }
201        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
202        {
203                //TODO
204                mBspTree = new BspTree();
205
206                environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
207                mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
208        }
209        else
210        {
211                cerr<<"Wrong view cells type" << viewCellsStr << endl;
212                exit(1);
213        }
214
215        int postProcessSamples = 0;
216        int visSamples = 0;
217
218        environment->GetIntValue("ViewCells.PostProcessing.samples", postProcessSamples);
219        environment->GetIntValue("ViewCells.Visualization.samples", visSamples);
220
221        mViewCellsManager->SetPostProcessSamples(postProcessSamples);
222        mViewCellsManager->SetVisualizationSamples(visSamples);
223
224        //Debug << "Visualization samples: " << mViewCellsManager->GetVisualizationSamples() << endl;
225
226        //-- parse view cells construction method
227
228        bool loadViewCells = false;
229        environment->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
230
231        //-- load view cells from file if requested
232        if (loadViewCells)
233        {
234                char buff[100];
235                environment->GetStringValue("ViewCells.filename", buff);
236                string vcFilename(buff);
237                mViewCellsManager->LoadViewCells(vcFilename);
238        }
239
240        return true;
241}
Note: See TracBrowser for help on using the repository browser.