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