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

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