Ignore:
Timestamp:
08/28/05 15:13:06 (19 years ago)
Author:
bittner
Message:
 
File:
1 edited

Legend:

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

    r245 r256  
    55#include "Environment.h" 
    66#include "MutualVisibility.h" 
     7#include "Polygon3.h" 
    78 
    89SamplingPreprocessor::SamplingPreprocessor() 
     
    3738int 
    3839SamplingPreprocessor::AddNodeSamples(Intersectable *object, 
    39                                      const Ray &ray, 
    40                                      const int pass) 
     40                                                                                                                                                 const Ray &ray 
     41                                                                                                                                                 ) 
    4142{ 
    4243  int contributingSamples = 0; 
     
    4748  } 
    4849   
    49   if (pass > 10) 
     50  if (mPass > 10) 
    5051    for (j=1; j < ray.leaves.size() - 1; j++) { 
    5152      ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); 
     
    6768    cout<<leaf->mPassingRays<<endl; 
    6869  } 
     70} 
     71 
     72 
     73int 
     74SamplingPreprocessor::CastRay(Intersectable *object, Ray &ray) 
     75{ 
     76        mKdTree->CastRay(ray); 
     77        int sampleContributions = 0; 
     78        if (ray.leaves.size()) { 
     79                sampleContributions += AddNodeSamples(object, ray); 
     80                 
     81                if (ray.intersections.size()) { 
     82                        sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray); 
     83                } 
     84        } 
     85        return sampleContributions; 
    6986} 
    7087 
     
    121138 
    122139 
     140void 
     141SamplingPreprocessor::CastEdgeSamples( 
     142                                                                                                                                                        Intersectable *object, 
     143                                                                                                                                                        const Vector3 &point, 
     144                                                                                                                                                        Mesh &mesh, 
     145                                                                                                                                                        const int samples 
     146                                                                                                                                                        ) 
     147{ 
     148        Ray ray; 
     149        int i; 
     150        for (i=0; i < samples; i++) { 
     151                // pickup a random face of each mesh  
     152                int face = RandomValue(0, mesh.mFaces.size()-1); 
     153                 
     154                Polygon3 poly(mesh.mFaces[face], &mesh); 
     155                poly.Scale(1.001); 
     156                // now extend a random edge of the face 
     157                int edge = RandomValue(0, poly.mVertices.size()-1); 
     158                float t = RandomValue(0.0f,1.0f); 
     159                Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)% 
     160                                                                                                                                                                                                                                                                                 poly.mVertices.size()]; 
     161          SetupRay(ray, point, target - point); 
     162          CastRay(object, ray); 
     163        } 
     164} 
    123165 
    124166bool 
     
    137179   
    138180  int i; 
    139   int pass = 0; 
    140181  int totalSamples = 0; 
    141182 
     
    144185  vector<Ray> rays[10]; 
    145186   
    146   while (totalSamples < mTotalSamples) 
    147     { 
    148       int passContributingSamples = 0; 
    149       int passSampleContributions = 0; 
    150       int passSamples = 0; 
    151       int index = 0; 
    152  
    153       for (i =0; i < objects.size(); i++) { 
    154         KdNode *nodeToSample = NULL; 
    155         Intersectable *object = objects[i]; 
     187  while (totalSamples < mTotalSamples) { 
     188                int passContributingSamples = 0; 
     189                int passSampleContributions = 0; 
     190                int passSamples = 0; 
     191                int index = 0; 
     192                 
     193                for (i =0; i < objects.size(); i++) { 
     194                        KdNode *nodeToSample = NULL; 
     195                        Intersectable *object = objects[i]; 
     196                         
     197                        int pvsSize = object->mKdPvs.GetSize(); 
     198                         
     199                         
     200                         
     201                        if (0 && pvsSize) { 
     202                                // mail all nodes from the pvs 
     203                                Intersectable::NewMail(); 
     204                                KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
     205                           
     206                                for (; i != object->mKdPvs.mEntries.end(); i++) { 
     207                                        KdNode *node = (*i).first; 
     208                                        node->Mail(); 
     209                                } 
     210                                 
     211                                int maxTries = 2*pvsSize; 
     212                                 
     213                                for (int tries = 0; tries < 10; tries++) { 
     214                                        index = RandomValue(0, pvsSize - 1); 
     215                                        KdPvsData data; 
     216                                        KdNode *node; 
     217                                        object->mKdPvs.GetData(index, node, data); 
     218                                        nodeToSample = mKdTree->FindRandomNeighbor(node, true); 
     219                                        if (nodeToSample) 
     220                                                break; 
     221                                } 
     222                        } 
     223                         
     224                        if (0 && pvsSize && mPass == 1000 ) { 
     225                                // mail all nodes from the pvs 
     226                                Intersectable::NewMail(); 
     227                                KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
     228                                for (; i != object->mKdPvs.mEntries.end(); i++) { 
     229                                        KdNode *node = (*i).first; 
     230                                        node->Mail(); 
     231                                } 
     232                                 
     233                                vector<KdNode *> invisibleNeighbors; 
     234                                // get all neighbors of all PVS nodes 
     235                                i = object->mKdPvs.mEntries.begin(); 
     236                                for (; i != object->mKdPvs.mEntries.end(); i++) { 
     237                                        KdNode *node = (*i).first; 
     238                                        mKdTree->FindNeighbors(node, invisibleNeighbors, true); 
     239                                        AxisAlignedBox3 box = object->GetBox(); 
     240                                        for (int j=0; j < invisibleNeighbors.size(); j++) { 
     241                                                int visibility = ComputeBoxVisibility(mSceneGraph, 
     242                                                                                                                                                                                                        mKdTree, 
     243                                                                                                                                                                                                        box, 
     244                                                                                                                                                                                                        mKdTree->GetBox(invisibleNeighbors[j]), 
     245                                                                                                                                                                                                        1e-6f); 
     246                                                //            exit(0); 
     247                                        } 
     248                                        // now rank all the neighbors according to probability that a new 
     249                                        // sample creates some contribution 
     250                                } 
     251                        } 
     252                         
     253                         
     254                        object->GetRandomSurfacePoint(point, normal); 
     255                        bool viewcellSample = true; 
     256                        int sampleContributions; 
     257                        if (viewcellSample) { 
     258                                nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point)); 
     259                                 
     260                                for (int k=0; k < mSamplesPerPass; k++) { 
     261                                         
     262                                        if (nodeToSample) { 
     263                                                int maxTries = 5; 
     264                                                 
     265                                                for (int tries = 0; tries < maxTries; tries++) { 
     266                                                        direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point; 
     267                                                         
     268                                                        if (DotProd(direction, normal) > Limits::Small) 
     269                                                                break; 
     270                                                } 
     271                                                 
     272                                                if (tries == maxTries) 
     273                                                        direction = UniformRandomVector(normal); 
     274                                        } 
     275                                        else { 
     276                                                direction = UniformRandomVector(normal); 
     277                                        } 
     278                                         
     279                                        // construct a ray 
     280                                        SetupRay(ray, point, direction); 
     281                                        sampleContributions = CastRay(object, ray); 
     282                                } 
     283                        } else { 
     284                                // edge samples 
     285                                // get random visible mesh 
     286                                //                              object->GetRandomVisibleMesh(Plane3(normal, point)); 
     287                                 
     288                                 
     289                        } 
     290                                 
     291                         
     292                        if ( i < pvsOut ) 
     293                                rays[i].push_back(ray); 
     294                         
     295                        if (ray.intersections.size()) { 
     296                                        // check whether we can add this to the rays 
     297                                for (int j = 0; j < pvsOut; j++) { 
     298                                        if (objects[j] == ray.intersections[0].mObject) { 
     299                                                rays[j].push_back(ray); 
     300                                        } 
     301                                } 
     302                        } 
     303                         
     304                        passSamples++; 
     305                         
     306                        if (sampleContributions) { 
     307                                passContributingSamples++; 
     308                                passSampleContributions += sampleContributions; 
     309                        } 
     310                } 
     311                 
     312                 
    156313         
    157         int pvsSize = object->mKdPvs.GetSize(); 
    158  
    159  
    160  
    161         if (0 && pvsSize) { 
    162           // mail all nodes from the pvs 
    163           Intersectable::NewMail(); 
    164           KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    165                            
    166           for (; i != object->mKdPvs.mEntries.end(); i++) { 
    167             KdNode *node = (*i).first; 
    168             node->Mail(); 
    169           } 
    170  
    171           int maxTries = 2*pvsSize; 
    172          
    173           for (int tries = 0; tries < 10; tries++) { 
    174             index = RandomValue(0, pvsSize - 1); 
    175             KdPvsData data; 
    176             KdNode *node; 
    177             object->mKdPvs.GetData(index, node, data); 
    178             nodeToSample = mKdTree->FindRandomNeighbor(node, true); 
    179             if (nodeToSample) 
    180               break; 
    181           } 
     314                totalSamples += passSamples; 
     315                 
     316                //    if (pass>10) 
     317                //      HoleSamplingPass(); 
     318     
     319                 
     320                mPass++; 
     321     
     322                int pvsSize = 0; 
     323                for (i=0; i < objects.size(); i++) { 
     324                        Intersectable *object = objects[i]; 
     325                        pvsSize += object->mKdPvs.GetSize(); 
     326                } 
     327     
     328                cout << "#Pass " << mPass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     329                cout << "#TotalSamples=" << totalSamples/1000  
     330                                 << "k   #SampleContributions=" << passSampleContributions << " ("  
     331                                 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 
     332                                 << pvsSize/(float)objects.size() << endl  
     333                                 << "avg ray contrib=" << passSampleContributions/(float)passContributingSamples << endl; 
     334                 
     335                 
     336                mStats << 
     337                        "#Pass\n" <<mPass<<endl<< 
     338                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 
     339                        "#TotalSamples\n" << totalSamples<< endl<< 
     340                        "#SampleContributions\n" << passSampleContributions << endl <<  
     341                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 
     342                        "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << 
     343                        "#AvgRayContrib\n" << passSampleContributions/(float)passContributingSamples << endl; 
    182344        } 
    183345         
    184         if (0 && pvsSize && pass == 1000 ) { 
    185           // mail all nodes from the pvs 
    186           Intersectable::NewMail(); 
    187           KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    188           for (; i != object->mKdPvs.mEntries.end(); i++) { 
    189             KdNode *node = (*i).first; 
    190             node->Mail(); 
    191           } 
    192          
    193           vector<KdNode *> invisibleNeighbors; 
    194           // get all neighbors of all PVS nodes 
    195           i = object->mKdPvs.mEntries.begin(); 
    196           for (; i != object->mKdPvs.mEntries.end(); i++) { 
    197             KdNode *node = (*i).first; 
    198             mKdTree->FindNeighbors(node, invisibleNeighbors, true); 
    199             AxisAlignedBox3 box = object->GetBox(); 
    200             for (int j=0; j < invisibleNeighbors.size(); j++) { 
    201               int visibility = ComputeBoxVisibility(mSceneGraph, 
    202                                                     mKdTree, 
    203                                                     box, 
    204                                                     mKdTree->GetBox(invisibleNeighbors[j]), 
    205                                                     1e-6f); 
    206               //              exit(0); 
    207             } 
    208             // now rank all the neighbors according to probability that a new 
    209             // sample creates some contribution 
    210           } 
    211         } 
    212  
    213         object->GetRandomSurfacePoint(point, normal); 
    214         nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point)); 
    215  
    216         for (int k=0; k < mSamplesPerPass; k++) { 
    217            
    218           if (nodeToSample) { 
    219             int maxTries = 5; 
    220              
    221             for (int tries = 0; tries < maxTries; tries++) { 
    222               direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point; 
    223                
    224               if (DotProd(direction, normal) > Limits::Small) 
    225                 break; 
    226             } 
    227              
    228             if (tries == maxTries) 
    229               direction = UniformRandomVector(normal); 
    230           } 
    231           else { 
    232             direction = UniformRandomVector(normal); 
    233           } 
    234            
    235           // construct a ray 
    236           SetupRay(ray, point, direction); 
    237           mKdTree->CastRay(ray); 
    238            
    239           if ( i < pvsOut ) 
    240             rays[i].push_back(ray); 
    241            
    242           int sampleContributions = 0; 
    243            
    244           if (ray.leaves.size()) { 
    245             sampleContributions += AddNodeSamples(object, ray, pass); 
    246              
    247             if (ray.intersections.size()) { 
    248               sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); 
    249               // check whether we can add this to the rays 
    250               for (int j = 0; j < pvsOut; j++) { 
    251                 if (objects[j] == ray.intersections[0].mObject) { 
    252                   rays[j].push_back(ray); 
    253                 } 
    254               } 
    255             } 
    256  
    257             passSamples++; 
    258                          
    259             if (sampleContributions) { 
    260               passContributingSamples++; 
    261               passSampleContributions += sampleContributions; 
    262             } 
    263           } 
    264         } 
    265       } 
    266  
    267       totalSamples += passSamples; 
    268  
    269       //    if (pass>10) 
    270       //      HoleSamplingPass(); 
    271      
    272  
    273       pass++; 
    274      
    275       int pvsSize = 0; 
    276       for (i=0; i < objects.size(); i++) { 
    277         Intersectable *object = objects[i]; 
    278         pvsSize += object->mKdPvs.GetSize(); 
    279       } 
    280      
    281       cout << "#Pass " << pass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
    282       cout << "#TotalSamples=" << totalSamples/1000  
    283            << "k   #SampleContributions=" << passSampleContributions << " ("  
    284            << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 
    285            << pvsSize/(float)objects.size() << endl  
    286            << "avg ray contrib=" << passSampleContributions/(float)passContributingSamples << endl; 
    287  
    288  
    289       mStats << 
    290         "#Pass\n" <<pass<<endl<< 
    291         "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 
    292         "#TotalSamples\n" << totalSamples<< endl<< 
    293         "#SampleContributions\n" << passSampleContributions << endl <<  
    294         "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 
    295         "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << 
    296         "#AvgRayContrib\n" << passSampleContributions/(float)passContributingSamples << endl; 
    297     } 
    298  
    299346  int totalPvsSize = mKdTree->CollectLeafPvs(); 
    300347  cout << "#totalPvsSize=" << totalPvsSize << endl; 
Note: See TracChangeset for help on using the changeset viewer.