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 "RenderSimulator.h"
|
---|
12 | #include "GlRenderer.h"
|
---|
13 | #include "PlyParser.h"
|
---|
14 | #include "SamplingStrategy.h"
|
---|
15 | #include "VspTree.h"
|
---|
16 | #include "OspTree.h"
|
---|
17 | #include "ObjParser.h"
|
---|
18 | #include "BvHierarchy.h"
|
---|
19 | #include "HierarchyManager.h"
|
---|
20 | #include "VssRay.h"
|
---|
21 | #include "IntelRayCaster.h"
|
---|
22 | #include "InternalRayCaster.h"
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | #define DEBUG_RAYCAST 0
|
---|
27 | #define SHOW_RAYCAST_TIMING 1
|
---|
28 |
|
---|
29 |
|
---|
30 | namespace GtpVisibilityPreprocessor {
|
---|
31 |
|
---|
32 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
33 |
|
---|
34 | Preprocessor *preprocessor = NULL;
|
---|
35 |
|
---|
36 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
37 | static void AddGeometry(SceneGraph *scene)
|
---|
38 | {
|
---|
39 | scene->GetRoot()->UpdateBox();
|
---|
40 |
|
---|
41 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
42 |
|
---|
43 | int n = 200;
|
---|
44 |
|
---|
45 | if (0)
|
---|
46 | {
|
---|
47 | // form grid of boxes
|
---|
48 | for (int i = 0; i < n; ++ i)
|
---|
49 | {
|
---|
50 | for (int j = 0; j < n; ++ j)
|
---|
51 | {
|
---|
52 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
53 |
|
---|
54 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
55 |
|
---|
56 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
57 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
58 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
59 |
|
---|
60 | mesh->Preprocess();
|
---|
61 |
|
---|
62 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
63 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | for (int i = 0; i < n; ++ i)
|
---|
68 | {
|
---|
69 | for (int j = 0; j < n; ++ j)
|
---|
70 | {
|
---|
71 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
72 |
|
---|
73 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
74 |
|
---|
75 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
76 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
77 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
78 |
|
---|
79 | mesh->Preprocess();
|
---|
80 |
|
---|
81 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
82 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | for (int i = 0; i < n; ++ i)
|
---|
87 | {
|
---|
88 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
89 |
|
---|
90 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
91 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
92 |
|
---|
93 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
94 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
95 |
|
---|
96 | mesh->Preprocess();
|
---|
97 |
|
---|
98 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
99 | scene->GetRoot()->mGeometry.push_back(mi);
|
---|
100 | }
|
---|
101 |
|
---|
102 | scene->GetRoot()->UpdateBox();
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (1)
|
---|
106 | {
|
---|
107 | // plane separating view space regions
|
---|
108 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
109 |
|
---|
110 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
111 |
|
---|
112 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
113 | Mesh *planeMesh = new Mesh();
|
---|
114 |
|
---|
115 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
116 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
117 |
|
---|
118 | planeMesh->Preprocess();
|
---|
119 |
|
---|
120 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
121 | scene->GetRoot()->mGeometry.push_back(planeMi);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | Preprocessor::Preprocessor():
|
---|
127 | mKdTree(NULL),
|
---|
128 | mBspTree(NULL),
|
---|
129 | mVspBspTree(NULL),
|
---|
130 | mViewCellsManager(NULL),
|
---|
131 | mRenderSimulator(NULL),
|
---|
132 | mPass(0),
|
---|
133 | mSceneGraph(NULL),
|
---|
134 | mRayCaster(NULL),
|
---|
135 | mStopComputation(false),
|
---|
136 | mThread(NULL)
|
---|
137 | {
|
---|
138 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
139 |
|
---|
140 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
141 | renderer = NULL;
|
---|
142 |
|
---|
143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
144 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
|
---|
145 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
146 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
147 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
148 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
149 |
|
---|
150 | char buffer[256];
|
---|
151 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
152 | mVisibilityFileName = buffer;
|
---|
153 |
|
---|
154 | Environment::GetSingleton()->GetStringValue("Preprocessor.stats", buffer);
|
---|
155 | mStats.open(buffer);
|
---|
156 |
|
---|
157 |
|
---|
158 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
|
---|
159 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
160 | mApplyVisibilitySpatialFilter );
|
---|
161 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
162 |
|
---|
163 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportObj", mExportObj);
|
---|
164 |
|
---|
165 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
166 |
|
---|
167 | Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.rays", mExportRays);
|
---|
168 | Environment::GetSingleton()->GetIntValue("Preprocessor.Export.numRays", mExportNumRays);
|
---|
169 |
|
---|
170 | Debug << "******* Preprocessor Options **********" << endl;
|
---|
171 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
172 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
173 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
174 | Debug << "export obj: " << mExportObj << endl;
|
---|
175 | Debug << "use view space box: " << mUseViewSpaceBox << endl;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | Preprocessor::~Preprocessor()
|
---|
180 | {
|
---|
181 | cout << "cleaning up" << endl;
|
---|
182 |
|
---|
183 | cout << "Deleting view cells manager ... \n";
|
---|
184 | DEL_PTR(mViewCellsManager);
|
---|
185 | cout << "done.\n";
|
---|
186 |
|
---|
187 | cout << "Deleting bsp tree ... \n";
|
---|
188 | DEL_PTR(mBspTree);
|
---|
189 | cout << "done.\n";
|
---|
190 |
|
---|
191 | cout << "Deleting kd tree ...\n";
|
---|
192 | DEL_PTR(mKdTree);
|
---|
193 | cout << "done.\n";
|
---|
194 |
|
---|
195 | cout << "Deleting vspbsp tree ... \n";
|
---|
196 | DEL_PTR(mVspBspTree);
|
---|
197 | cout << "done.\n";
|
---|
198 |
|
---|
199 | cout << "Deleting scene graph ... \n";
|
---|
200 | DEL_PTR(mSceneGraph);
|
---|
201 | cout << "done.\n";
|
---|
202 |
|
---|
203 | DEL_PTR(mRenderSimulator);
|
---|
204 | DEL_PTR(renderer);
|
---|
205 | DEL_PTR(mRayCaster);
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | GlRendererBuffer *Preprocessor::GetRenderer()
|
---|
210 | {
|
---|
211 | return renderer;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
216 | {
|
---|
217 | int pos = 0;
|
---|
218 |
|
---|
219 | while(1) {
|
---|
220 | int npos = (int)str.find(';', pos);
|
---|
221 |
|
---|
222 | if (npos < 0 || npos - pos < 1)
|
---|
223 | break;
|
---|
224 | filenames.push_back(string(str, pos, npos - pos));
|
---|
225 | pos = npos + 1;
|
---|
226 | }
|
---|
227 |
|
---|
228 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
229 | return (int)filenames.size();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | void Preprocessor::SetThread(PreprocessorThread *t)
|
---|
234 | {
|
---|
235 | mThread = t;
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | PreprocessorThread *Preprocessor::GetThread() const
|
---|
240 | {
|
---|
241 | return mThread;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | bool Preprocessor::LoadBinaryObj(const string filename,
|
---|
246 | SceneGraphNode *root,
|
---|
247 | vector<FaceParentInfo> *parents)
|
---|
248 | {
|
---|
249 | //ifstream samplesIn(filename, ios::binary);
|
---|
250 | igzstream samplesIn(filename.c_str());
|
---|
251 |
|
---|
252 | if (!samplesIn.is_open())
|
---|
253 | return false;
|
---|
254 |
|
---|
255 | cout << "binary obj dump available, loading " << filename.c_str() << endl;
|
---|
256 | // table associating indices with vectors
|
---|
257 | map<int, Vector3> hashTable;
|
---|
258 |
|
---|
259 | // table for vertices
|
---|
260 | VertexContainer vertices;
|
---|
261 | FaceContainer faces;
|
---|
262 |
|
---|
263 | if (parents)
|
---|
264 | cout << "using face parents" << endl;
|
---|
265 | else
|
---|
266 | cout << "not using face parents" << endl;
|
---|
267 |
|
---|
268 | while (1)
|
---|
269 | {
|
---|
270 | Triangle3 tri;
|
---|
271 |
|
---|
272 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
273 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
274 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
275 |
|
---|
276 | // end of file reached
|
---|
277 | if (samplesIn.eof())
|
---|
278 | break;
|
---|
279 |
|
---|
280 | TriangleIntersectable *obj = new TriangleIntersectable(tri);
|
---|
281 | root->mGeometry.push_back(obj);
|
---|
282 |
|
---|
283 | // matt: we don't really need to keep an additional data structure
|
---|
284 | // if working with triangles => remove this
|
---|
285 | if (parents)
|
---|
286 | {
|
---|
287 | FaceParentInfo info(obj, 0);
|
---|
288 | parents->push_back(info);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | bool Preprocessor::ExportBinaryObj(const string filename, SceneGraphNode *root)
|
---|
297 | {
|
---|
298 | //ifstream samplesIn(filename, ios::binary);
|
---|
299 | ogzstream samplesOut(filename.c_str());
|
---|
300 | if (!samplesOut.is_open())
|
---|
301 | return false;
|
---|
302 |
|
---|
303 | ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
|
---|
304 |
|
---|
305 | for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
|
---|
306 | {
|
---|
307 | Intersectable *obj = *oit;
|
---|
308 |
|
---|
309 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
310 | {
|
---|
311 | Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
312 |
|
---|
313 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
314 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
315 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
316 | }
|
---|
317 | else
|
---|
318 | {
|
---|
319 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | return true;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | bool Preprocessor::ExportObj(const string filename, const ObjectContainer &objects)
|
---|
328 | {
|
---|
329 | ofstream samplesOut(filename.c_str());
|
---|
330 |
|
---|
331 | if (!samplesOut.is_open())
|
---|
332 | return false;
|
---|
333 |
|
---|
334 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
335 |
|
---|
336 | AxisAlignedBox3 bbox = mSceneGraph->GetBox(); bbox.Enlarge(30.0);
|
---|
337 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
338 | {
|
---|
339 | Intersectable *obj = *oit;
|
---|
340 |
|
---|
341 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
342 | {
|
---|
343 | Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
344 | //if (bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))
|
---|
345 | //{
|
---|
346 | samplesOut << "v " << tri.mVertices[0].x << " " << tri.mVertices[0].y << " " << tri.mVertices[0].z << endl;
|
---|
347 | samplesOut << "v " << tri.mVertices[1].x << " " << tri.mVertices[1].y << " " << tri.mVertices[1].z << endl;
|
---|
348 | samplesOut << "v " << tri.mVertices[2].x << " " << tri.mVertices[2].y << " " << tri.mVertices[2].z << endl;
|
---|
349 | //}
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | // write faces
|
---|
358 | int i = 1;
|
---|
359 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
360 | {
|
---|
361 | Intersectable *obj = *oit;
|
---|
362 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
363 | {
|
---|
364 | //Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
365 | //if (bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))
|
---|
366 | //{
|
---|
367 | Triangle3 tri = dynamic_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
368 | samplesOut << "f " << i << " " << i + 1 << " " << i + 2 << endl;
|
---|
369 | i += 3;
|
---|
370 | //}
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | return true;
|
---|
379 |
|
---|
380 | }
|
---|
381 |
|
---|
382 | static string ReplaceSuffix(string filename, string a, string b)
|
---|
383 | {
|
---|
384 | string result = filename;
|
---|
385 |
|
---|
386 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
387 | if (pos == filename.size() - a.size()) {
|
---|
388 | result.replace(pos, a.size(), b);
|
---|
389 | }
|
---|
390 | return result;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | Intersectable *Preprocessor::GetParentObject(const int index) const
|
---|
395 | {
|
---|
396 | if (index == -1)
|
---|
397 | return NULL;
|
---|
398 |
|
---|
399 | if (!mFaceParents.empty())
|
---|
400 | {
|
---|
401 | if (index >= (int)mFaceParents.size())
|
---|
402 | {
|
---|
403 | cerr<<"Warning: triangle index out of range! "<<index<<endl;
|
---|
404 | return NULL;
|
---|
405 | }
|
---|
406 | else
|
---|
407 | {
|
---|
408 | return mFaceParents[index].mObject;
|
---|
409 | }
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 | if (index >= (int)mObjects.size())
|
---|
414 | {
|
---|
415 | cerr<<"Warning: triangle index out of range! "<<index<<endl;
|
---|
416 | return NULL;
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | return mObjects[index];
|
---|
421 | }
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | Vector3 Preprocessor::GetParentNormal(const int index) const
|
---|
427 | {
|
---|
428 | if (!mFaceParents.empty())
|
---|
429 | {
|
---|
430 | return mFaceParents[index].mObject->GetNormal(mFaceParents[index].mFaceIndex);
|
---|
431 | }
|
---|
432 | else
|
---|
433 | {
|
---|
434 | return mObjects[index]->GetNormal(0);
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | bool
|
---|
440 | Preprocessor::LoadScene(const string filename)
|
---|
441 | {
|
---|
442 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
443 | mSceneGraph = new SceneGraph;
|
---|
444 |
|
---|
445 | Parser *parser;
|
---|
446 | vector<string> filenames;
|
---|
447 | const int files = SplitFilenames(filename, filenames);
|
---|
448 | cout << "number of input files: " << files << endl;
|
---|
449 | bool result = false;
|
---|
450 | bool isObj = false;
|
---|
451 |
|
---|
452 | // root for different files
|
---|
453 | mSceneGraph->SetRoot(new SceneGraphNode());
|
---|
454 |
|
---|
455 | // intel ray caster can only trace triangles
|
---|
456 | int rayCastMethod;
|
---|
457 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
458 | vector<FaceParentInfo> *fi =
|
---|
459 | ((rayCastMethod == RayCaster::INTEL_RAYCASTER) && mLoadMeshes) ?
|
---|
460 | &mFaceParents : NULL;
|
---|
461 |
|
---|
462 | if (files == 1)
|
---|
463 | {
|
---|
464 | if (strstr(filename.c_str(), ".x3d"))
|
---|
465 | {
|
---|
466 | parser = new X3dParser;
|
---|
467 |
|
---|
468 | result = parser->ParseFile(filename,
|
---|
469 | mSceneGraph->GetRoot(),
|
---|
470 | mLoadMeshes,
|
---|
471 | fi);
|
---|
472 | delete parser;
|
---|
473 | }
|
---|
474 | else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
475 | {
|
---|
476 | parser = new PlyParser;
|
---|
477 |
|
---|
478 | result = parser->ParseFile(filename,
|
---|
479 | mSceneGraph->GetRoot(),
|
---|
480 | mLoadMeshes,
|
---|
481 | fi);
|
---|
482 | delete parser;
|
---|
483 | }
|
---|
484 | else if (strstr(filename.c_str(), ".obj"))
|
---|
485 | {
|
---|
486 | isObj = true;
|
---|
487 |
|
---|
488 | // hack: load binary dump
|
---|
489 | string binFile = ReplaceSuffix(filename, ".obj", ".bin");
|
---|
490 |
|
---|
491 | if (!mLoadMeshes)
|
---|
492 | {
|
---|
493 | result = LoadBinaryObj(binFile, mSceneGraph->GetRoot(), fi);
|
---|
494 | }
|
---|
495 |
|
---|
496 | if (!result)
|
---|
497 | {
|
---|
498 | cout << "no binary dump available or loading full meshes, parsing file" << endl;
|
---|
499 | parser = new ObjParser;
|
---|
500 |
|
---|
501 | result = parser->ParseFile(filename,
|
---|
502 | mSceneGraph->GetRoot(),
|
---|
503 | mLoadMeshes,
|
---|
504 | fi);
|
---|
505 |
|
---|
506 | // only works for triangles
|
---|
507 | if (!mLoadMeshes)
|
---|
508 | {
|
---|
509 | cout << "exporting binary obj to " << binFile << "... " << endl;
|
---|
510 | ExportBinaryObj(binFile, mSceneGraph->GetRoot());
|
---|
511 | cout << "finished" << endl;
|
---|
512 | }
|
---|
513 |
|
---|
514 | delete parser;
|
---|
515 | }
|
---|
516 | else if (0)
|
---|
517 | {
|
---|
518 | ExportBinaryObj("../data/test.bin", mSceneGraph->GetRoot());
|
---|
519 | }
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | parser = new UnigraphicsParser;
|
---|
524 | result = parser->ParseFile(filename,
|
---|
525 | mSceneGraph->GetRoot(),
|
---|
526 | mLoadMeshes,
|
---|
527 | fi);
|
---|
528 | delete parser;
|
---|
529 | }
|
---|
530 |
|
---|
531 | cout << filename << endl;
|
---|
532 | }
|
---|
533 | else
|
---|
534 | {
|
---|
535 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
536 |
|
---|
537 | for (fit = filenames.begin(); fit != fit_end; ++ fit)
|
---|
538 | {
|
---|
539 | const string filename = *fit;
|
---|
540 |
|
---|
541 | cout << "parsing file " << filename.c_str() << endl;
|
---|
542 | if (strstr(filename.c_str(), ".x3d"))
|
---|
543 | parser = new X3dParser;
|
---|
544 | else
|
---|
545 | parser = new UnigraphicsParser;
|
---|
546 |
|
---|
547 | SceneGraphNode *node = new SceneGraphNode();
|
---|
548 | const bool success =
|
---|
549 | parser->ParseFile(filename, node, mLoadMeshes, fi);
|
---|
550 |
|
---|
551 | if (success)
|
---|
552 | {
|
---|
553 | mSceneGraph->GetRoot()->mChildren.push_back(node);
|
---|
554 | result = true; // at least one file parsed
|
---|
555 | }
|
---|
556 |
|
---|
557 | // temporare hack
|
---|
558 | //if (!strstr(filename.c_str(), "plane")) mSceneGraph->GetRoot()->UpdateBox();
|
---|
559 |
|
---|
560 | delete parser;
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | if (result)
|
---|
565 | {
|
---|
566 | // HACK
|
---|
567 | if (ADDITIONAL_GEOMETRY_HACK)
|
---|
568 | AddGeometry(mSceneGraph);
|
---|
569 |
|
---|
570 | mSceneGraph->AssignObjectIds();
|
---|
571 |
|
---|
572 | int intersectables, faces;
|
---|
573 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
574 |
|
---|
575 | cout<<filename<<" parsed successfully."<<endl;
|
---|
576 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
577 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
578 |
|
---|
579 | mObjects.reserve(intersectables);
|
---|
580 | mSceneGraph->CollectObjects(&mObjects);
|
---|
581 |
|
---|
582 | // temp hack
|
---|
583 | //ExportObj("cropped_vienna.obj", mObjects);
|
---|
584 | mSceneGraph->GetRoot()->UpdateBox();
|
---|
585 |
|
---|
586 | cout << "finished loading" << endl;
|
---|
587 | }
|
---|
588 |
|
---|
589 | return result;
|
---|
590 | }
|
---|
591 |
|
---|
592 | bool
|
---|
593 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
594 | {
|
---|
595 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
596 | return true;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | bool
|
---|
601 | Preprocessor::PostProcessVisibility()
|
---|
602 | {
|
---|
603 |
|
---|
604 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
605 | cout<<"Applying visibility filter ...";
|
---|
606 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
607 |
|
---|
608 | if (!mViewCellsManager)
|
---|
609 | return false;
|
---|
610 |
|
---|
611 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
612 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
613 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
614 | cout << "done." << endl;
|
---|
615 | }
|
---|
616 |
|
---|
617 | // export the preprocessed information to a file
|
---|
618 | if (mExportVisibility)
|
---|
619 | {
|
---|
620 | ExportPreprocessedData(mVisibilityFileName);
|
---|
621 | }
|
---|
622 |
|
---|
623 | return true;
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 | bool
|
---|
628 | Preprocessor::BuildKdTree()
|
---|
629 | {
|
---|
630 | mKdTree = new KdTree;
|
---|
631 |
|
---|
632 | // add mesh instances of the scene graph to the root of the tree
|
---|
633 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
634 |
|
---|
635 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
636 |
|
---|
637 | const long startTime = GetTime();
|
---|
638 | cout << "building kd tree ... " << endl;
|
---|
639 |
|
---|
640 | mKdTree->Construct();
|
---|
641 |
|
---|
642 | cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
643 | << " secs " << endl;
|
---|
644 |
|
---|
645 | return true;
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | void
|
---|
650 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
651 | {
|
---|
652 | s<<mKdTree->GetStatistics();
|
---|
653 | }
|
---|
654 |
|
---|
655 | void
|
---|
656 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
657 | {
|
---|
658 | s << mBspTree->GetStatistics();
|
---|
659 | }
|
---|
660 |
|
---|
661 | bool
|
---|
662 | Preprocessor::Export( const string filename,
|
---|
663 | const bool scene,
|
---|
664 | const bool kdtree
|
---|
665 | )
|
---|
666 | {
|
---|
667 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
668 |
|
---|
669 | if (exporter) {
|
---|
670 | if (2 && scene)
|
---|
671 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
672 |
|
---|
673 | if (1 && kdtree) {
|
---|
674 | exporter->SetWireframe();
|
---|
675 | exporter->ExportKdTree(*mKdTree);
|
---|
676 | }
|
---|
677 |
|
---|
678 | delete exporter;
|
---|
679 | return true;
|
---|
680 | }
|
---|
681 |
|
---|
682 | return false;
|
---|
683 | }
|
---|
684 |
|
---|
685 |
|
---|
686 | bool Preprocessor::PrepareViewCells()
|
---|
687 | {
|
---|
688 | ///////
|
---|
689 | //-- parse view cells construction method
|
---|
690 |
|
---|
691 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
692 | char buf[100];
|
---|
693 |
|
---|
694 | if (mLoadViewCells)
|
---|
695 | {
|
---|
696 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
697 | cout << "loading view cells from " << buf << endl<<flush;
|
---|
698 |
|
---|
699 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true, NULL);
|
---|
700 |
|
---|
701 | cout << "view cells loaded." << endl<<flush;
|
---|
702 |
|
---|
703 | if (!mViewCellsManager)
|
---|
704 | {
|
---|
705 | return false;
|
---|
706 | }
|
---|
707 | }
|
---|
708 | else
|
---|
709 | {
|
---|
710 | // parse type of view cell container
|
---|
711 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
712 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
713 |
|
---|
714 | // default view space is the extent of the scene
|
---|
715 | AxisAlignedBox3 viewSpaceBox;
|
---|
716 |
|
---|
717 | if (mUseViewSpaceBox)
|
---|
718 | {
|
---|
719 | viewSpaceBox = mSceneGraph->GetBox();
|
---|
720 |
|
---|
721 | // use a small box outside of the scene
|
---|
722 | viewSpaceBox.Scale(Vector3(0.15f, 0.3f, 0.5f));
|
---|
723 | //viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.5f, 0, 0));
|
---|
724 | viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.3f, 0, 0));
|
---|
725 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
726 | }
|
---|
727 | else
|
---|
728 | {
|
---|
729 | viewSpaceBox = mSceneGraph->GetBox();
|
---|
730 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
731 | }
|
---|
732 |
|
---|
733 | bool loadVcGeometry;
|
---|
734 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry);
|
---|
735 |
|
---|
736 | bool extrudeBaseTriangles;
|
---|
737 | Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles);
|
---|
738 |
|
---|
739 | char vcGeomFilename[100];
|
---|
740 | Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename);
|
---|
741 |
|
---|
742 | // create view cells from specified geometry
|
---|
743 | if (loadVcGeometry)
|
---|
744 | {
|
---|
745 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
746 | {
|
---|
747 | if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles))
|
---|
748 | {
|
---|
749 | cerr << "loading view cells geometry failed" << endl;
|
---|
750 | }
|
---|
751 | }
|
---|
752 | else
|
---|
753 | {
|
---|
754 | cerr << "loading view cells geometry is not implemented for this manager" << endl;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | ////////
|
---|
760 | //-- evaluation of render cost heuristics
|
---|
761 |
|
---|
762 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
763 |
|
---|
764 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
765 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
766 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
767 |
|
---|
768 | mRenderSimulator =
|
---|
769 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
770 |
|
---|
771 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
772 |
|
---|
773 | mViewCellsManager->SetPreprocessor(this);
|
---|
774 |
|
---|
775 | return true;
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 | bool Preprocessor::ConstructViewCells()
|
---|
780 | {
|
---|
781 | // construct view cells using it's own set of samples
|
---|
782 | mViewCellsManager->Construct(this);
|
---|
783 |
|
---|
784 | // visualizations and statistics
|
---|
785 | Debug << "finished view cells:" << endl;
|
---|
786 | mViewCellsManager->PrintStatistics(Debug);
|
---|
787 |
|
---|
788 | return true;
|
---|
789 | }
|
---|
790 |
|
---|
791 |
|
---|
792 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
793 | {
|
---|
794 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
795 |
|
---|
796 | if (strcmp(name, "kdTree") == 0)
|
---|
797 | {
|
---|
798 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
799 | }
|
---|
800 | else if (strcmp(name, "bspTree") == 0)
|
---|
801 | {
|
---|
802 | Debug << "view cell type: Bsp" << endl;
|
---|
803 |
|
---|
804 | mBspTree = new BspTree();
|
---|
805 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
806 | }
|
---|
807 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
808 | {
|
---|
809 | Debug << "view cell type: VspBsp" << endl;
|
---|
810 |
|
---|
811 | mVspBspTree = new VspBspTree();
|
---|
812 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
813 | }
|
---|
814 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
815 | {
|
---|
816 | Debug << "view cell type: VspOsp" << endl;
|
---|
817 | char buf[100];
|
---|
818 | Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);
|
---|
819 |
|
---|
820 | mViewCellsManager = new VspOspViewCellsManager(vcTree, buf);
|
---|
821 | }
|
---|
822 | else if (strcmp(name, "sceneDependent") == 0) //TODO
|
---|
823 | {
|
---|
824 | Debug << "view cell type: Bsp" << endl;
|
---|
825 |
|
---|
826 | mBspTree = new BspTree();
|
---|
827 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
828 | }
|
---|
829 | else
|
---|
830 | {
|
---|
831 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
832 | exit(1);
|
---|
833 | }
|
---|
834 |
|
---|
835 | return mViewCellsManager;
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | // use ascii format to store rays
|
---|
840 | #define USE_ASCII 0
|
---|
841 |
|
---|
842 |
|
---|
843 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
844 | {
|
---|
845 | return obj1->mId < obj2->mId;
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | bool Preprocessor::LoadKdTree(const string filename)
|
---|
850 | {
|
---|
851 | mKdTree = new KdTree();
|
---|
852 |
|
---|
853 | return mKdTree->LoadBinTree(filename.c_str(), mObjects);
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | bool Preprocessor::ExportKdTree(const string filename)
|
---|
858 | {
|
---|
859 | return mKdTree->ExportBinTree(filename.c_str());
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
864 | ObjectContainer &objects) const
|
---|
865 | {
|
---|
866 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
867 | char fileName[100];
|
---|
868 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
869 |
|
---|
870 | Vector3 origin, termination;
|
---|
871 | // HACK: needed only for lower_bound algorithm to find the
|
---|
872 | // intersected objects
|
---|
873 | MeshInstance sObj(NULL);
|
---|
874 | MeshInstance tObj(NULL);
|
---|
875 |
|
---|
876 | #if USE_ASCII
|
---|
877 | ifstream samplesIn(fileName);
|
---|
878 | if (!samplesIn.is_open())
|
---|
879 | return false;
|
---|
880 |
|
---|
881 | string buf;
|
---|
882 | while (!(getline(samplesIn, buf)).eof())
|
---|
883 | {
|
---|
884 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
885 | &origin.x, &origin.y, &origin.z,
|
---|
886 | &termination.x, &termination.y, &termination.z,
|
---|
887 | &(sObj.mId), &(tObj.mId));
|
---|
888 |
|
---|
889 | Intersectable *sourceObj = NULL;
|
---|
890 | Intersectable *termObj = NULL;
|
---|
891 |
|
---|
892 | if (sObj.mId >= 0)
|
---|
893 | {
|
---|
894 | ObjectContainer::iterator oit =
|
---|
895 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
896 | sourceObj = *oit;
|
---|
897 | }
|
---|
898 |
|
---|
899 | if (tObj.mId >= 0)
|
---|
900 | {
|
---|
901 | ObjectContainer::iterator oit =
|
---|
902 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
903 | termObj = *oit;
|
---|
904 | }
|
---|
905 |
|
---|
906 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
907 | }
|
---|
908 | #else
|
---|
909 | ifstream samplesIn(fileName, ios::binary);
|
---|
910 | if (!samplesIn.is_open())
|
---|
911 | return false;
|
---|
912 |
|
---|
913 | while (1)
|
---|
914 | {
|
---|
915 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
916 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
917 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
918 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
919 |
|
---|
920 | if (samplesIn.eof())
|
---|
921 | break;
|
---|
922 |
|
---|
923 | Intersectable *sourceObj = NULL;
|
---|
924 | Intersectable *termObj = NULL;
|
---|
925 |
|
---|
926 | if (sObj.mId >= 0)
|
---|
927 | {
|
---|
928 | ObjectContainer::iterator oit =
|
---|
929 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
930 | sourceObj = *oit;
|
---|
931 | }
|
---|
932 |
|
---|
933 | if (tObj.mId >= 0)
|
---|
934 | {
|
---|
935 | ObjectContainer::iterator oit =
|
---|
936 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
937 | termObj = *oit;
|
---|
938 | }
|
---|
939 |
|
---|
940 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
941 | }
|
---|
942 | #endif
|
---|
943 |
|
---|
944 | samplesIn.close();
|
---|
945 |
|
---|
946 | return true;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
951 | {
|
---|
952 | char fileName[100];
|
---|
953 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
954 |
|
---|
955 |
|
---|
956 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
957 |
|
---|
958 | #if USE_ASCII
|
---|
959 | ofstream samplesOut(fileName);
|
---|
960 | if (!samplesOut.is_open())
|
---|
961 | return false;
|
---|
962 |
|
---|
963 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
964 | {
|
---|
965 | VssRay *ray = *it;
|
---|
966 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
967 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
968 |
|
---|
969 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
970 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
971 | << sourceid << " " << termid << "\n";
|
---|
972 | }
|
---|
973 | #else
|
---|
974 | ofstream samplesOut(fileName, ios::binary);
|
---|
975 | if (!samplesOut.is_open())
|
---|
976 | return false;
|
---|
977 |
|
---|
978 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
979 | {
|
---|
980 | VssRay *ray = *it;
|
---|
981 | Vector3 origin(ray->GetOrigin());
|
---|
982 | Vector3 termination(ray->GetTermination());
|
---|
983 |
|
---|
984 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
985 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
986 |
|
---|
987 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
988 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
989 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
990 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
991 | }
|
---|
992 | #endif
|
---|
993 | samplesOut.close();
|
---|
994 |
|
---|
995 | return true;
|
---|
996 | }
|
---|
997 |
|
---|
998 |
|
---|
999 | int
|
---|
1000 | Preprocessor::GenerateRays(const int number,
|
---|
1001 | SamplingStrategy &strategy,
|
---|
1002 | SimpleRayContainer &rays)
|
---|
1003 | {
|
---|
1004 | return strategy.GenerateSamples(number, rays);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | int
|
---|
1008 | Preprocessor::GenerateRays(const int number,
|
---|
1009 | const int sampleType,
|
---|
1010 | SimpleRayContainer &rays)
|
---|
1011 | {
|
---|
1012 | const int startSize = (int)rays.size();
|
---|
1013 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
1014 | int castRays = 0;
|
---|
1015 |
|
---|
1016 | if (!strategy)
|
---|
1017 | {
|
---|
1018 | return 0;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | #if 1
|
---|
1022 | castRays = strategy->GenerateSamples(number, rays);
|
---|
1023 | #else
|
---|
1024 | GenerateRayBundle(rays, newRay, 16, 0);
|
---|
1025 | castRays += 16;
|
---|
1026 | #endif
|
---|
1027 |
|
---|
1028 | delete strategy;
|
---|
1029 | return castRays;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 |
|
---|
1033 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId)
|
---|
1034 | {
|
---|
1035 | switch (strategyId)
|
---|
1036 | {
|
---|
1037 | case SamplingStrategy::OBJECT_BASED_DISTRIBUTION:
|
---|
1038 | return new ObjectBasedDistribution(*this);
|
---|
1039 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
1040 | return new ObjectDirectionBasedDistribution(*this);
|
---|
1041 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
1042 | return new DirectionBasedDistribution(*this);
|
---|
1043 | case SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
1044 | return new DirectionBoxBasedDistribution(*this);
|
---|
1045 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
1046 | return new SpatialBoxBasedDistribution(*this);
|
---|
1047 | case SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION:
|
---|
1048 | return new ReverseObjectBasedDistribution(*this);
|
---|
1049 | case SamplingStrategy::VIEWCELL_BORDER_BASED_DISTRIBUTION:
|
---|
1050 | return new ViewCellBorderBasedDistribution(*this);
|
---|
1051 | case SamplingStrategy::VIEWSPACE_BORDER_BASED_DISTRIBUTION:
|
---|
1052 | return new ViewSpaceBorderBasedDistribution(*this);
|
---|
1053 | case SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION:
|
---|
1054 | return new ReverseViewSpaceBorderBasedDistribution(*this);
|
---|
1055 | case SamplingStrategy::GLOBAL_LINES_DISTRIBUTION:
|
---|
1056 | return new GlobalLinesDistribution(*this);
|
---|
1057 |
|
---|
1058 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
1059 | // return new ObjectsInteriorDistribution(*this);
|
---|
1060 | default: // no valid strategy
|
---|
1061 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
1062 | return NULL;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | return NULL; // should never come here
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | bool Preprocessor::InitRayCast(const string externKdTree, const string internKdTree)
|
---|
1070 | {
|
---|
1071 | // always try to load the kd tree
|
---|
1072 | cout << "loading kd tree file " << internKdTree << " ... " << endl;
|
---|
1073 |
|
---|
1074 | if (!LoadKdTree(internKdTree))
|
---|
1075 | {
|
---|
1076 | cout << "error loading kd tree with filename " << internKdTree << ", rebuilding it instead ... " << endl;
|
---|
1077 | // build new kd tree from scene geometry
|
---|
1078 | BuildKdTree();
|
---|
1079 |
|
---|
1080 | // export kd tree?
|
---|
1081 | const long startTime = GetTime();
|
---|
1082 | cout << "exporting kd tree ... ";
|
---|
1083 |
|
---|
1084 | if (!ExportKdTree(internKdTree))
|
---|
1085 | {
|
---|
1086 | cout << " error exporting kd tree with filename " << internKdTree << endl;
|
---|
1087 | }
|
---|
1088 | else
|
---|
1089 | {
|
---|
1090 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
1091 | }
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | KdTreeStatistics(cout);
|
---|
1095 | cout << mKdTree->GetBox() << endl;
|
---|
1096 |
|
---|
1097 | if (0)
|
---|
1098 | {
|
---|
1099 | Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
|
---|
1100 |
|
---|
1101 | if (exporter)
|
---|
1102 | {
|
---|
1103 | exporter->ExportKdTree(*mKdTree, true);
|
---|
1104 | delete exporter;
|
---|
1105 | }
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | int rayCastMethod;
|
---|
1109 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
1110 |
|
---|
1111 | if (rayCastMethod == 0)
|
---|
1112 | {
|
---|
1113 | cout << "ray cast method: internal" << endl;
|
---|
1114 | mRayCaster = new InternalRayCaster(*this, mKdTree);
|
---|
1115 | }
|
---|
1116 | else
|
---|
1117 | {
|
---|
1118 | #ifdef GTP_INTERNAL
|
---|
1119 | cout << "ray cast method: intel" << endl;
|
---|
1120 | mRayCaster = new IntelRayCaster(*this, externKdTree);
|
---|
1121 | #endif
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | return true;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 |
|
---|
1128 | void
|
---|
1129 | Preprocessor::CastRays(
|
---|
1130 | SimpleRayContainer &rays,
|
---|
1131 | VssRayContainer &vssRays,
|
---|
1132 | const bool castDoubleRays,
|
---|
1133 | const bool pruneInvalidRays
|
---|
1134 | )
|
---|
1135 | {
|
---|
1136 | const long t1 = GetTime();
|
---|
1137 |
|
---|
1138 | for (int i = 0; i < (int)rays.size();)
|
---|
1139 | {
|
---|
1140 | if (i + 16 < (int)rays.size())
|
---|
1141 | {
|
---|
1142 | mRayCaster->CastRays16(
|
---|
1143 | i,
|
---|
1144 | rays,
|
---|
1145 | vssRays,
|
---|
1146 | mViewCellsManager->GetViewSpaceBox(),
|
---|
1147 | castDoubleRays,
|
---|
1148 | pruneInvalidRays);
|
---|
1149 | i += 16;
|
---|
1150 | }
|
---|
1151 | else
|
---|
1152 | {
|
---|
1153 | mRayCaster->CastRay(
|
---|
1154 | rays[i],
|
---|
1155 | vssRays,
|
---|
1156 | mViewCellsManager->GetViewSpaceBox(),
|
---|
1157 | castDoubleRays,
|
---|
1158 | pruneInvalidRays);
|
---|
1159 | ++ i;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | if (rays.size() > 10000 && i % 10000 == 0)
|
---|
1163 | cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | if (rays.size() > 10000)
|
---|
1167 | {
|
---|
1168 | cout<<endl;
|
---|
1169 |
|
---|
1170 | long t2 = GetTime();
|
---|
1171 |
|
---|
1172 | #if SHOW_RAYCAST_TIMING
|
---|
1173 | if (castDoubleRays)
|
---|
1174 | cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
1175 | else
|
---|
1176 | cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
1177 | #endif
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 |
|
---|
1182 | bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,
|
---|
1183 | const SimpleRay &mainRay,
|
---|
1184 | const int number,
|
---|
1185 | const int pertubType) const
|
---|
1186 | {
|
---|
1187 | rayBundle.push_back(mainRay);
|
---|
1188 |
|
---|
1189 | const float pertubOrigin = 0.0f;
|
---|
1190 | const float pertubDir = 0.2f;
|
---|
1191 |
|
---|
1192 | for (int i = 0; i < number - 1; ++ i)
|
---|
1193 | {
|
---|
1194 | Vector3 pertub;
|
---|
1195 |
|
---|
1196 | pertub.x = RandomValue(0.0f, pertubDir);
|
---|
1197 | pertub.y = RandomValue(0.0f, pertubDir);
|
---|
1198 | pertub.z = RandomValue(0.0f, pertubDir);
|
---|
1199 |
|
---|
1200 | const Vector3 newDir = mainRay.mDirection + pertub;
|
---|
1201 | //const Vector3 newDir = mainRay.mDirection;
|
---|
1202 |
|
---|
1203 | pertub.x = RandomValue(0.0f, pertubOrigin);
|
---|
1204 | pertub.y = RandomValue(0.0f, pertubOrigin);
|
---|
1205 | pertub.z = RandomValue(0.0f, pertubOrigin);
|
---|
1206 |
|
---|
1207 | const Vector3 newOrigin = mainRay.mOrigin + pertub;
|
---|
1208 | //const Vector3 newOrigin = mainRay.mOrigin;
|
---|
1209 |
|
---|
1210 | rayBundle.push_back(SimpleRay(newOrigin, newDir, 0, 1.0f));
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | return true;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 |
|
---|
1217 | void Preprocessor::SetupRay(Ray &ray,
|
---|
1218 | const Vector3 &point,
|
---|
1219 | const Vector3 &direction) const
|
---|
1220 | {
|
---|
1221 | ray.Clear();
|
---|
1222 | // do not store anything else then intersections at the ray
|
---|
1223 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | void Preprocessor::EvalViewCellHistogram()
|
---|
1227 | {
|
---|
1228 | char filename[256];
|
---|
1229 | Environment::GetSingleton()->GetStringValue("Preprocessor.histogram.file", filename);
|
---|
1230 |
|
---|
1231 | // mViewCellsManager->EvalViewCellHistogram(filename, 1000000);
|
---|
1232 | mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, 1000000);
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 |
|
---|
1236 | bool
|
---|
1237 | Preprocessor::ExportRays(const char *filename,
|
---|
1238 | const VssRayContainer &vssRays,
|
---|
1239 | const int number
|
---|
1240 | )
|
---|
1241 | {
|
---|
1242 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
1243 |
|
---|
1244 | Exporter *exporter = NULL;
|
---|
1245 | exporter = Exporter::GetExporter(filename);
|
---|
1246 |
|
---|
1247 | if (0) {
|
---|
1248 | exporter->SetWireframe();
|
---|
1249 | exporter->ExportKdTree(*mKdTree);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | exporter->SetFilled();
|
---|
1253 | // $$JB temporarily do not export the scene
|
---|
1254 | if (0)
|
---|
1255 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
1256 |
|
---|
1257 | exporter->SetWireframe();
|
---|
1258 |
|
---|
1259 | if (1) {
|
---|
1260 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
1261 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
1262 | exporter->ResetForcedMaterial();
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | VssRayContainer rays;
|
---|
1266 | vssRays.SelectRays(number, rays);
|
---|
1267 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
1268 | delete exporter;
|
---|
1269 | cout<<"done."<<endl<<flush;
|
---|
1270 |
|
---|
1271 | return true;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | void
|
---|
1275 | Preprocessor::ComputeRenderError()
|
---|
1276 | {
|
---|
1277 | // compute rendering error
|
---|
1278 |
|
---|
1279 | if (renderer && renderer->mPvsStatFrames) {
|
---|
1280 | // emit EvalPvsStat();
|
---|
1281 | // QMutex mutex;
|
---|
1282 | // mutex.lock();
|
---|
1283 | // renderer->mRenderingFinished.wait(&mutex);
|
---|
1284 | // mutex.unlock();
|
---|
1285 |
|
---|
1286 | renderer->EvalPvsStat();
|
---|
1287 |
|
---|
1288 | mStats <<
|
---|
1289 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
1290 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
1291 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
1292 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
1293 | }
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 |
|
---|
1297 | Intersectable *Preprocessor::GetObjectById(const int id)
|
---|
1298 | {
|
---|
1299 | #if 1
|
---|
1300 | // create a dummy mesh instance to be able to use stl
|
---|
1301 | MeshInstance object(NULL);
|
---|
1302 | object.SetId(id);
|
---|
1303 |
|
---|
1304 | ObjectContainer::const_iterator oit =
|
---|
1305 | lower_bound(mObjects.begin(), mObjects.end(), &object, ilt);
|
---|
1306 |
|
---|
1307 | // objects sorted by id
|
---|
1308 | if ((oit != mObjects.end()) && ((*oit)->GetId() == object.GetId()))
|
---|
1309 | {
|
---|
1310 | return (*oit);
|
---|
1311 | }
|
---|
1312 | else
|
---|
1313 | {
|
---|
1314 | return NULL;
|
---|
1315 | }
|
---|
1316 | #else
|
---|
1317 | return mObjects[id - 1];
|
---|
1318 | #endif
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | }
|
---|