[1460] | 1 | #include "Environment.h"
|
---|
| 2 | #include "GvsPreprocessor.h"
|
---|
| 3 | #include "GlRenderer.h"
|
---|
[1473] | 4 | #include "VssRay.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
[1486] | 6 | #include "Triangle3.h"
|
---|
[1489] | 7 | #include "IntersectableWrapper.h"
|
---|
[1500] | 8 | #include "Plane3.h"
|
---|
[1521] | 9 | #include "RayCaster.h"
|
---|
[1522] | 10 | #include "Exporter.h"
|
---|
[1545] | 11 | #include "SamplingStrategy.h"
|
---|
[1990] | 12 | #include "BvHierarchy.h"
|
---|
[2116] | 13 | #include "Polygon3.h"
|
---|
[2643] | 14 | #include "IntersectableWrapper.h"
|
---|
[1460] | 15 |
|
---|
[1473] | 16 |
|
---|
[2643] | 17 |
|
---|
[1460] | 18 | namespace GtpVisibilityPreprocessor
|
---|
| 19 | {
|
---|
| 20 |
|
---|
[2633] | 21 | #define GVS_DEBUG 0
|
---|
[2669] | 22 | #define NOT_ACCOUNTED_OBJECT 0
|
---|
| 23 | #define ACCOUNTED_OBJECT 2
|
---|
| 24 | #define SHOW_QT_VISUALIZATION 0
|
---|
[1934] | 25 |
|
---|
[2669] | 26 | static const float MIN_DIST = 0.001f;
|
---|
| 27 |
|
---|
| 28 | static ObjectContainer myobjects;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /** Visualization structure for the GVS algorithm.
|
---|
| 32 | */
|
---|
[1522] | 33 | struct VizStruct
|
---|
| 34 | {
|
---|
| 35 | Polygon3 *enlargedTriangle;
|
---|
| 36 | Triangle3 originalTriangle;
|
---|
| 37 | VssRay *ray;
|
---|
| 38 | };
|
---|
[1460] | 39 |
|
---|
[2669] | 40 | static vector<VizStruct> vizContainer;
|
---|
[2048] | 41 |
|
---|
[1990] | 42 |
|
---|
[1522] | 43 |
|
---|
[2648] | 44 |
|
---|
[1533] | 45 | GvsPreprocessor::GvsPreprocessor():
|
---|
[1545] | 46 | Preprocessor(),
|
---|
[1996] | 47 | mProcessedViewCells(0),
|
---|
[2625] | 48 | mCurrentViewCell(NULL),
|
---|
[2691] | 49 | mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f)),
|
---|
| 50 | mOnlyRandomSampling(false)
|
---|
| 51 | //mOnlyRandomSampling(true)
|
---|
[1460] | 52 | {
|
---|
[1486] | 53 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
| 54 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
[2048] | 55 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.gvsSamplesPerPass", mGvsSamplesPerPass);
|
---|
[1996] | 56 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
|
---|
[1486] | 57 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
[1500] | 58 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
[1990] | 59 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.perViewCell", mPerViewCell);
|
---|
[1996] | 60 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
|
---|
[1473] | 61 |
|
---|
[2048] | 62 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
|
---|
| 63 |
|
---|
[2615] | 64 | Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
|
---|
| 65 |
|
---|
| 66 |
|
---|
[1934] | 67 | char gvsStatsLog[100];
|
---|
| 68 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
| 69 | mGvsStatsStream.open(gvsStatsLog);
|
---|
| 70 |
|
---|
[2677] | 71 | cout << "number of gvs samples per pass: " << mGvsSamplesPerPass << endl;
|
---|
| 72 | cout << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
[2691] | 73 | cout << "only random sampling: " << mOnlyRandomSampling << endl;
|
---|
[2677] | 74 |
|
---|
[1486] | 75 | Debug << "Gvs preprocessor options" << endl;
|
---|
| 76 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
| 77 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
[1501] | 78 | Debug << "threshold: " << mThreshold << endl;
|
---|
[1934] | 79 | Debug << "epsilon: " << mEps << endl;
|
---|
| 80 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
[1996] | 81 | Debug << "per view cell: " << mPerViewCell << endl;
|
---|
| 82 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
| 83 | Debug << "min contribution: " << mMinContribution << endl;
|
---|
[1486] | 84 |
|
---|
[2691] | 85 | mDistribution = new ViewCellBasedDistribution(*this, NULL);
|
---|
[1934] | 86 |
|
---|
| 87 | mGvsStats.Reset();
|
---|
[2643] | 88 |
|
---|
[2648] | 89 | // hack: some generic statistical values that can be read
|
---|
| 90 | // by an application using the preprocessor
|
---|
[2691] | 91 | for (int i = 0; i < 2; ++ i)
|
---|
| 92 | mGenericStats[i] = 0;
|
---|
[1460] | 93 | }
|
---|
| 94 |
|
---|
[1473] | 95 |
|
---|
[1990] | 96 | GvsPreprocessor::~GvsPreprocessor()
|
---|
| 97 | {
|
---|
[2691] | 98 | delete mDistribution;
|
---|
[1996] | 99 | ClearRayQueue();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | void GvsPreprocessor::ClearRayQueue()
|
---|
| 104 | {
|
---|
[1990] | 105 | // clean ray queue
|
---|
| 106 | while (!mRayQueue.empty())
|
---|
| 107 | {
|
---|
| 108 | // handle next ray
|
---|
| 109 | VssRay *ray = mRayQueue.top();
|
---|
| 110 | mRayQueue.pop();
|
---|
| 111 |
|
---|
[2048] | 112 | //delete ray;
|
---|
[1990] | 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[2625] | 117 | ViewCell *GvsPreprocessor::NextViewCell()
|
---|
[1982] | 118 | {
|
---|
[1996] | 119 | if (mProcessedViewCells == (int)mViewCells.size())
|
---|
[2625] | 120 | return NULL; // no more view cells
|
---|
[1982] | 121 |
|
---|
[2625] | 122 | ViewCell *vc = mViewCells[mProcessedViewCells];
|
---|
[1982] | 123 |
|
---|
[2625] | 124 | if (!vc->GetMesh())
|
---|
| 125 | mViewCellsManager->CreateMesh(vc);
|
---|
[1990] | 126 |
|
---|
[2625] | 127 | mGvsStats.mViewCellId = vc->GetId();
|
---|
[1996] | 128 |
|
---|
[2625] | 129 | Debug << "vc: " << vc->GetId() << endl;
|
---|
[1996] | 130 |
|
---|
| 131 | ++ mProcessedViewCells;
|
---|
[1982] | 132 |
|
---|
[2625] | 133 | return vc;
|
---|
[1982] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[1996] | 137 | int GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
| 138 | const Triangle3 &hitTriangle,
|
---|
| 139 | const VssRay &oldRay)
|
---|
[1460] | 140 | {
|
---|
[1932] | 141 | // the predicted hitpoint: we expect to hit the same mesh again
|
---|
| 142 | const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 143 |
|
---|
| 144 | const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
| 145 | const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
| 146 |
|
---|
| 147 | // distance large => this is likely to be a discontinuity
|
---|
[1933] | 148 | #if 1
|
---|
[1932] | 149 | if ((predictedLen - len) > mThreshold)
|
---|
[1933] | 150 | #else // rather use relative distance
|
---|
| 151 | if ((predictedLen / len) > mThreshold)
|
---|
| 152 | #endif
|
---|
[1500] | 153 | {
|
---|
[1933] | 154 | // apply reverse sampling to find the gap
|
---|
[1500] | 155 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
[1933] | 156 |
|
---|
| 157 | if (!newRay)
|
---|
[1996] | 158 | return 1;
|
---|
[1933] | 159 |
|
---|
[1572] | 160 | // set flag for visualization
|
---|
[2690] | 161 | if (0) newRay->mFlags |= VssRay::ReverseSample;
|
---|
[1580] | 162 |
|
---|
[2669] | 163 | // check if ray is not further processed (ray not deleted because using ray pool)
|
---|
| 164 | HandleRay(newRay);
|
---|
[1999] | 165 |
|
---|
[1996] | 166 | return 1;
|
---|
[1500] | 167 | }
|
---|
| 168 |
|
---|
[1996] | 169 | return 0;
|
---|
[1486] | 170 | }
|
---|
[1473] | 171 |
|
---|
[2053] | 172 |
|
---|
[2669] | 173 | void GvsPreprocessor::CountObject(Intersectable *triObj)
|
---|
| 174 | {
|
---|
[2687] | 175 | if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) &&
|
---|
| 176 | (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
|
---|
[2669] | 177 | {
|
---|
| 178 | ++ triObj->mCounter;
|
---|
[2691] | 179 | ++ mGenericStats[1];
|
---|
[2669] | 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
|
---|
| 185 | {
|
---|
[2690] | 186 | //mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
|
---|
[2669] | 187 |
|
---|
| 188 | // count new objects in pvs due to kd node conversion
|
---|
| 189 | myobjects.clear();
|
---|
| 190 | mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
|
---|
| 191 |
|
---|
| 192 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
| 193 |
|
---|
| 194 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 195 | CountObject(*oit);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
[1999] | 199 | bool GvsPreprocessor::HasContribution(VssRay &ray)
|
---|
[1486] | 200 | {
|
---|
[1999] | 201 | if (!ray.mTerminationObject)
|
---|
| 202 | return false;
|
---|
[1933] | 203 |
|
---|
[1999] | 204 | bool result;
|
---|
| 205 |
|
---|
[1977] | 206 | if (!mPerViewCell)
|
---|
| 207 | {
|
---|
[1999] | 208 | // store the rays + the intersected view cells
|
---|
| 209 | const bool storeViewCells = false; //GVS_DEBUG;
|
---|
| 210 |
|
---|
| 211 | mViewCellsManager->ComputeSampleContribution(ray,
|
---|
[1977] | 212 | true,
|
---|
[1999] | 213 | storeViewCells,
|
---|
[1990] | 214 | true);
|
---|
[1999] | 215 |
|
---|
| 216 | result = ray.mPvsContribution > 0;
|
---|
[1977] | 217 | }
|
---|
[2625] | 218 | else // original per view cell gvs
|
---|
[1977] | 219 | {
|
---|
[1999] | 220 | Intersectable *obj = ray.mTerminationObject;
|
---|
| 221 |
|
---|
[2692] | 222 | // test if triangle was accounted for yet
|
---|
[2691] | 223 | result = AddTriangleObject(obj);
|
---|
| 224 |
|
---|
| 225 | if (mUseKdPvs && (1 || result))
|
---|
[1999] | 226 | {
|
---|
[2669] | 227 | // exchange the triangle with the node in the pvs for kd pvs
|
---|
[2691] | 228 | KdNode *node = mKdTree->GetPvsNode(ray.mTermination);
|
---|
| 229 |
|
---|
| 230 | //KdNode *node = mKdTree->GetPvsNodeCheck(ray.mTermination, obj);
|
---|
| 231 | //if (!node) cerr << "e: " << obj->GetBox() << " " << ray.mTermination << endl; else
|
---|
| 232 | if (!node->Mailed())
|
---|
[2615] | 233 | {
|
---|
[2691] | 234 | // add to pvs
|
---|
| 235 | node->Mail();
|
---|
| 236 | KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
|
---|
| 237 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
[1999] | 238 |
|
---|
[2691] | 239 | if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt);
|
---|
[2615] | 240 | }
|
---|
[1999] | 241 | }
|
---|
[1977] | 242 | }
|
---|
[1932] | 243 |
|
---|
[1999] | 244 | return result;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
| 249 | {
|
---|
| 250 | if (!HasContribution(*vssRay))
|
---|
[1977] | 251 | return false;
|
---|
| 252 |
|
---|
[2005] | 253 | if (0 && GVS_DEBUG)
|
---|
[1990] | 254 | mVssRays.push_back(new VssRay(*vssRay));
|
---|
| 255 |
|
---|
[1977] | 256 | // add new ray to ray queue
|
---|
| 257 | mRayQueue.push(vssRay);
|
---|
| 258 |
|
---|
[1990] | 259 | ++ mGvsStats.mTotalContribution;
|
---|
[1533] | 260 |
|
---|
[1977] | 261 | return true;
|
---|
[1460] | 262 | }
|
---|
| 263 |
|
---|
[1500] | 264 |
|
---|
| 265 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
[1489] | 266 | */
|
---|
[1932] | 267 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
| 268 | const Triangle3 &hitTriangle,
|
---|
| 269 | const VssRay &ray,
|
---|
| 270 | const int index) const
|
---|
[1460] | 271 | {
|
---|
[1486] | 272 | const int indexU = (index + 1) % 3;
|
---|
| 273 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
| 274 |
|
---|
[1524] | 275 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
[1486] | 276 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
| 277 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
[1492] | 278 |
|
---|
[1533] | 279 | const float len = Magnitude(a);
|
---|
[1932] | 280 |
|
---|
[1523] | 281 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
| 282 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
[1524] | 283 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
| 284 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
[1492] | 285 |
|
---|
[1486] | 286 | // compute the new three hit points
|
---|
[1500] | 287 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 288 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
[1500] | 289 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 290 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
[1500] | 291 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
[1932] | 292 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
[1489] | 293 |
|
---|
[1524] | 294 | vertices.push_back(pt2);
|
---|
| 295 | vertices.push_back(pt3);
|
---|
[1500] | 296 | vertices.push_back(pt1);
|
---|
[1489] | 297 | }
|
---|
| 298 |
|
---|
| 299 |
|
---|
[1500] | 300 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
| 301 | const Triangle3 &hitTriangle,
|
---|
[1932] | 302 | const VssRay &ray) const
|
---|
[1500] | 303 | {
|
---|
[1932] | 304 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
| 305 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
| 306 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
[1500] | 307 | }
|
---|
[1492] | 308 |
|
---|
[1500] | 309 |
|
---|
[1932] | 310 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
| 311 | const Triangle3 &hitTriangle,
|
---|
| 312 | const VssRay &oldRay) const
|
---|
[1489] | 313 | {
|
---|
[1932] | 314 | // find the intersection of the plane induced by the
|
---|
| 315 | // hit triangle with the new ray
|
---|
[1500] | 316 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
| 317 |
|
---|
| 318 | const Vector3 hitPt =
|
---|
| 319 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
| 320 |
|
---|
| 321 | return hitPt;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
| 326 | {
|
---|
| 327 | return a.mTerminationObject == b.mTerminationObject;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
| 332 | const Vector3 &p1,
|
---|
| 333 | const Vector3 &p2,
|
---|
[1932] | 334 | const VssRay &ray1,
|
---|
| 335 | const VssRay &ray2,
|
---|
[1500] | 336 | const VssRay &oldRay)
|
---|
[2048] | 337 | {
|
---|
| 338 | //cout <<"y"<<Magnitude(p1 - p2) << " ";
|
---|
[1996] | 339 | int castRays = 0;
|
---|
| 340 |
|
---|
[1990] | 341 | // cast reverse rays if necessary
|
---|
[1996] | 342 | castRays += CheckDiscontinuity(ray1, hitTriangle, oldRay);
|
---|
| 343 | castRays += CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
[1500] | 344 |
|
---|
[2017] | 345 | if (EqualVisibility(ray1, ray2) || (SqrMagnitude(p1 - p2) <= MIN_DIST))
|
---|
[1500] | 346 | {
|
---|
[1996] | 347 | return castRays;
|
---|
[1500] | 348 | }
|
---|
[1932] | 349 |
|
---|
[1990] | 350 | // the new subdivision point
|
---|
| 351 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
[2017] | 352 | //cout << "p: " << p << " " << p1 << " " << p2 << endl;
|
---|
[1990] | 353 | // cast ray into the new point
|
---|
| 354 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
[1932] | 355 |
|
---|
[1996] | 356 | VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
| 357 |
|
---|
| 358 | ++ castRays;
|
---|
[1580] | 359 |
|
---|
[1996] | 360 | if (!newRay) return castRays;
|
---|
[1580] | 361 |
|
---|
[2053] | 362 | //newRay->mFlags |= VssRay::BorderSample;
|
---|
[1990] | 363 |
|
---|
| 364 | // add new ray to queue
|
---|
| 365 | const bool enqueued = HandleRay(newRay);
|
---|
| 366 |
|
---|
| 367 | // subdivide further
|
---|
[1996] | 368 | castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
| 369 | castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
[1990] | 370 |
|
---|
| 371 | // this ray will not be further processed
|
---|
[2692] | 372 | // note: ray deletion is handled by ray pool
|
---|
[1990] | 373 |
|
---|
[1996] | 374 | return castRays;
|
---|
[1500] | 375 | }
|
---|
| 376 |
|
---|
| 377 |
|
---|
| 378 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
| 379 | {
|
---|
| 380 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[2597] | 381 | // q matt: can this be possible
|
---|
| 382 | if (!tObj) return 0;
|
---|
| 383 |
|
---|
[1486] | 384 | Triangle3 hitTriangle;
|
---|
[1489] | 385 |
|
---|
| 386 | // other types not implemented yet
|
---|
| 387 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
[2017] | 388 | hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
[1522] | 389 | else
|
---|
[2601] | 390 | cout << "border sampling: " << Intersectable::GetTypeName(tObj) << " not yet implemented" << endl;
|
---|
[1489] | 391 |
|
---|
[2601] | 392 | //cout << "type: " << Intersectable::GetTypeName(tObj) << endl;
|
---|
| 393 |
|
---|
[1500] | 394 | VertexContainer enlargedTriangle;
|
---|
| 395 |
|
---|
| 396 | /// create 3 new hit points for each vertex
|
---|
| 397 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
| 398 |
|
---|
| 399 | /// create rays from sample points and handle them
|
---|
[1492] | 400 | SimpleRayContainer simpleRays;
|
---|
| 401 | simpleRays.reserve(9);
|
---|
[1486] | 402 |
|
---|
[1500] | 403 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
[1486] | 404 |
|
---|
[1500] | 405 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
| 406 | {
|
---|
| 407 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
[1990] | 408 |
|
---|
[1883] | 409 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1990] | 410 | simpleRays.AddRay(sr);
|
---|
[1500] | 411 | }
|
---|
[1522] | 412 |
|
---|
[1566] | 413 | if (0)
|
---|
| 414 | {
|
---|
[1932] | 415 | // visualize enlarged triangles
|
---|
[1566] | 416 | VizStruct dummy;
|
---|
| 417 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
| 418 | dummy.originalTriangle = hitTriangle;
|
---|
| 419 | vizContainer.push_back(dummy);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[1524] | 422 | // cast rays to triangle vertices and determine visibility
|
---|
[1489] | 423 | VssRayContainer vssRays;
|
---|
[1533] | 424 |
|
---|
[1932] | 425 | // don't cast double rays as we need only the forward rays
|
---|
[1996] | 426 | const bool castDoubleRays = !mPerViewCell;
|
---|
[1990] | 427 | // cannot prune invalid rays because we have to compare adjacent rays.
|
---|
[1932] | 428 | const bool pruneInvalidRays = false;
|
---|
| 429 |
|
---|
[1996] | 430 |
|
---|
| 431 | //////////
|
---|
| 432 | //-- fill up simple rays with random rays so we can cast 16
|
---|
| 433 |
|
---|
| 434 | //const int numRandomRays = 0;
|
---|
| 435 | const int numRandomRays = 16 - (int)simpleRays.size();
|
---|
| 436 |
|
---|
[2691] | 437 | GenerateRays(numRandomRays, *mDistribution, simpleRays);
|
---|
[1996] | 438 |
|
---|
[2053] | 439 |
|
---|
[1996] | 440 | /////////////////////
|
---|
| 441 |
|
---|
[1990] | 442 | // keep origin for per view cell sampling
|
---|
[1996] | 443 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
[1932] | 444 |
|
---|
[1996] | 445 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
[2648] | 446 |
|
---|
| 447 | #if 0
|
---|
[1580] | 448 | // set flags
|
---|
[2648] | 449 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
[1996] | 450 | for (rit = vssRays.begin(); rit != rit_end; ++ rit, ++ i)
|
---|
[2053] | 451 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
[2648] | 452 | #endif
|
---|
| 453 |
|
---|
[1999] | 454 | int castRays = (int)simpleRays.size();
|
---|
| 455 |
|
---|
| 456 | VssRayContainer invalidSamples;
|
---|
| 457 |
|
---|
[1932] | 458 | // handle rays
|
---|
[1999] | 459 | EnqueueRays(vssRays, invalidSamples);
|
---|
| 460 |
|
---|
[1500] | 461 | // recursivly subdivide each edge
|
---|
[1996] | 462 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
[1500] | 463 | {
|
---|
[1932] | 464 | castRays += SubdivideEdge(hitTriangle,
|
---|
| 465 | enlargedTriangle[i],
|
---|
[1996] | 466 | enlargedTriangle[(i + 1) % numBorderSamples],
|
---|
[1932] | 467 | *vssRays[i],
|
---|
[1996] | 468 | *vssRays[(i + 1) % numBorderSamples],
|
---|
[1932] | 469 | currentRay);
|
---|
[1500] | 470 | }
|
---|
[1999] | 471 |
|
---|
[1934] | 472 | mGvsStats.mBorderSamples += castRays;
|
---|
[1932] | 473 |
|
---|
[2053] | 474 | //CLEAR_CONTAINER(invalidSamples);
|
---|
[2048] | 475 |
|
---|
[2687] | 476 | //cout << "here3 cast rays: " << castRays << endl;
|
---|
[1533] | 477 | return castRays;
|
---|
[1473] | 478 | }
|
---|
| 479 |
|
---|
| 480 |
|
---|
[1996] | 481 | bool GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
| 482 | const Triangle3 &occluder,
|
---|
| 483 | const VssRay &oldRay,
|
---|
| 484 | Vector3 &newPoint) const
|
---|
[1933] | 485 | {
|
---|
| 486 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
| 487 | //-- with the newly found occluder (xold is the previous ray from
|
---|
| 488 | //-- which x was generated). On the intersecting line, we select a point
|
---|
| 489 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
| 490 | //-- just passes through the gap
|
---|
| 491 |
|
---|
| 492 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
| 493 | currentRay.GetTermination(),
|
---|
| 494 | oldRay.GetTermination());
|
---|
| 495 |
|
---|
| 496 | Vector3 pt1, pt2;
|
---|
| 497 |
|
---|
| 498 | const bool intersects = occluder.GetPlaneIntersection(plane, pt1, pt2);
|
---|
| 499 |
|
---|
| 500 | if (!intersects)
|
---|
[1996] | 501 | {
|
---|
[1999] | 502 | //cerr << "big error!! no intersection " << pt1 << " " << pt2 << endl;
|
---|
[1996] | 503 | return false;
|
---|
| 504 | }
|
---|
[1933] | 505 |
|
---|
| 506 | // get the intersection point on the old ray
|
---|
| 507 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
| 508 |
|
---|
| 509 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
| 510 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
| 511 |
|
---|
| 512 | // Evaluate new hitpoint just outside the triangle
|
---|
| 513 | const float eps = mEps;
|
---|
[1990] | 514 |
|
---|
[1933] | 515 | // the point is chosen to be on the side closer to the original ray
|
---|
| 516 | if (Distance(pt1, pt3) < Distance(pt2, pt3))
|
---|
| 517 | newPoint = pt1 + eps * (pt1 - pt2);
|
---|
| 518 | else
|
---|
| 519 | newPoint = pt2 + eps * (pt2 - pt1);
|
---|
| 520 |
|
---|
[1934] | 521 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
[1996] | 522 | return true;
|
---|
[1500] | 523 | }
|
---|
| 524 |
|
---|
| 525 |
|
---|
| 526 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
| 527 | const Triangle3 &hitTriangle,
|
---|
| 528 | const VssRay &oldRay)
|
---|
| 529 | {
|
---|
[1933] | 530 | // get triangle occluding the path to the hit mesh
|
---|
| 531 | Triangle3 occluder;
|
---|
| 532 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
[1934] | 533 |
|
---|
[1933] | 534 | // q: why can this happen?
|
---|
| 535 | if (!tObj)
|
---|
| 536 | return NULL;
|
---|
| 537 |
|
---|
| 538 | // other types not implemented yet
|
---|
| 539 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
[2048] | 540 | {
|
---|
[2017] | 541 | occluder = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
[2048] | 542 | }
|
---|
[1933] | 543 | else
|
---|
[2048] | 544 | {
|
---|
| 545 | cout << "reverse sampling: " << tObj->Type() << " not yet implemented" << endl;
|
---|
| 546 | }
|
---|
[1933] | 547 | // get a point which is passing just outside of the occluder
|
---|
[1996] | 548 | Vector3 newPoint;
|
---|
| 549 |
|
---|
| 550 | // why is there sometimes no intersecton found?
|
---|
| 551 | if (!GetPassingPoint(currentRay, occluder, oldRay, newPoint))
|
---|
| 552 | return NULL;
|
---|
| 553 |
|
---|
[1500] | 554 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
| 555 |
|
---|
[1990] | 556 | Vector3 newDir, newOrigin;
|
---|
| 557 |
|
---|
[1932] | 558 | //-- Construct the mutated ray with xnew,
|
---|
| 559 | //-- dir = predicted(x)- pnew as direction vector
|
---|
[1990] | 560 | newDir = predicted - newPoint;
|
---|
[1932] | 561 |
|
---|
[1933] | 562 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
[1500] | 563 | // difficult to say!!
|
---|
[1935] | 564 | const float offset = 0.5f;
|
---|
[1990] | 565 | newOrigin = newPoint - newDir * offset;
|
---|
| 566 |
|
---|
| 567 | //////////////
|
---|
| 568 | //-- for per view cell sampling, we must check for intersection
|
---|
| 569 | //-- with the current view cell
|
---|
[1500] | 570 |
|
---|
[1990] | 571 | if (mPerViewCell)
|
---|
| 572 | {
|
---|
| 573 | // send ray to view cell
|
---|
| 574 | static Ray ray;
|
---|
| 575 | ray.Clear();
|
---|
| 576 | ray.Init(newOrigin, -newDir, Ray::LOCAL_RAY);
|
---|
| 577 |
|
---|
| 578 | //cout << "z";
|
---|
| 579 | // check if ray intersects view cell
|
---|
| 580 | if (!mCurrentViewCell->CastRay(ray))
|
---|
| 581 | return NULL;
|
---|
[1981] | 582 |
|
---|
[1990] | 583 | Ray::Intersection &hit = ray.intersections[0];
|
---|
| 584 |
|
---|
| 585 | //cout << "q";
|
---|
| 586 | // the ray starts from the view cell
|
---|
| 587 | newOrigin = ray.Extrap(hit.mT);
|
---|
| 588 | }
|
---|
[1981] | 589 |
|
---|
[1933] | 590 | const SimpleRay simpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
[1571] | 591 |
|
---|
[1933] | 592 | VssRay *reverseRay =
|
---|
[1996] | 593 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
[1933] | 594 |
|
---|
[1934] | 595 | ++ mGvsStats.mReverseSamples;
|
---|
[1933] | 596 |
|
---|
| 597 | return reverseRay;
|
---|
[1473] | 598 | }
|
---|
| 599 |
|
---|
[1489] | 600 |
|
---|
[2691] | 601 | int GvsPreprocessor::CastInitialSamples(int numSamples)
|
---|
[1489] | 602 | {
|
---|
[1473] | 603 | const long startTime = GetTime();
|
---|
[1595] | 604 |
|
---|
[1489] | 605 | // generate simple rays
|
---|
| 606 | SimpleRayContainer simpleRays;
|
---|
[1990] | 607 |
|
---|
[2691] | 608 | ViewCellBorderBasedDistribution vcStrat(*this, mCurrentViewCell);
|
---|
| 609 | GenerateRays(numSamples, *mDistribution, simpleRays);
|
---|
[1990] | 610 |
|
---|
| 611 | //cout << "sr: " << simpleRays.size() << endl;
|
---|
[1489] | 612 | // generate vss rays
|
---|
[1473] | 613 | VssRayContainer samples;
|
---|
[1990] | 614 |
|
---|
[1996] | 615 | const bool castDoubleRays = !mPerViewCell;
|
---|
[1990] | 616 | const bool pruneInvalidRays = true;
|
---|
| 617 |
|
---|
[1996] | 618 | CastRays(simpleRays, samples, castDoubleRays, pruneInvalidRays);
|
---|
| 619 |
|
---|
[1999] | 620 | VssRayContainer invalidSamples;
|
---|
| 621 |
|
---|
[1489] | 622 | // add to ray queue
|
---|
[1999] | 623 | EnqueueRays(samples, invalidSamples);
|
---|
[1533] | 624 |
|
---|
[2053] | 625 | //CLEAR_CONTAINER(invalidSamples);
|
---|
[2633] | 626 | //Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1990] | 627 | return (int)simpleRays.size();
|
---|
[1489] | 628 | }
|
---|
| 629 |
|
---|
| 630 |
|
---|
[1999] | 631 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples)
|
---|
[1489] | 632 | {
|
---|
[1486] | 633 | // add samples to ray queue
|
---|
[1473] | 634 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
[1576] | 635 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
[1473] | 636 | {
|
---|
[1999] | 637 | VssRay *ray = *vit;
|
---|
| 638 |
|
---|
[2053] | 639 | HandleRay(ray);
|
---|
[2633] | 640 | //if (!HandleRay(ray)) invalidSamples.push_back(ray);
|
---|
[1460] | 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[1473] | 644 |
|
---|
[1990] | 645 | int GvsPreprocessor::ProcessQueue()
|
---|
[1460] | 646 | {
|
---|
[1990] | 647 | ++ mGvsStats.mGvsPass;
|
---|
[1545] | 648 |
|
---|
[2687] | 649 | int castSamples = 0;
|
---|
| 650 |
|
---|
[2048] | 651 | while (!mRayQueue.empty())//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
|
---|
[2690] | 652 | {
|
---|
[1990] | 653 | // handle next ray
|
---|
| 654 | VssRay *ray = mRayQueue.top();
|
---|
| 655 | mRayQueue.pop();
|
---|
[1996] | 656 |
|
---|
[1990] | 657 | const int newSamples = AdaptiveBorderSampling(*ray);
|
---|
[1934] | 658 |
|
---|
[1990] | 659 | castSamples += newSamples;
|
---|
[2687] | 660 | // note: don't have to delete because handled by ray pool
|
---|
[1473] | 661 | }
|
---|
| 662 |
|
---|
[2161] | 663 | /*if (mRayCaster->mVssRayPool.mIndex > mSamplesPerPass)
|
---|
[2048] | 664 | {
|
---|
| 665 | cout << "warning: new samples: " << castSamples << " " << "queue: " << (int)mRayQueue.size() << endl;
|
---|
| 666 | Debug << "warning: new samples: " << castSamples << " " << "queue: " << (int)mRayQueue.size() << endl;
|
---|
[2161] | 667 | }*/
|
---|
[2048] | 668 |
|
---|
[1533] | 669 | return castSamples;
|
---|
[1460] | 670 | }
|
---|
| 671 |
|
---|
[1473] | 672 |
|
---|
[1990] | 673 | void ExportVssRays(Exporter *exporter, const VssRayContainer &vssRays)
|
---|
[1460] | 674 | {
|
---|
[1990] | 675 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
[1473] | 676 |
|
---|
[1990] | 677 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
| 678 |
|
---|
| 679 | // prepare some rays for output
|
---|
| 680 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
[1473] | 681 | {
|
---|
[1990] | 682 | //const float p = RandomValue(0.0f, (float)vssRays.size());
|
---|
[1545] | 683 |
|
---|
[1990] | 684 | if (1)//(p < raysOut)
|
---|
| 685 | {
|
---|
| 686 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
| 687 | {
|
---|
| 688 | vcRays.push_back(*rit);
|
---|
| 689 | }
|
---|
| 690 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
| 691 | {
|
---|
| 692 | vcRays2.push_back(*rit);
|
---|
| 693 | }
|
---|
| 694 | else
|
---|
| 695 | {
|
---|
| 696 | vcRays3.push_back(*rit);
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
[1460] | 699 | }
|
---|
[1990] | 700 |
|
---|
[2005] | 701 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
| 702 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
[2003] | 703 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
[1990] | 704 | }
|
---|
| 705 |
|
---|
| 706 |
|
---|
[1999] | 707 | void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects)
|
---|
[1990] | 708 | {
|
---|
| 709 | Intersectable::NewMail();
|
---|
| 710 | Material m;
|
---|
[1528] | 711 |
|
---|
[1996] | 712 | char str[64]; sprintf(str, "pass%06d.wrl", mProcessedViewCells);
|
---|
[1990] | 713 |
|
---|
| 714 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
| 715 | if (!exporter)
|
---|
| 716 | return;
|
---|
| 717 |
|
---|
[1999] | 718 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 719 |
|
---|
| 720 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 721 | {
|
---|
| 722 | Intersectable *intersect = *oit;
|
---|
| 723 |
|
---|
| 724 | m = RandomMaterial();
|
---|
| 725 | exporter->SetForcedMaterial(m);
|
---|
| 726 | exporter->ExportIntersectable(intersect);
|
---|
[1990] | 727 | }
|
---|
| 728 |
|
---|
| 729 | cout << "vssrays: " << (int)mVssRays.size() << endl;
|
---|
| 730 | ExportVssRays(exporter, mVssRays);
|
---|
| 731 |
|
---|
[2597] | 732 |
|
---|
[1990] | 733 | /////////////////
|
---|
| 734 | //-- export view cell geometry
|
---|
| 735 |
|
---|
[2597] | 736 | //exporter->SetWireframe();
|
---|
[1990] | 737 |
|
---|
| 738 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 739 | exporter->SetForcedMaterial(m);
|
---|
| 740 |
|
---|
[1999] | 741 | //mViewCellsManager->ExportViewCellGeometry(exporter, vc, NULL, NULL);
|
---|
| 742 | //mViewCellsManager->ExportViewCellGeometry(exporter, mCurrentViewCell, NULL, NULL);
|
---|
| 743 |
|
---|
| 744 | AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox;
|
---|
| 745 | exporter->ExportBox(bbox);
|
---|
[1990] | 746 | //exporter->SetFilled();
|
---|
| 747 |
|
---|
[2053] | 748 | delete exporter;
|
---|
[1460] | 749 | }
|
---|
| 750 |
|
---|
| 751 |
|
---|
[1982] | 752 | void GvsPreprocessor::VisualizeViewCells()
|
---|
| 753 | {
|
---|
[1996] | 754 | char str[64]; sprintf(str, "tmp/pass%06d_%04d-", mProcessedViewCells, mPass);
|
---|
[1982] | 755 |
|
---|
| 756 | // visualization
|
---|
| 757 | if (mGvsStats.mPassContribution > 0)
|
---|
| 758 | {
|
---|
| 759 | const bool exportRays = true;
|
---|
| 760 | const bool exportPvs = true;
|
---|
| 761 |
|
---|
| 762 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
| 763 | 10,
|
---|
| 764 | false,
|
---|
| 765 | exportPvs,
|
---|
| 766 | exportRays,
|
---|
| 767 | 1000,
|
---|
| 768 | str);
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | // remove pass samples
|
---|
| 772 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
| 773 |
|
---|
| 774 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
| 775 | {
|
---|
| 776 | (*vit)->DelRayRefs();
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 |
|
---|
[2687] | 781 | void GvsPreprocessor::CompileViewCellsFromPointList()
|
---|
[1996] | 782 | {
|
---|
[2687] | 783 | ViewCell::NewMail();
|
---|
| 784 |
|
---|
[2648] | 785 | // Receive list of view cells from view cells manager
|
---|
[2598] | 786 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
[2687] | 787 |
|
---|
| 788 | vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
|
---|
| 789 |
|
---|
| 790 | for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
|
---|
[2048] | 791 | {
|
---|
[2687] | 792 | ViewCellPoints *vp = *vit;
|
---|
[2048] | 793 |
|
---|
[2687] | 794 | if (vp->first) // view cell specified
|
---|
[2648] | 795 | {
|
---|
[2687] | 796 | ViewCell *viewCell = vp->first;
|
---|
[2048] | 797 |
|
---|
[2687] | 798 | if (!viewCell->Mailed())
|
---|
| 799 | {
|
---|
| 800 | viewCell->Mail();
|
---|
| 801 | mViewCells.push_back(viewCell);
|
---|
| 802 |
|
---|
| 803 | if (mViewCells.size() >= mMaxViewCells)
|
---|
| 804 | break;
|
---|
| 805 | }
|
---|
[2662] | 806 | }
|
---|
[2687] | 807 | else // no view cell specified => compute view cells using view point
|
---|
| 808 | {
|
---|
| 809 | SimpleRayContainer::const_iterator rit, rit_end = vp->second.end();
|
---|
| 810 |
|
---|
| 811 | for (rit = vp->second.begin(); rit != rit_end; ++ rit)
|
---|
| 812 | {
|
---|
| 813 | SimpleRay ray = *rit;
|
---|
| 814 |
|
---|
| 815 | ViewCell *viewCell = mViewCellsManager->GetViewCell(ray.mOrigin);
|
---|
| 816 |
|
---|
| 817 | if (viewCell && !viewCell->Mailed())
|
---|
| 818 | {
|
---|
| 819 | viewCell->Mail();
|
---|
| 820 | mViewCells.push_back(viewCell);
|
---|
| 821 |
|
---|
| 822 | if (mViewCells.size() >= mMaxViewCells)
|
---|
| 823 | break;
|
---|
| 824 | }
|
---|
| 825 | }
|
---|
| 826 | }
|
---|
[2048] | 827 | }
|
---|
[2687] | 828 | }
|
---|
| 829 |
|
---|
| 830 |
|
---|
| 831 | void GvsPreprocessor::CompileViewCellsList()
|
---|
| 832 | {
|
---|
| 833 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
| 834 | {
|
---|
| 835 | cout << "processing view point list" << endl;
|
---|
| 836 | CompileViewCellsFromPointList();
|
---|
| 837 | }
|
---|
[2662] | 838 | else
|
---|
| 839 | {
|
---|
[2687] | 840 | ViewCell::NewMail();
|
---|
| 841 |
|
---|
[2662] | 842 | while ((int)mViewCells.size() < mMaxViewCells)
|
---|
[1996] | 843 | {
|
---|
[2662] | 844 | const int tries = 10000;
|
---|
| 845 | int i = 0;
|
---|
[1996] | 846 |
|
---|
[2662] | 847 | for (i = 0; i < tries; ++ i)
|
---|
[1996] | 848 | {
|
---|
[2662] | 849 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f);
|
---|
| 850 |
|
---|
| 851 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
| 852 |
|
---|
| 853 | if (!viewCell->Mailed())
|
---|
| 854 | {
|
---|
| 855 | viewCell->Mail();
|
---|
| 856 | break;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | mViewCells.push_back(viewCell);
|
---|
[1999] | 860 | }
|
---|
[2597] | 861 |
|
---|
[2662] | 862 | if (i == tries)
|
---|
| 863 | {
|
---|
| 864 | cerr << "big error! no view cell found" << endl;
|
---|
| 865 | break;
|
---|
| 866 | }
|
---|
[1999] | 867 | }
|
---|
| 868 | }
|
---|
[2662] | 869 |
|
---|
| 870 | cout << "\ncomputing list of " << mViewCells.size() << " view cells" << endl;
|
---|
[1999] | 871 | }
|
---|
[1996] | 872 |
|
---|
[2669] | 873 |
|
---|
[2687] | 874 | void GvsPreprocessor::IntersectWithViewCell()
|
---|
| 875 | {
|
---|
[2691] | 876 | AxisAlignedBox3 box = mCurrentViewCell->GetBox();
|
---|
[2687] | 877 |
|
---|
[2691] | 878 | int oldTrianglePvs = (int)mTrianglePvs.size();
|
---|
| 879 | int newKdObj = 0;
|
---|
| 880 |
|
---|
[2687] | 881 | // compute pvs kd nodes that intersect view cell
|
---|
| 882 | ObjectContainer kdobjects;
|
---|
[2691] | 883 |
|
---|
| 884 | // find intersecting objects not in pvs (i.e., only unmailed)
|
---|
[2687] | 885 | mKdTree->CollectKdObjects(box, kdobjects);
|
---|
| 886 |
|
---|
| 887 | ObjectContainer pvsKdObjects;
|
---|
| 888 |
|
---|
| 889 | ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
|
---|
| 890 |
|
---|
| 891 | for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 892 | {
|
---|
| 893 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
|
---|
| 894 |
|
---|
| 895 | myobjects.clear();
|
---|
[2691] | 896 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), myobjects);
|
---|
[2687] | 897 |
|
---|
| 898 | // account for kd object pvs
|
---|
| 899 | bool addkdobj = false;
|
---|
| 900 |
|
---|
| 901 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
| 902 |
|
---|
| 903 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
| 904 | {
|
---|
| 905 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
|
---|
[2691] | 906 |
|
---|
| 907 | // test if the triangle itself intersects
|
---|
[2687] | 908 | if (box.Intersects(triObj->GetItem()))
|
---|
[2691] | 909 | addkdobj = AddTriangleObject(triObj);
|
---|
[2687] | 910 | }
|
---|
| 911 |
|
---|
[2691] | 912 | // add node to kd pvs
|
---|
| 913 | if (1 && addkdobj)
|
---|
| 914 | {
|
---|
| 915 | ++ newKdObj;
|
---|
| 916 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
| 917 | //mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
|
---|
| 918 | if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt);
|
---|
| 919 | }
|
---|
[2687] | 920 | }
|
---|
| 921 |
|
---|
[2691] | 922 | cout << "added " << (int)mTrianglePvs.size() - oldTrianglePvs << " triangles (" << newKdObj << " objects) by intersection" << endl;
|
---|
[2687] | 923 | }
|
---|
| 924 |
|
---|
[2691] | 925 |
|
---|
[1990] | 926 | void GvsPreprocessor::PerViewCellComputation()
|
---|
[1982] | 927 | {
|
---|
[2690] | 928 | while (mCurrentViewCell = NextViewCell())
|
---|
[1999] | 929 | {
|
---|
[2625] | 930 | // hack: reset counters
|
---|
| 931 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
[2615] | 932 |
|
---|
[2625] | 933 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
[2669] | 934 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
[1982] | 935 |
|
---|
[2691] | 936 | mDistribution->SetViewCell(mCurrentViewCell);
|
---|
[2690] | 937 | ComputeViewCell();
|
---|
[2625] | 938 | }
|
---|
| 939 | }
|
---|
[1990] | 940 |
|
---|
[1996] | 941 |
|
---|
[2625] | 942 | void GvsPreprocessor::PerViewCellComputation2()
|
---|
| 943 | {
|
---|
| 944 | while (1)
|
---|
| 945 | {
|
---|
| 946 | if (!mRendererWidget)
|
---|
| 947 | continue;
|
---|
[1990] | 948 |
|
---|
[2690] | 949 | mCurrentViewCell = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
|
---|
[2601] | 950 |
|
---|
[2625] | 951 | // no valid view cell or view cell already computed
|
---|
[2690] | 952 | if (!mCurrentViewCell || !mCurrentViewCell->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
|
---|
[2625] | 953 | continue;
|
---|
[2615] | 954 |
|
---|
[2643] | 955 | mRendererWidget->mComputeGVS = false;
|
---|
[2625] | 956 | // hack: reset counters
|
---|
| 957 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
[2615] | 958 |
|
---|
[2625] | 959 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
[2669] | 960 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
[2601] | 961 |
|
---|
[2690] | 962 | ComputeViewCell();
|
---|
[2625] | 963 | ++ mProcessedViewCells;
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
[1999] | 966 |
|
---|
[1982] | 967 |
|
---|
[2048] | 968 | void GvsPreprocessor::StorePvs(const ObjectContainer &objectPvs)
|
---|
| 969 | {
|
---|
| 970 | ObjectContainer::const_iterator oit, oit_end = objectPvs.end();
|
---|
| 971 |
|
---|
| 972 | for (oit = objectPvs.begin(); oit != oit_end; ++ oit)
|
---|
| 973 | {
|
---|
| 974 | mCurrentViewCell->GetPvs().AddSample(*oit, 1);
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 |
|
---|
[1990] | 979 | void GvsPreprocessor::UpdatePvs(ViewCell *currentViewCell)
|
---|
| 980 | {
|
---|
| 981 | ObjectPvs newPvs;
|
---|
| 982 | BvhLeaf::NewMail();
|
---|
| 983 |
|
---|
| 984 | ObjectPvsIterator pit = currentViewCell->GetPvs().GetIterator();
|
---|
| 985 |
|
---|
| 986 | // output PVS of view cell
|
---|
| 987 | while (pit.HasMoreEntries())
|
---|
| 988 | {
|
---|
[2117] | 989 | Intersectable *intersect = pit.Next();
|
---|
[1990] | 990 |
|
---|
| 991 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 992 |
|
---|
| 993 | if (!bv || bv->Mailed())
|
---|
| 994 | continue;
|
---|
| 995 |
|
---|
| 996 | bv->Mail();
|
---|
| 997 |
|
---|
| 998 | //m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 999 | newPvs.AddSampleDirty(bv, 1.0f);
|
---|
[1982] | 1000 | }
|
---|
[1990] | 1001 |
|
---|
| 1002 | newPvs.SimpleSort();
|
---|
| 1003 |
|
---|
| 1004 | currentViewCell->SetPvs(newPvs);
|
---|
[1982] | 1005 | }
|
---|
| 1006 |
|
---|
[1999] | 1007 |
|
---|
| 1008 | void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const
|
---|
| 1009 | {
|
---|
[2053] | 1010 | objectPvs.reserve((int)mTrianglePvs.size());
|
---|
| 1011 |
|
---|
[1999] | 1012 | BvhLeaf::NewMail();
|
---|
[1982] | 1013 |
|
---|
[1999] | 1014 | ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
|
---|
| 1015 |
|
---|
| 1016 | for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit)
|
---|
| 1017 | {
|
---|
| 1018 | Intersectable *intersect = *oit;
|
---|
| 1019 |
|
---|
| 1020 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
| 1021 |
|
---|
[2053] | 1022 | // hack: reset counter
|
---|
[2669] | 1023 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
[2053] | 1024 |
|
---|
[1999] | 1025 | if (!bv || bv->Mailed())
|
---|
| 1026 | continue;
|
---|
[2615] | 1027 |
|
---|
[1999] | 1028 | bv->Mail();
|
---|
| 1029 | objectPvs.push_back(bv);
|
---|
| 1030 | }
|
---|
| 1031 | }
|
---|
| 1032 |
|
---|
| 1033 |
|
---|
[1990] | 1034 | void GvsPreprocessor::GlobalComputation()
|
---|
| 1035 | {
|
---|
| 1036 | int passSamples = 0;
|
---|
| 1037 | int oldContribution = 0;
|
---|
| 1038 |
|
---|
| 1039 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
| 1040 | {
|
---|
[2048] | 1041 | mRayCaster->InitPass();
|
---|
[2691] | 1042 |
|
---|
[1990] | 1043 | // Ray queue empty =>
|
---|
| 1044 | // cast a number of uniform samples to fill ray queue
|
---|
[2691] | 1045 | int newSamples = CastInitialSamples(mInitialSamples);
|
---|
[1990] | 1046 |
|
---|
| 1047 | if (!mOnlyRandomSampling)
|
---|
| 1048 | newSamples += ProcessQueue();
|
---|
| 1049 |
|
---|
| 1050 | passSamples += newSamples;
|
---|
| 1051 | mGvsStats.mTotalSamples += newSamples;
|
---|
| 1052 |
|
---|
[2048] | 1053 | if (passSamples % (mGvsSamplesPerPass + 1) == mGvsSamplesPerPass)
|
---|
[1990] | 1054 | {
|
---|
| 1055 | ++ mPass;
|
---|
| 1056 |
|
---|
| 1057 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 1058 |
|
---|
| 1059 | ////////
|
---|
| 1060 | //-- stats
|
---|
| 1061 |
|
---|
| 1062 | //cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
|
---|
| 1063 | mGvsStats.mPass = mPass;
|
---|
| 1064 | mGvsStats.Stop();
|
---|
| 1065 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1066 |
|
---|
| 1067 | // reset
|
---|
| 1068 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1069 | mGvsStats.mPassContribution = 0;
|
---|
| 1070 | passSamples = 0;
|
---|
| 1071 |
|
---|
| 1072 | if (GVS_DEBUG)
|
---|
| 1073 | VisualizeViewCells();
|
---|
| 1074 | }
|
---|
| 1075 | }
|
---|
| 1076 | }
|
---|
| 1077 |
|
---|
| 1078 |
|
---|
[1489] | 1079 | bool GvsPreprocessor::ComputeVisibility()
|
---|
[1460] | 1080 | {
|
---|
[1533] | 1081 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
[1545] | 1082 | const long startTime = GetTime();
|
---|
[1533] | 1083 |
|
---|
[1990] | 1084 | //Randomize(0);
|
---|
[1934] | 1085 | mGvsStats.Reset();
|
---|
| 1086 | mGvsStats.Start();
|
---|
[1545] | 1087 |
|
---|
[1486] | 1088 | if (!mLoadViewCells)
|
---|
[1563] | 1089 | {
|
---|
| 1090 | /// construct the view cells from the scratch
|
---|
| 1091 | ConstructViewCells();
|
---|
[1580] | 1092 | // reset pvs already gathered during view cells construction
|
---|
| 1093 | mViewCellsManager->ResetPvs();
|
---|
[1563] | 1094 | cout << "finished view cell construction" << endl;
|
---|
[1486] | 1095 | }
|
---|
[1460] | 1096 |
|
---|
[1982] | 1097 | if (mPerViewCell)
|
---|
[1473] | 1098 | {
|
---|
[2648] | 1099 | #if 1
|
---|
[2597] | 1100 | // provide list of view cells to compute
|
---|
[1996] | 1101 | CompileViewCellsList();
|
---|
[2686] | 1102 |
|
---|
[2597] | 1103 | // start per view cell gvs
|
---|
[2048] | 1104 | PerViewCellComputation();
|
---|
[2625] | 1105 | #else
|
---|
| 1106 | PerViewCellComputation2();
|
---|
| 1107 |
|
---|
| 1108 | #endif
|
---|
[1460] | 1109 | }
|
---|
[1982] | 1110 | else
|
---|
| 1111 | {
|
---|
| 1112 | GlobalComputation();
|
---|
| 1113 | }
|
---|
[1460] | 1114 |
|
---|
[2687] | 1115 | cout << "cast " << mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M single rays/s" << endl;
|
---|
[1545] | 1116 |
|
---|
[1934] | 1117 | if (GVS_DEBUG)
|
---|
| 1118 | {
|
---|
| 1119 | Visualize();
|
---|
| 1120 | CLEAR_CONTAINER(mVssRays);
|
---|
| 1121 | }
|
---|
[2048] | 1122 |
|
---|
| 1123 | // export the preprocessed information to a file
|
---|
| 1124 | if (0 && mExportVisibility)
|
---|
| 1125 | {
|
---|
| 1126 | ExportPreprocessedData(mVisibilityFileName);
|
---|
| 1127 | }
|
---|
[2648] | 1128 |
|
---|
| 1129 | // compute the pixel error of this visibility solution
|
---|
| 1130 | if (mEvaluatePixelError)
|
---|
| 1131 | {
|
---|
| 1132 | ComputeRenderError();
|
---|
| 1133 | }
|
---|
[1934] | 1134 |
|
---|
[1473] | 1135 | return true;
|
---|
[1460] | 1136 | }
|
---|
| 1137 |
|
---|
[1522] | 1138 |
|
---|
[2048] | 1139 | void GvsPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
| 1140 | {
|
---|
[2187] | 1141 | // store triangle directly
|
---|
[2048] | 1142 | mViewCellsManager->DeterminePvsObjects(rays, true);
|
---|
| 1143 | }
|
---|
| 1144 |
|
---|
| 1145 |
|
---|
[1522] | 1146 | void GvsPreprocessor::Visualize()
|
---|
| 1147 | {
|
---|
| 1148 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
| 1149 |
|
---|
| 1150 | if (!exporter)
|
---|
| 1151 | return;
|
---|
| 1152 |
|
---|
| 1153 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
[1999] | 1154 |
|
---|
[1522] | 1155 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
| 1156 | {
|
---|
[1524] | 1157 | exporter->SetWireframe();
|
---|
[1522] | 1158 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
| 1159 | //Material m;
|
---|
[1524] | 1160 | exporter->SetFilled();
|
---|
| 1161 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
| 1162 | exporter->ExportPolygon(&poly);
|
---|
[1522] | 1163 | }
|
---|
| 1164 |
|
---|
[1933] | 1165 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
[1999] | 1166 |
|
---|
[1933] | 1167 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 1168 | {
|
---|
| 1169 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
| 1170 | exporter->ExportIntersectable(obj);
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
[1990] | 1173 | ExportVssRays(exporter, mVssRays);
|
---|
| 1174 |
|
---|
[1522] | 1175 | delete exporter;
|
---|
[1460] | 1176 | }
|
---|
[1522] | 1177 |
|
---|
[1934] | 1178 |
|
---|
| 1179 | void GvsStatistics::Print(ostream &app) const
|
---|
| 1180 | {
|
---|
[2003] | 1181 | app << "#ViewCells\n" << mViewCells << endl;
|
---|
[2648] | 1182 | app << "#Id\n" << mViewCellId << endl;
|
---|
| 1183 | app << "#Time\n" << mTimePerViewCell << endl;;
|
---|
[2690] | 1184 | app << "#TriPvs\n" << mTrianglePvs << endl;
|
---|
[2648] | 1185 | app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
|
---|
[2687] | 1186 | app << "#UpdatedPvs\n" << (int)mPvsCost << endl;
|
---|
[2003] | 1187 |
|
---|
[2677] | 1188 | app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
|
---|
| 1189 | app << "#RaysPerSec\n" << RaysPerSec() << endl;
|
---|
| 1190 |
|
---|
[2003] | 1191 | app << "#TotalPvs\n" << mTotalPvs << endl;
|
---|
[2648] | 1192 | app << "#TotalTime\n" << mTotalTime << endl;
|
---|
[1934] | 1193 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
[2677] | 1194 |
|
---|
[2648] | 1195 | app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
|
---|
| 1196 |
|
---|
[2677] | 1197 | if (0)
|
---|
| 1198 | {
|
---|
| 1199 | app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
| 1200 | app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
[2648] | 1201 |
|
---|
[2677] | 1202 | app << "#Pass\n" << mPass << endl;
|
---|
| 1203 | app << "#ScDiff\n" << mPassContribution << endl;
|
---|
| 1204 | app << "#GvsRuns\n" << mGvsPass << endl;
|
---|
[2003] | 1205 |
|
---|
[2677] | 1206 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
| 1207 | }
|
---|
[2003] | 1208 |
|
---|
| 1209 | app << endl;
|
---|
[1883] | 1210 | }
|
---|
[1934] | 1211 |
|
---|
| 1212 |
|
---|
[2690] | 1213 | void GvsPreprocessor::ComputeViewCell()
|
---|
[2625] | 1214 | {
|
---|
[2691] | 1215 | mCurrentViewCell->GetMesh()->ComputeBoundingBox();
|
---|
| 1216 |
|
---|
[2643] | 1217 | KdNode::NewMail();
|
---|
[2625] | 1218 |
|
---|
[2670] | 1219 | const long startTime = GetTime();
|
---|
| 1220 |
|
---|
[2625] | 1221 | cout << "\n***********************\n"
|
---|
[2643] | 1222 | << "computing view cell " << mProcessedViewCells
|
---|
[2625] | 1223 | << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
|
---|
[2691] | 1224 | cout << "bb: " << mCurrentViewCell->GetBox() << endl;
|
---|
[2625] | 1225 |
|
---|
[2690] | 1226 | mGvsStats.mPerViewCellSamples = 0;
|
---|
| 1227 |
|
---|
| 1228 | int oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1229 | int passSamples = 0;
|
---|
| 1230 |
|
---|
[2691] | 1231 | for (int i = 0; i < 0; ++ i)
|
---|
| 1232 | mGenericStats[i] = 0;
|
---|
[2690] | 1233 |
|
---|
| 1234 | while (1)
|
---|
| 1235 | {
|
---|
[2691] | 1236 | int oldPvsSize = mCurrentViewCell->GetPvs().GetSize();
|
---|
[2690] | 1237 |
|
---|
| 1238 | mRayCaster->InitPass();
|
---|
| 1239 |
|
---|
| 1240 | // Ray queue empty =>
|
---|
| 1241 | // cast a number of uniform samples to fill ray queue
|
---|
[2691] | 1242 | int newSamples = CastInitialSamples(mInitialSamples);
|
---|
[2690] | 1243 |
|
---|
| 1244 | if (!mOnlyRandomSampling)
|
---|
| 1245 | {
|
---|
| 1246 | int samplesPerRun = ProcessQueue();
|
---|
| 1247 | newSamples += samplesPerRun;
|
---|
| 1248 | //if (samplesPerRun > 0) cout << "spr: " << samplesPerRun << " ";
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
| 1251 | passSamples += newSamples;
|
---|
| 1252 | mGvsStats.mPerViewCellSamples += newSamples;
|
---|
| 1253 |
|
---|
| 1254 | if (passSamples >= mGvsSamplesPerPass)
|
---|
| 1255 | {
|
---|
| 1256 | if (GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
|
---|
| 1257 |
|
---|
[2691] | 1258 | //const bool convertPerPass = true;
|
---|
[2690] | 1259 | const bool convertPerPass = false;
|
---|
| 1260 |
|
---|
| 1261 | if (convertPerPass)
|
---|
| 1262 | {
|
---|
| 1263 | int newObjects = ConvertObjectPvs();
|
---|
| 1264 |
|
---|
| 1265 | cout << "added " << newObjects << " triangles to triangle pvs" << endl;
|
---|
| 1266 | mGvsStats.mTotalContribution += newObjects;
|
---|
| 1267 | }
|
---|
| 1268 |
|
---|
| 1269 | ++ mPass;
|
---|
| 1270 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
| 1271 |
|
---|
| 1272 |
|
---|
| 1273 | ////////
|
---|
| 1274 | //-- stats
|
---|
| 1275 |
|
---|
| 1276 | mGvsStats.mPass = mPass;
|
---|
| 1277 |
|
---|
| 1278 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
|
---|
| 1279 | cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
|
---|
| 1280 | cout << "obj contri=" << mCurrentViewCell->GetPvs().GetSize() - oldPvsSize << " (of " << mMinContribution << ")" << endl;
|
---|
| 1281 |
|
---|
| 1282 | // termination criterium
|
---|
| 1283 | if (mGvsStats.mPassContribution < mMinContribution)
|
---|
| 1284 | break;
|
---|
| 1285 |
|
---|
| 1286 | // reset
|
---|
| 1287 | oldContribution = mGvsStats.mTotalContribution;
|
---|
| 1288 | mGvsStats.mPassContribution = 0;
|
---|
| 1289 | passSamples = 0;
|
---|
| 1290 | }
|
---|
| 1291 | }
|
---|
[2677] | 1292 |
|
---|
[2691] | 1293 | // at last compute objects that directly intersect view cell
|
---|
| 1294 | IntersectWithViewCell();
|
---|
[2690] | 1295 |
|
---|
| 1296 |
|
---|
[2677] | 1297 | ////////
|
---|
| 1298 | //-- stats
|
---|
[2625] | 1299 |
|
---|
[2690] | 1300 | // timing
|
---|
| 1301 | mGvsStats.mTimePerViewCell = TimeDiff(startTime, GetTime()) * 1e-3f;
|
---|
| 1302 |
|
---|
| 1303 | ComputeStats();
|
---|
| 1304 |
|
---|
| 1305 | // clean up
|
---|
| 1306 | mTrianglePvs.clear();
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 |
|
---|
| 1310 | void GvsPreprocessor::ComputeStats()
|
---|
| 1311 | {
|
---|
[2677] | 1312 | // compute pvs size using larger (bvh objects)
|
---|
| 1313 | // note: for kd pvs we had to do this during gvs computation
|
---|
| 1314 |
|
---|
[2625] | 1315 | if (!mUseKdPvs)
|
---|
| 1316 | {
|
---|
| 1317 | ObjectContainer objectPvs;
|
---|
| 1318 |
|
---|
| 1319 | // optain object pvs
|
---|
| 1320 | GetObjectPvs(objectPvs);
|
---|
| 1321 |
|
---|
| 1322 | // add pvs
|
---|
| 1323 | ObjectContainer::const_iterator it, it_end = objectPvs.end();
|
---|
| 1324 |
|
---|
| 1325 | for (it = objectPvs.begin(); it != it_end; ++ it)
|
---|
| 1326 | {
|
---|
| 1327 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
| 1328 | }
|
---|
| 1329 |
|
---|
| 1330 | cout << "triangle pvs of " << (int)mTrianglePvs.size()
|
---|
| 1331 | << " was converted to object pvs of " << (int)objectPvs.size() << endl;
|
---|
| 1332 |
|
---|
| 1333 | mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
|
---|
[2677] | 1334 | mGvsStats.mPvsCost = 0; // todo objectPvs.EvalPvsCost();
|
---|
[2625] | 1335 | }
|
---|
[2691] | 1336 | else if (0)
|
---|
[2625] | 1337 | {
|
---|
[2691] | 1338 | //KdNode::NewMail();
|
---|
| 1339 |
|
---|
| 1340 | // compute pvs kd nodes that intersect view cell
|
---|
| 1341 | ObjectContainer mykdobjects;
|
---|
| 1342 |
|
---|
| 1343 | // extract kd pvs
|
---|
| 1344 | ObjectContainer::const_iterator it, it_end = mTrianglePvs.end();
|
---|
| 1345 |
|
---|
| 1346 | for (it = mTrianglePvs.begin(); it != it_end; ++ it)
|
---|
| 1347 | {
|
---|
| 1348 | Intersectable *obj = *it;
|
---|
| 1349 | // find intersecting objects not yet in pvs (i.e., only unmailed)
|
---|
| 1350 | mKdTree->CollectKdObjects(obj->GetBox(), mykdobjects);
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
| 1353 | // add pvs
|
---|
| 1354 | it_end = mykdobjects.end();
|
---|
| 1355 |
|
---|
| 1356 | for (it = mykdobjects.begin(); it != it_end; ++ it)
|
---|
| 1357 | {
|
---|
| 1358 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
| 1359 | }
|
---|
| 1360 |
|
---|
| 1361 | cout << "added " << (int)mykdobjects.size() << " new objects " << endl;
|
---|
| 1362 |
|
---|
[2643] | 1363 | mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
|
---|
[2648] | 1364 | mGvsStats.mPvsCost = mCurrentViewCell->GetPvs().EvalPvsCost();
|
---|
[2643] | 1365 |
|
---|
[2625] | 1366 | }
|
---|
| 1367 |
|
---|
[2677] | 1368 | mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
|
---|
[2625] | 1369 |
|
---|
| 1370 |
|
---|
[2677] | 1371 | ////////////////
|
---|
| 1372 | // global values
|
---|
| 1373 |
|
---|
[2690] | 1374 | mGvsStats.mViewCells = mProcessedViewCells;
|
---|
[2677] | 1375 | mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
|
---|
| 1376 | mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
|
---|
| 1377 | mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
|
---|
[2625] | 1378 | mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
|
---|
| 1379 |
|
---|
| 1380 | mGvsStats.Stop();
|
---|
| 1381 | mGvsStats.Print(mGvsStatsStream);
|
---|
| 1382 |
|
---|
[2677] | 1383 | cout << "id: " << mCurrentViewCell->GetId() << " pvs cost: "
|
---|
[2690] | 1384 | << mGvsStats.mPvsCost << " pvs tri: " << mTrianglePvs.size() << endl;
|
---|
| 1385 | }
|
---|
[2677] | 1386 |
|
---|
| 1387 |
|
---|
[2690] | 1388 | ObjectContainer triangles;
|
---|
| 1389 |
|
---|
| 1390 | int GvsPreprocessor::ConvertObjectPvs()
|
---|
| 1391 | {
|
---|
| 1392 | int newObjects = 0;
|
---|
| 1393 |
|
---|
| 1394 | ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
|
---|
| 1395 |
|
---|
| 1396 | triangles.clear();
|
---|
| 1397 |
|
---|
| 1398 | // output PVS of view cell
|
---|
| 1399 | while (pit.HasMoreEntries())
|
---|
| 1400 | {
|
---|
| 1401 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(pit.Next());
|
---|
| 1402 |
|
---|
| 1403 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), triangles);
|
---|
| 1404 |
|
---|
| 1405 | ObjectContainer::const_iterator oit, oit_end = triangles.end();
|
---|
| 1406 |
|
---|
| 1407 | for (oit = triangles.begin(); oit != oit_end; ++ oit)
|
---|
| 1408 | {
|
---|
| 1409 | if (AddTriangleObject(*oit))
|
---|
| 1410 | ++ newObjects;
|
---|
| 1411 | }
|
---|
| 1412 | }
|
---|
| 1413 |
|
---|
| 1414 | return newObjects;
|
---|
[1934] | 1415 | }
|
---|
[2625] | 1416 |
|
---|
[2690] | 1417 |
|
---|
| 1418 | bool GvsPreprocessor::AddTriangleObject(Intersectable *triObj)
|
---|
| 1419 | {
|
---|
| 1420 | if ((triObj->mCounter < ACCOUNTED_OBJECT))
|
---|
| 1421 | {
|
---|
| 1422 | triObj->mCounter += ACCOUNTED_OBJECT;
|
---|
| 1423 |
|
---|
| 1424 | mTrianglePvs.push_back(triObj);
|
---|
[2691] | 1425 | mGenericStats[0] = (int)mTrianglePvs.size();
|
---|
[2690] | 1426 | return true;
|
---|
| 1427 | }
|
---|
| 1428 |
|
---|
| 1429 | return false;
|
---|
[2625] | 1430 | }
|
---|
[2690] | 1431 |
|
---|
[2691] | 1432 |
|
---|
| 1433 |
|
---|
[2690] | 1434 | }
|
---|