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