[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);
|
---|
[991] | 43 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 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);
|
---|
[991] | 62 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 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);
|
---|
[991] | 80 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 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) {
|
---|
[997] | 277 | cout<<"Applying visibility filter ...";
|
---|
| 278 | cout<<"filyter width = " << mVisibilityFilterWidth << endl;
|
---|
[904] | 279 |
|
---|
[871] | 280 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
[904] | 281 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
| 282 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
[997] | 283 | cout << "done." << endl;
|
---|
[871] | 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)
|
---|
[997] | 356 | {
|
---|
[577] | 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);
|
---|
[997] | 365 |
|
---|
[577] | 366 | mViewCellsManager = CreateViewCellsManager(viewCellsStr);
|
---|
| 367 | }
|
---|
[439] | 368 |
|
---|
[997] | 369 | //-- parameters for render heuristics evaluation
|
---|
[473] | 370 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 371 |
|
---|
| 372 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 373 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 374 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
[694] | 375 |
|
---|
[473] | 376 | mRenderSimulator =
|
---|
| 377 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
[440] | 378 |
|
---|
[480] | 379 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
[440] | 380 |
|
---|
[997] | 381 | // default view space is the extent of the scene
|
---|
| 382 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
[574] | 383 |
|
---|
[538] | 384 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
[540] | 385 | {
|
---|
| 386 | // NOTE: render texture should be power of 2 and square
|
---|
| 387 | // renderer must be initialised
|
---|
| 388 | renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
[556] | 389 | // renderer->makeCurrent();
|
---|
[746] | 390 |
|
---|
[540] | 391 | }
|
---|
[496] | 392 |
|
---|
[463] | 393 | return true;
|
---|
[490] | 394 | }
|
---|
| 395 |
|
---|
| 396 |
|
---|
[575] | 397 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
| 398 | {
|
---|
| 399 | if (strcmp(name, "kdTree") == 0)
|
---|
| 400 | {
|
---|
[938] | 401 | mViewCellsManager = new KdViewCellsManager(mKdTree, environment);
|
---|
[575] | 402 | }
|
---|
| 403 | else if (strcmp(name, "bspTree") == 0)
|
---|
| 404 | {
|
---|
| 405 | Debug << "view cell type: Bsp" << endl;
|
---|
| 406 |
|
---|
[577] | 407 | mBspTree = new BspTree();
|
---|
[938] | 408 | mViewCellsManager = new BspViewCellsManager(mBspTree, environment);
|
---|
[575] | 409 | }
|
---|
| 410 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
| 411 | {
|
---|
| 412 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 413 |
|
---|
[870] | 414 | mVspBspTree = new VspBspTree(environment);
|
---|
[938] | 415 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree, environment);
|
---|
[575] | 416 | }
|
---|
| 417 | else if (strcmp(name, "vspKdTree") == 0)
|
---|
| 418 | {
|
---|
| 419 | mVspKdTree = new VspKdTree();
|
---|
| 420 |
|
---|
[938] | 421 | mViewCellsManager = new VspKdViewCellsManager(mVspKdTree, environment);
|
---|
[575] | 422 | }
|
---|
| 423 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
| 424 | {
|
---|
| 425 | //TODO
|
---|
| 426 | mBspTree = new BspTree();
|
---|
| 427 |
|
---|
| 428 | Debug << "view cell type: Bsp" << endl;
|
---|
| 429 |
|
---|
[938] | 430 | mViewCellsManager = new BspViewCellsManager(mBspTree, environment);
|
---|
[575] | 431 | }
|
---|
| 432 | else
|
---|
| 433 | {
|
---|
[664] | 434 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
[575] | 435 | exit(1);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | return mViewCellsManager;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 |
|
---|
[491] | 442 | // use ascii format to store rays
|
---|
| 443 | #define USE_ASCII 0
|
---|
| 444 |
|
---|
| 445 |
|
---|
[490] | 446 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 447 | {
|
---|
| 448 | return obj1->mId < obj2->mId;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 |
|
---|
| 452 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
| 453 | ObjectContainer &objects) const
|
---|
| 454 | {
|
---|
| 455 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 456 | char fileName[100];
|
---|
| 457 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
| 458 |
|
---|
[491] | 459 | Vector3 origin, termination;
|
---|
| 460 | // HACK: needed only for lower_bound algorithm to find the
|
---|
| 461 | // intersected objects
|
---|
| 462 | MeshInstance sObj(NULL);
|
---|
| 463 | MeshInstance tObj(NULL);
|
---|
[490] | 464 |
|
---|
[491] | 465 | #if USE_ASCII
|
---|
[656] | 466 | ifstream samplesIn(fileName);
|
---|
[490] | 467 | if (!samplesIn.is_open())
|
---|
| 468 | return false;
|
---|
| 469 |
|
---|
| 470 | string buf;
|
---|
| 471 | while (!(getline(samplesIn, buf)).eof())
|
---|
| 472 | {
|
---|
[491] | 473 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
[490] | 474 | &origin.x, &origin.y, &origin.z,
|
---|
[491] | 475 | &termination.x, &termination.y, &termination.z,
|
---|
| 476 | &(sObj.mId), &(tObj.mId));
|
---|
[490] | 477 |
|
---|
[491] | 478 | Intersectable *sourceObj = NULL;
|
---|
| 479 | Intersectable *termObj = NULL;
|
---|
| 480 |
|
---|
| 481 | if (sObj.mId >= 0)
|
---|
[490] | 482 | {
|
---|
| 483 | ObjectContainer::iterator oit =
|
---|
[491] | 484 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 485 | sourceObj = *oit;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | if (tObj.mId >= 0)
|
---|
| 489 | {
|
---|
| 490 | ObjectContainer::iterator oit =
|
---|
| 491 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 492 | termObj = *oit;
|
---|
| 493 | }
|
---|
[490] | 494 |
|
---|
[491] | 495 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
| 496 | }
|
---|
| 497 | #else
|
---|
| 498 | ifstream samplesIn(fileName, ios::binary);
|
---|
| 499 | if (!samplesIn.is_open())
|
---|
| 500 | return false;
|
---|
| 501 |
|
---|
| 502 | while (1)
|
---|
| 503 | {
|
---|
| 504 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 505 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 506 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
| 507 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
| 508 |
|
---|
| 509 | if (samplesIn.eof())
|
---|
| 510 | break;
|
---|
| 511 |
|
---|
| 512 | Intersectable *sourceObj = NULL;
|
---|
| 513 | Intersectable *termObj = NULL;
|
---|
| 514 |
|
---|
| 515 | if (sObj.mId >= 0)
|
---|
| 516 | {
|
---|
| 517 | ObjectContainer::iterator oit =
|
---|
| 518 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 519 | sourceObj = *oit;
|
---|
[490] | 520 | }
|
---|
[491] | 521 |
|
---|
| 522 | if (tObj.mId >= 0)
|
---|
[490] | 523 | {
|
---|
[491] | 524 | ObjectContainer::iterator oit =
|
---|
| 525 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 526 | termObj = *oit;
|
---|
[490] | 527 | }
|
---|
[491] | 528 |
|
---|
| 529 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
[490] | 530 | }
|
---|
[491] | 531 |
|
---|
| 532 | #endif
|
---|
[490] | 533 | samplesIn.close();
|
---|
| 534 |
|
---|
| 535 | return true;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[508] | 538 |
|
---|
| 539 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
[490] | 540 | {
|
---|
[491] | 541 | char fileName[100];
|
---|
| 542 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
| 543 |
|
---|
[490] | 544 |
|
---|
| 545 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
| 546 |
|
---|
[491] | 547 | #if USE_ASCII
|
---|
| 548 | ofstream samplesOut(fileName);
|
---|
[490] | 549 | if (!samplesOut.is_open())
|
---|
| 550 | return false;
|
---|
| 551 |
|
---|
| 552 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 553 | {
|
---|
| 554 | VssRay *ray = *it;
|
---|
[491] | 555 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 556 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 557 |
|
---|
[490] | 558 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
| 559 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
[491] | 560 | << sourceid << " " << termid << "\n";
|
---|
[490] | 561 | }
|
---|
[491] | 562 | #else
|
---|
| 563 | ofstream samplesOut(fileName, ios::binary);
|
---|
| 564 | if (!samplesOut.is_open())
|
---|
| 565 | return false;
|
---|
| 566 |
|
---|
| 567 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 568 | {
|
---|
| 569 | VssRay *ray = *it;
|
---|
| 570 | Vector3 origin(ray->GetOrigin());
|
---|
| 571 | Vector3 termination(ray->GetTermination());
|
---|
| 572 |
|
---|
| 573 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 574 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 575 |
|
---|
| 576 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 577 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 578 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
| 579 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
| 580 | }
|
---|
| 581 | #endif
|
---|
[490] | 582 | samplesOut.close();
|
---|
| 583 | return true;
|
---|
| 584 | }
|
---|
[563] | 585 |
|
---|
| 586 |
|
---|
| 587 |
|
---|
| 588 | bool
|
---|
| 589 | Preprocessor::GenerateRays(
|
---|
| 590 | const int number,
|
---|
| 591 | const int sampleType,
|
---|
| 592 | SimpleRayContainer &rays
|
---|
| 593 | )
|
---|
| 594 | {
|
---|
| 595 | Vector3 origin, direction;
|
---|
[837] | 596 | int startSize = (int)rays.size();
|
---|
| 597 | for (int i=0; (int)rays.size() - startSize < number; i ++) {
|
---|
[563] | 598 | // now get the direction
|
---|
| 599 | switch (sampleType) {
|
---|
| 600 | case OBJECT_BASED_DISTRIBUTION: {
|
---|
[576] | 601 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 602 | Vector3 point;
|
---|
| 603 | Vector3 normal;
|
---|
| 604 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 605 | Intersectable *object = mObjects[i];
|
---|
| 606 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 607 | direction = point - origin;
|
---|
| 608 | }
|
---|
| 609 | break;
|
---|
[576] | 610 | case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
|
---|
| 611 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 612 | Intersectable *object = mObjects[i];
|
---|
| 613 | Vector3 normal;
|
---|
| 614 | object->GetRandomSurfacePoint(origin, normal);
|
---|
| 615 | direction = UniformRandomVector(normal);
|
---|
| 616 | origin += 0.1f*direction;
|
---|
| 617 | }
|
---|
| 618 | break;
|
---|
[563] | 619 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
[576] | 620 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 621 | direction = UniformRandomVector();
|
---|
| 622 | break;
|
---|
| 623 | case DIRECTION_BOX_BASED_DISTRIBUTION: {
|
---|
[576] | 624 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 625 | float alpha = RandomValue(0.0f, 2*M_PI);
|
---|
| 626 | float beta = RandomValue(-M_PI/2, M_PI/2);
|
---|
| 627 | direction = VssRay::GetDirection(alpha, beta);
|
---|
| 628 | break;
|
---|
| 629 | }
|
---|
| 630 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
[576] | 631 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 632 | direction = mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
| 633 | break;
|
---|
| 634 | default:
|
---|
| 635 | // unsuported distribution type
|
---|
| 636 | return false;
|
---|
| 637 | }
|
---|
| 638 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 639 | float pdf = 1.0f;
|
---|
| 640 | float c = Magnitude(direction);
|
---|
| 641 | if (c > Limits::Small) {
|
---|
| 642 | direction*=1.0f/c;
|
---|
| 643 | rays.AddRay(SimpleRay(origin, direction, pdf));
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 | return true;
|
---|
| 647 | }
|
---|
[860] | 648 |
|
---|
[878] | 649 | }
|
---|