[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"
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | namespace GtpVisibilityPreprocessor {
|
---|
| 10 |
|
---|
| 11 |
|
---|
[1520] | 12 | SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor):
|
---|
[1785] | 13 | mPreprocessor(preprocessor), mRatio(1.0f)
|
---|
[1520] | 14 | {
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | SamplingStrategy::~SamplingStrategy()
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[1771] | 22 | int
|
---|
| 23 | SamplingStrategy::GenerateSamples(const int number,
|
---|
| 24 | SimpleRayContainer &rays) const
|
---|
| 25 | {
|
---|
[1772] | 26 | SimpleRay ray;
|
---|
| 27 | int samples = 0;
|
---|
| 28 | int i = 0;
|
---|
| 29 | const int maxTries = 20;
|
---|
| 30 | // tmp changed matt. Q: should one rejected sample
|
---|
| 31 | // terminate the whole method?
|
---|
| 32 | if (0)
|
---|
| 33 | {
|
---|
| 34 | for (; i < number; i++) {
|
---|
| 35 | if (!GenerateSample(ray))
|
---|
| 36 | return i;
|
---|
| 37 | rays.push_back(ray);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | else
|
---|
| 41 | {
|
---|
| 42 | for (; i < number; i++)
|
---|
| 43 | {
|
---|
| 44 | int j = 0;
|
---|
| 45 | bool sampleGenerated = false;
|
---|
[1520] | 46 |
|
---|
[1772] | 47 | for (j = 0; !sampleGenerated && (j < maxTries); ++ j)
|
---|
| 48 | {
|
---|
| 49 | sampleGenerated = GenerateSample(ray);
|
---|
| 50 |
|
---|
| 51 | if (sampleGenerated)
|
---|
| 52 | {
|
---|
| 53 | ++ samples;
|
---|
| 54 | rays.push_back(ray);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | return samples;
|
---|
[1771] | 61 | }
|
---|
[1520] | 62 |
|
---|
[1771] | 63 |
|
---|
[1020] | 64 | /*********************************************************************/
|
---|
| 65 | /* Individual sampling strategies implementation */
|
---|
| 66 | /*********************************************************************/
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 70 | {
|
---|
| 71 | Vector3 origin, direction;
|
---|
| 72 |
|
---|
| 73 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 74 |
|
---|
| 75 | Vector3 point;
|
---|
| 76 | Vector3 normal;
|
---|
[1692] | 77 | //cout << "y";
|
---|
[1588] | 78 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
[1020] | 79 |
|
---|
| 80 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 81 |
|
---|
| 82 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 83 | direction = point - origin;
|
---|
| 84 |
|
---|
| 85 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 86 | const float c = Magnitude(direction);
|
---|
| 87 |
|
---|
| 88 | if (c <= Limits::Small)
|
---|
| 89 | return false;
|
---|
| 90 |
|
---|
| 91 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 92 | const float pdf = 1.0f;
|
---|
| 93 |
|
---|
| 94 | direction *= 1.0f / c;
|
---|
| 95 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 96 |
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
[1278] | 102 | {
|
---|
[1020] | 103 | Vector3 origin, direction;
|
---|
[1587] | 104 | const int i = (int)RandomValue(0, (Real)mPreprocessor.mObjects.size() - 0.5f);
|
---|
[1020] | 105 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 106 |
|
---|
| 107 | Vector3 normal;
|
---|
[1692] | 108 | //cout << "x";
|
---|
[1020] | 109 | object->GetRandomSurfacePoint(origin, normal);
|
---|
| 110 | direction = UniformRandomVector(normal);
|
---|
| 111 |
|
---|
| 112 | origin += 0.1f * direction;
|
---|
| 113 |
|
---|
| 114 | const float c = Magnitude(direction);
|
---|
| 115 |
|
---|
| 116 | if (c <= Limits::Small)
|
---|
| 117 | return false;
|
---|
[1278] | 118 |
|
---|
[1020] | 119 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 120 | const float pdf = 1.0f;
|
---|
| 121 |
|
---|
| 122 | direction *= 1.0f / c;
|
---|
| 123 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 124 |
|
---|
| 125 | return true;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
[1278] | 130 | {
|
---|
[1020] | 131 | Vector3 origin, direction;
|
---|
| 132 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 133 |
|
---|
| 134 | direction = UniformRandomVector();
|
---|
| 135 | const float c = Magnitude(direction);
|
---|
| 136 |
|
---|
| 137 | if (c <= Limits::Small)
|
---|
| 138 | return false;
|
---|
| 139 |
|
---|
| 140 | const float pdf = 1.0f;
|
---|
| 141 |
|
---|
| 142 | direction *= 1.0f / c;
|
---|
| 143 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 144 |
|
---|
| 145 | return true;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 150 | {
|
---|
| 151 | Vector3 origin, direction;
|
---|
| 152 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 153 |
|
---|
[1297] | 154 | const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
|
---|
| 155 | const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
|
---|
[1020] | 156 |
|
---|
| 157 | direction = VssRay::GetDirection(alpha, beta);
|
---|
| 158 |
|
---|
| 159 | const float c = Magnitude(direction);
|
---|
| 160 |
|
---|
| 161 | if (c <= Limits::Small)
|
---|
| 162 | return false;
|
---|
| 163 |
|
---|
| 164 | const float pdf = 1.0f;
|
---|
| 165 |
|
---|
| 166 | direction *= 1.0f / c;
|
---|
| 167 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 168 |
|
---|
| 169 | return true;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 174 | {
|
---|
| 175 | Vector3 origin, direction;
|
---|
[1278] | 176 |
|
---|
[1020] | 177 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 178 | direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin;
|
---|
[1692] | 179 | //cout << "z";
|
---|
[1020] | 180 | const float c = Magnitude(direction);
|
---|
| 181 |
|
---|
| 182 | if (c <= Limits::Small)
|
---|
| 183 | return false;
|
---|
| 184 |
|
---|
| 185 | const float pdf = 1.0f;
|
---|
| 186 |
|
---|
| 187 | direction *= 1.0f / c;
|
---|
| 188 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 189 |
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[1695] | 193 |
|
---|
| 194 | bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 195 | {
|
---|
| 196 | Vector3 origin, direction;
|
---|
| 197 |
|
---|
| 198 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
| 199 |
|
---|
| 200 | Vector3 point;
|
---|
| 201 | Vector3 normal;
|
---|
| 202 | //cout << "y";
|
---|
| 203 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 204 |
|
---|
| 205 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 206 |
|
---|
| 207 | object->GetRandomSurfacePoint(point, normal);
|
---|
[1765] | 208 |
|
---|
[1695] | 209 | direction = origin - point;
|
---|
[1765] | 210 |
|
---|
[1695] | 211 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 212 | const float c = Magnitude(direction);
|
---|
[1765] | 213 |
|
---|
| 214 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
| 215 | {
|
---|
[1695] | 216 | return false;
|
---|
[1765] | 217 | }
|
---|
[1695] | 218 |
|
---|
| 219 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 220 | const float pdf = 1.0f;
|
---|
| 221 | //cout << "p: " << point << " ";
|
---|
| 222 | direction *= 1.0f / c;
|
---|
[1765] | 223 | // a little offset
|
---|
[1772] | 224 | point += direction * 0.001f;
|
---|
[1765] | 225 |
|
---|
[1695] | 226 | ray = SimpleRay(point, direction, pdf);
|
---|
| 227 |
|
---|
| 228 | return true;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[1763] | 231 |
|
---|
| 232 | bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 233 | {
|
---|
| 234 | Vector3 origin, direction;
|
---|
| 235 |
|
---|
| 236 | ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
|
---|
| 237 |
|
---|
| 238 | Vector3 point;
|
---|
| 239 | Vector3 normal, normal2;
|
---|
| 240 |
|
---|
| 241 | const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
|
---|
| 242 | const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 243 |
|
---|
| 244 | Intersectable *object = mPreprocessor.mObjects[objIdx];
|
---|
| 245 | ViewCell *viewCell = viewCells[vcIdx];
|
---|
| 246 |
|
---|
[1769] | 247 | //cout << "vc: " << vcIdx << endl;
|
---|
| 248 | //cout << "obj: " << objIdx << endl;
|
---|
| 249 |
|
---|
[1763] | 250 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 251 | viewCell->GetRandomEdgePoint(origin, normal2);
|
---|
| 252 |
|
---|
| 253 | direction = point - origin;
|
---|
| 254 |
|
---|
| 255 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 256 | const float c = Magnitude(direction);
|
---|
| 257 |
|
---|
[1769] | 258 | if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
|
---|
[1768] | 259 | {
|
---|
[1763] | 260 | return false;
|
---|
[1768] | 261 | }
|
---|
[1763] | 262 |
|
---|
| 263 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 264 | const float pdf = 1.0f;
|
---|
| 265 | //cout << "p: " << point << " ";
|
---|
| 266 | direction *= 1.0f / c;
|
---|
| 267 | ray = SimpleRay(origin, direction, pdf);
|
---|
[1769] | 268 |
|
---|
| 269 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
| 270 |
|
---|
[1763] | 271 | return true;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[1765] | 274 |
|
---|
[1520] | 275 | #if 0
|
---|
[1020] | 276 | bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 277 | {
|
---|
| 278 | Vector3 origin, direction;
|
---|
| 279 |
|
---|
| 280 | // get random object
|
---|
| 281 | const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
|
---|
| 282 |
|
---|
| 283 | const Intersectable *obj = mPreprocessor.mObjects[i];
|
---|
| 284 |
|
---|
| 285 | // note: if we load the polygons as meshes,
|
---|
| 286 | // asymtotically every second sample is lost!
|
---|
| 287 | origin = obj->GetBox().GetRandomPoint();
|
---|
| 288 |
|
---|
| 289 | // uniformly distributed direction
|
---|
| 290 | direction = UniformRandomVector();
|
---|
| 291 |
|
---|
| 292 | const float c = Magnitude(direction);
|
---|
| 293 |
|
---|
| 294 | if (c <= Limits::Small)
|
---|
| 295 | return false;
|
---|
| 296 |
|
---|
| 297 | const float pdf = 1.0f;
|
---|
| 298 |
|
---|
| 299 | direction *= 1.0f / c;
|
---|
| 300 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 301 |
|
---|
| 302 | return true;
|
---|
[1520] | 303 | }
|
---|
[1769] | 304 |
|
---|
[1520] | 305 | #endif
|
---|
[1771] | 306 |
|
---|
[1772] | 307 |
|
---|
| 308 | bool ReverseViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 309 | {
|
---|
| 310 | Vector3 origin, direction;
|
---|
| 311 |
|
---|
| 312 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
| 313 |
|
---|
| 314 | Vector3 point;
|
---|
| 315 | Vector3 normal;
|
---|
| 316 | //cout << "y";
|
---|
| 317 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
| 318 |
|
---|
| 319 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
| 320 |
|
---|
| 321 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 322 |
|
---|
| 323 | direction = origin - point;
|
---|
| 324 |
|
---|
| 325 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 326 | const float c = Magnitude(direction);
|
---|
| 327 |
|
---|
| 328 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
| 329 | {
|
---|
| 330 | return false;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 334 | const float pdf = 1.0f;
|
---|
| 335 | //cout << "p: " << point << " ";
|
---|
| 336 | direction *= 1.0f / c;
|
---|
| 337 | // a little offset
|
---|
| 338 | point += direction * 0.001f;
|
---|
| 339 |
|
---|
| 340 | ray = SimpleRay(point, direction, pdf);
|
---|
| 341 |
|
---|
| 342 | return true;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 | bool ViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 347 | {
|
---|
| 348 | Vector3 origin, direction;
|
---|
| 349 |
|
---|
| 350 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
| 351 |
|
---|
| 352 | Vector3 point;
|
---|
| 353 | Vector3 normal;
|
---|
| 354 | //cout << "w";
|
---|
| 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 | direction = point - origin;
|
---|
| 361 |
|
---|
| 362 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 363 | const float c = Magnitude(direction);
|
---|
| 364 |
|
---|
| 365 | if (c <= Limits::Small)
|
---|
| 366 | return false;
|
---|
| 367 |
|
---|
| 368 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 369 | const float pdf = 1.0f;
|
---|
| 370 |
|
---|
| 371 | direction *= 1.0f / c;
|
---|
| 372 |
|
---|
| 373 | // a little offset
|
---|
| 374 | origin += direction * 0.001f;
|
---|
| 375 |
|
---|
| 376 | ray = SimpleRay(origin, direction, pdf);
|
---|
| 377 |
|
---|
| 378 | return true;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 |
|
---|
[1771] | 382 | bool
|
---|
| 383 | RssBasedDistribution::GenerateSample(SimpleRay &ray) const
|
---|
| 384 | {
|
---|
| 385 | return false;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 |
|
---|