[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"
|
---|
[1022] | 15 | #include "VspOspTree.h"
|
---|
[1221] | 16 | #include "ObjParser.h"
|
---|
[1232] | 17 | #ifdef INTEL_COMPILER
|
---|
[1221] | 18 | #include "ArchModeler2MLRT.hxx"
|
---|
[1232] | 19 | #endif
|
---|
[372] | 20 |
|
---|
[863] | 21 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 22 |
|
---|
[1020] | 23 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
[860] | 24 |
|
---|
[1145] | 25 | //Preprocessor *preprocessor;
|
---|
[492] | 26 |
|
---|
[750] | 27 |
|
---|
[1001] | 28 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
[750] | 29 | static void AddGeometry(SceneGraph *scene)
|
---|
| 30 | {
|
---|
[752] | 31 | scene->mRoot->UpdateBox();
|
---|
| 32 |
|
---|
[750] | 33 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
| 34 |
|
---|
| 35 | int n = 200;
|
---|
| 36 |
|
---|
| 37 | if (0){
|
---|
[840] | 38 | // form grid of boxes
|
---|
[750] | 39 | for (int i = 0; i < n; ++ i)
|
---|
| 40 | {
|
---|
| 41 | for (int j = 0; j < n; ++ j)
|
---|
| 42 | {
|
---|
[1135] | 43 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 44 |
|
---|
[840] | 45 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
[750] | 46 |
|
---|
[840] | 47 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
[750] | 48 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
[991] | 49 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 50 |
|
---|
| 51 | mesh->Preprocess();
|
---|
| 52 |
|
---|
| 53 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 54 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | for (int i = 0; i < n; ++ i)
|
---|
| 59 | {
|
---|
| 60 | for (int j = 0; j < n; ++ j)
|
---|
| 61 | {
|
---|
[1135] | 62 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 63 |
|
---|
| 64 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
| 65 |
|
---|
[1135] | 66 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
[750] | 67 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
[991] | 68 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 69 |
|
---|
| 70 | mesh->Preprocess();
|
---|
| 71 |
|
---|
| 72 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 73 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | for (int i = 0; i < n; ++ i)
|
---|
| 78 | {
|
---|
[1135] | 79 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 80 |
|
---|
| 81 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
| 82 |
|
---|
| 83 | //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
|
---|
[1135] | 84 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
[1020] | 85 |
|
---|
[1135] | 86 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
[991] | 87 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 88 |
|
---|
| 89 | mesh->Preprocess();
|
---|
| 90 |
|
---|
| 91 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 92 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | scene->mRoot->UpdateBox();
|
---|
| 96 | }
|
---|
[1221] | 97 |
|
---|
[840] | 98 | if (1)
|
---|
| 99 | {
|
---|
[1221] | 100 | // plane separating view space regions
|
---|
[1135] | 101 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
[750] | 102 |
|
---|
[840] | 103 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
[750] | 104 |
|
---|
[840] | 105 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
| 106 | Mesh *planeMesh = new Mesh();
|
---|
| 107 |
|
---|
| 108 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
| 109 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
| 110 |
|
---|
| 111 | planeMesh->Preprocess();
|
---|
| 112 |
|
---|
| 113 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
| 114 | scene->mRoot->mGeometry.push_back(planeMi);
|
---|
| 115 | }
|
---|
[750] | 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
[372] | 119 | Preprocessor::Preprocessor():
|
---|
| 120 | mKdTree(NULL),
|
---|
[409] | 121 | mBspTree(NULL),
|
---|
[445] | 122 | mVspBspTree(NULL),
|
---|
[1002] | 123 | mViewCellsManager(NULL),
|
---|
| 124 | mRenderSimulator(NULL)
|
---|
[308] | 125 | {
|
---|
[1004] | 126 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
[538] | 127 |
|
---|
[840] | 128 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
| 129 | renderer = NULL;
|
---|
[496] | 130 |
|
---|
[1004] | 131 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
| 132 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes);
|
---|
| 133 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
| 134 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
| 135 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
| 136 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
[1221] | 137 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", mRayCastMethod);
|
---|
[599] | 138 |
|
---|
[871] | 139 | char buffer[256];
|
---|
[1004] | 140 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
[871] | 141 | mVisibilityFileName = buffer;
|
---|
[1004] | 142 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
|
---|
| 143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
[904] | 144 | mApplyVisibilitySpatialFilter );
|
---|
[1004] | 145 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
[878] | 146 |
|
---|
[840] | 147 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
| 148 | Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
|
---|
[372] | 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | Preprocessor::~Preprocessor()
|
---|
| 153 | {
|
---|
[752] | 154 | cout << "cleaning up" << endl;
|
---|
| 155 |
|
---|
| 156 | cout << "Deleting view cells manager ... \n";
|
---|
[496] | 157 | DEL_PTR(mViewCellsManager);
|
---|
[752] | 158 | cout << "done.\n";
|
---|
| 159 |
|
---|
| 160 | cout << "Deleting bsp tree ... \n";
|
---|
[496] | 161 | DEL_PTR(mBspTree);
|
---|
[752] | 162 | cout << "done.\n";
|
---|
| 163 |
|
---|
[840] | 164 | cout << "Deleting kd tree...\n";
|
---|
[496] | 165 | DEL_PTR(mKdTree);
|
---|
[840] | 166 | cout << "done.\n";
|
---|
[1006] | 167 |
|
---|
[1145] | 168 | cout << "Deleting vsp tree...\n";
|
---|
| 169 | DEL_PTR(mVspTree);
|
---|
[840] | 170 | cout << "done.\n";
|
---|
[752] | 171 |
|
---|
[1145] | 172 | cout << "Deleting osp tree...\n";
|
---|
| 173 | DEL_PTR(mOspTree);
|
---|
| 174 | cout << "done.\n";
|
---|
| 175 |
|
---|
[840] | 176 | cout << "Deleting vspbsp tree...\n";
|
---|
[496] | 177 | DEL_PTR(mVspBspTree);
|
---|
[840] | 178 | cout << "done.\n";
|
---|
[1002] | 179 |
|
---|
| 180 | cout << "Deleting scene graph...\n";
|
---|
| 181 | DEL_PTR(mSceneGraph);
|
---|
| 182 | cout << "done.\n";
|
---|
| 183 |
|
---|
| 184 | DEL_PTR(mRenderSimulator);
|
---|
| 185 | DEL_PTR(renderer);
|
---|
[372] | 186 | }
|
---|
| 187 |
|
---|
[387] | 188 | int
|
---|
[419] | 189 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
[387] | 190 | {
|
---|
| 191 | int pos = 0;
|
---|
| 192 |
|
---|
| 193 | while(1) {
|
---|
[469] | 194 | int npos = (int)str.find(';', pos);
|
---|
[387] | 195 |
|
---|
| 196 | if (npos < 0 || npos - pos < 1)
|
---|
| 197 | break;
|
---|
| 198 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 199 | pos = npos + 1;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
[440] | 203 | return (int)filenames.size();
|
---|
[387] | 204 | }
|
---|
| 205 |
|
---|
[750] | 206 |
|
---|
[372] | 207 | bool
|
---|
| 208 | Preprocessor::LoadScene(const string filename)
|
---|
| 209 | {
|
---|
[925] | 210 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
[508] | 211 | mSceneGraph = new SceneGraph;
|
---|
[372] | 212 |
|
---|
[508] | 213 | Parser *parser;
|
---|
[387] | 214 | vector<string> filenames;
|
---|
| 215 | int files = SplitFilenames(filename, filenames);
|
---|
[712] | 216 | cout << "number of input files: " << files << endl;
|
---|
[387] | 217 | bool result = false;
|
---|
| 218 | if (files == 1) {
|
---|
| 219 |
|
---|
| 220 | if (strstr(filename.c_str(), ".x3d"))
|
---|
[749] | 221 | parser = new X3dParser;
|
---|
[387] | 222 | else
|
---|
[811] | 223 | if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
[749] | 224 | parser = new PlyParser;
|
---|
[1221] | 225 | else if (strstr(filename.c_str(), ".obj"))
|
---|
| 226 | parser = new ObjParser;
|
---|
| 227 | else
|
---|
| 228 | parser = new UnigraphicsParser;
|
---|
[372] | 229 |
|
---|
[387] | 230 | cout<<filename<<endl;
|
---|
[372] | 231 |
|
---|
[1221] | 232 | if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
|
---|
| 233 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes, &mFaceParents);
|
---|
| 234 | else
|
---|
| 235 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
|
---|
| 236 |
|
---|
[387] | 237 | delete parser;
|
---|
[372] | 238 |
|
---|
[387] | 239 | } else {
|
---|
| 240 | // root for different files
|
---|
| 241 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
| 242 | for (int i= 0; i < filenames.size(); i++) {
|
---|
| 243 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
| 244 | parser = new X3dParser;
|
---|
[1221] | 245 | else
|
---|
[387] | 246 | parser = new UnigraphicsParser;
|
---|
[1221] | 247 |
|
---|
[387] | 248 | SceneGraphNode *node;
|
---|
| 249 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
| 250 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
| 251 | // at least one file parsed
|
---|
| 252 | result = true;
|
---|
| 253 | }
|
---|
| 254 | delete parser;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
[372] | 257 |
|
---|
| 258 |
|
---|
[752] | 259 | if (result)
|
---|
[925] | 260 | {
|
---|
[752] | 261 | // HACK
|
---|
[1020] | 262 | if (ADDITIONAL_GEOMETRY_HACK)
|
---|
| 263 | AddGeometry(mSceneGraph);
|
---|
[752] | 264 |
|
---|
[1020] | 265 | mSceneGraph->AssignObjectIds();
|
---|
| 266 |
|
---|
| 267 | int intersectables, faces;
|
---|
| 268 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
| 269 |
|
---|
| 270 | cout<<filename<<" parsed successfully."<<endl;
|
---|
| 271 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
| 272 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
| 273 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 274 | mSceneGraph->mRoot->UpdateBox();
|
---|
[752] | 275 |
|
---|
[1020] | 276 | if (0)
|
---|
| 277 | {
|
---|
| 278 | Exporter *exporter = Exporter::GetExporter("testload.x3d");
|
---|
[658] | 279 |
|
---|
[1020] | 280 | if (exporter)
|
---|
| 281 | {
|
---|
| 282 | exporter->ExportGeometry(mObjects);
|
---|
| 283 | delete exporter;
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
[387] | 286 | }
|
---|
[372] | 287 |
|
---|
[492] | 288 |
|
---|
| 289 | return result;
|
---|
[372] | 290 | }
|
---|
| 291 |
|
---|
| 292 | bool
|
---|
| 293 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
| 294 | {
|
---|
[871] | 295 |
|
---|
[931] | 296 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
[871] | 297 |
|
---|
| 298 | return true;
|
---|
[372] | 299 | }
|
---|
| 300 |
|
---|
| 301 | bool
|
---|
[871] | 302 | Preprocessor::PostProcessVisibility()
|
---|
| 303 | {
|
---|
| 304 |
|
---|
[904] | 305 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
[997] | 306 | cout<<"Applying visibility filter ...";
|
---|
[1002] | 307 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
[904] | 308 |
|
---|
[1002] | 309 | if (!mViewCellsManager)
|
---|
[1199] | 310 | return false;
|
---|
| 311 |
|
---|
[871] | 312 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
[904] | 313 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
| 314 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
[997] | 315 | cout << "done." << endl;
|
---|
[871] | 316 | }
|
---|
| 317 |
|
---|
| 318 | // export the preprocessed information to a file
|
---|
| 319 | if (mExportVisibility)
|
---|
| 320 | ExportPreprocessedData(mVisibilityFileName);
|
---|
| 321 |
|
---|
| 322 | return true;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | bool
|
---|
[372] | 327 | Preprocessor::BuildKdTree()
|
---|
| 328 | {
|
---|
| 329 | mKdTree = new KdTree;
|
---|
| 330 | // add mesh instances of the scene graph to the root of the tree
|
---|
| 331 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
| 332 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
| 333 |
|
---|
[1201] | 334 | long startTime = GetTime();
|
---|
| 335 |
|
---|
| 336 | cout << "building kd tree ... " << endl;
|
---|
[372] | 337 | mKdTree->Construct();
|
---|
[1201] | 338 | cout << "construction finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs " << endl;
|
---|
[372] | 339 | return true;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | void
|
---|
| 343 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
| 344 | {
|
---|
| 345 | s<<mKdTree->GetStatistics();
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | void
|
---|
| 349 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
| 350 | {
|
---|
| 351 | s << mBspTree->GetStatistics();
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | bool
|
---|
| 355 | Preprocessor::Export( const string filename,
|
---|
[492] | 356 | const bool scene,
|
---|
| 357 | const bool kdtree,
|
---|
| 358 | const bool bsptree
|
---|
| 359 | )
|
---|
[372] | 360 | {
|
---|
| 361 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 362 |
|
---|
| 363 | if (exporter) {
|
---|
| 364 | if (scene)
|
---|
| 365 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 366 |
|
---|
| 367 | if (kdtree) {
|
---|
| 368 | exporter->SetWireframe();
|
---|
| 369 | exporter->ExportKdTree(*mKdTree);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | if (bsptree) {
|
---|
| 373 | //exporter->SetWireframe();
|
---|
| 374 | exporter->ExportBspTree(*mBspTree);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | delete exporter;
|
---|
| 378 | return true;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | return false;
|
---|
| 382 | }
|
---|
[429] | 383 |
|
---|
[508] | 384 |
|
---|
[463] | 385 | bool Preprocessor::PrepareViewCells()
|
---|
| 386 | {
|
---|
[577] | 387 | //-- parse view cells construction method
|
---|
[1004] | 388 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
[577] | 389 | char buf[100];
|
---|
| 390 |
|
---|
| 391 | if (mLoadViewCells)
|
---|
[997] | 392 | {
|
---|
[1004] | 393 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
| 394 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
|
---|
[577] | 395 | }
|
---|
| 396 | else
|
---|
| 397 | {
|
---|
| 398 | //-- parse type of view cell container
|
---|
[1004] | 399 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
[1002] | 400 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
[1112] | 401 |
|
---|
| 402 | // default view space is the extent of the scene
|
---|
| 403 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
| 404 |
|
---|
| 405 |
|
---|
[577] | 406 | }
|
---|
[1112] | 407 |
|
---|
[997] | 408 | //-- parameters for render heuristics evaluation
|
---|
[473] | 409 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 410 |
|
---|
[1004] | 411 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 412 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 413 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
[694] | 414 |
|
---|
[473] | 415 | mRenderSimulator =
|
---|
| 416 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
[440] | 417 |
|
---|
[480] | 418 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
[440] | 419 |
|
---|
[574] | 420 |
|
---|
[538] | 421 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
[540] | 422 | {
|
---|
| 423 | // NOTE: render texture should be power of 2 and square
|
---|
| 424 | // renderer must be initialised
|
---|
[1145] | 425 | // $$matt
|
---|
| 426 | // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
[556] | 427 | // renderer->makeCurrent();
|
---|
[746] | 428 |
|
---|
[540] | 429 | }
|
---|
[496] | 430 |
|
---|
[463] | 431 | return true;
|
---|
[490] | 432 | }
|
---|
| 433 |
|
---|
| 434 |
|
---|
[575] | 435 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
| 436 | {
|
---|
| 437 | if (strcmp(name, "kdTree") == 0)
|
---|
| 438 | {
|
---|
[1004] | 439 | mViewCellsManager = new KdViewCellsManager(mKdTree);
|
---|
[575] | 440 | }
|
---|
| 441 | else if (strcmp(name, "bspTree") == 0)
|
---|
| 442 | {
|
---|
| 443 | Debug << "view cell type: Bsp" << endl;
|
---|
| 444 |
|
---|
[577] | 445 | mBspTree = new BspTree();
|
---|
[1004] | 446 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
[575] | 447 | }
|
---|
| 448 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
| 449 | {
|
---|
| 450 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 451 |
|
---|
[1004] | 452 | mVspBspTree = new VspBspTree();
|
---|
| 453 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
[575] | 454 | }
|
---|
[1006] | 455 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
[575] | 456 | {
|
---|
[1022] | 457 | mVspTree = new VspTree();
|
---|
[1144] | 458 | mOspTree = new OspTree();
|
---|
[1174] | 459 |
|
---|
| 460 | // HACK for testing if per kd evaluation works!!
|
---|
| 461 | mOspTree = new OspTree(*mKdTree);
|
---|
[1022] | 462 |
|
---|
| 463 | mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
|
---|
[575] | 464 | }
|
---|
| 465 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
| 466 | {
|
---|
[1143] | 467 | Debug << "view cell type: Bsp" << endl;
|
---|
| 468 |
|
---|
[575] | 469 | //TODO
|
---|
| 470 | mBspTree = new BspTree();
|
---|
[1004] | 471 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
[575] | 472 | }
|
---|
| 473 | else
|
---|
| 474 | {
|
---|
[664] | 475 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
[575] | 476 | exit(1);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | return mViewCellsManager;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 |
|
---|
[491] | 483 | // use ascii format to store rays
|
---|
| 484 | #define USE_ASCII 0
|
---|
| 485 |
|
---|
| 486 |
|
---|
[1145] | 487 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
[490] | 488 | {
|
---|
| 489 | return obj1->mId < obj2->mId;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 |
|
---|
[1197] | 493 | bool Preprocessor::LoadKdTree()
|
---|
| 494 | {
|
---|
| 495 | return true;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | bool Preprocessor::ExportKdTree()
|
---|
| 499 | {
|
---|
| 500 | return true;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 |
|
---|
[490] | 504 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
| 505 | ObjectContainer &objects) const
|
---|
| 506 | {
|
---|
| 507 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 508 | char fileName[100];
|
---|
[1004] | 509 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[490] | 510 |
|
---|
[491] | 511 | Vector3 origin, termination;
|
---|
| 512 | // HACK: needed only for lower_bound algorithm to find the
|
---|
| 513 | // intersected objects
|
---|
| 514 | MeshInstance sObj(NULL);
|
---|
| 515 | MeshInstance tObj(NULL);
|
---|
[490] | 516 |
|
---|
[491] | 517 | #if USE_ASCII
|
---|
[656] | 518 | ifstream samplesIn(fileName);
|
---|
[490] | 519 | if (!samplesIn.is_open())
|
---|
| 520 | return false;
|
---|
| 521 |
|
---|
| 522 | string buf;
|
---|
| 523 | while (!(getline(samplesIn, buf)).eof())
|
---|
| 524 | {
|
---|
[491] | 525 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
[490] | 526 | &origin.x, &origin.y, &origin.z,
|
---|
[491] | 527 | &termination.x, &termination.y, &termination.z,
|
---|
| 528 | &(sObj.mId), &(tObj.mId));
|
---|
[490] | 529 |
|
---|
[491] | 530 | Intersectable *sourceObj = NULL;
|
---|
| 531 | Intersectable *termObj = NULL;
|
---|
| 532 |
|
---|
| 533 | if (sObj.mId >= 0)
|
---|
[490] | 534 | {
|
---|
| 535 | ObjectContainer::iterator oit =
|
---|
[491] | 536 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 537 | sourceObj = *oit;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | if (tObj.mId >= 0)
|
---|
| 541 | {
|
---|
| 542 | ObjectContainer::iterator oit =
|
---|
| 543 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 544 | termObj = *oit;
|
---|
| 545 | }
|
---|
[490] | 546 |
|
---|
[491] | 547 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
| 548 | }
|
---|
| 549 | #else
|
---|
| 550 | ifstream samplesIn(fileName, ios::binary);
|
---|
| 551 | if (!samplesIn.is_open())
|
---|
| 552 | return false;
|
---|
| 553 |
|
---|
| 554 | while (1)
|
---|
| 555 | {
|
---|
| 556 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 557 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 558 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
| 559 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
| 560 |
|
---|
| 561 | if (samplesIn.eof())
|
---|
| 562 | break;
|
---|
| 563 |
|
---|
| 564 | Intersectable *sourceObj = NULL;
|
---|
| 565 | Intersectable *termObj = NULL;
|
---|
| 566 |
|
---|
| 567 | if (sObj.mId >= 0)
|
---|
| 568 | {
|
---|
| 569 | ObjectContainer::iterator oit =
|
---|
| 570 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 571 | sourceObj = *oit;
|
---|
[490] | 572 | }
|
---|
[491] | 573 |
|
---|
| 574 | if (tObj.mId >= 0)
|
---|
[490] | 575 | {
|
---|
[491] | 576 | ObjectContainer::iterator oit =
|
---|
| 577 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 578 | termObj = *oit;
|
---|
[490] | 579 | }
|
---|
[491] | 580 |
|
---|
| 581 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
[490] | 582 | }
|
---|
[491] | 583 |
|
---|
| 584 | #endif
|
---|
[490] | 585 | samplesIn.close();
|
---|
| 586 |
|
---|
| 587 | return true;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
[508] | 590 |
|
---|
| 591 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
[490] | 592 | {
|
---|
[491] | 593 | char fileName[100];
|
---|
[1004] | 594 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[491] | 595 |
|
---|
[490] | 596 |
|
---|
| 597 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
| 598 |
|
---|
[491] | 599 | #if USE_ASCII
|
---|
| 600 | ofstream samplesOut(fileName);
|
---|
[490] | 601 | if (!samplesOut.is_open())
|
---|
| 602 | return false;
|
---|
| 603 |
|
---|
| 604 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 605 | {
|
---|
| 606 | VssRay *ray = *it;
|
---|
[491] | 607 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 608 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 609 |
|
---|
[490] | 610 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
| 611 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
[491] | 612 | << sourceid << " " << termid << "\n";
|
---|
[490] | 613 | }
|
---|
[491] | 614 | #else
|
---|
| 615 | ofstream samplesOut(fileName, ios::binary);
|
---|
| 616 | if (!samplesOut.is_open())
|
---|
| 617 | return false;
|
---|
| 618 |
|
---|
| 619 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 620 | {
|
---|
| 621 | VssRay *ray = *it;
|
---|
| 622 | Vector3 origin(ray->GetOrigin());
|
---|
| 623 | Vector3 termination(ray->GetTermination());
|
---|
| 624 |
|
---|
| 625 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 626 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 627 |
|
---|
| 628 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 629 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 630 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
| 631 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
| 632 | }
|
---|
| 633 | #endif
|
---|
[490] | 634 | samplesOut.close();
|
---|
[1194] | 635 |
|
---|
[490] | 636 | return true;
|
---|
| 637 | }
|
---|
[563] | 638 |
|
---|
[1020] | 639 | #if 0 // matt: implemented interface samplestrategy
|
---|
[563] | 640 | bool
|
---|
| 641 | Preprocessor::GenerateRays(
|
---|
| 642 | const int number,
|
---|
| 643 | const int sampleType,
|
---|
| 644 | SimpleRayContainer &rays
|
---|
| 645 | )
|
---|
| 646 | {
|
---|
| 647 | Vector3 origin, direction;
|
---|
[837] | 648 | int startSize = (int)rays.size();
|
---|
| 649 | for (int i=0; (int)rays.size() - startSize < number; i ++) {
|
---|
[563] | 650 | // now get the direction
|
---|
| 651 | switch (sampleType) {
|
---|
| 652 | case OBJECT_BASED_DISTRIBUTION: {
|
---|
[576] | 653 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 654 | Vector3 point;
|
---|
| 655 | Vector3 normal;
|
---|
| 656 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 657 | Intersectable *object = mObjects[i];
|
---|
| 658 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 659 | direction = point - origin;
|
---|
| 660 | }
|
---|
| 661 | break;
|
---|
[576] | 662 | case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
|
---|
| 663 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 664 | Intersectable *object = mObjects[i];
|
---|
| 665 | Vector3 normal;
|
---|
| 666 | object->GetRandomSurfacePoint(origin, normal);
|
---|
| 667 | direction = UniformRandomVector(normal);
|
---|
| 668 | origin += 0.1f*direction;
|
---|
| 669 | }
|
---|
| 670 | break;
|
---|
[563] | 671 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
[576] | 672 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 673 | direction = UniformRandomVector();
|
---|
| 674 | break;
|
---|
| 675 | case DIRECTION_BOX_BASED_DISTRIBUTION: {
|
---|
[576] | 676 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 677 | float alpha = RandomValue(0.0f, 2*M_PI);
|
---|
| 678 | float beta = RandomValue(-M_PI/2, M_PI/2);
|
---|
| 679 | direction = VssRay::GetDirection(alpha, beta);
|
---|
| 680 | break;
|
---|
| 681 | }
|
---|
| 682 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
[576] | 683 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 684 | direction = mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
| 685 | break;
|
---|
| 686 | default:
|
---|
| 687 | // unsuported distribution type
|
---|
| 688 | return false;
|
---|
| 689 | }
|
---|
| 690 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 691 | float pdf = 1.0f;
|
---|
| 692 | float c = Magnitude(direction);
|
---|
| 693 | if (c > Limits::Small) {
|
---|
| 694 | direction*=1.0f/c;
|
---|
| 695 | rays.AddRay(SimpleRay(origin, direction, pdf));
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
| 698 | return true;
|
---|
| 699 | }
|
---|
[1020] | 700 | #endif
|
---|
| 701 | bool Preprocessor::GenerateRays(const int number,
|
---|
| 702 | const int sampleType,
|
---|
| 703 | SimpleRayContainer &rays)
|
---|
| 704 | {
|
---|
| 705 | Vector3 origin, direction;
|
---|
| 706 |
|
---|
| 707 | const int startSize = (int)rays.size();
|
---|
| 708 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
[860] | 709 |
|
---|
[1020] | 710 | if (!strategy)
|
---|
| 711 | return false;
|
---|
| 712 |
|
---|
| 713 | for (int i=0; (int)rays.size() - startSize < number; ++ i)
|
---|
| 714 | {
|
---|
| 715 | SimpleRay newRay;
|
---|
| 716 | bool success = strategy->GenerateSample(newRay);
|
---|
| 717 |
|
---|
| 718 | if (success)
|
---|
| 719 | rays.AddRay(newRay);
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | delete strategy;
|
---|
| 723 |
|
---|
| 724 | return true;
|
---|
[878] | 725 | }
|
---|
[1020] | 726 |
|
---|
| 727 |
|
---|
| 728 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
|
---|
| 729 | {
|
---|
| 730 | switch (strategyId)
|
---|
| 731 | {
|
---|
| 732 | case OBJECT_BASED_DISTRIBUTION:
|
---|
| 733 | return new ObjectBasedDistribution(*this);
|
---|
| 734 | case OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
| 735 | return new ObjectDirectionBasedDistribution(*this);
|
---|
| 736 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
| 737 | return new DirectionBasedDistribution(*this);
|
---|
| 738 | case DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
| 739 | return new DirectionBoxBasedDistribution(*this);
|
---|
| 740 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
| 741 | return new SpatialBoxBasedDistribution(*this);
|
---|
[1021] | 742 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
| 743 | // return new ObjectsInteriorDistribution(*this);
|
---|
[1020] | 744 | default: // no valid strategy
|
---|
| 745 | return NULL;
|
---|
| 746 | }
|
---|
[1221] | 747 |
|
---|
[1020] | 748 | // should never come here
|
---|
| 749 | return NULL;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 |
|
---|
[1221] | 753 | bool Preprocessor::InitRayCast(const string externKdTree)
|
---|
| 754 | {
|
---|
| 755 | switch (mRayCastMethod) // use intel ray tracing
|
---|
| 756 | {
|
---|
| 757 | case INTEL_RAYCASTER:
|
---|
[1232] | 758 | #ifdef INTEL_COMPILER
|
---|
[1221] | 759 | return mlrtaLoadAS(externKdTree.c_str());
|
---|
[1232] | 760 | #endif
|
---|
[1221] | 761 | case INTERNAL_RAYCASTER:
|
---|
| 762 | default:
|
---|
| 763 | break;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | return true;
|
---|
[1020] | 767 | }
|
---|
[1221] | 768 |
|
---|
| 769 | }
|
---|