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

Revision 840, 14.4 KB checked in by mattausch, 18 years ago (diff)

implementing bounding box hack

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