[1020] | 1 | #include "SamplingStrategy.h"
|
---|
| 2 | #include "Ray.h"
|
---|
| 3 | #include "Intersectable.h"
|
---|
| 4 | #include "Preprocessor.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
| 6 | #include "AxisAlignedBox3.h"
|
---|
[1891] | 7 | #include "RssTree.h"
|
---|
[1989] | 8 | #include "Mutation.h"
|
---|
[2227] | 9 | #include "Timer/PerfTimer.h"
|
---|
[2548] | 10 | #include "DifferenceSampling.h"
|
---|
[1020] | 11 |
|
---|
[2069] | 12 |
|
---|
[2548] | 13 |
|
---|
[1020] | 14 | namespace GtpVisibilityPreprocessor {
|
---|
| 15 |
|
---|
[2076] | 16 | extern PerfTimer haltonTimer;
|
---|
[2044] | 17 | extern PerfTimer pvsTimer;
|
---|
| 18 | extern PerfTimer viewCellCastTimer;
|
---|
[2015] | 19 |
|
---|
[1901] | 20 | //HaltonSequence SamplingStrategy::sHalton;
|
---|
| 21 |
|
---|
[1899] | 22 | HaltonSequence ObjectBasedDistribution::sHalton;
|
---|
| 23 | HaltonSequence MixtureDistribution::sHalton;
|
---|
| 24 | HaltonSequence GlobalLinesDistribution::sHalton;
|
---|
| 25 | HaltonSequence SpatialBoxBasedDistribution::sHalton;
|
---|
| 26 | HaltonSequence ObjectDirectionBasedDistribution::sHalton;
|
---|
[1952] | 27 | HaltonSequence DirectionBasedDistribution::sHalton;
|
---|
[1968] | 28 | HaltonSequence HwGlobalLinesDistribution::sHalton;
|
---|
[1899] | 29 |
|
---|
[1996] | 30 | HaltonSequence ViewCellBasedDistribution::sHalton;
|
---|
[1899] | 31 |
|
---|
[1884] | 32 | SamplingStrategy::SamplingStrategy(Preprocessor &preprocessor):
|
---|
[1968] | 33 | mPreprocessor(preprocessor),
|
---|
| 34 | mRatio(1.0f),
|
---|
| 35 | mTotalRays(0),
|
---|
| 36 | mTotalContribution(0.0f)
|
---|
| 37 | {
|
---|
[1520] | 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | SamplingStrategy::~SamplingStrategy()
|
---|
| 42 | {
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1771] | 45 | int
|
---|
| 46 | SamplingStrategy::GenerateSamples(const int number,
|
---|
[1867] | 47 | SimpleRayContainer &rays)
|
---|
[1771] | 48 | {
|
---|
[1772] | 49 | SimpleRay ray;
|
---|
| 50 | int samples = 0;
|
---|
| 51 | int i = 0;
|
---|
| 52 | const int maxTries = 20;
|
---|
| 53 | // tmp changed matt. Q: should one rejected sample
|
---|
| 54 | // terminate the whole method?
|
---|
[1989] | 55 | for (; i < number; i++)
|
---|
[2187] | 56 | {
|
---|
[1989] | 57 | int j = 0;
|
---|
| 58 | bool sampleGenerated = false;
|
---|
[2187] | 59 |
|
---|
[1989] | 60 | for (j = 0; !sampleGenerated && (j < maxTries); ++ j)
|
---|
[2187] | 61 | {
|
---|
[1989] | 62 | sampleGenerated = GenerateSample(ray);
|
---|
[2187] | 63 |
|
---|
[1989] | 64 | if (sampleGenerated)
|
---|
[2187] | 65 | {
|
---|
[1989] | 66 | ++ samples;
|
---|
| 67 | rays.push_back(ray);
|
---|
[2187] | 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[1520] | 71 |
|
---|
[1772] | 72 | return samples;
|
---|
[1771] | 73 | }
|
---|
[1520] | 74 |
|
---|
[1771] | 75 |
|
---|
[1020] | 76 | /*********************************************************************/
|
---|
| 77 | /* Individual sampling strategies implementation */
|
---|
| 78 | /*********************************************************************/
|
---|
| 79 |
|
---|
| 80 |
|
---|
[1867] | 81 | bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1020] | 82 | {
|
---|
[1883] | 83 | Vector3 origin, direction;
|
---|
| 84 |
|
---|
| 85 | float r[5];
|
---|
[1898] | 86 | sHalton.GetNext(5, r);
|
---|
[1883] | 87 |
|
---|
| 88 | mPreprocessor.mViewCellsManager->GetViewPoint(origin,
|
---|
| 89 | Vector3(r[2],r[3],r[4]));
|
---|
| 90 |
|
---|
[1020] | 91 |
|
---|
[1883] | 92 | Vector3 point, normal;
|
---|
| 93 |
|
---|
[1898] | 94 | r[0] *= (float)mPreprocessor.mObjects.size() - 1;
|
---|
[1883] | 95 | const int i = (int)r[0];
|
---|
| 96 |
|
---|
| 97 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 98 |
|
---|
| 99 | // take the remainder as a parameter over objects surface
|
---|
| 100 | r[0] -= (float)i;
|
---|
| 101 |
|
---|
| 102 | object->GetRandomSurfacePoint(r[0], r[1], point, normal);
|
---|
| 103 |
|
---|
| 104 | direction = point - origin;
|
---|
| 105 |
|
---|
| 106 | const float c = Magnitude(direction);
|
---|
| 107 |
|
---|
| 108 | if (c <= Limits::Small)
|
---|
| 109 | return false;
|
---|
| 110 |
|
---|
| 111 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 112 | const float pdf = 1.0f;
|
---|
| 113 |
|
---|
| 114 | direction *= 1.0f / c;
|
---|
| 115 | ray = SimpleRay(origin, direction, OBJECT_BASED_DISTRIBUTION, pdf);
|
---|
| 116 |
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
[1020] | 119 |
|
---|
| 120 |
|
---|
[1883] | 121 | bool
|
---|
| 122 | ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
| 123 | {
|
---|
| 124 | Vector3 origin, direction;
|
---|
[1877] | 125 |
|
---|
[1020] | 126 |
|
---|
[1883] | 127 | float r[4];
|
---|
[1898] | 128 | sHalton.GetNext(4, r);
|
---|
[1877] | 129 |
|
---|
[2076] | 130 | // static Halton<4> halton;
|
---|
| 131 | // halton.GetNext(r);
|
---|
| 132 |
|
---|
[1901] | 133 | r[0] *= (float)mPreprocessor.mObjects.size() - 1;
|
---|
[1883] | 134 | const int i = (int)r[0];
|
---|
| 135 |
|
---|
| 136 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 137 |
|
---|
| 138 | // take the remainder as a parameter over objects surface
|
---|
| 139 | r[0] -= (float)i;
|
---|
[1020] | 140 |
|
---|
[1883] | 141 | Vector3 normal;
|
---|
[1020] | 142 |
|
---|
[1883] | 143 | object->GetRandomSurfacePoint(r[0], r[1], origin, normal);
|
---|
| 144 |
|
---|
| 145 | direction = Normalize(CosineRandomVector(r[2], r[3], normal));
|
---|
| 146 |
|
---|
[1899] | 147 | origin += 1e-2f*direction;
|
---|
[1883] | 148 |
|
---|
| 149 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 150 | const float pdf = 1.0f;
|
---|
| 151 |
|
---|
| 152 | ray = SimpleRay(origin, direction, OBJECT_DIRECTION_BASED_DISTRIBUTION, pdf);
|
---|
| 153 |
|
---|
| 154 | return true;
|
---|
[1020] | 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
[1867] | 158 | bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1278] | 159 | {
|
---|
[1020] | 160 |
|
---|
[1952] | 161 | float r[5];
|
---|
| 162 | sHalton.GetNext(5, r);
|
---|
[1020] | 163 |
|
---|
[1952] | 164 | Vector3 origin, direction;
|
---|
| 165 | mPreprocessor.mViewCellsManager->GetViewPoint(origin,
|
---|
| 166 | Vector3(r[2],r[3],r[4])
|
---|
| 167 | );
|
---|
| 168 |
|
---|
| 169 | direction = UniformRandomVector(r[0], r[1]);
|
---|
| 170 | const float c = Magnitude(direction);
|
---|
| 171 |
|
---|
| 172 | if (c <= Limits::Small)
|
---|
| 173 | return false;
|
---|
| 174 |
|
---|
| 175 | const float pdf = 1.0f;
|
---|
| 176 |
|
---|
| 177 | direction *= 1.0f / c;
|
---|
| 178 | ray = SimpleRay(origin, direction, DIRECTION_BASED_DISTRIBUTION, pdf);
|
---|
| 179 |
|
---|
| 180 | return true;
|
---|
[1020] | 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
[1867] | 184 | bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1020] | 185 | {
|
---|
| 186 | Vector3 origin, direction;
|
---|
| 187 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 188 |
|
---|
[1297] | 189 | const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
|
---|
| 190 | const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
|
---|
[1020] | 191 |
|
---|
| 192 | direction = VssRay::GetDirection(alpha, beta);
|
---|
| 193 |
|
---|
| 194 | const float c = Magnitude(direction);
|
---|
| 195 |
|
---|
| 196 | if (c <= Limits::Small)
|
---|
| 197 | return false;
|
---|
| 198 |
|
---|
| 199 | const float pdf = 1.0f;
|
---|
| 200 |
|
---|
| 201 | direction *= 1.0f / c;
|
---|
[1883] | 202 | ray = SimpleRay(origin, direction, DIRECTION_BOX_BASED_DISTRIBUTION, pdf);
|
---|
[1020] | 203 |
|
---|
| 204 | return true;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[1867] | 208 | bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1020] | 209 | {
|
---|
[2071] | 210 |
|
---|
[2072] | 211 | Vector3 origin, direction;
|
---|
[1278] | 212 |
|
---|
[2072] | 213 | float r[6];
|
---|
| 214 | sHalton.GetNext(6, r);
|
---|
[2199] | 215 |
|
---|
[2076] | 216 | mPreprocessor.mViewCellsManager->GetViewPoint(origin, Vector3(r[0],
|
---|
| 217 | r[1],
|
---|
| 218 | r[2]));
|
---|
[2072] | 219 |
|
---|
| 220 | direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint(Vector3(r[3],
|
---|
| 221 | r[4],
|
---|
| 222 | r[5])
|
---|
| 223 | ) - origin;
|
---|
[2199] | 224 |
|
---|
[2072] | 225 | const float c = Magnitude(direction);
|
---|
| 226 |
|
---|
| 227 | if (c <= Limits::Small)
|
---|
| 228 | return false;
|
---|
| 229 |
|
---|
| 230 | const float pdf = 1.0f;
|
---|
| 231 |
|
---|
| 232 | direction *= 1.0f / c;
|
---|
| 233 | ray = SimpleRay(origin, direction, SPATIAL_BOX_BASED_DISTRIBUTION, pdf);
|
---|
[1867] | 234 |
|
---|
[2072] | 235 | return true;
|
---|
[1020] | 236 | }
|
---|
| 237 |
|
---|
[1695] | 238 |
|
---|
[1867] | 239 | bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1695] | 240 | {
|
---|
| 241 | Vector3 origin, direction;
|
---|
| 242 |
|
---|
| 243 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 244 |
|
---|
| 245 | Vector3 point;
|
---|
| 246 | Vector3 normal;
|
---|
[1999] | 247 |
|
---|
[1695] | 248 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 249 |
|
---|
| 250 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 251 |
|
---|
| 252 | object->GetRandomSurfacePoint(point, normal);
|
---|
[1765] | 253 |
|
---|
[1695] | 254 | direction = origin - point;
|
---|
[1765] | 255 |
|
---|
[1695] | 256 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 257 | const float c = Magnitude(direction);
|
---|
[1765] | 258 |
|
---|
| 259 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
| 260 | {
|
---|
[1695] | 261 | return false;
|
---|
[1765] | 262 | }
|
---|
[1695] | 263 |
|
---|
| 264 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 265 | const float pdf = 1.0f;
|
---|
[2072] | 266 |
|
---|
[1695] | 267 | direction *= 1.0f / c;
|
---|
[1765] | 268 | // a little offset
|
---|
[1772] | 269 | point += direction * 0.001f;
|
---|
[1765] | 270 |
|
---|
[1883] | 271 | ray = SimpleRay(point, direction, REVERSE_OBJECT_BASED_DISTRIBUTION, pdf);
|
---|
[1695] | 272 |
|
---|
| 273 | return true;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[1763] | 276 |
|
---|
[1867] | 277 | bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1763] | 278 | {
|
---|
| 279 | Vector3 origin, direction;
|
---|
| 280 |
|
---|
| 281 | ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
|
---|
| 282 |
|
---|
| 283 | Vector3 point;
|
---|
| 284 | Vector3 normal, normal2;
|
---|
| 285 |
|
---|
| 286 | const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
|
---|
| 287 | const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 288 |
|
---|
| 289 | Intersectable *object = mPreprocessor.mObjects[objIdx];
|
---|
| 290 | ViewCell *viewCell = viewCells[vcIdx];
|
---|
| 291 |
|
---|
| 292 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 293 | viewCell->GetRandomEdgePoint(origin, normal2);
|
---|
| 294 |
|
---|
| 295 | direction = point - origin;
|
---|
| 296 |
|
---|
| 297 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 298 | const float c = Magnitude(direction);
|
---|
| 299 |
|
---|
[1769] | 300 | if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
|
---|
[1768] | 301 | {
|
---|
[1763] | 302 | return false;
|
---|
[1768] | 303 | }
|
---|
[1763] | 304 |
|
---|
| 305 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 306 | const float pdf = 1.0f;
|
---|
| 307 | //cout << "p: " << point << " ";
|
---|
| 308 | direction *= 1.0f / c;
|
---|
[1883] | 309 | ray = SimpleRay(origin, direction, VIEWCELL_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
[1769] | 310 |
|
---|
| 311 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
| 312 |
|
---|
[1763] | 313 | return true;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[1765] | 316 |
|
---|
[1867] | 317 | bool ReverseViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1772] | 318 | {
|
---|
| 319 | Vector3 origin, direction;
|
---|
| 320 |
|
---|
| 321 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
| 322 |
|
---|
| 323 | Vector3 point;
|
---|
| 324 | Vector3 normal;
|
---|
[1999] | 325 |
|
---|
[1772] | 326 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 327 |
|
---|
| 328 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 329 |
|
---|
| 330 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 331 |
|
---|
| 332 | direction = origin - point;
|
---|
| 333 |
|
---|
| 334 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 335 | const float c = Magnitude(direction);
|
---|
| 336 |
|
---|
| 337 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
| 338 | {
|
---|
| 339 | return false;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 343 | const float pdf = 1.0f;
|
---|
| 344 | //cout << "p: " << point << " ";
|
---|
| 345 | direction *= 1.0f / c;
|
---|
| 346 | // a little offset
|
---|
| 347 | point += direction * 0.001f;
|
---|
| 348 |
|
---|
[1883] | 349 | ray = SimpleRay(point, direction, REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
[1772] | 350 |
|
---|
| 351 | return true;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 |
|
---|
[1867] | 355 | bool ViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1772] | 356 | {
|
---|
| 357 | Vector3 origin, direction;
|
---|
| 358 |
|
---|
| 359 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
| 360 |
|
---|
| 361 | Vector3 point;
|
---|
| 362 | Vector3 normal;
|
---|
[2066] | 363 |
|
---|
[1772] | 364 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 365 |
|
---|
| 366 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 367 |
|
---|
| 368 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 369 | direction = point - origin;
|
---|
| 370 |
|
---|
| 371 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 372 | const float c = Magnitude(direction);
|
---|
| 373 |
|
---|
| 374 | if (c <= Limits::Small)
|
---|
| 375 | return false;
|
---|
| 376 |
|
---|
| 377 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 378 | const float pdf = 1.0f;
|
---|
| 379 |
|
---|
| 380 | direction *= 1.0f / c;
|
---|
| 381 |
|
---|
| 382 | // a little offset
|
---|
| 383 | origin += direction * 0.001f;
|
---|
| 384 |
|
---|
[1883] | 385 | ray = SimpleRay(origin, direction, VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
[1772] | 386 |
|
---|
| 387 | return true;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 |
|
---|
[1824] | 391 | bool
|
---|
[1867] | 392 | GlobalLinesDistribution::GenerateSample(SimpleRay &ray)
|
---|
[1824] | 393 | {
|
---|
[1867] | 394 | Vector3 origin, termination, direction;
|
---|
[1824] | 395 |
|
---|
[1867] | 396 | float radius = 0.5f*Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size());
|
---|
| 397 | Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center();
|
---|
| 398 |
|
---|
| 399 | const int tries = 1000;
|
---|
| 400 | int i;
|
---|
| 401 | for (i=0; i < tries; i++) {
|
---|
| 402 | float r[4];
|
---|
[1898] | 403 | sHalton.GetNext(4, r);
|
---|
[1824] | 404 |
|
---|
[1867] | 405 | origin = center + (radius*UniformRandomVector(r[0], r[1]));
|
---|
| 406 | termination = center + (radius*UniformRandomVector(r[2], r[3]));
|
---|
| 407 |
|
---|
[1824] | 408 | direction = termination - origin;
|
---|
[1867] | 409 |
|
---|
| 410 |
|
---|
[1824] | 411 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 412 | const float c = Magnitude(direction);
|
---|
| 413 | if (c <= Limits::Small)
|
---|
[1867] | 414 | return false;
|
---|
[1824] | 415 |
|
---|
| 416 | direction *= 1.0f / c;
|
---|
[1867] | 417 |
|
---|
| 418 | // check if the ray intersects the view space box
|
---|
| 419 | static Ray ray;
|
---|
| 420 | ray.Init(origin, direction, Ray::LOCAL_RAY);
|
---|
[1824] | 421 |
|
---|
[1867] | 422 | float tmin, tmax;
|
---|
| 423 | if (mPreprocessor.mViewCellsManager->
|
---|
| 424 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
| 425 | break;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | if (i!=tries) {
|
---|
[1824] | 429 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 430 | const float pdf = 1.0f;
|
---|
[1867] | 431 |
|
---|
| 432 |
|
---|
[1883] | 433 | ray = SimpleRay(origin, direction, GLOBAL_LINES_DISTRIBUTION, pdf);
|
---|
[1824] | 434 | ray.mType = Ray::GLOBAL_RAY;
|
---|
| 435 | return true;
|
---|
[1867] | 436 | }
|
---|
| 437 |
|
---|
| 438 | return false;
|
---|
[1771] | 439 | }
|
---|
| 440 |
|
---|
[1883] | 441 |
|
---|
[2210] | 442 | /******************************************************************/
|
---|
| 443 | /* class MixtureDistribution implementation */
|
---|
| 444 | /******************************************************************/
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | // has to be called before first usage
|
---|
[1883] | 448 | void
|
---|
| 449 | MixtureDistribution::Init()
|
---|
| 450 | {
|
---|
[1884] | 451 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
| 452 | // small non-zero value
|
---|
| 453 | mDistributions[i]->mRays = 1;
|
---|
[1966] | 454 | mDistributions[i]->mGeneratedRays = 1;
|
---|
[1884] | 455 | // unit contribution per ray
|
---|
| 456 | if (1 || mDistributions[i]->mType != RSS_BASED_DISTRIBUTION)
|
---|
| 457 | mDistributions[i]->mContribution = 1.0f;
|
---|
| 458 | else
|
---|
| 459 | mDistributions[i]->mContribution = 0.0f;
|
---|
| 460 | }
|
---|
| 461 | UpdateRatios();
|
---|
[1824] | 462 | }
|
---|
[1884] | 463 |
|
---|
| 464 | void
|
---|
| 465 | MixtureDistribution::Reset()
|
---|
| 466 | {
|
---|
| 467 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
| 468 | // small non-zero value
|
---|
[1891] | 469 | mDistributions[i]->mTotalRays = 0;
|
---|
[1884] | 470 | // unit contribution per ray
|
---|
[1891] | 471 | mDistributions[i]->mTotalContribution = 0.0f;
|
---|
[1884] | 472 | }
|
---|
| 473 | UpdateRatios();
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | // Generate a new sample according to a mixture distribution
|
---|
[1883] | 477 | bool
|
---|
| 478 | MixtureDistribution::GenerateSample(SimpleRay &ray)
|
---|
| 479 | {
|
---|
[1884] | 480 | float r;
|
---|
[2076] | 481 |
|
---|
[1898] | 482 | sHalton.GetNext(1, &r);
|
---|
[2076] | 483 | //static Halton<1> halton;
|
---|
| 484 | // halton.GetNext(&r);
|
---|
| 485 |
|
---|
[1900] | 486 | int i;
|
---|
[1884] | 487 | // pickup a distribution
|
---|
[2199] | 488 | for (i=0; i < (int)mDistributions.size()-1; i++)
|
---|
[1884] | 489 | if (r < mDistributions[i]->mRatio)
|
---|
| 490 | break;
|
---|
[2199] | 491 |
|
---|
[1966] | 492 | bool result = mDistributions[i]->GenerateSample(ray);
|
---|
| 493 |
|
---|
| 494 | if (result)
|
---|
| 495 | mDistributions[i]->mGeneratedRays++;
|
---|
| 496 |
|
---|
| 497 | return result;
|
---|
[1883] | 498 | }
|
---|
[1824] | 499 |
|
---|
[2353] | 500 |
|
---|
| 501 | #if TEST_PACKETS
|
---|
| 502 |
|
---|
| 503 | // add contributions of the sample to the strategies
|
---|
| 504 | void MixtureDistribution::ComputeContributions(VssRayContainer &vssRays)
|
---|
| 505 | {
|
---|
| 506 | int i;
|
---|
| 507 |
|
---|
| 508 | mPreprocessor.mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
| 509 |
|
---|
| 510 | VssRayContainer::iterator it = vssRays.begin();
|
---|
| 511 |
|
---|
| 512 | for (i=0; i < mDistributions.size(); i++) {
|
---|
| 513 | mDistributions[i]->mContribution = 0;
|
---|
| 514 | mDistributions[i]->mRays = 0;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | for(; it != vssRays.end(); ++ it)
|
---|
| 518 | {
|
---|
| 519 | VssRay *ray = *it;
|
---|
| 520 |
|
---|
| 521 | for (i = 0; i < mDistributions.size() - 1; ++ i)
|
---|
| 522 | {
|
---|
| 523 | if (mDistributions[i]->mType == ray->mDistribution)
|
---|
| 524 | break;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | float contribution =
|
---|
| 528 | mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false);
|
---|
| 529 |
|
---|
| 530 | mDistributions[i]->mContribution += contribution;
|
---|
| 531 | mDistributions[i]->mRays ++;
|
---|
| 532 |
|
---|
| 533 | mDistributions[i]->mTotalContribution += contribution;
|
---|
| 534 | mDistributions[i]->mTotalRays ++;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | UpdateRatios();
|
---|
| 538 |
|
---|
| 539 |
|
---|
| 540 | const float vcTime = viewCellCastTimer.TotalTime();
|
---|
| 541 | const float pvsTime = pvsTimer.TotalTime();
|
---|
| 542 | const float haltonTime = haltonTimer.TotalTime();
|
---|
| 543 |
|
---|
| 544 | cout << "view cell cast time: " << vcTime << " s" << endl;
|
---|
| 545 | cout << "pvs time: " << pvsTime << " s" << endl;
|
---|
| 546 | cout << "halton time: "<< haltonTime << " s" << endl;
|
---|
| 547 |
|
---|
| 548 | Debug << "view cell cast time: " << vcTime << " s" << endl;
|
---|
| 549 | Debug << "pvs time: " << pvsTime << " s" << endl;
|
---|
| 550 | Debug << "halton time: "<< haltonTime << " s" << endl;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 | #else
|
---|
| 555 |
|
---|
| 556 | // add contributions of the sample to the strategies
|
---|
[1883] | 557 | void
|
---|
[1884] | 558 | MixtureDistribution::ComputeContributions(VssRayContainer &vssRays)
|
---|
[1883] | 559 | {
|
---|
[2353] | 560 | int i;
|
---|
[1883] | 561 |
|
---|
[2353] | 562 | VssRayContainer::iterator it = vssRays.begin();
|
---|
[1891] | 563 |
|
---|
[2353] | 564 | for (i=0; i < mDistributions.size(); i++) {
|
---|
| 565 | mDistributions[i]->mContribution = 0;
|
---|
| 566 | mDistributions[i]->mRays = 0;
|
---|
[1891] | 567 | }
|
---|
[1931] | 568 |
|
---|
[2353] | 569 | for(; it != vssRays.end(); ++it) {
|
---|
| 570 | VssRay *ray = *it;
|
---|
| 571 | for (i=0; i < mDistributions.size()-1; i++) {
|
---|
| 572 | if (mDistributions[i]->mType == ray->mDistribution)
|
---|
| 573 | break;
|
---|
| 574 | }
|
---|
[1884] | 575 |
|
---|
[2353] | 576 | float contribution =
|
---|
| 577 | mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray,
|
---|
| 578 | true,
|
---|
| 579 | false);
|
---|
[2044] | 580 |
|
---|
[2353] | 581 | mDistributions[i]->mContribution += contribution;
|
---|
| 582 | mDistributions[i]->mRays ++;
|
---|
[1966] | 583 |
|
---|
[2353] | 584 | mDistributions[i]->mTotalContribution += contribution;
|
---|
| 585 | mDistributions[i]->mTotalRays ++;
|
---|
| 586 | }
|
---|
[2116] | 587 |
|
---|
[2353] | 588 | UpdateRatios();
|
---|
[2116] | 589 |
|
---|
[2353] | 590 |
|
---|
| 591 | if (1)
|
---|
| 592 | {
|
---|
| 593 | const float vcTime = viewCellCastTimer.TotalTime();
|
---|
| 594 | const float pvsTime = pvsTimer.TotalTime();
|
---|
| 595 | const float haltonTime = haltonTimer.TotalTime();
|
---|
| 596 |
|
---|
| 597 | cout << "view cell cast time: " << vcTime << " s" << endl;
|
---|
| 598 | cout << "pvs time: " << pvsTime << " s" << endl;
|
---|
| 599 | cout << "halton time: "<< haltonTime << " s" << endl;
|
---|
| 600 |
|
---|
| 601 | Debug << "view cell cast time: " << vcTime << " s" << endl;
|
---|
| 602 | Debug << "pvs time: " << pvsTime << " s" << endl;
|
---|
| 603 | Debug << "halton time: "<< haltonTime << " s" << endl;
|
---|
| 604 | }
|
---|
[1883] | 605 | }
|
---|
| 606 |
|
---|
[2353] | 607 | #endif
|
---|
| 608 |
|
---|
| 609 |
|
---|
[1884] | 610 | void
|
---|
[1942] | 611 | MixtureDistribution::UpdateDistributions(VssRayContainer &vssRays)
|
---|
| 612 | {
|
---|
| 613 | // now update the distributions with all the rays
|
---|
| 614 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
| 615 | mDistributions[i]->Update(vssRays);
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
[1966] | 618 |
|
---|
[1974] | 619 | #define RAY_CAST_TIME 0.7f
|
---|
| 620 | #define VIEWCELL_CAST_TIME 0.3f
|
---|
[1966] | 621 |
|
---|
[1942] | 622 | void
|
---|
[1884] | 623 | MixtureDistribution::UpdateRatios()
|
---|
| 624 | {
|
---|
| 625 | // now compute importance (ratio) of all distributions
|
---|
| 626 | float sum = 0.0f;
|
---|
| 627 | int i;
|
---|
| 628 | for (i=0; i < mDistributions.size(); i++) {
|
---|
[1891] | 629 | cout<<i<<": c="<<mDistributions[i]->mContribution<<
|
---|
| 630 | " rays="<<mDistributions[i]->mRays<<endl;
|
---|
| 631 | float importance = 0.0f;
|
---|
| 632 | if (mDistributions[i]->mRays != 0) {
|
---|
[1966] | 633 | //importance = pow(mDistributions[i]->mContribution/mDistributions[i]->mRays, 2);
|
---|
| 634 | importance = mDistributions[i]->mContribution/
|
---|
| 635 | (RAY_CAST_TIME*mDistributions[i]->mGeneratedRays +
|
---|
| 636 | VIEWCELL_CAST_TIME*mDistributions[i]->mRays);
|
---|
[1891] | 637 | }
|
---|
[1884] | 638 | mDistributions[i]->mRatio = importance;
|
---|
| 639 | sum += importance;
|
---|
| 640 | }
|
---|
[1891] | 641 |
|
---|
[1966] | 642 | if (sum == 0.0f)
|
---|
| 643 | sum = Limits::Small;
|
---|
[1974] | 644 |
|
---|
[1966] | 645 | const float minratio = 0.01f;
|
---|
[1974] | 646 |
|
---|
| 647 | for (i=0; i < mDistributions.size(); i++) {
|
---|
| 648 | mDistributions[i]->mRatio /= sum;
|
---|
| 649 | if (mDistributions[i]->mRatio < minratio)
|
---|
| 650 | mDistributions[i]->mRatio = minratio;
|
---|
| 651 | }
|
---|
[1966] | 652 |
|
---|
[1974] | 653 | // recaluate the sum after clip
|
---|
[1966] | 654 | sum = 0.0f;
|
---|
[1974] | 655 | for (i=0; i < mDistributions.size(); i++)
|
---|
[1966] | 656 | sum += mDistributions[i]->mRatio;
|
---|
[1974] | 657 |
|
---|
| 658 | for (i=0; i < mDistributions.size(); i++)
|
---|
| 659 | mDistributions[i]->mRatio /= sum;
|
---|
[1884] | 660 |
|
---|
| 661 | for (i=1; i < mDistributions.size(); i++) {
|
---|
[1974] | 662 | mDistributions[i]->mRatio = mDistributions[i-1]->mRatio + mDistributions[i]->mRatio;
|
---|
[1884] | 663 | }
|
---|
| 664 |
|
---|
| 665 | cout<<"ratios: ";
|
---|
[1931] | 666 | float last = 0.0f;
|
---|
| 667 | for (i=0; i < mDistributions.size(); i++) {
|
---|
| 668 | cout<<mDistributions[i]->mRatio-last<<" ";
|
---|
| 669 | last = mDistributions[i]->mRatio;
|
---|
| 670 | }
|
---|
[1884] | 671 | cout<<endl;
|
---|
[1883] | 672 | }
|
---|
[1891] | 673 |
|
---|
| 674 |
|
---|
| 675 |
|
---|
| 676 | bool
|
---|
| 677 | MixtureDistribution::Construct(char *str)
|
---|
| 678 | {
|
---|
| 679 | char *curr = str;
|
---|
| 680 |
|
---|
| 681 | while (1) {
|
---|
| 682 | char *e = strchr(curr,'+');
|
---|
| 683 | if (e!=NULL) {
|
---|
| 684 | *e=0;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | if (strcmp(curr, "rss")==0) {
|
---|
| 688 | mDistributions.push_back(new RssBasedDistribution(mPreprocessor));
|
---|
| 689 | } else
|
---|
| 690 | if (strcmp(curr, "object")==0) {
|
---|
| 691 | mDistributions.push_back(new ObjectBasedDistribution(mPreprocessor));
|
---|
| 692 | } else
|
---|
| 693 | if (strcmp(curr, "spatial")==0) {
|
---|
| 694 | mDistributions.push_back(new SpatialBoxBasedDistribution(mPreprocessor));
|
---|
| 695 | } else
|
---|
| 696 | if (strcmp(curr, "global")==0) {
|
---|
| 697 | mDistributions.push_back(new GlobalLinesDistribution(mPreprocessor));
|
---|
| 698 | } else
|
---|
| 699 | if (strcmp(curr, "direction")==0) {
|
---|
| 700 | mDistributions.push_back(new DirectionBasedDistribution(mPreprocessor));
|
---|
| 701 | } else
|
---|
| 702 | if (strcmp(curr, "object_direction")==0) {
|
---|
| 703 | mDistributions.push_back(new ObjectDirectionBasedDistribution(mPreprocessor));
|
---|
| 704 | } else
|
---|
| 705 | if (strcmp(curr, "reverse_object")==0) {
|
---|
| 706 | mDistributions.push_back(new ReverseObjectBasedDistribution(mPreprocessor));
|
---|
| 707 | } else
|
---|
[2044] | 708 |
|
---|
[1891] | 709 | if (strcmp(curr, "reverse_viewspace_border")==0) {
|
---|
| 710 | mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(mPreprocessor));
|
---|
[1952] | 711 | } else
|
---|
| 712 | if (strcmp(curr, "mutation")==0) {
|
---|
[2048] | 713 | mDistributions.push_back(new MutationBasedDistribution(mPreprocessor));
|
---|
[2001] | 714 | }
|
---|
[2548] | 715 | else if (strcmp(curr, "difference")==0) {
|
---|
| 716 | mDistributions.push_back(new DifferenceSampling(mPreprocessor));
|
---|
| 717 | }
|
---|
[1891] | 718 |
|
---|
[1952] | 719 |
|
---|
[1891] | 720 | if (e==NULL)
|
---|
| 721 | break;
|
---|
| 722 | curr = e+1;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | Init();
|
---|
| 726 | return true;
|
---|
[1884] | 727 | }
|
---|
[1883] | 728 |
|
---|
[1966] | 729 | int
|
---|
| 730 | MixtureDistribution::GenerateSamples(const int number,
|
---|
| 731 | SimpleRayContainer &rays)
|
---|
| 732 | {
|
---|
| 733 | for (int i=0; i < mDistributions.size(); i++)
|
---|
| 734 | mDistributions[i]->mGeneratedRays = 0;
|
---|
[1952] | 735 |
|
---|
[1966] | 736 | return SamplingStrategy::GenerateSamples(number, rays);
|
---|
| 737 | }
|
---|
[1952] | 738 |
|
---|
[1974] | 739 |
|
---|
[2076] | 740 | HwGlobalLinesDistribution::HwGlobalLinesDistribution(Preprocessor &preprocessor):
|
---|
| 741 | SamplingStrategy(preprocessor)
|
---|
| 742 | {
|
---|
| 743 | mType = HW_GLOBAL_LINES_DISTRIBUTION;
|
---|
| 744 | preprocessor.mUseHwGlobalLines = true;
|
---|
| 745 | }
|
---|
[1966] | 746 |
|
---|
[2210] | 747 |
|
---|
[1968] | 748 | bool HwGlobalLinesDistribution::GenerateSample(SimpleRay &ray)
|
---|
| 749 | {
|
---|
| 750 | Vector3 origin, termination, direction;
|
---|
| 751 |
|
---|
| 752 | float radius = 0.5f *
|
---|
| 753 | Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size());
|
---|
| 754 |
|
---|
| 755 | Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center();
|
---|
| 756 |
|
---|
| 757 | const int tries = 1000;
|
---|
| 758 | int i;
|
---|
| 759 | for (i=0; i < tries; i++)
|
---|
| 760 | {
|
---|
| 761 | float r[2];
|
---|
| 762 | sHalton.GetNext(2, r);
|
---|
| 763 |
|
---|
| 764 | origin = center + (radius * UniformRandomVector(r[0], r[1]));
|
---|
| 765 | termination = center;
|
---|
| 766 |
|
---|
| 767 | if (0)
|
---|
| 768 | {
|
---|
| 769 | // add a small offset to provide some more randomness in the sampling
|
---|
| 770 | Vector3 offset(Random(radius * 1e-3f),
|
---|
| 771 | Random(radius * 1e-3f),
|
---|
| 772 | Random(radius * 1e-3f));
|
---|
| 773 | termination += offset;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | direction = termination - origin;
|
---|
| 777 |
|
---|
| 778 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 779 | const float c = Magnitude(direction);
|
---|
| 780 |
|
---|
| 781 | if (c <= Limits::Small)
|
---|
| 782 | return false;
|
---|
| 783 |
|
---|
| 784 | direction *= 1.0f / c;
|
---|
| 785 |
|
---|
| 786 | // check if the ray intersects the view space box
|
---|
| 787 | static Ray ray;
|
---|
| 788 | ray.Init(origin, direction, Ray::LOCAL_RAY);
|
---|
| 789 |
|
---|
| 790 | float tmin, tmax;
|
---|
| 791 | if (mPreprocessor.mViewCellsManager->
|
---|
| 792 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
| 793 | break;
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | if (i != tries)
|
---|
| 797 | {
|
---|
| 798 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 799 | const float pdf = 1.0f;
|
---|
| 800 |
|
---|
| 801 | ray = SimpleRay(origin, direction, HW_GLOBAL_LINES_DISTRIBUTION, pdf);
|
---|
| 802 | ray.mType = Ray::GLOBAL_RAY;
|
---|
| 803 | return true;
|
---|
| 804 | }
|
---|
| 805 |
|
---|
| 806 | return false;
|
---|
[1952] | 807 | }
|
---|
| 808 |
|
---|
| 809 |
|
---|
[1996] | 810 | bool ViewCellBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
| 811 | {
|
---|
| 812 | Vector3 origin, direction;
|
---|
| 813 |
|
---|
| 814 | ViewCellContainer &viewCells =
|
---|
| 815 | mPreprocessor.mViewCellsManager->GetViewCells();
|
---|
| 816 |
|
---|
| 817 | Vector3 point;
|
---|
[2003] | 818 | Vector3 normal;
|
---|
| 819 |
|
---|
| 820 | //Vector normalObj;
|
---|
[2000] | 821 | // float r[1];
|
---|
| 822 | // sHalton.GetNext(1, r);
|
---|
| 823 | // const int objIdx = (int)(r[0] * (float)mPreprocessor.mObjects.size() - 1.0f);
|
---|
| 824 | // Intersectable *object = mPreprocessor.mObjects[objIdx];
|
---|
| 825 | // object->GetRandomSurfacePoint(point, normal);
|
---|
| 826 | // cout << "obj: " << objIdx << endl;
|
---|
[1996] | 827 |
|
---|
[2000] | 828 | float r[2];
|
---|
| 829 | sHalton.GetNext(2, r);
|
---|
[1996] | 830 |
|
---|
[2000] | 831 | direction = UniformRandomVector(r[0], r[1]);
|
---|
| 832 | const float c = Magnitude(direction);
|
---|
[1996] | 833 |
|
---|
[2000] | 834 | if (c <= Limits::Small)
|
---|
| 835 | return false;
|
---|
| 836 |
|
---|
| 837 | direction *= 1.0f / c;
|
---|
| 838 |
|
---|
| 839 | // get point on view cell surface
|
---|
[2003] | 840 | mViewCell->GetRandomSurfacePoint(origin, normal);
|
---|
[1996] | 841 |
|
---|
[2000] | 842 | //direction = point - origin;
|
---|
[1996] | 843 |
|
---|
| 844 | // move a little bit back to avoid piercing through walls
|
---|
| 845 | // that bound the view cell
|
---|
[2003] | 846 | origin -= 0.01f * normal;
|
---|
[1996] | 847 |
|
---|
| 848 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 849 | const float pdf = 1.0f;
|
---|
| 850 |
|
---|
| 851 | ray = SimpleRay(origin, direction, VIEWCELL_BASED_DISTRIBUTION, pdf);
|
---|
| 852 |
|
---|
| 853 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
| 854 |
|
---|
| 855 | return true;
|
---|
[1968] | 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
[1996] | 859 | }
|
---|
| 860 |
|
---|
| 861 |
|
---|