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

Revision 1763, 25.1 KB checked in by mattausch, 18 years ago (diff)
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        //-- mailing options
211       
212//      static void NewMail(const int reserve = 1) {
213//              sMailId += sReservedMailboxes;
214//              sReservedMailboxes = reserve;
215//      }
216       
217//      void Mail() { mMailbox = sMailId; }
218//      bool Mailed() const { return mMailbox == sMailId; }
219
220//      void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
221//      bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
222
223//      int IncMail() { return ++ mMailbox - sMailId; }
224
225//      static int sMailId;
226//      //int mMailbox;
227//      static int sReservedMailboxes;
228
229
230
231        /////////////////////////////////////////////
232        //-- inherited functions from Intersectable
233
234        AxisAlignedBox3 GetBox() const { return mBoundingBox; }
235       
236        int CastRay(Ray &ray) { return 0; }
237       
238        bool IsConvex() const { return true; }
239        bool IsWatertight() const { return true; }
240        float IntersectionComplexity() { return 1; }
241 
242        int NumberOfFaces() const { return 6; };
243       
244        int GetRandomSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
245                                                          GtpVisibilityPreprocessor::Vector3 &normal)
246        {
247                // TODO
248                return 0;
249        }
250
251        int GetRandomVisibleSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
252                                                                         GtpVisibilityPreprocessor::Vector3 &normal,
253                                                                         const GtpVisibilityPreprocessor::Vector3 &viewpoint,
254                                                                         const int maxTries)
255        {
256                // TODO
257                return 0;
258        }
259 
260        int Type() const
261        {
262                return Intersectable::BVH_INTERSECTABLE;
263        }
264
265        ostream &Describe(ostream &s) { return s; }
266
267        ///////////////////////////////////
268
269protected:
270       
271        /// the bounding box of the node
272        AxisAlignedBox3 mBoundingBox;
273        /// parent of this node
274        BvhInterior *mParent;
275        int mTimeStamp;
276};
277
278
279/** BSP interior node implementation
280*/
281class BvhInterior: public BvhNode
282{
283public:
284        /** Standard contructor taking a bounding box as argument.
285        */
286        BvhInterior(const AxisAlignedBox3 &bbox);
287        BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent);
288
289        ~BvhInterior();
290        /** @return false since it is an interior node
291        */
292        bool IsLeaf() const;
293       
294        BvhNode *GetBack() { return mBack; }
295        BvhNode *GetFront() { return mFront; }
296
297        /** Replace front or back child with new child.
298        */
299        void ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild);
300
301        /** Replace front and back child.
302        */
303        void SetupChildLinks(BvhNode *front, BvhNode *back);
304
305        friend ostream &operator<<(ostream &s, const BvhInterior &A)
306        {
307                return s << A.mBoundingBox;
308        }
309
310        virtual void CollectObjects(ObjectContainer &objects);
311
312protected:
313
314        /// back node
315        BvhNode *mBack;
316        /// front node
317        BvhNode *mFront;
318};
319
320
321/** BSP leaf node implementation.
322*/
323class BvhLeaf: public BvhNode
324{
325public:
326        /** Standard contructor taking a bounding box as argument.
327        */
328        BvhLeaf(const AxisAlignedBox3 &bbox);
329        BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
330        BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
331
332        ~BvhLeaf();
333
334        /** @return true since it is an interior node
335        */
336        bool IsLeaf() const;
337       
338        SubdivisionCandidate *GetSubdivisionCandidate()// const
339        {
340                return mSubdivisionCandidate;
341        }
342
343        void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
344        {
345                mSubdivisionCandidate = candidate;
346        }
347       
348        virtual void CollectObjects(ObjectContainer &objects);
349
350        /** Returns level of the hierarchy that is "active" right now.
351        */
352        BvhNode *GetActiveNode()
353        {
354                return mActiveNode;
355        }
356
357        /** Returns level of the hierarchy that is "active" right now.
358        */
359        void SetActiveNode(BvhNode *node)
360        {
361                mActiveNode = node;
362        }
363
364public:
365
366        /// Rays piercing this leaf.
367        //VssRayContainer mVssRays;
368        /// objects
369        ObjectContainer mObjects;
370        /// universal counter
371        int mCounter;
372
373protected:
374
375        /// pointer to a split plane candidate splitting this leaf
376        SubdivisionCandidate *mSubdivisionCandidate;
377
378        /// the active node which will be accounted for in the pvs
379        BvhNode *mActiveNode;
380};
381
382
383/** View Space Partitioning tree.
384*/
385class BvHierarchy
386{
387        friend class ViewCellsParseHandlers;
388        friend class HierarchyManager;
389
390protected:
391        struct SortableEntry;
392        typedef vector<SortableEntry> SortableEntryContainer;
393
394public:
395       
396        /** Additional data which is passed down the BSP tree during traversal.
397        */
398        class BvhTraversalData
399        { 
400        public:
401               
402                BvhTraversalData():
403                mNode(NULL),
404                mDepth(0),
405                mProbability(0.0),
406                mMaxCostMisses(0),
407                mAxis(0),
408                mNumRays(0)
409                {
410                        mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
411                }
412               
413                BvhTraversalData(BvhLeaf *node,
414                                                 const int depth,
415                                                 const float v,
416                                                 const int numRays):
417                mNode(node),
418                mDepth(depth),
419                //mBoundingBox(box),
420                mProbability(v),
421                mMaxCostMisses(0),
422                mAxis(0),
423                mNumRays(numRays)
424                {
425                        mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
426                }
427
428                /** Deletes contents and sets them to NULL.
429                */
430                void Clear()
431                {
432                        DEL_PTR(mNode);
433                        DEL_PTR(mSortedObjects[0]);
434                        DEL_PTR(mSortedObjects[1]);
435                        DEL_PTR(mSortedObjects[2]);
436                }
437
438                /// the current node
439                BvhLeaf *mNode;
440                /// current depth
441                int mDepth;
442                /// the probability that this node is seen
443                float mProbability;
444                /// the bounding box of the node
445                //AxisAlignedBox3 mBoundingBox;
446                /// how often this branch has missed the max-cost ratio
447                int mMaxCostMisses;
448                /// current axis
449                int mAxis;
450                /// number of rays
451                int mNumRays;
452                /// the sorted objects for the three dimensions
453                ObjectContainer *mSortedObjects[3];             
454    };
455
456
457        /** Candidate for a object space split.
458        */
459        class BvhSubdivisionCandidate: public SubdivisionCandidate
460        { 
461        public:
462
463        BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
464                {};
465
466                ~BvhSubdivisionCandidate()
467                {
468                        mParentData.Clear();
469                }
470
471                int Type() const { return OBJECT_SPACE; }
472       
473                void EvalCandidate(bool computeSplitplane = true)
474                {
475            mDirty = false;
476                        sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
477                }
478
479                bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
480                {
481                        BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
482
483                        // local or global termination criteria failed
484                        return !n->IsLeaf();           
485                }
486
487                void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
488                                                                        const bool onlyUnmailed)
489                {
490                        sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
491                }
492
493                bool GlobalTerminationCriteriaMet() const
494                {
495                        return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
496                }
497
498                BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
499                                                                const ObjectContainer &backObjects,
500                                                                const BvhTraversalData &tData):
501                mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
502                {}
503
504                float GetPriority() const
505                {
506                        return mPriority;
507                }
508
509                /// pointer to parent tree.
510                static BvHierarchy *sBvHierarchy;
511
512                /// parent data
513                BvhTraversalData mParentData;
514                /// the objects on the front of the potential split
515                ObjectContainer mFrontObjects;
516                /// the objects on the back of the potential split
517                ObjectContainer mBackObjects;
518        };
519
520        /** Struct for traversing line segment.
521        */
522        struct LineTraversalData
523        {
524                BvhNode *mNode;
525                Vector3 mExitPoint;
526               
527                float mMaxT;
528   
529                LineTraversalData () {}
530                LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
531                mNode(n), mExitPoint(p), mMaxT(maxt) {}
532        };
533
534
535        /** Default constructor creating an empty tree.
536        */
537        BvHierarchy();
538
539        /** Default destructor.
540        */
541        ~BvHierarchy();
542
543        /** Returns tree statistics.
544        */
545        const BvhStatistics &GetStatistics() const;
546 
547        /** Returns bounding box of the specified node.
548        */
549        AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
550
551        /** Reads parameters from environment singleton.
552        */
553        void ReadEnvironment();
554
555        /** Evaluates candidate for splitting.
556        */
557        void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
558                                                                  bool computeSplitPlane = true);
559
560        /** Returns vector of leaves.
561        */
562        void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
563
564        /** Returns bounding box of the whole tree (= bbox of root node)
565        */
566        AxisAlignedBox3 GetBoundingBox()const;
567
568        /** Returns root of the view space partitioning tree.
569        */
570        BvhNode *GetRoot() const;
571
572        /** finds neighbouring leaves of this tree node.
573        */
574        int FindNeighbors(BvhLeaf *n,
575                                          vector<BvhLeaf *> &neighbors,
576                                          const bool onlyUnmailed) const;
577
578        /** Returns random leaf of BSP tree.
579                @param halfspace defines the halfspace from which the leaf is taken.
580        */
581        BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
582
583        /** Returns random leaf of BSP tree.
584                @param onlyUnmailed if only unmailed leaves should be returned.
585        */
586        BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
587
588        /** Casts line segment into the tree.
589                @param origin the origin of the line segment
590                @param termination the end point of the line segment
591                @returns view cells intersecting the line segment.
592        */
593    int CastLineSegment(const Vector3 &origin,
594                                                const Vector3 &termination,
595                                                ViewCellContainer &viewcells);
596               
597        /** Sets pointer to view cells manager.
598        */
599        void SetViewCellsManager(ViewCellsManager *vcm);
600
601        /** Writes tree to output stream
602        */
603        bool Export(OUT_STREAM &stream);
604
605        /** Collects rays associated with the objects.
606        */
607        void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
608
609        /** Intersects box with the tree and returns the number of intersected boxes.
610                @returns number of view cells found
611        */
612        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
613                                                                ViewCellContainer &viewCells) const;
614
615        /** Returns leaf the point pt lies in, starting from root.
616        */
617        BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
618
619        /** Sets a pointer to the view cells tree.
620        */
621        ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
622       
623        /** See Get
624        */
625        void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
626
627        /** Returns estimated memory usage of tree.
628        */
629        float GetMemUsage() const;
630
631        /** Sets this node to be an active node.
632        */
633        void SetActive(BvhNode *node) const;
634
635
636        ///////////////////////////
637        // hacks in order to provide interleaved heurisitcs
638
639        BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
640
641        /////////////////////////////////
642
643        static float EvalAbsCost(const ObjectContainer &objects);
644
645        void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
646
647protected:
648
649        /** Returns true if tree can be terminated.
650        */
651        bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
652
653        /** Returns true if global tree can be terminated.
654        */
655        bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
656
657        /** For sorting the objects during the heuristics
658        */
659        struct SortableEntry
660        {
661                Intersectable *mObject;
662                float mPos;
663
664                SortableEntry() {}
665
666                SortableEntry(Intersectable *obj, const float pos):
667                mObject(obj), mPos(pos)
668                {}
669
670                bool operator<(const SortableEntry &b) const
671                {
672                        return mPos < b.mPos;
673                }
674        };
675
676        /** Evaluate balanced object partition.
677        */
678        float EvalLocalObjectPartition(const BvhTraversalData &tData,
679                                                                   const int axis,
680                                                                   ObjectContainer &objectsFront,
681                                                                   ObjectContainer &objectsBack);
682
683        /** Evaluate surface area heuristic for the node.
684        */
685        float EvalSah(const BvhTraversalData &tData,
686                                  const int axis,
687                                  ObjectContainer &objectsFront,
688                                  ObjectContainer &objectsBack);
689
690        /** Computes priority of the traversal data and stores it in tData.
691        */
692        void EvalPriority(BvhTraversalData &tData) const;
693
694        /** Evaluates render cost of the bv induced by these objects
695        */
696        float EvalRenderCost(const ObjectContainer &objects) const;
697
698        /** Evaluates tree stats in the BSP tree leafs.
699        */
700        void EvaluateLeafStats(const BvhTraversalData &data);
701
702        /** Subdivides node using a best split priority queue.
703            @param tQueue the best split priority queue
704                @param splitCandidate the candidate for the next split
705                @param globalCriteriaMet if the global termination criteria were already met
706                @returns new root of the subtree
707        */
708        BvhNode *Subdivide(SplitQueue &tQueue,
709                                           SubdivisionCandidate *splitCandidate,
710                                           const bool globalCriteriaMet);
711       
712        /** Subdivides leaf.
713                @param sc the subdivisionCandidate holding all necessary data for subdivision           
714               
715                @param frontData returns the traversal data for the front node
716                @param backData returns the traversal data for the back node
717
718                @returns the new interior node = the of the subdivision
719        */
720        BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
721                                                           BvhTraversalData &frontData,
722                                                           BvhTraversalData &backData);
723
724        /** Splits the objects for the next subdivision.
725                @returns cost for this split
726        */
727        float SelectObjectPartition(const BvhTraversalData &tData,
728                                                                ObjectContainer &frontObjects,
729                                                                ObjectContainer &backObjects,
730                                                                bool useVisibilityBasedHeuristics);
731       
732        /** Writes the node to disk
733                @note: should be implemented as visitor.
734        */
735        void ExportNode(BvhNode *node, OUT_STREAM &stream);
736
737        /** Exports objects associated with this leaf.
738        */
739        void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
740
741        /** Associates the objects with their bvh leaves.
742        */
743        static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
744
745
746        /////////////////////////////
747        // Helper functions for local cost heuristics
748       
749        /** Prepare split candidates for cost heuristics using axis aligned splits.
750                @param node the current node
751                @param axis the current split axis
752        */
753        void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
754                                                                                   const int axis);
755
756        static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
757                                                                                                 SortableEntryContainer **subdivisionCandidates,
758                                                                                                 const bool sort,
759                                                                                                 const int axis);
760
761        /** Computes object partition with the best cost according to the heurisics.
762                @param tData the traversal data
763                @param axis the split axis
764                @param objectsFront the objects in the front child bv
765                @param objectsBack the objects in the back child bv
766                @param backObjectsStart the iterator marking the position where the back objects begin
767
768                @returns relative cost (relative to parent cost)
769        */
770        float EvalLocalCostHeuristics(const BvhTraversalData &tData,
771                                                                  const int axis,
772                                                                  ObjectContainer &objectsFront,
773                                                                  ObjectContainer &objectsBack);
774
775        /** Evaluates the contribution to the front and back volume
776                when this object is changing sides in the bvs.
777
778                @param object the object
779                @param volLeft updates the left pvs
780                @param volPvs updates the right pvs
781        */
782        void EvalHeuristicsContribution(Intersectable *obj,
783                                                                        float &volLeft,
784                                                                        float &volRight);
785
786        /** Prepares objects for the cost heuristics.
787                @returns sum of volume of associated view cells
788        */
789        float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
790       
791        /** Evaluates cost for a leaf given the surface area heuristics.
792        */
793        float EvalSahCost(BvhLeaf *leaf);
794
795        ////////////////////////////////////////////////
796
797
798        /** Prepares construction for vsp and osp trees.
799        */
800        AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
801                                                                        const AxisAlignedBox3 *parentBox = NULL) const;
802
803        /** Collects list of invalid candidates. Candidates
804                are invalidated by a view space subdivision step
805                that affects this candidate.
806        */
807        void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
808                                                                vector<SubdivisionCandidate *> &dirtyList,
809                                                                const bool onlyUnmailed);
810
811        /** Collect view cells which see this bvh leaf.
812        */
813        int CollectViewCells(const ObjectContainer &objects,
814                                                 ViewCellContainer &viewCells,
815                                                 const bool setCounter,
816                                                 const bool onlyUnmailedRays) const;
817
818        /** Counts the view cells of this object. note: only
819                counts unmailed objects.
820        */
821        int CountViewCells(Intersectable *obj) const;
822
823        /** Counts the view cells seen by this bvh leaf
824        */
825        int CountViewCells(const ObjectContainer &objects) const;
826
827        /** Collects view cells which see an object.
828        */
829        int CollectViewCells(Intersectable *object,
830                                                 ViewCellContainer &viewCells,
831                                                 const bool useMailBoxing,
832                                                 const bool setCounter,
833                                                 const bool onlyUnmailedRays) const;
834
835        /** Evaluates increase in pvs size.
836        */
837        int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate) const;
838
839        /** Rays will be clipped to the bounding box.
840        */
841        void PreprocessRays(BvhLeaf *root,
842                                                const VssRayContainer &sampleRays,
843                                                RayInfoContainer &rays);
844
845        /** Print the subdivision stats in the subdivison log.
846        */
847        void PrintSubdivisionStats(const SubdivisionCandidate &tData);
848
849        /** Prints out the stats for this subdivision.
850        */
851        void AddSubdivisionStats(const int viewCells,
852                                                         const float renderCostDecr,
853                                                         const float totalRenderCost);
854
855        /** Stores rays with objects that see the rays.
856        */
857        int AssociateObjectsWithRays(const VssRayContainer &rays) const;
858
859        /** Tests if object is in this leaf.
860                @note: assumes that objects are sorted by their id.
861        */
862        bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
863
864        /** Prepares the construction of the bv hierarchy and returns
865                the first subdivision candidate.
866        */
867        SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
868                                                                                          const ObjectContainer &objects);
869
870        /** Resets bv hierarchy. E.g. deletes root and resets stats.
871        */
872        SubdivisionCandidate *Reset(const VssRayContainer &rays,
873                                                                const ObjectContainer &objects);
874
875        /** Evaluates volume of view cells that see the objects.
876        */
877        float EvalViewCellsVolume(const ObjectContainer &objects) const;
878
879        /** Assigns or newly creates initial list of sorted objects.
880        */
881        void AssignInitialSortedObjectList(BvhTraversalData &tData);
882
883        /** Assigns sorted objects to front and back data.
884        */
885        void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
886                                                         BvhTraversalData &frontData,
887                                                         BvhTraversalData &backData);
888       
889        /** Creates new root of hierarchy and computes bounding box.
890                Has to be called before the preparation of the subdivision.
891        */
892        void Initialise(const ObjectContainer &objects);
893
894
895protected:
896       
897        /// pre-sorted subdivision candidtes for all three directions.
898        vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
899        /// pointer to the hierarchy of view cells
900        ViewCellsTree *mViewCellsTree;
901        /// The view cells manager
902        ViewCellsManager *mViewCellsManager;
903        /// candidates for placing split planes during cost heuristics
904        vector<SortableEntry> *mSubdivisionCandidates;
905        /// Pointer to the root of the tree
906        BvhNode *mRoot;
907        /// Statistics for the object space partition
908        BvhStatistics mBvhStats;       
909        /// box around the whole view domain
910        AxisAlignedBox3 mBoundingBox;
911        /// the hierarchy manager
912        HierarchyManager *mHierarchyManager;
913
914
915        ////////////////////
916        //-- local termination criteria
917
918        /// maximal possible depth
919        int mTermMaxDepth;
920        /// mininum probability
921        float mTermMinProbability;
922        /// minimal number of objects
923        int mTermMinObjects;
924        /// maximal acceptable cost ratio
925        float mTermMaxCostRatio;
926        /// tolerance value indicating how often the max cost ratio can be failed
927        int mTermMissTolerance;
928        /// minimum number of rays
929        int mTermMinRays;
930
931
932        ////////////////////
933        //-- global termination criteria
934
935        /// the minimal accepted global cost ratio
936        float mTermMinGlobalCostRatio;
937        //// number of accepted misses of the global cost ratio
938        int mTermGlobalCostMissTolerance;
939        /// maximal number of view cells
940        int mTermMaxLeaves;
941        /// maximal tree memory
942        float mMaxMemory;
943        /// the tree is out of memory
944        bool mOutOfMemory;
945
946
947        ////////////////////////////////////////
948        //-- split heuristics based parameters
949       
950        /// if a heuristics should be used for finding a split plane
951    bool mUseCostHeuristics;
952        /// if sah heuristcs should be used for finding a split plane
953        bool mUseSah;
954    /// balancing factor for PVS criterium
955        float mCtDivCi;
956        /// if only driving axis should be used for split
957        bool mOnlyDrivingAxis;
958        /// current time stamp (used for keeping split history)
959        int mTimeStamp;
960        // if rays should be stored in leaves
961        bool mStoreRays;
962        // subdivision stats output file
963        ofstream  mSubdivisionStats;
964        /// keeps track of cost during subdivision
965        float mTotalCost;
966        int mPvsEntries;
967        /// keeps track of overall pvs size during subdivision
968        int mTotalPvsSize;
969        /// number of currenly generated view cells
970        int mCreatedLeaves;
971        /// represents min and max band for sweep
972        float mSplitBorder;
973        /// weight between render cost decrease and node render cost
974        float mRenderCostDecreaseWeight;
975
976        /// if the objects should be sorted in one global step
977        bool mUseGlobalSorting;
978
979        bool mUseBboxAreaForSah;
980
981        SortableEntryContainer *mSortedObjects[3];
982
983        int mMinRaysForVisibility;
984
985        /// constant value for driving the heuristics
986        float mMemoryConst;
987
988        int mMaxTests;
989};
990
991}
992
993#endif
Note: See TracBrowser for help on using the repository browser.