Ignore:
Timestamp:
01/20/07 14:03:37 (17 years ago)
Author:
bittner
Message:

mutation updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp

    r1997 r2001  
    99#include "RndGauss.h" 
    1010#include "Mutation.h" 
     11#include "Exporter.h" 
    1112 
    1213#ifdef GTP_INTERNAL 
     
    2021 
    2122#define USE_SIL_TERMINATION_MUTATION 1 
     23#define MUTATE_ORIGIN 1 
    2224 
    2325#define EVALUATE_MUTATION_STATS 1 
     26 
     27#define Q_SEARCH_STEPS 2 
    2428 
    2529void 
     
    5155  int dummyNcMutations = 0; 
    5256  int dummyCMutations = 0; 
     57 
     58  int reverseCandidates = 0; 
    5359   
    5460  for (int i=0; i < vssRays.size(); i++) { 
     
    8692                *mRays[mBufferStart].mRay = *vssRays[i]; 
    8793                mRays[mBufferStart].mMutations = 0; 
     94                mRays[mBufferStart].ResetReverseMutation(); 
    8895                //              mRays[mBufferStart] = RayEntry(newRay); 
    8996                mBufferStart++; 
    9097                if (mBufferStart >= mMaxRays) 
    9198                  mBufferStart = 0; 
     99                 
     100 
    92101          } 
    93102        } else { 
    94 #if EVALUATE_MUTATION_STATS  
    95103          if (vssRays[i]->mDistribution == MUTATION_BASED_DISTRIBUTION && 
    96104                  vssRays[i]->mGeneratorId != -1 
    97105                  ) { 
     106                // check whether not to store a new backward mutation candidate 
     107                VssRay *oldRay = mRays[vssRays[i]->mGeneratorId].mRay; 
     108                VssRay *newRay = vssRays[i]; 
     109 
     110#define DIST_THRESHOLD 3.0f 
     111 
     112                Intersectable *oldObject = 
     113                  mPreprocessor.mViewCellsManager->GetIntersectable( 
     114                                                                                                                        *oldRay, 
     115                                                                                                                        true); 
     116                 
     117 
     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  
    98135                mutationRays++; 
    99  
     136                 
    100137                Intersectable *newObject = 
    101138                  mPreprocessor.mViewCellsManager->GetIntersectable( 
     
    103140                                                                                                                        true); 
    104141 
    105                 Intersectable *oldObject = 
    106                   mPreprocessor.mViewCellsManager->GetIntersectable( 
    107                                                                                                                         *mRays[vssRays[i]->mGeneratorId].mRay, 
    108                                                                                                                         true); 
    109142 
    110143                if (oldObject == newObject) 
    111144                  dummyNcMutations++; 
     145#endif 
    112146          } 
    113 #endif 
    114147        } 
    115148  } 
    116  
     149   
    117150  if (mutationRays) { 
    118151        cout<<"Mutated rays:"<<mutationRays<<endl; 
     
    121154        cout<<"Dummy NC mutations ratio:"<<100.0f*dummyNcMutations/(float)mutationRays<<"%"<<endl; 
    122155        cout<<"Dummy C mutations ratio:"<<100.0f*dummyCMutations/(float)mutationRays<<"%"<<endl; 
     156        cout<<"Reverse candidates:"<<reverseCandidates<<endl; 
    123157  } 
    124158 
     
    259293} 
    260294 
     295bool 
     296MutationBasedDistribution::ComputeReverseMutation( 
     297                                                                                                  const VssRay &oldRay, 
     298                                                                                                  const VssRay &newRay, 
     299                                                                                                  Vector3 &origin, 
     300                                                                                                  Vector3 &termination 
     301                                                                                                  ) 
     302{ 
     303  // first reconstruct the termination point 
     304  Vector3 oldDir = Normalize(oldRay.GetDir()); 
     305  Plane3 oldPlane(oldDir, oldRay.mTermination); 
     306   
     307  termination = oldPlane.FindIntersection(newRay.mOrigin, 
     308                                                                                  newRay.mTermination); 
     309   
     310  // now find the new origin of the ray by casting ray backward from the termination and termining 
     311  // silhouette point with respect to the occluding object (object containing the newRay termination) 
     312 
     313  Plane3 newPlane(oldDir, newRay.mTermination); 
     314 
     315  Vector3 oldPivot = newPlane.FindIntersection(oldRay.mOrigin, 
     316                                                                                           oldRay.mTermination); 
     317 
     318  Vector3 newPivot = newRay.mTermination; 
     319  Vector3 line = 2.0f*(oldPivot - newPivot); 
     320 
     321  Intersectable *occluder = mPreprocessor.mViewCellsManager->GetIntersectable( 
     322                                                                                                                                                          newRay, 
     323                                                                                                                                                          true); 
     324   
     325  AxisAlignedBox3 box = occluder->GetBox(); 
     326  box.Scale(2.0f); 
     327   
     328  const int packetSize = 4; 
     329  static int hit_triangles[packetSize]; 
     330  static float dist[packetSize]; 
     331  static Vector3 dirs[packetSize]; 
     332  static Vector3 shifts[packetSize]; 
     333  // now find the silhouette along the line 
     334  int i; 
     335  float left = 0.0f; 
     336  float right = 1.0f; 
     337  // cast rays to find silhouette ray 
     338  for (int j=0; j < Q_SEARCH_STEPS; j++) { 
     339        for (i=0; i < packetSize; i++) { 
     340          float r = left + (i+1)*(right-left)/(packetSize+1); 
     341          shifts[i] = r*line; 
     342          dirs[i] = Normalize(newPivot + shifts[i] - termination ); 
     343          mlrtaStoreRayASEye4(&termination.x, 
     344                                                  &dirs[i].x, 
     345                                                  i); 
     346        } 
     347         
     348        mlrtaTraverseGroupASEye4(&box.Min().x, 
     349                                                         &box.Max().x, 
     350                                                         hit_triangles, 
     351                                                         dist); 
     352         
     353        for (i=0; i < packetSize; i++) { 
     354          if (hit_triangles[i] == -1) { 
     355                // break on first passing ray 
     356                break; 
     357          } 
     358        } 
     359        float rr = left + (i+1)*(right-left)/(packetSize+1); 
     360        float rl = left + i*(right-left)/(packetSize+1); 
     361        left = rl; 
     362        right = rr; 
     363  } 
     364 
     365  float t = right; 
     366  if (right==1.0f) 
     367        return false; 
     368   
     369  if (i == packetSize) 
     370        origin = newPivot + right*line; 
     371  else 
     372        origin = newPivot + shifts[i]; 
     373 
     374  if (0) { 
     375         
     376        static VssRayContainer rRays; 
     377        static int counter = 0; 
     378        char filename[256]; 
     379 
     380        if (counter < 50) { 
     381          sprintf(filename, "reverse_rays_%03d.x3d", counter++); 
     382           
     383          VssRay tRay(origin, termination, NULL, NULL); 
     384          rRays.push_back((VssRay *)&oldRay); 
     385          rRays.push_back((VssRay *)&newRay); 
     386          rRays.push_back(&tRay); 
     387           
     388          Exporter *exporter = NULL; 
     389          exporter = Exporter::GetExporter(filename); 
     390           
     391          exporter->SetFilled(); 
     392           
     393          Intersectable *occludee = mPreprocessor.mViewCellsManager->GetIntersectable( 
     394                                                                                                                                                                  oldRay, 
     395                                                                                                                                                                  true); 
     396           
     397          exporter->SetForcedMaterial(RgbColor(0,0,1)); 
     398          exporter->ExportIntersectable(occluder); 
     399          exporter->SetForcedMaterial(RgbColor(0,1,0)); 
     400          exporter->ExportIntersectable(occludee); 
     401          exporter->ResetForcedMaterial(); 
     402           
     403          exporter->SetWireframe(); 
     404           
     405           
     406          exporter->ExportRays(rRays, RgbColor(1, 0, 0)); 
     407          delete exporter; 
     408          rRays.clear(); 
     409        } 
     410  } 
     411 
     412 
     413   
     414  return true; 
     415   
     416  // now the origin and termination is swapped compred to the generator ray 
     417  // swap(origin, termination);??? 
     418  // -> perhaps not neccessary as the reverse mutation wil only be used once! 
     419} 
    261420 
    262421Vector3 
     
    270429                                                                                                                                ) 
    271430{ 
    272   const int packetSize = 16; 
    273  
     431  const int packetSize = 4; 
    274432  static int hit_triangles[packetSize]; 
    275433  static float dist[packetSize]; 
    276434  static Vector3 dirs[packetSize]; 
     435  static Vector3 shifts[packetSize]; 
    277436  // mutate the  
    278437  float alpha = RandomValue(0.0f, 2.0f*M_PI); 
     438  //float alpha = vr2.x*2.0f*M_PI; 
    279439   
    280440  // direction along which we will mutate the ray 
     
    283443  //  cout<<line<<endl; 
    284444  // create 16 rays along the selected dir 
    285    
     445  int i; 
     446  float left = 0.0f; 
     447  float right = radius; 
    286448  // cast rays to find silhouette ray 
    287   for (int i=0; i < packetSize; i++) { 
    288         dirs[i] = Normalize(ray.mTermination + ((radius/(packetSize - i))*line) - origin ); 
    289         mlrtaStoreRayAS16(&origin.x, 
    290                                           &dirs[i].x, 
    291                                           i); 
    292   } 
    293    
    294   mlrtaTraverseGroupAS16(&box.Min().x, 
    295                                                  &box.Max().x, 
    296                                                  hit_triangles, 
    297                                                  dist); 
    298    
    299   for (int i=0; i < packetSize; i++) { 
    300         if (hit_triangles[i] == -1 || !box.IsInside(origin + dist[i]*dirs[i])) { 
    301           // break on first passing ray 
    302           break; 
     449  for (int j=0; j < Q_SEARCH_STEPS; j++) { 
     450        for (i=0; i < packetSize; i++) { 
     451          float r = left + (i+1)*(right-left)/(packetSize+1); 
     452          shifts[i] = r*line; 
     453          dirs[i] = Normalize(ray.mTermination + shifts[i] - origin ); 
     454          mlrtaStoreRayASEye4(&origin.x, 
     455                                                  &dirs[i].x, 
     456                                                  i); 
    303457        } 
     458         
     459        mlrtaTraverseGroupASEye4(&box.Min().x, 
     460                                                         &box.Max().x, 
     461                                                         hit_triangles, 
     462                                                         dist); 
     463         
     464        for (i=0; i < packetSize; i++) { 
     465          if (hit_triangles[i] == -1) { 
     466                //        if (hit_triangles[i] == -1 || !box.IsInside(origin + dist[i]*dirs[i])) { 
     467                // break on first passing ray 
     468                break; 
     469          } 
     470        } 
     471        float rr = left + (i+1)*(right-left)/(packetSize+1); 
     472        float rl = left + i*(right-left)/(packetSize+1); 
     473        left = rl; 
     474        right = rr; 
    304475  } 
    305476   
     
    307478        //      cerr<<"Warning: hit the same box here should never happen!"<<endl; 
    308479        // shift the ray even a bit more 
    309         //      cout<<"W"<<i<<endl; 
    310         return ray.mTermination + (RandomValue(1.0f, 2.0f)*radius)*line; 
    311   } 
    312  
     480        //cout<<"W"<<i<<endl; 
     481        //      return (RandomValue(1.0f, 1.5f)*radius)*line; 
     482        return right*line; 
     483  } 
     484   
    313485  //  cout<<i<<endl; 
    314   return dirs[i]; 
     486  return shifts[i]; 
    315487} 
    316488 
     
    386558  mLastIndex = index; 
    387559 
     560  if (mRays[index].HasReverseMutation()) { 
     561        //cout<<"R "<<mRays[index].mutatedOrigin<<" "<<mRays[index].mutatedTermination<<endl; 
     562        sray = SimpleRay(mRays[index].mutatedOrigin, 
     563                                         Normalize(mRays[index].mutatedTermination - mRays[index].mutatedOrigin), 
     564                                         MUTATION_BASED_DISTRIBUTION, 1.0f); 
     565        sray.mGeneratorId = index; 
     566        mRays[index].ResetReverseMutation(); 
     567        mRays[index].mMutations++; 
     568        return true; 
     569  } 
     570   
    388571#if USE_SILHOUETTE_MUTATIONS 
    389572  return GenerateSilhouetteMutation(index, sray); 
     
    391574  return GenerateMutation(index, sray); 
    392575#endif 
    393    
    394576} 
    395577 
     
    410592 
    411593  mRays[index].mHalton.GetNext(4, rr); 
     594  //  rr[0] = RandomValue(0.0f,0.99999f); 
     595  //  rr[1] = RandomValue(0.0f,0.99999f); 
     596  //  rr[2] = RandomValue(0.0f,0.99999f); 
     597  //  rr[3] = RandomValue(0.0f,0.99999f); 
    412598   
    413599  // mutate the origin 
     
    430616  // optimal for Vienna 0.5f 
    431617   
    432   float radiusExtension = 0.05f; 
     618  float radiusExtension = 0.5f; 
    433619  //  + mRays[index].mMutations/50.0f; 
    434  
     620   
    435621  float mutationRadius = objectRadius*radiusExtension; 
    436  
     622   
    437623  // tmp for pompeii 
    438   mutationRadius = 0.22f; 
    439    
     624  //  mutationRadius = 0.22f; 
     625 
     626#if MUTATE_ORIGIN 
    440627  origin += ComputeOriginMutation(*ray, U, V, 
    441628                                                                  Vector2(rr[0], rr[1]), 
    442629                                                                  mutationRadius); 
     630#endif 
    443631   
    444632#if USE_SIL_TERMINATION_MUTATION 
     
    448636                                                                                                          U, V, 
    449637                                                                                                          Vector2(rr[2], rr[3]), 
    450                                                                                                           3.0f*objectRadius); 
     638                                                                                                          2.0f*objectRadius); 
    451639#else 
    452640  termination += ComputeTerminationMutation(*ray, U, V, 
Note: See TracChangeset for help on using the changeset viewer.