source: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h @ 542

Revision 542, 23.0 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef _VspBspTree_H__
2#define _VspBspTree_H__
3
4#include "Mesh.h"
5#include "Containers.h"
6#include "Polygon3.h"
7#include <stack>
8#include "Statistics.h"
9#include "VssRay.h"
10#include "RayInfo.h"
11#include "ViewCellBsp.h"
12
13class ViewCell;
14//class BspViewCell;
15class Plane3;
16class VspBspTree; 
17class BspInterior;
18class BspNode;
19class AxisAlignedBox3;
20class Ray;
21class ViewCellsStatistics;
22class ViewCellsManager;
23class BspMergeCandidate;
24class Beam;
25
26struct BspRay;
27
28
29/**
30        This is a view space partitioning specialised BSPtree. 
31        There are no polygon splits, but we split the sample rays.
32        The candidates for the next split plane are evaluated only
33        by checking the sampled visibility information.
34        The polygons are employed merely as candidates for the next split planes.
35*/
36class VspBspTree
37{
38        friend class ViewCellsParseHandlers;
39        friend class VspBspViewCellsManager;
40public:
41       
42        /** Additional data which is passed down the BSP tree during traversal.
43        */
44        struct VspBspTraversalData
45        { 
46                /// the current node
47                BspNode *mNode;
48                /// polygonal data for splitting
49                PolygonContainer *mPolygons;
50                /// current depth
51                int mDepth;
52                /// rays piercing this node
53                RayInfoContainer *mRays;
54                /// area of current node
55                float mArea;
56                /// geometry of node as induced by planes
57                BspNodeGeometry *mGeometry;
58                /// pvs size
59                int mPvs;
60                /// how often this branch has missed the max-cost ratio
61                int mMaxCostMisses;
62                /// bounding box of current view space.
63                ///AxisAlignedBox3 mBbox;
64               
65                /** Returns average ray contribution.
66                */
67                float GetAvgRayContribution() const
68                {
69                        return (float)mPvs / ((float)mRays->size() + Limits::Small);
70                }
71
72
73                VspBspTraversalData():
74                mNode(NULL),
75                mPolygons(NULL),
76                mDepth(0),
77                mRays(NULL),
78                mPvs(0),
79                mArea(0.0),
80                mGeometry(NULL),
81                mMaxCostMisses(0)
82                //,mIsAxisAligned(false)
83                {}
84               
85                VspBspTraversalData(BspNode *node,
86                                                        PolygonContainer *polys,
87                                                        const int depth,
88                                                        RayInfoContainer *rays,
89                                                        int pvs,
90                                                        float area,
91                                                        BspNodeGeometry *geom):
92                mNode(node),
93                mPolygons(polys),
94                mDepth(depth),
95                mRays(rays),
96                mPvs(pvs),
97                mArea(area),
98                mGeometry(geom),
99                mMaxCostMisses(0)
100                {}
101
102                VspBspTraversalData(PolygonContainer *polys,
103                                                        const int depth,
104                                                        RayInfoContainer *rays,
105                                                        BspNodeGeometry *geom):
106                mNode(NULL),
107                mPolygons(polys),
108                mDepth(depth),
109                mRays(rays),
110                mPvs(0),
111                mArea(0),
112                mGeometry(geom),
113                mMaxCostMisses(0)
114                {}
115
116                /** Returns cost of the traversal data.
117                */
118                float GetCost() const
119                {
120#if 1
121                        return mPvs * mArea;
122#endif
123#if 0
124                        return (float)(mPvs * (int)mRays->size());
125#endif
126#if 0
127                        return (float)mPvs;
128#endif
129#if 0
130                        return mArea * (float)mRays->size();
131#endif
132                }
133
134                // deletes contents and sets them to NULL
135                void Clear()
136                {
137                        DEL_PTR(mPolygons);
138                        DEL_PTR(mRays);
139                        DEL_PTR(mGeometry);
140                }
141
142                friend bool operator<(const VspBspTraversalData &a, const VspBspTraversalData &b)
143                {
144                        return a.GetCost() < b.GetCost();
145                }
146    };
147       
148        typedef std::priority_queue<VspBspTraversalData> VspBspTraversalStack;
149        //typedef std::stack<VspBspTraversalData> VspBspTraversalStack;
150
151        /** Default constructor creating an empty tree.
152        */
153        VspBspTree();
154
155        /** Default destructor.
156        */
157        ~VspBspTree();
158
159        /** Returns BSP Tree statistics.
160        */
161        const BspTreeStatistics &GetStatistics() const;
162 
163
164        /** Constructs the tree from a given set of rays.
165                @param sampleRays the set of sample rays the construction is based on
166                @param viewCells if not NULL, new view cells are
167                created in the leafs and stored in the container
168        */
169        void Construct(const VssRayContainer &sampleRays,
170                                   AxisAlignedBox3 *forcedBoundingBox);
171
172        /** Returns list of BSP leaves with pvs smaller than
173                a certain threshold.
174                @param onlyUnmailed if only the unmailed leaves should be considered
175                @param maxPvs the maximal pvs (-1 means unlimited)
176        */
177        void CollectLeaves(vector<BspLeaf *> &leaves,
178                                           const bool onlyUnmailed = false,
179                                           const int maxPvs = -1) const;
180
181        /** Returns box which bounds the whole tree.
182        */
183        AxisAlignedBox3 GetBoundingBox()const;
184
185        /** Returns root of BSP tree.
186        */
187        BspNode *GetRoot() const;
188
189        /** Collects the leaf view cells of the tree
190                @param viewCells returns the view cells
191        */
192        void CollectViewCells(ViewCellContainer &viewCells) const;
193
194        /** A ray is cast possible intersecting the tree.
195                @param the ray that is cast.
196                @returns the number of intersections with objects stored in the tree.
197        */
198        int CastRay(Ray &ray);
199
200        /// bsp tree construction types
201        enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
202
203        /** finds neighbouring leaves of this tree node.
204        */
205        int FindNeighbors(BspNode *n,
206                                          vector<BspLeaf *> &neighbors,
207                                          const bool onlyUnmailed) const;
208
209        /** Constructs geometry associated with the half space intersections
210                leading to this node.
211        */
212        void ConstructGeometry(BspNode *n, BspNodeGeometry &geom) const;
213       
214        /** Construct geometry of view cell.
215        */
216        void ConstructGeometry(BspViewCell *vc, BspNodeGeometry &geom) const;
217
218        /** Returns random leaf of BSP tree.
219                @param halfspace defines the halfspace from which the leaf is taken.
220        */
221        BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
222
223        /** Returns random leaf of BSP tree.
224                @param onlyUnmailed if only unmailed leaves should be returned.
225        */
226        BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
227
228        /** Returns epsilon of this tree.
229        */
230        float GetEpsilon() const;
231
232        /** Casts line segment into the tree.
233                @param origin the origin of the line segment
234                @param termination the end point of the line segment
235                @returns view cells intersecting the line segment.
236        */
237    int CastLineSegment(const Vector3 &origin,
238                                                const Vector3 &termination,
239                                                ViewCellContainer &viewcells);
240
241               
242        /** Sets pointer to view cells manager.
243        */
244        void SetViewCellsManager(ViewCellsManager *vcm);
245
246        /** Returns distance from node 1 to node 2.
247        */
248        int TreeDistance(BspNode *n1, BspNode *n2) const;
249
250        /** Merges view cells according to some cost heuristics.
251        */
252        int MergeViewCells(const VssRayContainer &rays);
253       
254        /** Refines view cells using shuffling, i.e., border leaves
255                of two view cells are exchanged if the resulting view cells
256                are tested to be "better" than the old ones.
257                @returns number of refined view cells
258        */
259        int RefineViewCells(const VssRayContainer &rays);
260
261        /** Collapses the tree with respect to the view cell partition.
262                @returns number of collapsed nodes
263        */
264        int CollapseTree();
265
266        /** Returns view cell the current point is located in.
267        */
268        ViewCell *GetViewCell(const Vector3 &point);
269
270        /** Constructs bsp rays for post processing and visualization.
271        */
272        void ConstructBspRays(vector<BspRay *> &bspRays,
273                                                  const VssRayContainer &rays);
274       
275        /** Merge view cells of leaves l1 and l2.
276        */
277        bool MergeViewCells(BspLeaf *l1, BspLeaf *l2) const;
278
279        /** Returns true if this view point is in a valid view space,
280                false otherwise.
281        */
282        bool ViewPointValid(const Vector3 &viewPoint) const;
283
284        /** Returns view cell corresponding to
285                the invalid view space.
286        */
287        BspViewCell *GetOutOfBoundsCell();
288
289        /** Writes tree to output stream
290        */
291        bool Export(ofstream &stream);
292
293        /** Casts beam, i.e. a 5D frustum of rays, into tree.
294                Tests conservative using the bounding box of the nodes.
295                @returns number of view cells it intersected
296        */
297        int CastBeam(Beam &beam);
298
299        void CollectViewCells(BspNode *root,
300                                                  ViewCellContainer &viewCells,
301                                                  bool onlyUnmailed = false) const;
302
303        /** returns maximal valid pvs.
304        */
305        int GetMaxPvs() { return mMaxPvs;}
306
307        /** Checks validy of view cells.
308                if not valid, sets regions invalid and deletes view cell.
309        */
310        void CheckValidy();
311
312protected:
313
314        // --------------------------------------------------------------
315        // For sorting objects
316        // --------------------------------------------------------------
317        struct SortableEntry
318        {
319                enum EType
320                {
321                        ERayMin,
322                        ERayMax
323                };
324
325                int type;
326                float value;
327                VssRay *ray;
328 
329                SortableEntry() {}
330                SortableEntry(const int t, const float v, VssRay *r):type(t),
331                                          value(v), ray(r)
332                {
333                }
334               
335                friend bool operator<(const SortableEntry &a, const SortableEntry &b)
336                {
337                        return a.value < b.value;
338                }
339        };
340
341        float EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
342                                                                   const AxisAlignedBox3 &box,
343                                                                   const int axis,
344                                                                   const float &position) const;
345
346        /** Returns view cell corresponding to
347                the invalid view space. If it does not exist, it is created.
348        */
349        BspViewCell *GetOrCreateOutOfBoundsCell();
350
351        /** Collapses the tree with respect to the view cell partition,
352                i.e. leaves having the same view cell are collapsed.
353                @param node the root of the subtree to be collapsed
354                @param collapsed returns the number of collapsed nodes
355                @returns node of type leaf if the node could be collapsed,
356                this node otherwise
357        */
358        BspNode *CollapseTree(BspNode *node, int &collapsed);
359
360        /** Shuffles the leaves, i.e., tests if exchanging
361                the leaves helps in improving the view cells.
362        */
363        bool ShuffleLeaves(BspLeaf *leaf1, BspLeaf *leaf2) const;
364
365        /** Helper function revalidating the view cell leaf list after merge.
366        */
367        void RepairViewCellsLeafLists();
368
369        /** Evaluates tree stats in the BSP tree leafs.
370        */
371        void EvaluateLeafStats(const VspBspTraversalData &data);
372
373        /** Subdivides node with respect to the traversal data.
374            @param tStack current traversal stack
375                @param tData traversal data also holding node to be subdivided
376                @returns new root of the subtree
377        */
378        BspNode *Subdivide(VspBspTraversalStack &tStack,
379                                           VspBspTraversalData &tData);
380
381        /** Constructs the tree from the given traversal data.
382                @param polys stores set of polygons on which subdivision may be based
383                @param rays storesset of rays on which subdivision may be based
384        */
385        void Construct(const PolygonContainer &polys, RayInfoContainer *rays);
386
387        /** Selects the best possible splitting plane.
388                @param plane returns the split plane
389                @param leaf the leaf to be split
390                @param polys the polygon list on which the split decition is based
391                @param rays ray container on which selection may be based
392                @note the polygons can be reordered in the process
393                @returns true if the cost of the split is under maxCostRatio
394
395        */
396        bool SelectPlane(Plane3 &plane,
397                                         BspLeaf *leaf,
398                                         VspBspTraversalData &data,
399                                         VspBspTraversalData &frontData,
400                                         VspBspTraversalData &backData);
401       
402        /** Strategies where the effect of the split plane is tested
403            on all input rays.
404
405                @returns the cost of the candidate split plane
406        */
407        float SplitPlaneCost(const Plane3 &candidatePlane,
408                                                 const VspBspTraversalData &data,
409                                                 BspNodeGeometry &geomFront,
410                                                 BspNodeGeometry &geomBack,
411                                                 float &areaFront,
412                                                 float &areaBack) const;
413
414        /** Subdivide leaf.
415                @param leaf the leaf to be subdivided
416               
417                @param polys the polygons to be split
418                @param frontPolys returns the polygons in front of the split plane
419                @param backPolys returns the polygons in the back of the split plane
420               
421                @param rays the polygons to be filtered
422                @param frontRays returns the polygons in front of the split plane
423                @param backRays returns the polygons in the back of the split plane
424
425                @returns the root of the subdivision
426        */
427
428        BspNode *SubdivideNode(VspBspTraversalData &tData,
429                                                   VspBspTraversalData &frontData,
430                                                   VspBspTraversalData &backData,
431                                                   PolygonContainer &coincident);
432
433        /** Extracts the meshes of the objects and adds them to polygons.
434                Adds object aabb to the aabb of the tree.
435                @param maxPolys the maximal number of objects to be stored as polygons
436                @returns the number of polygons
437        */
438        int AddToPolygonSoup(const ObjectContainer &objects,
439                                                 PolygonContainer &polys,
440                                                 int maxObjects = 0);
441
442        /** Extracts the meshes of the view cells and and adds them to polygons.
443                Adds view cell aabb to the aabb of the tree.
444                @param maxPolys the maximal number of objects to be stored as polygons
445                @returns the number of polygons
446        */
447        int AddToPolygonSoup(const ViewCellContainer &viewCells,
448                                                 PolygonContainer &polys,
449                                                 int maxObjects = 0);
450
451        /** Extract polygons of this mesh and add to polygon container.
452                @param mesh the mesh that drives the polygon construction
453                @param parent the parent intersectable this polygon is constructed from
454                @returns number of polygons
455        */
456        int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
457
458        /** Selects an axis aligned for the next split.
459                @returns cost for this split
460        */
461        float SelectAxisAlignedPlane(Plane3 &plane,
462                                                                 const VspBspTraversalData &tData,
463                                                                 int &axis,
464                                                                 BspNodeGeometry **frontGeom,
465                                                                 BspNodeGeometry **backGeom,
466                                                                 float &frontArea,
467                                                                 float &backArea,
468                                                                 bool useKdSplit);
469
470        /** Sorts split candidates for surface area heuristics for axis aligned splits.
471                @param polys the input for choosing split candidates
472                @param axis the current split axis
473                @param splitCandidates returns sorted list of split candidates
474        */
475        void SortSplitCandidates(const RayInfoContainer &rays, const int axis);
476
477        /** Computes best cost for axis aligned planes.
478        */
479        float BestCostRatioHeuristics(const RayInfoContainer &rays,
480                                                                  const AxisAlignedBox3 &box,
481                                                                  const int pvsSize,
482                                                                  const int &axis,
483                                                                  float &position);
484
485        /** Evaluates cost ratio for axis aligned splits.
486        */
487        /*float EvalCostRatio(const VspBspTraversalData &tData,
488                                                const AxisAlignedBox3 &box,
489                                                const int axis,
490                                                const float position,
491                                                int &raysBack,
492                                                int &raysFront,
493                                                int &pvsBack,
494                                                int &pvsFront);*/
495
496        /** Selects an axis aligned split plane.
497                @Returns true if split is valied
498        */
499        bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
500
501        /** Subdivides the rays into front and back rays according to the split plane.
502               
503                @param plane the split plane
504                @param rays contains the rays to be split. The rays are
505                           distributed into front and back rays.
506                @param frontRays returns rays on the front side of the plane
507                @param backRays returns rays on the back side of the plane
508               
509                @returns the number of splits
510        */
511        int SplitRays(const Plane3 &plane,
512                                  RayInfoContainer &rays,
513                              RayInfoContainer &frontRays,
514                                  RayInfoContainer &backRays);
515
516
517        /** Extracts the split planes representing the space bounded by node n.
518        */
519        void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
520
521        /** Adds the object to the pvs of the front and back leaf with a given classification.
522
523                @param obj the object to be added
524                @param cf the ray classification regarding the split plane
525                @param frontPvs returns the PVS of the front partition
526                @param backPvs returns the PVS of the back partition
527       
528        */
529        void AddObjToPvs(Intersectable *obj,
530                                         const int cf,
531                                         int &frontPvs,
532                                         int &backPvs,
533                                         int &totalPvs) const;
534       
535        /** Computes PVS size induced by the rays.
536        */
537        int ComputePvsSize(const RayInfoContainer &rays) const;
538
539        /** Returns true if tree can be terminated.
540        */
541        inline bool TerminationCriteriaMet(const VspBspTraversalData &data) const;
542
543        /** Computes accumulated ray lenght of this rays.
544        */
545        float AccumulatedRayLength(const RayInfoContainer &rays) const;
546
547        /** Splits polygons with respect to the split plane.
548
549                @param plane the split plane
550                @param polys the polygons to be split. the polygons are consumed and
551                           distributed to the containers frontPolys, backPolys, coincident.
552                @param frontPolys returns the polygons in the front of the split plane
553                @param backPolys returns the polygons in the back of the split plane
554                @param coincident returns the polygons coincident to the split plane
555
556                @returns the number of splits   
557        */
558        int SplitPolygons(const Plane3 &plane,
559                                          PolygonContainer &polys,
560                                          PolygonContainer &frontPolys,
561                                          PolygonContainer &backPolys,
562                                          PolygonContainer &coincident) const;
563
564        /** Adds ray sample contributions to the PVS.
565                @param sampleContributions the number contributions of the samples
566                @param contributingSampels the number of contributing rays
567               
568        */
569        void AddToPvs(BspLeaf *leaf,
570                                  const RayInfoContainer &rays,
571                                  int &sampleContributions,
572                                  int &contributingSamples);
573
574
575        /** Collects candidates for the merge in the merge queue.
576                @param leaves the leaves to be merged
577                @returns number of leaves in queue
578        */
579        int CollectMergeCandidates(const vector<BspLeaf *> leaves);
580        /** Collects candidates for the merge in the merge queue.
581                @returns number of leaves in queue
582        */
583        int CollectMergeCandidates(const VssRayContainer &rays);
584
585        /** Take 3 ray endpoints, where two are minimum and one a maximum
586                point or the other way round.
587        */
588        Plane3 ChooseCandidatePlane(const RayInfoContainer &rays) const;
589
590        /** Take plane normal as plane normal and the midpoint of the ray.
591                PROBLEM: does not resemble any point where visibility is
592                likely to change
593        */
594        Plane3 ChooseCandidatePlane2(const RayInfoContainer &rays) const;
595
596        /** Fit the plane between the two lines so that the plane
597                has equal shortest distance to both lines.
598        */
599        Plane3 ChooseCandidatePlane3(const RayInfoContainer &rays) const;
600 
601        /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
602                to view cell 2.
603        */
604        void ShuffleLeaf(BspLeaf *leaf,
605                                         BspViewCell *vc1,
606                                         BspViewCell *vc2) const;
607
608        /**
609                Checks if this traversal data corresponds to
610                a valid view space region.
611        */
612        bool CheckValid(const VspBspTraversalData &data) const;
613
614        /** Propagates valid flag up the tree.
615        */
616        void PropagateUpValidity(BspNode *node);
617
618        /** Writes the node to disk
619                @note: should be implemented as visitor
620        */
621        void ExportNode(BspNode *node, ofstream &stream);
622
623        /** Returns memory usage of tree.
624        */
625        float GetMemUsage() const;
626
627        /// Pointer to the root of the tree
628        BspNode *mRoot;
629               
630        BspTreeStatistics mStat;
631
632        /// Strategies for choosing next split plane.
633        enum {NO_STRATEGY = 0,
634                  RANDOM_POLYGON = 1,
635                  AXIS_ALIGNED = 2,
636                  LEAST_RAY_SPLITS = 256,
637                  BALANCED_RAYS = 512,
638                  PVS = 1024
639                };
640
641        /// box around the whole view domain
642        AxisAlignedBox3 mBox;
643
644        /// minimal number of rays before subdivision termination
645        int mTermMinRays;
646        /// maximal possible depth
647        int mTermMaxDepth;
648        /// mininum area
649        float mTermMinArea;
650        /// mininum PVS
651        int mTermMinPvs;
652        /// maximal contribution per ray
653        float mTermMaxRayContribution;
654        /// minimal accumulated ray length
655        float mTermMinAccRayLength;
656
657        //-- termination criteria for axis aligned split
658
659        /// minimal number of rays for axis aligned split
660        int mTermMinRaysForAxisAligned;
661        // max ray contribution
662        float mTermMaxRayContriForAxisAligned;
663
664        /// strategy to get the best split plane
665        int mSplitPlaneStrategy;
666        /// number of candidates evaluated for the next split plane
667        int mMaxPolyCandidates;
668        /// number of candidates for split planes evaluated using the rays
669        int mMaxRayCandidates;
670        /// balancing factor for PVS criterium
671        float mCtDivCi;
672
673        //-- axis aligned split criteria
674        float mAxisAlignedCtDivCi;
675        /// spezifies the split border of the axis aligned split
676        float mAxisAlignedSplitBorder;
677
678        /// maximal acceptable cost ratio
679        float mTermMaxCostRatio;
680        /// tolerance value indicating how often the max cost ratio can be failed
681        int mTermMissTolerance;
682
683        //-- factors guiding the split plane heuristics
684        float mLeastRaySplitsFactor;
685        float mBalancedRaysFactor;
686        float mPvsFactor;
687
688        /// if area or accumulated ray lenght should be used for PVS heuristics
689        bool mPvsUseArea;
690        /// tolerance for polygon split
691        float mEpsilon;
692        /// maximal number of test rays used to evaluate candidate split plane
693        int mMaxTests;
694        /// normalizes different bsp split plane criteria
695        float mCostNormalizer;
696        /// maximal number of view cells
697        int mMaxViewCells;
698        /// minimal number of view cells
699        int mMergeMinViewCells;
700        /// maximal cost ratio for the merge
701        float mMergeMaxCostRatio;
702
703        // if rays should be stored in leaves
704        bool mStoreRays;
705       
706        /// if only driving axis should be used for split
707        bool mOnlyDrivingAxis;
708
709        ViewCellsManager *mViewCellsManager;
710
711        vector<SortableEntry> *mSplitCandidates;
712
713
714        typedef priority_queue<BspMergeCandidate> MergeQueue;
715
716        MergeQueue mMergeQueue;
717
718        /// if rays should be used to collect merge candidates
719        bool mUseRaysForMerge;
720       
721        /// maximal allowed pvs so that view cell is valid
722        int mMaxPvs;
723
724        float mMaxPvsRatio;
725
726        /// View cell corresponding to the space outside the valid view space
727        BspViewCell *mOutOfBoundsCell;
728
729        /// if invalid space should be shown
730        bool mShowInvalidSpace;
731
732        int mCurrentViewCellsId;
733
734        /// maximal tree memory
735        float mMaxMemory;
736        /// the tree is out of memory
737        bool mOutOfMemory;
738
739private:
740       
741        static const float sLeastRaySplitsTable[5];
742        /** Evaluates split plane classification with respect to the plane's
743                contribution for balanced rays.
744        */
745        static const float sBalancedRaysTable[5];
746
747        /// Generates unique ids for PVS criterium
748        static void GenerateUniqueIdsForPvs();
749
750        //-- unique ids for PVS criterium
751        static int sFrontId;
752        static int sBackId;
753        static int sFrontAndBackId;
754};
755
756/**
757        Candidate for leaf merging based on priority.
758*/
759class BspMergeCandidate
760
761public:
762
763        BspMergeCandidate(BspLeaf *l1, BspLeaf *l2);
764
765        /** If this merge pair is still valid.
766        */
767        bool Valid() const;
768
769        /** Sets this merge candidate to be valid.
770        */
771        void SetValid();
772
773        friend bool operator<(const BspMergeCandidate &leafa, const BspMergeCandidate &leafb)
774        {
775                return leafb.GetMergeCost() < leafa.GetMergeCost();
776        }
777
778        void SetLeaf1(BspLeaf *l);
779        void SetLeaf2(BspLeaf *l);
780
781        BspLeaf *GetLeaf1();
782        BspLeaf *GetLeaf2();
783
784        /** Merge cost of this candidate pair.
785        */
786        float GetMergeCost() const;
787
788        /** Returns cost of leaf 1.
789        */
790        float GetLeaf1Cost() const;
791        /** Returns cost of leaf 2.
792        */
793        float GetLeaf2Cost() const;
794
795        /// maximal pvs size
796        static int sMaxPvsSize;
797        /// overall cost used to normalize cost ratio
798        static float sOverallCost;
799
800        /** Evaluates the merge costs of the leaves.
801        */
802        void EvalMergeCost();
803
804protected:
805
806        /** Cost of a view cell.
807        */
808        float GetCost(ViewCell *vc) const;
809       
810        int mLeaf1Id;
811        int mLeaf2Id;
812
813        float mMergeCost;
814       
815        BspLeaf *mLeaf1;
816        BspLeaf *mLeaf2;
817};
818
819
820class MergeStatistics: public StatisticsBase
821{
822public:
823       
824        int merged;
825        int siblings;
826        int candidates;
827        int nodes;
828
829        int accTreeDist;
830        int maxTreeDist;
831       
832        Real collectTime;
833        Real mergeTime;
834
835        // Constructor
836        MergeStatistics()
837        {
838                Reset();
839        }
840       
841        double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
842
843        void Reset()
844        {
845                nodes = 0;
846                merged = 0;
847                siblings = 0;
848                candidates = 0;
849       
850                accTreeDist = 0;
851                maxTreeDist = 0;
852
853                collectTime = 0;
854                mergeTime = 0;
855        }
856
857        void Print(ostream &app) const;
858
859        friend ostream &operator<<(ostream &s, const MergeStatistics &stat)
860        {
861                stat.Print(s);
862                return s;
863        }
864};
865
866#endif
Note: See TracBrowser for help on using the repository browser.