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