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

Revision 728, 13.3 KB checked in by mattausch, 18 years ago (diff)

added histogram (not tested)
last version before updating render cost evaluation

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