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

Revision 1913, 25.7 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        ////////////////////////
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                mMaxCostMisses(0),
385                mAxis(0),
386                mNumRays(0),
387                mCorrectedPvs(0),
388                mPvs(0),
389                mCorrectedVolume(0),
390                mVolume(0)
391                {
392                        for (int i = 0; i < 4; ++ i)
393                                mSortedObjects[i] = NULL;
394                }
395               
396                BvhTraversalData(BvhLeaf *node,
397                                                 const int depth,
398                                                 const float v,
399                                                 const int numRays):
400                mNode(node),
401                mDepth(depth),
402                mMaxCostMisses(0),
403                mAxis(0),
404                mNumRays(numRays),
405                mCorrectedPvs(0),
406                mPvs(0),
407                mCorrectedVolume(0),
408                mVolume(v)
409                {
410                        for (int i = 0; i < 4; ++ i)
411                                mSortedObjects[i] = NULL;
412                }
413
414                /** Deletes contents and sets them to NULL.
415                */
416                void Clear()
417                {
418                        DEL_PTR(mNode);
419                        for (int i = 0; i < 4; ++ i)
420                                DEL_PTR(mSortedObjects[i]);
421                }
422
423                /// the current node
424                BvhLeaf *mNode;
425                /// current depth
426                int mDepth;
427                /// the volume of the node
428                float mVolume;
429                /// the corrected volume
430                float mCorrectedVolume;
431                /// how often this branch has missed the max-cost ratio
432                int mMaxCostMisses;
433                /// current axis
434                int mAxis;
435                /// number of rays
436                int mNumRays;
437                /// parent Pvs;
438                float mPvs;
439                /// parent pvs correction factor
440                float mCorrectedPvs;
441
442                /// the sorted objects for the three dimensions
443                ObjectContainer *mSortedObjects[4];             
444    };
445
446
447        /** Candidate for a object space split.
448        */
449        class BvhSubdivisionCandidate: public SubdivisionCandidate
450        { 
451        public:
452
453        BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
454                {};
455
456                ~BvhSubdivisionCandidate()
457                {
458                        mParentData.Clear();
459                }
460
461                int Type() const { return OBJECT_SPACE; }
462       
463                void EvalCandidate(bool computeSplitplane = true)
464                {
465            mDirty = false;
466                        sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
467                }
468
469                bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
470                {
471                        BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
472
473                        // local or global termination criteria failed
474                        return !n->IsLeaf();           
475                }
476
477                void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
478                                                                        const bool onlyUnmailed)
479                {
480                        sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
481                }
482
483                bool GlobalTerminationCriteriaMet() const
484                {
485                        return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
486                }
487
488                BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
489                                                                const ObjectContainer &backObjects,
490                                                                const BvhTraversalData &tData):
491                mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
492                {}
493
494                float GetPriority() const
495                {
496                        return mPriority;
497                }
498
499                /////////////////////////////7
500
501                /// pointer to parent tree.
502                static BvHierarchy *sBvHierarchy;
503
504                /// parent data
505                BvhTraversalData mParentData;
506                /// the objects on the front of the potential split
507                ObjectContainer mFrontObjects;
508                /// the objects on the back of the potential split
509                ObjectContainer mBackObjects;
510                       
511                float mCorrectedFrontPvs;
512                float mCorrectedBackPvs;
513
514                float mCorrectedFrontVolume;
515                float mCorrectedBackVolume;
516        };
517
518        /** Struct for traversing line segment.
519        */
520        struct LineTraversalData
521        {
522                BvhNode *mNode;
523                Vector3 mExitPoint;
524               
525                float mMaxT;
526   
527                LineTraversalData () {}
528                LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
529                mNode(n), mExitPoint(p), mMaxT(maxt) {}
530        };
531
532
533        /** Default constructor creating an empty tree.
534        */
535        BvHierarchy();
536
537        /** Default destructor.
538        */
539        ~BvHierarchy();
540
541        /** Returns tree statistics.
542        */
543        const BvhStatistics &GetStatistics() const;
544 
545        /** Returns bounding box of the specified node.
546        */
547        AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
548
549        /** Reads parameters from environment singleton.
550        */
551        void ReadEnvironment();
552
553        /** Evaluates candidate for splitting.
554        */
555        void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
556                                                                  bool computeSplitPlane = true);
557
558        /** Returns vector of leaves.
559        */
560        void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
561
562        /** Returns bounding box of the whole tree (= bbox of root node)
563        */
564        AxisAlignedBox3 GetBoundingBox()const;
565
566        /** Returns root of the view space partitioning tree.
567        */
568        BvhNode *GetRoot() const;
569
570        /** finds neighbouring leaves of this tree node.
571        */
572        int FindNeighbors(BvhLeaf *n,
573                                          vector<BvhLeaf *> &neighbors,
574                                          const bool onlyUnmailed) const;
575
576        /** Returns random leaf of BSP tree.
577                @param halfspace defines the halfspace from which the leaf is taken.
578        */
579        BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
580
581        /** Returns random leaf of BSP tree.
582                @param onlyUnmailed if only unmailed leaves should be returned.
583        */
584        BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
585
586        /** Casts line segment into the tree.
587                @param origin the origin of the line segment
588                @param termination the end point of the line segment
589                @returns view cells intersecting the line segment.
590        */
591    int CastLineSegment(const Vector3 &origin,
592                                                const Vector3 &termination,
593                                                ViewCellContainer &viewcells);
594               
595        /** Sets pointer to view cells manager.
596        */
597        void SetViewCellsManager(ViewCellsManager *vcm);
598
599        float GetViewSpaceVolume() const;
600        /** Writes tree to output stream
601        */
602        bool Export(OUT_STREAM &stream);
603
604        /** Collects rays associated with the objects.
605        */
606        void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
607
608        /** Intersects box with the tree and returns the number of intersected boxes.
609                @returns number of view cells found
610        */
611        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
612                                                                ViewCellContainer &viewCells) const;
613
614        /** Returns leaf the point pt lies in, starting from root.
615        */
616        BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
617
618        /** Sets a pointer to the view cells tree.
619        */
620        ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
621       
622        /** See Get
623        */
624        void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
625
626        /** Returns estimated memory usage of tree.
627        */
628        float GetMemUsage() const;
629
630        /** Sets this node to be an active node.
631        */
632        void SetActive(BvhNode *node) const;
633
634
635        ///////////////////////////
636        // hacks in order to provide interleaved heurisitcs
637
638        BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
639
640        /////////////////////////////////
641
642        static float EvalAbsCost(const ObjectContainer &objects);
643
644        float EvalProb(const ObjectContainer &objects) const;
645
646        void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
647
648        float GetRenderCostIncrementially(BvhNode *node) const;
649
650        void Compress();
651        void CreateUniqueObjectIds();
652
653protected:
654
655        /** Returns true if tree can be terminated.
656        */
657        bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
658
659        /** Returns true if global tree can be terminated.
660        */
661        bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
662
663        /** For sorting the objects during the heuristics
664        */
665        struct SortableEntry
666        {
667                Intersectable *mObject;
668                float mPos;
669
670                SortableEntry() {}
671
672                SortableEntry(Intersectable *obj, const float pos):
673                mObject(obj), mPos(pos)
674                {}
675
676                bool operator<(const SortableEntry &b) const
677                {
678                        return mPos < b.mPos;
679                }
680        };
681
682        /** Evaluate balanced object partition.
683        */
684        float EvalLocalObjectPartition(const BvhTraversalData &tData,
685                                                                   const int axis,
686                                                                   ObjectContainer &objectsFront,
687                                                                   ObjectContainer &objectsBack);
688
689        /** Evaluate surface area heuristic for the node.
690        */
691        float EvalSah(const BvhTraversalData &tData,
692                                  const int axis,
693                                  ObjectContainer &objectsFront,
694                                  ObjectContainer &objectsBack);
695
696
697        /** Evaluates render cost of the bv induced by these objects
698        */
699        float EvalRenderCost(const ObjectContainer &objects) const;
700
701        /** Evaluates tree stats in the BSP tree leafs.
702        */
703        void EvaluateLeafStats(const BvhTraversalData &data);
704
705        /** Subdivides node using a best split priority queue.
706            @param tQueue the best split priority queue
707                @param splitCandidate the candidate for the next split
708                @param globalCriteriaMet if the global termination criteria were already met
709                @returns new root of the subtree
710        */
711        BvhNode *Subdivide(SplitQueue &tQueue,
712                                           SubdivisionCandidate *splitCandidate,
713                                           const bool globalCriteriaMet);
714       
715        /** Subdivides leaf.
716                @param sc the subdivisionCandidate holding all necessary data for subdivision           
717               
718                @param frontData returns the traversal data for the front node
719                @param backData returns the traversal data for the back node
720
721                @returns the new interior node = the of the subdivision
722        */
723        BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
724                                                           BvhTraversalData &frontData,
725                                                           BvhTraversalData &backData);
726
727        /** Splits the objects for the next subdivision.
728                @returns cost for this split
729        */
730        float SelectObjectPartition(const BvhTraversalData &tData,
731                                                                ObjectContainer &frontObjects,
732                                                                ObjectContainer &backObjects,
733                                                                bool useVisibilityBasedHeuristics);
734       
735        /** Writes the node to disk
736                @note: should be implemented as visitor.
737        */
738        void ExportNode(BvhNode *node, OUT_STREAM &stream);
739
740        /** Exports objects associated with this leaf.
741        */
742        void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
743
744        /** Associates the objects with their bvh leaves.
745        */
746        static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
747
748
749        /////////////////////////////
750        // Helper functions for local cost heuristics
751       
752        /** Prepare split candidates for cost heuristics using axis aligned splits.
753                @param node the current node
754                @param axis the current split axis
755        */
756        void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
757                                                                                   const int axis);
758
759        static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
760                                                                                                 SortableEntryContainer **subdivisionCandidates,
761                                                                                                 const bool sort,
762                                                                                                 const int axis);
763
764        float EvalPriority(const BvhSubdivisionCandidate &splitCandidate,
765                                           const float renderCostDecr,
766                                           const float oldRenderCost) const;
767
768        /** Computes object partition with the best cost according to the heurisics.
769                @param tData the traversal data
770                @param axis the split axis
771                @param objectsFront the objects in the front child bv
772                @param objectsBack the objects in the back child bv
773                @param backObjectsStart the iterator marking the position where the back objects begin
774
775                @returns relative cost (relative to parent cost)
776        */
777        float EvalLocalCostHeuristics(const BvhTraversalData &tData,
778                                                                  const int axis,
779                                                                  ObjectContainer &objectsFront,
780                                                                  ObjectContainer &objectsBack);
781
782        /** Evaluates the contribution to the front and back volume
783                when this object is changing sides in the bvs.
784
785                @param object the object
786                @param volLeft updates the left pvs
787                @param volPvs updates the right pvs
788        */
789        void EvalHeuristicsContribution(Intersectable *obj,
790                                                                        float &volLeft,
791                                                                        float &volRight);
792
793        /** Prepares objects for the cost heuristics.
794                @returns sum of volume of associated view cells
795        */
796        float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
797       
798        /** Evaluates cost for a leaf given the surface area heuristics.
799        */
800        float EvalSahCost(BvhLeaf *leaf) const;
801
802        ////////////////////////////////////////////////
803
804
805        /** Prepares construction for vsp and osp trees.
806        */
807        AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
808                                                                        const AxisAlignedBox3 *parentBox = NULL) const;
809
810        /** Collects list of invalid candidates. Candidates
811                are invalidated by a view space subdivision step
812                that affects this candidate.
813        */
814        void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
815                                                                vector<SubdivisionCandidate *> &dirtyList,
816                                                                const bool onlyUnmailed);
817
818        /** Collect view cells which see this bvh leaf.
819        */
820        int CollectViewCells(const ObjectContainer &objects,
821                                                 ViewCellContainer &viewCells,
822                                                 const bool setCounter,
823                                                 const bool onlyMailedRays) const;
824
825        /** Counts the view cells of this object. note: only
826                counts unmailed objects.
827        */
828        int CountViewCells(Intersectable *obj) const;
829
830        /** Counts the view cells seen by this bvh leaf
831        */
832        int CountViewCells(const ObjectContainer &objects) const;
833
834        /** Collects view cells which see an object.
835        */
836        int CollectViewCells(Intersectable *object,
837                                                 ViewCellContainer &viewCells,
838                                                 const bool useMailBoxing,
839                                                 const bool setCounter,
840                                                 const bool onlyMailedRays) const;
841
842        /** Evaluates increase in pvs size.
843        */
844        int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate, const float avgRayContri) const;
845
846        /** Rays will be clipped to the bounding box.
847        */
848        void PreprocessRays(BvhLeaf *root,
849                                                const VssRayContainer &sampleRays,
850                                                RayInfoContainer &rays);
851
852        /** Print the subdivision stats in the subdivison log.
853        */
854        void PrintSubdivisionStats(const SubdivisionCandidate &tData);
855
856        /** Prints out the stats for this subdivision.
857        */
858        void AddSubdivisionStats(const int viewCells,
859                                                         const float renderCostDecr,
860                                                         const float totalRenderCost);
861
862        /** Stores rays with objects that see the rays.
863        */
864        int AssociateObjectsWithRays(const VssRayContainer &rays) const;
865
866        /** Tests if object is in this leaf.
867                @note: assumes that objects are sorted by their id.
868        */
869        bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
870
871        /** Prepares the construction of the bv hierarchy and returns
872                the first subdivision candidate.
873        */
874        void PrepareConstruction(SplitQueue &tQueue,
875                                                         const VssRayContainer &sampleRays,
876                                                         const ObjectContainer &objects);
877
878        /** Resets bv hierarchy. E.g. deletes root and resets stats.
879        */
880        void Reset(SplitQueue &tQueue,
881                           const VssRayContainer &rays,
882                           const ObjectContainer &objects);
883
884        /** Evaluates volume of view cells that see the objects.
885        */
886        float EvalViewCellsVolume(const ObjectContainer &objects) const;
887
888        /** Assigns or newly creates initial list of sorted objects.
889        */
890        void AssignInitialSortedObjectList(BvhTraversalData &tData,
891                                                                           const ObjectContainer &objects);
892
893        /** Assigns sorted objects to front and back data.
894        */
895        void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
896                                                         BvhTraversalData &frontData,
897                                                         BvhTraversalData &backData);
898       
899        /** Creates new root of hierarchy and computes bounding box.
900                Has to be called before the preparation of the subdivision.
901        */
902        void Initialise(const ObjectContainer &objects);
903
904
905        ////////////////////
906        // initial subdivision
907
908        /** Makes an initial parititon of the object space based on
909                some criteria (size, shader)
910        */
911        void ApplyInitialSubdivision(SubdivisionCandidate *firstCandidate,
912                                                                 vector<SubdivisionCandidate *> &candidateContainer);
913
914        void ApplyInitialSplit(const BvhTraversalData &tData,
915                                                   ObjectContainer &frontObjects,
916                                                   ObjectContainer &backObjects);
917
918        bool InitialTerminationCriteriaMet(const BvhTraversalData &tData) const;
919
920
921protected:
922       
923        /// pre-sorted subdivision candidtes for all three directions.
924        vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
925        /// pointer to the hierarchy of view cells
926        ViewCellsTree *mViewCellsTree;
927        /// The view cells manager
928        ViewCellsManager *mViewCellsManager;
929        /// candidates for placing split planes during cost heuristics
930        vector<SortableEntry> *mSubdivisionCandidates;
931        /// Pointer to the root of the tree
932        BvhNode *mRoot;
933        /// Statistics for the object space partition
934        BvhStatistics mBvhStats;       
935        /// box around the whole view domain
936        AxisAlignedBox3 mBoundingBox;
937        /// the hierarchy manager
938        HierarchyManager *mHierarchyManager;
939
940
941        ////////////////////
942        //-- local termination criteria
943
944        /// maximal possible depth
945        int mTermMaxDepth;
946        /// mininum probability
947        float mTermMinProbability;
948        /// minimal number of objects
949        int mTermMinObjects;
950        /// maximal acceptable cost ratio
951        float mTermMaxCostRatio;
952        /// tolerance value indicating how often the max cost ratio can be failed
953        int mTermMissTolerance;
954        /// minimum number of rays
955        int mTermMinRays;
956
957
958        ////////////////////
959        //-- global termination criteria
960
961        /// the minimal accepted global cost ratio
962        float mTermMinGlobalCostRatio;
963        //// number of accepted misses of the global cost ratio
964        int mTermGlobalCostMissTolerance;
965        /// maximal number of view cells
966        int mTermMaxLeaves;
967        /// maximal tree memory
968        float mMaxMemory;
969        /// the tree is out of memory
970        bool mOutOfMemory;
971
972
973        ////////////////////////////////////////
974        //-- split heuristics based parameters
975       
976        /// if a heuristics should be used for finding a split plane
977    bool mUseCostHeuristics;
978        /// if sah heuristcs should be used for finding a split plane
979        bool mUseSah;
980    /// balancing factor for PVS criterium
981        float mCtDivCi;
982        /// if only driving axis should be used for split
983        bool mOnlyDrivingAxis;
984        /// current time stamp (used for keeping split history)
985        int mTimeStamp;
986        // if rays should be stored in leaves
987        bool mStoreRays;
988        // subdivision stats output file
989        ofstream  mSubdivisionStats;
990        /// keeps track of cost during subdivision
991        float mTotalCost;
992        int mPvsEntries;
993        /// keeps track of overall pvs size during subdivision
994        int mTotalPvsSize;
995        /// number of currenly generated view cells
996        int mCreatedLeaves;
997        /// represents min and max band for sweep
998        float mSplitBorder;
999        /// weight between render cost decrease and node render cost
1000        float mRenderCostDecreaseWeight;
1001
1002        /// if the objects should be sorted in one global step
1003        bool mUseGlobalSorting;
1004
1005        bool mUseBboxAreaForSah;
1006
1007        //SortableEntryContainer *mSortedObjects[4];
1008
1009        int mMinRaysForVisibility;
1010
1011        /// constant value for driving the heuristics
1012        float mMemoryConst;
1013
1014        int mMaxTests;
1015
1016        bool mIsInitialSubdivision;
1017
1018        bool mApplyInitialPartition;
1019       
1020        int mInitialMinObjects;
1021        float mInitialMaxAreaRatio;
1022        float mInitialMinArea;
1023};
1024
1025}
1026
1027#endif
Note: See TracBrowser for help on using the repository browser.