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

Revision 660, 13.1 KB checked in by mattausch, 19 years ago (diff)

adding function for testing purpose

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