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