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