[1460] | 1 | #include "Environment.h"
|
---|
| 2 | #include "GvsPreprocessor.h"
|
---|
| 3 | #include "GlRenderer.h"
|
---|
[1473] | 4 | #include "VssRay.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
[1486] | 6 | #include "Triangle3.h"
|
---|
[1489] | 7 | #include "IntersectableWrapper.h"
|
---|
[1500] | 8 | #include "Plane3.h"
|
---|
[1521] | 9 | #include "RayCaster.h"
|
---|
[1522] | 10 | #include "Exporter.h"
|
---|
[1545] | 11 | #include "SamplingStrategy.h"
|
---|
[1990] | 12 | #include "BvHierarchy.h"
|
---|
[1460] | 13 |
|
---|
[1473] | 14 |
|
---|
[1460] | 15 | namespace GtpVisibilityPreprocessor
|
---|
| 16 | {
|
---|
| 17 |
|
---|
[2000] | 18 | #define GVS_DEBUG 0
|
---|
[1934] | 19 |
|
---|
[1522] | 20 | struct VizStruct
|
---|
| 21 | {
|
---|
| 22 | Polygon3 *enlargedTriangle;
|
---|
| 23 | Triangle3 originalTriangle;
|
---|
| 24 | VssRay *ray;
|
---|
| 25 | };
|
---|
[1460] | 26 |
|
---|
[1990] | 27 | static const float MIN_DIST = 0.01f;
|
---|
| 28 |
|
---|
[1522] | 29 | static vector<VizStruct> vizContainer;
|
---|
| 30 |
|
---|
[1533] | 31 | GvsPreprocessor::GvsPreprocessor():
|
---|
[1545] | 32 | Preprocessor(),
|
---|
[2000] | 33 | mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION),
|
---|
[1996] | 34 | mProcessedViewCells(0),
|
---|
[1982] | 35 | mCurrentViewCell(NULL)
|
---|
[1460] | 36 | {
|
---|
[1486] | 37 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 38 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
| 39 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
[1996] | 40 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
|
---|
[1486] | 41 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
[1500] | 42 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
[1990] | 43 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.perViewCell", mPerViewCell);
|
---|
[1996] | 44 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
|
---|
[1473] | 45 |
|
---|
[1934] | 46 | char gvsStatsLog[100];
|
---|
| 47 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
| 48 | mGvsStatsStream.open(gvsStatsLog);
|
---|
| 49 |
|
---|
[1486] | 50 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 51 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 52 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
| 53 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
[1501] | 54 | Debug << "threshold: " << mThreshold << endl;
|
---|
[1934] | 55 | Debug << "epsilon: " << mEps << endl;
|
---|
| 56 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
[1996] | 57 | Debug << "per view cell: " << mPerViewCell << endl;
|
---|
| 58 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
| 59 | Debug << "min contribution: " << mMinContribution << endl;
|
---|
[1486] | 60 |
|
---|
[1990] | 61 | if (1)
|
---|
| 62 | mOnlyRandomSampling = false;
|
---|
| 63 | else
|
---|
[1934] | 64 | mOnlyRandomSampling = true;
|
---|
| 65 |
|
---|
| 66 | mGvsStats.Reset();
|
---|
[1460] | 67 | }
|
---|
| 68 |
|
---|
[1473] | 69 |
|
---|
[1990] | 70 | GvsPreprocessor::~GvsPreprocessor()
|
---|
| 71 | {
|
---|
[1996] | 72 | ClearRayQueue();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | void GvsPreprocessor::ClearRayQueue()
|
---|
| 77 | {
|
---|
[1990] | 78 | // clean ray queue
|
---|
| 79 | while (!mRayQueue.empty())
|
---|
| 80 | {
|
---|
| 81 | // handle next ray
|
---|
| 82 | VssRay *ray = mRayQueue.top();
|
---|
| 83 | mRayQueue.pop();
|
---|
| 84 |
|
---|
| 85 | delete ray;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[1982] | 90 | bool GvsPreprocessor::NextViewCell()
|
---|
| 91 | {
|
---|
[1996] | 92 | //if (mViewCellsManager->GetNumViewCells() == mProcessedViewCells)
|
---|
| 93 | // return false; // no more view cells
|
---|
| 94 |
|
---|
| 95 | if (mProcessedViewCells == (int)mViewCells.size())
|
---|
[1982] | 96 | return false; // no more view cells
|
---|
| 97 |
|
---|
[1996] | 98 | mCurrentViewCell = mViewCells[mProcessedViewCells];
|
---|
[1982] | 99 |
|
---|
[1996] | 100 | if (!mCurrentViewCell->GetMesh())
|
---|
[1990] | 101 | mViewCellsManager->CreateMesh(mCurrentViewCell);
|
---|
| 102 |
|
---|
[1996] | 103 | mGvsStats.mViewCellId = mCurrentViewCell->GetId();
|
---|
| 104 |
|
---|
| 105 | Debug << "vc: " << mCurrentViewCell->GetId() << endl;
|
---|
| 106 |
|
---|
| 107 | ++ mProcessedViewCells;
|
---|
[1982] | 108 |
|
---|
| 109 | return true;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[1996] | 113 | int GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
| 114 | const Triangle3 &hitTriangle,
|
---|
| 115 | const VssRay &oldRay)
|
---|
[1460] | 116 | {
|
---|
[1932] | 117 | // the predicted hitpoint: we expect to hit the same mesh again
|
---|
| 118 | const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 119 |
|
---|
| 120 | const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
| 121 | const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
| 122 |
|
---|
| 123 | // distance large => this is likely to be a discontinuity
|
---|
[1933] | 124 | #if 1
|
---|
[1932] | 125 | if ((predictedLen - len) > mThreshold)
|
---|
[1933] | 126 | #else // rather use relative distance
|
---|
| 127 | if ((predictedLen / len) > mThreshold)
|
---|
| 128 | #endif
|
---|
[1500] | 129 | {
|
---|
[1934] | 130 | //cout << "r";
|
---|
[1933] | 131 | // apply reverse sampling to find the gap
|
---|
[1500] | 132 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
[1933] | 133 |
|
---|
| 134 | if (!newRay)
|
---|
[1996] | 135 | return 1;
|
---|
[1933] | 136 |
|
---|
[1572] | 137 | // set flag for visualization
|
---|
[1571] | 138 | newRay->mFlags |= VssRay::ReverseSample;
|
---|
[1580] | 139 |
|
---|
[1977] | 140 | // if ray is not further processed => delete ray
|
---|
[1521] | 141 | if (!HandleRay(newRay))
|
---|
[1933] | 142 | {
|
---|
[1521] | 143 | delete newRay;
|
---|
[1933] | 144 | }
|
---|
[1999] | 145 |
|
---|
[1933] | 146 |
|
---|
[1996] | 147 | return 1;
|
---|
[1500] | 148 | }
|
---|
| 149 |
|
---|
[1996] | 150 | return 0;
|
---|
[1486] | 151 | }
|
---|
[1473] | 152 |
|
---|
[1999] | 153 | // #id: 1270
|
---|
| 154 | bool GvsPreprocessor::HasContribution(VssRay &ray)
|
---|
[1486] | 155 | {
|
---|
[1999] | 156 | if (!ray.mTerminationObject)
|
---|
| 157 | return false;
|
---|
[1933] | 158 |
|
---|
[1999] | 159 | bool result;
|
---|
| 160 |
|
---|
[1977] | 161 | if (!mPerViewCell)
|
---|
| 162 | {
|
---|
[1999] | 163 | // store the rays + the intersected view cells
|
---|
| 164 | const bool storeViewCells = false; //GVS_DEBUG;
|
---|
| 165 |
|
---|
| 166 | mViewCellsManager->ComputeSampleContribution(ray,
|
---|
[1977] | 167 | true,
|
---|
[1999] | 168 | storeViewCells,
|
---|
[1990] | 169 | true);
|
---|
[1999] | 170 |
|
---|
| 171 | result = ray.mPvsContribution > 0;
|
---|
[1977] | 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
[1999] | 175 | Intersectable *obj = ray.mTerminationObject;
|
---|
| 176 |
|
---|
| 177 | if (!obj->mCounter)
|
---|
| 178 | {
|
---|
| 179 | obj->mCounter = 1;
|
---|
| 180 | mTrianglePvs.push_back(obj);
|
---|
| 181 |
|
---|
| 182 | /*mViewCellsManager->ComputeSampleContribution(ray,
|
---|
[1977] | 183 | true,
|
---|
[1990] | 184 | mCurrentViewCell,
|
---|
[1999] | 185 | true);*/
|
---|
| 186 |
|
---|
| 187 | result = true;
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | result = false;
|
---|
| 192 | }
|
---|
[1977] | 193 | }
|
---|
[1932] | 194 |
|
---|
[1999] | 195 | return result;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
| 200 | {
|
---|
| 201 | if (!HasContribution(*vssRay))
|
---|
[1977] | 202 | return false;
|
---|
| 203 |
|
---|
[2005] | 204 | if (0 && GVS_DEBUG)
|
---|
[1990] | 205 | mVssRays.push_back(new VssRay(*vssRay));
|
---|
| 206 |
|
---|
[1977] | 207 | // add new ray to ray queue
|
---|
| 208 | mRayQueue.push(vssRay);
|
---|
| 209 |
|
---|
[1990] | 210 | ++ mGvsStats.mTotalContribution;
|
---|
[1533] | 211 |
|
---|
[1977] | 212 | return true;
|
---|
[1460] | 213 | }
|
---|
| 214 |
|
---|
[1500] | 215 |
|
---|
| 216 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
[1489] | 217 | */
|
---|
[1932] | 218 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
| 219 | const Triangle3 &hitTriangle,
|
---|
| 220 | const VssRay &ray,
|
---|
| 221 | const int index) const
|
---|
[1460] | 222 | {
|
---|
[1486] | 223 | const int indexU = (index + 1) % 3;
|
---|
| 224 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 225 |
|
---|
[1524] | 226 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
[1486] | 227 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 228 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
[1492] | 229 |
|
---|
[1533] | 230 | const float len = Magnitude(a);
|
---|
[1932] | 231 |
|
---|
[1523] | 232 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
| 233 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
[1524] | 234 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 235 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
[1492] | 236 |
|
---|
[1486] | 237 | // compute the new three hit points
|
---|
[1500] | 238 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 239 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
[1500] | 240 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 241 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
[1500] | 242 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 243 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
[1489] | 244 |
|
---|
[1524] | 245 | vertices.push_back(pt2);
|
---|
| 246 | vertices.push_back(pt3);
|
---|
[1500] | 247 | vertices.push_back(pt1);
|
---|
[1489] | 248 | }
|
---|
| 249 |
|
---|
| 250 |
|
---|
[1500] | 251 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
| 252 | const Triangle3 &hitTriangle,
|
---|
[1932] | 253 | const VssRay &ray) const
|
---|
[1500] | 254 | {
|
---|
[1932] | 255 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
| 256 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
| 257 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
[1500] | 258 | }
|
---|
[1492] | 259 |
|
---|
[1500] | 260 |
|
---|
[1932] | 261 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
| 262 | const Triangle3 &hitTriangle,
|
---|
| 263 | const VssRay &oldRay) const
|
---|
[1489] | 264 | {
|
---|
[1932] | 265 | // find the intersection of the plane induced by the
|
---|
| 266 | // hit triangle with the new ray
|
---|
[1500] | 267 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 268 |
|
---|
| 269 | const Vector3 hitPt =
|
---|
| 270 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
| 271 |
|
---|
| 272 | return hitPt;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
| 277 | {
|
---|
| 278 | return a.mTerminationObject == b.mTerminationObject;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
| 283 | const Vector3 &p1,
|
---|
| 284 | const Vector3 &p2,
|
---|
[1932] | 285 | const VssRay &ray1,
|
---|
| 286 | const VssRay &ray2,
|
---|
[1500] | 287 | const VssRay &oldRay)
|
---|
| 288 | {
|
---|
[1996] | 289 | int castRays = 0;
|
---|
| 290 |
|
---|
[1990] | 291 | // cast reverse rays if necessary
|
---|
[1996] | 292 | castRays += CheckDiscontinuity(ray1, hitTriangle, oldRay);
|
---|
| 293 | castRays += CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
[1500] | 294 |
|
---|
[1990] | 295 | if (EqualVisibility(ray1, ray2) || (Magnitude(p1 - p2) <= MIN_DIST))
|
---|
[1500] | 296 | {
|
---|
[1996] | 297 | return castRays;
|
---|
[1500] | 298 | }
|
---|
[1932] | 299 |
|
---|
[1990] | 300 | // the new subdivision point
|
---|
| 301 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
[1932] | 302 |
|
---|
[1990] | 303 | // cast ray into the new point
|
---|
| 304 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
[1932] | 305 |
|
---|
[1996] | 306 | VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
| 307 |
|
---|
| 308 | ++ castRays;
|
---|
[1580] | 309 |
|
---|
[1996] | 310 | if (!newRay) return castRays;
|
---|
[1580] | 311 |
|
---|
[1990] | 312 | newRay->mFlags |= VssRay::BorderSample;
|
---|
| 313 |
|
---|
| 314 | // add new ray to queue
|
---|
| 315 | const bool enqueued = HandleRay(newRay);
|
---|
| 316 |
|
---|
| 317 | // subdivide further
|
---|
[1996] | 318 | castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
| 319 | castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
[1990] | 320 |
|
---|
| 321 | // this ray will not be further processed
|
---|
| 322 | if (!enqueued)
|
---|
| 323 | delete newRay;
|
---|
| 324 |
|
---|
[1996] | 325 | return castRays;
|
---|
[1500] | 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
| 330 | {
|
---|
| 331 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1486] | 332 | Triangle3 hitTriangle;
|
---|
[1489] | 333 |
|
---|
| 334 | // other types not implemented yet
|
---|
| 335 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 336 | {
|
---|
| 337 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 338 | }
|
---|
[1522] | 339 | else
|
---|
| 340 | {
|
---|
| 341 | cout << "not yet implemented" << endl;
|
---|
| 342 | }
|
---|
[1489] | 343 |
|
---|
[1500] | 344 | VertexContainer enlargedTriangle;
|
---|
| 345 |
|
---|
| 346 | /// create 3 new hit points for each vertex
|
---|
| 347 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 348 |
|
---|
| 349 | /// create rays from sample points and handle them
|
---|
[1492] | 350 | SimpleRayContainer simpleRays;
|
---|
| 351 | simpleRays.reserve(9);
|
---|
[1486] | 352 |
|
---|
[1500] | 353 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
[1486] | 354 |
|
---|
[1500] | 355 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
| 356 | {
|
---|
| 357 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
[1990] | 358 |
|
---|
[1883] | 359 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1990] | 360 | simpleRays.AddRay(sr);
|
---|
[1500] | 361 | }
|
---|
[1522] | 362 |
|
---|
[1566] | 363 | if (0)
|
---|
| 364 | {
|
---|
[1932] | 365 | // visualize enlarged triangles
|
---|
[1566] | 366 | VizStruct dummy;
|
---|
| 367 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
| 368 | dummy.originalTriangle = hitTriangle;
|
---|
| 369 | vizContainer.push_back(dummy);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[1524] | 372 | // cast rays to triangle vertices and determine visibility
|
---|
[1489] | 373 | VssRayContainer vssRays;
|
---|
[1533] | 374 |
|
---|
[1932] | 375 | // don't cast double rays as we need only the forward rays
|
---|
[1996] | 376 | const bool castDoubleRays = !mPerViewCell;
|
---|
[1990] | 377 | // cannot prune invalid rays because we have to compare adjacent rays.
|
---|
[1932] | 378 | const bool pruneInvalidRays = false;
|
---|
| 379 |
|
---|
[1996] | 380 |
|
---|
| 381 | //////////
|
---|
| 382 | //-- fill up simple rays with random rays so we can cast 16
|
---|
| 383 |
|
---|
| 384 | //const int numRandomRays = 0;
|
---|
| 385 | const int numRandomRays = 16 - (int)simpleRays.size();
|
---|
| 386 | ViewCellBasedDistribution vcStrat(*this, mCurrentViewCell);
|
---|
| 387 |
|
---|
| 388 | GenerateRays(numRandomRays, vcStrat, simpleRays);
|
---|
| 389 |
|
---|
| 390 | /////////////////////
|
---|
| 391 |
|
---|
| 392 |
|
---|
[1990] | 393 | // keep origin for per view cell sampling
|
---|
[1996] | 394 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
[1932] | 395 |
|
---|
[1996] | 396 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
| 397 |
|
---|
[1580] | 398 | // set flags
|
---|
| 399 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
[1996] | 400 | int i = 0;
|
---|
| 401 |
|
---|
| 402 | for (rit = vssRays.begin(); rit != rit_end; ++ rit, ++ i)
|
---|
[1595] | 403 | {
|
---|
[1996] | 404 | if (i < numBorderSamples)
|
---|
| 405 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
[1580] | 406 | }
|
---|
[1595] | 407 |
|
---|
[1999] | 408 | int castRays = (int)simpleRays.size();
|
---|
| 409 |
|
---|
| 410 | VssRayContainer invalidSamples;
|
---|
| 411 |
|
---|
[1932] | 412 | // handle rays
|
---|
[1999] | 413 | EnqueueRays(vssRays, invalidSamples);
|
---|
| 414 |
|
---|
[1500] | 415 | // recursivly subdivide each edge
|
---|
[1996] | 416 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
[1500] | 417 | {
|
---|
[1932] | 418 | castRays += SubdivideEdge(hitTriangle,
|
---|
| 419 | enlargedTriangle[i],
|
---|
[1996] | 420 | enlargedTriangle[(i + 1) % numBorderSamples],
|
---|
[1932] | 421 | *vssRays[i],
|
---|
[1996] | 422 | *vssRays[(i + 1) % numBorderSamples],
|
---|
[1932] | 423 | currentRay);
|
---|
[1500] | 424 | }
|
---|
[1999] | 425 |
|
---|
[1934] | 426 | mGvsStats.mBorderSamples += castRays;
|
---|
[1932] | 427 |
|
---|
[1999] | 428 | CLEAR_CONTAINER(invalidSamples);
|
---|
| 429 |
|
---|
[1533] | 430 | return castRays;
|
---|
[1473] | 431 | }
|
---|
| 432 |
|
---|
| 433 |
|
---|
[1996] | 434 | bool GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
| 435 | const Triangle3 &occluder,
|
---|
| 436 | const VssRay &oldRay,
|
---|
| 437 | Vector3 &newPoint) const
|
---|
[1933] | 438 | {
|
---|
| 439 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
| 440 | //-- with the newly found occluder (xold is the previous ray from
|
---|
| 441 | //-- which x was generated). On the intersecting line, we select a point
|
---|
| 442 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
| 443 | //-- just passes through the gap
|
---|
| 444 |
|
---|
| 445 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
| 446 | currentRay.GetTermination(),
|
---|
| 447 | oldRay.GetTermination());
|
---|
| 448 |
|
---|
| 449 | Vector3 pt1, pt2;
|
---|
| 450 |
|
---|
| 451 | const bool intersects = occluder.GetPlaneIntersection(plane, pt1, pt2);
|
---|
| 452 |
|
---|
| 453 | if (!intersects)
|
---|
[1996] | 454 | {
|
---|
[1999] | 455 | //cerr << "big error!! no intersection " << pt1 << " " << pt2 << endl;
|
---|
[1996] | 456 | return false;
|
---|
| 457 | }
|
---|
[1933] | 458 |
|
---|
| 459 | // get the intersection point on the old ray
|
---|
| 460 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
| 461 |
|
---|
| 462 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
| 463 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
| 464 |
|
---|
| 465 | // Evaluate new hitpoint just outside the triangle
|
---|
| 466 | const float eps = mEps;
|
---|
[1990] | 467 |
|
---|
[1933] | 468 | // the point is chosen to be on the side closer to the original ray
|
---|
| 469 | if (Distance(pt1, pt3) < Distance(pt2, pt3))
|
---|
| 470 | {
|
---|
| 471 | newPoint = pt1 + eps * (pt1 - pt2);
|
---|
| 472 | }
|
---|
| 473 | else
|
---|
| 474 | {
|
---|
| 475 | newPoint = pt2 + eps * (pt2 - pt1);
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[1934] | 478 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
[1996] | 479 | return true;
|
---|
[1500] | 480 | }
|
---|
| 481 |
|
---|
| 482 |
|
---|
| 483 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
| 484 | const Triangle3 &hitTriangle,
|
---|
| 485 | const VssRay &oldRay)
|
---|
| 486 | {
|
---|
[1933] | 487 | // get triangle occluding the path to the hit mesh
|
---|
| 488 | Triangle3 occluder;
|
---|
| 489 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1934] | 490 |
|
---|
[1933] | 491 | // q: why can this happen?
|
---|
| 492 | if (!tObj)
|
---|
| 493 | return NULL;
|
---|
| 494 |
|
---|
| 495 | // other types not implemented yet
|
---|
| 496 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 497 | occluder = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 498 | else
|
---|
| 499 | cout << "not yet implemented" << endl;
|
---|
| 500 |
|
---|
| 501 | // get a point which is passing just outside of the occluder
|
---|
[1996] | 502 | Vector3 newPoint;
|
---|
| 503 |
|
---|
| 504 | // why is there sometimes no intersecton found?
|
---|
| 505 | if (!GetPassingPoint(currentRay, occluder, oldRay, newPoint))
|
---|
| 506 | return NULL;
|
---|
| 507 |
|
---|
[1500] | 508 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 509 |
|
---|
[1990] | 510 | Vector3 newDir, newOrigin;
|
---|
| 511 |
|
---|
[1932] | 512 | //-- Construct the mutated ray with xnew,
|
---|
| 513 | //-- dir = predicted(x)- pnew as direction vector
|
---|
[1990] | 514 | newDir = predicted - newPoint;
|
---|
[1932] | 515 |
|
---|
[1933] | 516 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
[1500] | 517 | // difficult to say!!
|
---|
[1935] | 518 | const float offset = 0.5f;
|
---|
[1990] | 519 | newOrigin = newPoint - newDir * offset;
|
---|
| 520 |
|
---|
| 521 | //////////////
|
---|
| 522 | //-- for per view cell sampling, we must check for intersection
|
---|
| 523 | //-- with the current view cell
|
---|
[1500] | 524 |
|
---|
[1990] | 525 | if (mPerViewCell)
|
---|
| 526 | {
|
---|
| 527 | // send ray to view cell
|
---|
| 528 | static Ray ray;
|
---|
| 529 | ray.Clear();
|
---|
| 530 | ray.Init(newOrigin, -newDir, Ray::LOCAL_RAY);
|
---|
| 531 |
|
---|
| 532 | //cout << "z";
|
---|
| 533 | // check if ray intersects view cell
|
---|
| 534 | if (!mCurrentViewCell->CastRay(ray))
|
---|
| 535 | return NULL;
|
---|
[1981] | 536 |
|
---|
[1990] | 537 | Ray::Intersection &hit = ray.intersections[0];
|
---|
| 538 |
|
---|
| 539 | //cout << "q";
|
---|
| 540 | // the ray starts from the view cell
|
---|
| 541 | newOrigin = ray.Extrap(hit.mT);
|
---|
| 542 | }
|
---|
[1981] | 543 |
|
---|
[1933] | 544 | const SimpleRay simpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1571] | 545 |
|
---|
[1933] | 546 | VssRay *reverseRay =
|
---|
[1996] | 547 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
[1933] | 548 |
|
---|
[1934] | 549 | ++ mGvsStats.mReverseSamples;
|
---|
[1933] | 550 |
|
---|
| 551 | return reverseRay;
|
---|
[1473] | 552 | }
|
---|
| 553 |
|
---|
[1489] | 554 |
|
---|
| 555 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
| 556 | const int sampleType)
|
---|
| 557 | {
|
---|
[1473] | 558 | const long startTime = GetTime();
|
---|
[1595] | 559 |
|
---|
[1489] | 560 | // generate simple rays
|
---|
| 561 | SimpleRayContainer simpleRays;
|
---|
[1990] | 562 |
|
---|
| 563 | ViewCellBasedDistribution vcStrat(*this, mCurrentViewCell);
|
---|
| 564 | GenerateRays(numSamples, vcStrat, simpleRays);
|
---|
| 565 |
|
---|
| 566 | //cout << "sr: " << simpleRays.size() << endl;
|
---|
[1489] | 567 | // generate vss rays
|
---|
[1473] | 568 | VssRayContainer samples;
|
---|
[1990] | 569 |
|
---|
[1996] | 570 | const bool castDoubleRays = !mPerViewCell;
|
---|
[1990] | 571 | const bool pruneInvalidRays = true;
|
---|
| 572 |
|
---|
[1996] | 573 | CastRays(simpleRays, samples, castDoubleRays, pruneInvalidRays);
|
---|
| 574 |
|
---|
[1999] | 575 | VssRayContainer invalidSamples;
|
---|
| 576 |
|
---|
[1489] | 577 | // add to ray queue
|
---|
[1999] | 578 | EnqueueRays(samples, invalidSamples);
|
---|
[1533] | 579 |
|
---|
[1999] | 580 | CLEAR_CONTAINER(invalidSamples);
|
---|
[1545] | 581 | //Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1990] | 582 | return (int)simpleRays.size();
|
---|
[1489] | 583 | }
|
---|
| 584 |
|
---|
| 585 |
|
---|
[1999] | 586 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples)
|
---|
[1489] | 587 | {
|
---|
[1486] | 588 | // add samples to ray queue
|
---|
[1473] | 589 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
[1576] | 590 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
[1473] | 591 | {
|
---|
[1999] | 592 | VssRay *ray = *vit;
|
---|
| 593 |
|
---|
| 594 | if (!HandleRay(ray))
|
---|
| 595 | invalidSamples.push_back(ray);
|
---|
[1460] | 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[1473] | 599 |
|
---|
[1990] | 600 | int GvsPreprocessor::ProcessQueue()
|
---|
[1460] | 601 | {
|
---|
[1533] | 602 | int castSamples = 0;
|
---|
[1990] | 603 | ++ mGvsStats.mGvsPass;
|
---|
[1545] | 604 |
|
---|
[1990] | 605 | while (!mRayQueue.empty()
|
---|
| 606 | )//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
|
---|
| 607 | {
|
---|
| 608 | // handle next ray
|
---|
| 609 | VssRay *ray = mRayQueue.top();
|
---|
| 610 | mRayQueue.pop();
|
---|
[1996] | 611 |
|
---|
[1990] | 612 | const int newSamples = AdaptiveBorderSampling(*ray);
|
---|
[1934] | 613 |
|
---|
[1990] | 614 | castSamples += newSamples;
|
---|
| 615 |
|
---|
[1999] | 616 | //cout << "new samples: " << newSamples << " " << "queue: " << (int)mRayQueue.size() << endl;
|
---|
[1990] | 617 | delete ray;
|
---|
[1473] | 618 | }
|
---|
| 619 |
|
---|
[1533] | 620 | return castSamples;
|
---|
[1460] | 621 | }
|
---|
| 622 |
|
---|
[1473] | 623 |
|
---|
[1990] | 624 | void ExportVssRays(Exporter *exporter, const VssRayContainer &vssRays)
|
---|
[1460] | 625 | {
|
---|
[1990] | 626 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
[1473] | 627 |
|
---|
[1990] | 628 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
| 629 |
|
---|
| 630 | // prepare some rays for output
|
---|
| 631 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
[1473] | 632 | {
|
---|
[1990] | 633 | //const float p = RandomValue(0.0f, (float)vssRays.size());
|
---|
[1545] | 634 |
|
---|
[1990] | 635 | if (1)//(p < raysOut)
|
---|
| 636 | {
|
---|
| 637 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
| 638 | {
|
---|
| 639 | vcRays.push_back(*rit);
|
---|
| 640 | }
|
---|
| 641 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
| 642 | {
|
---|
| 643 | vcRays2.push_back(*rit);
|
---|
| 644 | }
|
---|
| 645 | else
|
---|
| 646 | {
|
---|
| 647 | vcRays3.push_back(*rit);
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
[1460] | 650 | }
|
---|
[1990] | 651 |
|
---|
[2005] | 652 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
| 653 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
[2003] | 654 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
[1990] | 655 | }
|
---|
| 656 |
|
---|
| 657 |
|
---|
[1999] | 658 | //void GvsPreprocessor::VisualizeViewCell(ViewCell *vc)
|
---|
| 659 | void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects)
|
---|
[1990] | 660 | {
|
---|
| 661 | Intersectable::NewMail();
|
---|
| 662 |
|
---|
| 663 | Material m;
|
---|
[1528] | 664 |
|
---|
[1996] | 665 | char str[64]; sprintf(str, "pass%06d.wrl", mProcessedViewCells);
|
---|
[1990] | 666 |
|
---|
| 667 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
| 668 | if (!exporter)
|
---|
| 669 | return;
|
---|
| 670 |
|
---|
[1999] | 671 | /*ObjectPvsIterator pit = vc->GetPvs().GetIterator();
|
---|
[1990] | 672 |
|
---|
| 673 | // output PVS of view cell
|
---|
| 674 | while (pit.HasMoreEntries())
|
---|
| 675 | {
|
---|
| 676 | ObjectPvsEntry entry = pit.Next();
|
---|
| 677 |
|
---|
| 678 | Intersectable *intersect = entry.mObject;
|
---|
| 679 |
|
---|
| 680 | if (intersect->Mailed())
|
---|
| 681 | continue;
|
---|
| 682 |
|
---|
| 683 | intersect->Mail();
|
---|
| 684 | //m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 685 | m = RandomMaterial();
|
---|
| 686 | exporter->SetForcedMaterial(m);
|
---|
| 687 |
|
---|
| 688 | exporter->ExportIntersectable(intersect);
|
---|
[1999] | 689 | }*/
|
---|
| 690 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 691 |
|
---|
| 692 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 693 | {
|
---|
| 694 | Intersectable *intersect = *oit;
|
---|
| 695 |
|
---|
| 696 | m = RandomMaterial();
|
---|
| 697 | exporter->SetForcedMaterial(m);
|
---|
| 698 | exporter->ExportIntersectable(intersect);
|
---|
[1990] | 699 | }
|
---|
| 700 |
|
---|
| 701 | cout << "vssrays: " << (int)mVssRays.size() << endl;
|
---|
| 702 | ExportVssRays(exporter, mVssRays);
|
---|
| 703 |
|
---|
| 704 | /////////////////
|
---|
| 705 | //-- export view cell geometry
|
---|
| 706 |
|
---|
| 707 | exporter->SetWireframe();
|
---|
| 708 |
|
---|
| 709 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 710 | exporter->SetForcedMaterial(m);
|
---|
| 711 |
|
---|
[1999] | 712 | //mViewCellsManager->ExportViewCellGeometry(exporter, vc, NULL, NULL);
|
---|
| 713 | //mViewCellsManager->ExportViewCellGeometry(exporter, mCurrentViewCell, NULL, NULL);
|
---|
| 714 |
|
---|
| 715 | AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox;
|
---|
| 716 | exporter->ExportBox(bbox);
|
---|
[1990] | 717 | //exporter->SetFilled();
|
---|
| 718 |
|
---|
| 719 | DEL_PTR(exporter);
|
---|
[1460] | 720 | }
|
---|
| 721 |
|
---|
| 722 |
|
---|
[1982] | 723 | void GvsPreprocessor::VisualizeViewCells()
|
---|
| 724 | {
|
---|
[1996] | 725 | char str[64]; sprintf(str, "tmp/pass%06d_%04d-", mProcessedViewCells, mPass);
|
---|
[1982] | 726 |
|
---|
| 727 | // visualization
|
---|
| 728 | if (mGvsStats.mPassContribution > 0)
|
---|
| 729 | {
|
---|
| 730 | const bool exportRays = true;
|
---|
| 731 | const bool exportPvs = true;
|
---|
| 732 |
|
---|
| 733 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
| 734 | 10,
|
---|
| 735 | false,
|
---|
| 736 | exportPvs,
|
---|
| 737 | exportRays,
|
---|
| 738 | 1000,
|
---|
| 739 | str);
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | // remove pass samples
|
---|
| 743 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
| 744 |
|
---|
| 745 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
| 746 | {
|
---|
| 747 | (*vit)->DelRayRefs();
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 |
|
---|
[1990] | 752 | void GvsPreprocessor::ProcessViewCell()
|
---|
[1982] | 753 | {
|
---|
[1999] | 754 | //Intersectable::NewMail();
|
---|
| 755 | // compute direct intersections with view cell
|
---|
| 756 | IntersectWithViewCell();
|
---|
| 757 |
|
---|
[1990] | 758 | mGvsStats.mPerViewCellSamples = 0;
|
---|
[1996] | 759 | int oldContribution = mGvsStats.mTotalContribution;
|
---|
[1990] | 760 | int passSamples = 0;
|
---|
[1982] | 761 |
|
---|
[1996] | 762 | //while (mGvsStats.mPerViewCellSamples < mTotalSamples)
|
---|
| 763 | while (1)
|
---|
[1982] | 764 | {
|
---|
[1990] | 765 | // Ray queue empty =>
|
---|
| 766 | // cast a number of uniform samples to fill ray queue
|
---|
| 767 | int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
|
---|
[1982] | 768 |
|
---|
[1990] | 769 | if (!mOnlyRandomSampling)
|
---|
| 770 | newSamples += ProcessQueue();
|
---|
| 771 |
|
---|
| 772 | passSamples += newSamples;
|
---|
| 773 | mGvsStats.mPerViewCellSamples += newSamples;
|
---|
[2003] | 774 |
|
---|
[1996] | 775 |
|
---|
| 776 | if (passSamples >= mSamplesPerPass)
|
---|
[1982] | 777 | {
|
---|
| 778 | ++ mPass;
|
---|
| 779 |
|
---|
[1990] | 780 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 781 |
|
---|
[1982] | 782 | ////////
|
---|
| 783 | //-- stats
|
---|
| 784 |
|
---|
| 785 | mGvsStats.mPass = mPass;
|
---|
| 786 |
|
---|
[1996] | 787 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
|
---|
| 788 | cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
|
---|
[1990] | 789 |
|
---|
[1996] | 790 | // termination criterium
|
---|
| 791 | if (mGvsStats.mPassContribution < mMinContribution)
|
---|
| 792 | {
|
---|
| 793 | break;
|
---|
| 794 | }
|
---|
| 795 |
|
---|
[1990] | 796 | // reset
|
---|
| 797 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 798 | mGvsStats.mPassContribution = 0;
|
---|
| 799 | passSamples = 0;
|
---|
[1982] | 800 | }
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 |
|
---|
[1996] | 805 | void GvsPreprocessor::CompileViewCellsList()
|
---|
| 806 | {
|
---|
| 807 | while ((int)mViewCells.size() < mMaxViewCells)
|
---|
| 808 | {
|
---|
| 809 | if (0)
|
---|
| 810 | {
|
---|
| 811 | mViewCells.push_back(mViewCellsManager->GetViewCell((int)mViewCells.size()));
|
---|
[1999] | 812 | continue;
|
---|
[1996] | 813 | }
|
---|
[1999] | 814 |
|
---|
| 815 | // HACK
|
---|
| 816 | const int tries = 10000;
|
---|
| 817 | int i = 0;
|
---|
| 818 |
|
---|
| 819 | for (i = 0; i < tries; ++ i)
|
---|
[1996] | 820 | {
|
---|
[1999] | 821 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f);
|
---|
| 822 |
|
---|
| 823 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
[1996] | 824 |
|
---|
[1999] | 825 | if (!viewCell->Mailed())
|
---|
[1996] | 826 | {
|
---|
[1999] | 827 | viewCell->Mail();
|
---|
| 828 | break;
|
---|
| 829 | }
|
---|
[1996] | 830 |
|
---|
[1999] | 831 | mViewCells.push_back(viewCell);
|
---|
| 832 | }
|
---|
[1996] | 833 |
|
---|
[1999] | 834 | if (i == tries)
|
---|
| 835 | {
|
---|
| 836 | cerr << "big error! no view cell found" << endl;
|
---|
| 837 | return;
|
---|
| 838 | }
|
---|
| 839 | }
|
---|
| 840 | }
|
---|
[1996] | 841 |
|
---|
[1999] | 842 |
|
---|
| 843 | void GvsPreprocessor::IntersectWithViewCell()
|
---|
| 844 | {
|
---|
| 845 | mCurrentViewCell->GetMesh()->ComputeBoundingBox();
|
---|
| 846 | AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox;
|
---|
| 847 | //cout << "box: " << box << endl;
|
---|
| 848 | vector<KdLeaf *> leaves;
|
---|
| 849 |
|
---|
| 850 | mKdTree->GetBoxIntersections(box, leaves);
|
---|
| 851 |
|
---|
| 852 | vector<KdLeaf *>::const_iterator lit, lit_end = leaves.end();
|
---|
| 853 |
|
---|
| 854 | for (lit = leaves.begin(); lit != leaves.end(); ++ lit)
|
---|
| 855 | {
|
---|
| 856 | KdLeaf *leaf = *lit;
|
---|
| 857 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
| 858 |
|
---|
| 859 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 860 | {
|
---|
| 861 | TriangleIntersectable *triObj = dynamic_cast<TriangleIntersectable *>(*oit);
|
---|
| 862 |
|
---|
| 863 | if (box.Intersects(triObj->GetItem()))
|
---|
[1996] | 864 | {
|
---|
[1999] | 865 | if (!triObj->mCounter)
|
---|
| 866 | {
|
---|
| 867 | triObj->mCounter = 1;
|
---|
| 868 | mTrianglePvs.push_back(triObj);
|
---|
| 869 | }
|
---|
[1996] | 870 | }
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 |
|
---|
[1990] | 876 | void GvsPreprocessor::PerViewCellComputation()
|
---|
[1982] | 877 | {
|
---|
[1999] | 878 | // hack: reset counter
|
---|
| 879 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
| 880 |
|
---|
| 881 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 882 | {
|
---|
| 883 | (*oit)->mCounter = 0;
|
---|
| 884 | }
|
---|
[2003] | 885 | long lastTime = GetTime();
|
---|
[1999] | 886 |
|
---|
[1996] | 887 | while (NextViewCell())
|
---|
[1982] | 888 | {
|
---|
[1999] | 889 | cout << "\n***********************\n"
|
---|
| 890 | << "processing view cell " << mProcessedViewCells
|
---|
| 891 | << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
|
---|
[1982] | 892 |
|
---|
[1990] | 893 | // compute the pvs of the current view cell
|
---|
| 894 | ProcessViewCell();
|
---|
| 895 |
|
---|
[1999] | 896 | //mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
|
---|
| 897 | mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
|
---|
[2003] | 898 | mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
|
---|
[1996] | 899 |
|
---|
[1999] | 900 | ObjectContainer objectPvs;
|
---|
| 901 |
|
---|
[1990] | 902 | // exchange triangle pvs with objects
|
---|
[1999] | 903 | //UpdatePvs(mCurrentViewCell);
|
---|
| 904 | GetObjectPvs(objectPvs);
|
---|
[1990] | 905 |
|
---|
[1999] | 906 | cout << "triangle pvs of " << (int)mTrianglePvs.size() << " was converted to object pvs of " << (int)objectPvs.size() << endl;
|
---|
[1990] | 907 |
|
---|
[1982] | 908 | ////////
|
---|
| 909 | //-- stats
|
---|
[1990] | 910 |
|
---|
[1996] | 911 | mGvsStats.mViewCells = mProcessedViewCells;//mPass;
|
---|
[1999] | 912 | //mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
|
---|
[2003] | 913 | mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
|
---|
[1999] | 914 |
|
---|
| 915 | mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
|
---|
[1990] | 916 | mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
|
---|
[1982] | 917 |
|
---|
[2003] | 918 | // timing
|
---|
| 919 | const long currentTime = GetTime();
|
---|
| 920 |
|
---|
| 921 | mGvsStats.mTimePerViewCell = TimeDiff(lastTime, currentTime) * 1e-3f;
|
---|
| 922 | mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
|
---|
| 923 |
|
---|
| 924 | lastTime = currentTime;
|
---|
| 925 |
|
---|
[1982] | 926 | mGvsStats.Stop();
|
---|
| 927 | mGvsStats.Print(mGvsStatsStream);
|
---|
[1996] | 928 |
|
---|
[2003] | 929 | mTrianglePvs.clear();
|
---|
[1999] | 930 |
|
---|
| 931 | if (GVS_DEBUG)
|
---|
| 932 | {
|
---|
| 933 | //VisualizeViewCell(mCurrentViewCell);
|
---|
| 934 | VisualizeViewCell(objectPvs);
|
---|
| 935 | CLEAR_CONTAINER(mVssRays);
|
---|
| 936 | }
|
---|
| 937 |
|
---|
[1996] | 938 | // is this really necessary?
|
---|
| 939 | ClearRayQueue();
|
---|
[1990] | 940 | }
|
---|
| 941 | }
|
---|
[1982] | 942 |
|
---|
[1990] | 943 |
|
---|
| 944 | void GvsPreprocessor::UpdatePvs(ViewCell *currentViewCell)
|
---|
| 945 | {
|
---|
| 946 | ObjectPvs newPvs;
|
---|
| 947 | BvhLeaf::NewMail();
|
---|
| 948 |
|
---|
| 949 | ObjectPvsIterator pit = currentViewCell->GetPvs().GetIterator();
|
---|
| 950 |
|
---|
| 951 | // output PVS of view cell
|
---|
| 952 | while (pit.HasMoreEntries())
|
---|
| 953 | {
|
---|
| 954 | ObjectPvsEntry entry = pit.Next();
|
---|
| 955 |
|
---|
| 956 | Intersectable *intersect = entry.mObject;
|
---|
| 957 |
|
---|
| 958 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 959 |
|
---|
| 960 | if (!bv || bv->Mailed())
|
---|
| 961 | continue;
|
---|
| 962 |
|
---|
| 963 | bv->Mail();
|
---|
| 964 |
|
---|
| 965 | //m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 966 | newPvs.AddSampleDirty(bv, 1.0f);
|
---|
[1982] | 967 | }
|
---|
[1990] | 968 |
|
---|
| 969 | newPvs.SimpleSort();
|
---|
| 970 |
|
---|
| 971 | currentViewCell->SetPvs(newPvs);
|
---|
[1982] | 972 | }
|
---|
| 973 |
|
---|
[1999] | 974 |
|
---|
| 975 | void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const
|
---|
| 976 | {
|
---|
| 977 | BvhLeaf::NewMail();
|
---|
[1982] | 978 |
|
---|
[1999] | 979 | ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
|
---|
| 980 |
|
---|
| 981 | for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit)
|
---|
| 982 | {
|
---|
| 983 | Intersectable *intersect = *oit;
|
---|
| 984 |
|
---|
| 985 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 986 |
|
---|
| 987 | if (!bv || bv->Mailed())
|
---|
| 988 | continue;
|
---|
| 989 |
|
---|
| 990 | bv->Mail();
|
---|
| 991 |
|
---|
| 992 | objectPvs.push_back(bv);
|
---|
| 993 | // hack: reset counter
|
---|
| 994 | (*oit)->mCounter = 0;
|
---|
| 995 | }
|
---|
| 996 | }
|
---|
| 997 |
|
---|
| 998 |
|
---|
[1990] | 999 | void GvsPreprocessor::GlobalComputation()
|
---|
| 1000 | {
|
---|
| 1001 | int passSamples = 0;
|
---|
| 1002 | int oldContribution = 0;
|
---|
| 1003 |
|
---|
| 1004 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
| 1005 | {
|
---|
| 1006 | // Ray queue empty =>
|
---|
| 1007 | // cast a number of uniform samples to fill ray queue
|
---|
| 1008 | int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
|
---|
| 1009 |
|
---|
| 1010 | if (!mOnlyRandomSampling)
|
---|
| 1011 | newSamples += ProcessQueue();
|
---|
| 1012 |
|
---|
| 1013 | passSamples += newSamples;
|
---|
| 1014 | mGvsStats.mTotalSamples += newSamples;
|
---|
| 1015 |
|
---|
| 1016 | if (passSamples % (mSamplesPerPass + 1) == mSamplesPerPass)
|
---|
| 1017 | {
|
---|
| 1018 | ++ mPass;
|
---|
| 1019 |
|
---|
| 1020 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 1021 |
|
---|
| 1022 | ////////
|
---|
| 1023 | //-- stats
|
---|
| 1024 |
|
---|
| 1025 | //cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
|
---|
| 1026 | mGvsStats.mPass = mPass;
|
---|
| 1027 | mGvsStats.Stop();
|
---|
| 1028 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1029 |
|
---|
| 1030 | // reset
|
---|
| 1031 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1032 | mGvsStats.mPassContribution = 0;
|
---|
| 1033 | passSamples = 0;
|
---|
| 1034 |
|
---|
| 1035 | if (GVS_DEBUG)
|
---|
| 1036 | VisualizeViewCells();
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
| 1041 |
|
---|
[1489] | 1042 | bool GvsPreprocessor::ComputeVisibility()
|
---|
[1460] | 1043 | {
|
---|
[1533] | 1044 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
[1545] | 1045 | const long startTime = GetTime();
|
---|
[1533] | 1046 |
|
---|
[1990] | 1047 | //Randomize(0);
|
---|
[1934] | 1048 | mGvsStats.Reset();
|
---|
| 1049 | mGvsStats.Start();
|
---|
[1545] | 1050 |
|
---|
[1486] | 1051 | if (!mLoadViewCells)
|
---|
[1563] | 1052 | {
|
---|
| 1053 | /// construct the view cells from the scratch
|
---|
| 1054 | ConstructViewCells();
|
---|
[1580] | 1055 | // reset pvs already gathered during view cells construction
|
---|
| 1056 | mViewCellsManager->ResetPvs();
|
---|
[1563] | 1057 | cout << "finished view cell construction" << endl;
|
---|
[1486] | 1058 | }
|
---|
[1571] | 1059 | else if (0)
|
---|
[1563] | 1060 | {
|
---|
[1551] | 1061 | //-- test successful view cells loading by exporting them again
|
---|
| 1062 | VssRayContainer dummies;
|
---|
| 1063 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
[1563] | 1064 | mViewCellsManager->ExportViewCells("test.xml.gz", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
[1551] | 1065 | }
|
---|
[1460] | 1066 |
|
---|
[2005] | 1067 | cout << "here4 " << mKdTree->GetBox() << endl;
|
---|
| 1068 | cout << "here5 " << mViewCellsManager->GetViewSpaceBox() << endl;
|
---|
[1934] | 1069 | mGvsStats.Stop();
|
---|
| 1070 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1071 |
|
---|
[1982] | 1072 | if (mPerViewCell)
|
---|
[1473] | 1073 | {
|
---|
[1996] | 1074 | // list of view cells to compute
|
---|
| 1075 | CompileViewCellsList();
|
---|
| 1076 |
|
---|
[1982] | 1077 | PerViewCellComputation();
|
---|
[1460] | 1078 | }
|
---|
[1982] | 1079 | else
|
---|
| 1080 | {
|
---|
| 1081 | GlobalComputation();
|
---|
| 1082 | }
|
---|
[1460] | 1083 |
|
---|
[1934] | 1084 | cout << "cast " << 2 * mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl;
|
---|
[1545] | 1085 |
|
---|
[1934] | 1086 | if (GVS_DEBUG)
|
---|
| 1087 | {
|
---|
| 1088 | Visualize();
|
---|
| 1089 | CLEAR_CONTAINER(mVssRays);
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
[1473] | 1092 | return true;
|
---|
[1460] | 1093 | }
|
---|
| 1094 |
|
---|
[1522] | 1095 |
|
---|
| 1096 | void GvsPreprocessor::Visualize()
|
---|
| 1097 | {
|
---|
| 1098 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
| 1099 |
|
---|
| 1100 | if (!exporter)
|
---|
| 1101 | return;
|
---|
| 1102 |
|
---|
| 1103 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
[1999] | 1104 |
|
---|
[1522] | 1105 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
| 1106 | {
|
---|
[1524] | 1107 | exporter->SetWireframe();
|
---|
[1522] | 1108 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
| 1109 | //Material m;
|
---|
[1524] | 1110 | exporter->SetFilled();
|
---|
| 1111 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
| 1112 | exporter->ExportPolygon(&poly);
|
---|
[1522] | 1113 | }
|
---|
| 1114 |
|
---|
[1933] | 1115 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
[1999] | 1116 |
|
---|
[1933] | 1117 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 1118 | {
|
---|
| 1119 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
| 1120 | exporter->ExportIntersectable(obj);
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
[1990] | 1123 | ExportVssRays(exporter, mVssRays);
|
---|
| 1124 |
|
---|
[1522] | 1125 | delete exporter;
|
---|
[1460] | 1126 | }
|
---|
[1522] | 1127 |
|
---|
[1934] | 1128 |
|
---|
| 1129 | void GvsStatistics::Print(ostream &app) const
|
---|
| 1130 | {
|
---|
[2003] | 1131 | app << "#ViewCells\n" << mViewCells << endl;
|
---|
[1996] | 1132 | app << "#ViewCellId\n" << mViewCellId << endl;
|
---|
[2003] | 1133 | app << "#TotalTime\n" << mTotalTime << endl;
|
---|
| 1134 | app << "#TimePerViewCell\n" << mTimePerViewCell << endl;;
|
---|
| 1135 |
|
---|
| 1136 | app << "#RaysPerSec\n" << RaysPerSec() << endl;
|
---|
| 1137 |
|
---|
| 1138 | app << "#TrianglePvs\n" << mTrianglePvs << endl;
|
---|
| 1139 | app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
|
---|
| 1140 |
|
---|
| 1141 | app << "#PerViewCellPvs\n" << mPerViewCellPvs << endl;
|
---|
| 1142 | app << "#TotalPvs\n" << mTotalPvs << endl;
|
---|
| 1143 |
|
---|
| 1144 | app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
|
---|
[1934] | 1145 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
| 1146 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
[2003] | 1147 |
|
---|
| 1148 | //app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
| 1149 | //app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
| 1150 |
|
---|
| 1151 | //app << "#Pass\n" << mPass << endl;
|
---|
| 1152 | //app << "#ScDiff\n" << mPassContribution << endl;
|
---|
| 1153 | //app << "#GvsRuns\n" << mGvsPass << endl;
|
---|
| 1154 |
|
---|
| 1155 | app << endl;
|
---|
[1883] | 1156 | }
|
---|
[1934] | 1157 |
|
---|
| 1158 |
|
---|
| 1159 | }
|
---|