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