[372] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "Exporter.h"
|
---|
| 3 | #include "UnigraphicsParser.h"
|
---|
| 4 | #include "X3dParser.h"
|
---|
| 5 | #include "Preprocessor.h"
|
---|
| 6 | #include "ViewCell.h"
|
---|
| 7 | #include "Environment.h"
|
---|
[439] | 8 | #include "ViewCellsManager.h"
|
---|
[445] | 9 | #include "ViewCellBsp.h"
|
---|
| 10 | #include "VspBspTree.h"
|
---|
[469] | 11 | #include "RenderSimulator.h"
|
---|
[496] | 12 | #include "GlRenderer.h"
|
---|
[749] | 13 | #include "PlyParser.h"
|
---|
[1020] | 14 | #include "SamplingStrategy.h"
|
---|
[1233] | 15 | #include "VspTree.h"
|
---|
| 16 | #include "OspTree.h"
|
---|
[1221] | 17 | #include "ObjParser.h"
|
---|
[1264] | 18 | #include "BvHierarchy.h"
|
---|
[1279] | 19 | #include "HierarchyManager.h"
|
---|
[1264] | 20 |
|
---|
[1251] | 21 | #ifdef GTP_INTERNAL
|
---|
[1221] | 22 | #include "ArchModeler2MLRT.hxx"
|
---|
[1232] | 23 | #endif
|
---|
[372] | 24 |
|
---|
[863] | 25 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 26 |
|
---|
[1020] | 27 | const static bool ADDITIONAL_GEOMETRY_HACK = false;
|
---|
[860] | 28 |
|
---|
[1145] | 29 | //Preprocessor *preprocessor;
|
---|
[492] | 30 |
|
---|
[750] | 31 |
|
---|
[1001] | 32 | // HACK: Artificially modify scene to watch rendercost changes
|
---|
[750] | 33 | static void AddGeometry(SceneGraph *scene)
|
---|
| 34 | {
|
---|
[752] | 35 | scene->mRoot->UpdateBox();
|
---|
| 36 |
|
---|
[750] | 37 | AxisAlignedBox3 sceneBox = scene->GetBox();
|
---|
| 38 |
|
---|
| 39 | int n = 200;
|
---|
| 40 |
|
---|
| 41 | if (0){
|
---|
[840] | 42 | // form grid of boxes
|
---|
[750] | 43 | for (int i = 0; i < n; ++ i)
|
---|
| 44 | {
|
---|
| 45 | for (int j = 0; j < n; ++ j)
|
---|
| 46 | {
|
---|
[1135] | 47 | const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 48 |
|
---|
[840] | 49 | const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
[750] | 50 |
|
---|
[840] | 51 | const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
[750] | 52 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
[991] | 53 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 54 |
|
---|
| 55 | mesh->Preprocess();
|
---|
| 56 |
|
---|
| 57 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 58 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | for (int i = 0; i < n; ++ i)
|
---|
| 63 | {
|
---|
| 64 | for (int j = 0; j < n; ++ j)
|
---|
| 65 | {
|
---|
[1135] | 66 | const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 67 |
|
---|
| 68 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
| 69 |
|
---|
[1135] | 70 | Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f);
|
---|
[750] | 71 | AxisAlignedBox3 box(pt2, pt2 + boxSize);
|
---|
[991] | 72 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 73 |
|
---|
| 74 | mesh->Preprocess();
|
---|
| 75 |
|
---|
| 76 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 77 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | for (int i = 0; i < n; ++ i)
|
---|
| 82 | {
|
---|
[1135] | 83 | const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f);
|
---|
[750] | 84 |
|
---|
| 85 | Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min());
|
---|
| 86 |
|
---|
| 87 | //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025);
|
---|
[1135] | 88 | Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f);
|
---|
[1020] | 89 |
|
---|
[1135] | 90 | AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize);
|
---|
[991] | 91 | Mesh *mesh = CreateMeshFromBox(box);
|
---|
[750] | 92 |
|
---|
| 93 | mesh->Preprocess();
|
---|
| 94 |
|
---|
| 95 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 96 | scene->mRoot->mGeometry.push_back(mi);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | scene->mRoot->UpdateBox();
|
---|
| 100 | }
|
---|
[1221] | 101 |
|
---|
[840] | 102 | if (1)
|
---|
| 103 | {
|
---|
[1221] | 104 | // plane separating view space regions
|
---|
[1135] | 105 | const Vector3 scale(1.0f, 0.0, 0);
|
---|
[750] | 106 |
|
---|
[840] | 107 | Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min());
|
---|
[750] | 108 |
|
---|
[840] | 109 | Plane3 cuttingPlane(Vector3(1, 0, 0), pt);
|
---|
| 110 | Mesh *planeMesh = new Mesh();
|
---|
| 111 |
|
---|
| 112 | Polygon3 *poly = sceneBox.CrossSection(cuttingPlane);
|
---|
| 113 | IncludePolyInMesh(*poly, *planeMesh);
|
---|
| 114 |
|
---|
| 115 | planeMesh->Preprocess();
|
---|
| 116 |
|
---|
| 117 | MeshInstance *planeMi = new MeshInstance(planeMesh);
|
---|
| 118 | scene->mRoot->mGeometry.push_back(planeMi);
|
---|
| 119 | }
|
---|
[750] | 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
[372] | 123 | Preprocessor::Preprocessor():
|
---|
| 124 | mKdTree(NULL),
|
---|
[409] | 125 | mBspTree(NULL),
|
---|
[445] | 126 | mVspBspTree(NULL),
|
---|
[1264] | 127 | mVspTree(NULL),
|
---|
[1279] | 128 | mHierarchyManager(NULL),
|
---|
[1002] | 129 | mViewCellsManager(NULL),
|
---|
[1251] | 130 | mRenderSimulator(NULL),
|
---|
[1279] | 131 | mPass(0),
|
---|
| 132 | mRayCastMethod(0)
|
---|
[308] | 133 | {
|
---|
[1004] | 134 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
[538] | 135 |
|
---|
[840] | 136 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
| 137 | renderer = NULL;
|
---|
[496] | 138 |
|
---|
[1004] | 139 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
| 140 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes);
|
---|
| 141 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
| 142 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
| 143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
| 144 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
[1279] | 145 | //#if GTP_INTERNAL // choose other ray cast method
|
---|
[1221] | 146 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", mRayCastMethod);
|
---|
[1279] | 147 | //#endif
|
---|
[871] | 148 | char buffer[256];
|
---|
[1004] | 149 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
[871] | 150 | mVisibilityFileName = buffer;
|
---|
[1004] | 151 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter );
|
---|
| 152 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
[904] | 153 | mApplyVisibilitySpatialFilter );
|
---|
[1004] | 154 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
[878] | 155 |
|
---|
[840] | 156 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
| 157 | Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
|
---|
[372] | 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | Preprocessor::~Preprocessor()
|
---|
| 162 | {
|
---|
[752] | 163 | cout << "cleaning up" << endl;
|
---|
| 164 |
|
---|
| 165 | cout << "Deleting view cells manager ... \n";
|
---|
[496] | 166 | DEL_PTR(mViewCellsManager);
|
---|
[752] | 167 | cout << "done.\n";
|
---|
| 168 |
|
---|
| 169 | cout << "Deleting bsp tree ... \n";
|
---|
[496] | 170 | DEL_PTR(mBspTree);
|
---|
[752] | 171 | cout << "done.\n";
|
---|
| 172 |
|
---|
[840] | 173 | cout << "Deleting kd tree...\n";
|
---|
[496] | 174 | DEL_PTR(mKdTree);
|
---|
[840] | 175 | cout << "done.\n";
|
---|
[1006] | 176 |
|
---|
[1145] | 177 | cout << "Deleting vsp tree...\n";
|
---|
| 178 | DEL_PTR(mVspTree);
|
---|
[840] | 179 | cout << "done.\n";
|
---|
[752] | 180 |
|
---|
[1279] | 181 | cout << "Deleting hierarchy manager...\n";
|
---|
| 182 | DEL_PTR(mHierarchyManager);
|
---|
[1145] | 183 | cout << "done.\n";
|
---|
| 184 |
|
---|
[840] | 185 | cout << "Deleting vspbsp tree...\n";
|
---|
[496] | 186 | DEL_PTR(mVspBspTree);
|
---|
[840] | 187 | cout << "done.\n";
|
---|
[1002] | 188 |
|
---|
| 189 | cout << "Deleting scene graph...\n";
|
---|
| 190 | DEL_PTR(mSceneGraph);
|
---|
| 191 | cout << "done.\n";
|
---|
| 192 |
|
---|
| 193 | DEL_PTR(mRenderSimulator);
|
---|
| 194 | DEL_PTR(renderer);
|
---|
[372] | 195 | }
|
---|
| 196 |
|
---|
[387] | 197 | int
|
---|
[419] | 198 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
[387] | 199 | {
|
---|
| 200 | int pos = 0;
|
---|
| 201 |
|
---|
| 202 | while(1) {
|
---|
[469] | 203 | int npos = (int)str.find(';', pos);
|
---|
[387] | 204 |
|
---|
| 205 | if (npos < 0 || npos - pos < 1)
|
---|
| 206 | break;
|
---|
| 207 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 208 | pos = npos + 1;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
[440] | 212 | return (int)filenames.size();
|
---|
[387] | 213 | }
|
---|
| 214 |
|
---|
[750] | 215 |
|
---|
[372] | 216 | bool
|
---|
| 217 | Preprocessor::LoadScene(const string filename)
|
---|
| 218 | {
|
---|
[925] | 219 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
[508] | 220 | mSceneGraph = new SceneGraph;
|
---|
[372] | 221 |
|
---|
[508] | 222 | Parser *parser;
|
---|
[387] | 223 | vector<string> filenames;
|
---|
| 224 | int files = SplitFilenames(filename, filenames);
|
---|
[712] | 225 | cout << "number of input files: " << files << endl;
|
---|
[387] | 226 | bool result = false;
|
---|
| 227 | if (files == 1) {
|
---|
| 228 |
|
---|
| 229 | if (strstr(filename.c_str(), ".x3d"))
|
---|
[749] | 230 | parser = new X3dParser;
|
---|
[387] | 231 | else
|
---|
[811] | 232 | if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
[749] | 233 | parser = new PlyParser;
|
---|
[1221] | 234 | else if (strstr(filename.c_str(), ".obj"))
|
---|
| 235 | parser = new ObjParser;
|
---|
| 236 | else
|
---|
| 237 | parser = new UnigraphicsParser;
|
---|
[372] | 238 |
|
---|
[387] | 239 | cout<<filename<<endl;
|
---|
[372] | 240 |
|
---|
[1221] | 241 | if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
|
---|
[1272] | 242 | result = parser->ParseFile(filename, &mSceneGraph->mRoot,
|
---|
| 243 | mLoadPolygonsAsMeshes,
|
---|
| 244 | &mFaceParents);
|
---|
[1221] | 245 | else
|
---|
[1272] | 246 | result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
|
---|
| 247 |
|
---|
[387] | 248 | delete parser;
|
---|
[372] | 249 |
|
---|
[387] | 250 | } else {
|
---|
| 251 | // root for different files
|
---|
| 252 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
| 253 | for (int i= 0; i < filenames.size(); i++) {
|
---|
[1272] | 254 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
| 255 | parser = new X3dParser;
|
---|
| 256 | else
|
---|
| 257 | parser = new UnigraphicsParser;
|
---|
| 258 |
|
---|
| 259 | SceneGraphNode *node;
|
---|
| 260 | bool success;
|
---|
| 261 |
|
---|
| 262 | if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
|
---|
| 263 | success = parser->ParseFile(filename, &node,
|
---|
| 264 | mLoadPolygonsAsMeshes,
|
---|
| 265 | &mFaceParents);
|
---|
| 266 | else
|
---|
| 267 | success = parser->ParseFile(filename, &node, mLoadPolygonsAsMeshes);
|
---|
| 268 |
|
---|
| 269 | if (success) {
|
---|
| 270 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
| 271 | // at least one file parsed
|
---|
| 272 | result = true;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | delete parser;
|
---|
[387] | 276 | }
|
---|
| 277 | }
|
---|
[372] | 278 |
|
---|
[1272] | 279 |
|
---|
[752] | 280 | if (result)
|
---|
[1272] | 281 | {
|
---|
[752] | 282 | // HACK
|
---|
[1020] | 283 | if (ADDITIONAL_GEOMETRY_HACK)
|
---|
| 284 | AddGeometry(mSceneGraph);
|
---|
[752] | 285 |
|
---|
[1020] | 286 | mSceneGraph->AssignObjectIds();
|
---|
| 287 |
|
---|
| 288 | int intersectables, faces;
|
---|
| 289 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
| 290 |
|
---|
| 291 | cout<<filename<<" parsed successfully."<<endl;
|
---|
| 292 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
| 293 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
| 294 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 295 | mSceneGraph->mRoot->UpdateBox();
|
---|
[752] | 296 |
|
---|
[1020] | 297 | if (0)
|
---|
| 298 | {
|
---|
| 299 | Exporter *exporter = Exporter::GetExporter("testload.x3d");
|
---|
[658] | 300 |
|
---|
[1020] | 301 | if (exporter)
|
---|
| 302 | {
|
---|
| 303 | exporter->ExportGeometry(mObjects);
|
---|
| 304 | delete exporter;
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
[387] | 307 | }
|
---|
[372] | 308 |
|
---|
[492] | 309 |
|
---|
| 310 | return result;
|
---|
[372] | 311 | }
|
---|
| 312 |
|
---|
| 313 | bool
|
---|
| 314 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
| 315 | {
|
---|
[871] | 316 |
|
---|
[931] | 317 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
[871] | 318 |
|
---|
| 319 | return true;
|
---|
[372] | 320 | }
|
---|
| 321 |
|
---|
| 322 | bool
|
---|
[871] | 323 | Preprocessor::PostProcessVisibility()
|
---|
| 324 | {
|
---|
| 325 |
|
---|
[904] | 326 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
[997] | 327 | cout<<"Applying visibility filter ...";
|
---|
[1002] | 328 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
[904] | 329 |
|
---|
[1002] | 330 | if (!mViewCellsManager)
|
---|
[1199] | 331 | return false;
|
---|
| 332 |
|
---|
[871] | 333 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
[904] | 334 | mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
|
---|
| 335 | mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
|
---|
[997] | 336 | cout << "done." << endl;
|
---|
[871] | 337 | }
|
---|
| 338 |
|
---|
| 339 | // export the preprocessed information to a file
|
---|
| 340 | if (mExportVisibility)
|
---|
| 341 | ExportPreprocessedData(mVisibilityFileName);
|
---|
| 342 |
|
---|
| 343 | return true;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | bool
|
---|
[372] | 348 | Preprocessor::BuildKdTree()
|
---|
| 349 | {
|
---|
| 350 | mKdTree = new KdTree;
|
---|
| 351 | // add mesh instances of the scene graph to the root of the tree
|
---|
| 352 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
| 353 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
| 354 |
|
---|
[1201] | 355 | long startTime = GetTime();
|
---|
| 356 |
|
---|
| 357 | cout << "building kd tree ... " << endl;
|
---|
[372] | 358 | mKdTree->Construct();
|
---|
[1201] | 359 | cout << "construction finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs " << endl;
|
---|
[372] | 360 | return true;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | void
|
---|
| 364 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
| 365 | {
|
---|
| 366 | s<<mKdTree->GetStatistics();
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | void
|
---|
| 370 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
| 371 | {
|
---|
| 372 | s << mBspTree->GetStatistics();
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | bool
|
---|
| 376 | Preprocessor::Export( const string filename,
|
---|
[492] | 377 | const bool scene,
|
---|
| 378 | const bool kdtree,
|
---|
| 379 | const bool bsptree
|
---|
| 380 | )
|
---|
[372] | 381 | {
|
---|
| 382 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 383 |
|
---|
| 384 | if (exporter) {
|
---|
| 385 | if (scene)
|
---|
| 386 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 387 |
|
---|
| 388 | if (kdtree) {
|
---|
| 389 | exporter->SetWireframe();
|
---|
| 390 | exporter->ExportKdTree(*mKdTree);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | if (bsptree) {
|
---|
| 394 | //exporter->SetWireframe();
|
---|
| 395 | exporter->ExportBspTree(*mBspTree);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | delete exporter;
|
---|
| 399 | return true;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | return false;
|
---|
| 403 | }
|
---|
[429] | 404 |
|
---|
[508] | 405 |
|
---|
[463] | 406 | bool Preprocessor::PrepareViewCells()
|
---|
| 407 | {
|
---|
[577] | 408 | //-- parse view cells construction method
|
---|
[1004] | 409 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
[577] | 410 | char buf[100];
|
---|
| 411 |
|
---|
| 412 | if (mLoadViewCells)
|
---|
[997] | 413 | {
|
---|
[1004] | 414 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
[1264] | 415 | cout << "loading view cells from " << buf << endl;
|
---|
[1004] | 416 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true);
|
---|
[577] | 417 | }
|
---|
| 418 | else
|
---|
| 419 | {
|
---|
| 420 | //-- parse type of view cell container
|
---|
[1264] | 421 |
|
---|
[1004] | 422 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
[1002] | 423 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
[1112] | 424 |
|
---|
| 425 | // default view space is the extent of the scene
|
---|
| 426 | mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
|
---|
| 427 |
|
---|
| 428 |
|
---|
[577] | 429 | }
|
---|
[1112] | 430 |
|
---|
[997] | 431 | //-- parameters for render heuristics evaluation
|
---|
[473] | 432 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 433 |
|
---|
[1004] | 434 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 435 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 436 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
[694] | 437 |
|
---|
[473] | 438 | mRenderSimulator =
|
---|
| 439 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
[440] | 440 |
|
---|
[480] | 441 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
[440] | 442 |
|
---|
[574] | 443 |
|
---|
[538] | 444 | if (mUseGlRenderer || mUseGlDebugger)
|
---|
[540] | 445 | {
|
---|
| 446 | // NOTE: render texture should be power of 2 and square
|
---|
| 447 | // renderer must be initialised
|
---|
[1145] | 448 | // $$matt
|
---|
| 449 | // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
|
---|
[556] | 450 | // renderer->makeCurrent();
|
---|
[746] | 451 |
|
---|
[540] | 452 | }
|
---|
[496] | 453 |
|
---|
[463] | 454 | return true;
|
---|
[490] | 455 | }
|
---|
| 456 |
|
---|
| 457 |
|
---|
[575] | 458 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
| 459 | {
|
---|
[1264] | 460 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
| 461 |
|
---|
[575] | 462 | if (strcmp(name, "kdTree") == 0)
|
---|
| 463 | {
|
---|
[1264] | 464 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
[575] | 465 | }
|
---|
| 466 | else if (strcmp(name, "bspTree") == 0)
|
---|
| 467 | {
|
---|
| 468 | Debug << "view cell type: Bsp" << endl;
|
---|
| 469 |
|
---|
[577] | 470 | mBspTree = new BspTree();
|
---|
[1264] | 471 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
[575] | 472 | }
|
---|
| 473 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
| 474 | {
|
---|
| 475 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 476 |
|
---|
[1004] | 477 | mVspBspTree = new VspBspTree();
|
---|
[1264] | 478 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
[575] | 479 | }
|
---|
[1006] | 480 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
[575] | 481 | {
|
---|
[1022] | 482 | mVspTree = new VspTree();
|
---|
[1279] | 483 |
|
---|
[1174] | 484 | // HACK for testing if per kd evaluation works!!
|
---|
[1279] | 485 | const bool ishack = true;
|
---|
[1022] | 486 |
|
---|
[1279] | 487 | if (ishack)
|
---|
| 488 | {
|
---|
| 489 | mHierarchyManager = new HierarchyManager(mVspTree, mKdTree);
|
---|
| 490 | }
|
---|
| 491 | else
|
---|
| 492 | {
|
---|
| 493 | mHierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | mViewCellsManager = new VspOspViewCellsManager(vcTree, mHierarchyManager);
|
---|
[575] | 497 | }
|
---|
| 498 | else if (strcmp(name, "sceneDependent") == 0)
|
---|
| 499 | {
|
---|
[1143] | 500 | Debug << "view cell type: Bsp" << endl;
|
---|
| 501 |
|
---|
[575] | 502 | //TODO
|
---|
| 503 | mBspTree = new BspTree();
|
---|
[1264] | 504 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
[575] | 505 | }
|
---|
| 506 | else
|
---|
| 507 | {
|
---|
[664] | 508 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
[575] | 509 | exit(1);
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | return mViewCellsManager;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 |
|
---|
[491] | 516 | // use ascii format to store rays
|
---|
| 517 | #define USE_ASCII 0
|
---|
| 518 |
|
---|
| 519 |
|
---|
[1145] | 520 | static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
[490] | 521 | {
|
---|
| 522 | return obj1->mId < obj2->mId;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 |
|
---|
[1197] | 526 | bool Preprocessor::LoadKdTree()
|
---|
| 527 | {
|
---|
| 528 | return true;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | bool Preprocessor::ExportKdTree()
|
---|
| 532 | {
|
---|
| 533 | return true;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 |
|
---|
[490] | 537 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
| 538 | ObjectContainer &objects) const
|
---|
| 539 | {
|
---|
| 540 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 541 | char fileName[100];
|
---|
[1004] | 542 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[490] | 543 |
|
---|
[491] | 544 | Vector3 origin, termination;
|
---|
| 545 | // HACK: needed only for lower_bound algorithm to find the
|
---|
| 546 | // intersected objects
|
---|
| 547 | MeshInstance sObj(NULL);
|
---|
| 548 | MeshInstance tObj(NULL);
|
---|
[490] | 549 |
|
---|
[491] | 550 | #if USE_ASCII
|
---|
[656] | 551 | ifstream samplesIn(fileName);
|
---|
[490] | 552 | if (!samplesIn.is_open())
|
---|
| 553 | return false;
|
---|
| 554 |
|
---|
| 555 | string buf;
|
---|
| 556 | while (!(getline(samplesIn, buf)).eof())
|
---|
| 557 | {
|
---|
[491] | 558 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
[490] | 559 | &origin.x, &origin.y, &origin.z,
|
---|
[491] | 560 | &termination.x, &termination.y, &termination.z,
|
---|
| 561 | &(sObj.mId), &(tObj.mId));
|
---|
[490] | 562 |
|
---|
[491] | 563 | Intersectable *sourceObj = NULL;
|
---|
| 564 | Intersectable *termObj = NULL;
|
---|
| 565 |
|
---|
| 566 | if (sObj.mId >= 0)
|
---|
[490] | 567 | {
|
---|
| 568 | ObjectContainer::iterator oit =
|
---|
[491] | 569 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 570 | sourceObj = *oit;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | if (tObj.mId >= 0)
|
---|
| 574 | {
|
---|
| 575 | ObjectContainer::iterator oit =
|
---|
| 576 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 577 | termObj = *oit;
|
---|
| 578 | }
|
---|
[490] | 579 |
|
---|
[491] | 580 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
| 581 | }
|
---|
| 582 | #else
|
---|
| 583 | ifstream samplesIn(fileName, ios::binary);
|
---|
| 584 | if (!samplesIn.is_open())
|
---|
| 585 | return false;
|
---|
| 586 |
|
---|
| 587 | while (1)
|
---|
| 588 | {
|
---|
| 589 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 590 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 591 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
| 592 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
| 593 |
|
---|
| 594 | if (samplesIn.eof())
|
---|
| 595 | break;
|
---|
| 596 |
|
---|
| 597 | Intersectable *sourceObj = NULL;
|
---|
| 598 | Intersectable *termObj = NULL;
|
---|
| 599 |
|
---|
| 600 | if (sObj.mId >= 0)
|
---|
| 601 | {
|
---|
| 602 | ObjectContainer::iterator oit =
|
---|
| 603 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 604 | sourceObj = *oit;
|
---|
[490] | 605 | }
|
---|
[491] | 606 |
|
---|
| 607 | if (tObj.mId >= 0)
|
---|
[490] | 608 | {
|
---|
[491] | 609 | ObjectContainer::iterator oit =
|
---|
| 610 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 611 | termObj = *oit;
|
---|
[490] | 612 | }
|
---|
[491] | 613 |
|
---|
| 614 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
[490] | 615 | }
|
---|
[491] | 616 |
|
---|
| 617 | #endif
|
---|
[490] | 618 | samplesIn.close();
|
---|
| 619 |
|
---|
| 620 | return true;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[508] | 623 |
|
---|
| 624 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
[490] | 625 | {
|
---|
[491] | 626 | char fileName[100];
|
---|
[1004] | 627 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[491] | 628 |
|
---|
[490] | 629 |
|
---|
| 630 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
| 631 |
|
---|
[491] | 632 | #if USE_ASCII
|
---|
| 633 | ofstream samplesOut(fileName);
|
---|
[490] | 634 | if (!samplesOut.is_open())
|
---|
| 635 | return false;
|
---|
| 636 |
|
---|
| 637 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 638 | {
|
---|
| 639 | VssRay *ray = *it;
|
---|
[491] | 640 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 641 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 642 |
|
---|
[490] | 643 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
| 644 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
[491] | 645 | << sourceid << " " << termid << "\n";
|
---|
[490] | 646 | }
|
---|
[491] | 647 | #else
|
---|
| 648 | ofstream samplesOut(fileName, ios::binary);
|
---|
| 649 | if (!samplesOut.is_open())
|
---|
| 650 | return false;
|
---|
| 651 |
|
---|
| 652 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 653 | {
|
---|
| 654 | VssRay *ray = *it;
|
---|
| 655 | Vector3 origin(ray->GetOrigin());
|
---|
| 656 | Vector3 termination(ray->GetTermination());
|
---|
| 657 |
|
---|
| 658 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 659 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 660 |
|
---|
| 661 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 662 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 663 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
| 664 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
| 665 | }
|
---|
| 666 | #endif
|
---|
[490] | 667 | samplesOut.close();
|
---|
[1194] | 668 |
|
---|
[490] | 669 | return true;
|
---|
| 670 | }
|
---|
[563] | 671 |
|
---|
[1020] | 672 | #if 0 // matt: implemented interface samplestrategy
|
---|
[563] | 673 | bool
|
---|
| 674 | Preprocessor::GenerateRays(
|
---|
| 675 | const int number,
|
---|
| 676 | const int sampleType,
|
---|
| 677 | SimpleRayContainer &rays
|
---|
| 678 | )
|
---|
| 679 | {
|
---|
| 680 | Vector3 origin, direction;
|
---|
[837] | 681 | int startSize = (int)rays.size();
|
---|
| 682 | for (int i=0; (int)rays.size() - startSize < number; i ++) {
|
---|
[563] | 683 | // now get the direction
|
---|
| 684 | switch (sampleType) {
|
---|
| 685 | case OBJECT_BASED_DISTRIBUTION: {
|
---|
[576] | 686 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 687 | Vector3 point;
|
---|
| 688 | Vector3 normal;
|
---|
| 689 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 690 | Intersectable *object = mObjects[i];
|
---|
| 691 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 692 | direction = point - origin;
|
---|
| 693 | }
|
---|
| 694 | break;
|
---|
[576] | 695 | case OBJECT_DIRECTION_BASED_DISTRIBUTION: {
|
---|
| 696 | int i = RandomValue(0, mObjects.size() - 1);
|
---|
| 697 | Intersectable *object = mObjects[i];
|
---|
| 698 | Vector3 normal;
|
---|
| 699 | object->GetRandomSurfacePoint(origin, normal);
|
---|
| 700 | direction = UniformRandomVector(normal);
|
---|
| 701 | origin += 0.1f*direction;
|
---|
| 702 | }
|
---|
| 703 | break;
|
---|
[563] | 704 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
[576] | 705 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 706 | direction = UniformRandomVector();
|
---|
| 707 | break;
|
---|
| 708 | case DIRECTION_BOX_BASED_DISTRIBUTION: {
|
---|
[576] | 709 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 710 | float alpha = RandomValue(0.0f, 2*M_PI);
|
---|
| 711 | float beta = RandomValue(-M_PI/2, M_PI/2);
|
---|
| 712 | direction = VssRay::GetDirection(alpha, beta);
|
---|
| 713 | break;
|
---|
| 714 | }
|
---|
| 715 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
[576] | 716 | mViewCellsManager->GetViewPoint(origin);
|
---|
[563] | 717 | direction = mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
| 718 | break;
|
---|
| 719 | default:
|
---|
| 720 | // unsuported distribution type
|
---|
| 721 | return false;
|
---|
| 722 | }
|
---|
| 723 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 724 | float pdf = 1.0f;
|
---|
| 725 | float c = Magnitude(direction);
|
---|
| 726 | if (c > Limits::Small) {
|
---|
| 727 | direction*=1.0f/c;
|
---|
| 728 | rays.AddRay(SimpleRay(origin, direction, pdf));
|
---|
| 729 | }
|
---|
| 730 | }
|
---|
| 731 | return true;
|
---|
| 732 | }
|
---|
[1020] | 733 | #endif
|
---|
| 734 | bool Preprocessor::GenerateRays(const int number,
|
---|
| 735 | const int sampleType,
|
---|
| 736 | SimpleRayContainer &rays)
|
---|
| 737 | {
|
---|
| 738 | Vector3 origin, direction;
|
---|
| 739 |
|
---|
| 740 | const int startSize = (int)rays.size();
|
---|
| 741 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
[860] | 742 |
|
---|
[1020] | 743 | if (!strategy)
|
---|
| 744 | return false;
|
---|
| 745 |
|
---|
| 746 | for (int i=0; (int)rays.size() - startSize < number; ++ i)
|
---|
| 747 | {
|
---|
| 748 | SimpleRay newRay;
|
---|
| 749 | bool success = strategy->GenerateSample(newRay);
|
---|
| 750 |
|
---|
| 751 | if (success)
|
---|
| 752 | rays.AddRay(newRay);
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | delete strategy;
|
---|
| 756 |
|
---|
| 757 | return true;
|
---|
[878] | 758 | }
|
---|
[1020] | 759 |
|
---|
| 760 |
|
---|
| 761 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
|
---|
| 762 | {
|
---|
| 763 | switch (strategyId)
|
---|
| 764 | {
|
---|
| 765 | case OBJECT_BASED_DISTRIBUTION:
|
---|
| 766 | return new ObjectBasedDistribution(*this);
|
---|
| 767 | case OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
| 768 | return new ObjectDirectionBasedDistribution(*this);
|
---|
| 769 | case DIRECTION_BASED_DISTRIBUTION:
|
---|
| 770 | return new DirectionBasedDistribution(*this);
|
---|
| 771 | case DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
| 772 | return new DirectionBoxBasedDistribution(*this);
|
---|
| 773 | case SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
| 774 | return new SpatialBoxBasedDistribution(*this);
|
---|
[1021] | 775 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
| 776 | // return new ObjectsInteriorDistribution(*this);
|
---|
[1020] | 777 | default: // no valid strategy
|
---|
[1279] | 778 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
[1020] | 779 | return NULL;
|
---|
| 780 | }
|
---|
[1221] | 781 |
|
---|
[1020] | 782 | // should never come here
|
---|
| 783 | return NULL;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 |
|
---|
[1221] | 787 | bool Preprocessor::InitRayCast(const string externKdTree)
|
---|
| 788 | {
|
---|
| 789 | switch (mRayCastMethod) // use intel ray tracing
|
---|
| 790 | {
|
---|
| 791 | case INTEL_RAYCASTER:
|
---|
[1251] | 792 | #ifdef GTP_INTERNAL
|
---|
[1272] | 793 | cout<<"Ray Cast file: "<<externKdTree<<endl;
|
---|
| 794 | return mlrtaLoadAS(externKdTree.c_str());
|
---|
[1232] | 795 | #endif
|
---|
[1221] | 796 | case INTERNAL_RAYCASTER:
|
---|
| 797 | default:
|
---|
| 798 | break;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | return true;
|
---|
[1020] | 802 | }
|
---|
[1221] | 803 |
|
---|
[1251] | 804 |
|
---|
| 805 | int Preprocessor::CastIntelDoubleRay(
|
---|
| 806 | const Vector3 &viewPoint,
|
---|
| 807 | const Vector3 &direction,
|
---|
| 808 | const float probability,
|
---|
| 809 | VssRayContainer &vssRays,
|
---|
| 810 | const AxisAlignedBox3 &box
|
---|
| 811 | )
|
---|
| 812 | {
|
---|
| 813 | VssRay *vssRay = NULL;
|
---|
| 814 | int hits = 0;
|
---|
| 815 |
|
---|
| 816 | Vector3 pointA, pointB;
|
---|
| 817 |
|
---|
| 818 | Intersectable *objectA =
|
---|
| 819 | CastIntelSingleRay(viewPoint, direction, pointA, box);
|
---|
| 820 |
|
---|
| 821 | // cast ray into other direction
|
---|
| 822 | Intersectable *objectB =
|
---|
| 823 | CastIntelSingleRay(viewPoint, -direction, pointB, box);
|
---|
| 824 |
|
---|
| 825 | const bool validSample = (objectA != objectB);
|
---|
| 826 |
|
---|
| 827 | if (validSample)
|
---|
| 828 | {
|
---|
| 829 | if (objectA)
|
---|
| 830 | {
|
---|
| 831 | vssRay = new VssRay(pointB,
|
---|
| 832 | pointA,
|
---|
| 833 | objectB,
|
---|
| 834 | objectA,
|
---|
| 835 | probability,
|
---|
| 836 | mPass);
|
---|
| 837 |
|
---|
| 838 | vssRays.push_back(vssRay);
|
---|
| 839 | hits ++;
|
---|
| 840 | }
|
---|
| 841 |
|
---|
| 842 | if (objectB)
|
---|
| 843 | {
|
---|
| 844 | vssRay = new VssRay(pointA,
|
---|
| 845 | pointB,
|
---|
| 846 | objectA,
|
---|
| 847 | objectB,
|
---|
| 848 | probability,
|
---|
| 849 | mPass);
|
---|
| 850 |
|
---|
| 851 | vssRays.push_back(vssRay);
|
---|
| 852 | hits ++;
|
---|
| 853 | }
|
---|
| 854 | //Debug << "intel ray: " << *vssRay << endl;
|
---|
| 855 | }
|
---|
[1279] | 856 |
|
---|
[1251] | 857 | //cout << "a";
|
---|
| 858 | return hits;
|
---|
[1221] | 859 | }
|
---|
[1251] | 860 |
|
---|
| 861 |
|
---|
| 862 | Intersectable *Preprocessor::CastIntelSingleRay(const Vector3 &viewPoint,
|
---|
| 863 | const Vector3 &direction,
|
---|
| 864 | //const float probability,
|
---|
| 865 | Vector3 &tPoint,
|
---|
| 866 | const AxisAlignedBox3 &box
|
---|
| 867 | )
|
---|
| 868 | {
|
---|
| 869 | AxisAlignedBox3 sbox = box;
|
---|
| 870 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 871 |
|
---|
| 872 | if (!sbox.IsInside(viewPoint))
|
---|
| 873 | return 0;
|
---|
| 874 |
|
---|
| 875 | float pforg[3];
|
---|
| 876 | float pfdir[3];
|
---|
[1272] | 877 | double pfnorm[3];
|
---|
[1251] | 878 |
|
---|
| 879 | pforg[0] = viewPoint[0]; pforg[1] = viewPoint[1]; pforg[2] = viewPoint[2];
|
---|
| 880 | pfdir[0] = direction[0]; pfdir[1] = direction[1]; pfdir[2] = direction[2];
|
---|
| 881 |
|
---|
| 882 | float dist = 0;
|
---|
| 883 | #ifdef GTP_INTERNAL
|
---|
| 884 | const int hittriangle = mlrtaIntersectAS(pforg, pfdir, pfnorm, dist);
|
---|
| 885 | #else
|
---|
| 886 | const int hittriangle = -1;
|
---|
| 887 | #endif
|
---|
| 888 |
|
---|
| 889 | if (hittriangle == -1)
|
---|
| 890 | {
|
---|
| 891 | static Ray ray;
|
---|
| 892 | SetupRay(ray, viewPoint, direction);
|
---|
| 893 |
|
---|
| 894 | float tmin = 0, tmax;
|
---|
| 895 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 896 | {
|
---|
| 897 | tPoint = ray.Extrap(tmax);
|
---|
| 898 | }
|
---|
| 899 |
|
---|
| 900 | return NULL;
|
---|
| 901 | }
|
---|
| 902 | else
|
---|
| 903 | {
|
---|
| 904 | tPoint[0] = pforg[0] + pfdir[0] * dist;
|
---|
| 905 | tPoint[1] = pforg[1] + pfdir[1] * dist;
|
---|
| 906 | tPoint[2] = pforg[2] + pfdir[2] * dist;
|
---|
| 907 |
|
---|
[1281] | 908 | return mFaceParents[hittriangle].mObject;
|
---|
[1251] | 909 | }
|
---|
| 910 | }
|
---|
| 911 |
|
---|
| 912 |
|
---|
| 913 | int Preprocessor::CastInternalRay(
|
---|
| 914 | const Vector3 &viewPoint,
|
---|
| 915 | const Vector3 &direction,
|
---|
| 916 | const float probability,
|
---|
| 917 | VssRayContainer &vssRays,
|
---|
| 918 | const AxisAlignedBox3 &box
|
---|
| 919 | )
|
---|
| 920 | {
|
---|
| 921 |
|
---|
[1272] | 922 | int hits = 0;
|
---|
| 923 | static Ray ray;
|
---|
| 924 | Intersectable *objectA, *objectB;
|
---|
| 925 | Vector3 pointA, pointB;
|
---|
[1251] | 926 |
|
---|
[1272] | 927 | // AxisAlignedBox3 box = Union(mKdTree->GetBox(), mViewCellsManager->GetViewSpaceBox());
|
---|
| 928 |
|
---|
| 929 |
|
---|
| 930 | AxisAlignedBox3 sbox = box;
|
---|
| 931 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 932 | if (!sbox.IsInside(viewPoint))
|
---|
| 933 | return 0;
|
---|
[1251] | 934 |
|
---|
[1272] | 935 | SetupRay(ray, viewPoint, direction);
|
---|
| 936 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[1251] | 937 |
|
---|
[1272] | 938 | // cast ray to KD tree to find intersection with other objects
|
---|
| 939 | float bsize = Magnitude(box.Size());
|
---|
| 940 |
|
---|
| 941 |
|
---|
| 942 | if (mKdTree->CastRay(ray)) {
|
---|
| 943 | objectA = ray.intersections[0].mObject;
|
---|
| 944 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
| 945 | if (mDetectEmptyViewSpace)
|
---|
| 946 | if (DotProd(ray.intersections[0].mNormal, direction) >= 0) {
|
---|
| 947 | // discard the sample
|
---|
| 948 | return 0;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | } else {
|
---|
| 952 | objectA = NULL;
|
---|
| 953 | // compute intersection with the scene bounding box
|
---|
| 954 | float tmin, tmax;
|
---|
| 955 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 956 | pointA = ray.Extrap(tmax);
|
---|
[1251] | 957 | else
|
---|
[1272] | 958 | return 0;
|
---|
| 959 | }
|
---|
[1251] | 960 |
|
---|
| 961 |
|
---|
[1272] | 962 | SetupRay(ray, viewPoint, -direction);
|
---|
| 963 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
| 964 |
|
---|
| 965 | if (mKdTree->CastRay(ray)) {
|
---|
| 966 | objectB = ray.intersections[0].mObject;
|
---|
| 967 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
| 968 | if (mDetectEmptyViewSpace)
|
---|
| 969 | if (DotProd(ray.intersections[0].mNormal, direction) <= 0) {
|
---|
| 970 | // discard the sample
|
---|
| 971 | return 0;
|
---|
| 972 | }
|
---|
| 973 | } else {
|
---|
| 974 | objectB = NULL;
|
---|
| 975 | float tmin, tmax;
|
---|
| 976 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 977 | pointB = ray.Extrap(tmax);
|
---|
[1251] | 978 | else
|
---|
[1272] | 979 | return 0;
|
---|
| 980 | }
|
---|
[1251] | 981 |
|
---|
| 982 |
|
---|
[1272] | 983 | VssRay *vssRay = NULL;
|
---|
| 984 | bool validSample = (objectA != objectB);
|
---|
| 985 | if (validSample) {
|
---|
| 986 | if (objectA) {
|
---|
| 987 | vssRay = new VssRay(pointB,
|
---|
| 988 | pointA,
|
---|
| 989 | objectB,
|
---|
| 990 | objectA,
|
---|
| 991 | mPass,
|
---|
| 992 | probability
|
---|
| 993 | );
|
---|
| 994 | vssRays.push_back(vssRay);
|
---|
| 995 | hits ++;
|
---|
[1251] | 996 | }
|
---|
| 997 |
|
---|
[1272] | 998 | if (objectB) {
|
---|
| 999 | vssRay = new VssRay(pointA,
|
---|
| 1000 | pointB,
|
---|
| 1001 | objectA,
|
---|
| 1002 | objectB,
|
---|
| 1003 | mPass,
|
---|
| 1004 | probability
|
---|
| 1005 | );
|
---|
| 1006 | vssRays.push_back(vssRay);
|
---|
| 1007 | hits ++;
|
---|
[1251] | 1008 | }
|
---|
[1272] | 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | return hits;
|
---|
[1251] | 1012 | }
|
---|
| 1013 |
|
---|
| 1014 |
|
---|
[1281] | 1015 | int
|
---|
| 1016 | Preprocessor::ProcessRay(
|
---|
[1283] | 1017 | const Vector3 &viewPoint,
|
---|
| 1018 | const Vector3 &direction,
|
---|
[1281] | 1019 | Intersectable *objectA,
|
---|
| 1020 | Vector3 &pointA,
|
---|
[1283] | 1021 | const Vector3 &normalA,
|
---|
[1281] | 1022 | Intersectable *objectB,
|
---|
| 1023 | Vector3 &pointB,
|
---|
[1283] | 1024 | const Vector3 &normalB,
|
---|
[1281] | 1025 | const float probability,
|
---|
| 1026 | VssRayContainer &vssRays,
|
---|
| 1027 | const AxisAlignedBox3 &box
|
---|
| 1028 | )
|
---|
| 1029 | {
|
---|
| 1030 | int hits=0;
|
---|
| 1031 |
|
---|
| 1032 | if (objectA == NULL && objectB == NULL)
|
---|
| 1033 | return 0;
|
---|
| 1034 |
|
---|
| 1035 | AxisAlignedBox3 sbox = box;
|
---|
| 1036 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 1037 |
|
---|
| 1038 | if (!sbox.IsInside(viewPoint))
|
---|
| 1039 | return 0;
|
---|
| 1040 |
|
---|
| 1041 | if (objectA == NULL) {
|
---|
| 1042 | // compute intersection with the scene bounding box
|
---|
| 1043 | static Ray ray;
|
---|
| 1044 | SetupRay(ray, viewPoint, direction);
|
---|
| 1045 |
|
---|
| 1046 | float tmin, tmax;
|
---|
| 1047 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 1048 | pointA = ray.Extrap(tmax);
|
---|
| 1049 | else
|
---|
| 1050 | return 0;
|
---|
| 1051 | } else {
|
---|
| 1052 | if (mDetectEmptyViewSpace)
|
---|
| 1053 | if (DotProd(normalA, direction) >= 0) {
|
---|
| 1054 | // discard the sample
|
---|
| 1055 | return 0;
|
---|
| 1056 | }
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | if (objectB == NULL) {
|
---|
| 1060 | // compute intersection with the scene bounding box
|
---|
| 1061 | static Ray ray;
|
---|
| 1062 | SetupRay(ray, viewPoint, -direction);
|
---|
| 1063 |
|
---|
| 1064 | float tmin, tmax;
|
---|
| 1065 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 1066 | pointB = ray.Extrap(tmax);
|
---|
| 1067 | else
|
---|
| 1068 | return 0;
|
---|
| 1069 | } else {
|
---|
| 1070 | if (mDetectEmptyViewSpace)
|
---|
| 1071 | if (DotProd(normalB, direction) <= 0) {
|
---|
| 1072 | // discard the sample
|
---|
| 1073 | return 0;
|
---|
| 1074 | }
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | VssRay *vssRay = NULL;
|
---|
| 1078 | bool validSample = (objectA != objectB);
|
---|
| 1079 | if (validSample) {
|
---|
| 1080 | if (objectA) {
|
---|
| 1081 | vssRay = new VssRay(pointB,
|
---|
| 1082 | pointA,
|
---|
| 1083 | objectB,
|
---|
| 1084 | objectA,
|
---|
| 1085 | mPass,
|
---|
| 1086 | probability
|
---|
| 1087 | );
|
---|
| 1088 | vssRays.push_back(vssRay);
|
---|
| 1089 | hits ++;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | if (objectB) {
|
---|
| 1093 | vssRay = new VssRay(pointA,
|
---|
| 1094 | pointB,
|
---|
| 1095 | objectA,
|
---|
| 1096 | objectB,
|
---|
| 1097 | mPass,
|
---|
| 1098 | probability
|
---|
| 1099 | );
|
---|
| 1100 | vssRays.push_back(vssRay);
|
---|
| 1101 | hits ++;
|
---|
| 1102 | }
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | return hits;
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | void
|
---|
| 1109 | Preprocessor::CastRays16(const int index,
|
---|
| 1110 | SimpleRayContainer &rays,
|
---|
| 1111 | VssRayContainer &vssRays,
|
---|
| 1112 | const AxisAlignedBox3 &sbox)
|
---|
| 1113 | {
|
---|
| 1114 | int i;
|
---|
| 1115 | int num = 16;
|
---|
| 1116 |
|
---|
| 1117 | if (mRayCastMethod == INTEL_RAYCASTER) {
|
---|
| 1118 | #ifdef GTP_INTERNAL
|
---|
| 1119 |
|
---|
| 1120 | int forward_hit_triangles[16];
|
---|
| 1121 | float forward_dist[16];
|
---|
| 1122 |
|
---|
| 1123 | int backward_hit_triangles[16];
|
---|
| 1124 | float backward_dist[16];
|
---|
| 1125 |
|
---|
| 1126 | Vector3 min = sbox.Min();
|
---|
| 1127 | Vector3 max = sbox.Max();
|
---|
| 1128 |
|
---|
| 1129 | for (i=0; i < num; i++) {
|
---|
| 1130 | mlrtaStoreRayAS16(&rays[index + i].mOrigin.x,
|
---|
| 1131 | &rays[index + i].mDirection.x,
|
---|
| 1132 | i);
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
| 1135 |
|
---|
| 1136 | mlrtaTraverseGroupAS16(&min.x,
|
---|
| 1137 | &max.x,
|
---|
| 1138 | forward_hit_triangles,
|
---|
| 1139 | forward_dist);
|
---|
| 1140 |
|
---|
| 1141 | for (i=0; i < num; i++) {
|
---|
| 1142 | Vector3 dir = -rays[index+i].mDirection;
|
---|
| 1143 | mlrtaStoreRayAS16(&rays[index+i].mOrigin.x,
|
---|
| 1144 | &dir.x,
|
---|
| 1145 | i);
|
---|
| 1146 | }
|
---|
| 1147 |
|
---|
| 1148 |
|
---|
| 1149 | mlrtaTraverseGroupAS16(&min.x,
|
---|
| 1150 | &max.x,
|
---|
| 1151 | backward_hit_triangles,
|
---|
| 1152 | backward_dist);
|
---|
| 1153 |
|
---|
| 1154 |
|
---|
| 1155 | for (i=0; i < num; i++) {
|
---|
| 1156 | Intersectable *objectA = NULL, *objectB = NULL;
|
---|
| 1157 | Vector3 pointA, pointB;
|
---|
| 1158 | Vector3 normalA, normalB;
|
---|
| 1159 | if (forward_hit_triangles[i] !=-1 ) {
|
---|
| 1160 | objectA = mFaceParents[forward_hit_triangles[i]].mObject;
|
---|
| 1161 | // Get the normal of that face
|
---|
| 1162 | Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
|
---|
| 1163 | normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
| 1164 | //-rays[index+i].mDirection; // $$ temporary
|
---|
| 1165 | pointA = rays[index+i].Extrap(forward_dist[i]);
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | if (backward_hit_triangles[i]!=-1) {
|
---|
| 1169 | objectB = mFaceParents[backward_hit_triangles[i]].mObject;
|
---|
| 1170 | Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
|
---|
| 1171 |
|
---|
| 1172 | normalB = mesh->GetFacePlane(mFaceParents[backward_hit_triangles[i]].mFaceIndex).mNormal;
|
---|
| 1173 |
|
---|
| 1174 | // normalB = rays[index+i].mDirection; // $$ temporary
|
---|
| 1175 | pointB = rays[index+i].Extrap(-backward_dist[i]);
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
| 1178 | ProcessRay(rays[index+i].mOrigin,
|
---|
| 1179 | rays[index+i].mDirection,
|
---|
| 1180 | objectA, pointA, normalA,
|
---|
| 1181 | objectB, pointB, normalB,
|
---|
| 1182 | rays[index+i].mPdf,
|
---|
| 1183 | vssRays,
|
---|
| 1184 | sbox
|
---|
| 1185 | );
|
---|
| 1186 | }
|
---|
| 1187 | #endif
|
---|
| 1188 | } else {
|
---|
| 1189 |
|
---|
| 1190 | for (i=index; i < index + num; i++) {
|
---|
| 1191 | CastRay(rays[i].mOrigin,
|
---|
| 1192 | rays[i].mDirection,
|
---|
| 1193 | rays[i].mPdf,
|
---|
| 1194 | vssRays,
|
---|
| 1195 | sbox);
|
---|
| 1196 | }
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | void
|
---|
| 1202 | Preprocessor::CastRays(
|
---|
| 1203 | SimpleRayContainer &rays,
|
---|
| 1204 | VssRayContainer &vssRays
|
---|
| 1205 | )
|
---|
| 1206 | {
|
---|
| 1207 | long t1 = GetTime();
|
---|
| 1208 | for (int i=0; i < rays.size(); ) {
|
---|
| 1209 | if (i + 16 < rays.size()) {
|
---|
| 1210 | CastRays16(
|
---|
| 1211 | i,
|
---|
| 1212 | rays,
|
---|
| 1213 | vssRays,
|
---|
| 1214 | mViewCellsManager->GetViewSpaceBox());
|
---|
| 1215 | i += 16;
|
---|
| 1216 | } else {
|
---|
| 1217 | CastRay(rays[i].mOrigin,
|
---|
| 1218 | rays[i].mDirection,
|
---|
| 1219 | rays[i].mPdf,
|
---|
| 1220 | vssRays,
|
---|
| 1221 | mViewCellsManager->GetViewSpaceBox());
|
---|
| 1222 | i++;
|
---|
| 1223 | }
|
---|
| 1224 | if (i % 10000 == 0)
|
---|
| 1225 | cout<<".";
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 | long t2 = GetTime();
|
---|
| 1229 |
|
---|
| 1230 | cout<<2*rays.size()/(1e3*TimeDiff(t1, t2))<<"M rays/s"<<endl;
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
[1251] | 1233 | int Preprocessor::CastRay(
|
---|
| 1234 | const Vector3 &viewPoint,
|
---|
| 1235 | const Vector3 &direction,
|
---|
| 1236 | const float probability,
|
---|
| 1237 | VssRayContainer &vssRays,
|
---|
| 1238 | const AxisAlignedBox3 &box
|
---|
| 1239 | )
|
---|
| 1240 | {
|
---|
| 1241 | switch (mRayCastMethod)
|
---|
| 1242 | {
|
---|
| 1243 | case INTEL_RAYCASTER:
|
---|
| 1244 | return CastIntelDoubleRay(viewPoint, direction, probability, vssRays, box);
|
---|
| 1245 | case INTERNAL_RAYCASTER:
|
---|
| 1246 | default:
|
---|
| 1247 | return CastInternalRay(viewPoint, direction, probability, vssRays, box);
|
---|
| 1248 | }
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
| 1251 |
|
---|
| 1252 | void Preprocessor::SetupRay(Ray &ray,
|
---|
| 1253 | const Vector3 &point,
|
---|
| 1254 | const Vector3 &direction
|
---|
| 1255 | )
|
---|
| 1256 | {
|
---|
| 1257 | ray.Clear();
|
---|
| 1258 | // do not store anything else then intersections at the ray
|
---|
| 1259 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
| 1260 | }
|
---|
| 1261 |
|
---|
| 1262 | }
|
---|