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