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