source: obsolete/trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h @ 613

Revision 613, 12.4 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
360protected:
361
362
363        //////////////////////////////////////////////////////////////
364        //                 merge options                            //
365        //////////////////////////////////////////////////////////////
366       
367
368        /** Returns cost of this leaf according to current heuristics.
369        */
370        float GetCostHeuristics(ViewCell *vc) const;
371
372        /** Returns cost of leaf.
373        */
374        float GetRenderCost(ViewCell *vc) const;
375
376        /** Evaluates the merge cost of this merge candidate pair.
377        */
378        void EvalMergeCost(MergeCandidate &mc) const;
379
380        /** Variance of leaf.
381        */
382        float GetVariance(ViewCell *vc) const;
383
384        /** Standard deviation of leaf.
385        */
386        float GetDeviation(ViewCell *vc) const;
387
388        /** Tries to set this merge candidate to valid.
389                @returns false if both view cells are the same
390        */
391        bool ValidateMergeCandidate(MergeCandidate &mc) const;
392
393        /** Merge view cells of leaves l1 and l2.
394                @returns difference in pvs size
395        */
396        ViewCellInterior *MergeViewCells(ViewCell *l, ViewCell *r, int &pvsDiff); //const;
397
398        /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
399                to view cell 2.
400        */
401        void ShuffleLeaf(ViewCell *leaf, ViewCellInterior *vc1, ViewCellInterior *vc2) const;   
402               
403        /** Shuffles the leaves, i.e., tests if exchanging
404                the leaves helps in improving the view cells.
405        */
406        bool ShuffleLeaves(MergeCandidate &mc) const;
407
408        /** Calculates cost for merge of view cell 1 and 2.
409        */
410        float EvalShuffleCost(ViewCell *leaf,
411                                                  ViewCellInterior *vc1,
412                                                  ViewCellInterior *vc2) const;
413
414        /** Exports a snapshot of the merged view cells to disc.
415        */
416        void ExportMergedViewCells(ViewCellContainer &viewCells,
417                                                           const ObjectContainer &objects,
418                                                           const int numNewViewCells);
419
420        /** merge queue must be reset after some time because expected value
421                may not be valid.
422        */
423        void ResetMergeQueue();
424
425        /** Updates the current top level of view cells.
426                @returns number of newly merged view cells
427        */
428        int UpdateActiveViewCells(ViewCellContainer &viewCells);
429
430        void PullUpVisibility(ViewCellInterior *interior);
431
432        void CompressViewCellsPvs(ViewCell *root);
433
434        /** Returns memory usage of view cells.
435        */
436        float GetMemUsage() const;
437       
438        /**
439                Exports single view cell.
440                NOTE: should be in exporter!!
441        */
442        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
443
444
445
446        /// if the view cell tree hold compressed pvs
447        bool mIsCompressed;
448
449        ViewCellsManager *mViewCellsManager;
450        ViewCell *mRoot;
451
452        /// if merge visualization should be shown
453        bool mExportMergedViewCells;
454
455       
456               
457        ViewCellContainer mMergedViewCells;
458       
459
460        bool mRefineViewCells;
461
462        /// weights between variance and render cost increase (must be between zero and one)
463        float mRenderCostWeight;
464
465        /// overall cost used to normalize cost ratio
466        float mOverallCost;
467        float mExpectedCost;
468    float mDeviation;
469        float mAvgRenderCost;
470
471        int mUseAreaForPvs;
472
473        int mNumActiveViewCells;
474
475        /// minimal number of view cells
476        int mMergeMinViewCells;
477        /// maximal cost ratio for the merge
478        float mMergeMaxCostRatio;
479
480        ofstream mStats;
481
482        typedef priority_queue<MergeCandidate> MergeQueue;
483
484        MergeQueue mMergeQueue;
485
486        float mMaxMemory;
487
488};
489
490
491/**
492        Candidate for leaf merging based on priority.
493*/
494class MergeCandidate
495
496        friend class ViewCellsTree;
497
498public:
499
500        MergeCandidate(ViewCell *l, ViewCell *r);
501
502        /** If this merge pair is still valid.
503        */
504        bool IsValid() const;
505
506       
507        friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
508        {
509                return leafb.GetMergeCost() < leafa.GetMergeCost();
510        }
511
512        void SetLeftViewCell(ViewCell *l);
513        void SetRightViewCell(ViewCell *l);
514
515        ViewCell *GetLeftViewCell() const;
516        ViewCell *GetRightViewCell() const;
517
518        ViewCell *GetInitialLeftViewCell() const;
519        ViewCell *GetInitialRightViewCell() const;
520
521        /** Returns the increase of the standard deviation of this merge candidate.
522        */
523        float GetDeviationIncr() const;
524
525        /** Merge cost of this candidate pair.
526        */
527        float GetMergeCost() const;
528
529        /** Render cost of this candidate.
530        */
531        float GetRenderCost() const;
532       
533        static float sRenderCostWeight;
534
535protected:
536
537        /// render cost increase by this merge
538        float mRenderCost;
539        /// increase / decrease of standard deviation
540        float mDeviationIncr;
541
542        ViewCell *mLeftViewCell;
543        ViewCell *mRightViewCell;
544
545        ViewCell *mInitialLeftViewCell;
546        ViewCell *mInitialRightViewCell;
547};
548
549
550class MergeStatistics: public StatisticsBase
551{
552public:
553       
554        int merged;
555        int siblings;
556        int candidates;
557        int nodes;
558
559        int accTreeDist;
560        int maxTreeDist;
561       
562        Real collectTime;
563        Real mergeTime;
564
565        Real overallCost;
566
567        Real expectedRenderCost;
568        Real deviation;
569        Real heuristics;
570
571        // Constructor
572        MergeStatistics()
573        {
574                Reset();
575        }
576       
577        double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
578
579        void Reset()
580        {
581                nodes = 0;
582                merged = 0;
583                siblings = 0;
584                candidates = 0;
585       
586                accTreeDist = 0;
587                maxTreeDist = 0;
588
589                collectTime = 0;
590                mergeTime = 0;
591                overallCost = 0;
592
593                expectedRenderCost = 0;
594                deviation = 0;
595                heuristics = 0;
596
597        }
598
599        void Print(ostream &app) const;
600
601        friend ostream &operator<<(ostream &s, const MergeStatistics &stat)
602        {
603                stat.Print(s);
604                return s;
605        }
606};
607
608#endif
Note: See TracBrowser for help on using the repository browser.