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