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

Revision 729, 13.5 KB checked in by mattausch, 18 years ago (diff)

added cost flexible render cost measurements

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        /** Returns optimal set of view cells for a given number of view cells.
328        */
329        void CollectBestViewCellSet(ViewCellContainer &viewCells, const int numViewCells);
330
331        /** Root of view cells tree.
332        */
333        ViewCell *GetRoot() const;
334
335        /** Returns pvs of view cell.
336                @note pvs is returned per reference if tree is not compressed,
337                per copy else.
338        */
339        void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
340
341        /** Returns pvs size of view cell.
342        */
343        int GetPvsSize(ViewCell *vc) const;
344
345        /** Returns actual number of object in this pvs and the children.
346        */
347        int GetNumPvsEntries(ViewCell *vc) const;
348
349        /** Returns memory cost of this view cell.
350        */
351        float GetMemoryCost(ViewCell *vc) const;
352
353        /** Compresses the pvs of the view cells from the root.
354        */
355        void CompressViewCellsPvs();
356
357        /** If view cells in this tree have compressed pvs.
358        */
359        bool IsCompressed() const;
360
361        /** Returns active view cell that is in the path of this view cell.
362        */
363        ViewCell *GetActiveViewCell(ViewCell *vc) const;
364
365        /** Sets the leaves to be the currently active view cells.
366        */
367    void SetActiveSetToLeaves();
368
369        /** Propagates pvs up the tree to the root and downwards the tree.
370        */
371        void PropagatePvs(ViewCell *vc);
372
373        bool Export(ofstream &stream);
374
375        /** Export statistics of this view cell tree.
376        */
377        void ExportStats(const string &mergeStats);
378
379        /** Sets root of hierarchy.
380        */
381        void SetRoot(ViewCell *root);
382
383        //float ComputeVolume(ViewCell *vc);
384
385        /** Assignes unique ids to view cells.
386        */
387        void CreateUniqueViewCellsIds();
388
389        /** Resets pvs of whole tree.
390        */
391        void ResetPvs();
392
393protected:
394
395
396        //////////////////////////////////////////////////////////////
397        //                 merge related stuff                      //
398        //////////////////////////////////////////////////////////////
399
400        /** Computes render cost of the merged pvs.
401        */
402        float ComputeMergedPvsCost(const ObjectPvs &pvs1, const ObjectPvs &pvs2) const;
403
404        /** Returns cost of this leaf according to current heuristics.
405        */
406        float GetCostHeuristics(ViewCell *vc) const;
407
408        /** Returns cost of leaf.
409        */
410        float GetRenderCost(ViewCell *vc) const;
411
412        /** Evaluates the merge cost of this merge candidate pair.
413        */
414        void EvalMergeCost(MergeCandidate &mc) const;
415
416        /** Variance of leaf.
417        */
418        float GetVariance(ViewCell *vc) const;
419
420        /** Standard deviation of leaf.
421        */
422        float GetDeviation(ViewCell *vc) const;
423
424        /** Tries to set this merge candidate to valid.
425                @returns false if both view cells are the same
426        */
427        bool ValidateMergeCandidate(MergeCandidate &mc) const;
428
429        /** Merge view cells of leaves l1 and l2.
430                @returns difference in pvs size
431        */
432        ViewCellInterior *MergeViewCells(ViewCell *l, ViewCell *r, int &pvsDiff); //const;
433
434        /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
435                to view cell 2.
436        */
437        void ShuffleLeaf(ViewCell *leaf, ViewCellInterior *vc1, ViewCellInterior *vc2) const;   
438               
439        /** Shuffles the leaves, i.e., tests if exchanging
440                the leaves helps in improving the view cells.
441        */
442        bool ShuffleLeaves(MergeCandidate &mc) const;
443
444        /** Calculates cost for merge of view cell 1 and 2.
445        */
446        float EvalShuffleCost(ViewCell *leaf,
447                                                  ViewCellInterior *vc1,
448                                                  ViewCellInterior *vc2) const;
449
450        /** Exports a snapshot of the merged view cells to disc.
451        */
452        void ExportMergedViewCells(ViewCellContainer &viewCells,
453                                                           const ObjectContainer &objects,
454                                                           const int numNewViewCells);
455
456        /** merge queue must be reset after some time because expected value
457                may not be valid.
458        */
459        void ResetMergeQueue();
460
461        /** Updates the current top level of view cells.
462                @returns number of newly merged view cells
463        */
464        int UpdateActiveViewCells(ViewCellContainer &viewCells);
465
466        void PullUpVisibility(ViewCellInterior *interior);
467
468        void CompressViewCellsPvs(ViewCell *root);
469
470        /** Returns memory usage of view cells.
471        */
472        float GetMemUsage() const;
473       
474        /**
475                Exports single view cell.
476                NOTE: should be in exporter!!
477        */
478        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
479
480
481
482        /// if the view cell tree hold compressed pvs
483        bool mIsCompressed;
484
485        ViewCellsManager *mViewCellsManager;
486        ViewCell *mRoot;
487
488        /// if merge visualization should be shown
489        bool mExportMergedViewCells;
490
491       
492               
493        ViewCellContainer mMergedViewCells;
494       
495
496        bool mRefineViewCells;
497
498        /// weights between variance and render cost increase (must be between zero and one)
499        float mRenderCostWeight;
500
501        /// overall cost used to normalize cost ratio
502        float mOverallCost;
503        float mExpectedCost;
504    float mDeviation;
505        float mAvgRenderCost;
506
507        int mUseAreaForPvs;
508
509        int mNumActiveViewCells;
510
511        /// minimal number of view cells
512        int mMergeMinViewCells;
513        /// maximal cost ratio for the merge
514        float mMergeMaxCostRatio;
515
516        typedef priority_queue<MergeCandidate> MergeQueue;
517
518        MergeQueue mMergeQueue;
519
520        float mMaxMemory;
521
522};
523
524
525/**
526        Candidate for leaf merging based on priority.
527*/
528class MergeCandidate
529
530        friend class ViewCellsTree;
531
532public:
533
534        MergeCandidate(ViewCell *l, ViewCell *r);
535
536        /** If this merge pair is still valid.
537        */
538        bool IsValid() const;
539
540       
541        friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
542        {
543                return leafb.GetMergeCost() < leafa.GetMergeCost();
544        }
545
546        void SetLeftViewCell(ViewCell *l);
547        void SetRightViewCell(ViewCell *l);
548
549        ViewCell *GetLeftViewCell() const;
550        ViewCell *GetRightViewCell() const;
551
552        /** Returns leaf view cell initially associated with this merge candidate.
553        */
554        ViewCell *GetInitialLeftViewCell() const;
555        ViewCell *GetInitialRightViewCell() const;
556
557        /** Returns the increase of the standard deviation of this merge candidate.
558        */
559        float GetDeviationIncr() const;
560
561        /** Merge cost of this candidate pair.
562        */
563        float GetMergeCost() const;
564
565        /** Render cost of this candidate.
566        */
567        float GetRenderCost() const;
568       
569        static float sRenderCostWeight;
570
571protected:
572
573        /// render cost increase by this merge
574        float mRenderCost;
575        /// increase / decrease of standard deviation
576        float mDeviationIncr;
577
578        ViewCell *mLeftViewCell;
579        ViewCell *mRightViewCell;
580
581        ViewCell *mInitialLeftViewCell;
582        ViewCell *mInitialRightViewCell;
583};
584
585
586class MergeStatistics: public StatisticsBase
587{
588public:
589       
590        int merged;
591        int siblings;
592        int candidates;
593        int nodes;
594
595        int accTreeDist;
596        int maxTreeDist;
597       
598        Real collectTime;
599        Real mergeTime;
600
601        Real overallCost;
602
603        Real expectedRenderCost;
604        Real deviation;
605        Real heuristics;
606
607        // Constructor
608        MergeStatistics()
609        {
610                Reset();
611        }
612       
613        double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
614
615        void Reset()
616        {
617                nodes = 0;
618                merged = 0;
619                siblings = 0;
620                candidates = 0;
621       
622                accTreeDist = 0;
623                maxTreeDist = 0;
624
625                collectTime = 0;
626                mergeTime = 0;
627                overallCost = 0;
628
629                expectedRenderCost = 0;
630                deviation = 0;
631                heuristics = 0;
632
633        }
634
635        void Print(ostream &app) const;
636
637        friend ostream &operator<<(ostream &s, const MergeStatistics &stat)
638        {
639                stat.Print(s);
640                return s;
641        }
642};
643
644#endif
Note: See TracBrowser for help on using the repository browser.