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

Revision 321, 4.2 KB checked in by mattausch, 19 years ago (diff)
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
9Preprocessor::Preprocessor():
10mKdTree(NULL),
11mBspTree(NULL),
12mViewCellsType(BSP_VIEW_CELLS)
13{
14}
15
16Preprocessor::~Preprocessor()
17{
18        DeleteViewCells();
19               
20        DEL_PTR(mBspTree);
21        DEL_PTR(mKdTree);
22}
23
24bool
25Preprocessor::LoadViewCells(const string filename)
26{
27        X3dParser parser;
28        int maxViewCells = 0;
29        environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
30        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
31
32        bool loaded = parser.ParseFile(filename, mViewCells);
33
34        if (maxViewCells > 0)
35        {
36                while (mViewCells.size() > maxViewCells)
37                {
38                        ViewCell *vc = mViewCells.back();
39                        DEL_PTR(vc);
40                        mViewCells.pop_back();
41                }
42        }
43        return loaded;
44}
45
46bool Preprocessor::ParseViewCellsOptions()
47{
48        // parse type of view cells
49        char viewCellsStr[64];
50        environment->GetStringValue("ViewCells.hierarchyType", viewCellsStr);
51
52        int vcType = BSP_VIEW_CELLS;
53 
54        if (strcmp(viewCellsStr, "bspTree") == 0)
55                vcType = BSP_VIEW_CELLS;
56        else if (strcmp(viewCellsStr, "kdTree") == 0)
57                vcType = KD_VIEW_CELLS;
58        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
59                vcType = SCENE_DEPENDENT;
60        else
61        {
62                cerr<<"Wrong view cells type" << viewCellsStr << endl;
63                exit(1);
64        }
65
66        // decide about view cell subdivision type used for preprocessing
67        switch (vcType)
68        {
69        case BSP_VIEW_CELLS:
70        case KD_VIEW_CELLS:
71                mViewCellsType = vcType;
72                break;
73        case SCENE_DEPENDENT:
74                mViewCellsType = BSP_VIEW_CELLS; // TODO
75                break;
76        }
77
78        return true;
79}
80
81void Preprocessor::DeleteViewCells()
82{
83        for (int i = 0; i < mViewCells.size(); ++ i)
84                delete mViewCells[i]->GetMesh();
85
86        CLEAR_CONTAINER(mViewCells);
87}
88
89bool
90Preprocessor::GenerateViewCells()
91{
92        // TODO
93        // HACK: derive view cells from the scene objects
94        ObjectContainer objects;
95
96        int maxViewCells = 0;
97        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
98
99        mSceneGraph->CollectObjects(&objects);
100        ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
101
102        return true;
103}
104
105bool
106Preprocessor::LoadScene(const string filename)
107{
108  // use leaf nodes of the original spatial hiearrchy as occludees
109
110  mSceneGraph = new SceneGraph;
111
112 
113  Parser *parser;
114
115  if (strstr(filename.c_str(), ".x3d"))
116    parser = new X3dParser;
117  else
118    parser = new UnigraphicsParser;
119
120  bool result = parser->ParseFile(filename, &mSceneGraph->mRoot);
121 
122  delete parser;
123
124  return result;
125}
126
127bool
128Preprocessor::ExportPreprocessedData(const string filename)
129{
130  return false;
131}
132
133bool
134Preprocessor::BuildKdTree()
135{
136  mKdTree = new KdTree;
137  // add mesh instances of the scene graph to the root of the tree
138  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
139  mSceneGraph->CollectObjects(&root->mObjects);
140 
141  mKdTree->Construct();
142  return true;
143}
144
145
146bool
147Preprocessor::BuildBspTree()
148{
149        DEL_PTR(mBspTree);
150       
151        mBspTree = new BspTree(&mUnbounded);
152
153        ObjectContainer objects;
154        RayContainer rays;
155
156        switch (BspTree::sConstructionMethod)
157        {
158        case BspTree::FROM_INPUT_VIEW_CELLS:
159                mBspTree->Construct(mViewCells);
160                break;
161        case BspTree::FROM_SCENE_GEOMETRY:
162                DeleteViewCells(); // we generate new view cells
163                mSceneGraph->CollectObjects(&objects);
164                mBspTree->Construct(objects, &mViewCells);
165                break;
166        case BspTree::FROM_RAYS:
167                DeleteViewCells(); // we generate new view cells
168                mSceneGraph->CollectObjects(&objects);
169                mBspTree->Construct(objects, rays, &mViewCells);
170                break;
171        default:
172                Debug << "Error: Method not available\n";
173                break;
174        }
175        return true;
176}
177
178
179
180void
181Preprocessor::KdTreeStatistics(ostream &s)
182{
183  s<<mKdTree->GetStatistics();
184}
185
186void
187Preprocessor::BspTreeStatistics(ostream &s)
188{
189        s << mBspTree->GetStatistics();
190}
191
192bool
193Preprocessor::Export( const string filename,
194                      const bool scene,
195                      const bool kdtree,
196                          const bool bsptree
197                      )
198{
199  Exporter *exporter = Exporter::GetExporter(filename);
200
201  if (exporter) {
202    if (scene)
203      exporter->ExportScene(mSceneGraph->mRoot);
204
205    if (kdtree) {
206      exporter->SetWireframe();
207      exporter->ExportKdTree(*mKdTree);
208    }
209
210        if (bsptree) {
211                //exporter->SetWireframe();
212                exporter->ExportBspTree(*mBspTree);
213        }
214
215    delete exporter;
216    return true;
217  }
218
219  return false;
220}
Note: See TracBrowser for help on using the repository browser.