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

Revision 801, 14.2 KB checked in by mattausch, 18 years ago (diff)

debug version for testing subdivision

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