source: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h @ 880

Revision 880, 14.5 KB checked in by mattausch, 18 years ago (diff)

added filter to online stuff (not fully working, too slow
)

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