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

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