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