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