[1991] | 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 | #include "RssTree.h"
|
---|
| 8 | #include "Vector2.h"
|
---|
| 9 | #include "RndGauss.h"
|
---|
| 10 | #include "Mutation.h"
|
---|
[2001] | 11 | #include "Exporter.h"
|
---|
[1991] | 12 |
|
---|
| 13 | #ifdef GTP_INTERNAL
|
---|
| 14 | #include "ArchModeler2MLRT.hxx"
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | namespace GtpVisibilityPreprocessor {
|
---|
| 18 |
|
---|
| 19 | #define MUTATION_USE_CDF 0
|
---|
| 20 | #define USE_SILHOUETTE_MUTATIONS 0
|
---|
[1997] | 21 |
|
---|
| 22 | #define USE_SIL_TERMINATION_MUTATION 1
|
---|
[2001] | 23 | #define MUTATE_ORIGIN 1
|
---|
[1997] | 24 |
|
---|
[1991] | 25 | #define EVALUATE_MUTATION_STATS 1
|
---|
| 26 |
|
---|
[2001] | 27 | #define Q_SEARCH_STEPS 2
|
---|
| 28 |
|
---|
[1991] | 29 | void
|
---|
| 30 | MutationBasedDistribution::Update(VssRayContainer &vssRays)
|
---|
| 31 | {
|
---|
| 32 | // for (int i=0; i < mRays.size(); i++)
|
---|
| 33 | // cout<<mRays[i].mMutations<<" ";
|
---|
| 34 | // cout<<endl;
|
---|
| 35 | cerr<<"Muattion update..."<<endl;
|
---|
| 36 | cerr<<"rays = "<<mRays.size()<<endl;
|
---|
| 37 | if (mRays.size()) {
|
---|
| 38 | cerr<<"Oversampling factors = "<<
|
---|
| 39 | GetEntry(0).mMutations<<" "<<
|
---|
| 40 | GetEntry(1).mMutations<<" "<<
|
---|
| 41 | GetEntry(2).mMutations<<" "<<
|
---|
| 42 | GetEntry(3).mMutations<<" "<<
|
---|
| 43 | GetEntry(4).mMutations<<" "<<
|
---|
| 44 | GetEntry(5).mMutations<<" ... "<<
|
---|
| 45 | GetEntry(mRays.size()-6).mMutations<<" "<<
|
---|
| 46 | GetEntry(mRays.size()-5).mMutations<<" "<<
|
---|
| 47 | GetEntry(mRays.size()-4).mMutations<<" "<<
|
---|
| 48 | GetEntry(mRays.size()-3).mMutations<<" "<<
|
---|
| 49 | GetEntry(mRays.size()-2).mMutations<<" "<<
|
---|
| 50 | GetEntry(mRays.size()-1).mMutations<<endl;
|
---|
| 51 | }
|
---|
| 52 | int contributingRays = 0;
|
---|
| 53 |
|
---|
| 54 | int mutationRays = 0;
|
---|
| 55 | int dummyNcMutations = 0;
|
---|
| 56 | int dummyCMutations = 0;
|
---|
[2001] | 57 |
|
---|
| 58 | int reverseCandidates = 0;
|
---|
[1991] | 59 |
|
---|
| 60 | for (int i=0; i < vssRays.size(); i++) {
|
---|
| 61 | if (vssRays[i]->mPvsContribution) {
|
---|
| 62 | // reset the counter of unsuccsseful mutation for a generating ray (if it exists)
|
---|
| 63 | if (vssRays[i]->mDistribution == MUTATION_BASED_DISTRIBUTION &&
|
---|
| 64 | vssRays[i]->mGeneratorId != -1
|
---|
| 65 | ) {
|
---|
| 66 | mRays[vssRays[i]->mGeneratorId].mUnsuccessfulMutations = 0;
|
---|
| 67 | #if EVALUATE_MUTATION_STATS
|
---|
| 68 | mutationRays++;
|
---|
| 69 |
|
---|
| 70 | Intersectable *newObject =
|
---|
| 71 | mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 72 | *vssRays[i],
|
---|
| 73 | true);
|
---|
| 74 |
|
---|
| 75 | Intersectable *oldObject =
|
---|
| 76 | mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 77 | *mRays[vssRays[i]->mGeneratorId].mRay,
|
---|
| 78 | true);
|
---|
| 79 |
|
---|
| 80 | if (oldObject == newObject)
|
---|
| 81 | dummyCMutations++;
|
---|
| 82 | #endif
|
---|
| 83 | }
|
---|
| 84 | contributingRays++;
|
---|
| 85 | if (mRays.size() < mMaxRays) {
|
---|
| 86 | VssRay *newRay = new VssRay(*vssRays[i]);
|
---|
| 87 | // add this ray
|
---|
| 88 | newRay->Ref();
|
---|
| 89 | mRays.push_back(RayEntry(newRay));
|
---|
| 90 | } else {
|
---|
| 91 | // unref the old ray
|
---|
| 92 | *mRays[mBufferStart].mRay = *vssRays[i];
|
---|
| 93 | mRays[mBufferStart].mMutations = 0;
|
---|
[2001] | 94 | mRays[mBufferStart].ResetReverseMutation();
|
---|
[1991] | 95 | // mRays[mBufferStart] = RayEntry(newRay);
|
---|
| 96 | mBufferStart++;
|
---|
| 97 | if (mBufferStart >= mMaxRays)
|
---|
| 98 | mBufferStart = 0;
|
---|
[2001] | 99 |
|
---|
| 100 |
|
---|
[1991] | 101 | }
|
---|
| 102 | } else {
|
---|
| 103 | if (vssRays[i]->mDistribution == MUTATION_BASED_DISTRIBUTION &&
|
---|
| 104 | vssRays[i]->mGeneratorId != -1
|
---|
| 105 | ) {
|
---|
[2001] | 106 | // check whether not to store a new backward mutation candidate
|
---|
| 107 | VssRay *oldRay = mRays[vssRays[i]->mGeneratorId].mRay;
|
---|
| 108 | VssRay *newRay = vssRays[i];
|
---|
[1991] | 109 |
|
---|
[2001] | 110 | #define DIST_THRESHOLD 3.0f
|
---|
| 111 |
|
---|
| 112 | Intersectable *oldObject =
|
---|
[1991] | 113 | mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
[2001] | 114 | *oldRay,
|
---|
[1991] | 115 | true);
|
---|
[2001] | 116 |
|
---|
[1991] | 117 |
|
---|
[2001] | 118 | if (!mRays[newRay->mGeneratorId].HasReverseMutation()) {
|
---|
| 119 | if (DotProd(oldRay->GetDir(), newRay->GetDir()) > 0.0f) {
|
---|
| 120 | float oldDist = Magnitude(oldRay->mTermination - newRay->mOrigin);
|
---|
| 121 | float newDist = Magnitude(newRay->mTermination - newRay->mOrigin);
|
---|
| 122 |
|
---|
| 123 | if (newDist < oldDist - oldObject->GetBox().Radius()*DIST_THRESHOLD) {
|
---|
| 124 | Vector3 origin, termination;
|
---|
| 125 | if (ComputeReverseMutation(*oldRay, *newRay, origin, termination)) {
|
---|
| 126 | mRays[newRay->mGeneratorId].SetReverseMutation(origin, termination);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | reverseCandidates++;
|
---|
| 130 | //mReverseCandidates
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | #if EVALUATE_MUTATION_STATS
|
---|
| 135 | mutationRays++;
|
---|
| 136 |
|
---|
| 137 | Intersectable *newObject =
|
---|
[1991] | 138 | mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
[2001] | 139 | *vssRays[i],
|
---|
[1991] | 140 | true);
|
---|
| 141 |
|
---|
[2001] | 142 |
|
---|
[1991] | 143 | if (oldObject == newObject)
|
---|
| 144 | dummyNcMutations++;
|
---|
[2001] | 145 | #endif
|
---|
[1991] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[2001] | 149 |
|
---|
[1991] | 150 | if (mutationRays) {
|
---|
| 151 | cout<<"Mutated rays:"<<mutationRays<<endl;
|
---|
| 152 | cout<<"Dummy mutations ratio:"<<100.0f*(dummyCMutations + dummyNcMutations)/
|
---|
| 153 | (float)mutationRays<<"%"<<endl;
|
---|
| 154 | cout<<"Dummy NC mutations ratio:"<<100.0f*dummyNcMutations/(float)mutationRays<<"%"<<endl;
|
---|
| 155 | cout<<"Dummy C mutations ratio:"<<100.0f*dummyCMutations/(float)mutationRays<<"%"<<endl;
|
---|
[2001] | 156 | cout<<"Reverse candidates:"<<reverseCandidates<<endl;
|
---|
[1991] | 157 | }
|
---|
| 158 |
|
---|
| 159 | float pContributingRays = contributingRays/(float)vssRays.size();
|
---|
[2002] | 160 | // float importance = 1.0f/(pContributingRays + 1e-5);
|
---|
| 161 | float importance = 1.0f;
|
---|
[1991] | 162 | // set this values for last contributingRays
|
---|
| 163 | int index = mBufferStart - 1;
|
---|
| 164 |
|
---|
| 165 | for (int i=0; i < contributingRays; i++, index--) {
|
---|
| 166 | if (index < 0)
|
---|
| 167 | index = mRays.size()-1;
|
---|
| 168 | mRays[index].mImportance = importance;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | #if MUTATION_USE_CDF
|
---|
| 172 | // compute cdf
|
---|
| 173 | mRays[0].mCdf = mRays[0].mImportance/(mRays[0].mMutations+1);
|
---|
| 174 | for (int i=1; i < mRays.size(); i++)
|
---|
| 175 | mRays[i].mCdf = mRays[i-1].mCdf + mRays[i].mImportance/(mRays[i].mMutations+1);
|
---|
| 176 |
|
---|
| 177 | float scale = 1.0f/mRays[i-1].mCdf;
|
---|
| 178 | for (i=0; i < mRays.size(); i++) {
|
---|
| 179 | mRays[i].mCdf *= scale;
|
---|
| 180 | }
|
---|
| 181 | #endif
|
---|
| 182 |
|
---|
| 183 | cout<<"Importance = "<<
|
---|
| 184 | GetEntry(0).mImportance<<" "<<
|
---|
| 185 | GetEntry(mRays.size()-1).mImportance<<endl;
|
---|
| 186 |
|
---|
| 187 | cerr<<"Mutation update done."<<endl;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | Vector3
|
---|
| 192 | MutationBasedDistribution::ComputeOriginMutation(const VssRay &ray,
|
---|
| 193 | const Vector3 &U,
|
---|
| 194 | const Vector3 &V,
|
---|
| 195 | const Vector2 vr2,
|
---|
| 196 | const float radius
|
---|
| 197 | )
|
---|
| 198 | {
|
---|
| 199 | #if 0
|
---|
| 200 | Vector3 v;
|
---|
| 201 | if (d.DrivingAxis() == 0)
|
---|
| 202 | v = Vector3(0, r[0]-0.5f, r[1]-0.5f);
|
---|
| 203 | else
|
---|
| 204 | if (d.DrivingAxis() == 1)
|
---|
| 205 | v = Vector3(r[0]-0.5f, 0, r[1]-0.5f);
|
---|
| 206 | else
|
---|
| 207 | v = Vector3(r[0]-0.5f, r[1]-0.5f, 0);
|
---|
| 208 | return v*(2*radius);
|
---|
| 209 | #endif
|
---|
| 210 | #if 0
|
---|
| 211 | return (U*(r[0] - 0.5f) + V*(r[1] - 0.5f))*(2*radius);
|
---|
| 212 | #endif
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | // Output random variable
|
---|
| 216 | Vector2 gaussvec2;
|
---|
| 217 |
|
---|
| 218 | // Here we apply transform to gaussian, so 2D bivariate
|
---|
| 219 | // normal distribution
|
---|
| 220 | // float sigma = ComputeSigmaFromRadius(radius);
|
---|
| 221 | float sigma = radius;
|
---|
| 222 | GaussianOn2D(vr2,
|
---|
| 223 | sigma, // input
|
---|
| 224 | gaussvec2); // output
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | // Here we tranform the point correctly to 3D space using base
|
---|
| 228 | // vectors of the 3D space defined by the direction
|
---|
| 229 | Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
| 230 |
|
---|
| 231 | // cout<<shift<<endl;
|
---|
| 232 | return shift;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | Vector3
|
---|
| 236 | MutationBasedDistribution::ComputeTerminationMutation(const VssRay &ray,
|
---|
| 237 | const Vector3 &U,
|
---|
| 238 | const Vector3 &V,
|
---|
| 239 | const Vector2 vr2,
|
---|
| 240 | const float radius
|
---|
| 241 | )
|
---|
| 242 | {
|
---|
| 243 | #if 0
|
---|
| 244 | Vector3 v;
|
---|
| 245 | // mutate the termination
|
---|
| 246 | if (d.DrivingAxis() == 0)
|
---|
| 247 | v = Vector3(0, r[2]-0.5f, r[3]-0.5f);
|
---|
| 248 | else
|
---|
| 249 | if (d.DrivingAxis() == 1)
|
---|
| 250 | v = Vector3(r[2]-0.5f, 0, r[3]-0.5f);
|
---|
| 251 | else
|
---|
| 252 | v = Vector3(r[2]-0.5f, r[3]-0.5f, 0);
|
---|
| 253 |
|
---|
| 254 | // Vector3 nv;
|
---|
| 255 |
|
---|
| 256 | // if (Magnitude(v) > Limits::Small)
|
---|
| 257 | // nv = Normalize(v);
|
---|
| 258 | // else
|
---|
| 259 | // nv = v;
|
---|
| 260 |
|
---|
| 261 | // v = nv*size + v*size;
|
---|
| 262 |
|
---|
| 263 | return v*(4.0f*radius);
|
---|
| 264 | #endif
|
---|
| 265 | #if 0
|
---|
| 266 | return (U*(vr2.xx - 0.5f) + V*(vr2.yy - 0.5f))*(4.0f*radius);
|
---|
| 267 | #endif
|
---|
| 268 | Vector2 gaussvec2;
|
---|
| 269 | #if 1
|
---|
| 270 | float sigma = radius;
|
---|
| 271 | GaussianOn2D(vr2,
|
---|
| 272 | sigma, // input
|
---|
| 273 | gaussvec2); // output
|
---|
| 274 | Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
| 275 | // cout<<shift<<endl;
|
---|
| 276 | return shift;
|
---|
| 277 | #endif
|
---|
| 278 | #if 0
|
---|
| 279 | // Here we estimate standard deviation (sigma) from radius
|
---|
| 280 | float sigma = 1.1f*ComputeSigmaFromRadius(radius);
|
---|
| 281 | Vector3 vr3(vr2.xx, vr2.yy, RandomValue(0,1));
|
---|
| 282 | PolarGaussianOnDisk(vr3,
|
---|
| 283 | sigma,
|
---|
| 284 | radius, // input
|
---|
| 285 | gaussvec2); // output
|
---|
| 286 |
|
---|
| 287 | // Here we tranform the point correctly to 3D space using base
|
---|
| 288 | // vectors of the 3D space defined by the direction
|
---|
| 289 | Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
| 290 |
|
---|
| 291 | // cout<<shift<<endl;
|
---|
| 292 | return shift;
|
---|
| 293 | #endif
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[2001] | 296 | bool
|
---|
| 297 | MutationBasedDistribution::ComputeReverseMutation(
|
---|
| 298 | const VssRay &oldRay,
|
---|
| 299 | const VssRay &newRay,
|
---|
| 300 | Vector3 &origin,
|
---|
| 301 | Vector3 &termination
|
---|
| 302 | )
|
---|
| 303 | {
|
---|
| 304 | // first reconstruct the termination point
|
---|
| 305 | Vector3 oldDir = Normalize(oldRay.GetDir());
|
---|
| 306 | Plane3 oldPlane(oldDir, oldRay.mTermination);
|
---|
| 307 |
|
---|
| 308 | termination = oldPlane.FindIntersection(newRay.mOrigin,
|
---|
| 309 | newRay.mTermination);
|
---|
| 310 |
|
---|
| 311 | // now find the new origin of the ray by casting ray backward from the termination and termining
|
---|
| 312 | // silhouette point with respect to the occluding object (object containing the newRay termination)
|
---|
[1991] | 313 |
|
---|
[2001] | 314 | Plane3 newPlane(oldDir, newRay.mTermination);
|
---|
| 315 |
|
---|
| 316 | Vector3 oldPivot = newPlane.FindIntersection(oldRay.mOrigin,
|
---|
| 317 | oldRay.mTermination);
|
---|
| 318 |
|
---|
| 319 | Vector3 newPivot = newRay.mTermination;
|
---|
| 320 | Vector3 line = 2.0f*(oldPivot - newPivot);
|
---|
| 321 |
|
---|
| 322 | Intersectable *occluder = mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 323 | newRay,
|
---|
| 324 | true);
|
---|
| 325 |
|
---|
| 326 | AxisAlignedBox3 box = occluder->GetBox();
|
---|
| 327 | box.Scale(2.0f);
|
---|
| 328 |
|
---|
| 329 | const int packetSize = 4;
|
---|
| 330 | static int hit_triangles[packetSize];
|
---|
| 331 | static float dist[packetSize];
|
---|
| 332 | static Vector3 dirs[packetSize];
|
---|
| 333 | static Vector3 shifts[packetSize];
|
---|
| 334 | // now find the silhouette along the line
|
---|
| 335 | int i;
|
---|
| 336 | float left = 0.0f;
|
---|
| 337 | float right = 1.0f;
|
---|
| 338 | // cast rays to find silhouette ray
|
---|
| 339 | for (int j=0; j < Q_SEARCH_STEPS; j++) {
|
---|
| 340 | for (i=0; i < packetSize; i++) {
|
---|
| 341 | float r = left + (i+1)*(right-left)/(packetSize+1);
|
---|
| 342 | shifts[i] = r*line;
|
---|
| 343 | dirs[i] = Normalize(newPivot + shifts[i] - termination );
|
---|
| 344 | mlrtaStoreRayASEye4(&termination.x,
|
---|
| 345 | &dirs[i].x,
|
---|
| 346 | i);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | mlrtaTraverseGroupASEye4(&box.Min().x,
|
---|
| 350 | &box.Max().x,
|
---|
| 351 | hit_triangles,
|
---|
| 352 | dist);
|
---|
| 353 |
|
---|
| 354 | for (i=0; i < packetSize; i++) {
|
---|
| 355 | if (hit_triangles[i] == -1) {
|
---|
| 356 | // break on first passing ray
|
---|
| 357 | break;
|
---|
| 358 | }
|
---|
| 359 | }
|
---|
| 360 | float rr = left + (i+1)*(right-left)/(packetSize+1);
|
---|
| 361 | float rl = left + i*(right-left)/(packetSize+1);
|
---|
| 362 | left = rl;
|
---|
| 363 | right = rr;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | float t = right;
|
---|
| 367 | if (right==1.0f)
|
---|
| 368 | return false;
|
---|
| 369 |
|
---|
| 370 | if (i == packetSize)
|
---|
| 371 | origin = newPivot + right*line;
|
---|
| 372 | else
|
---|
| 373 | origin = newPivot + shifts[i];
|
---|
| 374 |
|
---|
| 375 | if (0) {
|
---|
| 376 |
|
---|
| 377 | static VssRayContainer rRays;
|
---|
| 378 | static int counter = 0;
|
---|
| 379 | char filename[256];
|
---|
| 380 |
|
---|
| 381 | if (counter < 50) {
|
---|
| 382 | sprintf(filename, "reverse_rays_%03d.x3d", counter++);
|
---|
| 383 |
|
---|
| 384 | VssRay tRay(origin, termination, NULL, NULL);
|
---|
| 385 | rRays.push_back((VssRay *)&oldRay);
|
---|
| 386 | rRays.push_back((VssRay *)&newRay);
|
---|
| 387 | rRays.push_back(&tRay);
|
---|
| 388 |
|
---|
| 389 | Exporter *exporter = NULL;
|
---|
| 390 | exporter = Exporter::GetExporter(filename);
|
---|
| 391 |
|
---|
| 392 | exporter->SetFilled();
|
---|
| 393 |
|
---|
| 394 | Intersectable *occludee = mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 395 | oldRay,
|
---|
| 396 | true);
|
---|
| 397 |
|
---|
| 398 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 399 | exporter->ExportIntersectable(occluder);
|
---|
| 400 | exporter->SetForcedMaterial(RgbColor(0,1,0));
|
---|
| 401 | exporter->ExportIntersectable(occludee);
|
---|
| 402 | exporter->ResetForcedMaterial();
|
---|
| 403 |
|
---|
| 404 | exporter->SetWireframe();
|
---|
| 405 |
|
---|
| 406 |
|
---|
| 407 | exporter->ExportRays(rRays, RgbColor(1, 0, 0));
|
---|
| 408 | delete exporter;
|
---|
| 409 | rRays.clear();
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | return true;
|
---|
| 416 |
|
---|
| 417 | // now the origin and termination is swapped compred to the generator ray
|
---|
| 418 | // swap(origin, termination);???
|
---|
| 419 | // -> perhaps not neccessary as the reverse mutation wil only be used once!
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[1997] | 422 | Vector3
|
---|
| 423 | MutationBasedDistribution::ComputeSilhouetteTerminationMutation(const VssRay &ray,
|
---|
| 424 | const Vector3 &origin,
|
---|
| 425 | const AxisAlignedBox3 &box,
|
---|
| 426 | const Vector3 &U,
|
---|
| 427 | const Vector3 &V,
|
---|
| 428 | const Vector2 vr2,
|
---|
| 429 | const float radius
|
---|
| 430 | )
|
---|
| 431 | {
|
---|
[2001] | 432 | const int packetSize = 4;
|
---|
[1997] | 433 | static int hit_triangles[packetSize];
|
---|
| 434 | static float dist[packetSize];
|
---|
| 435 | static Vector3 dirs[packetSize];
|
---|
[2001] | 436 | static Vector3 shifts[packetSize];
|
---|
[1997] | 437 | // mutate the
|
---|
| 438 | float alpha = RandomValue(0.0f, 2.0f*M_PI);
|
---|
[2001] | 439 | //float alpha = vr2.x*2.0f*M_PI;
|
---|
[1997] | 440 |
|
---|
| 441 | // direction along which we will mutate the ray
|
---|
| 442 | Vector3 line = sin(alpha)*U + cos(alpha)*V;
|
---|
| 443 |
|
---|
| 444 | // cout<<line<<endl;
|
---|
| 445 | // create 16 rays along the selected dir
|
---|
[2001] | 446 | int i;
|
---|
| 447 | float left = 0.0f;
|
---|
| 448 | float right = radius;
|
---|
[1997] | 449 | // cast rays to find silhouette ray
|
---|
[2001] | 450 | for (int j=0; j < Q_SEARCH_STEPS; j++) {
|
---|
| 451 | for (i=0; i < packetSize; i++) {
|
---|
| 452 | float r = left + (i+1)*(right-left)/(packetSize+1);
|
---|
| 453 | shifts[i] = r*line;
|
---|
| 454 | dirs[i] = Normalize(ray.mTermination + shifts[i] - origin );
|
---|
| 455 | mlrtaStoreRayASEye4(&origin.x,
|
---|
| 456 | &dirs[i].x,
|
---|
| 457 | i);
|
---|
[1997] | 458 | }
|
---|
[2001] | 459 |
|
---|
| 460 | mlrtaTraverseGroupASEye4(&box.Min().x,
|
---|
| 461 | &box.Max().x,
|
---|
| 462 | hit_triangles,
|
---|
| 463 | dist);
|
---|
| 464 |
|
---|
| 465 | for (i=0; i < packetSize; i++) {
|
---|
| 466 | if (hit_triangles[i] == -1) {
|
---|
| 467 | // if (hit_triangles[i] == -1 || !box.IsInside(origin + dist[i]*dirs[i])) {
|
---|
| 468 | // break on first passing ray
|
---|
| 469 | break;
|
---|
| 470 | }
|
---|
| 471 | }
|
---|
| 472 | float rr = left + (i+1)*(right-left)/(packetSize+1);
|
---|
| 473 | float rl = left + i*(right-left)/(packetSize+1);
|
---|
| 474 | left = rl;
|
---|
| 475 | right = rr;
|
---|
[1997] | 476 | }
|
---|
| 477 |
|
---|
| 478 | if (i == packetSize) {
|
---|
| 479 | // cerr<<"Warning: hit the same box here should never happen!"<<endl;
|
---|
| 480 | // shift the ray even a bit more
|
---|
[2001] | 481 | //cout<<"W"<<i<<endl;
|
---|
| 482 | // return (RandomValue(1.0f, 1.5f)*radius)*line;
|
---|
| 483 | return right*line;
|
---|
[1997] | 484 | }
|
---|
[2001] | 485 |
|
---|
[1997] | 486 | // cout<<i<<endl;
|
---|
[2001] | 487 | return shifts[i];
|
---|
[1997] | 488 | }
|
---|
| 489 |
|
---|
| 490 |
|
---|
[1991] | 491 | bool
|
---|
| 492 | MutationBasedDistribution::GenerateSample(SimpleRay &sray)
|
---|
| 493 | {
|
---|
| 494 |
|
---|
| 495 | if (mRays.size() == 0) {
|
---|
| 496 | float rr[5];
|
---|
| 497 | // use direction based distribution
|
---|
| 498 | Vector3 origin, direction;
|
---|
| 499 | static HaltonSequence halton;
|
---|
| 500 |
|
---|
| 501 | halton.GetNext(5, rr);
|
---|
| 502 | mPreprocessor.mViewCellsManager->GetViewPoint(origin,
|
---|
| 503 | Vector3(rr[0], rr[1], rr[2]));
|
---|
| 504 |
|
---|
| 505 |
|
---|
| 506 | direction = UniformRandomVector(rr[3], rr[4]);
|
---|
| 507 |
|
---|
| 508 | const float pdf = 1.0f;
|
---|
| 509 | sray = SimpleRay(origin, direction, MUTATION_BASED_DISTRIBUTION, pdf);
|
---|
| 510 | sray.mGeneratorId = -1;
|
---|
| 511 |
|
---|
| 512 | return true;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | int index;
|
---|
| 516 |
|
---|
| 517 | #if !MUTATION_USE_CDF
|
---|
| 518 | // get tail of the buffer
|
---|
| 519 | index = (mLastIndex+1)%mRays.size();
|
---|
| 520 | if (mRays[index].GetSamplingFactor() >
|
---|
| 521 | mRays[mLastIndex].GetSamplingFactor()) {
|
---|
| 522 | // search back for index where this is valid
|
---|
| 523 | index = (mLastIndex - 1 + mRays.size())%mRays.size();
|
---|
| 524 | for (int i=0; i < mRays.size(); i++) {
|
---|
| 525 |
|
---|
| 526 | // if (mRays[index].mMutations > mRays[mLastIndex].mMutations)
|
---|
| 527 | // break;
|
---|
| 528 | if (mRays[index].GetSamplingFactor() >
|
---|
| 529 | mRays[mLastIndex].GetSamplingFactor() )
|
---|
| 530 | break;
|
---|
| 531 | index = (index - 1 + mRays.size())%mRays.size();
|
---|
| 532 | }
|
---|
| 533 | // go one step back
|
---|
| 534 | index = (index+1)%mRays.size();
|
---|
| 535 | }
|
---|
| 536 | #else
|
---|
| 537 | static HaltonSequence iHalton;
|
---|
| 538 | iHalton.GetNext(1, rr);
|
---|
| 539 | //rr[0] = RandomValue(0,1);
|
---|
| 540 | // use binary search to find index with this cdf
|
---|
| 541 | int l=0, r=mRays.size()-1;
|
---|
| 542 | while(l<r) {
|
---|
| 543 | int i = (l+r)/2;
|
---|
| 544 | if (rr[0] < mRays[i].mCdf )
|
---|
| 545 | r = i;
|
---|
| 546 | else
|
---|
| 547 | l = i+1;
|
---|
| 548 | }
|
---|
| 549 | index = l;
|
---|
| 550 | // if (rr[0] >= mRays[r].mCdf)
|
---|
| 551 | // index = r;
|
---|
| 552 | // else
|
---|
| 553 | // index = l;
|
---|
| 554 |
|
---|
| 555 |
|
---|
| 556 | #endif
|
---|
| 557 | // cout<<index<<" "<<rr[0]<<" "<<mRays[index].mCdf<<" "<<mRays[(index+1)%mRays.size()].mCdf<<endl;
|
---|
| 558 |
|
---|
| 559 | mLastIndex = index;
|
---|
| 560 |
|
---|
[2001] | 561 | if (mRays[index].HasReverseMutation()) {
|
---|
| 562 | //cout<<"R "<<mRays[index].mutatedOrigin<<" "<<mRays[index].mutatedTermination<<endl;
|
---|
| 563 | sray = SimpleRay(mRays[index].mutatedOrigin,
|
---|
| 564 | Normalize(mRays[index].mutatedTermination - mRays[index].mutatedOrigin),
|
---|
| 565 | MUTATION_BASED_DISTRIBUTION, 1.0f);
|
---|
| 566 | sray.mGeneratorId = index;
|
---|
| 567 | mRays[index].ResetReverseMutation();
|
---|
| 568 | mRays[index].mMutations++;
|
---|
[2002] | 569 | mRays[index].mUnsuccessfulMutations++;
|
---|
| 570 |
|
---|
[2001] | 571 | return true;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[1991] | 574 | #if USE_SILHOUETTE_MUTATIONS
|
---|
| 575 | return GenerateSilhouetteMutation(index, sray);
|
---|
| 576 | #else
|
---|
| 577 | return GenerateMutation(index, sray);
|
---|
| 578 | #endif
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 |
|
---|
| 582 |
|
---|
| 583 |
|
---|
| 584 |
|
---|
| 585 | bool
|
---|
| 586 | MutationBasedDistribution::GenerateMutationCandidate(const int index,
|
---|
| 587 | SimpleRay &sray,
|
---|
| 588 | Intersectable *object,
|
---|
| 589 | const AxisAlignedBox3 &box
|
---|
| 590 | )
|
---|
| 591 | {
|
---|
| 592 | float rr[4];
|
---|
| 593 |
|
---|
| 594 | VssRay *ray = mRays[index].mRay;
|
---|
| 595 |
|
---|
| 596 | mRays[index].mHalton.GetNext(4, rr);
|
---|
[2001] | 597 | // rr[0] = RandomValue(0.0f,0.99999f);
|
---|
| 598 | // rr[1] = RandomValue(0.0f,0.99999f);
|
---|
| 599 | // rr[2] = RandomValue(0.0f,0.99999f);
|
---|
| 600 | // rr[3] = RandomValue(0.0f,0.99999f);
|
---|
[1991] | 601 |
|
---|
| 602 | // mutate the origin
|
---|
| 603 | Vector3 d = ray->GetDir();
|
---|
| 604 |
|
---|
| 605 | float objectRadius = 0.5f*Magnitude(box.Diagonal());
|
---|
| 606 | // cout<<objectRadius<<endl;
|
---|
| 607 | if (objectRadius < Limits::Small)
|
---|
| 608 | return false;
|
---|
| 609 |
|
---|
| 610 | // Compute right handed coordinate system from direction
|
---|
| 611 | Vector3 U, V;
|
---|
| 612 | Vector3 nd = Normalize(d);
|
---|
| 613 | nd.RightHandedBase(U, V);
|
---|
| 614 |
|
---|
| 615 | Vector3 origin = ray->mOrigin;
|
---|
| 616 | Vector3 termination = ray->mTermination; //box.Center(); //ray->mTermination; //box.Center();
|
---|
[1995] | 617 |
|
---|
| 618 | // optimal for Pompeii 0.1f
|
---|
| 619 | // optimal for Vienna 0.5f
|
---|
[1991] | 620 |
|
---|
[2002] | 621 | float radiusExtension = 0.1f;
|
---|
[1991] | 622 | // + mRays[index].mMutations/50.0f;
|
---|
[2001] | 623 |
|
---|
[1995] | 624 | float mutationRadius = objectRadius*radiusExtension;
|
---|
[2001] | 625 |
|
---|
| 626 | // tmp for pompeii
|
---|
| 627 | // mutationRadius = 0.22f;
|
---|
[1995] | 628 |
|
---|
[2001] | 629 | #if MUTATE_ORIGIN
|
---|
[1991] | 630 | origin += ComputeOriginMutation(*ray, U, V,
|
---|
| 631 | Vector2(rr[0], rr[1]),
|
---|
[1995] | 632 | mutationRadius);
|
---|
[2001] | 633 | #endif
|
---|
[1991] | 634 |
|
---|
[1997] | 635 | #if USE_SIL_TERMINATION_MUTATION
|
---|
| 636 | termination += ComputeSilhouetteTerminationMutation(*ray,
|
---|
| 637 | origin,
|
---|
| 638 | box,
|
---|
| 639 | U, V,
|
---|
| 640 | Vector2(rr[2], rr[3]),
|
---|
[2001] | 641 | 2.0f*objectRadius);
|
---|
[1997] | 642 | #else
|
---|
[1991] | 643 | termination += ComputeTerminationMutation(*ray, U, V,
|
---|
| 644 | Vector2(rr[2], rr[3]),
|
---|
[1995] | 645 | mutationRadius);
|
---|
[1997] | 646 | #endif
|
---|
[1991] | 647 | Vector3 direction = termination - origin;
|
---|
| 648 |
|
---|
| 649 | if (Magnitude(direction) < Limits::Small)
|
---|
| 650 | return false;
|
---|
| 651 |
|
---|
| 652 | // shift the origin a little bit
|
---|
| 653 | origin += direction*0.5f;
|
---|
| 654 |
|
---|
| 655 | direction.Normalize();
|
---|
| 656 |
|
---|
| 657 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 658 | const float pdf = 1.0f;
|
---|
| 659 |
|
---|
| 660 | sray = SimpleRay(origin, direction, MUTATION_BASED_DISTRIBUTION, pdf);
|
---|
| 661 | sray.mGeneratorId = index;
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | bool
|
---|
| 665 | MutationBasedDistribution::GenerateMutation(const int index, SimpleRay &sray)
|
---|
| 666 | {
|
---|
| 667 | VssRay *ray = mRays[index].mRay;
|
---|
| 668 |
|
---|
| 669 | Intersectable *object = mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 670 | *ray,
|
---|
| 671 | true);
|
---|
| 672 |
|
---|
| 673 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 674 |
|
---|
| 675 | if (GenerateMutationCandidate(index, sray, object, box)) {
|
---|
| 676 | mRays[index].mMutations++;
|
---|
[2002] | 677 | mRays[index].mUnsuccessfulMutations++;
|
---|
| 678 |
|
---|
[1991] | 679 | return true;
|
---|
| 680 | }
|
---|
| 681 | return false;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | bool
|
---|
| 685 | MutationBasedDistribution::GenerateSilhouetteMutation(const int index, SimpleRay &sray)
|
---|
| 686 | {
|
---|
| 687 | #ifndef GTP_INTERNAL
|
---|
| 688 | return GenerateMutation(index, sray);
|
---|
| 689 | #else
|
---|
| 690 | const int packetSize = 4;
|
---|
| 691 | const int maxTries = 8;
|
---|
| 692 |
|
---|
| 693 | static int hit_triangles[16];
|
---|
| 694 | static float dist[16];
|
---|
| 695 |
|
---|
| 696 | SimpleRay mutationCandidates[packetSize];
|
---|
| 697 | int candidates = 0;
|
---|
| 698 |
|
---|
| 699 | VssRay *ray = mRays[index].mRay;
|
---|
| 700 |
|
---|
| 701 | Intersectable *object = mPreprocessor.mViewCellsManager->GetIntersectable(
|
---|
| 702 | *ray,
|
---|
| 703 | true);
|
---|
| 704 |
|
---|
| 705 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 706 |
|
---|
| 707 | int id = 0;
|
---|
| 708 | int silhouetteRays = 0;
|
---|
| 709 | int tries = 0;
|
---|
| 710 | while (silhouetteRays == 0 && tries < maxTries) {
|
---|
| 711 | for (candidates = 0; candidates < packetSize && tries < maxTries; tries++)
|
---|
| 712 | if (GenerateMutationCandidate(index, mutationCandidates[candidates], object, box))
|
---|
| 713 | candidates++;
|
---|
| 714 |
|
---|
| 715 | if (candidates < packetSize)
|
---|
| 716 | break;
|
---|
| 717 |
|
---|
| 718 | // cout<<candidates<<endl;
|
---|
| 719 | // cast rays to find silhouette edge
|
---|
| 720 | for (int i=0; i < packetSize; i++)
|
---|
| 721 | mlrtaStoreRayAS4(&mutationCandidates[i].mOrigin.x,
|
---|
| 722 | &mutationCandidates[i].mDirection.x,
|
---|
| 723 | i);
|
---|
| 724 |
|
---|
| 725 | mlrtaTraverseGroupAS4(&box.Min().x,
|
---|
| 726 | &box.Max().x,
|
---|
| 727 | hit_triangles,
|
---|
| 728 | dist);
|
---|
| 729 |
|
---|
| 730 | for (int i=0; i < packetSize; i++)
|
---|
| 731 | if (hit_triangles[i] == -1) {
|
---|
| 732 | silhouetteRays++;
|
---|
| 733 | id = i;
|
---|
| 734 | break;
|
---|
| 735 | }
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 | if (candidates == 0)
|
---|
| 739 | return false;
|
---|
| 740 |
|
---|
| 741 | // cout<<id<<endl;
|
---|
| 742 | // cout<<tries<<endl;
|
---|
| 743 | sray = mutationCandidates[id];
|
---|
| 744 | mRays[index].mMutations++;
|
---|
[2002] | 745 | mRays[index].mUnsuccessfulMutations++;
|
---|
[1991] | 746 |
|
---|
| 747 | return true;
|
---|
| 748 | #endif
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 |
|
---|
| 752 |
|
---|
| 753 |
|
---|
| 754 | MutationBasedDistribution::MutationBasedDistribution(Preprocessor &preprocessor
|
---|
| 755 | ) :
|
---|
| 756 | SamplingStrategy(preprocessor)
|
---|
| 757 | {
|
---|
| 758 | mType = MUTATION_BASED_DISTRIBUTION;
|
---|
| 759 | mBufferStart = 0;
|
---|
| 760 | mMaxRays = 500000;
|
---|
| 761 | mRays.reserve(mMaxRays);
|
---|
| 762 | mOriginMutationSize = 10.0f;
|
---|
| 763 | mLastIndex = 0;
|
---|
| 764 | // mOriginMutationSize = Magnitude(preprocessor.mViewCellsManager->
|
---|
| 765 | // GetViewSpaceBox().Diagonal())*1e-3;
|
---|
| 766 |
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 |
|
---|
| 770 | }
|
---|