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