[2735] | 1 | #include "Environment.h"
|
---|
| 2 | #include "GvsPreprocessor.h"
|
---|
| 3 | #include "GlRenderer.h"
|
---|
| 4 | #include "VssRay.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
| 6 | #include "Triangle3.h"
|
---|
| 7 | #include "IntersectableWrapper.h"
|
---|
| 8 | #include "Plane3.h"
|
---|
| 9 | #include "RayCaster.h"
|
---|
| 10 | #include "Exporter.h"
|
---|
| 11 | #include "SamplingStrategy.h"
|
---|
| 12 | #include "BvHierarchy.h"
|
---|
| 13 | #include "Polygon3.h"
|
---|
| 14 | #include "IntersectableWrapper.h"
|
---|
| 15 | #include "Timer/PerfTimer.h"
|
---|
| 16 | #include "Vector2.h"
|
---|
| 17 | #include "RndGauss.h"
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | namespace GtpVisibilityPreprocessor
|
---|
| 22 | {
|
---|
| 23 |
|
---|
| 24 | #define GVS_DEBUG 0
|
---|
| 25 | #define NOT_ACCOUNTED_OBJECT 0
|
---|
| 26 | #define ACCOUNTED_OBJECT 2
|
---|
| 27 | #define DETERMINISTIC_GVS 0
|
---|
| 28 |
|
---|
| 29 | static const float MIN_DIST = 0.001f;
|
---|
| 30 |
|
---|
| 31 | static ObjectContainer myobjects;
|
---|
| 32 | static int sInvalidSamples = 0;
|
---|
| 33 |
|
---|
| 34 | ///////////
|
---|
| 35 | //-- timers
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | static PerfTimer rayTimer;
|
---|
| 39 | static PerfTimer kdPvsTimer;
|
---|
| 40 | static PerfTimer gvsTimer;
|
---|
| 41 | static PerfTimer initialTimer;
|
---|
| 42 | static PerfTimer intersectionTimer;
|
---|
| 43 | static PerfTimer preparationTimer;
|
---|
| 44 | static PerfTimer mainLoopTimer;
|
---|
| 45 | static PerfTimer contributionTimer;
|
---|
| 46 | static PerfTimer castTimer;
|
---|
| 47 | static PerfTimer generationTimer;
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /////////////////////
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | /** Visualization structure for the GVS algorithm.
|
---|
| 54 | */
|
---|
| 55 | struct VizStruct
|
---|
| 56 | {
|
---|
| 57 | Polygon3 *enlargedTriangle;
|
---|
| 58 | Triangle3 originalTriangle;
|
---|
| 59 | VssRay *ray;
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | static vector<VizStruct> vizContainer;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | GvsPreprocessor::GvsPreprocessor():
|
---|
| 68 | Preprocessor(),
|
---|
| 69 | mProcessedViewCells(0),
|
---|
| 70 | mCurrentViewCell(NULL),
|
---|
| 71 | mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f)),
|
---|
| 72 | mOnlyRandomSampling(false),
|
---|
| 73 | //mOnlyRandomSampling(true),
|
---|
| 74 | mUseProbablyVisibleSampling(false)
|
---|
| 75 | {
|
---|
| 76 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 77 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
| 78 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.gvsSamplesPerPass", mGvsSamplesPerPass);
|
---|
| 79 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
|
---|
| 80 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
| 81 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
| 82 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.perViewCell", mPerViewCell);
|
---|
| 83 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
|
---|
| 84 |
|
---|
| 85 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
|
---|
| 86 |
|
---|
| 87 | Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | char gvsStatsLog[100];
|
---|
| 91 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
| 92 | mGvsStatsStream.open(gvsStatsLog);
|
---|
| 93 |
|
---|
| 94 | cout << "number of gvs samples per pass: " << mGvsSamplesPerPass << endl;
|
---|
| 95 | cout << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
| 96 | cout << "only random sampling: " << mOnlyRandomSampling << endl;
|
---|
| 97 |
|
---|
| 98 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 99 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 100 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
| 101 | Debug << "threshold: " << mThreshold << endl;
|
---|
| 102 | Debug << "epsilon: " << mEps << endl;
|
---|
| 103 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
| 104 | Debug << "per view cell: " << mPerViewCell << endl;
|
---|
| 105 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
| 106 | Debug << "min contribution: " << mMinContribution << endl;
|
---|
| 107 |
|
---|
| 108 | mDistribution = new ViewCellBasedDistribution(*this, NULL);
|
---|
| 109 |
|
---|
| 110 | mGvsStats.Reset();
|
---|
| 111 |
|
---|
| 112 | // hack: some generic statistical values that can be used
|
---|
| 113 | // by an application using the preprocessor
|
---|
| 114 | for (int i = 0; i < 2; ++ i)
|
---|
| 115 | mGenericStats[i] = 0;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | GvsPreprocessor::~GvsPreprocessor()
|
---|
| 120 | {
|
---|
| 121 | delete mDistribution;
|
---|
| 122 | ClearRayQueue();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | void GvsPreprocessor::ClearRayQueue()
|
---|
| 127 | {
|
---|
| 128 | // clean ray queue
|
---|
| 129 | while (!mRayQueue.empty())
|
---|
| 130 | {
|
---|
| 131 | // handle next ray
|
---|
| 132 | VssRay *ray = mRayQueue.top();
|
---|
| 133 | mRayQueue.pop();
|
---|
| 134 |
|
---|
| 135 | // note: deletion of the ray is handled by ray pool
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | ViewCell *GvsPreprocessor::NextViewCell()
|
---|
| 141 | {
|
---|
| 142 | if (mProcessedViewCells == (int)mViewCells.size())
|
---|
| 143 | return NULL; // no more view cells
|
---|
| 144 |
|
---|
| 145 | ViewCell *vc = mViewCells[mProcessedViewCells];
|
---|
| 146 |
|
---|
| 147 | if (!vc->GetMesh())
|
---|
| 148 | mViewCellsManager->CreateMesh(vc);
|
---|
| 149 |
|
---|
| 150 | mGvsStats.mViewCellId = vc->GetId();
|
---|
| 151 |
|
---|
| 152 | Debug << "vc: " << vc->GetId() << endl;
|
---|
| 153 |
|
---|
| 154 | ++ mProcessedViewCells;
|
---|
| 155 |
|
---|
| 156 | return vc;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | int GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
| 161 | const Triangle3 &hitTriangle,
|
---|
| 162 | const VssRay &oldRay)
|
---|
| 163 | {
|
---|
| 164 | // the predicted hitpoint: we expect to hit the same mesh again
|
---|
| 165 | Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 166 |
|
---|
| 167 | float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
| 168 | float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
| 169 |
|
---|
| 170 | // distance large => this is likely to be a discontinuity
|
---|
| 171 | if (!((predictedLen - len) > mThreshold))
|
---|
| 172 | // q: rather use relative distance?
|
---|
| 173 | //if ((predictedLen / len) > mThreshold)
|
---|
| 174 | return 0;
|
---|
| 175 |
|
---|
| 176 | SimpleRay simpleRay;
|
---|
| 177 |
|
---|
| 178 | // apply reverse sampling to find the gap
|
---|
| 179 | if (!ComputeReverseRay(currentRay, hitTriangle, oldRay, simpleRay))
|
---|
| 180 | return 0;
|
---|
| 181 |
|
---|
| 182 | static VssRayContainer reverseRays;
|
---|
| 183 | reverseRays.clear();
|
---|
| 184 |
|
---|
| 185 | if (DETERMINISTIC_GVS)
|
---|
| 186 | {
|
---|
| 187 | VssRay *reverseRay =
|
---|
| 188 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
| 189 |
|
---|
| 190 | reverseRays.push_back(reverseRay);
|
---|
| 191 | }
|
---|
| 192 | else
|
---|
| 193 | {
|
---|
| 194 | if (0)
|
---|
| 195 | CastRayBundle4(simpleRay, reverseRays, mViewCellsManager->GetViewSpaceBox());
|
---|
| 196 | else
|
---|
| 197 | CastRayBundle16(simpleRay, reverseRays);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | mGvsStats.mReverseSamples += (int)reverseRays.size();
|
---|
| 201 |
|
---|
| 202 | EnqueueRays(reverseRays);
|
---|
| 203 |
|
---|
| 204 | return (int)reverseRays.size();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | int GvsPreprocessor::CountObject(Intersectable *triObj)
|
---|
| 209 | {
|
---|
| 210 | int numObjects = 0;
|
---|
| 211 |
|
---|
| 212 | if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) &&
|
---|
| 213 | (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
|
---|
| 214 | {
|
---|
| 215 | ++ triObj->mCounter;
|
---|
| 216 | ++ numObjects;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | mGenericStats[1] += numObjects;
|
---|
| 220 |
|
---|
| 221 | return numObjects;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
|
---|
| 226 | {
|
---|
| 227 | int numObj = 0;
|
---|
| 228 |
|
---|
| 229 | // count new objects in pvs due to kd node conversion
|
---|
| 230 | myobjects.clear();
|
---|
| 231 | // also gather duplicates to avoid mailing
|
---|
| 232 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), myobjects);
|
---|
| 233 |
|
---|
| 234 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
| 235 |
|
---|
| 236 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 237 | numObj += CountObject(*oit);
|
---|
| 238 |
|
---|
| 239 | mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt, numObj);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | void GvsPreprocessor::AddKdNodeToPvs(const Vector3 &termination)
|
---|
| 244 | {
|
---|
| 245 | kdPvsTimer.Entry();
|
---|
| 246 |
|
---|
| 247 | // exchange the triangle with the node in the pvs for kd pvs
|
---|
| 248 | KdNode *node = mKdTree->GetPvsNode(termination);
|
---|
| 249 |
|
---|
| 250 | //KdNode *node = mKdTree->GetPvsNodeCheck(ray.mTermination, obj);
|
---|
| 251 | //if (!node) cerr << "e " << obj->GetBox() << " " << ray.mTermination << endl; else
|
---|
| 252 | if (!node->Mailed())
|
---|
| 253 | {
|
---|
| 254 | // add to pvs
|
---|
| 255 | node->Mail();
|
---|
| 256 | KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
|
---|
| 257 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
| 258 |
|
---|
| 259 | if (QT_VISUALIZATION_SHOWN) UpdateStatsForVisualization(kdInt);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | kdPvsTimer.Exit();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | bool GvsPreprocessor::HasContribution(VssRay &ray)
|
---|
| 267 | {
|
---|
| 268 | if (!ray.mTerminationObject)
|
---|
| 269 | return false;
|
---|
| 270 |
|
---|
| 271 | contributionTimer.Entry();
|
---|
| 272 |
|
---|
| 273 | bool result;
|
---|
| 274 |
|
---|
| 275 | if (!mPerViewCell)
|
---|
| 276 | {
|
---|
| 277 | // store the rays + the intersected view cells
|
---|
| 278 | const bool storeViewCells = false;
|
---|
| 279 |
|
---|
| 280 | mViewCellsManager->ComputeSampleContribution(ray,
|
---|
| 281 | true,
|
---|
| 282 | storeViewCells,
|
---|
| 283 | true);
|
---|
| 284 |
|
---|
| 285 | result = ray.mPvsContribution > 0;
|
---|
| 286 | }
|
---|
| 287 | else // original per view cell gvs
|
---|
| 288 | {
|
---|
| 289 | // test if triangle was accounted for yet
|
---|
| 290 | result = AddTriangleObject(ray.mTerminationObject);
|
---|
| 291 |
|
---|
| 292 | if (mUseKdPvs)
|
---|
| 293 | AddKdNodeToPvs(ray.mTermination);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | contributionTimer.Exit();
|
---|
| 297 |
|
---|
| 298 | return result;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
| 303 | {
|
---|
| 304 | if (!vssRay || !HasContribution(*vssRay))
|
---|
| 305 | return false;
|
---|
| 306 |
|
---|
| 307 | if (0 && GVS_DEBUG)
|
---|
| 308 | mVssRays.push_back(new VssRay(*vssRay));
|
---|
| 309 |
|
---|
| 310 | // add new ray to ray queue
|
---|
| 311 | mRayQueue.push(vssRay);
|
---|
| 312 |
|
---|
| 313 | ++ mGvsStats.mTotalContribution;
|
---|
| 314 |
|
---|
| 315 | return true;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
| 320 | */
|
---|
| 321 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
| 322 | const Triangle3 &hitTriangle,
|
---|
| 323 | const VssRay &ray,
|
---|
| 324 | const int index) const
|
---|
| 325 | {
|
---|
| 326 | const int indexU = (index + 1) % 3;
|
---|
| 327 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 328 |
|
---|
| 329 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
| 330 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 331 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
| 332 |
|
---|
| 333 | const float len = Magnitude(a);
|
---|
| 334 |
|
---|
| 335 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
| 336 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
| 337 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 338 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
| 339 |
|
---|
| 340 | // compute the new three hit points
|
---|
| 341 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
| 342 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
| 343 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
| 344 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
| 345 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
| 346 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
| 347 |
|
---|
| 348 | vertices.push_back(pt2);
|
---|
| 349 | vertices.push_back(pt3);
|
---|
| 350 | vertices.push_back(pt1);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 |
|
---|
| 354 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
| 355 | const Triangle3 &hitTriangle,
|
---|
| 356 | const VssRay &ray) const
|
---|
| 357 | {
|
---|
| 358 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
| 359 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
| 360 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 |
|
---|
| 364 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
| 365 | const Triangle3 &hitTriangle,
|
---|
| 366 | const VssRay &oldRay) const
|
---|
| 367 | {
|
---|
| 368 | // find the intersection of the plane induced by the
|
---|
| 369 | // hit triangle with the new ray
|
---|
| 370 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 371 |
|
---|
| 372 | const Vector3 hitPt =
|
---|
| 373 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
| 374 |
|
---|
| 375 | return hitPt;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
| 380 | {
|
---|
| 381 | return a.mTerminationObject == b.mTerminationObject;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
| 386 | const Vector3 &p1,
|
---|
| 387 | const Vector3 &p2,
|
---|
| 388 | const VssRay &ray1,
|
---|
| 389 | const VssRay &ray2,
|
---|
| 390 | const VssRay &oldRay)
|
---|
| 391 | {
|
---|
| 392 | //cout <<"y"<<Magnitude(p1 - p2) << " ";
|
---|
| 393 | int castRays = 0;
|
---|
| 394 |
|
---|
| 395 | // cast reverse rays if necessary
|
---|
| 396 | castRays += CheckDiscontinuity(ray1, hitTriangle, oldRay);
|
---|
| 397 | castRays += CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
| 398 |
|
---|
| 399 | if (EqualVisibility(ray1, ray2) || (SqrMagnitude(p1 - p2) <= MIN_DIST))
|
---|
| 400 | {
|
---|
| 401 | return castRays;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | // the new subdivision point
|
---|
| 405 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
| 406 | //cout << "p: " << p << " " << p1 << " " << p2 << endl;
|
---|
| 407 |
|
---|
| 408 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
| 409 |
|
---|
| 410 | VssRay *newRay;
|
---|
| 411 |
|
---|
| 412 | // cast single ray
|
---|
| 413 | castTimer.Entry();
|
---|
| 414 |
|
---|
| 415 | // cast single ray to check if a triangle was missed. note: not efficient!!
|
---|
| 416 | newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
| 417 | castTimer.Exit();
|
---|
| 418 |
|
---|
| 419 | ++ castRays;
|
---|
| 420 |
|
---|
| 421 | // this ray will not be further processed
|
---|
| 422 | // note: ray deletion is handled by ray pool
|
---|
| 423 | if (!newRay) return castRays;
|
---|
| 424 |
|
---|
| 425 | rayTimer.Entry();
|
---|
| 426 |
|
---|
| 427 | // new triangle discovered? => add new ray to queue
|
---|
| 428 | HandleRay(newRay);
|
---|
| 429 |
|
---|
| 430 | rayTimer.Exit();
|
---|
| 431 |
|
---|
| 432 | //newRay->mFlags |= VssRay::BorderSample;
|
---|
| 433 |
|
---|
| 434 | // subdivide further
|
---|
| 435 | castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
| 436 | castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
| 437 |
|
---|
| 438 | return castRays;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 |
|
---|
| 442 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
| 443 | {
|
---|
| 444 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
| 445 |
|
---|
| 446 | // question matt: can this be possible?
|
---|
| 447 | if (!tObj) return 0;
|
---|
| 448 |
|
---|
| 449 | Triangle3 hitTriangle;
|
---|
| 450 |
|
---|
| 451 | // other types not implemented yet
|
---|
| 452 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 453 | hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 454 | else
|
---|
| 455 | cout << "border sampling: " << Intersectable::GetTypeName(tObj) << " not yet implemented" << endl;
|
---|
| 456 |
|
---|
| 457 | //cout << "type: " << Intersectable::GetTypeName(tObj) << endl;
|
---|
| 458 | generationTimer.Entry();
|
---|
| 459 |
|
---|
| 460 | static VertexContainer enlargedTriangle;
|
---|
| 461 | enlargedTriangle.clear();
|
---|
| 462 |
|
---|
| 463 | /// create 3 new hit points for each vertex
|
---|
| 464 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 465 |
|
---|
| 466 | /// create rays from sample points and handle them
|
---|
| 467 | static SimpleRayContainer simpleRays;
|
---|
| 468 | simpleRays.clear();
|
---|
| 469 |
|
---|
| 470 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
| 471 |
|
---|
| 472 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
| 473 | {
|
---|
| 474 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
| 475 |
|
---|
| 476 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
| 477 | simpleRays.AddRay(sr);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | if (0)
|
---|
| 481 | {
|
---|
| 482 | // visualize enlarged triangles
|
---|
| 483 | VizStruct dummy;
|
---|
| 484 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
| 485 | dummy.originalTriangle = hitTriangle;
|
---|
| 486 | vizContainer.push_back(dummy);
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
| 490 | //////////
|
---|
| 491 | //-- fill up simple rays with random rays so we can cast 16 rays at a time
|
---|
| 492 |
|
---|
| 493 | const int numRandomRays = 16 - (int)simpleRays.size();
|
---|
| 494 |
|
---|
| 495 | if (!DETERMINISTIC_GVS)
|
---|
| 496 | {
|
---|
| 497 | if (0)
|
---|
| 498 | {
|
---|
| 499 | SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(),
|
---|
| 500 | currentRay.GetNormalizedDir(),
|
---|
| 501 | SamplingStrategy::GVS,
|
---|
| 502 | 1.0f);
|
---|
| 503 |
|
---|
| 504 | GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0);
|
---|
| 505 | }
|
---|
| 506 | else
|
---|
| 507 | GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays);
|
---|
| 508 | }
|
---|
| 509 | else
|
---|
| 510 | GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples);
|
---|
| 511 |
|
---|
| 512 | generationTimer.Exit();
|
---|
| 513 |
|
---|
| 514 | /////////////////////
|
---|
| 515 | //-- cast rays to vertices and determine visibility
|
---|
| 516 |
|
---|
| 517 | static VssRayContainer vssRays;
|
---|
| 518 | vssRays.clear();
|
---|
| 519 |
|
---|
| 520 | // for the original implementation we don't cast double rays
|
---|
| 521 | const bool castDoubleRays = !mPerViewCell;
|
---|
| 522 | // cannot prune invalid rays because we have to compare adjacent rays
|
---|
| 523 | const bool pruneInvalidRays = false;
|
---|
| 524 |
|
---|
| 525 | //gvsTimer.Entry();
|
---|
| 526 | castTimer.Entry();
|
---|
| 527 |
|
---|
| 528 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
| 529 |
|
---|
| 530 | castTimer.Exit();
|
---|
| 531 | //gvsTimer.Exit();
|
---|
| 532 |
|
---|
| 533 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
| 534 | int castRays = (int)simpleRays.size();
|
---|
| 535 |
|
---|
| 536 | // handle cast rays
|
---|
| 537 | EnqueueRays(vssRays);
|
---|
| 538 |
|
---|
| 539 | if (0)
|
---|
| 540 | {
|
---|
| 541 | // set flags
|
---|
| 542 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
| 543 |
|
---|
| 544 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 545 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | // recursivly subdivide each edge
|
---|
| 549 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
| 550 | {
|
---|
| 551 | castRays += SubdivideEdge(hitTriangle,
|
---|
| 552 | enlargedTriangle[i],
|
---|
| 553 | enlargedTriangle[(i + 1) % numBorderSamples],
|
---|
| 554 | *vssRays[i],
|
---|
| 555 | *vssRays[(i + 1) % numBorderSamples],
|
---|
| 556 | currentRay);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | mGvsStats.mBorderSamples += castRays;
|
---|
| 560 |
|
---|
| 561 | return castRays;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 |
|
---|
| 565 | int GvsPreprocessor::AdaptiveBorderSamplingOpt(const VssRay ¤tRay)
|
---|
| 566 | {
|
---|
| 567 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
| 568 |
|
---|
| 569 | // question matt: can this be possible?
|
---|
| 570 | if (!tObj) return 0;
|
---|
| 571 |
|
---|
| 572 | Triangle3 hitTriangle;
|
---|
| 573 |
|
---|
| 574 | // other types not implemented yet
|
---|
| 575 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 576 | hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 577 | else
|
---|
| 578 | cout << "border sampling: " << Intersectable::GetTypeName(tObj) << " not yet implemented" << endl;
|
---|
| 579 |
|
---|
| 580 | generationTimer.Entry();
|
---|
| 581 |
|
---|
| 582 | static VertexContainer enlargedTriangle;
|
---|
| 583 | enlargedTriangle.clear();
|
---|
| 584 |
|
---|
| 585 | /// create 3 new hit points for each vertex
|
---|
| 586 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 587 |
|
---|
| 588 | static VertexContainer subdivEnlargedTri;
|
---|
| 589 | subdivEnlargedTri.clear();
|
---|
| 590 |
|
---|
| 591 | for (size_t i = 0; i < enlargedTriangle.size(); ++ i)
|
---|
| 592 | {
|
---|
| 593 | const Vector3 p1 = enlargedTriangle[i];
|
---|
| 594 | const Vector3 p2 = enlargedTriangle[(i + 1) % enlargedTriangle.size()];
|
---|
| 595 |
|
---|
| 596 | // the new subdivision point
|
---|
| 597 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
| 598 |
|
---|
| 599 | subdivEnlargedTri.push_back(p1);
|
---|
| 600 | subdivEnlargedTri.push_back(p);
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | /// create rays from sample points and handle them
|
---|
| 604 | static SimpleRayContainer simpleRays;
|
---|
| 605 | simpleRays.clear();
|
---|
| 606 |
|
---|
| 607 | VertexContainer::const_iterator vit, vit_end = subdivEnlargedTri.end();
|
---|
| 608 |
|
---|
| 609 | for (vit = subdivEnlargedTri.begin(); vit != vit_end; ++ vit)
|
---|
| 610 | {
|
---|
| 611 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
| 612 |
|
---|
| 613 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
| 614 | simpleRays.AddRay(sr);
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 |
|
---|
| 618 | //////////
|
---|
| 619 | //-- fill up simple rays with random rays so we can cast 16 rays at a time
|
---|
| 620 |
|
---|
| 621 | const int numRandomRays = 16 - ((int)simpleRays.size() % 16);
|
---|
| 622 |
|
---|
| 623 | SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(),
|
---|
| 624 | currentRay.GetNormalizedDir(),
|
---|
| 625 | SamplingStrategy::GVS, 1.0f);
|
---|
| 626 |
|
---|
| 627 | GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0);
|
---|
| 628 | //GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples);
|
---|
| 629 |
|
---|
| 630 | generationTimer.Exit();
|
---|
| 631 |
|
---|
| 632 |
|
---|
| 633 | /////////////////////
|
---|
| 634 | //-- cast rays to vertices and determine visibility
|
---|
| 635 |
|
---|
| 636 | static VssRayContainer vssRays;
|
---|
| 637 | vssRays.clear();
|
---|
| 638 |
|
---|
| 639 | // for the original implementation we don't cast double rays
|
---|
| 640 | const bool castDoubleRays = !mPerViewCell;
|
---|
| 641 | // cannot prune invalid rays because we have to compare adjacent rays
|
---|
| 642 | const bool pruneInvalidRays = false;
|
---|
| 643 |
|
---|
| 644 | //if ((simpleRays.size() % 16) != 0) cerr << "ray size not aligned" << endl;
|
---|
| 645 |
|
---|
| 646 | //gvsTimer.Entry();
|
---|
| 647 | castTimer.Entry();
|
---|
| 648 |
|
---|
| 649 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
| 650 |
|
---|
| 651 | castTimer.Exit();
|
---|
| 652 | //gvsTimer.Exit();
|
---|
| 653 |
|
---|
| 654 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
| 655 | int castRays = (int)simpleRays.size();
|
---|
| 656 |
|
---|
| 657 | // handle cast rays
|
---|
| 658 | EnqueueRays(vssRays);
|
---|
| 659 |
|
---|
| 660 | #if 0
|
---|
| 661 |
|
---|
| 662 | // recursivly subdivide each edge
|
---|
| 663 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
| 664 | {
|
---|
| 665 | castRays += SubdivideEdge(hitTriangle,
|
---|
| 666 | subdivEnlargedTri[i],
|
---|
| 667 | subdivEnlargedTri[(i + 1) % numBorderSamples],
|
---|
| 668 | *vssRays[i],
|
---|
| 669 | *vssRays[(i + 1) % numBorderSamples],
|
---|
| 670 | currentRay);
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | #endif
|
---|
| 674 |
|
---|
| 675 |
|
---|
| 676 | mGvsStats.mBorderSamples += castRays;
|
---|
| 677 |
|
---|
| 678 | return castRays;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 |
|
---|
| 682 | bool GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
| 683 | const Triangle3 &occluder,
|
---|
| 684 | const VssRay &oldRay,
|
---|
| 685 | Vector3 &newPoint) const
|
---|
| 686 | {
|
---|
| 687 | /////////////////////////
|
---|
| 688 | //-- We interserct the plane p = (xp, hit(x), hit(xold))
|
---|
| 689 | //-- with the newly found occluder (xold is the previous ray from
|
---|
| 690 | //-- which x was generated). On the intersecting line, we select a point
|
---|
| 691 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
| 692 | //-- just passes through the gap
|
---|
| 693 |
|
---|
| 694 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
| 695 | currentRay.GetTermination(),
|
---|
| 696 | oldRay.GetTermination());
|
---|
| 697 |
|
---|
| 698 | Vector3 pt1, pt2;
|
---|
| 699 |
|
---|
| 700 | if (!occluder.GetPlaneIntersection(plane, pt1, pt2))
|
---|
| 701 | return false;
|
---|
| 702 |
|
---|
| 703 | // get the intersection point on the old ray
|
---|
| 704 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
| 705 |
|
---|
| 706 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
| 707 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
| 708 |
|
---|
| 709 | // evaluate new hitpoint just outside the triangle
|
---|
| 710 | // the point is chosen to be on the side closer to the original ray
|
---|
| 711 | if (SqrDistance(pt1, pt3) < SqrDistance(pt2, pt3))
|
---|
| 712 | newPoint = pt1 + mEps * (pt1 - pt2);
|
---|
| 713 | else
|
---|
| 714 | newPoint = pt2 + mEps * (pt2 - pt1);
|
---|
| 715 |
|
---|
| 716 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
| 717 | return true;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 | bool GvsPreprocessor::ComputeReverseRay(const VssRay ¤tRay,
|
---|
| 722 | const Triangle3 &hitTriangle,
|
---|
| 723 | const VssRay &oldRay,
|
---|
| 724 | SimpleRay &reverseRay)
|
---|
| 725 | {
|
---|
| 726 | // get triangle occluding the path to the hit mesh
|
---|
| 727 | Triangle3 occluder;
|
---|
| 728 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
| 729 |
|
---|
| 730 | // q: why can this happen?
|
---|
| 731 | if (!tObj)
|
---|
| 732 | return false;
|
---|
| 733 |
|
---|
| 734 | // other types not implemented yet
|
---|
| 735 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 736 | occluder = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 737 | else
|
---|
| 738 | cout << "reverse sampling: " << tObj->Type() << " not yet implemented" << endl;
|
---|
| 739 |
|
---|
| 740 | // get a point which is passing just outside of the occluder
|
---|
| 741 | Vector3 newPoint;
|
---|
| 742 |
|
---|
| 743 | // q: why is there sometimes no intersecton found?
|
---|
| 744 | if (!GetPassingPoint(currentRay, occluder, oldRay, newPoint))
|
---|
| 745 | return false;
|
---|
| 746 |
|
---|
| 747 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 748 |
|
---|
| 749 | Vector3 newDir, newOrigin;
|
---|
| 750 |
|
---|
| 751 | //////////////
|
---|
| 752 | //-- Construct the mutated ray with xnew,
|
---|
| 753 | //-- dir = predicted(x)- pnew as direction vector
|
---|
| 754 |
|
---|
| 755 | newDir = predicted - newPoint;
|
---|
| 756 |
|
---|
| 757 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
| 758 | // difficult to say!!
|
---|
| 759 | const float offset = 0.5f;
|
---|
| 760 | newOrigin = newPoint - newDir * offset;
|
---|
| 761 |
|
---|
| 762 |
|
---|
| 763 | //////////////
|
---|
| 764 | //-- for per view cell sampling, we must check for intersection
|
---|
| 765 | //-- with the current view cell
|
---|
| 766 |
|
---|
| 767 | if (mPerViewCell)
|
---|
| 768 | {
|
---|
| 769 | // send ray to view cell
|
---|
| 770 | static Ray ray;
|
---|
| 771 |
|
---|
| 772 | ray.Clear();
|
---|
| 773 | ray.Init(newOrigin, -newDir, Ray::LOCAL_RAY);
|
---|
| 774 |
|
---|
| 775 | // check if ray intersects view cell
|
---|
| 776 | if (!mCurrentViewCell->CastRay(ray))
|
---|
| 777 | return NULL;
|
---|
| 778 |
|
---|
| 779 | Ray::Intersection &hit = ray.intersections[0];
|
---|
| 780 |
|
---|
| 781 | // the ray starts from the view cell
|
---|
| 782 | newOrigin = ray.Extrap(hit.mT);
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | reverseRay = SimpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
| 786 |
|
---|
| 787 | return true;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 |
|
---|
| 791 | int GvsPreprocessor::CastInitialSamples(int numSamples)
|
---|
| 792 | {
|
---|
| 793 | initialTimer.Entry();
|
---|
| 794 |
|
---|
| 795 | // generate simple rays
|
---|
| 796 | static SimpleRayContainer simpleRays;
|
---|
| 797 | simpleRays.clear();
|
---|
| 798 |
|
---|
| 799 | generationTimer.Entry();
|
---|
| 800 |
|
---|
| 801 | if (mUseProbablyVisibleSampling && (RandomValue(0, 1) > 0.5))
|
---|
| 802 | {
|
---|
| 803 | ProbablyVisibleDistribution distr(*this, mCurrentViewCell, &mProbablyVisibleTriangles);
|
---|
| 804 | GenerateRays(numSamples, distr, simpleRays, sInvalidSamples);
|
---|
| 805 | }
|
---|
| 806 | else
|
---|
| 807 | {
|
---|
| 808 | GenerateRays(DETERMINISTIC_GVS ? numSamples : numSamples / 16, *mDistribution, simpleRays, sInvalidSamples);
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 | generationTimer.Exit();
|
---|
| 812 |
|
---|
| 813 | // cast generated samples
|
---|
| 814 | static VssRayContainer vssRays;
|
---|
| 815 | vssRays.clear();
|
---|
| 816 |
|
---|
| 817 | castTimer.Entry();
|
---|
| 818 |
|
---|
| 819 | if (DETERMINISTIC_GVS)
|
---|
| 820 | {
|
---|
| 821 | const bool castDoubleRays = !mPerViewCell;
|
---|
| 822 | const bool pruneInvalidRays = false;
|
---|
| 823 | //const bool pruneInvalidRays = true;
|
---|
| 824 |
|
---|
| 825 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
| 826 | }
|
---|
| 827 | else
|
---|
| 828 | {
|
---|
| 829 | if (0)
|
---|
| 830 | // casting bundles of 4 rays generated from the simple rays
|
---|
| 831 | CastRayBundles4(simpleRays, vssRays);
|
---|
| 832 | else
|
---|
| 833 | CastRayBundles16(simpleRays, vssRays);
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 | castTimer.Exit();
|
---|
| 837 |
|
---|
| 838 | // add to ray queue
|
---|
| 839 | EnqueueRays(vssRays);
|
---|
| 840 |
|
---|
| 841 | initialTimer.Exit();
|
---|
| 842 |
|
---|
| 843 | return (int)vssRays.size();
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 |
|
---|
| 847 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
| 848 | {
|
---|
| 849 | rayTimer.Entry();
|
---|
| 850 |
|
---|
| 851 | // add samples to ray queue
|
---|
| 852 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
| 853 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
| 854 | {
|
---|
| 855 | VssRay *ray = *vit;
|
---|
| 856 | HandleRay(ray);
|
---|
| 857 | // note: deletion of invalid samples handked by ray pool
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | rayTimer.Exit();
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 |
|
---|
| 864 | int GvsPreprocessor::ProcessQueue()
|
---|
| 865 | {
|
---|
| 866 | gvsTimer.Entry();
|
---|
| 867 |
|
---|
| 868 | ++ mGvsStats.mGvsRuns;
|
---|
| 869 |
|
---|
| 870 | int castSamples = 0;
|
---|
| 871 |
|
---|
| 872 | while (!mRayQueue.empty())
|
---|
| 873 | {
|
---|
| 874 | // handle next ray
|
---|
| 875 | VssRay *ray = mRayQueue.top();
|
---|
| 876 | mRayQueue.pop();
|
---|
| 877 |
|
---|
| 878 | if (1)
|
---|
| 879 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
| 880 | else
|
---|
| 881 | castSamples += AdaptiveBorderSamplingOpt(*ray);
|
---|
| 882 | // note: don't have to delete because handled by ray pool
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | gvsTimer.Exit();
|
---|
| 886 |
|
---|
| 887 | return castSamples;
|
---|
| 888 | }
|
---|
| 889 |
|
---|
| 890 |
|
---|
| 891 | void ExportVssRays(Exporter *exporter, const VssRayContainer &vssRays)
|
---|
| 892 | {
|
---|
| 893 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
| 894 |
|
---|
| 895 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
| 896 |
|
---|
| 897 | // prepare some rays for output
|
---|
| 898 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 899 | {
|
---|
| 900 | //const float p = RandomValue(0.0f, (float)vssRays.size());
|
---|
| 901 |
|
---|
| 902 | if (1)//(p < raysOut)
|
---|
| 903 | {
|
---|
| 904 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
| 905 | vcRays.push_back(*rit);
|
---|
| 906 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
| 907 | vcRays2.push_back(*rit);
|
---|
| 908 | else
|
---|
| 909 | vcRays3.push_back(*rit);
|
---|
| 910 | }
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
| 914 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
| 915 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 |
|
---|
| 919 | void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects)
|
---|
| 920 | {
|
---|
| 921 | Intersectable::NewMail();
|
---|
| 922 | Material m;
|
---|
| 923 |
|
---|
| 924 | char str[64]; sprintf_s(str, "pass%06d.wrl", mProcessedViewCells);
|
---|
| 925 |
|
---|
| 926 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
| 927 | if (!exporter)
|
---|
| 928 | return;
|
---|
| 929 |
|
---|
| 930 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 931 |
|
---|
| 932 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 933 | {
|
---|
| 934 | Intersectable *intersect = *oit;
|
---|
| 935 |
|
---|
| 936 | m = RandomMaterial();
|
---|
| 937 | exporter->SetForcedMaterial(m);
|
---|
| 938 | exporter->ExportIntersectable(intersect);
|
---|
| 939 | }
|
---|
| 940 |
|
---|
| 941 | cout << "vssrays: " << (int)mVssRays.size() << endl;
|
---|
| 942 | ExportVssRays(exporter, mVssRays);
|
---|
| 943 |
|
---|
| 944 |
|
---|
| 945 | /////////////////
|
---|
| 946 | //-- export view cell geometry
|
---|
| 947 |
|
---|
| 948 | //exporter->SetWireframe();
|
---|
| 949 |
|
---|
| 950 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 951 | exporter->SetForcedMaterial(m);
|
---|
| 952 |
|
---|
| 953 | AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox;
|
---|
| 954 | exporter->ExportBox(bbox);
|
---|
| 955 | //exporter->SetFilled();
|
---|
| 956 |
|
---|
| 957 | delete exporter;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 |
|
---|
| 961 | void GvsPreprocessor::VisualizeViewCells()
|
---|
| 962 | {
|
---|
| 963 | char str[64]; sprintf_s(str, "tmp/pass%06d_%04d-", mProcessedViewCells, mPass);
|
---|
| 964 |
|
---|
| 965 | // visualization
|
---|
| 966 | if (mGvsStats.mPassContribution > 0)
|
---|
| 967 | {
|
---|
| 968 | const bool exportRays = true;
|
---|
| 969 | const bool exportPvs = true;
|
---|
| 970 |
|
---|
| 971 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
| 972 | 10,
|
---|
| 973 | false,
|
---|
| 974 | exportPvs,
|
---|
| 975 | exportRays,
|
---|
| 976 | 1000,
|
---|
| 977 | str);
|
---|
| 978 | }
|
---|
| 979 |
|
---|
| 980 | // remove pass samples
|
---|
| 981 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
| 982 |
|
---|
| 983 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
| 984 | {
|
---|
| 985 | (*vit)->DelRayRefs();
|
---|
| 986 | }
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 |
|
---|
| 990 | void GvsPreprocessor::CompileViewCellsFromPointList()
|
---|
| 991 | {
|
---|
| 992 | ViewCell::NewMail();
|
---|
| 993 |
|
---|
| 994 | // Receive list of view cells from view cells manager
|
---|
| 995 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
| 996 |
|
---|
| 997 | vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
|
---|
| 998 |
|
---|
| 999 | for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
|
---|
| 1000 | {
|
---|
| 1001 | ViewCellPoints *vp = *vit;
|
---|
| 1002 |
|
---|
| 1003 | if (vp->first) // view cell specified
|
---|
| 1004 | {
|
---|
| 1005 | ViewCell *viewCell = vp->first;
|
---|
| 1006 |
|
---|
| 1007 | if (!viewCell->Mailed())
|
---|
| 1008 | {
|
---|
| 1009 | viewCell->Mail();
|
---|
| 1010 | mViewCells.push_back(viewCell);
|
---|
| 1011 |
|
---|
| 1012 | if (mViewCells.size() >= mMaxViewCells)
|
---|
| 1013 | break;
|
---|
| 1014 | }
|
---|
| 1015 | }
|
---|
| 1016 | else // no view cell specified => compute view cells using view point
|
---|
| 1017 | {
|
---|
| 1018 | SimpleRayContainer::const_iterator rit, rit_end = vp->second.end();
|
---|
| 1019 |
|
---|
| 1020 | for (rit = vp->second.begin(); rit != rit_end; ++ rit)
|
---|
| 1021 | {
|
---|
| 1022 | SimpleRay ray = *rit;
|
---|
| 1023 |
|
---|
| 1024 | ViewCell *viewCell = mViewCellsManager->GetViewCell(ray.mOrigin);
|
---|
| 1025 |
|
---|
| 1026 | if (viewCell && !viewCell->Mailed())
|
---|
| 1027 | {
|
---|
| 1028 | viewCell->Mail();
|
---|
| 1029 | mViewCells.push_back(viewCell);
|
---|
| 1030 |
|
---|
| 1031 | if (mViewCells.size() >= mMaxViewCells)
|
---|
| 1032 | break;
|
---|
| 1033 | }
|
---|
| 1034 | }
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 |
|
---|
| 1040 | void GvsPreprocessor::CompileViewCellsList()
|
---|
| 1041 | {
|
---|
| 1042 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
| 1043 | {
|
---|
| 1044 | cout << "processing view point list" << endl;
|
---|
| 1045 | CompileViewCellsFromPointList();
|
---|
| 1046 | }
|
---|
| 1047 | else
|
---|
| 1048 | {
|
---|
| 1049 | ViewCell::NewMail();
|
---|
| 1050 |
|
---|
| 1051 | while ((int)mViewCells.size() < mMaxViewCells)
|
---|
| 1052 | {
|
---|
| 1053 | const int tries = 10000;
|
---|
| 1054 | int i = 0;
|
---|
| 1055 |
|
---|
| 1056 | for (i = 0; i < tries; ++ i)
|
---|
| 1057 | {
|
---|
| 1058 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f);
|
---|
| 1059 |
|
---|
| 1060 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
| 1061 |
|
---|
| 1062 | if (!viewCell->Mailed())
|
---|
| 1063 | {
|
---|
| 1064 | viewCell->Mail();
|
---|
| 1065 | break;
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 | mViewCells.push_back(viewCell);
|
---|
| 1069 | }
|
---|
| 1070 |
|
---|
| 1071 | if (i == tries)
|
---|
| 1072 | {
|
---|
| 1073 | cerr << "big error! no view cell found" << endl;
|
---|
| 1074 | break;
|
---|
| 1075 | }
|
---|
| 1076 | }
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | cout << "\ncomputing list of " << mViewCells.size() << " view cells" << endl;
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 |
|
---|
| 1083 | void GvsPreprocessor::ComputeViewCellGeometryIntersection()
|
---|
| 1084 | {
|
---|
| 1085 | intersectionTimer.Entry();
|
---|
| 1086 |
|
---|
| 1087 | AxisAlignedBox3 box = mCurrentViewCell->GetBox();
|
---|
| 1088 |
|
---|
| 1089 | int oldTrianglePvs = (int)mTrianglePvs.size();
|
---|
| 1090 | int newKdObj = 0;
|
---|
| 1091 |
|
---|
| 1092 | // compute pvs kd nodes that intersect view cell
|
---|
| 1093 | ObjectContainer kdobjects;
|
---|
| 1094 |
|
---|
| 1095 | // find intersecting objects not in pvs (i.e., only unmailed)
|
---|
| 1096 | mKdTree->CollectKdObjects(box, kdobjects);
|
---|
| 1097 |
|
---|
| 1098 | ObjectContainer pvsKdObjects;
|
---|
| 1099 |
|
---|
| 1100 | ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
|
---|
| 1101 |
|
---|
| 1102 | for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 1103 | {
|
---|
| 1104 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
|
---|
| 1105 |
|
---|
| 1106 | myobjects.clear();
|
---|
| 1107 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), myobjects);
|
---|
| 1108 |
|
---|
| 1109 | // account for kd object pvs
|
---|
| 1110 | bool addkdobj = false;
|
---|
| 1111 |
|
---|
| 1112 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
| 1113 |
|
---|
| 1114 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 1115 | {
|
---|
| 1116 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
|
---|
| 1117 |
|
---|
| 1118 | // test if the triangle itself intersects
|
---|
| 1119 | if (box.Intersects(triObj->GetItem()))
|
---|
| 1120 | addkdobj = AddTriangleObject(triObj);
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | // add node to kd pvs
|
---|
| 1124 | if (1 && addkdobj)
|
---|
| 1125 | {
|
---|
| 1126 | ++ newKdObj;
|
---|
| 1127 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
| 1128 | //mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
|
---|
| 1129 | if (QT_VISUALIZATION_SHOWN) UpdateStatsForVisualization(kdInt);
|
---|
| 1130 | }
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
| 1133 | cout << "added " << (int)mTrianglePvs.size() - oldTrianglePvs << " triangles (" << newKdObj << " objects) by intersection" << endl;
|
---|
| 1134 |
|
---|
| 1135 | intersectionTimer.Exit();
|
---|
| 1136 | }
|
---|
| 1137 |
|
---|
| 1138 |
|
---|
| 1139 | void GvsPreprocessor::PerViewCellComputation()
|
---|
| 1140 | {
|
---|
| 1141 | while (mCurrentViewCell = NextViewCell())
|
---|
| 1142 | {
|
---|
| 1143 | preparationTimer.Entry();
|
---|
| 1144 |
|
---|
| 1145 | // hack: reset counters (could be done with a mailing-like approach
|
---|
| 1146 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
| 1147 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 1148 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
| 1149 |
|
---|
| 1150 | preparationTimer.Exit();
|
---|
| 1151 |
|
---|
| 1152 | mDistribution->SetViewCell(mCurrentViewCell);
|
---|
| 1153 |
|
---|
| 1154 | ComputeViewCell();
|
---|
| 1155 | }
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 |
|
---|
| 1159 | void GvsPreprocessor::PerViewCellComputation2()
|
---|
| 1160 | {
|
---|
| 1161 | while (1)
|
---|
| 1162 | {
|
---|
| 1163 | if (!mRendererWidget)
|
---|
| 1164 | continue;
|
---|
| 1165 |
|
---|
| 1166 | mCurrentViewCell = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
|
---|
| 1167 |
|
---|
| 1168 | // no valid view cell or view cell already computed
|
---|
| 1169 | if (!mCurrentViewCell || !mCurrentViewCell->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
|
---|
| 1170 | continue;
|
---|
| 1171 |
|
---|
| 1172 | mRendererWidget->mComputeGVS = false;
|
---|
| 1173 | // hack: reset counters
|
---|
| 1174 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
| 1175 |
|
---|
| 1176 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 1177 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
| 1178 |
|
---|
| 1179 | ComputeViewCell();
|
---|
| 1180 | ++ mProcessedViewCells;
|
---|
| 1181 | }
|
---|
| 1182 | }
|
---|
| 1183 |
|
---|
| 1184 |
|
---|
| 1185 | void GvsPreprocessor::StorePvs(const ObjectContainer &objectPvs)
|
---|
| 1186 | {
|
---|
| 1187 | ObjectContainer::const_iterator oit, oit_end = objectPvs.end();
|
---|
| 1188 |
|
---|
| 1189 | for (oit = objectPvs.begin(); oit != oit_end; ++ oit)
|
---|
| 1190 | {
|
---|
| 1191 | mCurrentViewCell->GetPvs().AddSample(*oit, 1);
|
---|
| 1192 | }
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
| 1195 |
|
---|
| 1196 | void GvsPreprocessor::UpdatePvs(ViewCell *currentViewCell)
|
---|
| 1197 | {
|
---|
| 1198 | ObjectPvs newPvs;
|
---|
| 1199 | BvhLeaf::NewMail();
|
---|
| 1200 |
|
---|
| 1201 | ObjectPvsIterator pit = currentViewCell->GetPvs().GetIterator();
|
---|
| 1202 |
|
---|
| 1203 | // output PVS of view cell
|
---|
| 1204 | while (pit.HasMoreEntries())
|
---|
| 1205 | {
|
---|
| 1206 | Intersectable *intersect = pit.Next();
|
---|
| 1207 |
|
---|
| 1208 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 1209 |
|
---|
| 1210 | if (!bv || bv->Mailed())
|
---|
| 1211 | continue;
|
---|
| 1212 |
|
---|
| 1213 | bv->Mail();
|
---|
| 1214 |
|
---|
| 1215 | //m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 1216 | newPvs.AddSampleDirty(bv, 1.0f);
|
---|
| 1217 | }
|
---|
| 1218 |
|
---|
| 1219 | newPvs.SimpleSort();
|
---|
| 1220 |
|
---|
| 1221 | currentViewCell->SetPvs(newPvs);
|
---|
| 1222 | }
|
---|
| 1223 |
|
---|
| 1224 |
|
---|
| 1225 | void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const
|
---|
| 1226 | {
|
---|
| 1227 | BvhLeaf::NewMail();
|
---|
| 1228 |
|
---|
| 1229 | objectPvs.reserve((int)mTrianglePvs.size());
|
---|
| 1230 |
|
---|
| 1231 | ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
|
---|
| 1232 |
|
---|
| 1233 | for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit)
|
---|
| 1234 | {
|
---|
| 1235 | Intersectable *intersect = *oit;
|
---|
| 1236 |
|
---|
| 1237 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 1238 |
|
---|
| 1239 | // hack: reset counter
|
---|
| 1240 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
| 1241 |
|
---|
| 1242 | if (!bv || bv->Mailed())
|
---|
| 1243 | continue;
|
---|
| 1244 |
|
---|
| 1245 | bv->Mail();
|
---|
| 1246 | objectPvs.push_back(bv);
|
---|
| 1247 | }
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 |
|
---|
| 1251 | void GvsPreprocessor::GlobalComputation()
|
---|
| 1252 | {
|
---|
| 1253 | int passSamples = 0;
|
---|
| 1254 | int randomSamples = 0;
|
---|
| 1255 | int gvsSamples = 0;
|
---|
| 1256 | int oldContribution = 0;
|
---|
| 1257 |
|
---|
| 1258 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
| 1259 | {
|
---|
| 1260 | mRayCaster->InitPass();
|
---|
| 1261 |
|
---|
| 1262 | int newRandomSamples, newGvsSamples;
|
---|
| 1263 |
|
---|
| 1264 | // Ray queue empty =>
|
---|
| 1265 | // cast a number of uniform samples to fill ray queue
|
---|
| 1266 | newRandomSamples = CastInitialSamples(mInitialSamples);
|
---|
| 1267 |
|
---|
| 1268 | if (!mOnlyRandomSampling)
|
---|
| 1269 | newGvsSamples = ProcessQueue();
|
---|
| 1270 |
|
---|
| 1271 | passSamples += newRandomSamples + newGvsSamples;
|
---|
| 1272 | mGvsStats.mTotalSamples += newRandomSamples + newGvsSamples;
|
---|
| 1273 |
|
---|
| 1274 | mGvsStats.mRandomSamples += newRandomSamples;
|
---|
| 1275 | mGvsStats.mGvsSamples += newGvsSamples;
|
---|
| 1276 |
|
---|
| 1277 |
|
---|
| 1278 | if (passSamples % (mGvsSamplesPerPass + 1) == mGvsSamplesPerPass)
|
---|
| 1279 | {
|
---|
| 1280 | ++ mPass;
|
---|
| 1281 |
|
---|
| 1282 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 1283 |
|
---|
| 1284 | ////////
|
---|
| 1285 | //-- stats
|
---|
| 1286 |
|
---|
| 1287 | //cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
|
---|
| 1288 | mGvsStats.mPass = mPass;
|
---|
| 1289 | mGvsStats.Stop();
|
---|
| 1290 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1291 |
|
---|
| 1292 | // reset
|
---|
| 1293 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1294 | mGvsStats.mPassContribution = 0;
|
---|
| 1295 | passSamples = 0;
|
---|
| 1296 |
|
---|
| 1297 | if (GVS_DEBUG)
|
---|
| 1298 | VisualizeViewCells();
|
---|
| 1299 | }
|
---|
| 1300 | }
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 |
|
---|
| 1304 | bool GvsPreprocessor::ComputeVisibility()
|
---|
| 1305 | {
|
---|
| 1306 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
| 1307 | const long startTime = GetTime();
|
---|
| 1308 |
|
---|
| 1309 | //Randomize(0);
|
---|
| 1310 | mGvsStats.Reset();
|
---|
| 1311 | mGvsStats.Start();
|
---|
| 1312 |
|
---|
| 1313 | if (!mLoadViewCells)
|
---|
| 1314 | {
|
---|
| 1315 | /// construct the view cells from the scratch
|
---|
| 1316 | ConstructViewCells();
|
---|
| 1317 | // reset pvs already gathered during view cells construction
|
---|
| 1318 | mViewCellsManager->ResetPvs();
|
---|
| 1319 | cout << "finished view cell construction" << endl;
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
| 1322 | if (mPerViewCell)
|
---|
| 1323 | {
|
---|
| 1324 | #if 1
|
---|
| 1325 | // provide list of view cells to compute
|
---|
| 1326 | CompileViewCellsList();
|
---|
| 1327 |
|
---|
| 1328 | // start per view cell gvs
|
---|
| 1329 | PerViewCellComputation();
|
---|
| 1330 | #else
|
---|
| 1331 | PerViewCellComputation2();
|
---|
| 1332 |
|
---|
| 1333 | #endif
|
---|
| 1334 | }
|
---|
| 1335 | else
|
---|
| 1336 | {
|
---|
| 1337 | GlobalComputation();
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
| 1340 | cout << "cast " << mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M single rays/s" << endl;
|
---|
| 1341 |
|
---|
| 1342 | if (GVS_DEBUG)
|
---|
| 1343 | {
|
---|
| 1344 | Visualize();
|
---|
| 1345 | CLEAR_CONTAINER(mVssRays);
|
---|
| 1346 | }
|
---|
| 1347 |
|
---|
| 1348 | // export the preprocessed information to a file
|
---|
| 1349 | if (0 && mExportVisibility)
|
---|
| 1350 | ExportPreprocessedData(mVisibilityFileName);
|
---|
| 1351 |
|
---|
| 1352 | // compute the pixel error of this visibility solution
|
---|
| 1353 | if (0 && mEvaluatePixelError)
|
---|
| 1354 | ComputeRenderError();
|
---|
| 1355 |
|
---|
| 1356 | return true;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 |
|
---|
| 1360 | void GvsPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
| 1361 | {
|
---|
| 1362 | // store triangle directly
|
---|
| 1363 | mViewCellsManager->DeterminePvsObjects(rays, true);
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 |
|
---|
| 1367 | void GvsPreprocessor::Visualize()
|
---|
| 1368 | {
|
---|
| 1369 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
| 1370 |
|
---|
| 1371 | if (!exporter)
|
---|
| 1372 | return;
|
---|
| 1373 |
|
---|
| 1374 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
| 1375 |
|
---|
| 1376 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
| 1377 | {
|
---|
| 1378 | exporter->SetWireframe();
|
---|
| 1379 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
| 1380 | //Material m;
|
---|
| 1381 | exporter->SetFilled();
|
---|
| 1382 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
| 1383 | exporter->ExportPolygon(&poly);
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
| 1387 |
|
---|
| 1388 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 1389 | {
|
---|
| 1390 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
| 1391 | exporter->ExportIntersectable(obj);
|
---|
| 1392 | }
|
---|
| 1393 |
|
---|
| 1394 | ExportVssRays(exporter, mVssRays);
|
---|
| 1395 |
|
---|
| 1396 | delete exporter;
|
---|
| 1397 | }
|
---|
| 1398 |
|
---|
| 1399 |
|
---|
| 1400 | void GvsStatistics::Print(ostream &app) const
|
---|
| 1401 | {
|
---|
| 1402 | app << "#ViewCells\n" << mViewCells << endl;
|
---|
| 1403 | app << "#Id\n" << mViewCellId << endl;
|
---|
| 1404 | app << "#Time\n" << mTimePerViewCell << endl;;
|
---|
| 1405 | app << "#TriPvs\n" << mTrianglePvs << endl;
|
---|
| 1406 | app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
|
---|
| 1407 | app << "#UpdatedPvs\n" << (int)mPvsCost << endl;
|
---|
| 1408 |
|
---|
| 1409 | app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
|
---|
| 1410 | app << "#RaysPerSec\n" << RaysPerSec() << endl;
|
---|
| 1411 |
|
---|
| 1412 | app << "#TotalPvs\n" << mTotalPvs << endl;
|
---|
| 1413 | app << "#TotalTime\n" << mTotalTime << endl;
|
---|
| 1414 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
| 1415 |
|
---|
| 1416 | app << "#AvgPvs: " << mPvsCost / mViewCells << endl;
|
---|
| 1417 | app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
|
---|
| 1418 |
|
---|
| 1419 | if (0)
|
---|
| 1420 | {
|
---|
| 1421 | app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
| 1422 | app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
| 1423 |
|
---|
| 1424 | app << "#Pass\n" << mPass << endl;
|
---|
| 1425 | app << "#ScDiff\n" << mPassContribution << endl;
|
---|
| 1426 | app << "#GvsRuns\n" << mGvsRuns << endl;
|
---|
| 1427 |
|
---|
| 1428 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | app << endl;
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 |
|
---|
| 1435 | void GvsPreprocessor::ComputeViewCell()
|
---|
| 1436 | {
|
---|
| 1437 | const long startTime = GetTime();
|
---|
| 1438 |
|
---|
| 1439 | // initialise
|
---|
| 1440 | mGvsStats.mPerViewCellSamples = 0;
|
---|
| 1441 | mGvsStats.mRandomSamples = 0;
|
---|
| 1442 | mGvsStats.mGvsSamples = 0;
|
---|
| 1443 | mUseProbablyVisibleSampling = false;
|
---|
| 1444 | //renderer->mPvsStat.currentPass = 0;
|
---|
| 1445 |
|
---|
| 1446 | mPass = 0;
|
---|
| 1447 |
|
---|
| 1448 | int oldContribution = 0;
|
---|
| 1449 | int passSamples = 0;
|
---|
| 1450 |
|
---|
| 1451 | for (int i = 0; i < 2; ++ i)
|
---|
| 1452 | mGenericStats[i] = 0;
|
---|
| 1453 |
|
---|
| 1454 | mTrianglePvs.clear();
|
---|
| 1455 |
|
---|
| 1456 |
|
---|
| 1457 | // hack: take bounding box of view cell as view cell bounds
|
---|
| 1458 | mCurrentViewCell->GetMesh()->ComputeBoundingBox();
|
---|
| 1459 |
|
---|
| 1460 | // use mailing for kd node pvs
|
---|
| 1461 | KdNode::NewMail();
|
---|
| 1462 |
|
---|
| 1463 | cout << "\n***********************\n"
|
---|
| 1464 | << "computing view cell " << mProcessedViewCells
|
---|
| 1465 | << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
|
---|
| 1466 | cout << "bb: " << mCurrentViewCell->GetBox() << endl;
|
---|
| 1467 |
|
---|
| 1468 | mainLoopTimer.Entry();
|
---|
| 1469 |
|
---|
| 1470 | while (1)
|
---|
| 1471 | {
|
---|
| 1472 | sInvalidSamples = 0;
|
---|
| 1473 | int oldPvsSize = mCurrentViewCell->GetPvs().GetSize();
|
---|
| 1474 |
|
---|
| 1475 | mRayCaster->InitPass();
|
---|
| 1476 |
|
---|
| 1477 | int newRandomSamples, newGvsSamples, newSamples;
|
---|
| 1478 |
|
---|
| 1479 | // Ray queue empty =>
|
---|
| 1480 | // cast a number of uniform samples to fill ray queue
|
---|
| 1481 | newRandomSamples = CastInitialSamples(mInitialSamples);
|
---|
| 1482 |
|
---|
| 1483 | //if (!mUseProbablyVisibleSampling && !mOnlyRandomSampling)
|
---|
| 1484 | if (!mOnlyRandomSampling)
|
---|
| 1485 | newGvsSamples = ProcessQueue();
|
---|
| 1486 |
|
---|
| 1487 | newSamples = newRandomSamples + newGvsSamples;
|
---|
| 1488 |
|
---|
| 1489 | passSamples += newSamples;
|
---|
| 1490 | mGvsStats.mPerViewCellSamples += newSamples;
|
---|
| 1491 |
|
---|
| 1492 | mGvsStats.mRandomSamples += newRandomSamples;
|
---|
| 1493 | mGvsStats.mGvsSamples += newGvsSamples;
|
---|
| 1494 |
|
---|
| 1495 |
|
---|
| 1496 | if (passSamples >= mGvsSamplesPerPass)
|
---|
| 1497 | {
|
---|
| 1498 | if (GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
|
---|
| 1499 |
|
---|
| 1500 | //const bool convertPerPass = true;
|
---|
| 1501 | const bool convertPerPass = false;
|
---|
| 1502 |
|
---|
| 1503 | if (convertPerPass)
|
---|
| 1504 | {
|
---|
| 1505 | int newObjects = ConvertObjectPvs();
|
---|
| 1506 |
|
---|
| 1507 | cout << "added " << newObjects << " triangles to triangle pvs" << endl;
|
---|
| 1508 | mGvsStats.mTotalContribution += newObjects;
|
---|
| 1509 | }
|
---|
| 1510 |
|
---|
| 1511 | ++ mPass;
|
---|
| 1512 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 1513 |
|
---|
| 1514 | if (mGvsStats.mPassContribution < 2000)
|
---|
| 1515 | mUseProbablyVisibleSampling = true;
|
---|
| 1516 |
|
---|
| 1517 | if (mUseProbablyVisibleSampling)
|
---|
| 1518 | PrepareProbablyVisibleSampling();
|
---|
| 1519 |
|
---|
| 1520 |
|
---|
| 1521 | ////////
|
---|
| 1522 | //-- stats
|
---|
| 1523 |
|
---|
| 1524 | mGvsStats.mPass = mPass;
|
---|
| 1525 |
|
---|
| 1526 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
|
---|
| 1527 | cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
|
---|
| 1528 | cout << "obj contri=" << mCurrentViewCell->GetPvs().GetSize() - oldPvsSize << " (of " << mMinContribution << ")" << endl;
|
---|
| 1529 |
|
---|
| 1530 | // termination criterium
|
---|
| 1531 | if (mGvsStats.mPassContribution < mMinContribution)
|
---|
| 1532 | break;
|
---|
| 1533 |
|
---|
| 1534 | // reset stats
|
---|
| 1535 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1536 | mGvsStats.mPassContribution = 0;
|
---|
| 1537 | passSamples = 0;
|
---|
| 1538 |
|
---|
| 1539 | // compute the pixel error of this visibility solution
|
---|
| 1540 | if (0 && mEvaluatePixelError)
|
---|
| 1541 | ComputeRenderError();
|
---|
| 1542 | }
|
---|
| 1543 | }
|
---|
| 1544 |
|
---|
| 1545 | mainLoopTimer.Exit();
|
---|
| 1546 |
|
---|
| 1547 | // at last compute objects that directly intersect view cell
|
---|
| 1548 | ComputeViewCellGeometryIntersection();
|
---|
| 1549 |
|
---|
| 1550 | // compute the pixel error of this visibility solution
|
---|
| 1551 | if (mEvaluatePixelError)
|
---|
| 1552 | ComputeRenderError();
|
---|
| 1553 |
|
---|
| 1554 | ////////
|
---|
| 1555 | //-- stats
|
---|
| 1556 |
|
---|
| 1557 | // timing
|
---|
| 1558 | mGvsStats.mTimePerViewCell = TimeDiff(startTime, GetTime()) * 1e-3f;
|
---|
| 1559 |
|
---|
| 1560 | ComputeStats();
|
---|
| 1561 | }
|
---|
| 1562 |
|
---|
| 1563 |
|
---|
| 1564 | void GvsPreprocessor::ComputeStats()
|
---|
| 1565 | {
|
---|
| 1566 | // compute pvs size using larger (bvh objects)
|
---|
| 1567 | // note: for kd pvs we had to do this during gvs computation
|
---|
| 1568 | if (!mUseKdPvs)
|
---|
| 1569 | {
|
---|
| 1570 | ObjectContainer objectPvs;
|
---|
| 1571 |
|
---|
| 1572 | // optain object pvs
|
---|
| 1573 | GetObjectPvs(objectPvs);
|
---|
| 1574 |
|
---|
| 1575 | // add pvs
|
---|
| 1576 | ObjectContainer::const_iterator it, it_end = objectPvs.end();
|
---|
| 1577 |
|
---|
| 1578 | for (it = objectPvs.begin(); it != it_end; ++ it)
|
---|
| 1579 | {
|
---|
| 1580 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
| 1581 | }
|
---|
| 1582 |
|
---|
| 1583 | cout << "triangle pvs of " << (int)mTrianglePvs.size()
|
---|
| 1584 | << " was converted to object pvs of " << (int)objectPvs.size() << endl;
|
---|
| 1585 |
|
---|
| 1586 | mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
|
---|
| 1587 | mGvsStats.mPvsCost = 0; // todo objectPvs.EvalPvsCost();
|
---|
| 1588 | }
|
---|
| 1589 | else
|
---|
| 1590 | {
|
---|
| 1591 | if (0) // compute pvs kd nodes that intersect view cell
|
---|
| 1592 | {
|
---|
| 1593 | ObjectContainer mykdobjects;
|
---|
| 1594 |
|
---|
| 1595 | // extract kd pvs
|
---|
| 1596 | ObjectContainer::const_iterator it, it_end = mTrianglePvs.end();
|
---|
| 1597 |
|
---|
| 1598 | for (it = mTrianglePvs.begin(); it != it_end; ++ it)
|
---|
| 1599 | {
|
---|
| 1600 | Intersectable *obj = *it;
|
---|
| 1601 | // find intersecting objects not yet in pvs (i.e., only unmailed)
|
---|
| 1602 | mKdTree->CollectKdObjects(obj->GetBox(), mykdobjects);
|
---|
| 1603 | }
|
---|
| 1604 |
|
---|
| 1605 | // add pvs
|
---|
| 1606 | it_end = mykdobjects.end();
|
---|
| 1607 |
|
---|
| 1608 | for (it = mykdobjects.begin(); it != it_end; ++ it)
|
---|
| 1609 | {
|
---|
| 1610 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
| 1611 | }
|
---|
| 1612 |
|
---|
| 1613 | cout << "added " << (int)mykdobjects.size() << " new objects " << endl;
|
---|
| 1614 | }
|
---|
| 1615 |
|
---|
| 1616 | mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
|
---|
| 1617 | mGvsStats.mPvsCost = mCurrentViewCell->GetPvs().EvalPvsCost();
|
---|
| 1618 |
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
|
---|
| 1622 |
|
---|
| 1623 |
|
---|
| 1624 | ////////////////
|
---|
| 1625 | // global values
|
---|
| 1626 |
|
---|
| 1627 | mGvsStats.mViewCells = mProcessedViewCells;
|
---|
| 1628 | mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
|
---|
| 1629 | mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
|
---|
| 1630 | mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
|
---|
| 1631 | mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
|
---|
| 1632 |
|
---|
| 1633 | cout << "id: " << mCurrentViewCell->GetId() << endl;
|
---|
| 1634 | cout << "pvs cost " << mGvsStats.mPvsCost / 1000000 << " M" << endl;
|
---|
| 1635 | cout << "pvs tri: " << mTrianglePvs.size() << endl;
|
---|
| 1636 | cout << "samples: " << mGvsStats.mTotalSamples / 1000000 << " M" << endl;
|
---|
| 1637 | printf("contri: %f tri / K rays\n", 1000.0f * (float)mTrianglePvs.size() / mGvsStats.mTotalSamples);
|
---|
| 1638 |
|
---|
| 1639 | //cout << "invalid samples ratio: " << (float)sInvalidSamples / mGvsStats.mPerViewCellSamples << endl;
|
---|
| 1640 |
|
---|
| 1641 | double rayTime = rayTimer.TotalTime();
|
---|
| 1642 | double kdPvsTime = kdPvsTimer.TotalTime();
|
---|
| 1643 | double gvsTime = gvsTimer.TotalTime();
|
---|
| 1644 | double initialTime = initialTimer.TotalTime();
|
---|
| 1645 | double intersectionTime = intersectionTimer.TotalTime();
|
---|
| 1646 | double preparationTime = preparationTimer.TotalTime();
|
---|
| 1647 | double mainLoopTime = mainLoopTimer.TotalTime();
|
---|
| 1648 | double contributionTime = contributionTimer.TotalTime();
|
---|
| 1649 | double castTime = castTimer.TotalTime();
|
---|
| 1650 | double generationTime = generationTimer.TotalTime();
|
---|
| 1651 | double rawCastTime = mRayCaster->rawCastTimer.TotalTime();
|
---|
| 1652 |
|
---|
| 1653 | cout << "handleRay : " << rayTime << endl;
|
---|
| 1654 | cout << "kd pvs : " << kdPvsTime << endl;
|
---|
| 1655 | cout << "gvs sampling : " << gvsTime << endl;
|
---|
| 1656 | cout << "initial sampling : " << initialTime << endl;
|
---|
| 1657 | cout << "view cell intersection : " << intersectionTime << endl;
|
---|
| 1658 | cout << "preparation : " << preparationTime << endl;
|
---|
| 1659 | cout << "main loop : " << mainLoopTime << endl;
|
---|
| 1660 | cout << "has contribution : " << contributionTime << endl;
|
---|
| 1661 | cout << "cast time : " << castTime << endl;
|
---|
| 1662 | cout << "generation time : " << generationTime << endl;
|
---|
| 1663 | cout << "raw cast time : " << rawCastTime << endl;
|
---|
| 1664 |
|
---|
| 1665 | double randomRaysPerSec = mGvsStats.mRandomSamples / (1e6 * initialTime);
|
---|
| 1666 | double gvsRaysPerSec = mGvsStats.mGvsSamples / (1e6 * gvsTime);
|
---|
| 1667 | double rawRaysPerSec = mGvsStats.mPerViewCellSamples / (1e6 * rawCastTime);
|
---|
| 1668 |
|
---|
| 1669 | cout << "cast " << randomRaysPerSec << " M random rays/s: " << endl;
|
---|
| 1670 | cout << "cast " << gvsRaysPerSec << " M gvs rays/s: " << endl;
|
---|
| 1671 | cout << "cast " << rawRaysPerSec << " M raw rays/s: " << endl;
|
---|
| 1672 |
|
---|
| 1673 | mGvsStats.Stop();
|
---|
| 1674 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1675 | }
|
---|
| 1676 |
|
---|
| 1677 |
|
---|
| 1678 | int GvsPreprocessor::ConvertObjectPvs()
|
---|
| 1679 | {
|
---|
| 1680 | static ObjectContainer triangles;
|
---|
| 1681 |
|
---|
| 1682 | int newObjects = 0;
|
---|
| 1683 |
|
---|
| 1684 | ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
|
---|
| 1685 |
|
---|
| 1686 | triangles.clear();
|
---|
| 1687 |
|
---|
| 1688 | // output PVS of view cell
|
---|
| 1689 | while (pit.HasMoreEntries())
|
---|
| 1690 | {
|
---|
| 1691 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(pit.Next());
|
---|
| 1692 |
|
---|
| 1693 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), triangles);
|
---|
| 1694 |
|
---|
| 1695 | ObjectContainer::const_iterator oit, oit_end = triangles.end();
|
---|
| 1696 |
|
---|
| 1697 | for (oit = triangles.begin(); oit != oit_end; ++ oit)
|
---|
| 1698 | {
|
---|
| 1699 | if (AddTriangleObject(*oit))
|
---|
| 1700 | ++ newObjects;
|
---|
| 1701 | }
|
---|
| 1702 | }
|
---|
| 1703 |
|
---|
| 1704 | return newObjects;
|
---|
| 1705 | }
|
---|
| 1706 |
|
---|
| 1707 |
|
---|
| 1708 | bool GvsPreprocessor::AddTriangleObject(Intersectable *triObj)
|
---|
| 1709 | {
|
---|
| 1710 | if ((triObj->mCounter < ACCOUNTED_OBJECT))
|
---|
| 1711 | {
|
---|
| 1712 | triObj->mCounter += ACCOUNTED_OBJECT;
|
---|
| 1713 |
|
---|
| 1714 | mTrianglePvs.push_back(triObj);
|
---|
| 1715 | mGenericStats[0] = (int)mTrianglePvs.size();
|
---|
| 1716 |
|
---|
| 1717 | return true;
|
---|
| 1718 | }
|
---|
| 1719 |
|
---|
| 1720 | return false;
|
---|
| 1721 | }
|
---|
| 1722 |
|
---|
| 1723 |
|
---|
| 1724 | void GvsPreprocessor::CastRayBundles4(const SimpleRayContainer &rays, VssRayContainer &vssRays)
|
---|
| 1725 | {
|
---|
| 1726 | AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
| 1727 |
|
---|
| 1728 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 1729 |
|
---|
| 1730 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1731 | CastRayBundle4(*it, vssRays, box);
|
---|
| 1732 | }
|
---|
| 1733 |
|
---|
| 1734 |
|
---|
| 1735 | void GvsPreprocessor::CastRayBundle4(const SimpleRay &ray,
|
---|
| 1736 | VssRayContainer &vssRays,
|
---|
| 1737 | const AxisAlignedBox3 &box)
|
---|
| 1738 | {
|
---|
| 1739 | const float pertubDir = 0.01f;
|
---|
| 1740 | static Vector3 pertub;
|
---|
| 1741 |
|
---|
| 1742 | static int hit_triangles[4];
|
---|
| 1743 | static float dist[4];
|
---|
| 1744 | static Vector3 dirs[4];
|
---|
| 1745 | static Vector3 origs[4];
|
---|
| 1746 |
|
---|
| 1747 | origs[0] = ray.mOrigin;
|
---|
| 1748 | dirs[0] = ray.mDirection;
|
---|
| 1749 |
|
---|
| 1750 | for (int i = 1; i < 4; ++ i)
|
---|
| 1751 | {
|
---|
| 1752 | origs[i] = ray.mOrigin;
|
---|
| 1753 |
|
---|
| 1754 | pertub.x = RandomValue(-pertubDir, pertubDir);
|
---|
| 1755 | pertub.y = RandomValue(-pertubDir, pertubDir);
|
---|
| 1756 | pertub.z = RandomValue(-pertubDir, pertubDir);
|
---|
| 1757 |
|
---|
| 1758 | dirs[i] = ray.mDirection + pertub;
|
---|
| 1759 | dirs[i] *= 1.0f / Magnitude(dirs[i]);
|
---|
| 1760 | }
|
---|
| 1761 |
|
---|
| 1762 | Cast4Rays(dist, dirs, origs, vssRays, box);
|
---|
| 1763 | }
|
---|
| 1764 |
|
---|
| 1765 |
|
---|
| 1766 | void GvsPreprocessor::CastRays4(const SimpleRayContainer &rays, VssRayContainer &vssRays)
|
---|
| 1767 | {
|
---|
| 1768 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 1769 |
|
---|
| 1770 | static float dist[4];
|
---|
| 1771 | static Vector3 dirs[4];
|
---|
| 1772 | static Vector3 origs[4];
|
---|
| 1773 |
|
---|
| 1774 | AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
| 1775 |
|
---|
| 1776 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1777 | {
|
---|
| 1778 | for (int i = 1; i < 4; ++ i)
|
---|
| 1779 | {
|
---|
| 1780 | origs[i] = rays[i].mOrigin;
|
---|
| 1781 | dirs[i] = rays[i].mDirection;
|
---|
| 1782 | }
|
---|
| 1783 | }
|
---|
| 1784 |
|
---|
| 1785 | Cast4Rays(dist, dirs, origs, vssRays, box);
|
---|
| 1786 | }
|
---|
| 1787 |
|
---|
| 1788 |
|
---|
| 1789 | void GvsPreprocessor::Cast4Rays(float *dist,
|
---|
| 1790 | Vector3 *dirs,
|
---|
| 1791 | Vector3 *origs,
|
---|
| 1792 | VssRayContainer &vssRays,
|
---|
| 1793 | const AxisAlignedBox3 &box)
|
---|
| 1794 | {
|
---|
| 1795 | static int hit_triangles[4];
|
---|
| 1796 |
|
---|
| 1797 | mRayCaster->CastRaysPacket4(box.Min(), box.Max(), origs, dirs, hit_triangles, dist);
|
---|
| 1798 |
|
---|
| 1799 | VssRay *ray;
|
---|
| 1800 |
|
---|
| 1801 | for (int i = 0; i < 4; ++ i)
|
---|
| 1802 | {
|
---|
| 1803 | if (hit_triangles[i] != -1)
|
---|
| 1804 | {
|
---|
| 1805 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(mObjects[hit_triangles[i]]);
|
---|
| 1806 |
|
---|
| 1807 | if (DotProd(triObj->GetItem().GetNormal(), dirs[i] >= 0))
|
---|
| 1808 | ray = NULL;
|
---|
| 1809 | else
|
---|
| 1810 | ray = mRayCaster->RequestRay(origs[i], origs[i] + dirs[i] * dist[i], NULL, triObj, mPass, 1.0f);
|
---|
| 1811 | }
|
---|
| 1812 | else
|
---|
| 1813 | {
|
---|
| 1814 | ray = NULL;
|
---|
| 1815 | }
|
---|
| 1816 |
|
---|
| 1817 | vssRays.push_back(ray);
|
---|
| 1818 | }
|
---|
| 1819 | }
|
---|
| 1820 |
|
---|
| 1821 |
|
---|
| 1822 | void GvsPreprocessor::CastRayBundle16(const SimpleRay &ray, VssRayContainer &vssRays)
|
---|
| 1823 | {
|
---|
| 1824 | static SimpleRayContainer simpleRays;
|
---|
| 1825 | simpleRays.clear();
|
---|
| 1826 |
|
---|
| 1827 | simpleRays.push_back(ray);
|
---|
| 1828 | GenerateJitteredRays(simpleRays, ray, 15, 0);
|
---|
| 1829 |
|
---|
| 1830 | CastRays(simpleRays, vssRays, false, false);
|
---|
| 1831 | }
|
---|
| 1832 |
|
---|
| 1833 |
|
---|
| 1834 | void GvsPreprocessor::CastRayBundles16(const SimpleRayContainer &rays, VssRayContainer &vssRays)
|
---|
| 1835 | {
|
---|
| 1836 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 1837 |
|
---|
| 1838 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1839 | CastRayBundle16(*it, vssRays);
|
---|
| 1840 | }
|
---|
| 1841 |
|
---|
| 1842 |
|
---|
| 1843 | void GvsPreprocessor::ComputeRenderError()
|
---|
| 1844 | {
|
---|
| 1845 | // compute rendering error
|
---|
| 1846 | if (renderer && renderer->mPvsStatFrames)
|
---|
| 1847 | {
|
---|
| 1848 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
| 1849 | {
|
---|
| 1850 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
| 1851 | ViewCellPointsList::const_iterator vit = vcPoints->begin(), vit_end = vcPoints->end();
|
---|
| 1852 |
|
---|
| 1853 | SimpleRayContainer viewPoints;
|
---|
| 1854 |
|
---|
| 1855 | for (; vit != vit_end; ++ vit)
|
---|
| 1856 | {
|
---|
| 1857 | ViewCellPoints *vp = *vit;
|
---|
| 1858 |
|
---|
| 1859 | SimpleRayContainer::const_iterator rit = vp->second.begin(), rit_end = vp->second.end();
|
---|
| 1860 |
|
---|
| 1861 | for (; rit!=rit_end; ++rit)
|
---|
| 1862 | {
|
---|
| 1863 | ViewCell *vc = mViewCellsManager->GetViewCell((*rit).mOrigin);
|
---|
| 1864 | if (vc == mCurrentViewCell)
|
---|
| 1865 | viewPoints.push_back(*rit);
|
---|
| 1866 | }
|
---|
| 1867 | }
|
---|
| 1868 |
|
---|
| 1869 | renderer->mPvsErrorBuffer.clear();
|
---|
| 1870 |
|
---|
| 1871 | if (viewPoints.size() != renderer->mPvsErrorBuffer.size())
|
---|
| 1872 | {
|
---|
| 1873 | renderer->mPvsErrorBuffer.resize(viewPoints.size());
|
---|
| 1874 | renderer->ClearErrorBuffer();
|
---|
| 1875 | }
|
---|
| 1876 |
|
---|
| 1877 | cout << "evaluating list of " << viewPoints.size() << " pts" << endl;
|
---|
| 1878 | renderer->EvalPvsStat(viewPoints);
|
---|
| 1879 | }
|
---|
| 1880 | else
|
---|
| 1881 | {
|
---|
| 1882 | cout << "evaluating random points" << endl;
|
---|
| 1883 | renderer->EvalPvsStat();
|
---|
| 1884 | }
|
---|
| 1885 |
|
---|
| 1886 | mStats <<
|
---|
| 1887 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
| 1888 | "#AvgPixelError\n" <<renderer->GetAvgPixelError()<<endl<<
|
---|
| 1889 | "#MaxPixelError\n" <<renderer->GetMaxPixelError()<<endl<<
|
---|
| 1890 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
| 1891 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
| 1892 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
| 1893 | }
|
---|
| 1894 | }
|
---|
| 1895 |
|
---|
| 1896 |
|
---|
| 1897 | void GvsPreprocessor::GenerateImportanceSamples(const VssRay &ray,
|
---|
| 1898 | const Triangle3 &triangle,
|
---|
| 1899 | int numSamples,
|
---|
| 1900 | SimpleRayContainer &simpleRays)
|
---|
| 1901 | {
|
---|
| 1902 | const size_t samplesSize = simpleRays.size();
|
---|
| 1903 |
|
---|
| 1904 | while (simpleRays.size() < (samplesSize + numSamples))
|
---|
| 1905 | {
|
---|
| 1906 | SimpleRay sray;
|
---|
| 1907 | if (GenerateImportanceSample(ray, triangle, sray))
|
---|
| 1908 | simpleRays.push_back(sray);
|
---|
| 1909 | }
|
---|
| 1910 | }
|
---|
| 1911 |
|
---|
| 1912 |
|
---|
| 1913 | bool GvsPreprocessor::GenerateImportanceSample(const VssRay &ray,
|
---|
| 1914 | const Triangle3 &triangle,
|
---|
| 1915 | SimpleRay &sray)
|
---|
| 1916 | {
|
---|
| 1917 | Vector3 d = ray.GetDir();
|
---|
| 1918 |
|
---|
| 1919 | // Compute right handed coordinate system from direction
|
---|
| 1920 | Vector3 U, V;
|
---|
| 1921 |
|
---|
| 1922 | Vector3 nd = Normalize(d);
|
---|
| 1923 | nd.RightHandedBase(U, V);
|
---|
| 1924 |
|
---|
| 1925 | Vector3 origin = ray.mOrigin;
|
---|
| 1926 | Vector3 termination = ray.mTermination;
|
---|
| 1927 |
|
---|
| 1928 | float rr[2];
|
---|
| 1929 |
|
---|
| 1930 | rr[0] = RandomValue(0, 1);
|
---|
| 1931 | rr[1] = RandomValue(0, 1);
|
---|
| 1932 |
|
---|
| 1933 | Vector2 vr2(rr[0], rr[1]);
|
---|
| 1934 | const float sigma = triangle.GetBoundingBox().Radius() * 5.0f;
|
---|
| 1935 | Vector2 gaussvec2;
|
---|
| 1936 |
|
---|
| 1937 | GaussianOn2D(vr2, sigma, // input
|
---|
| 1938 | gaussvec2); // output
|
---|
| 1939 |
|
---|
| 1940 | const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
| 1941 |
|
---|
| 1942 | //cout << "t: " << termination;
|
---|
| 1943 | termination += shift;
|
---|
| 1944 | //cout << " new t: " << termination << endl;
|
---|
| 1945 | Vector3 direction = termination - origin;
|
---|
| 1946 |
|
---|
| 1947 | const float len = Magnitude(direction);
|
---|
| 1948 |
|
---|
| 1949 | if (len < Limits::Small)
|
---|
| 1950 | return false;
|
---|
| 1951 |
|
---|
| 1952 | direction /= len;
|
---|
| 1953 |
|
---|
| 1954 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 1955 | const float pdf = 1.0f;
|
---|
| 1956 | sray = SimpleRay(origin, direction, SamplingStrategy::GVS, pdf);
|
---|
| 1957 |
|
---|
| 1958 | return true;
|
---|
| 1959 | }
|
---|
| 1960 |
|
---|
| 1961 |
|
---|
| 1962 | void GvsPreprocessor::PrepareProbablyVisibleSampling()
|
---|
| 1963 | {
|
---|
| 1964 |
|
---|
| 1965 | // warning: using mailing!
|
---|
| 1966 | Intersectable::NewMail();
|
---|
| 1967 |
|
---|
| 1968 | mProbablyVisibleTriangles.clear();
|
---|
| 1969 | CollectProbablyVisibleTriangles(mProbablyVisibleTriangles);
|
---|
| 1970 | }
|
---|
| 1971 |
|
---|
| 1972 |
|
---|
| 1973 | void GvsPreprocessor::CollectProbablyVisibleTriangles(ObjectContainer &triangles)
|
---|
| 1974 | {
|
---|
| 1975 | ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
|
---|
| 1976 |
|
---|
| 1977 | static ObjectContainer tmpTriangles;
|
---|
| 1978 |
|
---|
| 1979 | while (pit.HasMoreEntries())
|
---|
| 1980 | {
|
---|
| 1981 | tmpTriangles.clear();
|
---|
| 1982 |
|
---|
| 1983 | KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
|
---|
| 1984 | mKdTree->CollectObjectsWithDublicates(kdObj->GetItem(), tmpTriangles);
|
---|
| 1985 |
|
---|
| 1986 | ObjectContainer::const_iterator oit, oit_end = tmpTriangles.end();
|
---|
| 1987 |
|
---|
| 1988 | for (oit = tmpTriangles.begin(); oit != oit_end; ++ oit)
|
---|
| 1989 | {
|
---|
| 1990 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
|
---|
| 1991 |
|
---|
| 1992 | // find objects which are not yet accounted for yet contained in kd pvs objects
|
---|
| 1993 | if (!triObj->Mailed() && (triObj->mCounter < ACCOUNTED_OBJECT))
|
---|
| 1994 | {
|
---|
| 1995 | triObj->Mail();
|
---|
| 1996 | triangles.push_back(triObj);
|
---|
| 1997 | }
|
---|
| 1998 | }
|
---|
| 1999 | }
|
---|
| 2000 | }
|
---|
| 2001 |
|
---|
| 2002 |
|
---|
| 2003 | }
|
---|