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

Revision 411, 5.7 KB checked in by mattausch, 19 years ago (diff)

worked on view space partition kd tree

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 "RenderSimulator.h"
9
10Preprocessor::Preprocessor():
11mKdTree(NULL),
12mBspTree(NULL),
13mRenderSimulator(NULL)
14{
15}
16
17
18Preprocessor::~Preprocessor()
19{
20        DeleteViewCells();
21               
22        DEL_PTR(mBspTree);
23        DEL_PTR(mKdTree);
24
25        DEL_PTR(mRenderSimulator);
26}
27
28bool
29Preprocessor::LoadViewCells(const string filename)
30{
31        X3dParser parser;
32        int maxViewCells = 0;
33        environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
34        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
35
36        bool loaded = parser.ParseFile(filename, mViewCells);
37
38        if (maxViewCells > 0)
39        {
40                while (mViewCells.size() > maxViewCells)
41                {
42                        ViewCell *vc = mViewCells.back();
43                        DEL_PTR(vc);
44                        mViewCells.pop_back();
45                }
46        }
47        return loaded;
48}
49
50bool
51Preprocessor::ParseViewCellsOptions()
52{
53        // parse type of view cells
54        char viewCellsStr[64];
55        environment->GetStringValue("ViewCells.hierarchy", viewCellsStr);
56
57        if (strcmp(viewCellsStr, "bspTree") == 0)
58        {
59                ViewCell::sHierarchy = ViewCell::BSP;
60        }
61        else if (strcmp(viewCellsStr, "kdTree") == 0)
62        {
63                ViewCell::sHierarchy = ViewCell::KD;
64        }
65        else if (strcmp(viewCellsStr, "vspTree") == 0)
66        {
67                ViewCell::sHierarchy = ViewCell::VSP;
68        }
69        else if (strcmp(viewCellsStr, "sceneDependent") == 0)
70        {
71                //TODO
72        }
73        else
74        {
75                cerr<<"Wrong view cells type" << viewCellsStr << endl;
76                exit(1);
77        }
78
79        return true;
80}
81
82RenderSimulator *Preprocessor::GetRenderSimulator()
83{
84        if (mRenderSimulator)
85                return mRenderSimulator;
86
87    float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
88
89        environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
90        environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
91        environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
92
93        Debug << "render simulator using render cost=" << objRenderCost << ", vc overhead=" << vcOverhead << ", move speed=" << moveSpeed << endl;
94        if (ViewCell::sHierarchy = ViewCell::BSP)
95                mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
96        else // KD view cells
97                mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
98
99        return mRenderSimulator;
100}
101
102void Preprocessor::DeleteViewCells()
103{
104        for (int i = 0; i < (int)mViewCells.size(); ++ i)
105        {
106                Mesh *mesh = mViewCells[i]->GetMesh();
107                DEL_PTR(mesh);
108        }
109        CLEAR_CONTAINER(mViewCells);
110}
111
112bool
113Preprocessor::GenerateViewCells()
114{
115        // TODO
116        // HACK: derive view cells from the scene objects
117        ObjectContainer objects;
118
119        int maxViewCells = 0;
120        environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
121
122        mSceneGraph->CollectObjects(&objects);
123        ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells);
124
125        return true;
126}
127
128int
129SplitFilenames(const string str,
130                                                         vector<string> &filenames)
131{
132        int pos = 0;
133
134        while(1) {
135                int npos = str.find(';', pos);
136               
137                if (npos < 0 || npos - pos < 1)
138                        break;
139                filenames.push_back(string(str, pos, npos - pos));
140                pos = npos + 1;
141        }
142       
143        filenames.push_back(string(str, pos, str.size() - pos));
144        return filenames.size();
145}
146
147bool
148Preprocessor::LoadScene(const string filename)
149{
150  // use leaf nodes of the original spatial hiearrchy as occludees
151
152  mSceneGraph = new SceneGraph;
153
154 
155  Parser *parser;
156        vector<string> filenames;
157        int files = SplitFilenames(filename, filenames);
158        cout<<files<<endl;
159        bool result = false;
160        if (files == 1) {
161               
162                if (strstr(filename.c_str(), ".x3d"))
163                        parser = new X3dParser;
164                else
165                        parser = new UnigraphicsParser;
166
167                cout<<filename<<endl;
168                result = parser->ParseFile(filename, &mSceneGraph->mRoot);
169
170                delete parser;
171
172        } else {
173                // root for different files
174                mSceneGraph->mRoot = new SceneGraphNode;
175                for (int i= 0; i < filenames.size(); i++) {
176                        if (strstr(filenames[i].c_str(), ".x3d"))
177                                parser = new X3dParser;
178                        else
179                                parser = new UnigraphicsParser;
180                       
181                        SceneGraphNode *node;
182                        if (parser->ParseFile(filenames[i], &node)) {
183                                mSceneGraph->mRoot->mChildren.push_back(node);
184                                // at least one file parsed
185                                result = true;
186                        }
187                        delete parser;
188                }
189        }
190       
191
192        if (result) {
193                mSceneGraph->AssignObjectIds();
194                int intersectables, faces;
195                mSceneGraph->GetStatistics(intersectables, faces);
196                cout<<filename<<" parsed successfully."<<endl;
197                cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
198                cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
199        }
200
201       
202  return result;
203}
204
205bool
206Preprocessor::ExportPreprocessedData(const string filename)
207{
208  return false;
209}
210
211bool
212Preprocessor::BuildKdTree()
213{
214  mKdTree = new KdTree;
215  // add mesh instances of the scene graph to the root of the tree
216  KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
217  mSceneGraph->CollectObjects(&root->mObjects);
218 
219  mKdTree->Construct();
220  return true;
221}
222
223void
224Preprocessor::KdTreeStatistics(ostream &s)
225{
226  s<<mKdTree->GetStatistics();
227}
228
229void
230Preprocessor::BspTreeStatistics(ostream &s)
231{
232        s << mBspTree->GetStatistics();
233}
234
235bool
236Preprocessor::Export( const string filename,
237                                                                                        const bool scene,
238                                                                                        const bool kdtree,
239                                                                                        const bool bsptree
240                                                                                        )
241{
242  Exporter *exporter = Exporter::GetExporter(filename);
243       
244  if (exporter) {
245    if (scene)
246      exporter->ExportScene(mSceneGraph->mRoot);
247
248    if (kdtree) {
249      exporter->SetWireframe();
250      exporter->ExportKdTree(*mKdTree);
251    }
252
253        if (bsptree) {
254                //exporter->SetWireframe();
255                exporter->ExportBspTree(*mBspTree);
256        }
257
258    delete exporter;
259    return true;
260  }
261
262  return false;
263}
Note: See TracBrowser for help on using the repository browser.