[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"
|
---|
[1287] | 20 | #include "VssRay.h"
|
---|
[1520] | 21 | #include "IntelRayCaster.h"
|
---|
| 22 | #include "InternalRayCaster.h"
|
---|
[1968] | 23 | #include "GlobalLinesRenderer.h"
|
---|
[2183] | 24 | #include "ObjectsParser.h"
|
---|
[1264] | 25 |
|
---|
[1287] | 26 |
|
---|
[1292] | 27 | #define DEBUG_RAYCAST 0
|
---|
[1584] | 28 | #define SHOW_RAYCAST_TIMING 1
|
---|
[1292] | 29 |
|
---|
[2183] | 30 | using namespace std;
|
---|
[1292] | 31 |
|
---|
[863] | 32 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 33 |
|
---|
| 34 |
|
---|
[2544] | 35 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 36 | {
|
---|
| 37 | return obj1->mId < obj2->mId;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
[2187] | 41 | Preprocessor *preprocessor = NULL;
|
---|
[1867] | 42 |
|
---|
[752] | 43 |
|
---|
[372] | 44 | Preprocessor::Preprocessor():
|
---|
| 45 | mKdTree(NULL),
|
---|
[409] | 46 | mBspTree(NULL),
|
---|
[445] | 47 | mVspBspTree(NULL),
|
---|
[1002] | 48 | mViewCellsManager(NULL),
|
---|
[1251] | 49 | mRenderSimulator(NULL),
|
---|
[1279] | 50 | mPass(0),
|
---|
[1520] | 51 | mSceneGraph(NULL),
|
---|
[1613] | 52 | mRayCaster(NULL),
|
---|
[1926] | 53 | mStopComputation(false),
|
---|
[1968] | 54 | mThread(NULL),
|
---|
[2076] | 55 | mGlobalLinesRenderer(NULL),
|
---|
| 56 | mUseHwGlobalLines(false)
|
---|
[308] | 57 | {
|
---|
[1004] | 58 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
[538] | 59 |
|
---|
[840] | 60 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
| 61 | renderer = NULL;
|
---|
[1785] | 62 |
|
---|
[1004] | 63 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
[1379] | 64 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
|
---|
[1004] | 65 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
| 66 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
| 67 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
| 68 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
[1520] | 69 |
|
---|
[871] | 70 | char buffer[256];
|
---|
[1004] | 71 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
[871] | 72 | mVisibilityFileName = buffer;
|
---|
[1771] | 73 |
|
---|
| 74 | Environment::GetSingleton()->GetStringValue("Preprocessor.stats", buffer);
|
---|
| 75 | mStats.open(buffer);
|
---|
[2048] | 76 |
|
---|
[1749] | 77 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
|
---|
[1004] | 78 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
[1695] | 79 | mApplyVisibilitySpatialFilter );
|
---|
[1004] | 80 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
[878] | 81 |
|
---|
[1695] | 82 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportObj", mExportObj);
|
---|
| 83 |
|
---|
[1723] | 84 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
| 85 |
|
---|
[1900] | 86 | Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.rays", mExportRays);
|
---|
[2008] | 87 | Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.animation", mExportAnimation);
|
---|
[1900] | 88 | Environment::GetSingleton()->GetIntValue("Preprocessor.Export.numRays", mExportNumRays);
|
---|
| 89 |
|
---|
[1966] | 90 | Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 91 | Environment::GetSingleton()->GetIntValue("Preprocessor.totalSamples", mTotalSamples);
|
---|
[2046] | 92 | Environment::GetSingleton()->GetIntValue("Preprocessor.totalTime", mTotalTime);
|
---|
[1966] | 93 | Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerEvaluation",
|
---|
| 94 | mSamplesPerEvaluation);
|
---|
| 95 |
|
---|
[1695] | 96 | Debug << "******* Preprocessor Options **********" << endl;
|
---|
[840] | 97 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
[1379] | 98 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
[1695] | 99 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
| 100 | Debug << "export obj: " << mExportObj << endl;
|
---|
[1723] | 101 | Debug << "use view space box: " << mUseViewSpaceBox << endl;
|
---|
[2048] | 102 |
|
---|
| 103 | cout << "samples per pass " << mSamplesPerPass << endl;
|
---|
[372] | 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | Preprocessor::~Preprocessor()
|
---|
| 108 | {
|
---|
[1291] | 109 | cout << "cleaning up" << endl;
|
---|
[752] | 110 |
|
---|
[1291] | 111 | cout << "Deleting view cells manager ... \n";
|
---|
| 112 | DEL_PTR(mViewCellsManager);
|
---|
| 113 | cout << "done.\n";
|
---|
[752] | 114 |
|
---|
[2047] | 115 |
|
---|
[1291] | 116 | cout << "Deleting bsp tree ... \n";
|
---|
| 117 | DEL_PTR(mBspTree);
|
---|
[1924] | 118 | cout << "done.\n";
|
---|
[752] | 119 |
|
---|
[1924] | 120 | cout << "Deleting kd tree ...\n";
|
---|
[1291] | 121 | DEL_PTR(mKdTree);
|
---|
| 122 | cout << "done.\n";
|
---|
[752] | 123 |
|
---|
[1923] | 124 | cout << "Deleting vspbsp tree ... \n";
|
---|
[1291] | 125 | DEL_PTR(mVspBspTree);
|
---|
| 126 | cout << "done.\n";
|
---|
[1002] | 127 |
|
---|
[1923] | 128 | cout << "Deleting scene graph ... \n";
|
---|
[1291] | 129 | DEL_PTR(mSceneGraph);
|
---|
| 130 | cout << "done.\n";
|
---|
| 131 |
|
---|
[2017] | 132 | cout << "deleting render simulator ... \n";
|
---|
[1291] | 133 | DEL_PTR(mRenderSimulator);
|
---|
[2017] | 134 | mRenderSimulator = NULL;
|
---|
| 135 | cout << "deleting renderer ... \n";
|
---|
[1291] | 136 | DEL_PTR(renderer);
|
---|
[2017] | 137 | renderer = NULL;
|
---|
| 138 | cout << "deleting ray caster ... \n";
|
---|
[1523] | 139 | DEL_PTR(mRayCaster);
|
---|
[1999] | 140 |
|
---|
[1990] | 141 | #ifdef USE_CG
|
---|
[2017] | 142 | cout << "deleting global lines renderer ... \n";
|
---|
[1968] | 143 | DEL_PTR(mGlobalLinesRenderer);
|
---|
[1990] | 144 | #endif
|
---|
[2017] | 145 | cout << "finished" << endl;
|
---|
[372] | 146 | }
|
---|
| 147 |
|
---|
[1521] | 148 |
|
---|
| 149 | GlRendererBuffer *Preprocessor::GetRenderer()
|
---|
| 150 | {
|
---|
| 151 | return renderer;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
[387] | 156 | {
|
---|
| 157 | int pos = 0;
|
---|
| 158 |
|
---|
| 159 | while(1) {
|
---|
[469] | 160 | int npos = (int)str.find(';', pos);
|
---|
[387] | 161 |
|
---|
| 162 | if (npos < 0 || npos - pos < 1)
|
---|
| 163 | break;
|
---|
| 164 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 165 | pos = npos + 1;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
[440] | 169 | return (int)filenames.size();
|
---|
[387] | 170 | }
|
---|
| 171 |
|
---|
[750] | 172 |
|
---|
[1926] | 173 | void Preprocessor::SetThread(PreprocessorThread *t)
|
---|
| 174 | {
|
---|
| 175 | mThread = t;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | PreprocessorThread *Preprocessor::GetThread() const
|
---|
| 180 | {
|
---|
| 181 | return mThread;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
[2183] | 185 | bool Preprocessor::LoadBinaryObj(const string &filename,
|
---|
[1655] | 186 | SceneGraphNode *root,
|
---|
| 187 | vector<FaceParentInfo> *parents)
|
---|
| 188 | {
|
---|
[2452] | 189 | //ifstream inStream(filename, ios::binary);
|
---|
| 190 | igzstream inStream(filename.c_str());
|
---|
[1658] | 191 |
|
---|
[2452] | 192 | if (!inStream.is_open())
|
---|
[1655] | 193 | return false;
|
---|
| 194 |
|
---|
[1658] | 195 | cout << "binary obj dump available, loading " << filename.c_str() << endl;
|
---|
[2003] | 196 |
|
---|
[2004] | 197 | // read in triangle size
|
---|
| 198 | int numTriangles;
|
---|
| 199 |
|
---|
[2452] | 200 | inStream.read(reinterpret_cast<char *>(&numTriangles), sizeof(int));
|
---|
[2004] | 201 | root->mGeometry.reserve(numTriangles);
|
---|
[2572] | 202 | cout << "loading " << numTriangles << " triangles (" << numTriangles *
|
---|
| 203 | (sizeof(TriangleIntersectable) + sizeof(TriangleIntersectable *)) /
|
---|
| 204 | (1024 * 1024) << " MB)" << endl;
|
---|
[2003] | 205 |
|
---|
[2004] | 206 | int i = 0;
|
---|
[2572] | 207 |
|
---|
[1655] | 208 | while (1)
|
---|
| 209 | {
|
---|
| 210 | Triangle3 tri;
|
---|
| 211 |
|
---|
[2452] | 212 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
| 213 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
| 214 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
[2005] | 215 |
|
---|
[1655] | 216 | // end of file reached
|
---|
[2452] | 217 | if (inStream.eof())
|
---|
[1655] | 218 | break;
|
---|
| 219 |
|
---|
| 220 | TriangleIntersectable *obj = new TriangleIntersectable(tri);
|
---|
| 221 | root->mGeometry.push_back(obj);
|
---|
[2005] | 222 |
|
---|
[2183] | 223 | if ((i ++) % 500000 == 499999)
|
---|
[2005] | 224 | cout<<"\r"<<i<<"/"<<numTriangles<<"\r";
|
---|
[1655] | 225 | }
|
---|
| 226 |
|
---|
[2004] | 227 | if (i != numTriangles)
|
---|
| 228 | {
|
---|
| 229 | cout << "warning: triangle size does not match with loaded triangle size" << endl;
|
---|
| 230 | return false;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | cout << "loaded " << numTriangles << " triangles" << endl;
|
---|
| 234 |
|
---|
[1655] | 235 | return true;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
[2342] | 239 | bool Preprocessor::ExportBinaryObj(const string &filename, SceneGraphNode *root)
|
---|
[1658] | 240 | {
|
---|
| 241 | ogzstream samplesOut(filename.c_str());
|
---|
[2004] | 242 |
|
---|
[1658] | 243 | if (!samplesOut.is_open())
|
---|
| 244 | return false;
|
---|
| 245 |
|
---|
[2004] | 246 | int numTriangles = (int)root->mGeometry.size();
|
---|
| 247 |
|
---|
| 248 | samplesOut.write(reinterpret_cast<char *>(&numTriangles), sizeof(int));
|
---|
| 249 |
|
---|
[1658] | 250 | ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
|
---|
| 251 |
|
---|
| 252 | for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
|
---|
| 253 | {
|
---|
| 254 | Intersectable *obj = *oit;
|
---|
| 255 |
|
---|
| 256 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 257 | {
|
---|
[2017] | 258 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
[1658] | 259 |
|
---|
| 260 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
| 261 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
| 262 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
| 263 | }
|
---|
| 264 | else
|
---|
| 265 | {
|
---|
| 266 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[2004] | 270 | cout << "exported " << numTriangles << " triangles" << endl;
|
---|
| 271 |
|
---|
[1658] | 272 | return true;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[1695] | 275 |
|
---|
[2342] | 276 | bool Preprocessor::ExportObj(const string &filename, const ObjectContainer &objects)
|
---|
[1695] | 277 | {
|
---|
| 278 | ofstream samplesOut(filename.c_str());
|
---|
| 279 |
|
---|
| 280 | if (!samplesOut.is_open())
|
---|
| 281 | return false;
|
---|
| 282 |
|
---|
| 283 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 284 |
|
---|
[2004] | 285 | //AxisAlignedBox3 bbox = mSceneGraph->GetBox(); bbox.Enlarge(30.0);
|
---|
[1695] | 286 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 287 | {
|
---|
| 288 | Intersectable *obj = *oit;
|
---|
| 289 |
|
---|
| 290 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 291 | {
|
---|
[2017] | 292 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
[2004] | 293 | //if (!(bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2])))continue;
|
---|
| 294 |
|
---|
| 295 | samplesOut << "v " << tri.mVertices[0].x << " " << tri.mVertices[0].y << " " << tri.mVertices[0].z << endl;
|
---|
| 296 | samplesOut << "v " << tri.mVertices[1].x << " " << tri.mVertices[1].y << " " << tri.mVertices[1].z << endl;
|
---|
| 297 | samplesOut << "v " << tri.mVertices[2].x << " " << tri.mVertices[2].y << " " << tri.mVertices[2].z << endl;
|
---|
[1701] | 298 | //}
|
---|
[1695] | 299 | }
|
---|
| 300 | else
|
---|
| 301 | {
|
---|
| 302 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | // write faces
|
---|
| 307 | int i = 1;
|
---|
[1713] | 308 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
[1695] | 309 | {
|
---|
| 310 | Intersectable *obj = *oit;
|
---|
| 311 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 312 | {
|
---|
[2017] | 313 | //Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
[2004] | 314 | //if (!(bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))) continue;
|
---|
| 315 |
|
---|
[2017] | 316 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
[2004] | 317 | samplesOut << "f " << i << " " << i + 1 << " " << i + 2 << endl;
|
---|
| 318 | i += 3;
|
---|
[1695] | 319 | }
|
---|
| 320 | else
|
---|
| 321 | {
|
---|
| 322 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | return true;
|
---|
| 327 |
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[2342] | 330 | static string ReplaceSuffix(const string &filename, string a, string b)
|
---|
[1658] | 331 | {
|
---|
| 332 | string result = filename;
|
---|
| 333 |
|
---|
| 334 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
| 335 | if (pos == filename.size() - a.size()) {
|
---|
| 336 | result.replace(pos, a.size(), b);
|
---|
| 337 | }
|
---|
| 338 | return result;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
[1786] | 342 | Intersectable *Preprocessor::GetParentObject(const int index) const
|
---|
| 343 | {
|
---|
[2003] | 344 | if (index < 0)
|
---|
| 345 | {
|
---|
[2004] | 346 | //cerr << "Warning: triangle index smaller zero! " << index << endl;
|
---|
[2003] | 347 | return NULL;
|
---|
| 348 | }
|
---|
[1786] | 349 |
|
---|
| 350 | if (!mFaceParents.empty())
|
---|
| 351 | {
|
---|
| 352 | if (index >= (int)mFaceParents.size())
|
---|
| 353 | {
|
---|
[1978] | 354 | cerr << "Warning: triangle index out of range! " << index << endl;
|
---|
[1786] | 355 | return NULL;
|
---|
| 356 | }
|
---|
| 357 | else
|
---|
| 358 | {
|
---|
| 359 | return mFaceParents[index].mObject;
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | else
|
---|
[2003] | 363 | {
|
---|
| 364 | if (index >= (int)mObjects.size())
|
---|
| 365 | {
|
---|
[2307] | 366 | cerr<<"Warning: triangle index out of range! " << index << " of " << (int)mObjects.size() << endl;
|
---|
[2003] | 367 | return NULL;
|
---|
| 368 | }
|
---|
| 369 | else
|
---|
| 370 | {
|
---|
| 371 | return mObjects[index];
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
[1786] | 374 | }
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 | Vector3 Preprocessor::GetParentNormal(const int index) const
|
---|
| 378 | {
|
---|
| 379 | if (!mFaceParents.empty())
|
---|
| 380 | {
|
---|
| 381 | return mFaceParents[index].mObject->GetNormal(mFaceParents[index].mFaceIndex);
|
---|
| 382 | }
|
---|
| 383 | else
|
---|
| 384 | {
|
---|
| 385 | return mObjects[index]->GetNormal(0);
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
[372] | 390 | bool
|
---|
[2342] | 391 | Preprocessor::LoadScene(const string &filename)
|
---|
[372] | 392 | {
|
---|
[1655] | 393 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
[508] | 394 | mSceneGraph = new SceneGraph;
|
---|
[372] | 395 |
|
---|
[508] | 396 | Parser *parser;
|
---|
[387] | 397 | vector<string> filenames;
|
---|
[1404] | 398 | const int files = SplitFilenames(filename, filenames);
|
---|
[712] | 399 | cout << "number of input files: " << files << endl;
|
---|
[2183] | 400 |
|
---|
[387] | 401 | bool result = false;
|
---|
[1695] | 402 | bool isObj = false;
|
---|
[1344] | 403 |
|
---|
| 404 | // root for different files
|
---|
| 405 | mSceneGraph->SetRoot(new SceneGraphNode());
|
---|
| 406 |
|
---|
[1404] | 407 | // intel ray caster can only trace triangles
|
---|
[1520] | 408 | int rayCastMethod;
|
---|
| 409 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
[1827] | 410 | vector<FaceParentInfo> *fi =
|
---|
[1867] | 411 | ((rayCastMethod == RayCaster::INTEL_RAYCASTER) && mLoadMeshes) ?
|
---|
| 412 | &mFaceParents : NULL;
|
---|
[2544] | 413 |
|
---|
[1658] | 414 | if (files == 1)
|
---|
[2544] | 415 | {
|
---|
[387] | 416 | if (strstr(filename.c_str(), ".x3d"))
|
---|
[2544] | 417 | {
|
---|
[1655] | 418 | parser = new X3dParser;
|
---|
[2544] | 419 |
|
---|
[1658] | 420 | result = parser->ParseFile(filename,
|
---|
[2544] | 421 | mSceneGraph->GetRoot(),
|
---|
| 422 | mLoadMeshes,
|
---|
| 423 | fi);
|
---|
[1658] | 424 | delete parser;
|
---|
[1655] | 425 | }
|
---|
[1658] | 426 | else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
[1655] | 427 | {
|
---|
[1658] | 428 | parser = new PlyParser;
|
---|
[1655] | 429 |
|
---|
[1658] | 430 | result = parser->ParseFile(filename,
|
---|
[2544] | 431 | mSceneGraph->GetRoot(),
|
---|
| 432 | mLoadMeshes,
|
---|
| 433 | fi);
|
---|
[1658] | 434 | delete parser;
|
---|
[1655] | 435 | }
|
---|
[1658] | 436 | else if (strstr(filename.c_str(), ".obj"))
|
---|
| 437 | {
|
---|
[1695] | 438 | isObj = true;
|
---|
| 439 |
|
---|
[1658] | 440 | // hack: load binary dump
|
---|
[2004] | 441 | const string bnFile = ReplaceSuffix(filename, ".obj", ".bn");
|
---|
[2544] | 442 |
|
---|
[1658] | 443 | if (!mLoadMeshes)
|
---|
| 444 | {
|
---|
[2004] | 445 | result = LoadBinaryObj(bnFile, mSceneGraph->GetRoot(), fi);
|
---|
[1658] | 446 | }
|
---|
| 447 |
|
---|
[2004] | 448 | // parse obj
|
---|
[2544] | 449 | if (!result)
|
---|
[1658] | 450 | {
|
---|
| 451 | cout << "no binary dump available or loading full meshes, parsing file" << endl;
|
---|
| 452 | parser = new ObjParser;
|
---|
[2544] | 453 |
|
---|
| 454 | result = parser->ParseFile(filename, mSceneGraph->GetRoot(), mLoadMeshes, fi);
|
---|
| 455 |
|
---|
[2004] | 456 | cout << "loaded " << (int)mSceneGraph->GetRoot()->mGeometry.size() << " entities" << endl;
|
---|
[2183] | 457 |
|
---|
[1658] | 458 | // only works for triangles
|
---|
[2004] | 459 | if (result && !mLoadMeshes)
|
---|
[1658] | 460 | {
|
---|
[2004] | 461 | cout << "exporting binary obj to " << bnFile << "... " << endl;
|
---|
[2183] | 462 |
|
---|
[2004] | 463 | ExportBinaryObj(bnFile, mSceneGraph->GetRoot());
|
---|
[2544] | 464 |
|
---|
[1658] | 465 | cout << "finished" << endl;
|
---|
| 466 | }
|
---|
[1655] | 467 |
|
---|
[1658] | 468 | delete parser;
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 | else
|
---|
| 472 | {
|
---|
| 473 | parser = new UnigraphicsParser;
|
---|
[2544] | 474 | result = parser->ParseFile(filename, mSceneGraph->GetRoot(), mLoadMeshes, fi);
|
---|
[1658] | 475 | delete parser;
|
---|
| 476 | }
|
---|
[2544] | 477 |
|
---|
[1658] | 478 | cout << filename << endl;
|
---|
[1404] | 479 | }
|
---|
[1658] | 480 | else
|
---|
| 481 | {
|
---|
[1404] | 482 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
[2544] | 483 |
|
---|
[1404] | 484 | for (fit = filenames.begin(); fit != fit_end; ++ fit)
|
---|
| 485 | {
|
---|
| 486 | const string filename = *fit;
|
---|
[1328] | 487 |
|
---|
[1404] | 488 | cout << "parsing file " << filename.c_str() << endl;
|
---|
| 489 | if (strstr(filename.c_str(), ".x3d"))
|
---|
| 490 | parser = new X3dParser;
|
---|
| 491 | else
|
---|
| 492 | parser = new UnigraphicsParser;
|
---|
| 493 |
|
---|
| 494 | SceneGraphNode *node = new SceneGraphNode();
|
---|
[1695] | 495 | const bool success =
|
---|
| 496 | parser->ParseFile(filename, node, mLoadMeshes, fi);
|
---|
[1404] | 497 |
|
---|
| 498 | if (success)
|
---|
| 499 | {
|
---|
| 500 | mSceneGraph->GetRoot()->mChildren.push_back(node);
|
---|
| 501 | result = true; // at least one file parsed
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[1713] | 504 | // temporare hack
|
---|
[1701] | 505 | //if (!strstr(filename.c_str(), "plane")) mSceneGraph->GetRoot()->UpdateBox();
|
---|
| 506 |
|
---|
[1404] | 507 | delete parser;
|
---|
[387] | 508 | }
|
---|
| 509 | }
|
---|
[2544] | 510 |
|
---|
[752] | 511 | if (result)
|
---|
[2544] | 512 | {
|
---|
[1020] | 513 | int intersectables, faces;
|
---|
| 514 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
[1344] | 515 |
|
---|
[1020] | 516 | cout<<filename<<" parsed successfully."<<endl;
|
---|
| 517 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
| 518 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
[1701] | 519 |
|
---|
[1697] | 520 | mObjects.reserve(intersectables);
|
---|
[1020] | 521 | mSceneGraph->CollectObjects(&mObjects);
|
---|
[2544] | 522 |
|
---|
| 523 | mSceneGraph->AssignObjectIds();
|
---|
| 524 |
|
---|
[1328] | 525 | mSceneGraph->GetRoot()->UpdateBox();
|
---|
[1713] | 526 |
|
---|
| 527 | cout << "finished loading" << endl;
|
---|
[387] | 528 | }
|
---|
[1328] | 529 |
|
---|
[492] | 530 | return result;
|
---|
[372] | 531 | }
|
---|
| 532 |
|
---|
| 533 | bool
|
---|
[2342] | 534 | Preprocessor::ExportPreprocessedData(const string &filename)
|
---|
[372] | 535 | {
|
---|
[1486] | 536 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
| 537 | return true;
|
---|
[372] | 538 | }
|
---|
| 539 |
|
---|
[1486] | 540 |
|
---|
[372] | 541 | bool
|
---|
[871] | 542 | Preprocessor::PostProcessVisibility()
|
---|
| 543 | {
|
---|
| 544 |
|
---|
[904] | 545 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
[997] | 546 | cout<<"Applying visibility filter ...";
|
---|
[1002] | 547 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
[904] | 548 |
|
---|
[1002] | 549 | if (!mViewCellsManager)
|
---|
[1199] | 550 | return false;
|
---|
| 551 |
|
---|
[2076] | 552 |
|
---|
[871] | 553 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
[2076] | 554 | mApplyVisibilityFilter ?
|
---|
| 555 | mVisibilityFilterWidth : -1.0f,
|
---|
| 556 | mApplyVisibilitySpatialFilter ?
|
---|
| 557 | mVisibilityFilterWidth : -1.0f);
|
---|
[997] | 558 | cout << "done." << endl;
|
---|
[871] | 559 | }
|
---|
| 560 |
|
---|
| 561 | // export the preprocessed information to a file
|
---|
[2053] | 562 | if (1 && mExportVisibility)
|
---|
[1486] | 563 | {
|
---|
| 564 | ExportPreprocessedData(mVisibilityFileName);
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[871] | 567 | return true;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 |
|
---|
| 571 | bool
|
---|
[372] | 572 | Preprocessor::BuildKdTree()
|
---|
| 573 | {
|
---|
| 574 | mKdTree = new KdTree;
|
---|
[1344] | 575 |
|
---|
[372] | 576 | // add mesh instances of the scene graph to the root of the tree
|
---|
| 577 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
[1344] | 578 |
|
---|
[372] | 579 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
[1344] | 580 |
|
---|
[1415] | 581 | const long startTime = GetTime();
|
---|
[1201] | 582 | cout << "building kd tree ... " << endl;
|
---|
[1344] | 583 |
|
---|
[372] | 584 | mKdTree->Construct();
|
---|
[1344] | 585 |
|
---|
[1415] | 586 | cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 587 | << " secs " << endl;
|
---|
[1344] | 588 |
|
---|
[372] | 589 | return true;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
[1415] | 592 |
|
---|
[372] | 593 | void
|
---|
| 594 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
| 595 | {
|
---|
| 596 | s<<mKdTree->GetStatistics();
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | void
|
---|
| 600 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
| 601 | {
|
---|
| 602 | s << mBspTree->GetStatistics();
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | bool
|
---|
[2342] | 606 | Preprocessor::Export( const string &filename,
|
---|
[2544] | 607 | const bool scene,
|
---|
| 608 | const bool kdtree
|
---|
| 609 | )
|
---|
[372] | 610 | {
|
---|
[2544] | 611 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
[372] | 612 |
|
---|
[2544] | 613 | if (exporter) {
|
---|
| 614 | if (2 && scene)
|
---|
| 615 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
[372] | 616 |
|
---|
[2544] | 617 | if (1 && kdtree) {
|
---|
| 618 | exporter->SetWireframe();
|
---|
| 619 | exporter->ExportKdTree(*mKdTree);
|
---|
| 620 | }
|
---|
[372] | 621 |
|
---|
[2544] | 622 | delete exporter;
|
---|
| 623 | return true;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | return false;
|
---|
[372] | 627 | }
|
---|
[429] | 628 |
|
---|
[508] | 629 |
|
---|
[463] | 630 | bool Preprocessor::PrepareViewCells()
|
---|
| 631 | {
|
---|
[2543] | 632 | // load the view cells assigning the found objects to the pvss
|
---|
[2544] | 633 | #if 0
|
---|
[2543] | 634 | cerr << "loading binary view cells" << endl;
|
---|
| 635 | ViewCellsManager *dummyViewCellsManager =
|
---|
| 636 | LoadViewCellsBinary("test.vc", mObjects, false, NULL);
|
---|
| 637 |
|
---|
| 638 | //cerr << "reexporting the binary view cells" << endl;
|
---|
| 639 | //dummyViewCellsManager->ExportViewCellsBinary("outvc.xml.gz", true, mObjects);
|
---|
| 640 |
|
---|
| 641 | return false;
|
---|
| 642 | #endif
|
---|
| 643 |
|
---|
[1523] | 644 | ///////
|
---|
[577] | 645 | //-- parse view cells construction method
|
---|
[1563] | 646 |
|
---|
[1004] | 647 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
[577] | 648 | char buf[100];
|
---|
[1585] | 649 |
|
---|
[2172] | 650 | if (mLoadViewCells)
|
---|
[997] | 651 | {
|
---|
[2183] | 652 |
|
---|
| 653 | #ifdef USE_BIT_PVS
|
---|
| 654 | // HACK: for kd pvs, set pvs size to maximal number of kd nodes
|
---|
| 655 | vector<KdLeaf *> leaves;
|
---|
| 656 | preprocessor->mKdTree->CollectLeaves(leaves);
|
---|
| 657 |
|
---|
| 658 | ObjectPvs::SetPvsSize((int)leaves.size());
|
---|
| 659 | #endif
|
---|
| 660 |
|
---|
[2172] | 661 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
[2183] | 662 | cout << "loading objects from " << buf << endl;
|
---|
[2117] | 663 |
|
---|
[2183] | 664 | // load objects which will be used as pvs entries
|
---|
| 665 | ObjectContainer pvsObjects;
|
---|
| 666 | if (0) LoadObjects(buf, pvsObjects, mObjects);
|
---|
[2117] | 667 |
|
---|
[2183] | 668 | const bool finalizeViewCells = true;
|
---|
| 669 | cout << "loading view cells from " << buf << endl;
|
---|
| 670 |
|
---|
| 671 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf,
|
---|
| 672 | pvsObjects,
|
---|
| 673 | mObjects,
|
---|
| 674 | finalizeViewCells,
|
---|
| 675 | NULL);
|
---|
| 676 |
|
---|
[1740] | 677 | cout << "view cells loaded." << endl<<flush;
|
---|
| 678 |
|
---|
| 679 | if (!mViewCellsManager)
|
---|
| 680 | {
|
---|
[2072] | 681 | cerr << "no view cells manager could be loaded" << endl;
|
---|
[1740] | 682 | return false;
|
---|
| 683 | }
|
---|
[577] | 684 | }
|
---|
| 685 | else
|
---|
| 686 | {
|
---|
[2543] | 687 | // parse type of view cells manager
|
---|
[1004] | 688 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
[1740] | 689 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
[1112] | 690 |
|
---|
| 691 | // default view space is the extent of the scene
|
---|
[1723] | 692 | AxisAlignedBox3 viewSpaceBox;
|
---|
[1740] | 693 |
|
---|
[1723] | 694 | if (mUseViewSpaceBox)
|
---|
[1563] | 695 | {
|
---|
[1723] | 696 | viewSpaceBox = mSceneGraph->GetBox();
|
---|
| 697 |
|
---|
[1563] | 698 | // use a small box outside of the scene
|
---|
[1723] | 699 | viewSpaceBox.Scale(Vector3(0.15f, 0.3f, 0.5f));
|
---|
| 700 | //viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.5f, 0, 0));
|
---|
| 701 | viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.3f, 0, 0));
|
---|
| 702 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
[1563] | 703 | }
|
---|
[1723] | 704 | else
|
---|
| 705 | {
|
---|
| 706 | viewSpaceBox = mSceneGraph->GetBox();
|
---|
| 707 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
| 708 | }
|
---|
[1545] | 709 |
|
---|
| 710 | bool loadVcGeometry;
|
---|
| 711 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry);
|
---|
| 712 |
|
---|
| 713 | bool extrudeBaseTriangles;
|
---|
| 714 | Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles);
|
---|
| 715 |
|
---|
| 716 | char vcGeomFilename[100];
|
---|
| 717 | Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename);
|
---|
[1740] | 718 |
|
---|
[1940] | 719 | // create view cells from specified geometry
|
---|
[1627] | 720 | if (loadVcGeometry)
|
---|
[1545] | 721 | {
|
---|
[1627] | 722 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
[1545] | 723 | {
|
---|
[1627] | 724 | if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles))
|
---|
| 725 | {
|
---|
| 726 | cerr << "loading view cells geometry failed" << endl;
|
---|
| 727 | }
|
---|
[1545] | 728 | }
|
---|
[1627] | 729 | else
|
---|
| 730 | {
|
---|
| 731 | cerr << "loading view cells geometry is not implemented for this manager" << endl;
|
---|
| 732 | }
|
---|
[1545] | 733 | }
|
---|
[577] | 734 | }
|
---|
[1740] | 735 |
|
---|
[1486] | 736 | ////////
|
---|
[1523] | 737 | //-- evaluation of render cost heuristics
|
---|
[1768] | 738 |
|
---|
[473] | 739 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 740 |
|
---|
[1004] | 741 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 742 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 743 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
[1740] | 744 |
|
---|
[473] | 745 | mRenderSimulator =
|
---|
| 746 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
[440] | 747 |
|
---|
[480] | 748 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
[1785] | 749 |
|
---|
[1581] | 750 | mViewCellsManager->SetPreprocessor(this);
|
---|
[1940] | 751 |
|
---|
[463] | 752 | return true;
|
---|
[490] | 753 | }
|
---|
| 754 |
|
---|
[1292] | 755 |
|
---|
[1563] | 756 | bool Preprocessor::ConstructViewCells()
|
---|
[1292] | 757 | {
|
---|
[1627] | 758 | // construct view cells using it's own set of samples
|
---|
| 759 | mViewCellsManager->Construct(this);
|
---|
| 760 |
|
---|
| 761 | // visualizations and statistics
|
---|
| 762 | Debug << "finished view cells:" << endl;
|
---|
| 763 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 764 |
|
---|
| 765 | return true;
|
---|
[1292] | 766 | }
|
---|
[490] | 767 |
|
---|
[1294] | 768 |
|
---|
[575] | 769 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
| 770 | {
|
---|
[1264] | 771 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
| 772 |
|
---|
[575] | 773 | if (strcmp(name, "kdTree") == 0)
|
---|
| 774 | {
|
---|
[1264] | 775 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
[575] | 776 | }
|
---|
| 777 | else if (strcmp(name, "bspTree") == 0)
|
---|
| 778 | {
|
---|
| 779 | Debug << "view cell type: Bsp" << endl;
|
---|
| 780 |
|
---|
[577] | 781 | mBspTree = new BspTree();
|
---|
[1264] | 782 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
[575] | 783 | }
|
---|
| 784 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
| 785 | {
|
---|
| 786 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 787 |
|
---|
[1004] | 788 | mVspBspTree = new VspBspTree();
|
---|
[1264] | 789 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
[575] | 790 | }
|
---|
[1006] | 791 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
[575] | 792 | {
|
---|
[1421] | 793 | Debug << "view cell type: VspOsp" << endl;
|
---|
[1288] | 794 | char buf[100];
|
---|
| 795 | Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);
|
---|
[1022] | 796 |
|
---|
[1740] | 797 | mViewCellsManager = new VspOspViewCellsManager(vcTree, buf);
|
---|
[575] | 798 | }
|
---|
[1421] | 799 | else if (strcmp(name, "sceneDependent") == 0) //TODO
|
---|
[575] | 800 | {
|
---|
[1143] | 801 | Debug << "view cell type: Bsp" << endl;
|
---|
[1421] | 802 |
|
---|
[575] | 803 | mBspTree = new BspTree();
|
---|
[1264] | 804 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
[575] | 805 | }
|
---|
| 806 | else
|
---|
| 807 | {
|
---|
[664] | 808 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
[575] | 809 | exit(1);
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | return mViewCellsManager;
|
---|
| 813 | }
|
---|
| 814 |
|
---|
| 815 |
|
---|
[491] | 816 | // use ascii format to store rays
|
---|
| 817 | #define USE_ASCII 0
|
---|
| 818 |
|
---|
| 819 |
|
---|
[2342] | 820 | bool Preprocessor::LoadKdTree(const string &filename)
|
---|
[1197] | 821 | {
|
---|
[1414] | 822 | mKdTree = new KdTree();
|
---|
[2539] | 823 | return mKdTree->ImportBinTree(filename.c_str(), mObjects);
|
---|
[1197] | 824 | }
|
---|
| 825 |
|
---|
[1414] | 826 |
|
---|
[2342] | 827 | bool Preprocessor::ExportKdTree(const string &filename)
|
---|
[1197] | 828 | {
|
---|
[1414] | 829 | return mKdTree->ExportBinTree(filename.c_str());
|
---|
[1197] | 830 | }
|
---|
| 831 |
|
---|
| 832 |
|
---|
[490] | 833 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
| 834 | ObjectContainer &objects) const
|
---|
| 835 | {
|
---|
| 836 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 837 | char fileName[100];
|
---|
[1004] | 838 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[490] | 839 |
|
---|
[491] | 840 | Vector3 origin, termination;
|
---|
| 841 | // HACK: needed only for lower_bound algorithm to find the
|
---|
| 842 | // intersected objects
|
---|
| 843 | MeshInstance sObj(NULL);
|
---|
| 844 | MeshInstance tObj(NULL);
|
---|
[490] | 845 |
|
---|
[491] | 846 | #if USE_ASCII
|
---|
[2452] | 847 | ifstream inStream(fileName);
|
---|
| 848 | if (!inStream.is_open())
|
---|
[490] | 849 | return false;
|
---|
| 850 |
|
---|
| 851 | string buf;
|
---|
[2452] | 852 | while (!(getline(inStream, buf)).eof())
|
---|
[490] | 853 | {
|
---|
[491] | 854 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
[490] | 855 | &origin.x, &origin.y, &origin.z,
|
---|
[491] | 856 | &termination.x, &termination.y, &termination.z,
|
---|
| 857 | &(sObj.mId), &(tObj.mId));
|
---|
[490] | 858 |
|
---|
[491] | 859 | Intersectable *sourceObj = NULL;
|
---|
| 860 | Intersectable *termObj = NULL;
|
---|
| 861 |
|
---|
| 862 | if (sObj.mId >= 0)
|
---|
[490] | 863 | {
|
---|
| 864 | ObjectContainer::iterator oit =
|
---|
[491] | 865 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 866 | sourceObj = *oit;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | if (tObj.mId >= 0)
|
---|
| 870 | {
|
---|
| 871 | ObjectContainer::iterator oit =
|
---|
| 872 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 873 | termObj = *oit;
|
---|
| 874 | }
|
---|
[490] | 875 |
|
---|
[491] | 876 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
| 877 | }
|
---|
| 878 | #else
|
---|
[2452] | 879 | ifstream inStream(fileName, ios::binary);
|
---|
| 880 | if (!inStream.is_open())
|
---|
[491] | 881 | return false;
|
---|
| 882 |
|
---|
| 883 | while (1)
|
---|
| 884 | {
|
---|
[2452] | 885 | inStream.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 886 | inStream.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 887 | inStream.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
| 888 | inStream.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
[491] | 889 |
|
---|
[2452] | 890 | if (inStream.eof())
|
---|
[491] | 891 | break;
|
---|
| 892 |
|
---|
| 893 | Intersectable *sourceObj = NULL;
|
---|
| 894 | Intersectable *termObj = NULL;
|
---|
| 895 |
|
---|
| 896 | if (sObj.mId >= 0)
|
---|
| 897 | {
|
---|
| 898 | ObjectContainer::iterator oit =
|
---|
| 899 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 900 | sourceObj = *oit;
|
---|
[490] | 901 | }
|
---|
[491] | 902 |
|
---|
| 903 | if (tObj.mId >= 0)
|
---|
[490] | 904 | {
|
---|
[491] | 905 | ObjectContainer::iterator oit =
|
---|
| 906 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 907 | termObj = *oit;
|
---|
[490] | 908 | }
|
---|
[491] | 909 |
|
---|
| 910 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
[490] | 911 | }
|
---|
[1779] | 912 | #endif
|
---|
[491] | 913 |
|
---|
[2452] | 914 | inStream.close();
|
---|
[490] | 915 |
|
---|
| 916 | return true;
|
---|
| 917 | }
|
---|
| 918 |
|
---|
[508] | 919 |
|
---|
| 920 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
[490] | 921 | {
|
---|
[491] | 922 | char fileName[100];
|
---|
[1004] | 923 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
[491] | 924 |
|
---|
[490] | 925 |
|
---|
| 926 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
| 927 |
|
---|
[491] | 928 | #if USE_ASCII
|
---|
| 929 | ofstream samplesOut(fileName);
|
---|
[490] | 930 | if (!samplesOut.is_open())
|
---|
| 931 | return false;
|
---|
| 932 |
|
---|
| 933 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 934 | {
|
---|
| 935 | VssRay *ray = *it;
|
---|
[491] | 936 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 937 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 938 |
|
---|
[490] | 939 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
| 940 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
[491] | 941 | << sourceid << " " << termid << "\n";
|
---|
[490] | 942 | }
|
---|
[491] | 943 | #else
|
---|
| 944 | ofstream samplesOut(fileName, ios::binary);
|
---|
| 945 | if (!samplesOut.is_open())
|
---|
| 946 | return false;
|
---|
| 947 |
|
---|
| 948 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 949 | {
|
---|
| 950 | VssRay *ray = *it;
|
---|
| 951 | Vector3 origin(ray->GetOrigin());
|
---|
| 952 | Vector3 termination(ray->GetTermination());
|
---|
| 953 |
|
---|
| 954 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 955 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 956 |
|
---|
| 957 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 958 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 959 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
| 960 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
| 961 | }
|
---|
| 962 | #endif
|
---|
[490] | 963 | samplesOut.close();
|
---|
[1194] | 964 |
|
---|
[490] | 965 | return true;
|
---|
| 966 | }
|
---|
[563] | 967 |
|
---|
[1771] | 968 |
|
---|
| 969 | int
|
---|
| 970 | Preprocessor::GenerateRays(const int number,
|
---|
[1867] | 971 | SamplingStrategy &strategy,
|
---|
| 972 | SimpleRayContainer &rays)
|
---|
[1020] | 973 | {
|
---|
[1867] | 974 | return strategy.GenerateSamples(number, rays);
|
---|
[1771] | 975 | }
|
---|
[860] | 976 |
|
---|
[1990] | 977 |
|
---|
[1771] | 978 | int
|
---|
| 979 | Preprocessor::GenerateRays(const int number,
|
---|
[1898] | 980 | const int sampleType,
|
---|
| 981 | SimpleRayContainer &rays)
|
---|
[1771] | 982 | {
|
---|
[1772] | 983 | const int startSize = (int)rays.size();
|
---|
| 984 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
| 985 | int castRays = 0;
|
---|
| 986 |
|
---|
| 987 | if (!strategy)
|
---|
[1381] | 988 | {
|
---|
[1772] | 989 | return 0;
|
---|
[1381] | 990 | }
|
---|
[1772] | 991 |
|
---|
[1381] | 992 | #if 1
|
---|
[1772] | 993 | castRays = strategy->GenerateSamples(number, rays);
|
---|
[1381] | 994 | #else
|
---|
[1772] | 995 | GenerateRayBundle(rays, newRay, 16, 0);
|
---|
| 996 | castRays += 16;
|
---|
[1381] | 997 | #endif
|
---|
[1772] | 998 |
|
---|
| 999 | delete strategy;
|
---|
| 1000 | return castRays;
|
---|
[878] | 1001 | }
|
---|
[1020] | 1002 |
|
---|
| 1003 |
|
---|
[1884] | 1004 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId)
|
---|
[1020] | 1005 | {
|
---|
| 1006 | switch (strategyId)
|
---|
| 1007 | {
|
---|
[1520] | 1008 | case SamplingStrategy::OBJECT_BASED_DISTRIBUTION:
|
---|
[1020] | 1009 | return new ObjectBasedDistribution(*this);
|
---|
[1520] | 1010 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
[1020] | 1011 | return new ObjectDirectionBasedDistribution(*this);
|
---|
[1520] | 1012 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
[1020] | 1013 | return new DirectionBasedDistribution(*this);
|
---|
[1520] | 1014 | case SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
[1020] | 1015 | return new DirectionBoxBasedDistribution(*this);
|
---|
[1520] | 1016 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
[1020] | 1017 | return new SpatialBoxBasedDistribution(*this);
|
---|
[1695] | 1018 | case SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION:
|
---|
| 1019 | return new ReverseObjectBasedDistribution(*this);
|
---|
[1769] | 1020 | case SamplingStrategy::VIEWCELL_BORDER_BASED_DISTRIBUTION:
|
---|
| 1021 | return new ViewCellBorderBasedDistribution(*this);
|
---|
[1772] | 1022 | case SamplingStrategy::VIEWSPACE_BORDER_BASED_DISTRIBUTION:
|
---|
| 1023 | return new ViewSpaceBorderBasedDistribution(*this);
|
---|
| 1024 | case SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION:
|
---|
| 1025 | return new ReverseViewSpaceBorderBasedDistribution(*this);
|
---|
[1824] | 1026 | case SamplingStrategy::GLOBAL_LINES_DISTRIBUTION:
|
---|
| 1027 | return new GlobalLinesDistribution(*this);
|
---|
| 1028 |
|
---|
| 1029 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
| 1030 | // return new ObjectsInteriorDistribution(*this);
|
---|
[1020] | 1031 | default: // no valid strategy
|
---|
[1279] | 1032 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
[1020] | 1033 | return NULL;
|
---|
| 1034 | }
|
---|
[1221] | 1035 |
|
---|
[1570] | 1036 | return NULL; // should never come here
|
---|
[1020] | 1037 | }
|
---|
| 1038 |
|
---|
| 1039 |
|
---|
[2342] | 1040 | bool Preprocessor::LoadInternKdTree(const string &internKdTree)
|
---|
[2003] | 1041 | {
|
---|
[2342] | 1042 | bool mUseKdTree = true;
|
---|
| 1043 |
|
---|
| 1044 | if (!mUseKdTree) {
|
---|
| 1045 | // create just a dummy KdTree
|
---|
| 1046 | mKdTree = new KdTree;
|
---|
| 1047 | return true;
|
---|
| 1048 | }
|
---|
| 1049 |
|
---|
| 1050 | // always try to load the kd tree
|
---|
| 1051 | cout << "loading kd tree file " << internKdTree << " ... " << endl;
|
---|
| 1052 |
|
---|
| 1053 | if (!LoadKdTree(internKdTree)) {
|
---|
| 1054 | cout << "error loading kd tree with filename "
|
---|
| 1055 | << internKdTree << ", rebuilding it instead ... " << endl;
|
---|
| 1056 | // build new kd tree from scene geometry
|
---|
| 1057 | BuildKdTree();
|
---|
| 1058 |
|
---|
| 1059 | // export kd tree?
|
---|
| 1060 | const long startTime = GetTime();
|
---|
| 1061 | cout << "exporting kd tree ... ";
|
---|
| 1062 |
|
---|
| 1063 | if (!ExportKdTree(internKdTree))
|
---|
| 1064 | {
|
---|
| 1065 | cout << " error exporting kd tree with filename "
|
---|
| 1066 | << internKdTree << endl;
|
---|
| 1067 | }
|
---|
| 1068 | else
|
---|
| 1069 | {
|
---|
| 1070 | cout << "finished in "
|
---|
| 1071 | << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 1072 | << " secs" << endl;
|
---|
| 1073 | }
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
| 1076 | KdTreeStatistics(cout);
|
---|
| 1077 | cout << mKdTree->GetBox() << endl;
|
---|
| 1078 |
|
---|
[2035] | 1079 | return true;
|
---|
[2003] | 1080 | }
|
---|
| 1081 |
|
---|
| 1082 |
|
---|
[2342] | 1083 | bool Preprocessor::InitRayCast(const string &externKdTree,
|
---|
| 1084 | const string &internKdTree)
|
---|
[1221] | 1085 | {
|
---|
[1633] | 1086 | // always try to load the kd tree
|
---|
[2003] | 1087 | /* cout << "loading kd tree file " << internKdTree << " ... " << endl;
|
---|
[1634] | 1088 |
|
---|
| 1089 | if (!LoadKdTree(internKdTree))
|
---|
| 1090 | {
|
---|
[1968] | 1091 | cout << "error loading kd tree with filename "
|
---|
| 1092 | << internKdTree << ", rebuilding it instead ... " << endl;
|
---|
[1633] | 1093 | // build new kd tree from scene geometry
|
---|
[1415] | 1094 | BuildKdTree();
|
---|
| 1095 |
|
---|
[1627] | 1096 | // export kd tree?
|
---|
[1633] | 1097 | const long startTime = GetTime();
|
---|
[1627] | 1098 | cout << "exporting kd tree ... ";
|
---|
| 1099 |
|
---|
| 1100 | if (!ExportKdTree(internKdTree))
|
---|
| 1101 | {
|
---|
[1968] | 1102 | cout << " error exporting kd tree with filename "
|
---|
| 1103 | << internKdTree << endl;
|
---|
[1627] | 1104 | }
|
---|
| 1105 | else
|
---|
| 1106 | {
|
---|
[1968] | 1107 | cout << "finished in "
|
---|
| 1108 | << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 1109 | << " secs" << endl;
|
---|
[1627] | 1110 | }
|
---|
[1415] | 1111 | }
|
---|
[1633] | 1112 |
|
---|
| 1113 | KdTreeStatistics(cout);
|
---|
| 1114 | cout << mKdTree->GetBox() << endl;
|
---|
[2003] | 1115 | */
|
---|
[1520] | 1116 | int rayCastMethod;
|
---|
[1968] | 1117 | Environment::GetSingleton()->
|
---|
| 1118 | GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
[1520] | 1119 |
|
---|
| 1120 | if (rayCastMethod == 0)
|
---|
[1221] | 1121 | {
|
---|
[1867] | 1122 | cout << "ray cast method: internal" << endl;
|
---|
[2003] | 1123 | mRayCaster = new InternalRayCaster(*this);
|
---|
[1221] | 1124 | }
|
---|
[1251] | 1125 | else
|
---|
[1520] | 1126 | {
|
---|
[1524] | 1127 | #ifdef GTP_INTERNAL
|
---|
[1867] | 1128 | cout << "ray cast method: intel" << endl;
|
---|
| 1129 | mRayCaster = new IntelRayCaster(*this, externKdTree);
|
---|
[1524] | 1130 | #endif
|
---|
[1251] | 1131 | }
|
---|
[1867] | 1132 |
|
---|
[2559] | 1133 | /////////////////
|
---|
[2072] | 1134 | //-- reserve constant block of rays
|
---|
| 1135 |
|
---|
| 1136 | // hack: If we dont't use view cells loading, there must be at least as much rays
|
---|
| 1137 | // as are needed for the view cells construction
|
---|
| 1138 | bool loadViewCells;
|
---|
| 1139 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
|
---|
| 1140 |
|
---|
[2187] | 1141 | int reserveRays;
|
---|
| 1142 | int constructionSamples;
|
---|
[2072] | 1143 |
|
---|
| 1144 | if (!loadViewCells)
|
---|
| 1145 | {
|
---|
[2187] | 1146 | cout << "hack: setting ray pool size to view cell construction or evaluation size" << endl;
|
---|
[2072] | 1147 |
|
---|
[2187] | 1148 | constructionSamples = 1000000;
|
---|
| 1149 |
|
---|
[2072] | 1150 | char buf[100];
|
---|
| 1151 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
[2187] | 1152 |
|
---|
[2072] | 1153 | if (strcmp(buf, "vspBspTree") == 0)
|
---|
| 1154 | {
|
---|
[2187] | 1155 | Environment::GetSingleton()->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
|
---|
| 1156 |
|
---|
[2072] | 1157 | }
|
---|
| 1158 | else if (strcmp(buf, "vspOspTree") == 0)
|
---|
| 1159 | {
|
---|
[2187] | 1160 | Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", constructionSamples);
|
---|
[2072] | 1161 | }
|
---|
[2187] | 1162 |
|
---|
| 1163 | int evalSamplesPerPass;
|
---|
| 1164 |
|
---|
| 1165 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", evalSamplesPerPass);
|
---|
| 1166 |
|
---|
| 1167 | reserveRays = max(constructionSamples, evalSamplesPerPass);
|
---|
| 1168 | reserveRays *= 2;
|
---|
[2072] | 1169 | }
|
---|
| 1170 | else
|
---|
| 1171 | {
|
---|
[2187] | 1172 | cout << "setting ray pool size to samples per pass" << endl;
|
---|
[2072] | 1173 | reserveRays = mSamplesPerPass * 2;
|
---|
| 1174 | }
|
---|
| 1175 |
|
---|
[2048] | 1176 | cout << "======================" << endl;
|
---|
[2072] | 1177 | cout << "reserving " << reserveRays << " rays " << endl;
|
---|
| 1178 | mRayCaster->ReserveVssRayPool(reserveRays);
|
---|
[2012] | 1179 |
|
---|
[1520] | 1180 | return true;
|
---|
[1281] | 1181 | }
|
---|
| 1182 |
|
---|
[1292] | 1183 |
|
---|
[1281] | 1184 | void
|
---|
| 1185 | Preprocessor::CastRays(
|
---|
| 1186 | SimpleRayContainer &rays,
|
---|
[1520] | 1187 | VssRayContainer &vssRays,
|
---|
[1528] | 1188 | const bool castDoubleRays,
|
---|
[1996] | 1189 | const bool pruneInvalidRays
|
---|
[1281] | 1190 | )
|
---|
| 1191 | {
|
---|
[2035] | 1192 |
|
---|
[2069] | 1193 | const long t1 = GetTime();
|
---|
[2035] | 1194 |
|
---|
[2198] | 1195 | if (rays.size() > 10000)
|
---|
[2187] | 1196 | {
|
---|
[2539] | 1197 |
|
---|
[2072] | 1198 | mRayCaster->SortRays(rays);
|
---|
[2069] | 1199 | cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
|
---|
[1984] | 1200 | }
|
---|
[1972] | 1201 |
|
---|
[2072] | 1202 |
|
---|
[2076] | 1203 | if (mUseHwGlobalLines)
|
---|
[2187] | 1204 | {
|
---|
[2539] | 1205 | CastRaysWithHwGlobalLines(
|
---|
| 1206 | rays,
|
---|
| 1207 | vssRays,
|
---|
| 1208 | castDoubleRays,
|
---|
| 1209 | pruneInvalidRays
|
---|
| 1210 | );
|
---|
[2187] | 1211 | }
|
---|
[2076] | 1212 | else
|
---|
[2187] | 1213 | {
|
---|
[2539] | 1214 | mRayCaster->CastRays(
|
---|
| 1215 | rays,
|
---|
| 1216 | vssRays,
|
---|
| 1217 | mViewCellsManager->GetViewSpaceBox(),
|
---|
| 1218 | castDoubleRays,
|
---|
| 1219 | pruneInvalidRays);
|
---|
[2187] | 1220 | }
|
---|
| 1221 |
|
---|
| 1222 | if (rays.size() > 10000)
|
---|
[2076] | 1223 | {
|
---|
| 1224 | cout << endl;
|
---|
[2539] | 1225 | long t2 = GetTime();
|
---|
[2072] | 1226 |
|
---|
[2076] | 1227 | #if SHOW_RAYCAST_TIMING
|
---|
| 1228 | if (castDoubleRays)
|
---|
| 1229 | cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
| 1230 | else
|
---|
| 1231 | cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
|
---|
| 1232 | #endif
|
---|
| 1233 | }
|
---|
| 1234 |
|
---|
| 1235 | DeterminePvsObjects(vssRays);
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
| 1239 | void
|
---|
| 1240 | Preprocessor::CastRaysWithHwGlobalLines(
|
---|
| 1241 | SimpleRayContainer &rays,
|
---|
[2187] | 1242 | VssRayContainer &vssRays,
|
---|
| 1243 | const bool castDoubleRays,
|
---|
| 1244 | const bool pruneInvalidRays)
|
---|
[2076] | 1245 | {
|
---|
| 1246 | SimpleRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 1247 | SimpleRayContainer rayBucket;
|
---|
| 1248 | int i = 0;
|
---|
| 1249 | for (rit = rays.begin(); rit != rit_end; ++ rit, ++ i)
|
---|
[1292] | 1250 | {
|
---|
[2076] | 1251 | SimpleRay ray = *rit;
|
---|
[1990] | 1252 | #ifdef USE_CG
|
---|
[1972] | 1253 | // HACK: global lines must be treated special
|
---|
[2076] | 1254 | if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
|
---|
[1968] | 1255 | {
|
---|
[2076] | 1256 | mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
|
---|
| 1257 | continue;
|
---|
[1968] | 1258 | }
|
---|
[1990] | 1259 | #endif
|
---|
[2187] | 1260 | rayBucket.push_back(ray);
|
---|
| 1261 |
|
---|
| 1262 | // 16 rays gathered => do ray casting
|
---|
| 1263 | if (rayBucket.size() >= 16)
|
---|
[1520] | 1264 | {
|
---|
[2187] | 1265 | mRayCaster->CastRays16(
|
---|
| 1266 | rayBucket,
|
---|
| 1267 | vssRays,
|
---|
| 1268 | mViewCellsManager->GetViewSpaceBox(),
|
---|
| 1269 | castDoubleRays,
|
---|
| 1270 | pruneInvalidRays);
|
---|
| 1271 |
|
---|
| 1272 | rayBucket.clear();
|
---|
[1292] | 1273 | }
|
---|
[2187] | 1274 |
|
---|
| 1275 | if (rays.size() > 100000 && i % 100000 == 0)
|
---|
[2072] | 1276 | cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
|
---|
[1972] | 1277 | }
|
---|
[2072] | 1278 |
|
---|
[1972] | 1279 | // cast rest of rays
|
---|
| 1280 | SimpleRayContainer::const_iterator sit, sit_end = rayBucket.end();
|
---|
| 1281 |
|
---|
| 1282 | for (sit = rayBucket.begin(); sit != sit_end; ++ sit)
|
---|
| 1283 | {
|
---|
| 1284 | SimpleRay ray = *sit;
|
---|
[1990] | 1285 |
|
---|
| 1286 | #ifdef USE_CG
|
---|
[1972] | 1287 | // HACK: global lines must be treated special
|
---|
| 1288 | if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
|
---|
[1932] | 1289 | {
|
---|
[1972] | 1290 | mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
|
---|
[1990] | 1291 | continue;
|
---|
[1972] | 1292 | }
|
---|
[1990] | 1293 | #endif
|
---|
| 1294 | mRayCaster->CastRay(
|
---|
| 1295 | ray,
|
---|
| 1296 | vssRays,
|
---|
| 1297 | mViewCellsManager->GetViewSpaceBox(),
|
---|
| 1298 | castDoubleRays,
|
---|
[1996] | 1299 | pruneInvalidRays);
|
---|
[1990] | 1300 |
|
---|
[1524] | 1301 | }
|
---|
[1743] | 1302 |
|
---|
[1251] | 1303 | }
|
---|
| 1304 |
|
---|
| 1305 |
|
---|
[1381] | 1306 | bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,
|
---|
| 1307 | const SimpleRay &mainRay,
|
---|
| 1308 | const int number,
|
---|
| 1309 | const int pertubType) const
|
---|
| 1310 | {
|
---|
| 1311 | rayBundle.push_back(mainRay);
|
---|
| 1312 |
|
---|
[1564] | 1313 | const float pertubOrigin = 0.0f;
|
---|
| 1314 | const float pertubDir = 0.2f;
|
---|
[1381] | 1315 |
|
---|
| 1316 | for (int i = 0; i < number - 1; ++ i)
|
---|
| 1317 | {
|
---|
| 1318 | Vector3 pertub;
|
---|
| 1319 |
|
---|
| 1320 | pertub.x = RandomValue(0.0f, pertubDir);
|
---|
| 1321 | pertub.y = RandomValue(0.0f, pertubDir);
|
---|
| 1322 | pertub.z = RandomValue(0.0f, pertubDir);
|
---|
[1713] | 1323 |
|
---|
[1381] | 1324 | const Vector3 newDir = mainRay.mDirection + pertub;
|
---|
| 1325 | //const Vector3 newDir = mainRay.mDirection;
|
---|
| 1326 |
|
---|
| 1327 | pertub.x = RandomValue(0.0f, pertubOrigin);
|
---|
| 1328 | pertub.y = RandomValue(0.0f, pertubOrigin);
|
---|
| 1329 | pertub.z = RandomValue(0.0f, pertubOrigin);
|
---|
[1713] | 1330 |
|
---|
[1381] | 1331 | const Vector3 newOrigin = mainRay.mOrigin + pertub;
|
---|
| 1332 | //const Vector3 newOrigin = mainRay.mOrigin;
|
---|
| 1333 |
|
---|
[1883] | 1334 | rayBundle.push_back(SimpleRay(newOrigin, newDir, 0, 1.0f));
|
---|
[1381] | 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | return true;
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
| 1340 |
|
---|
[1251] | 1341 | void Preprocessor::SetupRay(Ray &ray,
|
---|
| 1342 | const Vector3 &point,
|
---|
[1713] | 1343 | const Vector3 &direction) const
|
---|
[1251] | 1344 | {
|
---|
| 1345 | ray.Clear();
|
---|
| 1346 | // do not store anything else then intersections at the ray
|
---|
[1713] | 1347 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
[1251] | 1348 | }
|
---|
| 1349 |
|
---|
[1968] | 1350 |
|
---|
[1917] | 1351 | void Preprocessor::EvalViewCellHistogram()
|
---|
[1771] | 1352 | {
|
---|
[1917] | 1353 | char filename[256];
|
---|
| 1354 | Environment::GetSingleton()->GetStringValue("Preprocessor.histogram.file", filename);
|
---|
[1771] | 1355 |
|
---|
[1917] | 1356 | // mViewCellsManager->EvalViewCellHistogram(filename, 1000000);
|
---|
| 1357 | mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, 1000000);
|
---|
[1771] | 1358 | }
|
---|
[1520] | 1359 |
|
---|
[1900] | 1360 |
|
---|
| 1361 | bool
|
---|
| 1362 | Preprocessor::ExportRays(const char *filename,
|
---|
| 1363 | const VssRayContainer &vssRays,
|
---|
[2001] | 1364 | const int number,
|
---|
| 1365 | const bool exportScene
|
---|
[1900] | 1366 | )
|
---|
| 1367 | {
|
---|
| 1368 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
| 1369 |
|
---|
| 1370 | Exporter *exporter = NULL;
|
---|
| 1371 | exporter = Exporter::GetExporter(filename);
|
---|
| 1372 |
|
---|
| 1373 | if (0) {
|
---|
| 1374 | exporter->SetWireframe();
|
---|
| 1375 | exporter->ExportKdTree(*mKdTree);
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 | exporter->SetFilled();
|
---|
| 1379 | // $$JB temporarily do not export the scene
|
---|
[2001] | 1380 | if (exportScene)
|
---|
[1900] | 1381 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
| 1382 |
|
---|
| 1383 | exporter->SetWireframe();
|
---|
| 1384 |
|
---|
| 1385 | if (1) {
|
---|
| 1386 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
| 1387 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
| 1388 | exporter->ResetForcedMaterial();
|
---|
| 1389 | }
|
---|
| 1390 |
|
---|
| 1391 | VssRayContainer rays;
|
---|
| 1392 | vssRays.SelectRays(number, rays);
|
---|
| 1393 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
| 1394 | delete exporter;
|
---|
| 1395 | cout<<"done."<<endl<<flush;
|
---|
| 1396 |
|
---|
| 1397 | return true;
|
---|
[1251] | 1398 | }
|
---|
[1900] | 1399 |
|
---|
[2002] | 1400 | bool
|
---|
| 1401 | Preprocessor::ExportRayAnimation(const char *filename,
|
---|
| 1402 | const vector<VssRayContainer> &vssRays
|
---|
| 1403 | )
|
---|
| 1404 | {
|
---|
| 1405 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
| 1406 |
|
---|
| 1407 | Exporter *exporter = NULL;
|
---|
| 1408 | exporter = Exporter::GetExporter(filename);
|
---|
| 1409 | if (0) {
|
---|
| 1410 | exporter->SetWireframe();
|
---|
| 1411 | exporter->ExportKdTree(*mKdTree);
|
---|
| 1412 | }
|
---|
| 1413 | exporter->SetFilled();
|
---|
| 1414 | // $$JB temporarily do not export the scene
|
---|
[2008] | 1415 | if (0)
|
---|
[2002] | 1416 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
| 1417 | exporter->SetWireframe();
|
---|
| 1418 |
|
---|
| 1419 | if (1) {
|
---|
| 1420 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
| 1421 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
| 1422 | exporter->ResetForcedMaterial();
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
| 1425 | exporter->ExportRaySets(vssRays, RgbColor(1, 0, 0));
|
---|
| 1426 |
|
---|
| 1427 | delete exporter;
|
---|
| 1428 |
|
---|
| 1429 | cout<<"done."<<endl<<flush;
|
---|
| 1430 |
|
---|
| 1431 | return true;
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
[1931] | 1434 | void
|
---|
| 1435 | Preprocessor::ComputeRenderError()
|
---|
| 1436 | {
|
---|
| 1437 | // compute rendering error
|
---|
| 1438 |
|
---|
| 1439 | if (renderer && renderer->mPvsStatFrames) {
|
---|
| 1440 | // emit EvalPvsStat();
|
---|
| 1441 | // QMutex mutex;
|
---|
| 1442 | // mutex.lock();
|
---|
| 1443 | // renderer->mRenderingFinished.wait(&mutex);
|
---|
| 1444 | // mutex.unlock();
|
---|
[2049] | 1445 |
|
---|
| 1446 | if (mViewCellsManager->GetViewCellPoints()->size()) {
|
---|
| 1447 |
|
---|
| 1448 | vector<ViewCellPoints *> *vcPoints = mViewCellsManager->GetViewCellPoints();
|
---|
| 1449 |
|
---|
[2050] | 1450 | vector<ViewCellPoints *>::const_iterator
|
---|
| 1451 | vit = vcPoints->begin(),
|
---|
| 1452 | vit_end = vcPoints->end();
|
---|
| 1453 |
|
---|
[2049] | 1454 | SimpleRayContainer viewPoints;
|
---|
| 1455 |
|
---|
[2050] | 1456 | for (; vit != vit_end; ++ vit) {
|
---|
[2049] | 1457 | ViewCellPoints *vp = *vit;
|
---|
| 1458 |
|
---|
| 1459 | SimpleRayContainer::const_iterator rit = vp->second.begin(), rit_end = vp->second.end();
|
---|
| 1460 | for (; rit!=rit_end; ++rit)
|
---|
| 1461 | viewPoints.push_back(*rit);
|
---|
| 1462 | }
|
---|
[2050] | 1463 |
|
---|
| 1464 | if (viewPoints.size() != renderer->mPvsErrorBuffer.size()) {
|
---|
| 1465 | renderer->mPvsErrorBuffer.resize(viewPoints.size());
|
---|
| 1466 | renderer->ClearErrorBuffer();
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
[2049] | 1469 | renderer->EvalPvsStat(viewPoints);
|
---|
| 1470 | } else
|
---|
| 1471 | renderer->EvalPvsStat();
|
---|
[2050] | 1472 |
|
---|
[1931] | 1473 | mStats <<
|
---|
| 1474 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
[2002] | 1475 | "#AvgPixelError\n" <<renderer->GetAvgPixelError()<<endl<<
|
---|
[2008] | 1476 | "#MaxPixelError\n" <<renderer->GetMaxPixelError()<<endl<<
|
---|
[1931] | 1477 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
| 1478 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
| 1479 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
| 1480 | }
|
---|
[1900] | 1481 | }
|
---|
[1931] | 1482 |
|
---|
[1958] | 1483 |
|
---|
| 1484 | Intersectable *Preprocessor::GetObjectById(const int id)
|
---|
| 1485 | {
|
---|
[1960] | 1486 | #if 1
|
---|
[1958] | 1487 | // create a dummy mesh instance to be able to use stl
|
---|
| 1488 | MeshInstance object(NULL);
|
---|
| 1489 | object.SetId(id);
|
---|
| 1490 |
|
---|
| 1491 | ObjectContainer::const_iterator oit =
|
---|
| 1492 | lower_bound(mObjects.begin(), mObjects.end(), &object, ilt);
|
---|
| 1493 |
|
---|
| 1494 | // objects sorted by id
|
---|
| 1495 | if ((oit != mObjects.end()) && ((*oit)->GetId() == object.GetId()))
|
---|
| 1496 | {
|
---|
| 1497 | return (*oit);
|
---|
| 1498 | }
|
---|
| 1499 | else
|
---|
| 1500 | {
|
---|
| 1501 | return NULL;
|
---|
| 1502 | }
|
---|
| 1503 | #else
|
---|
[1960] | 1504 | return mObjects[id - 1];
|
---|
[1958] | 1505 | #endif
|
---|
[1931] | 1506 | }
|
---|
[1958] | 1507 |
|
---|
[1968] | 1508 |
|
---|
| 1509 | void Preprocessor::PrepareHwGlobalLines()
|
---|
| 1510 | {
|
---|
| 1511 | int texHeight, texWidth;
|
---|
| 1512 | float eps;
|
---|
| 1513 | int maxDepth;
|
---|
| 1514 | bool sampleReverse;
|
---|
| 1515 |
|
---|
| 1516 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texHeight", texHeight);
|
---|
[1969] | 1517 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texWidth", texWidth);
|
---|
[1968] | 1518 | Environment::GetSingleton()->GetFloatValue("Preprocessor.HwGlobalLines.stepSize", eps);
|
---|
| 1519 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.maxDepth", maxDepth);
|
---|
| 1520 | Environment::GetSingleton()->GetBoolValue("Preprocessor.HwGlobalLines.sampleReverse", sampleReverse);
|
---|
| 1521 |
|
---|
[1969] | 1522 | Debug << "****** hw global line options *******" << endl;
|
---|
| 1523 | Debug << "texWidth: " << texWidth << endl;
|
---|
| 1524 | Debug << "texHeight: " << texHeight << endl;
|
---|
| 1525 | Debug << "sampleReverse: " << sampleReverse << endl;
|
---|
| 1526 | Debug << "max depth: " << maxDepth << endl;
|
---|
| 1527 | Debug << "step size: " << eps << endl;
|
---|
| 1528 | Debug << endl;
|
---|
| 1529 |
|
---|
[1990] | 1530 | #ifdef USE_CG
|
---|
[1969] | 1531 | globalLinesRenderer = mGlobalLinesRenderer =
|
---|
[1968] | 1532 | new GlobalLinesRenderer(this,
|
---|
| 1533 | texHeight,
|
---|
| 1534 | texWidth,
|
---|
| 1535 | eps,
|
---|
| 1536 | maxDepth,
|
---|
| 1537 | sampleReverse);
|
---|
[1990] | 1538 |
|
---|
[1968] | 1539 | mGlobalLinesRenderer->InitGl();
|
---|
[1990] | 1540 |
|
---|
| 1541 | #endif
|
---|
[1958] | 1542 | }
|
---|
[1968] | 1543 |
|
---|
[2048] | 1544 |
|
---|
| 1545 | void Preprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
| 1546 | {
|
---|
| 1547 | mViewCellsManager->DeterminePvsObjects(rays, false);
|
---|
[1968] | 1548 | }
|
---|
[2048] | 1549 |
|
---|
| 1550 |
|
---|
[2183] | 1551 | bool Preprocessor::LoadObjects(const string &filename,
|
---|
| 1552 | ObjectContainer &pvsObjects,
|
---|
| 1553 | const ObjectContainer &preprocessorObjects)
|
---|
| 1554 | {
|
---|
| 1555 | ObjectsParser parser;
|
---|
| 1556 |
|
---|
| 1557 | const bool success = parser.ParseObjects(filename,
|
---|
| 1558 | pvsObjects,
|
---|
| 1559 | preprocessorObjects);
|
---|
| 1560 |
|
---|
| 1561 | if (!success)
|
---|
| 1562 | {
|
---|
| 1563 | Debug << "Error: loading objects failed!" << endl;
|
---|
| 1564 | }
|
---|
| 1565 |
|
---|
| 1566 | // hack: no bvh object could be found => take preprocessor objects
|
---|
| 1567 | if (pvsObjects.empty())
|
---|
| 1568 | {
|
---|
| 1569 | Debug << "no objects" << endl;
|
---|
| 1570 | pvsObjects = preprocessorObjects;
|
---|
| 1571 | }
|
---|
| 1572 |
|
---|
| 1573 | return success;
|
---|
[2048] | 1574 | }
|
---|
[2183] | 1575 |
|
---|
| 1576 |
|
---|
| 1577 | }
|
---|