Ignore:
Timestamp:
08/03/05 11:15:30 (19 years ago)
Author:
bittner
Message:

basic sampling strategies

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp

    r181 r191  
    44#include "X3dExporter.h" 
    55#include "Environment.h" 
     6#include "MutualVisibility.h" 
     7 
    68SamplingPreprocessor::SamplingPreprocessor() 
    79{ 
     
    3335 
    3436int 
    35 SamplingPreprocessor::AddNodeSamples(Intersectable *object, const Ray &ray) 
     37SamplingPreprocessor::AddNodeSamples(Intersectable *object, 
     38                                     const Ray &ray, 
     39                                     const int pass) 
    3640{ 
    3741  int contributingSamples = 0; 
    38  
    39   for (int j=0; j < ray.leaves.size(); j++) { 
     42  int j; 
     43  for (j=0; j < ray.leaves.size(); j++) { 
    4044    KdNode *node = GetNodeForPvs( ray.leaves[j] ); 
    4145    contributingSamples += object->mKdPvs.AddNodeSample(node); 
    4246  } 
    4347   
     48  if (pass > 10) 
     49    for (j=1; j < ray.leaves.size() - 1; j++) { 
     50      ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); 
     51    } 
     52   
    4453  return contributingSamples; 
     54} 
     55 
     56 
     57void 
     58SamplingPreprocessor::HoleSamplingPass() 
     59{ 
     60  vector<KdLeaf *> leaves; 
     61  mKdTree->CollectLeaves(leaves); 
     62   
     63  // go through all the leaves and evaluate their passing contribution 
     64  for (int i=0 ; i < leaves.size(); i++) { 
     65    KdLeaf *leaf = leaves[i]; 
     66    cout<<leaf->mPassingRays<<endl; 
     67  } 
    4568} 
    4669 
     
    6386  int totalSamples = 0; 
    6487 
    65  
    6688  int pvsOut = Min((int)objects.size(), 10); 
    6789   
     
    7395    int passSampleContributions = 0; 
    7496    int passSamples = 0; 
    75         int index = 0; 
    76     KdNode *visibleNode = NULL; 
     97    int index = 0; 
    7798 
    7899    for (i =0; i < objects.size(); i++) { 
    79100      KdNode *nodeToSample = NULL; 
    80101      Intersectable *object = objects[i]; 
    81  
     102       
    82103      int pvsSize = object->mKdPvs.GetSize(); 
    83  
    84        
    85     if (pvsSize) { 
    86                 // mail all nodes from the pvs 
    87                 Intersectable::NewMail(); 
    88                 KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    89                 for (; i != object->mKdPvs.mEntries.end(); i++) { 
    90                         KdNode *node = (*i).first; 
    91                         node->Mail(); 
    92                 } 
    93                 int maxTries = 2*pvsSize; 
    94          
    95                 for (int tries = 0; tries < 10; tries++) { 
    96                         index = RandomValue(0, pvsSize - 1); 
    97                         KdPvsData data; 
    98                         object->mKdPvs.GetData(index, visibleNode, data); 
    99                         nodeToSample = mKdTree->FindRandomNeighbor(visibleNode, true); 
    100                         if (nodeToSample) 
    101                         break; 
    102                 } 
    103         } 
    104  
    105     if (0 && pvsSize) { 
    106                 // mail all nodes from the pvs 
    107                 Intersectable::NewMail(); 
    108                 KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    109                 for (; i != object->mKdPvs.mEntries.end(); i++) { 
    110                         KdNode *node = (*i).first; 
    111                         node->Mail(); 
    112                 } 
    113          
    114                 vector<KdNode *> invisibleNeighbors; 
    115                 // get all neighbors of all PVS nodes 
    116                 i = object->mKdPvs.mEntries.begin(); 
    117                 for (; i != object->mKdPvs.mEntries.end(); i++) { 
    118                         KdNode *node = (*i).first; 
    119                         mKdTree->FindNeighbors(visibleNode, invisibleNeighbors, true); 
    120                 } 
    121  
    122                 KdPvsData data; 
    123                 KdNode *visibleNode; 
    124                 object->mKdPvs.GetData(index, visibleNode, data); 
    125                 nodeToSample = mKdTree->FindRandomNeighbor(visibleNode, true); 
    126                 if (nodeToSample) 
    127                         break; 
    128       } 
    129        
     104       
     105      if (0 && pvsSize) { 
     106        // mail all nodes from the pvs 
     107        Intersectable::NewMail(); 
     108        KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
     109        for (; i != object->mKdPvs.mEntries.end(); i++) { 
     110          KdNode *node = (*i).first; 
     111          node->Mail(); 
     112        } 
     113        int maxTries = 2*pvsSize; 
     114         
     115        for (int tries = 0; tries < 10; tries++) { 
     116          index = RandomValue(0, pvsSize - 1); 
     117          KdPvsData data; 
     118          KdNode *node; 
     119          object->mKdPvs.GetData(index, node, data); 
     120          nodeToSample = mKdTree->FindRandomNeighbor(node, true); 
     121          if (nodeToSample) 
     122            break; 
     123        } 
     124      } 
     125       
     126      if (0 && pvsSize && pass == 100 ) { 
     127        // mail all nodes from the pvs 
     128        Intersectable::NewMail(); 
     129        KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
     130        for (; i != object->mKdPvs.mEntries.end(); i++) { 
     131          KdNode *node = (*i).first; 
     132          node->Mail(); 
     133        } 
     134         
     135        vector<KdNode *> invisibleNeighbors; 
     136        // get all neighbors of all PVS nodes 
     137        i = object->mKdPvs.mEntries.begin(); 
     138        for (; i != object->mKdPvs.mEntries.end(); i++) { 
     139          KdNode *node = (*i).first; 
     140          mKdTree->FindNeighbors(node, invisibleNeighbors, true); 
     141          AxisAlignedBox3 box = object->GetBox(); 
     142          for (int j=0; j < invisibleNeighbors.size(); j++) { 
     143            int visibility = ComputeBoxVisibility(mKdTree, 
     144                                                  box, 
     145                                                  mKdTree->GetBox(invisibleNeighbors[j]), 
     146                                                  1.0f); 
     147             
     148          } 
     149           
     150         
     151          // now rank all the neighbors according to probability that a new 
     152          // sample creates some contribution 
     153        } 
     154      } 
    130155 
    131156      for (int k=0; k < mSamplesPerPass; k++) { 
    132                   object->GetRandomSurfacePoint(point, normal); 
    133                   if (nodeToSample) { 
    134                           int maxTries = 5; 
    135                           for (int tries = 0; tries < maxTries; tries++) { 
    136                                   direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point; 
    137                                   if (DotProd(direction, normal) > Limits::Small) 
    138                                         break; 
    139                           } 
     157        object->GetRandomSurfacePoint(point, normal); 
     158        if (nodeToSample) { 
     159          int maxTries = 5; 
     160          for (int tries = 0; tries < maxTries; tries++) { 
     161            direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point; 
     162            if (DotProd(direction, normal) > Limits::Small) 
     163              break; 
     164          } 
    140165                 
    141                           if (tries == maxTries) 
    142                                   direction = UniformRandomVector(normal); 
    143                   } else 
    144                           direction = UniformRandomVector(normal); 
    145          
    146                   // construct a ray 
    147                   SetupRay(ray, point, direction); 
    148                   mKdTree->CastRay(ray); 
    149  
    150                   if (i < pvsOut) 
    151                           rays[i].push_back(ray); 
    152          
    153                   int sampleContributions = 0; 
     166          if (tries == maxTries) 
     167            direction = UniformRandomVector(normal); 
     168        } else 
     169          direction = UniformRandomVector(normal); 
     170         
     171        // construct a ray 
     172        SetupRay(ray, point, direction); 
     173        mKdTree->CastRay(ray); 
     174 
     175        if (i < pvsOut) 
     176          rays[i].push_back(ray); 
     177         
     178        int sampleContributions = 0; 
    154179         
    155180                   
    156                   if (ray.leaves.size()) { 
    157                           sampleContributions += AddNodeSamples(object, ray); 
     181        if (ray.leaves.size()) { 
     182          sampleContributions += AddNodeSamples(object, ray, pass); 
    158183           
    159                           if (ray.intersections.size()) { 
    160                                   sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray); 
    161                                   // check whether we can add this to the rays 
    162                                   for (int j = 0; j < pvsOut; j++) { 
    163                                           if (objects[j] == ray.intersections[0].mObject) { 
    164                                                   rays[j].push_back(ray); 
    165                                           } 
    166                                   } 
    167                           } 
     184          if (ray.intersections.size()) { 
     185            sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); 
     186            // check whether we can add this to the rays 
     187            for (int j = 0; j < pvsOut; j++) { 
     188              if (objects[j] == ray.intersections[0].mObject) { 
     189                rays[j].push_back(ray); 
     190              } 
     191            } 
     192          } 
    168193                 
    169                           passSamples++; 
     194          passSamples++; 
    170195                         
    171                           if (sampleContributions) { 
    172                                   passContributingSamples++; 
    173                                   passSampleContributions += sampleContributions; 
    174                           } 
    175                   } 
    176           } 
    177         } 
     196          if (sampleContributions) { 
     197            passContributingSamples++; 
     198            passSampleContributions += sampleContributions; 
     199          } 
     200        } 
     201      } 
     202    } 
     203 
     204 
    178205    totalSamples += passSamples; 
     206 
     207    //    if (pass>10) 
     208    //      HoleSamplingPass(); 
     209     
     210 
    179211    pass++; 
    180212     
     
    193225      endl; 
    194226  } 
     227 
     228  int totalPvsSize = mKdTree->CollectLeafPvs(); 
     229  cout<<"#totalPvsSize="<<totalPvsSize<<endl; 
     230   
     231  //  HoleSamplingPass(); 
     232  if (1) { 
     233    Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); 
     234    exporter->SetExportRayDensity(true); 
     235    exporter->ExportKdTree(*mKdTree); 
     236    delete exporter; 
     237  } 
     238   
    195239  bool exportRays = false; 
    196240  if (exportRays) { 
     
    214258      KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    215259      Intersectable::NewMail(); 
    216         // avoid adding the object to the list 
     260      // avoid adding the object to the list 
    217261      object->Mail(); 
    218262      ObjectContainer visibleObjects; 
Note: See TracChangeset for help on using the changeset viewer.