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

Revision 1709, 19.8 KB checked in by mattausch, 18 years ago (diff)
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#include "gzstream.h"
10
11
12namespace GtpVisibilityPreprocessor {
13
14struct Triangle3;
15
16class BspInterior;
17class BspPvs;
18class BspLeaf;
19class VspLeaf;
20class KdLeaf;
21class ViewCellInterior;
22class MergeCandidate;
23class ViewCellsManager;
24class ViewCellLeaf;
25
26
27
28/** Statistics for a view cell partition.
29*/
30
31class ViewCellsStatistics: public StatisticsBase
32{
33public:
34
35        /// number of view cells
36        int viewCells;
37        /// cost of the PVS
38        float pvsCost;
39        /// largest PVS of all view cells
40        float maxPvs;
41        /// smallest PVS of all view cells
42        float minPvs;
43        /// view cells with empty PVS
44        int emptyPvs;
45        /// number of leaves covering the view space
46        int leaves;
47        /// largest number of leaves covered by one view cell
48        int maxLeaves;
49        /// number of invalid view cells
50        int invalid;
51
52    // Constructor
53        ViewCellsStatistics()
54        {
55                Reset();
56        }
57
58        double AvgLeaves() const {return (double)leaves / (double)viewCells;};
59        double AvgPvs() const {return (double)pvsCost / (double)viewCells;};
60
61        void Reset()
62        {
63                viewCells = 0;
64                pvsCost = 0;
65                maxPvs = 0;
66
67                minPvs = 999999;
68                emptyPvs = 0;
69                leaves = 0;
70                maxLeaves = 0;
71                invalid = 0;
72        }
73
74        void Print(ostream &app) const;
75
76        friend ostream &operator<<(ostream &s, const ViewCellsStatistics &stat)
77        {
78                stat.Print(s);
79                return s;
80        }
81};
82
83
84class ViewCellsTreeStats
85{
86public:
87        int mPass;
88   
89        int mNumViewCells;
90               
91        float mRenderCostDecrease;
92
93    float mTotalRenderCost;
94   
95        float mCurrentPvsCost;
96                                                       
97        float mExpectedCost;
98   
99        float mAvgRenderCost;
100       
101        float mDeviation;
102                       
103        float mTotalPvsCost;
104       
105        int mEntriesInPvs;
106   
107        float mMemoryCost;
108       
109        int mPvsSizeDecr;
110       
111        float mVolume;
112
113
114        void Reset()
115        {
116                mPass = 0;
117                mNumViewCells = 0;
118                mRenderCostDecrease = 0;
119                mTotalRenderCost = 0;
120                mCurrentPvsCost = 0;
121                mExpectedCost = 0;
122                mAvgRenderCost = 0;
123                mDeviation = 0;
124                mTotalPvsCost = 0;
125                mEntriesInPvs = 0;
126                mMemoryCost = 0;
127                mPvsSizeDecr = 0;
128                mVolume = 0;
129        }
130
131
132        void Print(ostream &app) const;
133
134        friend ostream &operator<<(ostream &s, const ViewCellsTreeStats &stat)
135        {
136                stat.Print(s);
137                return s;
138        }
139};
140
141
142/**
143        A view cell. View cells are regions in space. The visibility informations,
144        i.e., the primitives seen by the view cell are stored in a PVs.
145        A view cell can be represented in many different ways, e.g.,
146        a mesh representation.
147*/
148class ViewCell: public MeshInstance
149{
150        friend class ViewCellsTree;
151        friend class ViewCellsManager;
152
153public:
154        ViewCell();
155
156        /** Constructor taking a mesh representing the shape of the viewcell.
157        */
158        ViewCell(Mesh *mesh);
159
160        /** Default destructor.
161        */
162        virtual ~ViewCell();
163        /** Returns Pvs.
164        */
165        const ObjectPvs &GetPvs() const;
166        /** Returns pvs.
167        */
168        ObjectPvs &GetPvs();
169        /** Completely substitutes the pvs.
170        */
171        void SetPvs(const ObjectPvs &pvs);
172        /** Type of view cells.
173        */
174        int Type() const;
175        /** Adds a passing ray to the passing ray container.
176        */
177        void AddPassingRay(const Ray &ray, const int contributions);
178        /** Returns volume of the view cell.
179        */
180        float GetVolume() const;
181        /** Returns area of the view cell.
182        */
183        float GetArea() const;
184        /** Sets the volume of the view cell.
185        */
186        void SetVolume(float volume);
187        /** Sets the area of the view cell.
188        */
189        void SetArea(float area);
190        /** if this view cell is the root of a view cell hierarchy
191        */
192        bool IsRoot() const;
193        /** Returns parent view cell.
194        */
195        ViewCellInterior *GetParent() const;
196        /** Sets parent of this view cell.
197        */
198        void SetParent(ViewCellInterior *parent);
199        /** Sets the mesh for this view cell.
200        */
201        void SetMesh(Mesh *mesh);
202
203        /** Sets this view cell to be a valid view cell according to some criteria.
204        */
205        void SetValid(const bool valid);
206        /** Returns true if this view cell is considered to be valid according to
207                some criteria.
208        */
209        bool GetValid() const;
210
211        /** Returns estimated render cost of this view cell.
212        */
213        float GetRenderCost() const;
214
215        /** set color for visiualizations.
216        */
217        void SetColor(const RgbColor &color);
218
219        /** get color for visualuzations.
220        */
221    RgbColor GetColor() const;
222
223        /** Adds a sample to the pvs.
224                @param sample the sample to be added
225                @param pdf a continuos measure of visibility
226                @param contribution returns the contribution of this sample to the pvs
227        */
228        bool AddPvsSample(Intersectable *sample, const float pdf, float &contribution);
229 
230        /// Rays piercing this view cell.
231        //RayContainer mPiercingRays;
232
233        /** if this is a view cell correspending to a leaf in a hierarchy.
234        */
235        virtual bool IsLeaf() const = 0;
236
237        static bool SmallerPvs(const ViewCell *a, const ViewCell *b)
238        {
239                // HACK: take scalar value because pvs may not have been stored properly
240#if 1
241                return a->mPvsCost < b->mPvsCost;
242#else
243                return a->GetPvs().EvalPvsCost() < b->GetPvs().EvalPvsCost();
244#endif
245        }
246
247        static bool SmallerRenderCost(const ViewCell *a, const ViewCell *b)
248        {
249                return a->GetRenderCost() < b->GetRenderCost();
250        }
251
252        static bool LargerRenderCost(const ViewCell *a, const ViewCell *b)
253        {
254                return a->GetRenderCost() > b->GetRenderCost();
255        }
256
257        /** Sets merge cost used for merging this view cell from other cells.
258                @hack The function is available for leaves also to have a common interface,
259                but it should be less than zero for leaves.
260                */
261        void SetMergeCost(const float mergeCost);
262
263        /** Returns merge cost needed to merge this leaf from other cells.
264                @hack The function is available for leaves also to have a common interface,
265                but it should be less than zero for leaves.
266        */
267        float GetMergeCost() const;
268
269
270        //////////
271        //-- mailing stuff
272
273        static void NewMail(const int reserve = 1)
274        {
275                sMailId += sReservedMailboxes;
276                sReservedMailboxes = reserve;
277        }
278
279        void Mail() { mMailbox = sMailId; }
280        bool Mailed() const { return mMailbox == sMailId; }
281
282        void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
283        bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
284
285        int IncMail() { return ++ mMailbox - sMailId; }
286
287
288        // last mail id -> warning not thread safe!
289        // both mailId and mailbox should be unique for each thread!!!
290        static int sMailId;
291        static int sReservedMailboxes;
292       
293
294protected:
295
296        /// parent view cell in the view cell hierarchy
297        ViewCellInterior *mParent;
298        /// the potentially visible objects
299        ObjectPvs mPvs;
300        /// the volume of this view cell
301        float mVolume;
302        /// the area of this view cell
303        float mArea;
304        /// the cost that were paid for merging this view cells from two others.
305        float mMergeCost;
306        /// if the view cell is valid view space
307        bool mValid;
308        /// color used for consistent visualization
309        RgbColor mColor;
310        /// store pvs size, used for evaluation purpose when pvss are stored only in the leaves
311        float mPvsCost;
312        /** stores number of entries in pvs
313            this variable has the same value as mPvsSize for object pvs,
314                but usually not for kd cell based pvs
315        */
316        int mEntriesInPvs;
317        /** if the pvs size scalar (+ entries into pvs)
318                is up to date and corresponding to the real pvs size
319        */
320        bool mPvsSizeValid;
321};
322
323
324class ViewCellInterior: public ViewCell
325{
326        friend class ViewCellsManager;
327
328public:
329        ViewCellInterior();
330        ~ViewCellInterior();
331
332        ViewCellInterior(Mesh *mesh);
333       
334        /** Sets pointer from parent to child and vice versa.
335        */
336        void SetupChildLink(ViewCell *l);
337        void ReplaceChildLink(ViewCell *prev, ViewCell *cur);
338
339        void RemoveChildLink(ViewCell *l);
340        bool IsLeaf() const;
341
342        void SetCost(const float c) {
343                mCost = c;
344        }
345       
346        float GetCost() const {
347                return mCost;
348        }
349 
350  ViewCellContainer mChildren;
351
352protected:
353  /** overall cost resulting from the merge */
354  float mCost;
355};
356
357
358/**
359        Leaf of the view cell.
360*/
361class ViewCellLeaf: public ViewCell
362{
363public:
364        ViewCellLeaf()  {  mActiveViewCell = this; }
365        ViewCellLeaf(Mesh *mesh):
366        ViewCell(mesh) { mActiveViewCell = this; }
367
368        bool IsLeaf() const
369        {
370                return true;
371        }
372
373        /** Returns active view cell, i.e. this view cell or
374                a parent view cell which is set as active view cell.
375        */
376        ViewCell *GetActiveViewCell() const
377        { return mActiveViewCell; }
378
379        /** Sets this view cell to be an active view cell.
380        */
381        void SetActiveViewCell(ViewCell *vc)
382        { mActiveViewCell = vc;}
383       
384        /** points to the currently active view cell. This is the
385                view cell representing the current brach.
386        */
387        ViewCell *mActiveViewCell;
388};
389
390/** Leaf of the view cell hierarchy corresponding
391        to a leaf in a spatial hierarchy.
392*/
393template<typename T>
394class HierarchyLeafViewCell: public ViewCellLeaf
395{
396public:
397
398        HierarchyLeafViewCell<T>(): ViewCellLeaf() {  }
399        HierarchyLeafViewCell<T>(Mesh *mesh):
400        ViewCellLeaf(mesh) {  }
401               
402        bool IsLeaf() const
403        {
404                return true;
405        }
406
407        /// Leaves of some hierarchy which contains this view cell.
408        vector<T> mLeaves;
409};
410
411
412typedef HierarchyLeafViewCell<VspLeaf *> VspViewCell;
413typedef HierarchyLeafViewCell<BspLeaf *> BspViewCell;
414typedef HierarchyLeafViewCell<KdLeaf *> KdViewCell;
415
416
417
418
419class ViewCellsTree
420{
421        friend class ViewCellsManager;
422        friend class ViewCellsParseHandlers;
423
424public:
425        ViewCellsTree();
426        /** View cells tree constructor taking a view cell mnanager as parameter
427        */
428        ViewCellsTree(ViewCellsManager *vcm);
429        ~ViewCellsTree();
430
431        /** Returns number of leaves this view cell consists of.
432        */
433        int GetNumInitialViewCells(ViewCell *vc) const;
434
435        /** Collects leaves corresponding to a view cell.
436        */
437        void CollectLeaves(ViewCell *vc, ViewCellContainer &leaves) const;
438
439        /** Merges view cells according to some cost heuristics.
440        */
441        int ConstructMergeTree(const VssRayContainer &rays, const ObjectContainer &objects);
442       
443        /** Refines view cells using shuffling, i.e., border leaves
444                of two view cells are exchanged if the resulting view cells
445                are tested to be "better" than the old ones.
446                @returns number of refined view cells
447        */
448        int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
449       
450        /** Assign colors to the viewcells so that they can be renderered interactively without
451            color flickering. 
452        */
453        void AssignRandomColors();
454
455        /** Updates view cell stats for this particular view cell.
456        */
457        void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
458
459        /** Get costs resulting from each merge step.
460        */
461        void GetCostFunction(vector<float> &costFunction);
462       
463        /** Returns storage cost resulting from each merge step.
464        */
465        void GetStorageFunction(vector<int> &storageCost);
466
467        /** Returns optimal set of view cells for a given number of view cells.
468        */
469        void CollectBestViewCellSet(ViewCellContainer &viewCells, const int numViewCells);
470
471        /** Root of view cells tree.
472        */
473        ViewCell *GetRoot() const;
474
475        /** Returns pvs of view cell.
476                @note pvs is returned per reference if tree is not compressed,
477                per copy else.
478        */
479        void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
480
481        /** Returns pvs size (i.e. the render cost of the stored objects)
482        */
483        float GetPvsCost(ViewCell *vc) const;
484
485        /** Returns number of entries associated with this view cell.
486
487                This returns the same value as the "GetPvsSize" function for object pvs
488                but most likely different values if we use object space grouping.
489                E.g., using bounding volumes.
490        */
491        int GetPvsEntries(ViewCell *vc) const;
492
493        /** Returns the number of physically stored entries in the view cells sub tree.
494                This can vary based on the current storage method
495        */
496        int CountStoredPvsEntries(ViewCell *root) const;
497
498        /** Returns memory cost of this view cell.
499        */
500        float GetMemoryCost(ViewCell *vc) const;
501
502        /** Sets method of storage for view cells.
503        */
504        void SetViewCellsStorage(int type);
505
506        /** pvs storage methods
507        */
508        enum {PVS_IN_INTERIORS, COMPRESSED, PVS_IN_LEAVES};
509
510        /** If view cells in this tree have compressed pvs.
511        */
512        int ViewCellsStorage() const;
513
514        /** Returns active view cell that is in the path of this view cell.
515        */
516        ViewCell *GetActiveViewCell(ViewCellLeaf *vc) const;
517
518        /** Sets the leaves to be the currently active view cells.
519        */
520    void SetActiveSetToLeaves();
521
522        /** Propagates pvs up the tree to the root and downwards the tree.
523        */
524        void PropagatePvs(ViewCell *vc);
525
526        /** Exports view cells to file.
527        */
528        bool Export(OUT_STREAM &stream, const bool exportPvs = false);
529
530        /** Export statistics of this view cell tree.
531        */
532        void ExportStats(const string &mergeStats);
533
534        /** Sets root of hierarchy.
535        */
536        void SetRoot(ViewCell *root);
537
538        /** Assignes unique ids to view cells.
539        */
540        void CreateUniqueViewCellsIds();
541
542        /** Resets pvs of whole tree.
543        */
544        void ResetPvs();
545
546        /** Counts pvs of the view cell taking the kd cells into account.
547        */
548        int CountKdPvs(const ViewCellLeaf *vc) const;
549
550        /** Sets pointer to view cells manager.
551        */
552        void SetViewCellsManager(ViewCellsManager *vcm);
553
554        void Update();
555
556protected:
557
558        /** Reads the environment and sets member variables.
559        */
560        void ReadEnvironment();
561
562        /////////////////////////////////////////////////////////////////
563        //                    merge related stuff                      //
564        /////////////////////////////////////////////////////////////////
565
566        /** Computes render cost of the merged pvs.
567        */
568        float ComputeMergedPvsCost(const ObjectPvs &pvs1, const ObjectPvs &pvs2) const;
569
570        /** Returns cost of this leaf according to current heuristics.
571        */
572        float GetCostHeuristics(ViewCell *vc) const;
573
574        /** Returns cost of leaf.
575        */
576        float GetRenderCost(ViewCell *vc) const;
577
578        /** Evaluates the merge cost of this merge candidate pair.
579        */
580        void EvalMergeCost(MergeCandidate &mc) const;
581
582        /** Variance of leaf.
583        */
584        float GetVariance(ViewCell *vc) const;
585
586        /** Standard deviation of leaf.
587        */
588        float GetDeviation(ViewCell *vc) const;
589
590        /** Tries to set this merge candidate to valid.
591                @returns false if both view cells are the same
592        */
593        bool ValidateMergeCandidate(MergeCandidate &mc) const;
594
595        /** Merge view cells of leaves l1 and l2.
596                @returns difference in pvs size
597        */
598        ViewCellInterior *MergeViewCells(ViewCell *l, ViewCell *r, float &pvsDiff); //const;
599
600        /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
601                to view cell 2.
602        */
603        void ShuffleLeaf(ViewCell *leaf, ViewCellInterior *vc1, ViewCellInterior *vc2) const;   
604               
605        /** Shuffles the leaves, i.e., tests if exchanging
606                the leaves helps in improving the view cells.
607        */
608        bool ShuffleLeaves(MergeCandidate &mc) const;
609
610        /** Calculates cost for merge of view cell 1 and 2.
611        */
612        float EvalShuffleCost(ViewCell *leaf,
613                                                  ViewCellInterior *vc1,
614                                                  ViewCellInterior *vc2) const;
615
616        /** Exports a snapshot of the merged view cells to disc.
617        */
618        void ExportMergedViewCells(ViewCellContainer &viewCells,
619                                                           const ObjectContainer &objects,
620                                                           const int numNewViewCells);
621
622        /** Merge queue must be reset after some time because expected value
623                may not be valid.
624        */
625        void ResetMergeQueue();
626
627        /** Updates the current cut of view cells.
628                @returns number of newly merged view cells
629        */
630        int UpdateActiveViewCells(ViewCellContainer &viewCells);
631
632        /** Helper function pullling pvs as high up in the tree as possible.
633        */
634        void PullUpVisibility(ViewCellInterior *interior);
635
636        /** Compress pvs of view cell and children.
637        */
638        void CompressViewCellsPvs(ViewCell *root);
639
640        /** Returns memory usage of view cells.
641        */
642        float GetMemUsage() const;
643
644        /**     Exports single view cell.
645                NOTE: should be in exporter!!
646        */
647        void ExportViewCell(ViewCell *viewCell, OUT_STREAM &stream, const bool exportPvs);     
648
649        /** Exports pvs of a view cell.
650        */
651        void ExportPvs(ViewCell *viewCell, OUT_STREAM &stream);
652
653        /** Counts the logical number of entries in the pvs this view cell.
654                The pvs is assumed to be stored using lossless compression.
655        */
656        int GetEntriesInPvsForCompressedStorage(ViewCell *vc) const;
657
658        /** Computes pvs size of this view cell.
659                The pvs is assumed to be stored using lossless compression.
660        */
661        float GetPvsCostForCompressedStorage(ViewCell *vc) const;
662       
663        /** Computes pvs size of this view cell.
664                The pvs is assumed to be stored in the leaves.
665        */
666        float GetPvsCostForLeafStorage(ViewCell *vc) const;
667
668        /** Counts the logical number of entries in the pvs this view cell.
669                The pvs is assumed to be stored using the leaves.
670        */
671        int GetEntriesInPvsForLeafStorage(ViewCell *vc) const;
672
673        /** Update stats for the log.
674        */
675        void UpdateStats(ofstream &stats,
676                                         const ViewCellsTreeStats &vcStats);
677
678
679        //////////////////////////////////////
680
681        /// if the view cell tree hold compressed pvs
682        int mViewCellsStorage;
683        /// pointer to the view cells manager
684        ViewCellsManager *mViewCellsManager;
685        /// the root of the view cells hierarchy
686        ViewCell *mRoot;
687
688        /// if merge visualization should be shown
689        bool mExportMergedViewCells;
690        /// intermediate container of merged view cells.
691        ViewCellContainer mMergedViewCells;
692        /// if merged view cells are refined.
693        bool mRefineViewCells;
694        /// weights between variance and render cost increase (must be between zero and one)
695        float mRenderCostWeight;
696
697        /// overall cost used to normalize cost ratio
698        float mOverallCost;
699        float mExpectedCost;
700    float mDeviation;
701        float mAvgRenderCost;
702       
703        /// the area is used for pvs heuristics
704        int mUseAreaForPvs;
705        /// number of currently active view cells (=current cut)
706        int mNumActiveViewCells;
707        /// minimal number of view cells
708        int mMergeMinViewCells;
709        /// maximal cost ratio for the merge
710        float mMergeMaxCostRatio;
711
712        typedef priority_queue<MergeCandidate> MergeQueue;
713
714        MergeQueue mMergeQueue;
715
716        float mMaxMemory;
717
718        int mMaxMergesPerPass;
719        float mAvgCostMaxDeviation;
720};
721
722
723/**
724        Candidate for leaf merging based on priority.
725*/
726class MergeCandidate
727
728        friend class ViewCellsTree;
729
730public:
731
732        MergeCandidate(ViewCell *l, ViewCell *r);
733
734        /** If this merge pair is still valid.
735        */
736        bool IsValid() const;
737
738       
739        friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
740        {
741                return leafb.GetMergeCost() < leafa.GetMergeCost();
742        }
743
744        void SetLeftViewCell(ViewCell *l);
745        void SetRightViewCell(ViewCell *l);
746
747        ViewCell *GetLeftViewCell() const;
748        ViewCell *GetRightViewCell() const;
749
750        /** Returns leaf view cell initially associated with this merge candidate.
751        */
752        ViewCell *GetInitialLeftViewCell() const;
753        /** Returns leaf view cell initially associated with this merge candidate.
754        */
755        ViewCell *GetInitialRightViewCell() const;
756
757        /** Returns the increase of the standard deviation of this merge candidate.
758        */
759        float GetDeviationIncr() const;
760
761        /** Merge cost of this candidate pair.
762        */
763        float GetMergeCost() const;
764
765        /** Render cost of this candidate.
766        */
767        float GetRenderCost() const;
768       
769        static float sRenderCostWeight;
770
771protected:
772
773        /// render cost increase by this merge
774        float mRenderCost;
775        /// increase / decrease of standard deviation
776        float mDeviationIncr;
777
778        ViewCell *mLeftViewCell;
779        ViewCell *mRightViewCell;
780
781        ViewCell *mInitialLeftViewCell;
782        ViewCell *mInitialRightViewCell;
783};
784
785
786class MergeStatistics: public StatisticsBase
787{
788public:
789       
790        int merged;
791        int siblings;
792        int candidates;
793        int nodes;
794
795        int accTreeDist;
796        int maxTreeDist;
797       
798        Real collectTime;
799        Real mergeTime;
800
801        Real overallCost;
802
803        Real expectedRenderCost;
804        Real deviation;
805        Real heuristics;
806
807        // Constructor
808        MergeStatistics()
809        {
810                Reset();
811        }
812       
813        double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
814
815        void Reset()
816        {
817                nodes = 0;
818                merged = 0;
819                siblings = 0;
820                candidates = 0;
821       
822                accTreeDist = 0;
823                maxTreeDist = 0;
824
825                collectTime = 0;
826                mergeTime = 0;
827                overallCost = 0;
828
829                expectedRenderCost = 0;
830                deviation = 0;
831                heuristics = 0;
832
833        }
834
835        void Print(ostream &app) const;
836
837        friend ostream &operator<<(ostream &s, const MergeStatistics &stat)
838        {
839                stat.Print(s);
840                return s;
841        }
842};
843
844}
845
846#endif
Note: See TracBrowser for help on using the repository browser.