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