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