source: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h @ 1843

Revision 1843, 25.2 KB checked in by mattausch, 18 years ago (diff)

warning! changed pvs output because of paper hack

Line 
1#ifndef _BvHierarchy_H__
2#define _BvHierarchy_H__
3
4#include <stack>
5
6#include "Mesh.h"
7#include "Containers.h"
8#include "Statistics.h"
9#include "VssRay.h"
10#include "RayInfo.h"
11#include "gzstream.h"
12#include "SubdivisionCandidate.h"
13#include "AxisAlignedBox3.h"
14#include "IntersectableWrapper.h"
15#include "HierarchyManager.h"
16
17
18namespace GtpVisibilityPreprocessor {
19
20
21class ViewCellLeaf;
22class Plane3;
23class AxisAlignedBox3;
24class Ray;
25class ViewCellsStatistics;
26class ViewCellsManager;
27class MergeCandidate;
28class Beam;
29class ViewCellsTree;
30class Environment;
31class BvhInterior;
32class BvhLeaf;
33class BvhNode;
34class BvhTree;
35class VspTree;
36class ViewCellsContainer;
37class HierarchyManager;
38
39
40/** View space partition statistics.
41*/
42class BvhStatistics: public StatisticsBase
43{
44public:
45       
46        /// Constructor
47        BvhStatistics()
48        {
49                Reset();
50        }
51
52        int Nodes() const {return nodes;}
53        int Interior() const { return nodes / 2; }
54        int Leaves() const { return (nodes / 2) + 1; }
55       
56        double AvgDepth() const
57        { return accumDepth / (double)Leaves(); }
58
59        double AvgObjectRefs() const
60        { return objectRefs / (double)Leaves(); }
61
62        double AvgRayRefs() const
63        { return rayRefs / (double)Leaves(); }
64
65       
66        void Reset()
67        {
68                nodes = 0;
69                splits = 0;
70                maxDepth = 0;
71
72                minDepth = 99999;
73                accumDepth = 0;
74        maxDepthNodes = 0;
75                minProbabilityNodes = 0;
76                maxCostNodes = 0;
77                ///////////////////
78                minObjectsNodes = 0;
79                maxObjectRefs = 0;
80                minObjectRefs = 999999999;
81                objectRefs = 0;
82                emptyNodes = 0;
83
84                ///////////////////
85                minRaysNodes = 0;
86                maxRayRefs = 0;
87                minRayRefs = 999999999;
88                rayRefs = 0;
89                maxRayContriNodes = 0;
90                mGlobalCostMisses = 0;
91        }
92
93
94public:
95
96        // total number of nodes
97        int nodes;
98        // number of splits
99        int splits;
100        // maximal reached depth
101        int maxDepth;
102        // minimal depth
103        int minDepth;
104        // max depth nodes
105        int maxDepthNodes;
106        // accumulated depth (used to compute average)
107        int accumDepth;
108        // minimum area nodes
109        int minProbabilityNodes;
110        /// nodes termination because of max cost ratio;
111        int maxCostNodes;
112        // global cost ratio violations
113        int mGlobalCostMisses;
114
115        //////////////////
116        // nodes with minimum objects
117        int minObjectsNodes;
118        // max number of rays per node
119        int maxObjectRefs;
120        // min number of rays per node
121        int minObjectRefs;
122        /// object references
123        int objectRefs;
124        // leaves with no objects
125        int emptyNodes;
126
127        //////////////////////////
128        // nodes with minimum rays
129        int minRaysNodes;
130        // max number of rays per node
131        int maxRayRefs;
132        // min number of rays per node
133        int minRayRefs;
134        /// object references
135        int rayRefs;
136        /// nodes with max ray contribution
137        int maxRayContriNodes;
138
139        void Print(ostream &app) const;
140
141        friend ostream &operator<<(ostream &s, const BvhStatistics &stat)
142        {
143                stat.Print(s);
144                return s;
145        }
146};
147
148
149/**
150    VspNode abstract class serving for interior and leaf node implementation
151*/
152class BvhNode: public Intersectable
153{
154public:
155       
156        // types of vsp nodes
157        enum {Interior, Leaf};
158
159        BvhNode();
160        BvhNode(const AxisAlignedBox3 &bbox);
161        BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent);
162
163        virtual ~BvhNode(){};
164
165        /** Determines whether this node is a leaf or not
166                @return true if leaf
167        */
168        virtual bool IsLeaf() const = 0;
169
170        /** Determines whether this node is a root
171                @return true if root
172        */
173        virtual bool IsRoot() const;
174
175        /** Returns parent node.
176        */
177        BvhInterior *GetParent();
178
179        /** Sets parent node.
180        */
181        void SetParent(BvhInterior *parent);
182
183        /** collects all objects under this node.
184        */
185        virtual void CollectObjects(ObjectContainer &objects) = 0;
186
187        /** The bounding box specifies the node extent.
188        */
189        inline
190        AxisAlignedBox3 GetBoundingBox() const
191        { return mBoundingBox; }
192
193        /** Sets bouding box of this node.
194        */
195        inline
196        void SetBoundingBox(const AxisAlignedBox3 &boundingBox)
197        { mBoundingBox = boundingBox; }
198
199        /** Cost of mergin this node.
200        */
201        float GetMergeCost() {return (float)-mTimeStamp; }
202
203        virtual int GetRandomEdgePoint(Vector3 &point,
204                                                                 Vector3 &normal);
205
206        inline int GetTimeStamp() const { return mTimeStamp; }
207        inline void SetTimeStamp(const int timeStamp) { mTimeStamp = timeStamp; };
208
209
210        ////////////////////////
211        //-- inherited functions from Intersectable
212
213        AxisAlignedBox3 GetBox() const { return mBoundingBox; }
214       
215        int CastRay(Ray &ray) { return 0; }
216       
217        bool IsConvex() const { return true; }
218        bool IsWatertight() const { return true; }
219        float IntersectionComplexity() { return 1; }
220 
221        int NumberOfFaces() const { return 6; };
222       
223        int GetRandomSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
224                                                          GtpVisibilityPreprocessor::Vector3 &normal)
225        {
226                // TODO
227                return 0;
228        }
229
230        int GetRandomVisibleSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
231                                                                         GtpVisibilityPreprocessor::Vector3 &normal,
232                                                                         const GtpVisibilityPreprocessor::Vector3 &viewpoint,
233                                                                         const int maxTries)
234        {
235                // TODO
236                return 0;
237        }
238 
239        int Type() const
240        {
241                return Intersectable::BVH_INTERSECTABLE;
242        }
243
244        ostream &Describe(ostream &s) { return s; }
245
246        ///////////////////////////////////
247
248        float mRenderCost;
249
250protected:
251       
252        /// the bounding box of the node
253        AxisAlignedBox3 mBoundingBox;
254        /// parent of this node
255        BvhInterior *mParent;
256        int mTimeStamp;
257};
258
259
260/** BSP interior node implementation
261*/
262class BvhInterior: public BvhNode
263{
264public:
265        /** Standard contructor taking a bounding box as argument.
266        */
267        BvhInterior(const AxisAlignedBox3 &bbox);
268        BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent);
269
270        ~BvhInterior();
271        /** @return false since it is an interior node
272        */
273        bool IsLeaf() const;
274       
275        BvhNode *GetBack() { return mBack; }
276        BvhNode *GetFront() { return mFront; }
277
278        /** Replace front or back child with new child.
279        */
280        void ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild);
281
282        /** Replace front and back child.
283        */
284        void SetupChildLinks(BvhNode *front, BvhNode *back);
285
286        friend ostream &operator<<(ostream &s, const BvhInterior &A)
287        {
288                return s << A.mBoundingBox;
289        }
290
291        virtual void CollectObjects(ObjectContainer &objects);
292
293protected:
294
295        /// back node
296        BvhNode *mBack;
297        /// front node
298        BvhNode *mFront;
299};
300
301
302/** BSP leaf node implementation.
303*/
304class BvhLeaf: public BvhNode
305{
306public:
307        /** Standard contructor taking a bounding box as argument.
308        */
309        BvhLeaf(const AxisAlignedBox3 &bbox);
310  BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
311        BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
312
313        ~BvhLeaf();
314
315        /** @return true since it is an interior node
316        */
317        bool IsLeaf() const;
318       
319        SubdivisionCandidate *GetSubdivisionCandidate()// const
320        {
321                return mSubdivisionCandidate;
322        }
323
324        void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
325        {
326                mSubdivisionCandidate = candidate;
327        }
328       
329        virtual void CollectObjects(ObjectContainer &objects);
330
331        /** Returns level of the hierarchy that is "active" right now.
332        */
333        BvhNode *GetActiveNode()
334        {
335                return mActiveNode;
336        }
337
338        /** Returns level of the hierarchy that is "active" right now.
339        */
340        void SetActiveNode(BvhNode *node)
341        {
342                mActiveNode = node;
343        }
344
345public:
346  // gl list use to store the geometry on the gl server
347  int mGlList;
348 
349  /// objects
350  ObjectContainer mObjects;
351 
352protected:
353 
354  /// pointer to a split plane candidate splitting this leaf
355  SubdivisionCandidate *mSubdivisionCandidate;
356 
357  /// the active node which will be accounted for in the pvs
358  BvhNode *mActiveNode;
359};
360
361
362/** View Space Partitioning tree.
363*/
364class BvHierarchy
365{
366        friend class ViewCellsParseHandlers;
367        friend class HierarchyManager;
368
369protected:
370        struct SortableEntry;
371        typedef vector<SortableEntry> SortableEntryContainer;
372
373public:
374       
375        /** Additional data which is passed down the BSP tree during traversal.
376        */
377        class BvhTraversalData
378        { 
379        public:
380               
381                BvhTraversalData():
382                mNode(NULL),
383                mDepth(0),
384                mProbability(0.0),
385                mMaxCostMisses(0),
386                mAxis(0),
387                mNumRays(0)
388                {
389                        for (int i = 0; i < 4; ++ i)
390                                mSortedObjects[i] = NULL;
391                }
392               
393                BvhTraversalData(BvhLeaf *node,
394                                                 const int depth,
395                                                 const float v,
396                                                 const int numRays):
397                mNode(node),
398                mDepth(depth),
399                //mBoundingBox(box),
400                mProbability(v),
401                mMaxCostMisses(0),
402                mAxis(0),
403                mNumRays(numRays)
404                {
405                        for (int i = 0; i < 4; ++ i)
406                                mSortedObjects[i] = NULL;
407                }
408
409                /** Deletes contents and sets them to NULL.
410                */
411                void Clear()
412                {
413                        DEL_PTR(mNode);
414                        for (int i = 0; i < 4; ++ i)
415                                DEL_PTR(mSortedObjects[i]);
416                }
417
418                /// the current node
419                BvhLeaf *mNode;
420                /// current depth
421                int mDepth;
422                /// the probability that this node is seen
423                float mProbability;
424                /// the bounding box of the node
425                //AxisAlignedBox3 mBoundingBox;
426                /// how often this branch has missed the max-cost ratio
427                int mMaxCostMisses;
428                /// current axis
429                int mAxis;
430                /// number of rays
431                int mNumRays;
432                /// the sorted objects for the three dimensions
433                ObjectContainer *mSortedObjects[4];             
434    };
435
436
437        /** Candidate for a object space split.
438        */
439        class BvhSubdivisionCandidate: public SubdivisionCandidate
440        { 
441        public:
442
443        BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
444                {};
445
446                ~BvhSubdivisionCandidate()
447                {
448                        mParentData.Clear();
449                }
450
451                int Type() const { return OBJECT_SPACE; }
452       
453                void EvalCandidate(bool computeSplitplane = true)
454                {
455            mDirty = false;
456                        sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
457                }
458
459                bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
460                {
461                        BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
462
463                        // local or global termination criteria failed
464                        return !n->IsLeaf();           
465                }
466
467                void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
468                                                                        const bool onlyUnmailed)
469                {
470                        sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
471                }
472
473                bool GlobalTerminationCriteriaMet() const
474                {
475                        return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
476                }
477
478                BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
479                                                                const ObjectContainer &backObjects,
480                                                                const BvhTraversalData &tData):
481                mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
482                {}
483
484                float GetPriority() const
485                {
486                        return mPriority;
487                }
488
489                /// pointer to parent tree.
490                static BvHierarchy *sBvHierarchy;
491
492                /// parent data
493                BvhTraversalData mParentData;
494                /// the objects on the front of the potential split
495                ObjectContainer mFrontObjects;
496                /// the objects on the back of the potential split
497                ObjectContainer mBackObjects;
498        };
499
500        /** Struct for traversing line segment.
501        */
502        struct LineTraversalData
503        {
504                BvhNode *mNode;
505                Vector3 mExitPoint;
506               
507                float mMaxT;
508   
509                LineTraversalData () {}
510                LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
511                mNode(n), mExitPoint(p), mMaxT(maxt) {}
512        };
513
514
515        /** Default constructor creating an empty tree.
516        */
517        BvHierarchy();
518
519        /** Default destructor.
520        */
521        ~BvHierarchy();
522
523        /** Returns tree statistics.
524        */
525        const BvhStatistics &GetStatistics() const;
526 
527        /** Returns bounding box of the specified node.
528        */
529        AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
530
531        /** Reads parameters from environment singleton.
532        */
533        void ReadEnvironment();
534
535        /** Evaluates candidate for splitting.
536        */
537        void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
538                                                                  bool computeSplitPlane = true);
539
540        /** Returns vector of leaves.
541        */
542        void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
543
544        /** Returns bounding box of the whole tree (= bbox of root node)
545        */
546        AxisAlignedBox3 GetBoundingBox()const;
547
548        /** Returns root of the view space partitioning tree.
549        */
550        BvhNode *GetRoot() const;
551
552        /** finds neighbouring leaves of this tree node.
553        */
554        int FindNeighbors(BvhLeaf *n,
555                                          vector<BvhLeaf *> &neighbors,
556                                          const bool onlyUnmailed) const;
557
558        /** Returns random leaf of BSP tree.
559                @param halfspace defines the halfspace from which the leaf is taken.
560        */
561        BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
562
563        /** Returns random leaf of BSP tree.
564                @param onlyUnmailed if only unmailed leaves should be returned.
565        */
566        BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
567
568        /** Casts line segment into the tree.
569                @param origin the origin of the line segment
570                @param termination the end point of the line segment
571                @returns view cells intersecting the line segment.
572        */
573    int CastLineSegment(const Vector3 &origin,
574                                                const Vector3 &termination,
575                                                ViewCellContainer &viewcells);
576               
577        /** Sets pointer to view cells manager.
578        */
579        void SetViewCellsManager(ViewCellsManager *vcm);
580
581        /** Writes tree to output stream
582        */
583        bool Export(OUT_STREAM &stream);
584
585        /** Collects rays associated with the objects.
586        */
587        void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
588
589        /** Intersects box with the tree and returns the number of intersected boxes.
590                @returns number of view cells found
591        */
592        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
593                                                                ViewCellContainer &viewCells) const;
594
595        /** Returns leaf the point pt lies in, starting from root.
596        */
597        BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
598
599        /** Sets a pointer to the view cells tree.
600        */
601        ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
602       
603        /** See Get
604        */
605        void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
606
607        /** Returns estimated memory usage of tree.
608        */
609        float GetMemUsage() const;
610
611        /** Sets this node to be an active node.
612        */
613        void SetActive(BvhNode *node) const;
614
615
616        ///////////////////////////
617        // hacks in order to provide interleaved heurisitcs
618
619        BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
620
621        /////////////////////////////////
622
623        static float EvalAbsCost(const ObjectContainer &objects);
624
625        void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
626
627        float GetRenderCostIncrementially(BvhNode *node) const;
628
629        void CreateUniqueObjectIds();
630
631protected:
632
633        /** Returns true if tree can be terminated.
634        */
635        bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
636
637        /** Returns true if global tree can be terminated.
638        */
639        bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
640
641        /** For sorting the objects during the heuristics
642        */
643        struct SortableEntry
644        {
645                Intersectable *mObject;
646                float mPos;
647
648                SortableEntry() {}
649
650                SortableEntry(Intersectable *obj, const float pos):
651                mObject(obj), mPos(pos)
652                {}
653
654                bool operator<(const SortableEntry &b) const
655                {
656                        return mPos < b.mPos;
657                }
658        };
659
660        /** Evaluate balanced object partition.
661        */
662        float EvalLocalObjectPartition(const BvhTraversalData &tData,
663                                                                   const int axis,
664                                                                   ObjectContainer &objectsFront,
665                                                                   ObjectContainer &objectsBack);
666
667        /** Evaluate surface area heuristic for the node.
668        */
669        float EvalSah(const BvhTraversalData &tData,
670                                  const int axis,
671                                  ObjectContainer &objectsFront,
672                                  ObjectContainer &objectsBack);
673
674
675        /** Evaluates render cost of the bv induced by these objects
676        */
677        float EvalRenderCost(const ObjectContainer &objects) const;
678
679        /** Evaluates tree stats in the BSP tree leafs.
680        */
681        void EvaluateLeafStats(const BvhTraversalData &data);
682
683        /** Subdivides node using a best split priority queue.
684            @param tQueue the best split priority queue
685                @param splitCandidate the candidate for the next split
686                @param globalCriteriaMet if the global termination criteria were already met
687                @returns new root of the subtree
688        */
689        BvhNode *Subdivide(SplitQueue &tQueue,
690                                           SubdivisionCandidate *splitCandidate,
691                                           const bool globalCriteriaMet);
692       
693        /** Subdivides leaf.
694                @param sc the subdivisionCandidate holding all necessary data for subdivision           
695               
696                @param frontData returns the traversal data for the front node
697                @param backData returns the traversal data for the back node
698
699                @returns the new interior node = the of the subdivision
700        */
701        BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
702                                                           BvhTraversalData &frontData,
703                                                           BvhTraversalData &backData);
704
705        /** Splits the objects for the next subdivision.
706                @returns cost for this split
707        */
708        float SelectObjectPartition(const BvhTraversalData &tData,
709                                                                ObjectContainer &frontObjects,
710                                                                ObjectContainer &backObjects,
711                                                                bool useVisibilityBasedHeuristics);
712       
713        /** Writes the node to disk
714                @note: should be implemented as visitor.
715        */
716        void ExportNode(BvhNode *node, OUT_STREAM &stream);
717
718        /** Exports objects associated with this leaf.
719        */
720        void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
721
722        /** Associates the objects with their bvh leaves.
723        */
724        static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
725
726
727        /////////////////////////////
728        // Helper functions for local cost heuristics
729       
730        /** Prepare split candidates for cost heuristics using axis aligned splits.
731                @param node the current node
732                @param axis the current split axis
733        */
734        void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
735                                                                                   const int axis);
736
737        static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
738                                                                                                 SortableEntryContainer **subdivisionCandidates,
739                                                                                                 const bool sort,
740                                                                                                 const int axis);
741
742        float EvalPriority(const BvhSubdivisionCandidate &splitCandidate,
743                                           const float renderCostDecr,
744                                           const float oldRenderCost) const;
745
746        /** Computes object partition with the best cost according to the heurisics.
747                @param tData the traversal data
748                @param axis the split axis
749                @param objectsFront the objects in the front child bv
750                @param objectsBack the objects in the back child bv
751                @param backObjectsStart the iterator marking the position where the back objects begin
752
753                @returns relative cost (relative to parent cost)
754        */
755        float EvalLocalCostHeuristics(const BvhTraversalData &tData,
756                                                                  const int axis,
757                                                                  ObjectContainer &objectsFront,
758                                                                  ObjectContainer &objectsBack);
759
760        /** Evaluates the contribution to the front and back volume
761                when this object is changing sides in the bvs.
762
763                @param object the object
764                @param volLeft updates the left pvs
765                @param volPvs updates the right pvs
766        */
767        void EvalHeuristicsContribution(Intersectable *obj,
768                                                                        float &volLeft,
769                                                                        float &volRight);
770
771        /** Prepares objects for the cost heuristics.
772                @returns sum of volume of associated view cells
773        */
774        float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
775       
776        /** Evaluates cost for a leaf given the surface area heuristics.
777        */
778        float EvalSahCost(BvhLeaf *leaf) const;
779
780        ////////////////////////////////////////////////
781
782
783        /** Prepares construction for vsp and osp trees.
784        */
785        AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
786                                                                        const AxisAlignedBox3 *parentBox = NULL) const;
787
788        /** Collects list of invalid candidates. Candidates
789                are invalidated by a view space subdivision step
790                that affects this candidate.
791        */
792        void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
793                                                                vector<SubdivisionCandidate *> &dirtyList,
794                                                                const bool onlyUnmailed);
795
796        /** Collect view cells which see this bvh leaf.
797        */
798        int CollectViewCells(const ObjectContainer &objects,
799                                                 ViewCellContainer &viewCells,
800                                                 const bool setCounter,
801                                                 const bool onlyUnmailedRays) const;
802
803        /** Counts the view cells of this object. note: only
804                counts unmailed objects.
805        */
806        int CountViewCells(Intersectable *obj) const;
807
808        /** Counts the view cells seen by this bvh leaf
809        */
810        int CountViewCells(const ObjectContainer &objects) const;
811
812        /** Collects view cells which see an object.
813        */
814        int CollectViewCells(Intersectable *object,
815                                                 ViewCellContainer &viewCells,
816                                                 const bool useMailBoxing,
817                                                 const bool setCounter,
818                                                 const bool onlyUnmailedRays) const;
819
820        /** Evaluates increase in pvs size.
821        */
822        int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate) const;
823
824        /** Rays will be clipped to the bounding box.
825        */
826        void PreprocessRays(BvhLeaf *root,
827                                                const VssRayContainer &sampleRays,
828                                                RayInfoContainer &rays);
829
830        /** Print the subdivision stats in the subdivison log.
831        */
832        void PrintSubdivisionStats(const SubdivisionCandidate &tData);
833
834        /** Prints out the stats for this subdivision.
835        */
836        void AddSubdivisionStats(const int viewCells,
837                                                         const float renderCostDecr,
838                                                         const float totalRenderCost);
839
840        /** Stores rays with objects that see the rays.
841        */
842        int AssociateObjectsWithRays(const VssRayContainer &rays) const;
843
844        /** Tests if object is in this leaf.
845                @note: assumes that objects are sorted by their id.
846        */
847        bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
848
849        /** Prepares the construction of the bv hierarchy and returns
850                the first subdivision candidate.
851        */
852        void PrepareConstruction(SplitQueue &tQueue,
853                                                         const VssRayContainer &sampleRays,
854                                                         const ObjectContainer &objects);
855
856        /** Resets bv hierarchy. E.g. deletes root and resets stats.
857        */
858        void Reset(SplitQueue &tQueue,
859                           const VssRayContainer &rays,
860                           const ObjectContainer &objects);
861
862        /** Evaluates volume of view cells that see the objects.
863        */
864        float EvalViewCellsVolume(const ObjectContainer &objects) const;
865
866        /** Assigns or newly creates initial list of sorted objects.
867        */
868        void AssignInitialSortedObjectList(BvhTraversalData &tData,
869                                                                           const ObjectContainer &objects);
870
871        /** Assigns sorted objects to front and back data.
872        */
873        void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
874                                                         BvhTraversalData &frontData,
875                                                         BvhTraversalData &backData);
876       
877        /** Creates new root of hierarchy and computes bounding box.
878                Has to be called before the preparation of the subdivision.
879        */
880        void Initialise(const ObjectContainer &objects);
881
882
883        ////////////////////
884        // initial subdivision
885
886        /** Makes an initial parititon of the object space based on
887                some criteria (size, shader)
888        */
889        void ApplyInitialSubdivision(SubdivisionCandidate *firstCandidate,
890                                                                 vector<SubdivisionCandidate *> &candidateContainer);
891
892        void ApplyInitialSplit(const BvhTraversalData &tData,
893                                                   ObjectContainer &frontObjects,
894                                                   ObjectContainer &backObjects);
895
896        bool InitialTerminationCriteriaMet(const BvhTraversalData &tData) const;
897
898
899protected:
900       
901        /// pre-sorted subdivision candidtes for all three directions.
902        vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
903        /// pointer to the hierarchy of view cells
904        ViewCellsTree *mViewCellsTree;
905        /// The view cells manager
906        ViewCellsManager *mViewCellsManager;
907        /// candidates for placing split planes during cost heuristics
908        vector<SortableEntry> *mSubdivisionCandidates;
909        /// Pointer to the root of the tree
910        BvhNode *mRoot;
911        /// Statistics for the object space partition
912        BvhStatistics mBvhStats;       
913        /// box around the whole view domain
914        AxisAlignedBox3 mBoundingBox;
915        /// the hierarchy manager
916        HierarchyManager *mHierarchyManager;
917
918
919        ////////////////////
920        //-- local termination criteria
921
922        /// maximal possible depth
923        int mTermMaxDepth;
924        /// mininum probability
925        float mTermMinProbability;
926        /// minimal number of objects
927        int mTermMinObjects;
928        /// maximal acceptable cost ratio
929        float mTermMaxCostRatio;
930        /// tolerance value indicating how often the max cost ratio can be failed
931        int mTermMissTolerance;
932        /// minimum number of rays
933        int mTermMinRays;
934
935
936        ////////////////////
937        //-- global termination criteria
938
939        /// the minimal accepted global cost ratio
940        float mTermMinGlobalCostRatio;
941        //// number of accepted misses of the global cost ratio
942        int mTermGlobalCostMissTolerance;
943        /// maximal number of view cells
944        int mTermMaxLeaves;
945        /// maximal tree memory
946        float mMaxMemory;
947        /// the tree is out of memory
948        bool mOutOfMemory;
949
950
951        ////////////////////////////////////////
952        //-- split heuristics based parameters
953       
954        /// if a heuristics should be used for finding a split plane
955    bool mUseCostHeuristics;
956        /// if sah heuristcs should be used for finding a split plane
957        bool mUseSah;
958    /// balancing factor for PVS criterium
959        float mCtDivCi;
960        /// if only driving axis should be used for split
961        bool mOnlyDrivingAxis;
962        /// current time stamp (used for keeping split history)
963        int mTimeStamp;
964        // if rays should be stored in leaves
965        bool mStoreRays;
966        // subdivision stats output file
967        ofstream  mSubdivisionStats;
968        /// keeps track of cost during subdivision
969        float mTotalCost;
970        int mPvsEntries;
971        /// keeps track of overall pvs size during subdivision
972        int mTotalPvsSize;
973        /// number of currenly generated view cells
974        int mCreatedLeaves;
975        /// represents min and max band for sweep
976        float mSplitBorder;
977        /// weight between render cost decrease and node render cost
978        float mRenderCostDecreaseWeight;
979
980        /// if the objects should be sorted in one global step
981        bool mUseGlobalSorting;
982
983        bool mUseBboxAreaForSah;
984
985        //SortableEntryContainer *mSortedObjects[4];
986
987        int mMinRaysForVisibility;
988
989        /// constant value for driving the heuristics
990        float mMemoryConst;
991
992        int mMaxTests;
993
994        bool mIsInitialSubdivision;
995
996        bool mApplyInitialPartition;
997       
998        int mInitialMinObjects;
999        float mInitialMaxAreaRatio;
1000        float mInitialMinArea;
1001};
1002
1003}
1004
1005#endif
Note: See TracBrowser for help on using the repository browser.