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