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

Revision 313, 4.2 KB checked in by mattausch, 19 years ago (diff)

bsp viewcells now work

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