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

Revision 262, 3.9 KB checked in by mattausch, 19 years ago (diff)

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