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