Ignore:
Timestamp:
01/03/07 01:36:35 (17 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r1928 r1932  
    4646        Debug << "eps: " << mEps << endl; 
    4747 
    48         mStats.open("gvspreprocessor.log"); 
     48        mGvsStats.open("gvspreprocessor.log"); 
    4949} 
    5050 
     
    5454                                                                                 const VssRay &oldRay) 
    5555{ 
    56         const float dist = Magnitude(oldRay.GetDir()); 
    57         const float newDist = Magnitude(currentRay.GetDir()); 
    58   
    59 #if 0 
    60         if ((dist - newDist) > mThresHold) 
    61 #else  
    62         // rather take relative distance 
    63         if ((dist / newDist) > mThreshold) 
    64 #endif 
    65         { 
     56        // the predicted hitpoint: we expect to hit the same mesh again 
     57        const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay); 
     58 
     59        const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin); 
     60        const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin); 
     61         
     62        // distance large => this is likely to be a discontinuity 
     63        if ((predictedLen - len) > mThreshold) 
     64        { 
     65                cout << "d"; 
    6666                VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay); 
     67                 
    6768                // set flag for visualization 
    6869                newRay->mFlags |= VssRay::ReverseSample; 
    6970                 
    70                 // ray is not pushed into the queue => can delete ray 
     71                // if ray is not further processed => can delete ray 
    7172                if (!HandleRay(newRay)) 
    7273                        delete newRay; 
     
    8182bool GvsPreprocessor::HandleRay(VssRay *vssRay) 
    8283{ 
     84        // compute the contribution to the view cells 
    8385        const bool storeRaysForViz = true; 
    84         mViewCellsManager->ComputeSampleContribution(*vssRay, true, storeRaysForViz); 
    85                  
     86        mViewCellsManager->ComputeSampleContribution(*vssRay,  
     87                                                                                                 true,  
     88                                                                                                 storeRaysForViz); 
     89 
    8690        // some pvs contribution for this ray? 
    8791        if (vssRay->mPvsContribution > 0) 
     
    9296                if (storeRaysForViz) 
    9397                { 
     98                        VssRay *nray = new VssRay(*vssRay); 
     99                        nray->mFlags = vssRay->mFlags; 
     100 
     101                        // store ray in contributing view cell 
    94102                        ViewCellContainer::const_iterator vit, vit_end = vssRay->mViewCells.end(); 
    95103                        for (vit = vssRay->mViewCells.begin(); vit != vit_end; ++ vit) 
    96                         { 
    97                                 VssRay *nray = new VssRay(*vssRay); 
     104                        {                        
    98105                                (*vit)->GetOrCreateRays()->push_back(nray);                              
    99106                        } 
    100107                } 
     108 
    101109                //mVssRays.push_back(new VssRay(*vssRay)); 
    102110        ++ mSampleContriPerPass; 
     
    111119/** Creates 3 new vertices for triangle vertex with specified index. 
    112120*/ 
    113 static void CreateNewVertices(VertexContainer &vertices, 
    114                                                           const Triangle3 &hitTriangle, 
    115                                                           const VssRay &ray,  
    116                                                           const int index, 
    117                                                           const float eps) 
     121void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices, 
     122                                                                                          const Triangle3 &hitTriangle, 
     123                                                                                          const VssRay &ray,  
     124                                                                                          const int index) const 
    118125{ 
    119126        const int indexU = (index + 1) % 3; 
     
    125132         
    126133        const float len = Magnitude(a); 
    127  
     134         
    128135        const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi)); 
    129136        const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1)) 
     
    133140        // compute the new three hit points 
    134141        // pi, i + 1:  pi+ e·|pi-xp|·di, j 
    135         const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1; 
     142        const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1; 
    136143        // pi, i - 1:  pi+ e·|pi-xp|·di, j 
    137     const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2; 
     144    const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2; 
    138145        // pi, i:  pi+ e·|pi-xp|·di, j 
    139         const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3; 
     146        const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3; 
    140147         
    141148        vertices.push_back(pt2); 
     
    147154void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices, 
    148155                                                                          const Triangle3 &hitTriangle, 
    149                                                                           const VssRay &ray) 
    150 { 
    151         CreateNewVertices(vertices, hitTriangle, ray, 0, mEps); 
    152         CreateNewVertices(vertices, hitTriangle, ray, 1, mEps); 
    153         CreateNewVertices(vertices, hitTriangle, ray, 2, mEps); 
    154 } 
    155  
    156  
    157 static Vector3 CalcPredictedHitPoint(const VssRay &newRay,  
    158                                                                          const Triangle3 &hitTriangle, 
    159                                                                          const VssRay &oldRay) 
    160 { 
     156                                                                          const VssRay &ray) const 
     157{ 
     158        CreateDisplacedVertices(vertices, hitTriangle, ray, 0); 
     159        CreateDisplacedVertices(vertices, hitTriangle, ray, 1); 
     160        CreateDisplacedVertices(vertices, hitTriangle, ray, 2); 
     161} 
     162 
     163 
     164Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,  
     165                                                                                           const Triangle3 &hitTriangle, 
     166                                                                                           const VssRay &oldRay) const 
     167{ 
     168        // find the intersection of the plane induced by the  
     169        // hit triangle with the new ray 
    161170        Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]); 
    162171 
     
    177186                                                                   const Vector3 &p1,  
    178187                                                                   const Vector3 &p2,  
    179                                                                    const VssRay &x,  
    180                                                                    const VssRay &y, 
     188                                                                   const VssRay &ray1,  
     189                                                                   const VssRay &ray2, 
    181190                                                                   const VssRay &oldRay) 
    182191{ 
    183         // the predicted hitpoint expects to hit the same mesh again 
    184         const Vector3 predictedHitX = CalcPredictedHitPoint(x, hitTriangle, oldRay); 
    185         const Vector3 predictedHitY = CalcPredictedHitPoint(y, hitTriangle, oldRay); 
    186  
    187         CheckDiscontinuity(x, hitTriangle, oldRay); 
    188         CheckDiscontinuity(y, hitTriangle, oldRay); 
    189  
    190         if (EqualVisibility(x, y)) 
     192        CheckDiscontinuity(ray1, hitTriangle, oldRay); 
     193        CheckDiscontinuity(ray2, hitTriangle, oldRay); 
     194 
     195        if (EqualVisibility(ray1, ray2) || (Magnitude(p1 - p2) <= mEps)) 
    191196        { 
    192197                return 0; 
     
    194199        else 
    195200        { 
    196                 cout << "s"; 
     201                // the new subdivision point 
    197202                const Vector3 p = (p1 + p2) * 0.5f; 
     203         
     204                //cout << "tobj " << ray1.mTerminationObject << " " << ray2.mTerminationObject << " " << p1 << " " << p2 << endl; 
     205                //cout << "term " << ray1.mTermination << " " << ray2.mTermination << endl; 
     206 
     207                // cast ray into the new point 
    198208                SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f); 
    199209         
    200                 // cast ray into the new subdivision point 
    201                 VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), false); 
    202                  
     210                VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox()); 
     211 
    203212                if (!newRay) return 0; 
    204213 
     
    209218                 
    210219                // subdivide further 
    211                 const int s1 = SubdivideEdge(hitTriangle, p1, p, x, *newRay, oldRay); 
    212                 const int s2 = SubdivideEdge(hitTriangle, p, p2, *newRay, y, oldRay); 
    213                                  
     220                const int samples1 = SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay); 
     221                const int samples2 = SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay); 
     222                         
     223                // this ray will not be further processed 
    214224                if (!enqueued) 
    215225                        delete newRay; 
    216226                 
    217                 return s1 + s2 + 1; 
     227                return samples1 + samples2 + 1; 
    218228        } 
    219229} 
     
    222232int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &currentRay) 
    223233{ 
    224         cout << "a"; 
    225234        Intersectable *tObj = currentRay.mTerminationObject; 
    226235        Triangle3 hitTriangle; 
     
    245254        simpleRays.reserve(9); 
    246255 
     256        //cout << "currentRay: " << currentRay.mOrigin << " dir: " << currentRay.GetDir() << endl; 
     257 
    247258        VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end(); 
    248259 
     
    252263                SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f); 
    253264                simpleRays.AddRay(sr); 
    254         } 
    255  
    256         // visualize enlarged triangles 
     265 
     266                //cout << "new: " << sr.mOrigin << " dist: " << sr.mDirection << endl; 
     267        } 
     268 
    257269        if (0) 
    258270        { 
     271                // visualize enlarged triangles 
    259272                VizStruct dummy; 
    260273                dummy.enlargedTriangle = new Polygon3(enlargedTriangle); 
    261274                dummy.originalTriangle = hitTriangle; 
    262                 //dummy.ray = new VssRay(currentRay); 
    263275                vizContainer.push_back(dummy); 
    264276        } 
     
    266278        // cast rays to triangle vertices and determine visibility 
    267279        VssRayContainer vssRays; 
    268         CastRays(simpleRays, vssRays, false, false); 
     280 
     281        // don't cast double rays as we need only the forward rays 
     282        const bool castDoubleRays = false; 
     283        // cannot prune invalid rays because we have to  
     284        // compare adjacent  rays. 
     285        const bool pruneInvalidRays = false; 
     286 
     287        CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays); 
    269288 
    270289        // set flags 
     
    275294        } 
    276295 
    277         // add to ray queue 
     296        // handle rays 
    278297        EnqueueRays(vssRays); 
    279298         
     
    282301 
    283302    // recursivly subdivide each edge 
    284         for (int i = 0; 1 && (i < n); ++ i) 
    285         { 
    286                 castRays += SubdivideEdge( 
    287                         hitTriangle, 
    288                         enlargedTriangle[i],  
    289                         enlargedTriangle[(i + 1) % n],  
    290                         *vssRays[i],  
    291                         *vssRays[(i + 1) % n], 
    292                         currentRay); 
     303        for (int i = 0; i < n; ++ i) 
     304        { 
     305                castRays += SubdivideEdge(hitTriangle, 
     306                                                                  enlargedTriangle[i],  
     307                                                                  enlargedTriangle[(i + 1) % n],  
     308                                                                  *vssRays[i],  
     309                                                                  *vssRays[(i + 1) % n], 
     310                                                                  currentRay); 
    293311        } 
    294312 
    295313        mBorderSamples += castRays; 
     314 
    296315        return castRays; 
    297316} 
    298317 
    299318 
    300 static Vector3 GetPassingPoint(const VssRay &currentRay, 
    301                                                           const Triangle3 &hitTriangle, 
    302                                                            const VssRay &oldRay) 
    303 { 
    304         // intersect triangle plane with plane spanned by current samples 
     319Vector3 GvsPreprocessor::GetPassingPoint(const VssRay &currentRay, 
     320                                                                                const Triangle3 &hitTriangle, 
     321                                                                                 const VssRay &oldRay) const 
     322{ 
     323        //-- intersect triangle plane with plane spanned by current samples 
    305324        Plane3 plane(currentRay.GetOrigin(), currentRay.GetTermination(), oldRay.GetTermination()); 
    306325        Plane3 triPlane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]); 
     
    310329        // Evaluate new hitpoint just outside the triangle 
    311330        const float factor = 0.95f; 
    312         float t = triPlane.FindT(intersectLine); 
     331        const float t = triPlane.FindT(intersectLine); 
    313332        const Vector3 newPoint = intersectLine.mOrigin + t * factor * intersectLine.mDirection; 
    314333 
     
    329348        const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay); 
    330349 
    331         //-- Construct the mutated ray with xnew,dir = predicted(x)- pnew  
    332         //-- as direction vector 
     350        //-- Construct the mutated ray with xnew, 
     351        //-- dir = predicted(x)- pnew as direction vector 
    333352        const Vector3 newDir = predicted - newPoint ; 
     353 
    334354        // take xnew,p = intersect(viewcell, line(pnew, predicted(x)) as origin ? 
    335355        // difficult to say!! 
     
    438458        else if (0) 
    439459        {        
    440                 //-- load view cells from file 
    441460                //-- test successful view cells loading by exporting them again 
    442461                VssRayContainer dummies; 
     
    451470                //////// 
    452471                //-- stats 
     472 
    453473                cout << "\nPass " << mPass << " #samples: " << castSamples << " of " << mTotalSamples << endl; 
    454  
    455                 //mVssRays.PrintStatistics(mStats); 
    456                 mStats  
     474                //mVssRays.PrintStatistics(mGvsStats); 
     475                mGvsStats  
    457476                        << "#Pass\n" << mPass << endl 
    458477                        << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl 
     
    464483                        << "#GvsRuns\n" << mGvsPass << endl; 
    465484 
    466                 mViewCellsManager->PrintPvsStatistics(mStats); 
     485                mViewCellsManager->PrintPvsStatistics(mGvsStats); 
    467486 
    468487                char str[64]; sprintf(str, "tmp/pass%04d-", mPass); 
     
    489508        } 
    490509 
    491         cout << 2 * castSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl; 
     510        cout << "cast " << 2 * castSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl; 
    492511        Visualize(); 
    493512 
Note: See TracChangeset for help on using the changeset viewer.