[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"
|
---|
[1460] | 8 |
|
---|
[1473] | 9 |
|
---|
[1460] | 10 | namespace GtpVisibilityPreprocessor
|
---|
| 11 | {
|
---|
| 12 |
|
---|
| 13 |
|
---|
[1473] | 14 | GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
|
---|
[1460] | 15 | {
|
---|
[1486] | 16 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 17 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
| 18 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 19 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
| 20 |
|
---|
[1473] | 21 |
|
---|
[1486] | 22 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 23 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 24 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
| 25 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
| 26 |
|
---|
| 27 | mStats.open("gvspreprocessor.log");
|
---|
[1460] | 28 | }
|
---|
| 29 |
|
---|
[1473] | 30 |
|
---|
[1489] | 31 | const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray,
|
---|
| 32 | const VssRay &oldRay) const
|
---|
[1460] | 33 | {
|
---|
[1492] | 34 | //|predicted(x)-xp|-|hit(x)-xp| > ?,
|
---|
[1489] | 35 | //const Plane3 plane = tri->GetPlane();
|
---|
[1486] | 36 | return false;
|
---|
| 37 | }
|
---|
[1473] | 38 |
|
---|
[1486] | 39 |
|
---|
| 40 | int GvsPreprocessor::HandleRay(const GvsRayInfo &gvsRay)
|
---|
| 41 | {
|
---|
| 42 | VssRay *ray = gvsRay.mRay;
|
---|
| 43 |
|
---|
| 44 | if (!mViewCellsManager->ComputeSampleContribution(*ray, true, false))
|
---|
| 45 | return 1;
|
---|
| 46 |
|
---|
| 47 | if (gvsRay.mFoundDiscontinuity)
|
---|
[1473] | 48 | {
|
---|
[1486] | 49 | return ReverseSampling(*ray);
|
---|
[1473] | 50 | }
|
---|
| 51 | else
|
---|
| 52 | {
|
---|
[1486] | 53 | return AdaptiveBorderSampling(*ray);
|
---|
[1473] | 54 | }
|
---|
[1460] | 55 | }
|
---|
| 56 |
|
---|
[1489] | 57 | /** Hepler function for adaptive border sampling. It finds
|
---|
| 58 | new sample points around a triangle in a eps environment
|
---|
| 59 | */
|
---|
[1492] | 60 | static void CreateNewRays(SimpleRayContainer &simpleRays,
|
---|
| 61 | const Triangle3 &hitTriangle,
|
---|
| 62 | const VssRay &ray,
|
---|
| 63 | const int index,
|
---|
| 64 | const float eps)
|
---|
[1460] | 65 | {
|
---|
[1486] | 66 | const int indexU = (index + 1) % 3;
|
---|
| 67 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 68 |
|
---|
| 69 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetDir();
|
---|
| 70 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 71 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
[1492] | 72 |
|
---|
[1486] | 73 | const float len = Magnitude(a);
|
---|
| 74 |
|
---|
[1492] | 75 | const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
|
---|
| 76 | const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
|
---|
| 77 | const Vector3 dir3 = DotProd(dir1, dir2) > 0 ?
|
---|
| 78 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a)); // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 79 |
|
---|
[1486] | 80 | // compute the new three hit points
|
---|
| 81 | // pi, i + 1
|
---|
| 82 | const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1; //pi+ e·|pi-xp|·di, j
|
---|
| 83 | // pi, i - 1
|
---|
[1492] | 84 | const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2; //pi+ e·|pi-xp|·di, j
|
---|
[1486] | 85 | // pi, i
|
---|
| 86 | const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3; //pi+ e·|pi-xp|·di, j
|
---|
[1489] | 87 |
|
---|
[1492] | 88 | /// create simple rays and store them in container
|
---|
| 89 | Vector3 rayDir;
|
---|
| 90 | rayDir = Normalize(pt1 - ray.GetOrigin());
|
---|
| 91 | simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
|
---|
| 92 | rayDir = Normalize(pt2 - ray.GetOrigin());
|
---|
| 93 | simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
|
---|
| 94 | rayDir = Normalize(pt3 - ray.GetOrigin());
|
---|
| 95 | simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
|
---|
[1489] | 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
[1492] | 99 |
|
---|
[1489] | 100 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &prevRay)
|
---|
| 101 | {
|
---|
[1486] | 102 | cout << "a";
|
---|
[1489] | 103 | Intersectable *tObj = prevRay.mTerminationObject;
|
---|
[1486] | 104 | Triangle3 hitTriangle;
|
---|
[1489] | 105 |
|
---|
| 106 | // other types not implemented yet
|
---|
| 107 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 108 | {
|
---|
| 109 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[1492] | 112 | SimpleRayContainer simpleRays;
|
---|
| 113 | simpleRays.reserve(9);
|
---|
[1486] | 114 |
|
---|
[1492] | 115 | CreateNewRays(simpleRays, hitTriangle, prevRay, 0, mEps);
|
---|
| 116 | CreateNewRays(simpleRays, hitTriangle, prevRay, 1, mEps);
|
---|
| 117 | CreateNewRays(simpleRays, hitTriangle, prevRay, 2, mEps);
|
---|
[1486] | 118 |
|
---|
[1489] | 119 | VssRayContainer vssRays;
|
---|
[1486] | 120 |
|
---|
[1492] | 121 | CastRays(simpleRays, vssRays);
|
---|
| 122 | // add to ray queue
|
---|
| 123 | EnqueueRays(vssRays);
|
---|
[1486] | 124 |
|
---|
[1492] | 125 | return (int)vssRays.size();
|
---|
[1473] | 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
[1492] | 129 | int GvsPreprocessor::ReverseSampling(const VssRay &oldRay)
|
---|
[1473] | 130 | {
|
---|
[1486] | 131 | cout << "r" << endl;
|
---|
[1473] | 132 | // TODO
|
---|
[1486] | 133 | return 1;
|
---|
[1473] | 134 | }
|
---|
| 135 |
|
---|
[1489] | 136 |
|
---|
| 137 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
| 138 | const int sampleType)
|
---|
| 139 | {
|
---|
[1473] | 140 | const long startTime = GetTime();
|
---|
| 141 |
|
---|
[1489] | 142 | // generate simple rays
|
---|
| 143 | SimpleRayContainer simpleRays;
|
---|
| 144 | GenerateRays(numSamples, sampleType, simpleRays);
|
---|
| 145 |
|
---|
| 146 | // generate vss rays
|
---|
[1473] | 147 | VssRayContainer samples;
|
---|
[1486] | 148 | CastRays(simpleRays, samples);
|
---|
[1489] | 149 |
|
---|
| 150 | // add to ray queue
|
---|
[1492] | 151 | EnqueueRays(samples);
|
---|
[1473] | 152 |
|
---|
[1489] | 153 | Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1473] | 154 |
|
---|
[1489] | 155 | return (int)samples.size();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
[1492] | 159 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRay *oldRay)
|
---|
[1489] | 160 | {
|
---|
[1486] | 161 | // add samples to ray queue
|
---|
[1473] | 162 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
| 163 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
| 164 | {
|
---|
[1489] | 165 | /// if there is no old ray, no discontinuity
|
---|
| 166 | const bool gap = oldRay ? DiscontinuityFound(*(*vit), *oldRay) : false;
|
---|
| 167 | mRayQueue.push(GvsRayInfo(*vit, gap));
|
---|
[1460] | 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[1473] | 171 |
|
---|
[1486] | 172 | int GvsPreprocessor::Pass()
|
---|
[1460] | 173 | {
|
---|
[1473] | 174 | int castSamples = 0;
|
---|
[1489] | 175 | const int mSampleType = 0;
|
---|
[1486] | 176 | while (castSamples < mSamplesPerPass)
|
---|
[1473] | 177 | {
|
---|
| 178 | // Ray queue empty =>
|
---|
| 179 | // cast a number of uniform samples to fill ray Queue
|
---|
[1489] | 180 | CastInitialSamples(mInitialSamples, mSampleType);
|
---|
| 181 |
|
---|
| 182 | const int gvsSamples = ProcessQueue();
|
---|
[1486] | 183 | castSamples += gvsSamples;
|
---|
[1489] | 184 | //cout << "\ncast " << castSamples << " of " << mSamplesPerPass << endl;
|
---|
[1473] | 185 | }
|
---|
| 186 |
|
---|
| 187 | return castSamples;
|
---|
[1460] | 188 | }
|
---|
| 189 |
|
---|
[1473] | 190 |
|
---|
[1489] | 191 | int GvsPreprocessor::ProcessQueue()
|
---|
[1460] | 192 | {
|
---|
[1473] | 193 | int castSamples = 0;
|
---|
| 194 |
|
---|
| 195 | while (!mRayQueue.empty())
|
---|
| 196 | {
|
---|
| 197 | // handle next ray
|
---|
[1489] | 198 | GvsRayInfo rayInfo = mRayQueue.top();
|
---|
[1473] | 199 | mRayQueue.pop();
|
---|
| 200 |
|
---|
[1489] | 201 | castSamples += HandleRay(rayInfo);
|
---|
[1460] | 202 | }
|
---|
[1473] | 203 |
|
---|
| 204 | return castSamples;
|
---|
[1460] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[1489] | 208 | bool GvsPreprocessor::ComputeVisibility()
|
---|
[1460] | 209 | {
|
---|
[1473] | 210 | Randomize(0);
|
---|
| 211 | const long startTime = GetTime();
|
---|
| 212 |
|
---|
[1486] | 213 | mViewSpaceBox = mKdTree->GetBox();
|
---|
| 214 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
[1473] | 215 |
|
---|
[1486] | 216 | if (!mLoadViewCells)
|
---|
| 217 | { /// construct the view cells from the scratch
|
---|
| 218 | ConstructViewCells(mViewSpaceBox);
|
---|
| 219 | cout << "view cells loaded" << endl;
|
---|
| 220 | }
|
---|
[1460] | 221 |
|
---|
[1486] | 222 | int castSamples = 0;
|
---|
[1460] | 223 |
|
---|
[1486] | 224 | while (castSamples < mTotalSamples)
|
---|
[1473] | 225 | {
|
---|
[1486] | 226 | const int passSamples = Pass();
|
---|
| 227 | castSamples += passSamples;
|
---|
[1489] | 228 |
|
---|
| 229 | /////////////
|
---|
| 230 | // -- stats
|
---|
[1486] | 231 | cout << "+";
|
---|
| 232 | cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
|
---|
| 233 | //mVssRays.PrintStatistics(mStats);
|
---|
[1489] | 234 | mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
|
---|
| 235 | << "#TotalSamples\n" << castSamples << endl;
|
---|
[1460] | 236 |
|
---|
[1473] | 237 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 238 | // ComputeRenderError();
|
---|
[1460] | 239 | }
|
---|
| 240 |
|
---|
[1473] | 241 | return true;
|
---|
[1460] | 242 | }
|
---|
| 243 |
|
---|
| 244 | }
|
---|