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

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

worked on the ray based subdivision. finished extracting polygons from rays

RevLine 
[162]1#include "SceneGraph.h"
2#include "Exporter.h"
3#include "UnigraphicsParser.h"
[170]4#include "X3dParser.h"
[65]5#include "Preprocessor.h"
[235]6#include "ViewCell.h"
7#include "Environment.h"
[65]8
[262]9Preprocessor::Preprocessor():
10mKdTree(NULL),
[297]11mBspTree(NULL),
[309]12mViewCellsType(BSP_VIEW_CELLS)
[262]13{
14}
[261]15
16Preprocessor::~Preprocessor()
[308]17{
18        DeleteViewCells();
19               
20        DEL_PTR(mBspTree);
21        DEL_PTR(mKdTree);
[261]22}
23
[162]24bool
[261]25Preprocessor::LoadViewCells(const string filename)
[162]26{
[312]27        X3dParser parser;
[313]28        int maxViewCells = 0;
[312]29        environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
[313]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;
[162]44}
[65]45
[310]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
[308]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
[162]89bool
[261]90Preprocessor::GenerateViewCells()
[162]91{
[263]92        // TODO
93        // HACK: derive view cells from the scene objects
94        ObjectContainer objects;
95
96        int maxViewCells = 0;
[313]97        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
[263]98
99        mSceneGraph->CollectObjects(&objects);
100        ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
101
102        return true;
[162]103}
[65]104
[162]105bool
106Preprocessor::LoadScene(const string filename)
107{
108  // use leaf nodes of the original spatial hiearrchy as occludees
109
110  mSceneGraph = new SceneGraph;
[170]111
112 
113  Parser *parser;
114
115  if (strstr(filename.c_str(), ".x3d"))
116    parser = new X3dParser;
117  else
118    parser = new UnigraphicsParser;
119
[162]120  bool result = parser->ParseFile(filename, &mSceneGraph->mRoot);
[65]121 
[261]122  delete parser;
123
[162]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();
[176]139  mSceneGraph->CollectObjects(&root->mObjects);
[65]140 
[162]141  mKdTree->Construct();
142  return true;
143}
[65]144
[263]145
[234]146bool
147Preprocessor::BuildBspTree()
148{
[262]149        DEL_PTR(mBspTree);
[321]150       
151        mBspTree = new BspTree(&mUnbounded);
[235]152
[262]153        ObjectContainer objects;
[327]154        RayContainer *rays = new RayContainer();
[308]155
[263]156        switch (BspTree::sConstructionMethod)
[235]157        {
[310]158        case BspTree::FROM_INPUT_VIEW_CELLS:
[261]159                mBspTree->Construct(mViewCells);
[235]160                break;
[310]161        case BspTree::FROM_SCENE_GEOMETRY:
[308]162                DeleteViewCells(); // we generate new view cells
[263]163                mSceneGraph->CollectObjects(&objects);
[261]164                mBspTree->Construct(objects, &mViewCells);
[235]165                break;
[310]166        case BspTree::FROM_RAYS:
[319]167                DeleteViewCells(); // we generate new view cells
168                mSceneGraph->CollectObjects(&objects);
[327]169                mBspTree->Construct(rays, &mViewCells);
[261]170                break;
[235]171        default:
[262]172                Debug << "Error: Method not available\n";
[235]173                break;
174        }
175        return true;
[234]176}
[162]177
[234]178
[310]179
[162]180void
181Preprocessor::KdTreeStatistics(ostream &s)
[65]182{
[162]183  s<<mKdTree->GetStatistics();
[65]184}
[162]185
[234]186void
187Preprocessor::BspTreeStatistics(ostream &s)
188{
[235]189        s << mBspTree->GetStatistics();
[234]190}
[162]191
192bool
193Preprocessor::Export( const string filename,
194                      const bool scene,
[242]195                      const bool kdtree,
196                          const bool bsptree
[162]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
[242]210        if (bsptree) {
[264]211                //exporter->SetWireframe();
[242]212                exporter->ExportBspTree(*mBspTree);
213        }
214
[162]215    delete exporter;
216    return true;
217  }
218
219  return false;
[310]220}
Note: See TracBrowser for help on using the repository browser.