[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"
|
---|
[1460] | 9 |
|
---|
[1473] | 10 |
|
---|
[1500] | 11 |
|
---|
[1460] | 12 | namespace GtpVisibilityPreprocessor
|
---|
| 13 | {
|
---|
| 14 |
|
---|
| 15 |
|
---|
[1473] | 16 | GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
|
---|
[1460] | 17 | {
|
---|
[1486] | 18 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 19 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
| 20 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 21 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
[1500] | 22 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
[1473] | 23 |
|
---|
[1486] | 24 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 25 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 26 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
| 27 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
[1501] | 28 | Debug << "threshold: " << mThreshold << endl;
|
---|
| 29 | Debug << "eps: " << mEps << endl;
|
---|
[1486] | 30 |
|
---|
| 31 | mStats.open("gvspreprocessor.log");
|
---|
[1460] | 32 | }
|
---|
| 33 |
|
---|
[1473] | 34 |
|
---|
[1500] | 35 | bool GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
| 36 | const Triangle3 &hitTriangle,
|
---|
| 37 | const VssRay &oldRay)
|
---|
[1460] | 38 | {
|
---|
[1500] | 39 | const float dist = Magnitude(oldRay.GetDir());
|
---|
| 40 | const float newDist = Magnitude(currentRay.GetDir());
|
---|
| 41 |
|
---|
| 42 | #if 0
|
---|
| 43 | if ((dist - newDist) > mThresHold)
|
---|
| 44 | #else // rather take relative distance
|
---|
| 45 | if ((dist / newDist) > mThreshold)
|
---|
| 46 | #endif
|
---|
| 47 | {
|
---|
| 48 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
| 49 | HandleRay(*newRay);
|
---|
| 50 | return true;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[1486] | 53 | return false;
|
---|
| 54 | }
|
---|
[1473] | 55 |
|
---|
[1486] | 56 |
|
---|
[1500] | 57 | bool GvsPreprocessor::HandleRay(VssRay &vssRay)
|
---|
[1486] | 58 | {
|
---|
[1501] | 59 | if (mViewCellsManager->ComputeSampleContribution(vssRay, true, false))
|
---|
[1473] | 60 | {
|
---|
[1501] | 61 | //cout << "h";
|
---|
[1500] | 62 | mRayQueue.push(&vssRay);
|
---|
| 63 | return true;
|
---|
[1473] | 64 | }
|
---|
[1500] | 65 |
|
---|
| 66 | return false;
|
---|
[1460] | 67 | }
|
---|
| 68 |
|
---|
[1500] | 69 |
|
---|
| 70 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
[1489] | 71 | */
|
---|
[1500] | 72 | static void CreateNewVertices(VertexContainer &vertices,
|
---|
| 73 | const Triangle3 &hitTriangle,
|
---|
| 74 | const VssRay &ray,
|
---|
| 75 | const int index,
|
---|
| 76 | const float eps)
|
---|
[1460] | 77 | {
|
---|
[1486] | 78 | const int indexU = (index + 1) % 3;
|
---|
| 79 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 80 |
|
---|
| 81 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetDir();
|
---|
| 82 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 83 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
[1492] | 84 |
|
---|
[1486] | 85 | const float len = Magnitude(a);
|
---|
| 86 |
|
---|
[1492] | 87 | const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
|
---|
| 88 | const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
|
---|
[1500] | 89 | const Vector3 dir3 = DotProd(dir1, dir2) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 90 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a));
|
---|
[1492] | 91 |
|
---|
[1486] | 92 | // compute the new three hit points
|
---|
[1500] | 93 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
| 94 | const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1;
|
---|
| 95 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
| 96 | const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2;
|
---|
| 97 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
| 98 | const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3;
|
---|
[1489] | 99 |
|
---|
[1500] | 100 | vertices.push_back(pt1);
|
---|
| 101 | vertices.push_back(pt2);
|
---|
| 102 | vertices.push_back(pt3);
|
---|
[1489] | 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[1500] | 106 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
| 107 | const Triangle3 &hitTriangle,
|
---|
| 108 | const VssRay &ray)
|
---|
| 109 | {
|
---|
| 110 | CreateNewVertices(vertices, hitTriangle, ray, 0, mEps);
|
---|
| 111 | CreateNewVertices(vertices, hitTriangle, ray, 1, mEps);
|
---|
| 112 | CreateNewVertices(vertices, hitTriangle, ray, 2, mEps);
|
---|
| 113 | }
|
---|
[1492] | 114 |
|
---|
[1500] | 115 |
|
---|
| 116 | static Vector3 CalcPredictedHitPoint(const VssRay &newRay,
|
---|
| 117 | const Triangle3 &hitTriangle,
|
---|
| 118 | const VssRay &oldRay)
|
---|
[1489] | 119 | {
|
---|
[1500] | 120 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 121 |
|
---|
| 122 | const Vector3 hitPt =
|
---|
| 123 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
| 124 |
|
---|
| 125 | return hitPt;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
| 130 | {
|
---|
| 131 | return a.mTerminationObject == b.mTerminationObject;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
| 136 | const Vector3 &p1,
|
---|
| 137 | const Vector3 &p2,
|
---|
| 138 | const VssRay &x,
|
---|
| 139 | const VssRay &y,
|
---|
| 140 | const VssRay &oldRay)
|
---|
| 141 | {
|
---|
| 142 | // the predicted hitpoint expects to hit the same mesh again
|
---|
| 143 | const Vector3 predictedHitX = CalcPredictedHitPoint(x, hitTriangle, oldRay);
|
---|
| 144 | const Vector3 predictedHitY = CalcPredictedHitPoint(y, hitTriangle, oldRay);
|
---|
| 145 |
|
---|
| 146 | CheckDiscontinuity(x, hitTriangle, oldRay);
|
---|
| 147 | CheckDiscontinuity(y, hitTriangle, oldRay);
|
---|
| 148 |
|
---|
| 149 | if (EqualVisibility(x, y))
|
---|
| 150 | {
|
---|
| 151 | return 2;
|
---|
| 152 | }
|
---|
| 153 | else
|
---|
| 154 | {
|
---|
| 155 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
| 156 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin);
|
---|
| 157 | VssRay *newRay = NULL;
|
---|
| 158 | //VssRay *newRay = CastSingleRay(sray);
|
---|
| 159 | HandleRay(*newRay);
|
---|
| 160 |
|
---|
| 161 | const int s1 = SubdivideEdge(hitTriangle, p1, p, x, *newRay, oldRay);
|
---|
| 162 | const int s2 = SubdivideEdge(hitTriangle, p, p2, *newRay, y, oldRay);
|
---|
| 163 |
|
---|
| 164 | return s1 + s2;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
| 170 | {
|
---|
[1486] | 171 | cout << "a";
|
---|
[1500] | 172 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1486] | 173 | Triangle3 hitTriangle;
|
---|
[1489] | 174 |
|
---|
| 175 | // other types not implemented yet
|
---|
| 176 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 177 | {
|
---|
| 178 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[1500] | 181 | VertexContainer enlargedTriangle;
|
---|
| 182 |
|
---|
| 183 | /// create 3 new hit points for each vertex
|
---|
| 184 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 185 |
|
---|
| 186 | /// create rays from sample points and handle them
|
---|
[1492] | 187 | SimpleRayContainer simpleRays;
|
---|
| 188 | simpleRays.reserve(9);
|
---|
[1486] | 189 |
|
---|
[1500] | 190 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
[1486] | 191 |
|
---|
[1500] | 192 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
| 193 | {
|
---|
| 194 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
| 195 | simpleRays.push_back(SimpleRay(*vit, rayDir));
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | // establish visibility
|
---|
[1489] | 199 | VssRayContainer vssRays;
|
---|
[1520] | 200 | CastRays(simpleRays, vssRays, false);
|
---|
[1492] | 201 | // add to ray queue
|
---|
| 202 | EnqueueRays(vssRays);
|
---|
[1501] | 203 | /*
|
---|
[1500] | 204 | // recursivly subdivide each edge
|
---|
| 205 | for (int i = 0; i < 9; ++ i)
|
---|
| 206 | {
|
---|
| 207 | SubdivideEdge(
|
---|
| 208 | hitTriangle,
|
---|
| 209 | enlargedTriangle[i],
|
---|
| 210 | enlargedTriangle[(i + 1) % 9],
|
---|
| 211 | *vssRays[i],
|
---|
| 212 | *vssRays[(i + 1) % 9],
|
---|
| 213 | currentRay);
|
---|
| 214 | }
|
---|
[1501] | 215 | */
|
---|
[1492] | 216 | return (int)vssRays.size();
|
---|
[1473] | 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
[1500] | 220 | static Vector3 GetPassingPoint(const VssRay ¤tRay,
|
---|
| 221 | const Triangle3 &hitTriangle,
|
---|
| 222 | const VssRay &oldRay)
|
---|
[1473] | 223 | {
|
---|
[1500] | 224 | // intersect triangle plane with plane spanned by current samples
|
---|
| 225 | Plane3 plane(currentRay.GetOrigin(), currentRay.GetTermination(), oldRay.GetTermination());
|
---|
| 226 | Plane3 triPlane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 227 |
|
---|
| 228 | SimpleRay intersectLine = GetPlaneIntersection(plane, triPlane);
|
---|
| 229 |
|
---|
| 230 | // Evaluate new hitpoint just outside the triangle
|
---|
| 231 | const float factor = 0.95f;
|
---|
| 232 | float t = triPlane.FindT(intersectLine);
|
---|
| 233 |
|
---|
| 234 | const Vector3 newPoint = intersectLine.mOrigin + t * factor * intersectLine.mDirection;
|
---|
| 235 |
|
---|
| 236 | return newPoint;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
| 241 | const Triangle3 &hitTriangle,
|
---|
| 242 | const VssRay &oldRay)
|
---|
| 243 | {
|
---|
[1486] | 244 | cout << "r" << endl;
|
---|
[1500] | 245 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
| 246 | //-- with the newly found triangle (xold is the previous ray from
|
---|
| 247 | //-- which x was generated). On the intersecting line, we select a point
|
---|
| 248 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
| 249 | //-- just passes by inside the gap
|
---|
| 250 | const Vector3 newPoint = GetPassingPoint(currentRay, hitTriangle, oldRay);
|
---|
| 251 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 252 |
|
---|
| 253 | //-- Construct the mutated ray with xnew,dir = predicted(x)- pnew
|
---|
| 254 | //-- as direction vector
|
---|
| 255 | const Vector3 newDir = predicted - newPoint ;
|
---|
| 256 | // take xnew,p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
| 257 | // difficult to say!!
|
---|
| 258 | const Vector3 newOrigin = newDir * -5000.0f;
|
---|
| 259 |
|
---|
| 260 | return new VssRay(currentRay);
|
---|
[1473] | 261 | }
|
---|
| 262 |
|
---|
[1489] | 263 |
|
---|
| 264 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
| 265 | const int sampleType)
|
---|
| 266 | {
|
---|
[1473] | 267 | const long startTime = GetTime();
|
---|
| 268 |
|
---|
[1489] | 269 | // generate simple rays
|
---|
| 270 | SimpleRayContainer simpleRays;
|
---|
| 271 | GenerateRays(numSamples, sampleType, simpleRays);
|
---|
| 272 |
|
---|
| 273 | // generate vss rays
|
---|
[1473] | 274 | VssRayContainer samples;
|
---|
[1520] | 275 | CastRays(simpleRays, samples, true);
|
---|
[1489] | 276 |
|
---|
| 277 | // add to ray queue
|
---|
[1492] | 278 | EnqueueRays(samples);
|
---|
[1473] | 279 |
|
---|
[1489] | 280 | Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1473] | 281 |
|
---|
[1489] | 282 | return (int)samples.size();
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 |
|
---|
[1500] | 286 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
[1489] | 287 | {
|
---|
[1486] | 288 | // add samples to ray queue
|
---|
[1473] | 289 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
[1500] | 290 |
|
---|
[1473] | 291 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
| 292 | {
|
---|
[1500] | 293 | HandleRay(*(*vit));
|
---|
[1460] | 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[1473] | 297 |
|
---|
[1486] | 298 | int GvsPreprocessor::Pass()
|
---|
[1460] | 299 | {
|
---|
[1473] | 300 | int castSamples = 0;
|
---|
[1489] | 301 | const int mSampleType = 0;
|
---|
[1486] | 302 | while (castSamples < mSamplesPerPass)
|
---|
[1473] | 303 | {
|
---|
| 304 | // Ray queue empty =>
|
---|
| 305 | // cast a number of uniform samples to fill ray Queue
|
---|
[1489] | 306 | CastInitialSamples(mInitialSamples, mSampleType);
|
---|
| 307 |
|
---|
| 308 | const int gvsSamples = ProcessQueue();
|
---|
[1501] | 309 | #if 0
|
---|
[1486] | 310 | castSamples += gvsSamples;
|
---|
[1501] | 311 | #else
|
---|
| 312 | castSamples += mInitialSamples;
|
---|
| 313 | #endif
|
---|
[1489] | 314 | //cout << "\ncast " << castSamples << " of " << mSamplesPerPass << endl;
|
---|
[1473] | 315 | }
|
---|
| 316 |
|
---|
| 317 | return castSamples;
|
---|
[1460] | 318 | }
|
---|
| 319 |
|
---|
[1473] | 320 |
|
---|
[1489] | 321 | int GvsPreprocessor::ProcessQueue()
|
---|
[1460] | 322 | {
|
---|
[1473] | 323 | int castSamples = 0;
|
---|
| 324 |
|
---|
| 325 | while (!mRayQueue.empty())
|
---|
| 326 | {
|
---|
| 327 | // handle next ray
|
---|
[1500] | 328 | VssRay *ray = mRayQueue.top();
|
---|
[1473] | 329 | mRayQueue.pop();
|
---|
| 330 |
|
---|
[1501] | 331 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
[1460] | 332 | }
|
---|
[1473] | 333 |
|
---|
| 334 | return castSamples;
|
---|
[1460] | 335 | }
|
---|
| 336 |
|
---|
| 337 |
|
---|
[1489] | 338 | bool GvsPreprocessor::ComputeVisibility()
|
---|
[1460] | 339 | {
|
---|
[1473] | 340 | Randomize(0);
|
---|
| 341 | const long startTime = GetTime();
|
---|
| 342 |
|
---|
[1486] | 343 | mViewSpaceBox = mKdTree->GetBox();
|
---|
| 344 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
[1473] | 345 |
|
---|
[1486] | 346 | if (!mLoadViewCells)
|
---|
| 347 | { /// construct the view cells from the scratch
|
---|
| 348 | ConstructViewCells(mViewSpaceBox);
|
---|
| 349 | cout << "view cells loaded" << endl;
|
---|
| 350 | }
|
---|
[1460] | 351 |
|
---|
[1486] | 352 | int castSamples = 0;
|
---|
[1460] | 353 |
|
---|
[1486] | 354 | while (castSamples < mTotalSamples)
|
---|
[1473] | 355 | {
|
---|
[1486] | 356 | const int passSamples = Pass();
|
---|
| 357 | castSamples += passSamples;
|
---|
[1489] | 358 |
|
---|
[1500] | 359 | ////////
|
---|
| 360 | //-- stats
|
---|
[1486] | 361 | cout << "+";
|
---|
| 362 | cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
|
---|
| 363 | //mVssRays.PrintStatistics(mStats);
|
---|
[1489] | 364 | mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
|
---|
| 365 | << "#TotalSamples\n" << castSamples << endl;
|
---|
[1460] | 366 |
|
---|
[1473] | 367 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 368 | // ComputeRenderError();
|
---|
[1460] | 369 | }
|
---|
| 370 |
|
---|
[1473] | 371 | return true;
|
---|
[1460] | 372 | }
|
---|
| 373 |
|
---|
| 374 | }
|
---|