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