source: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h @ 2740

Revision 2740, 9.3 KB checked in by mattausch, 16 years ago (diff)
  • Property svn:executable set to *
RevLine 
[1460]1#ifndef _GvsPreprocessor_H__
2#define _GvsPreprocessor_H__
3
4#include <fstream>
[1473]5#include <stack>
[1460]6#include "Preprocessor.h"
[2738]7#include "FlexibleHeap.h"
[1460]8
[2738]9
10
[1460]11namespace GtpVisibilityPreprocessor {
12
13class Exporter;
[1473]14class VssRay;
[2669]15class TriangleIntersectable;
16class KdIntersectable;
[2691]17class ViewCellBasedDistribution;
[2694]18class ViewCellBorderBasedDistribution;
[1460]19
[2694]20
[1934]21/** View space partition statistics.
22*/
23class GvsStatistics: public StatisticsBase
24{
25public:
26       
27        /// Constructor
28        GvsStatistics()
29        {
30                Reset();
31        }
32       
33        void Reset()
34        {
35                mPass = 0;
36                mTotalSamples = 0;
37                mPassContribution = 0;
38                mTotalContribution = 0;
39                mReverseSamples = 0;
40                mBorderSamples = 0;
[2695]41                mGvsRuns = 0;
[1990]42
[2726]43                mRandomSamples = 0;
44                mGvsSamples = 0;
[1990]45                mTotalPvs = 0;
46                mViewCells = 0;
47                mPerViewCellSamples = 0;
[1996]48                mPerViewCellPvs = 0;
49                mTrianglePvs = 0;
50                mViewCellId = 0;
[2003]51                mTotalTime = 0;
52                mTimePerViewCell = 0;
53                mTotalTrianglePvs = 0;
[2648]54                mPvsCost = 0;
[1934]55        }
[1473]56
[1489]57
[1934]58public:
59
[2726]60        int mRandomSamples;
61        int mGvsSamples;
[1934]62        int mPass;
63        int mTotalSamples;
64        int mPassContribution;
65        int mTotalContribution;
66        int mReverseSamples;
67        int mBorderSamples;
[2695]68        int mGvsRuns;
[2003]69
[1990]70        int mTotalPvs;
71        int mViewCells;
72        int mPerViewCellSamples;
[1996]73        int mPerViewCellPvs;
74        int mTrianglePvs;
[2003]75        int mTotalTrianglePvs;
[1996]76        int mViewCellId;
77
[2003]78        float mTimePerViewCell;
79        float mTotalTime;
[2648]80       
81        float mPvsCost;
[2003]82
[2615]83        ObjectContainer mKdPvs;
84
[2740]85        float RaysPerSec() const
86        {
87                if (!mTotalTime) return 0;
88                return (float)(mTotalSamples / mTotalTime) * 1e-6f;
89        }
[2003]90
[1934]91        void Print(ostream &app) const;
92
93        friend ostream &operator<<(ostream &s, const GvsStatistics &stat)
94        {
95                stat.Print(s);
96                return s;
97        }
98};
99
100
[2738]101struct ProbablyVisibleTriangleContainer
102{
103        KdNode *node;
104        ObjectContainer mTriangles;
105};
106
107
108typedef FlexibleHeap<ProbablyVisibleTriangleContainer *> ProbablyVisibleTriangleQueue;
109
110
111
[1473]112/** Sampling based visibility preprocessing. The implementation is
113        based on heuristical sampling of view space.
114*/
[2625]115class GvsPreprocessor : public Preprocessor
116{
[1460]117
118public:
[1473]119
120        GvsPreprocessor();
[1990]121        ~GvsPreprocessor();
[1473]122
123        virtual bool ComputeVisibility();
124       
125
[1460]126protected:
[1492]127
[1500]128        typedef stack<VssRay *> RayQueue;
[1492]129
[1489]130        /** Runs the adaptive sampling until the ray queue is empty.
131                The method starts with a number of random rays given
132                by the queue and continues as long it finds new visible geometry
133                (i.e., the queue is     not empty).
[1473]134                @returns the number of samples cast.
135        */
[1489]136        int ProcessQueue();
[1473]137        /** Generates the rays starting the adaptive visibility sampling process.
138        */
[2691]139        int CastInitialSamples(int numSamples);
[1473]140        /** Uses the information gained from the ray for doing adaptive border sampling.
141                This function tries to find the border of the triangle found visible by the
142                current ray. New rays are generated which sample this border.
143               
144                We use the following strategies:
145
146                a) if new triangle was found: adaptive border sampling
147                b) if triangle was found reverse sampling
148        */
[2726]149        inline bool HandleRay(VssRay *ray);
[1489]150        /** The adaptive border sampling step. It aims to find neighbouring
[1500]151                triangles of the one hit by the current ray.
[1473]152        */     
[1500]153        int AdaptiveBorderSampling(const VssRay &currentRay);
[2739]154
[2729]155        int AdaptiveBorderSamplingOpt(const VssRay &currentRay);
[1489]156        /** The reverse sampling step. It is started once the cast
157                ray finds a discontinuity, i.e., a closer triangle.
158                Then the process tries to find a ray from the old
159                triangle passing through a gap.
160        */
[2729]161        bool ComputeReverseRay(const VssRay &currentRay,
162                                 const Triangle3 &hitTriangle,
163                                                         const VssRay &oldRay,
164                                                         SimpleRay &reverseRay);
[1500]165        /** Returns true if we sampled a closer triangle than with the previous ray.
166                Does reverse sampling if gap found.
[1473]167        */
[1996]168        int CheckDiscontinuity(const VssRay &currentRay,
169                                                   const Triangle3 &hitTriangle,
170                                                   const VssRay &oldRay);
[2739]171        /** Returns true if we sampled a closer triangle than with the previous ray.
172                Does reverse sampling if gap found.
173        */
174        int CheckDiscontinuity2(const VssRay &currentRay,
175                                    const Triangle3 &hitTriangle,
176                                                        const VssRay &oldRay);
[1489]177        /** Adds new samples to the ray queue and classifies them
178                with respect to the previous ray.
179        */
[2726]180        void EnqueueRays(VssRayContainer &samples);
[1522]181        /** Hepler function for adaptive border sampling. It finds
[1500]182                new sample points around a triangle in a eps environment
183        */
184        void EnlargeTriangle(VertexContainer &vertices,
185                                                 const Triangle3 &hitTriangle,
[1932]186                                                 const VssRay &ray) const;
[1500]187
[1932]188        int SubdivideEdge(const Triangle3 &hitTriangle,
189                                          const Vector3 &p1,
190                                          const Vector3 &p2,
191                                          const VssRay &ray1,
192                      const VssRay &ray2,
193                                          const VssRay &oldRay);
[1500]194
[1522]195        void Visualize();
[1545]196
[1932]197        void CreateDisplacedVertices(VertexContainer &vertices,
198                                                                 const Triangle3 &hitTriangle,
199                                                                 const VssRay &ray,
200                                                                 const int index) const;
201
202        Vector3 CalcPredictedHitPoint(const VssRay &newRay,
203                                                                  const Triangle3 &hitTriangle,
204                                                                  const VssRay &oldRay) const;
205
[1996]206        bool GetPassingPoint(const VssRay &currentRay,
207                                                 const Triangle3 &occluder,
208                                                 const VssRay &oldRay,
209                                                 Vector3 &newPoint) const;
[1932]210
[2625]211        ViewCell *NextViewCell();
[1982]212
213        void GlobalComputation();
214
[2625]215        /** Loops over aall view cellls.
216        */
[1982]217        void PerViewCellComputation();
[2625]218        void PerViewCellComputation2();
[1982]219
220        void VisualizeViewCells();
221
[1990]222        void VisualizeViewCell(ViewCell *vc);
[1999]223        void VisualizeViewCell(const ObjectContainer &objects);
[1982]224
[1990]225        /** Exchanges view cell triangle pvs with bvh leaf pvs.
226        */
227        void UpdatePvs(ViewCell *currentViewCell);
228
[1996]229        void ClearRayQueue();
[1990]230
[2648]231        /** Create a list of view cells gvs is run on.
232        */
[1996]233        void CompileViewCellsList();
234
[1999]235        void GetObjectPvs(ObjectContainer &trianglePvs) const;
[1996]236
[1999]237        bool HasContribution(VssRay &ray);
238
[2705]239        void ComputeViewCellGeometryIntersection();
[1999]240
[2048]241        void DeterminePvsObjects(VssRayContainer &rays);
242
243        void StorePvs(const ObjectContainer &objectPvs);
244
[2690]245        /** Compute visibility for the current view cell using gvs
[2648]246        */
[2690]247        void ComputeViewCell();
[2669]248        /** Use this for qt visualization.
249        */
250        void UpdateStatsForVisualization(KdIntersectable *kdInt);
[2726]251       
[2687]252        void CompileViewCellsFromPointList();
[2625]253
[2690]254        void ComputeStats();
255
256        int ConvertObjectPvs();
[2726]257        /** Keep count of new objects for stats. Returns number of new pvs entries.
258        */
259        inline int CountObject(Intersectable *triObj);
[2690]260
[2726]261        inline bool AddTriangleObject(Intersectable *triObj);
[2690]262
[2726]263        inline void AddKdNodeToPvs(const Vector3 &termination);
[2729]264        /** For all rayys of the ray container, generates a ray bundle of 4 jittered rays
265                and casts them using optimized 4 eye ray casting.
266        */
267        void CastRayBundles4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
268       
[2736]269        void CastRayBundles16(const SimpleRayContainer &rays, VssRayContainer &vssRays, float scale);
[2726]270
[2729]271        /** Generates a ray bundle of 4 jittered rays and casts them using optimized 4 eye ray casting.
272        */
273        void CastRayBundle4(const SimpleRay &ray, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
274        /**  Cast rays using the optimized 4 eye ray casting routine.
275        */
276        void CastRays4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
277        /** Wrapper for optized ray casting routine taking 4 eye rays and a bounding box for ray clipping.
278        */
279        void Cast4Rays(float *dist, Vector3 *dirs, Vector3 *origs, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
[2726]280
[2736]281        void CastRayBundle16(const SimpleRay &ray, VssRayContainer &vssRays, float scale);
[2729]282
[2730]283        virtual void ComputeRenderError();
[2729]284
[2731]285        bool GenerateImportanceSample(const VssRay &ray, const Triangle3 &triangle, SimpleRay &sray);
[2730]286
[2731]287        void GenerateImportanceSamples(const VssRay &ray,
288                                           const Triangle3 &triangle,
289                                                                   int numSamples,
290                                                                   SimpleRayContainer &simpleRays);
[2730]291
[2735]292        /** Collects triangles which are probably visible. Warning: returns
293                only unmailed objects.
294        */
295        void CollectProbablyVisibleTriangles(ObjectContainer &triangles);
296
297        void PrepareProbablyVisibleSampling();
[2739]298       
299        void CreateRandomizedReverseRays(const Vector3 &origin,
300                                             const Vector3 &termination,
301                                                                         const Triangle3 &triangle,                   
302                                                                         SimpleRayContainer &rays,
303                                                                         int number);
[2735]304
[2739]305        bool CreateRandomizedReverseRay(const Vector3 &origin,
306                                            const Vector3 &termination,
307                                                                        const Triangle3 &triangle,
308                                                                        SimpleRay &ray);
[2735]309
[2740]310        inline bool IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const;
[2739]311
312
[1473]313        //////////////////////
314
[2615]315        bool mUseKdPvs;
[1545]316        int mInitialSamples;
317
[1473]318        RayQueue mRayQueue;
[2691]319        ViewCellBasedDistribution *mDistribution;
[1545]320
[1563]321        //AxisAlignedBox3 mViewSpaceBox;
[1486]322        float mEps;
[1500]323        float mThreshold;
[1522]324        VssRayContainer mVssRays;
[1545]325       
[2048]326
[2615]327        ObjectContainer mKdPvs;
[2048]328
[1545]329        ///////////
330        // stats
[1932]331
[1934]332        ofstream mGvsStatsStream;
333        GvsStatistics mGvsStats;
334
[1976]335        bool mPerViewCell;
[1934]336        bool mOnlyRandomSampling;
[1977]337
338        ViewCell *mCurrentViewCell;
[1982]339
[1996]340        int mProcessedViewCells;
341
342        int mMinContribution;
343
344        ViewCellContainer mViewCells;
345
346        int mMaxViewCells;
[1999]347
[2048]348        int mGvsSamplesPerPass;
349
350        bool mComputeRenderError;
351
352        bool mEvaluatePixelError;
[2625]353
354        Vector3 mCurrentViewPoint;
[2735]355
356        /// container for probably visible triangles
357        ObjectContainer mProbablyVisibleTriangles;
358
359        bool mUseProbablyVisibleSampling;
[1460]360
[2738]361        float mInitialJitter;
362
363        bool mUseDeterministicGvs;
364
365        float mRadiusOfInfluence;
[1460]366};
367
[2738]368
369}
370
[1460]371#endif
Note: See TracBrowser for help on using the repository browser.