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

Revision 752, 14.0 KB checked in by mattausch, 19 years ago (diff)

after rendering workshop submissioin
x3dparser can use def - use constructs
implemented improved evaluation (samples are only stored in leaves, only propagate pvs size)

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