source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h @ 608

Revision 608, 12.2 KB checked in by bittner, 18 years ago (diff)

slider support for viewcells

Line 
1#ifndef _ViewCell_H__
2#define _ViewCell_H__
3
4#include "Mesh.h"
5#include "Containers.h"
6#include "Ray.h"
7#include "Statistics.h"
8#include "Material.h"
9//namespace GtpVisibilityPreprocessor {
10
11struct Triangle3;
12
13class BspInterior;
14class BspPvs;
15class BspLeaf;
16class VspKdTree;
17class VspKdLeaf;
18class KdLeaf;
19class ViewCellInterior;
20class MergeCandidate;
21class ViewCellsManager;
22
23/** Statistics for a view cell partition.
24*/
25
26class ViewCellsStatistics: public StatisticsBase
27{
28public:
29
30        /// number of view cells
31        int viewCells;
32
33        /// size of the PVS
34        int pvs;
35
36        /// largest PVS of all view cells
37        int maxPvs;
38
39        /// smallest PVS of all view cells
40        int minPvs;
41
42        /// view cells with empty PVS
43        int emptyPvs;
44
45        /// number of leaves covering the view space
46        int leaves;
47
48        /// largest number of leaves covered by one view cell
49        int maxLeaves;
50
51        int invalid;
52
53    // Constructor
54        ViewCellsStatistics()
55        {
56                Reset();
57        }
58
59        double AvgLeaves() const {return (double)leaves / (double)viewCells;};
60        double AvgPvs() const {return (double)pvs / (double)viewCells;};
61
62        void Reset()
63        {
64                viewCells = 0;
65                pvs = 0;
66                maxPvs = 0;
67
68                minPvs = 999999;
69                emptyPvs = 0;
70                leaves = 0;
71                maxLeaves = 0;
72                invalid = 0;
73        }
74
75        void Print(ostream &app) const;
76
77        friend ostream &operator<<(ostream &s, const ViewCellsStatistics &stat)
78        {
79                stat.Print(s);
80                return s;
81        }
82};
83
84
85/**
86        View cell with an optional mesh representation
87*/
88
89
90class ViewCell: public MeshInstance
91{
92public:
93        ViewCell();
94
95        /** Constructor taking a mesh representing the shape of the viewcell.
96        */
97        ViewCell(Mesh *mesh);
98
99        /** Default destructor.
100        */
101        virtual ~ViewCell() {}
102
103        /** Returns Pvs.
104        */
105        const ObjectPvs &GetPvs() const;
106        ObjectPvs &GetPvs();
107
108        int Type() const;
109
110  void SetParent(ViewCellInterior *parent);
111
112        /** Adds a passing ray to the passing ray container.
113        */
114        void AddPassingRay(const Ray &ray, const int contributions);
115
116        /** Returns volume of the view cell.
117        */
118        float GetVolume() const;
119
120        /** Returns area of the view cell.
121        */
122        float GetArea() const;
123
124        /** Sets the volume of the view cell.
125        */
126        void SetVolume(float volume);
127       
128        /** Sets the area of the view cell.
129        */
130        void SetArea(float area);
131
132
133        /** if this view cell is the root of a view cell hierarchy
134        */
135        bool IsRoot() const;
136
137        /** Returns parent view cell.
138        */
139        ViewCellInterior *GetParent() const;
140
141       
142        /** Sets the mesh for this view cell.
143        */
144        void SetMesh(Mesh *mesh);
145
146        void SetValid(const bool valid);
147        bool GetValid() const;
148
149
150  /** set color for visiualizations */
151  void SetColor(const RgbColor &color) {
152        mColor = color;
153  }
154
155  /** get color for visualuzations */
156  RgbColor GetColor() const {
157        return mColor;
158  }
159
160 
161        /// parent view cell in the view cell hierarchy
162        ViewCellInterior *mParent;
163
164        /// Rays piercing this view cell.
165        RayContainer mPiercingRays;
166
167
168        /** if this is a view cell correspending to a leaf in a hierarchy.
169        */
170        virtual bool IsLeaf() const = 0;
171
172  static bool SmallerPvs(const ViewCell *a,
173                                                 const ViewCell *b) {
174        return a->GetPvs().GetSize() < b->GetPvs().GetSize();
175  }
176
177
178        void SetMergeCost(const float mergeCost);
179        float GetMergeCost() const;
180        static void NewMail(const int reserve = 1) {
181                sMailId += sReservedMailboxes;
182                sReservedMailboxes = reserve;
183        }
184        void Mail() { mMailbox = sMailId; }
185        bool Mailed() const { return mMailbox == sMailId; }
186
187        void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
188        bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
189
190        int IncMail() { return ++mMailbox - sMailId; }
191
192
193                                                 
194        // last mail id -> warning not thread safe!
195        // both mailId and mailbox should be unique for each thread!!!
196        static int sMailId;
197        static int sReservedMailboxes;
198       
199        bool mIsActive;
200
201protected:
202
203
204        /// the potentially visible objects
205        ObjectPvs mPvs;
206
207        float mVolume;
208        float mArea;
209
210        float mMergeCost;
211
212        bool mValid;
213
214  /** color used for consistent visualization */
215  RgbColor mColor;
216};
217
218
219class ViewCellInterior: public ViewCell
220{
221public:
222        ViewCellInterior();
223        ~ViewCellInterior();
224
225        ViewCellInterior(Mesh *mesh);
226       
227
228        /** Sets pointer from parent to child and vice versa.
229        */
230        void SetupChildLink(ViewCell *l);
231        void RemoveChildLink(ViewCell *l);
232        bool IsLeaf() const;
233
234        ViewCellContainer mChildren;
235
236  void SetCost(const float c) {
237        mCost = c;
238  }
239  float GetCost() const {
240        return mCost;
241  }
242 
243protected:
244  /** overall cost resulting from the merge */
245  float mCost;
246};
247
248/**
249        View cell belonging to a hierarchy.
250*/
251template<typename T>
252class ViewCellLeaf: public ViewCell
253{
254public:
255
256        ViewCellLeaf<T>(): mLeaf(NULL) {}
257        ViewCellLeaf<T>(Mesh *mesh):
258                ViewCell(mesh), mLeaf(NULL) {}
259
260       
261
262        bool IsLeaf() const
263        {
264                return true;
265        }
266
267        /// Leaf of some hierarchy which is part of this view cell.
268        T mLeaf;
269};
270
271
272typedef ViewCellLeaf<BspLeaf *> BspViewCell;
273typedef ViewCellLeaf<KdLeaf *> KdViewCell;
274typedef ViewCellLeaf<VspKdLeaf *> VspKdViewCell;
275
276
277
278class ViewCellsTree
279{
280        friend class ViewCellsManager;
281public:
282        ViewCellsTree(ViewCellsManager *vcm);
283        ~ViewCellsTree();
284
285        /** Returns number of leaves this view cell consists of.
286        */
287        int GetSize(ViewCell *vc) const;
288
289        /** Collects leaves corresponding to a view cell.
290        */
291        void CollectLeaves(ViewCell *vc, ViewCellContainer &leaves) const;
292
293        /** Merges view cells according to some cost heuristics.
294        */
295        int ConstructMergeTree(const VssRayContainer &rays, const ObjectContainer &objects);
296       
297        /** Refines view cells using shuffling, i.e., border leaves
298                of two view cells are exchanged if the resulting view cells
299                are tested to be "better" than the old ones.
300                @returns number of refined view cells
301        */
302        int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
303
304  /** Assign colors to the viewcells so that they can be renderered interactively without
305          color flickering.
306  */
307  void AssignRandomColors();
308        /** Updates view cell stats for this particular view cell
309        */
310        void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
311       
312
313  /** Get costs resulting from each merge step. */
314  void GetCostFunction(vector<float> &costFunction);
315 
316
317        /** Returns optimal set of view cells for a given number of view cells.
318        */
319        void CollectBestViewCellSet(ViewCellContainer &viewCells, const int numViewCells);
320
321        /** Root of view cells tree.
322        */
323        ViewCell *GetRoot() const;
324
325        /** Returns pvs of view cell.
326                @note pvs is returned per reference if tree is not compressed,
327                per copy else.
328        */
329        void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
330
331        /** Returns pvs size of view cell.
332        */
333        int GetPvsSize(ViewCell *vc) const;
334
335        /** Returns actual number of object in this pvs and the children.
336        */
337        int GetNumPvsEntries(ViewCell *vc) const;
338
339        /** Returns memory cost of this view cell.
340        */
341        float GetMemoryCost(ViewCell *vc) const;
342
343        /** Compresses the pvs of the view cells from the root.
344        */
345        void CompressViewCellsPvs();
346
347        /** If view cells in this tree have compressed pvs.
348        */
349        bool IsCompressed() const;
350
351        ViewCell *GetActiveViewCell(ViewCell *vc) const;
352
353protected:
354
355
356        //////////////////////////////////////////////////////////////
357        //                 merge options                            //
358        //////////////////////////////////////////////////////////////
359       
360
361        /** Returns cost of this leaf according to current heuristics.
362        */
363        float GetCostHeuristics(ViewCell *vc) const;
364
365        /** Returns cost of leaf.
366        */
367        float GetRenderCost(ViewCell *vc) const;
368
369        /** Evaluates the merge cost of this merge candidate pair.
370        */
371        void EvalMergeCost(MergeCandidate &mc) const;
372
373        /** Variance of leaf.
374        */
375        float GetVariance(ViewCell *vc) const;
376
377        /** Standard deviation of leaf.
378        */
379        float GetDeviation(ViewCell *vc) const;
380
381        /** Tries to set this merge candidate to valid.
382                @returns false if both view cells are the same
383        */
384        bool ValidateMergeCandidate(MergeCandidate &mc) const;
385
386        /** Merge view cells of leaves l1 and l2.
387                @returns difference in pvs size
388        */
389        ViewCellInterior *MergeViewCells(ViewCell *l, ViewCell *r, int &pvsDiff); //const;
390
391        /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
392                to view cell 2.
393        */
394        void ShuffleLeaf(ViewCell *leaf, ViewCellInterior *vc1, ViewCellInterior *vc2) const;   
395               
396        /** Shuffles the leaves, i.e., tests if exchanging
397                the leaves helps in improving the view cells.
398        */
399        bool ShuffleLeaves(MergeCandidate &mc) const;
400
401        /** Calculates cost for merge of view cell 1 and 2.
402        */
403        float EvalShuffleCost(ViewCell *leaf,
404                                                  ViewCellInterior *vc1,
405                                                  ViewCellInterior *vc2) const;
406
407        /** Exports a snapshot of the merged view cells to disc.
408        */
409        void ExportMergedViewCells(ViewCellContainer &viewCells,
410                                                           const ObjectContainer &objects,
411                                                           const int numNewViewCells);
412
413        /** merge queue must be reset after some time because expected value
414                may not be valid.
415        */
416        void ResetMergeQueue();
417
418        /** Updates the current top level of view cells.
419                @returns number of newly merged view cells
420        */
421        int UpdateActiveViewCells(ViewCellContainer &viewCells);
422
423        void PropagateUpVisibility(ViewCellInterior *interior);
424
425        void CompressViewCellsPvs(ViewCell *root);
426
427        /** Returns memory usage of view cells.
428        */
429        float GetMemUsage() const;
430
431
432        /// if the view cell tree hold compressed pvs
433        bool mIsCompressed;
434
435        ViewCellsManager *mViewCellsManager;
436        ViewCell *mRoot;
437
438        /// if merge visualization should be shown
439        bool mExportMergedViewCells;
440
441       
442               
443        ViewCellContainer mMergedViewCells;
444       
445
446        bool mRefineViewCells;
447
448        /// weights between variance and render cost increase (must be between zero and one)
449        float mRenderCostWeight;
450
451        /// overall cost used to normalize cost ratio
452        float mOverallCost;
453        float mExpectedCost;
454    float mDeviation;
455        float mAvgRenderCost;
456
457        int mUseAreaForPvs;
458
459        int mNumActiveViewCells;
460
461        /// minimal number of view cells
462        int mMergeMinViewCells;
463        /// maximal cost ratio for the merge
464        float mMergeMaxCostRatio;
465
466        ofstream mStats;
467
468        typedef priority_queue<MergeCandidate> MergeQueue;
469
470        MergeQueue mMergeQueue;
471
472        float mMaxMemory;
473
474};
475
476
477/**
478        Candidate for leaf merging based on priority.
479*/
480class MergeCandidate
481
482        friend class ViewCellsTree;
483
484public:
485
486        MergeCandidate(ViewCell *l, ViewCell *r);
487
488        /** If this merge pair is still valid.
489        */
490        bool IsValid() const;
491
492       
493        friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
494        {
495                return leafb.GetMergeCost() < leafa.GetMergeCost();
496        }
497
498        void SetLeftViewCell(ViewCell *l);
499        void SetRightViewCell(ViewCell *l);
500
501        ViewCell *GetLeftViewCell() const;
502        ViewCell *GetRightViewCell() const;
503
504        ViewCell *GetInitialLeftViewCell() const;
505        ViewCell *GetInitialRightViewCell() const;
506
507        /** Returns the increase of the standard deviation of this merge candidate.
508        */
509        float GetDeviationIncr() const;
510
511        /** Merge cost of this candidate pair.
512        */
513        float GetMergeCost() const;
514
515        /** Render cost of this candidate.
516        */
517        float GetRenderCost() const;
518       
519        static float sRenderCostWeight;
520
521protected:
522
523        /// render cost increase by this merge
524        float mRenderCost;
525        /// increase / decrease of standard deviation
526        float mDeviationIncr;
527
528        ViewCell *mLeftViewCell;
529        ViewCell *mRightViewCell;
530
531        ViewCell *mInitialLeftViewCell;
532        ViewCell *mInitialRightViewCell;
533};
534
535
536class MergeStatistics: public StatisticsBase
537{
538public:
539       
540        int merged;
541        int siblings;
542        int candidates;
543        int nodes;
544
545        int accTreeDist;
546        int maxTreeDist;
547       
548        Real collectTime;
549        Real mergeTime;
550
551        Real overallCost;
552
553        Real expectedRenderCost;
554        Real deviation;
555        Real heuristics;
556
557        // Constructor
558        MergeStatistics()
559        {
560                Reset();
561        }
562       
563        double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
564
565        void Reset()
566        {
567                nodes = 0;
568                merged = 0;
569                siblings = 0;
570                candidates = 0;
571       
572                accTreeDist = 0;
573                maxTreeDist = 0;
574
575                collectTime = 0;
576                mergeTime = 0;
577                overallCost = 0;
578
579                expectedRenderCost = 0;
580                deviation = 0;
581                heuristics = 0;
582
583        }
584
585        void Print(ostream &app) const;
586
587        friend ostream &operator<<(ostream &s, const MergeStatistics &stat)
588        {
589                stat.Print(s);
590                return s;
591        }
592};
593
594#endif
Note: See TracBrowser for help on using the repository browser.