Changeset 2739


Ignore:
Timestamp:
06/05/08 02:49:49 (16 years ago)
Author:
mattausch
Message:

worked on randomized sampling

Location:
GTP/trunk/Lib/Vis/Preprocessing/src
Files:
2 edited

Legend:

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

    r2738 r2739  
    176176        // discrepancy between predicted and actual hit point large => this is likely to be a discontinuity 
    177177 
    178         // matt: problem: this does only find CLOSER discontinuities, but not discontinuities which are FARTHER away 
     178        // matt: problem: this only finds CLOSER discontinuities, but not discontinuities which are FARTHER away 
    179179        if ((predictedLen - len) < mThreshold) 
    180         //if ((fabs(predictedLen - len) < mThreshold) 
    181180        // q: rather use relative distance? 
    182181        //if ((predictedLen / len) < mThreshold) 
     
    208207                        CastRayBundle16(simpleRay, reverseRays, scale); 
    209208        } 
     209 
     210        mGvsStats.mReverseSamples += (int)reverseRays.size(); 
     211 
     212        EnqueueRays(reverseRays); 
     213 
     214        return (int)reverseRays.size(); 
     215} 
     216 
     217 
     218int GvsPreprocessor::CheckDiscontinuity2(const VssRay &currentRay, 
     219                                                                                 const Triangle3 &hitTriangle, 
     220                                                                                 const VssRay &oldRay) 
     221{ 
     222        // the predicted hitpoint is the point where the ray would have intersected the hit triangle 
     223        Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay); 
     224 
     225        float predictedLen = Magnitude(predictedHit - currentRay.mOrigin); 
     226        float len = Magnitude(currentRay.mTermination - currentRay.mOrigin); 
     227         
     228        // discrepancy between predicted and actual hit point large  
     229        // => this is likely to be a discontinuity 
     230        if (fabs(predictedLen - len) < mThreshold) 
     231                return 0; 
     232 
     233        static SimpleRayContainer simpleRays; 
     234        simpleRays.clear(); 
     235 
     236        CreateRandomizedReverseRays(currentRay.mOrigin, predictedHit, hitTriangle, simpleRays, 16); 
     237 
     238        static VssRayContainer reverseRays; 
     239        reverseRays.clear(); 
     240        CastRays(simpleRays, reverseRays, false, false); 
    210241 
    211242        mGvsStats.mReverseSamples += (int)reverseRays.size(); 
     
    487518        } 
    488519 
     520        ////////// 
     521        //-- fill up simple rays with random rays so we can cast 16 rays at a time 
     522 
     523        const int numRandomRays = 16 - (int)simpleRays.size(); 
     524        GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples); 
     525 
     526        generationTimer.Exit(); 
     527 
     528 
     529        ///////////////////// 
     530        //-- cast rays to vertices and determine visibility 
     531         
     532        static VssRayContainer vssRays; 
     533        vssRays.clear(); 
     534 
     535        // for the original implementation we don't cast double rays 
     536        const bool castDoubleRays = !mPerViewCell; 
     537        // cannot prune invalid rays because we have to compare adjacent rays 
     538        const bool pruneInvalidRays = false; 
     539 
     540        //gvsTimer.Entry(); 
     541        castTimer.Entry(); 
     542 
     543        CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays); 
     544         
     545        castTimer.Exit(); 
     546        //gvsTimer.Exit(); 
     547 
     548        const int numBorderSamples = (int)vssRays.size() - numRandomRays; 
     549        int castRays = (int)simpleRays.size(); 
     550 
     551        // handle cast rays 
     552        EnqueueRays(vssRays); 
     553 
    489554        if (0) 
    490555        { 
     
    493558                dummy.enlargedTriangle = new Polygon3(enlargedTriangle); 
    494559                dummy.originalTriangle = hitTriangle; 
     560         
    495561                vizContainer.push_back(dummy); 
    496         } 
    497  
    498          
    499         ////////// 
    500         //-- fill up simple rays with random rays so we can cast 16 rays at a time 
    501  
    502         const int numRandomRays = 16 - (int)simpleRays.size(); 
    503  
    504         if (!mUseDeterministicGvs) 
    505         { 
    506                 if (0) 
    507                 { 
    508                         SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(),  
    509                                                           currentRay.GetNormalizedDir(),  
    510                                                                                   SamplingStrategy::GVS,  
    511                                                                                   1.0f); 
    512                          
    513                         GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0, 0.01f); 
    514                 } 
    515                 else 
    516                         GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays); 
    517         } 
    518         else 
    519                 GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples); 
    520  
    521         generationTimer.Exit(); 
    522  
    523  
    524         ///////////////////// 
    525         //-- cast rays to vertices and determine visibility 
    526          
    527         static VssRayContainer vssRays; 
    528         vssRays.clear(); 
    529  
    530         // for the original implementation we don't cast double rays 
    531         const bool castDoubleRays = !mPerViewCell; 
    532         // cannot prune invalid rays because we have to compare adjacent rays 
    533         const bool pruneInvalidRays = false; 
    534  
    535         //gvsTimer.Entry(); 
    536         castTimer.Entry(); 
    537  
    538         CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays); 
    539          
    540         castTimer.Exit(); 
    541         //gvsTimer.Exit(); 
    542  
    543         const int numBorderSamples = (int)vssRays.size() - numRandomRays; 
    544         int castRays = (int)simpleRays.size(); 
    545  
    546         // handle cast rays 
    547         EnqueueRays(vssRays); 
    548  
    549         if (0) 
    550         { 
     562         
    551563                // set flags 
    552564                VssRayContainer::const_iterator rit, rit_end = vssRays.end(); 
     
    556568        } 
    557569 
    558         for (size_t i = 0; i < vssRays.size(); ++ i) 
    559         { 
    560                 // check for discontinuity and cast reverse rays if necessary 
    561                 castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay); 
    562         } 
    563          
    564  
    565570    // recursivly subdivide each edge 
    566571        for (int i = 0; i < numBorderSamples; ++ i) 
    567572        { 
     573                castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay); 
     574 
    568575                castRays += SubdivideEdge(hitTriangle, 
    569576                                                                  enlargedTriangle[i],  
     
    638645        const int numRandomRays = 16 - ((int)simpleRays.size() % 16); 
    639646 
    640                 if (!mUseDeterministicGvs) 
    641         { 
    642                 if (0) 
    643                 { 
    644                         SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(),  
    645                                                           currentRay.GetNormalizedDir(),  
    646                                                                                   SamplingStrategy::GVS,  
    647                                                                                   1.0f); 
    648                          
    649                         GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0, 0.01f); 
    650                 } 
    651                 else 
    652                         GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays); 
    653         } 
    654         else 
    655                 GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples); 
    656  
     647        GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays); 
     648         
    657649        generationTimer.Exit(); 
    658650 
     
    689681        { 
    690682                // check for discontinuity and cast reverse rays if necessary 
    691                 castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay); 
     683                castRays += CheckDiscontinuity2(*vssRays[i], hitTriangle, currentRay); 
    692684        } 
    693685 
     
    794786 
    795787        ////////////// 
    796         //-- for per view cell sampling, we must check for intersection 
     788        //-- for per view cell sampling, we must intersect 
    797789        //-- with the current view cell 
    798790 
    799791    if (mPerViewCell) 
    800792        { 
    801                 // send ray to view cell 
    802                 static Ray ray; 
    803  
    804                 ray.Clear(); 
    805                 ray.Init(newOrigin, -newDir, Ray::LOCAL_RAY); 
    806                  
    807                 // check if ray intersects view cell 
    808                 if (!mCurrentViewCell->CastRay(ray)) 
     793                if (!IntersectViewCell(newOrigin, newDir)) 
    809794                        return false; 
    810  
    811                 Ray::Intersection &hit = ray.intersections[0]; 
    812          
    813                 // the ray starts from the view cell 
    814                 newOrigin = ray.Extrap(hit.mT); 
    815795        } 
    816796 
    817797        reverseRay = SimpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f); 
     798 
     799        return true; 
     800} 
     801 
     802 
     803bool GvsPreprocessor::IntersectViewCell(Vector3 &origin, Vector3 &dir) const 
     804{ 
     805        // send ray to view cell 
     806        static Ray testRay; 
     807 
     808        testRay.Clear(); 
     809        testRay.Init(origin, -dir, Ray::LOCAL_RAY); 
     810 
     811        // check if ray intersects view cell 
     812        if (!mCurrentViewCell->CastRay(testRay)) 
     813                return false; 
     814 
     815        Ray::Intersection &hit = testRay.intersections[0]; 
     816 
     817        // the ray starts from the view cell 
     818        origin = testRay.Extrap(hit.mT); 
    818819 
    819820        return true; 
     
    908909                mRayQueue.pop(); 
    909910                 
    910                 if (0) 
     911                if (mUseDeterministicGvs) 
    911912                        castSamples += AdaptiveBorderSampling(*ray); 
    912913                else 
    913914                        castSamples += AdaptiveBorderSamplingOpt(*ray); 
     915 
    914916                // note: don't have to delete because handled by ray pool 
    915917        } 
     
    20332035 
    20342036 
    2035 } 
     2037void GvsPreprocessor::CreateRandomizedReverseRays(const Vector3 &origin,  
     2038                                                                                                  const Vector3 &termination,  
     2039                                                                                                  const Triangle3 &triangle,                    
     2040                                                                                                  SimpleRayContainer &rays,  
     2041                                                                                                  int number) 
     2042{ 
     2043        size_t currentNumber = rays.size(); 
     2044 
     2045        while ((rays.size() - currentNumber) < number) 
     2046        { 
     2047                SimpleRay ray; 
     2048                if (CreateRandomizedReverseRay(origin, termination, triangle, ray)) 
     2049                        rays.push_back(ray); 
     2050        } 
     2051} 
     2052 
     2053 
     2054bool GvsPreprocessor::CreateRandomizedReverseRay(const Vector3 &origin,  
     2055                                                                                                 const Vector3 &termination,  
     2056                                                                                                 const Triangle3 &triangle,                    
     2057                                                                                                 SimpleRay &ray) 
     2058{ 
     2059        Vector3 nd = triangle.GetNormal(); 
     2060   
     2061        // Compute right handed coordinate system from direction 
     2062        Vector3 U, V; 
     2063        nd.RightHandedBase(U, V); 
     2064         
     2065        float rr[2]; 
     2066 
     2067        rr[0] = RandomValue(0, 1); 
     2068        rr[1] = RandomValue(0, 1); 
     2069 
     2070        Vector2 vr2(rr[0], rr[1]); 
     2071        const float sigma = triangle.GetBoundingBox().Radius() * mRadiusOfInfluence; 
     2072        Vector2 gaussvec2; 
     2073 
     2074        GaussianOn2D(vr2, sigma, // input 
     2075                         gaussvec2); // output 
     2076         
     2077        const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V; 
     2078 
     2079        Vector3 newOrigin = origin; 
     2080 
     2081        //cout << "t: " << termination; 
     2082        newOrigin += shift; 
     2083        //cout << " new t: " << termination << endl; 
     2084        Vector3 direction = termination - newOrigin; 
     2085 
     2086        const float len = Magnitude(direction); 
     2087         
     2088        if (len < Limits::Small) 
     2089                return false; 
     2090   
     2091        direction /= len; 
     2092 
     2093        if (!IntersectViewCell(newOrigin, direction)) 
     2094                return false; 
     2095 
     2096        // $$ jb the pdf is yet not correct for all sampling methods! 
     2097        const float pdf = 1.0f; 
     2098        ray = SimpleRay(newOrigin, direction, SamplingStrategy::GVS, pdf); 
     2099         
     2100        return true; 
     2101} 
     2102 
     2103 
     2104} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h

    r2738 r2739  
    121121 
    122122protected: 
    123 #if 0 
    124         struct PendingRay 
    125         { 
    126                 PendingRay(VssRay *ray, const bool d) 
    127                         : mRay(ray), mFoundDiscontinuity(d)  
    128                 {} 
    129  
    130                 VssRay *mRay; 
    131                 bool mFoundDiscontinuity; 
    132         }; 
    133  
    134         typedef stack<PendingRay> PendingQueue; 
    135 #endif 
     123 
    136124        typedef stack<VssRay *> RayQueue; 
    137125 
     
    140128                by the queue and continues as long it finds new visible geometry  
    141129                (i.e., the queue is     not empty). 
    142  
    143130                @returns the number of samples cast. 
    144131        */ 
    145132        int ProcessQueue(); 
    146  
    147133        /** Generates the rays starting the adaptive visibility sampling process. 
    148134        */ 
    149135        int CastInitialSamples(int numSamples); 
    150  
    151136        /** Uses the information gained from the ray for doing adaptive border sampling. 
    152137                This function tries to find the border of the triangle found visible by the 
     
    163148        */       
    164149        int AdaptiveBorderSampling(const VssRay &currentRay); 
     150 
    165151        int AdaptiveBorderSamplingOpt(const VssRay &currentRay); 
    166152        /** The reverse sampling step. It is started once the cast 
     
    179165                                                   const Triangle3 &hitTriangle, 
    180166                                                   const VssRay &oldRay); 
     167        /** Returns true if we sampled a closer triangle than with the previous ray.  
     168                Does reverse sampling if gap found. 
     169        */ 
     170        int CheckDiscontinuity2(const VssRay &currentRay,  
     171                                    const Triangle3 &hitTriangle, 
     172                                                        const VssRay &oldRay); 
    181173        /** Adds new samples to the ray queue and classifies them 
    182174                with respect to the previous ray. 
     
    300292 
    301293        void PrepareProbablyVisibleSampling(); 
     294         
     295        void CreateRandomizedReverseRays(const Vector3 &origin,  
     296                                             const Vector3 &termination,  
     297                                                                         const Triangle3 &triangle,                    
     298                                                                         SimpleRayContainer &rays,  
     299                                                                         int number); 
     300 
     301        bool CreateRandomizedReverseRay(const Vector3 &origin,  
     302                                            const Vector3 &termination, 
     303                                                                        const Triangle3 &triangle, 
     304                                                                        SimpleRay &ray); 
     305 
     306        inline bool IntersectViewCell(Vector3 &origin, Vector3 &dir) const; 
    302307 
    303308 
Note: See TracChangeset for help on using the changeset viewer.