Ignore:
Timestamp:
11/03/05 19:31:56 (19 years ago)
Author:
bittner
Message:

vsspreprocessor kdtree meshkdtree optimization

File:
1 edited

Legend:

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

    r374 r376  
    77#include "Polygon3.h" 
    88#include "ViewCell.h" 
     9#include "VssRay.h" 
    910 
    1011VssPreprocessor::VssPreprocessor(): 
    1112        mPass(0), 
    12         mSampleRays(NULL) 
     13        mVssRays() 
    1314{ 
    1415  // this should increase coherence of the samples 
    15   environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass); 
    16   environment->GetIntValue("Sampling.totalSamples", mTotalSamples); 
    17   mKdPvsDepth = 100; 
     16  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass); 
     17  environment->GetIntValue("VssPreprocessor.totalSamples", mTotalSamples); 
    1818  mStats.open("stats.log"); 
    19  
    2019} 
    2120 
    2221VssPreprocessor::~VssPreprocessor() 
    2322{ 
    24         CLEAR_CONTAINER(mSampleRays); 
     23        CLEAR_CONTAINER(mVssRays); 
    2524} 
    2625 
     
    3332  ray.intersections.clear(); 
    3433        // do not store anything else then intersections at the ray 
    35         ray.kdLeaves.clear(); 
    36         ray.mFlags = Ray::STORE_KDLEAVES; 
    37          
    38   //  cout<<point<<" "<<direction<<endl; 
    3934  ray.Init(point, direction, Ray::LOCAL_RAY); 
    4035} 
    4136 
    42 KdNode * 
    43 VssPreprocessor::GetNodeForPvs(KdLeaf *leaf) 
     37VssRay *  
     38VssPreprocessor::CastRay( 
     39                                                                                                 Vector3 &viewPoint, 
     40                                                                                                 Vector3 &direction 
     41                                                                                                 ) 
    4442{ 
    45   KdNode *node = leaf; 
    46   while (node->mParent && node->mDepth > mKdPvsDepth) 
    47     node = node->mParent; 
    48   return node; 
     43        static Ray ray; 
     44        AxisAlignedBox3 box = mKdTree->GetBox(); 
     45         
     46        SetupRay(ray, viewPoint, direction); 
     47        // cast ray to KD tree to find intersection with other objects 
     48        Intersectable *objectA, *objectB; 
     49        Vector3 pointA, pointB; 
     50 
     51        if (mKdTree->CastRay(ray)) { 
     52                objectA = ray.intersections[0].mObject; 
     53                pointA = ray.Extrap(ray.intersections[0].mT); 
     54        } else { 
     55                objectA = NULL; 
     56                // compute intersection with the scene bounding box 
     57                float tmin, tmax; 
     58                box.ComputeMinMaxT(ray, &tmin, &tmax); 
     59                pointA = ray.Extrap(tmax); 
     60        } 
     61         
     62        SetupRay(ray, viewPoint, -direction); 
     63         
     64        if (mKdTree->CastRay(ray)) { 
     65                objectB = ray.intersections[0].mObject; 
     66          pointB = ray.Extrap(ray.intersections[0].mT); 
     67        } else { 
     68                objectB = NULL; 
     69                float tmin, tmax; 
     70                box.ComputeMinMaxT(ray, &tmin, &tmax); 
     71                pointB = ray.Extrap(tmax); 
     72        } 
     73 
     74        VssRay *vssRay  = NULL; 
     75         
     76        if (objectA || objectB) { 
     77                vssRay = new VssRay(pointA, 
     78                                                                                                pointB, 
     79                                                                                                objectA, 
     80                                                                                                objectB); 
     81        } 
     82         
     83        return vssRay; 
    4984} 
    5085 
    5186 
    52 int 
    53 VssPreprocessor::AddNodeSamples(const Ray &ray, 
    54                                                                                                                                 Intersectable *sObject, 
    55                                                                                                                                 Intersectable *tObject 
    56                                                                                                                                 ) 
     87Vector3 
     88VssPreprocessor::GetViewpoint() 
    5789{ 
    58   int contributingSamples = 0; 
    59   int j; 
    60         int objects = 0; 
    61         if (sObject) 
    62                 objects++; 
    63         if (tObject) 
    64                 objects++; 
     90        AxisAlignedBox3 box = mKdTree->GetBox(); 
     91 
     92        // shrink the box in the y direction 
     93        Vector3 diff(0, -box.Size().y*0.4f, 0); 
     94        box.Enlarge(diff); 
    6595         
    66         if (objects) { 
    67                 for (j=0; j < ray.kdLeaves.size(); j++) { 
    68                         KdNode *node = GetNodeForPvs( ray.kdLeaves[j] ); 
    69                         if (sObject) 
    70                                 contributingSamples += sObject->mKdPvs.AddSample(node); 
    71                         if (tObject) 
    72                                 contributingSamples += tObject->mKdPvs.AddSample(node); 
    73                 } 
    74         } 
    75          
    76         for (j=1; j < ((int)ray.kdLeaves.size() - 1); j++) { 
    77                 ray.kdLeaves[j]->AddPassingRay2(ray, 
    78                                                                                                                                                 objects, 
    79                                                                                                                                                 ray.kdLeaves.size() 
    80                                                                                                                                                 ); 
    81   } 
    82    
    83   return contributingSamples; 
     96        return box.GetRandomPoint(); 
     97} 
     98 
     99Vector3 
     100VssPreprocessor::GetDirection(const Vector3 &viewpoint) 
     101{ 
     102        int i = RandomValue(0, mObjects.size()); 
     103        Intersectable *object = mObjects[i]; 
     104        Vector3 point, normal; 
     105        object->GetRandomSurfacePoint(point, normal); 
     106        return point - viewpoint; 
    84107} 
    85108 
    86109 
    87  
    88  
    89 int 
    90 VssPreprocessor::CastRay(Intersectable *object, 
    91                                                                                                  Ray &ray 
    92                                                                                                  ) 
    93 { 
    94         int sampleContributions = 0; 
    95  
    96         long t1 = GetRealTime(); 
    97         // cast ray to KD tree to find intersection with other objects 
    98         mKdTree->CastRay(ray); 
    99         long t2 = GetRealTime(); 
    100  
    101         if (0 && object && object->GetId() > 2197) { 
    102                 object->Describe(cout)<<endl; 
    103                 cout<<ray<<endl; 
    104         } 
    105  
    106         if (ray.kdLeaves.size()) { 
    107                 Intersectable *terminator = 
    108                         ray.intersections.size() ? ray.intersections[0].mObject: NULL; 
    109                  
    110                 sampleContributions += AddNodeSamples(ray, 
    111                                                                                                                                                                         object, 
    112                                                                                                                                                                         terminator); 
    113         } 
    114          
    115          
    116         return sampleContributions; 
    117 } 
    118  
    119  
    120 KdNode * 
    121 VssPreprocessor::GetNodeToSample(Intersectable *object) 
    122 { 
    123         int pvsSize = object->mKdPvs.GetSize(); 
    124         KdNode *nodeToSample = NULL; 
    125          
    126         // just pickup a random node 
    127         nodeToSample = mKdTree->GetRandomLeaf(); 
    128         return nodeToSample; 
    129 } 
    130110 
    131111bool 
     
    133113{ 
    134114   
    135   // pickup an object 
    136   ObjectContainer objects; 
    137    
    138   mSceneGraph->CollectObjects(&objects); 
    139  
    140   Vector3 point, normal, direction; 
    141   Ray ray; 
     115  mSceneGraph->CollectObjects(&mObjects); 
     116         
    142117 
    143118  long startTime = GetTime(); 
     
    146121  int totalSamples = 0; 
    147122 
    148   int pvsOut = Min((int)objects.size(), 10); 
    149  
    150   vector<Ray> rays[10]; 
    151123 
    152124  while (totalSamples < mTotalSamples) { 
     
    156128                int index = 0; 
    157129                 
    158                 int reverseSamples = 0; 
     130                int sampleContributions; 
    159131                 
     132                 
     133                for (int k=0; k < mSamplesPerPass; k++) { 
     134 
     135                        Vector3 viewpoint = GetViewpoint(); 
     136                        Vector3 direction = GetDirection(viewpoint); 
     137 
     138                        VssRay *vssRay = CastRay(viewpoint, direction); 
    160139                         
    161                 //cout << "totalSamples: "  << totalSamples << endl; 
    162  
    163                 for (i = 0; i < objects.size(); i++) { 
    164                                                  
    165                         KdNode *nodeToSample = NULL; 
    166                         Intersectable *object = objects[i]; 
    167                  
    168                         int pvsSize = 0; 
    169                         if (ViewCell::sHierarchy == ViewCell::KD) 
    170                                 pvsSize = object->mKdPvs.GetSize(); 
    171                                                  
     140                        if (vssRay) { 
     141                                sampleContributions = vssRay->HitCount(); 
     142                                mVssRays.push_back(vssRay); 
     143                        } 
    172144                         
    173                          
    174                         int faceIndex = object->GetRandomSurfacePoint(point, normal); 
    175                          
    176                         bool viewcellSample = true; 
    177                         int sampleContributions; 
    178                         bool debug = false; //(object->GetId() >= 2199); 
    179                         if (viewcellSample) { 
    180                                 //mKdTree->GetRandomLeaf(Plane3(normal, point)); 
    181  
    182                                 nodeToSample = GetNodeToSample(object); 
    183  
    184                                 for (int k=0; k < mSamplesPerPass; k++) { 
    185                                         bool reverseSample = false; 
    186                                          
    187  
    188                                         if (nodeToSample) { 
    189                                                 AxisAlignedBox3 box = mKdTree->GetBox(nodeToSample); 
    190                                                 Vector3 pointToSample = box.GetRandomPoint(); 
    191                                                 //                                              pointToSample.y = 0.9*box.Min().y + 0.1*box.Max().y; 
    192                                                 if (object->GetRandomVisibleSurfacePoint( point, normal, pointToSample, 3 )) { 
    193                                                         direction = pointToSample - point; 
    194                                                 } else { 
    195                                                         reverseSamples++; 
    196                                                         reverseSample = true; 
    197                                                         direction = point - pointToSample; 
    198                                                         point = pointToSample; 
    199                                                 } 
    200                                         } 
    201                                         else { 
    202                                                 direction = UniformRandomVector(normal); 
    203                                         } 
    204                                          
    205                                         // construct a ray 
    206                                         SetupRay(ray, point, direction); 
    207                                          
    208                                         sampleContributions = CastRay(reverseSample ? NULL : object, ray); 
    209                                          
    210                                         //-- CORR matt: put block inside loop 
    211                                         if (sampleContributions) { 
    212                                                 passContributingSamples ++; 
    213                                                 passSampleContributions += sampleContributions; 
    214                                         } 
    215  
    216                                         if ( i < pvsOut ) 
    217                                                 rays[i].push_back(ray); 
    218                  
    219                                         if (!ray.intersections.empty()) { 
    220                                                 // check whether we can add this to the rays 
    221                                                 for (int j = 0; j < pvsOut; j++) { 
    222                                                         if (objects[j] == ray.intersections[0].mObject) { 
    223                                                                 rays[j].push_back(ray); 
    224                                                         } 
    225                                                 } 
    226                                         } 
    227                                         //------------------- 
    228                                 } 
    229                         } else { 
    230                                 // edge samples 
    231                                 // get random visible mesh 
    232                                 //                              object->GetRandomVisibleMesh(Plane3(normal, point)); 
     145                        //-- CORR matt: put block inside loop 
     146                        if (sampleContributions) { 
     147                                passContributingSamples ++; 
     148                                passSampleContributions += sampleContributions; 
    233149                        } 
    234          
    235                         // CORR matt: must add all samples 
    236                         passSamples += mSamplesPerPass; 
     150                        passSamples++; 
     151                        totalSamples++; 
    237152                } 
    238          
    239                 totalSamples += passSamples;  
    240                  
    241                 //    if (pass>10) 
    242                 //      HoleSamplingPass(); 
    243153     
    244154                mPass++; 
    245155 
    246156                int pvsSize = 0; 
    247          
    248                 for (i=0; i < objects.size(); i++) { 
    249                         Intersectable *object = objects[i]; 
    250                         pvsSize += object->mKdPvs.GetSize(); 
    251                 } 
    252                  
    253                  
    254157                float avgRayContrib = (passContributingSamples > 0) ?  
    255158                        passSampleContributions/(float)passContributingSamples : 0; 
    256  
     159                 
    257160                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
    258161                cout << "#TotalSamples=" << totalSamples/1000  
    259162                                 << "k   #SampleContributions=" << passSampleContributions << " ("  
    260163                                 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 
    261                                  << pvsSize/(float)objects.size() << endl  
    262                                  << "avg ray contrib=" << avgRayContrib << endl 
    263                                  << "reverse samples [%]" << reverseSamples/(float)passSamples*100.0f << endl; 
    264  
     164                                 << pvsSize/(float)mObjects.size() << endl  
     165                                 << "avg ray contrib=" << avgRayContrib << endl; 
     166                 
    265167                mStats << 
    266168                        "#Pass\n" <<mPass<<endl<< 
     
    269171                        "#SampleContributions\n" << passSampleContributions << endl <<  
    270172                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 
    271                         "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << 
     173                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl << 
    272174                        "#AvgRayContrib\n" << avgRayContrib << endl; 
    273175        } 
    274176         
    275177        cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 
    276    
    277          
    278         if (0) { 
    279                 Exporter *exporter = Exporter::GetExporter("ray-density.x3d");  
    280                 exporter->SetExportRayDensity(true); 
    281                 exporter->ExportKdTree(*mKdTree); 
     178        cout << "#totalRayStackSize=" << mVssRays.size() << endl; 
    282179 
    283                 delete exporter; 
    284         } 
    285    
    286         bool exportRays = false; 
    287         if (exportRays) { 
    288                 Exporter *exporter = NULL; 
    289                 exporter = Exporter::GetExporter("sample-rays.x3d"); 
    290                 exporter->SetWireframe(); 
    291                 exporter->ExportKdTree(*mKdTree); 
    292  
    293                 for (i=0; i < pvsOut; i++)  
    294                 exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); 
    295                 exporter->SetFilled(); 
    296                    
    297                 delete exporter; 
    298         } 
    299  
    300         //-- several visualizations and statistics 
    301         if (1) { 
    302                 for (int k=0; k < pvsOut; k++) { 
    303                         Intersectable *object = objects[k]; 
    304                         char s[64]; 
    305                         sprintf(s, "sample-pvs%04d.x3d", k); 
    306                         Exporter *exporter = Exporter::GetExporter(s); 
    307                         exporter->SetWireframe(); 
    308                          
    309                          
    310                         KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); 
    311                         Intersectable::NewMail(); 
    312                          
    313                         // avoid adding the object to the list 
    314                         object->Mail(); 
    315                         ObjectContainer visibleObjects; 
    316                          
    317                         for (; i != object->mKdPvs.mEntries.end(); i++)  
    318                                 { 
    319                                         KdNode *node = (*i).first; 
    320                                         exporter->ExportBox(mKdTree->GetBox(node)); 
    321                                         mKdTree->CollectObjects(node, visibleObjects); 
    322                                 } 
    323                          
    324                         exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0)); 
    325                         exporter->SetFilled(); 
    326                          
    327                         for (int j = 0; j < visibleObjects.size(); j++) 
    328                                 exporter->ExportIntersectable(visibleObjects[j]); 
    329                          
    330                          
    331                         Material m; 
    332                         m.mDiffuseColor = RgbColor(1, 0, 0); 
    333                         exporter->SetForcedMaterial(m); 
    334                         exporter->ExportIntersectable(object); 
    335                          
    336                         delete exporter; 
    337                 } 
    338   } 
    339    
    340180  return true; 
    341181} 
Note: See TracChangeset for help on using the changeset viewer.