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