[1460] | 1 | #include "Environment.h"
|
---|
| 2 | #include "GvsPreprocessor.h"
|
---|
| 3 | #include "GlRenderer.h"
|
---|
[1473] | 4 | #include "VssRay.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
[1486] | 6 | #include "Triangle3.h"
|
---|
[1489] | 7 | #include "IntersectableWrapper.h"
|
---|
[1500] | 8 | #include "Plane3.h"
|
---|
[1521] | 9 | #include "RayCaster.h"
|
---|
[1522] | 10 | #include "Exporter.h"
|
---|
[1545] | 11 | #include "SamplingStrategy.h"
|
---|
[1460] | 12 |
|
---|
[1473] | 13 |
|
---|
[1460] | 14 | namespace GtpVisibilityPreprocessor
|
---|
| 15 | {
|
---|
| 16 |
|
---|
[1935] | 17 | #define GVS_DEBUG 1
|
---|
[1934] | 18 |
|
---|
[1522] | 19 | struct VizStruct
|
---|
| 20 | {
|
---|
| 21 | Polygon3 *enlargedTriangle;
|
---|
| 22 | Triangle3 originalTriangle;
|
---|
| 23 | VssRay *ray;
|
---|
| 24 | };
|
---|
[1460] | 25 |
|
---|
[1522] | 26 | static vector<VizStruct> vizContainer;
|
---|
| 27 |
|
---|
[1533] | 28 | GvsPreprocessor::GvsPreprocessor():
|
---|
[1545] | 29 | Preprocessor(),
|
---|
| 30 | //mSamplingType(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION),
|
---|
[1934] | 31 | mSamplingType(SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION)
|
---|
[1460] | 32 | {
|
---|
[1486] | 33 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 34 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
| 35 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 36 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
[1500] | 37 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
[1473] | 38 |
|
---|
[1934] | 39 | char gvsStatsLog[100];
|
---|
| 40 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
| 41 | mGvsStatsStream.open(gvsStatsLog);
|
---|
| 42 |
|
---|
[1486] | 43 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 44 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 45 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
| 46 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
[1501] | 47 | Debug << "threshold: " << mThreshold << endl;
|
---|
[1934] | 48 | Debug << "epsilon: " << mEps << endl;
|
---|
| 49 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
[1486] | 50 |
|
---|
[1934] | 51 | if (0)
|
---|
| 52 | mOnlyRandomSampling = true;
|
---|
| 53 | else
|
---|
| 54 | mOnlyRandomSampling = false;
|
---|
| 55 |
|
---|
| 56 | //mGvsStatsStream.open("gvspreprocessor.log");
|
---|
| 57 | mGvsStats.Reset();
|
---|
[1460] | 58 | }
|
---|
| 59 |
|
---|
[1473] | 60 |
|
---|
[1500] | 61 | bool GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
| 62 | const Triangle3 &hitTriangle,
|
---|
| 63 | const VssRay &oldRay)
|
---|
[1460] | 64 | {
|
---|
[1932] | 65 | // the predicted hitpoint: we expect to hit the same mesh again
|
---|
| 66 | const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 67 |
|
---|
| 68 | const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
| 69 | const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
| 70 |
|
---|
| 71 | // distance large => this is likely to be a discontinuity
|
---|
[1933] | 72 | #if 1
|
---|
[1932] | 73 | if ((predictedLen - len) > mThreshold)
|
---|
[1933] | 74 | #else // rather use relative distance
|
---|
| 75 | if ((predictedLen / len) > mThreshold)
|
---|
| 76 | #endif
|
---|
[1500] | 77 | {
|
---|
[1934] | 78 | //cout << "r";
|
---|
[1933] | 79 | // apply reverse sampling to find the gap
|
---|
[1500] | 80 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
[1933] | 81 |
|
---|
| 82 | if (!newRay)
|
---|
| 83 | return false;
|
---|
| 84 |
|
---|
[1572] | 85 | // set flag for visualization
|
---|
[1571] | 86 | newRay->mFlags |= VssRay::ReverseSample;
|
---|
[1580] | 87 |
|
---|
[1932] | 88 | // if ray is not further processed => can delete ray
|
---|
[1521] | 89 | if (!HandleRay(newRay))
|
---|
[1933] | 90 | {
|
---|
[1521] | 91 | delete newRay;
|
---|
[1933] | 92 | }
|
---|
[1934] | 93 | else if (GVS_DEBUG && (mVssRays.size() < 9))
|
---|
[1933] | 94 | {
|
---|
| 95 | mVssRays.push_back(new VssRay(oldRay));
|
---|
| 96 | mVssRays.push_back(new VssRay(currentRay));
|
---|
| 97 | mVssRays.push_back(new VssRay(*newRay));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[1500] | 100 | return true;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1486] | 103 | return false;
|
---|
| 104 | }
|
---|
[1473] | 105 |
|
---|
[1486] | 106 |
|
---|
[1521] | 107 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
[1486] | 108 | {
|
---|
[1932] | 109 | // compute the contribution to the view cells
|
---|
[1934] | 110 | const bool storeRaysForViz = GVS_DEBUG;
|
---|
[1933] | 111 |
|
---|
[1932] | 112 | mViewCellsManager->ComputeSampleContribution(*vssRay,
|
---|
| 113 | true,
|
---|
| 114 | storeRaysForViz);
|
---|
| 115 |
|
---|
[1528] | 116 | // some pvs contribution for this ray?
|
---|
| 117 | if (vssRay->mPvsContribution > 0)
|
---|
| 118 | {
|
---|
[1580] | 119 | // add new ray to ray queue
|
---|
[1521] | 120 | mRayQueue.push(vssRay);
|
---|
[1570] | 121 |
|
---|
| 122 | if (storeRaysForViz)
|
---|
| 123 | {
|
---|
[1932] | 124 | VssRay *nray = new VssRay(*vssRay);
|
---|
| 125 | nray->mFlags = vssRay->mFlags;
|
---|
| 126 |
|
---|
| 127 | // store ray in contributing view cell
|
---|
[1570] | 128 | ViewCellContainer::const_iterator vit, vit_end = vssRay->mViewCells.end();
|
---|
| 129 | for (vit = vssRay->mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
[1932] | 130 | {
|
---|
[1696] | 131 | (*vit)->GetOrCreateRays()->push_back(nray);
|
---|
[1570] | 132 | }
|
---|
| 133 | }
|
---|
[1932] | 134 |
|
---|
[1934] | 135 | ++ mGvsStats.mPassContribution;
|
---|
[1533] | 136 |
|
---|
[1500] | 137 | return true;
|
---|
[1473] | 138 | }
|
---|
[1500] | 139 |
|
---|
| 140 | return false;
|
---|
[1460] | 141 | }
|
---|
| 142 |
|
---|
[1500] | 143 |
|
---|
| 144 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
[1489] | 145 | */
|
---|
[1932] | 146 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
| 147 | const Triangle3 &hitTriangle,
|
---|
| 148 | const VssRay &ray,
|
---|
| 149 | const int index) const
|
---|
[1460] | 150 | {
|
---|
[1486] | 151 | const int indexU = (index + 1) % 3;
|
---|
| 152 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 153 |
|
---|
[1524] | 154 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
[1486] | 155 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 156 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
[1492] | 157 |
|
---|
[1533] | 158 | const float len = Magnitude(a);
|
---|
[1932] | 159 |
|
---|
[1523] | 160 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
| 161 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
[1524] | 162 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 163 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
[1492] | 164 |
|
---|
[1486] | 165 | // compute the new three hit points
|
---|
[1500] | 166 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 167 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
[1500] | 168 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 169 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
[1500] | 170 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 171 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
[1489] | 172 |
|
---|
[1524] | 173 | vertices.push_back(pt2);
|
---|
| 174 | vertices.push_back(pt3);
|
---|
[1500] | 175 | vertices.push_back(pt1);
|
---|
[1489] | 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[1500] | 179 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
| 180 | const Triangle3 &hitTriangle,
|
---|
[1932] | 181 | const VssRay &ray) const
|
---|
[1500] | 182 | {
|
---|
[1932] | 183 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
| 184 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
| 185 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
[1500] | 186 | }
|
---|
[1492] | 187 |
|
---|
[1500] | 188 |
|
---|
[1932] | 189 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
| 190 | const Triangle3 &hitTriangle,
|
---|
| 191 | const VssRay &oldRay) const
|
---|
[1489] | 192 | {
|
---|
[1932] | 193 | // find the intersection of the plane induced by the
|
---|
| 194 | // hit triangle with the new ray
|
---|
[1500] | 195 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 196 |
|
---|
| 197 | const Vector3 hitPt =
|
---|
| 198 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
| 199 |
|
---|
| 200 | return hitPt;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
| 205 | {
|
---|
| 206 | return a.mTerminationObject == b.mTerminationObject;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
| 211 | const Vector3 &p1,
|
---|
| 212 | const Vector3 &p2,
|
---|
[1932] | 213 | const VssRay &ray1,
|
---|
| 214 | const VssRay &ray2,
|
---|
[1500] | 215 | const VssRay &oldRay)
|
---|
| 216 | {
|
---|
[1932] | 217 | CheckDiscontinuity(ray1, hitTriangle, oldRay);
|
---|
| 218 | CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
[1500] | 219 |
|
---|
[1932] | 220 | if (EqualVisibility(ray1, ray2) || (Magnitude(p1 - p2) <= mEps))
|
---|
[1500] | 221 | {
|
---|
[1533] | 222 | return 0;
|
---|
[1500] | 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
[1932] | 226 | // the new subdivision point
|
---|
[1500] | 227 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
[1932] | 228 |
|
---|
| 229 | //cout << "tobj " << ray1.mTerminationObject << " " << ray2.mTerminationObject << " " << p1 << " " << p2 << endl;
|
---|
| 230 | //cout << "term " << ray1.mTermination << " " << ray2.mTermination << endl;
|
---|
| 231 |
|
---|
| 232 | // cast ray into the new point
|
---|
[1883] | 233 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
[1521] | 234 |
|
---|
[1932] | 235 | VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox());
|
---|
| 236 |
|
---|
[1551] | 237 | if (!newRay) return 0;
|
---|
[1580] | 238 |
|
---|
| 239 | newRay->mFlags |= VssRay::BorderSample;
|
---|
| 240 |
|
---|
| 241 | // add new ray to queue
|
---|
[1522] | 242 | const bool enqueued = HandleRay(newRay);
|
---|
[1521] | 243 |
|
---|
[1566] | 244 | // subdivide further
|
---|
[1932] | 245 | const int samples1 = SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
| 246 | const int samples2 = SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
| 247 |
|
---|
| 248 | // this ray will not be further processed
|
---|
[1522] | 249 | if (!enqueued)
|
---|
[1521] | 250 | delete newRay;
|
---|
[1533] | 251 |
|
---|
[1932] | 252 | return samples1 + samples2 + 1;
|
---|
[1500] | 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
| 258 | {
|
---|
| 259 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1486] | 260 | Triangle3 hitTriangle;
|
---|
[1489] | 261 |
|
---|
| 262 | // other types not implemented yet
|
---|
| 263 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 264 | {
|
---|
| 265 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 266 | }
|
---|
[1522] | 267 | else
|
---|
| 268 | {
|
---|
| 269 | cout << "not yet implemented" << endl;
|
---|
| 270 | }
|
---|
[1489] | 271 |
|
---|
[1500] | 272 | VertexContainer enlargedTriangle;
|
---|
| 273 |
|
---|
| 274 | /// create 3 new hit points for each vertex
|
---|
| 275 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 276 |
|
---|
| 277 | /// create rays from sample points and handle them
|
---|
[1492] | 278 | SimpleRayContainer simpleRays;
|
---|
| 279 | simpleRays.reserve(9);
|
---|
[1486] | 280 |
|
---|
[1932] | 281 | //cout << "currentRay: " << currentRay.mOrigin << " dir: " << currentRay.GetDir() << endl;
|
---|
| 282 |
|
---|
[1500] | 283 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
[1486] | 284 |
|
---|
[1500] | 285 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
| 286 | {
|
---|
| 287 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
[1883] | 288 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1528] | 289 | simpleRays.AddRay(sr);
|
---|
[1932] | 290 |
|
---|
| 291 | //cout << "new: " << sr.mOrigin << " dist: " << sr.mDirection << endl;
|
---|
[1500] | 292 | }
|
---|
[1522] | 293 |
|
---|
[1566] | 294 | if (0)
|
---|
| 295 | {
|
---|
[1932] | 296 | // visualize enlarged triangles
|
---|
[1566] | 297 | VizStruct dummy;
|
---|
| 298 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
| 299 | dummy.originalTriangle = hitTriangle;
|
---|
| 300 | vizContainer.push_back(dummy);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[1524] | 303 | // cast rays to triangle vertices and determine visibility
|
---|
[1489] | 304 | VssRayContainer vssRays;
|
---|
[1533] | 305 |
|
---|
[1932] | 306 | // don't cast double rays as we need only the forward rays
|
---|
| 307 | const bool castDoubleRays = false;
|
---|
| 308 | // cannot prune invalid rays because we have to
|
---|
| 309 | // compare adjacent rays.
|
---|
| 310 | const bool pruneInvalidRays = false;
|
---|
| 311 |
|
---|
| 312 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
| 313 |
|
---|
[1580] | 314 | // set flags
|
---|
| 315 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
| 316 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
[1595] | 317 | {
|
---|
[1580] | 318 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
| 319 | }
|
---|
[1595] | 320 |
|
---|
[1932] | 321 | // handle rays
|
---|
[1492] | 322 | EnqueueRays(vssRays);
|
---|
[1528] | 323 |
|
---|
[1524] | 324 | const int n = (int)enlargedTriangle.size();
|
---|
[1533] | 325 | int castRays = (int)vssRays.size();
|
---|
[1571] | 326 |
|
---|
[1500] | 327 | // recursivly subdivide each edge
|
---|
[1932] | 328 | for (int i = 0; i < n; ++ i)
|
---|
[1500] | 329 | {
|
---|
[1932] | 330 | castRays += SubdivideEdge(hitTriangle,
|
---|
| 331 | enlargedTriangle[i],
|
---|
| 332 | enlargedTriangle[(i + 1) % n],
|
---|
| 333 | *vssRays[i],
|
---|
| 334 | *vssRays[(i + 1) % n],
|
---|
| 335 | currentRay);
|
---|
[1500] | 336 | }
|
---|
[1533] | 337 |
|
---|
[1934] | 338 | mGvsStats.mBorderSamples += castRays;
|
---|
[1932] | 339 |
|
---|
[1533] | 340 | return castRays;
|
---|
[1473] | 341 | }
|
---|
| 342 |
|
---|
| 343 |
|
---|
[1933] | 344 | /*Vector3 GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
[1932] | 345 | const Triangle3 &hitTriangle,
|
---|
| 346 | const VssRay &oldRay) const
|
---|
[1473] | 347 | {
|
---|
[1932] | 348 | //-- intersect triangle plane with plane spanned by current samples
|
---|
[1500] | 349 | Plane3 plane(currentRay.GetOrigin(), currentRay.GetTermination(), oldRay.GetTermination());
|
---|
| 350 | Plane3 triPlane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 351 |
|
---|
| 352 | SimpleRay intersectLine = GetPlaneIntersection(plane, triPlane);
|
---|
| 353 |
|
---|
| 354 | // Evaluate new hitpoint just outside the triangle
|
---|
| 355 | const float factor = 0.95f;
|
---|
[1932] | 356 | const float t = triPlane.FindT(intersectLine);
|
---|
[1500] | 357 | const Vector3 newPoint = intersectLine.mOrigin + t * factor * intersectLine.mDirection;
|
---|
| 358 |
|
---|
| 359 | return newPoint;
|
---|
[1933] | 360 | }*/
|
---|
| 361 |
|
---|
| 362 |
|
---|
| 363 | Vector3 GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
| 364 | const Triangle3 &occluder,
|
---|
| 365 | const VssRay &oldRay) const
|
---|
| 366 | {
|
---|
| 367 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
| 368 | //-- with the newly found occluder (xold is the previous ray from
|
---|
| 369 | //-- which x was generated). On the intersecting line, we select a point
|
---|
| 370 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
| 371 | //-- just passes through the gap
|
---|
| 372 |
|
---|
| 373 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
| 374 | currentRay.GetTermination(),
|
---|
| 375 | oldRay.GetTermination());
|
---|
| 376 |
|
---|
| 377 | Vector3 pt1, pt2;
|
---|
| 378 |
|
---|
| 379 | const bool intersects = occluder.GetPlaneIntersection(plane, pt1, pt2);
|
---|
| 380 |
|
---|
| 381 | if (!intersects)
|
---|
| 382 | cerr << "big error!! no intersection" << endl;
|
---|
| 383 |
|
---|
| 384 | // get the intersection point on the old ray
|
---|
| 385 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
| 386 |
|
---|
| 387 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
| 388 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
| 389 |
|
---|
| 390 | // Evaluate new hitpoint just outside the triangle
|
---|
| 391 | Vector3 newPoint;
|
---|
| 392 |
|
---|
| 393 | const float eps = mEps;
|
---|
| 394 | // the point is chosen to be on the side closer to the original ray
|
---|
| 395 | if (Distance(pt1, pt3) < Distance(pt2, pt3))
|
---|
| 396 | {
|
---|
| 397 | newPoint = pt1 + eps * (pt1 - pt2);
|
---|
| 398 | }
|
---|
| 399 | else
|
---|
| 400 | {
|
---|
| 401 | newPoint = pt2 + eps * (pt2 - pt1);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[1934] | 404 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
[1933] | 405 | return newPoint;
|
---|
[1500] | 406 | }
|
---|
| 407 |
|
---|
| 408 |
|
---|
| 409 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
| 410 | const Triangle3 &hitTriangle,
|
---|
| 411 | const VssRay &oldRay)
|
---|
| 412 | {
|
---|
[1933] | 413 | // get triangle occluding the path to the hit mesh
|
---|
| 414 | Triangle3 occluder;
|
---|
| 415 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1934] | 416 |
|
---|
[1933] | 417 | // q: why can this happen?
|
---|
| 418 | if (!tObj)
|
---|
| 419 | return NULL;
|
---|
| 420 |
|
---|
| 421 | // other types not implemented yet
|
---|
| 422 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
| 423 | occluder = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
| 424 | else
|
---|
| 425 | cout << "not yet implemented" << endl;
|
---|
| 426 |
|
---|
| 427 | // get a point which is passing just outside of the occluder
|
---|
| 428 | const Vector3 newPoint = GetPassingPoint(currentRay, occluder, oldRay);
|
---|
[1500] | 429 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 430 |
|
---|
[1932] | 431 | //-- Construct the mutated ray with xnew,
|
---|
| 432 | //-- dir = predicted(x)- pnew as direction vector
|
---|
[1933] | 433 | const Vector3 newDir = predicted - newPoint;
|
---|
[1932] | 434 |
|
---|
[1933] | 435 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
[1500] | 436 | // difficult to say!!
|
---|
[1935] | 437 | const float offset = 0.5f;
|
---|
| 438 | const Vector3 newOrigin = newPoint - newDir * offset;
|
---|
[1500] | 439 |
|
---|
[1933] | 440 | const SimpleRay simpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1571] | 441 |
|
---|
[1933] | 442 | VssRay *reverseRay =
|
---|
| 443 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox());
|
---|
| 444 |
|
---|
[1934] | 445 | ++ mGvsStats.mReverseSamples;
|
---|
[1933] | 446 |
|
---|
| 447 | return reverseRay;
|
---|
[1473] | 448 | }
|
---|
| 449 |
|
---|
[1489] | 450 |
|
---|
| 451 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
| 452 | const int sampleType)
|
---|
| 453 | {
|
---|
[1473] | 454 | const long startTime = GetTime();
|
---|
[1595] | 455 |
|
---|
[1489] | 456 | // generate simple rays
|
---|
| 457 | SimpleRayContainer simpleRays;
|
---|
| 458 | GenerateRays(numSamples, sampleType, simpleRays);
|
---|
[1528] | 459 |
|
---|
[1489] | 460 | // generate vss rays
|
---|
[1473] | 461 | VssRayContainer samples;
|
---|
[1520] | 462 | CastRays(simpleRays, samples, true);
|
---|
[1489] | 463 | // add to ray queue
|
---|
[1492] | 464 | EnqueueRays(samples);
|
---|
[1533] | 465 |
|
---|
[1545] | 466 | //Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1489] | 467 | return (int)samples.size();
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 |
|
---|
[1500] | 471 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
[1489] | 472 | {
|
---|
[1486] | 473 | // add samples to ray queue
|
---|
[1473] | 474 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
[1576] | 475 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
[1473] | 476 | {
|
---|
[1521] | 477 | HandleRay(*vit);
|
---|
[1460] | 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[1473] | 481 |
|
---|
[1486] | 482 | int GvsPreprocessor::Pass()
|
---|
[1460] | 483 | {
|
---|
[1528] | 484 | // reset samples
|
---|
[1533] | 485 | int castSamples = 0;
|
---|
[1934] | 486 | mGvsStats.mPassContribution = 0;
|
---|
[1545] | 487 |
|
---|
[1533] | 488 | while (castSamples < mSamplesPerPass)
|
---|
[1528] | 489 | {
|
---|
[1473] | 490 | // Ray queue empty =>
|
---|
[1545] | 491 | // cast a number of uniform samples to fill ray queue
|
---|
| 492 | castSamples += CastInitialSamples(mInitialSamples, mSamplingType);
|
---|
[1934] | 493 |
|
---|
| 494 | if (!mOnlyRandomSampling)
|
---|
| 495 | castSamples += ProcessQueue();
|
---|
[1473] | 496 | }
|
---|
| 497 |
|
---|
[1934] | 498 | mGvsStats.mTotalContribution += mGvsStats.mPassContribution;
|
---|
[1533] | 499 | return castSamples;
|
---|
[1460] | 500 | }
|
---|
| 501 |
|
---|
[1473] | 502 |
|
---|
[1489] | 503 | int GvsPreprocessor::ProcessQueue()
|
---|
[1460] | 504 | {
|
---|
[1473] | 505 | int castSamples = 0;
|
---|
[1934] | 506 | ++ mGvsStats.mGvsPass;
|
---|
[1473] | 507 |
|
---|
| 508 | while (!mRayQueue.empty())
|
---|
| 509 | {
|
---|
| 510 | // handle next ray
|
---|
[1500] | 511 | VssRay *ray = mRayQueue.top();
|
---|
[1473] | 512 | mRayQueue.pop();
|
---|
[1545] | 513 |
|
---|
[1501] | 514 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
[1521] | 515 | delete ray;
|
---|
[1460] | 516 | }
|
---|
[1528] | 517 |
|
---|
[1473] | 518 | return castSamples;
|
---|
[1460] | 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
[1489] | 522 | bool GvsPreprocessor::ComputeVisibility()
|
---|
[1460] | 523 | {
|
---|
[1533] | 524 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
[1545] | 525 | const long startTime = GetTime();
|
---|
[1533] | 526 |
|
---|
[1473] | 527 | Randomize(0);
|
---|
[1545] | 528 |
|
---|
[1934] | 529 | mGvsStats.Reset();
|
---|
| 530 | mGvsStats.Start();
|
---|
[1545] | 531 |
|
---|
[1486] | 532 | if (!mLoadViewCells)
|
---|
[1563] | 533 | {
|
---|
| 534 | /// construct the view cells from the scratch
|
---|
| 535 | ConstructViewCells();
|
---|
[1580] | 536 | // reset pvs already gathered during view cells construction
|
---|
| 537 | mViewCellsManager->ResetPvs();
|
---|
[1563] | 538 | cout << "finished view cell construction" << endl;
|
---|
[1486] | 539 | }
|
---|
[1571] | 540 | else if (0)
|
---|
[1563] | 541 | {
|
---|
[1551] | 542 | //-- test successful view cells loading by exporting them again
|
---|
| 543 | VssRayContainer dummies;
|
---|
| 544 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
[1563] | 545 | mViewCellsManager->ExportViewCells("test.xml.gz", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
[1551] | 546 | }
|
---|
[1460] | 547 |
|
---|
[1934] | 548 | mGvsStats.Stop();
|
---|
| 549 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 550 |
|
---|
| 551 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
[1473] | 552 | {
|
---|
[1934] | 553 | ++ mPass;
|
---|
| 554 |
|
---|
| 555 | mGvsStats.mTotalSamples += Pass();
|
---|
[1528] | 556 |
|
---|
[1500] | 557 | ////////
|
---|
| 558 | //-- stats
|
---|
[1932] | 559 |
|
---|
[1934] | 560 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
|
---|
| 561 | mGvsStats.mPass = mPass;
|
---|
| 562 | mGvsStats.Stop();
|
---|
| 563 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 564 | //mViewCellsManager->PrintPvsStatistics(mGvsStats);
|
---|
[1460] | 565 |
|
---|
[1934] | 566 | if (GVS_DEBUG)
|
---|
| 567 | {
|
---|
| 568 | char str[64]; sprintf(str, "tmp/pass%04d-", mPass);
|
---|
[1570] | 569 |
|
---|
[1934] | 570 | // visualization
|
---|
| 571 | if (mGvsStats.mPassContribution > 0)
|
---|
| 572 | {
|
---|
| 573 | const bool exportRays = true;
|
---|
| 574 | const bool exportPvs = true;
|
---|
[1571] | 575 |
|
---|
[1934] | 576 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
| 577 | 10,
|
---|
| 578 | false,
|
---|
| 579 | exportPvs,
|
---|
| 580 | exportRays,
|
---|
| 581 | 1000,
|
---|
| 582 | str);
|
---|
| 583 | }
|
---|
[1571] | 584 |
|
---|
[1934] | 585 | // remove pass samples
|
---|
| 586 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
| 587 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
| 588 | {
|
---|
| 589 | (*vit)->DelRayRefs();
|
---|
| 590 | }
|
---|
[1570] | 591 | }
|
---|
| 592 |
|
---|
[1473] | 593 | // ComputeRenderError();
|
---|
[1460] | 594 | }
|
---|
| 595 |
|
---|
[1934] | 596 | cout << "cast " << 2 * mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl;
|
---|
[1545] | 597 |
|
---|
[1934] | 598 | if (GVS_DEBUG)
|
---|
| 599 | {
|
---|
| 600 | Visualize();
|
---|
| 601 | CLEAR_CONTAINER(mVssRays);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
[1473] | 604 | return true;
|
---|
[1460] | 605 | }
|
---|
| 606 |
|
---|
[1522] | 607 |
|
---|
| 608 | void GvsPreprocessor::Visualize()
|
---|
| 609 | {
|
---|
| 610 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
| 611 |
|
---|
| 612 | if (!exporter)
|
---|
| 613 | return;
|
---|
| 614 |
|
---|
| 615 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
| 616 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
| 617 | {
|
---|
[1524] | 618 | exporter->SetWireframe();
|
---|
[1522] | 619 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
| 620 | //Material m;
|
---|
[1524] | 621 | exporter->SetFilled();
|
---|
| 622 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
| 623 | exporter->ExportPolygon(&poly);
|
---|
[1522] | 624 | }
|
---|
| 625 |
|
---|
[1933] | 626 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
| 627 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 628 | {
|
---|
| 629 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
| 630 | exporter->ExportIntersectable(obj);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
| 634 |
|
---|
| 635 | // prepare some rays for output
|
---|
| 636 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 637 | {
|
---|
| 638 | const float p = RandomValue(0.0f, (float)mVssRays.size());
|
---|
| 639 | if (1)//(p < raysOut)
|
---|
| 640 | {
|
---|
| 641 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
| 642 | {
|
---|
| 643 | vcRays.push_back(*rit);
|
---|
| 644 | }
|
---|
| 645 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
| 646 | {
|
---|
| 647 | vcRays2.push_back(*rit);
|
---|
| 648 | }
|
---|
| 649 | else
|
---|
| 650 | {
|
---|
| 651 | vcRays3.push_back(*rit);
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
| 657 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
| 658 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
| 659 |
|
---|
| 660 | //exporter->ExportRays(mVssRays);
|
---|
[1522] | 661 | delete exporter;
|
---|
[1460] | 662 | }
|
---|
[1522] | 663 |
|
---|
[1934] | 664 |
|
---|
| 665 | void GvsStatistics::Print(ostream &app) const
|
---|
| 666 | {
|
---|
| 667 | app << "#Pass\n" << mPass << endl;
|
---|
| 668 | app << "#Time\n" << Time() << endl;
|
---|
| 669 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
| 670 | app << "#ScDiff\n" << mPassContribution << endl;
|
---|
| 671 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
| 672 | app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
| 673 | app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
| 674 | app << "#GvsRuns\n" << mGvsPass << endl;
|
---|
[1883] | 675 | }
|
---|
[1934] | 676 |
|
---|
| 677 |
|
---|
| 678 | }
|
---|