00001
00002 #include <cassert>
00003
00004 #include "KDTree.h"
00005 #include "Event.h"
00006 #include "SplitPlane.h"
00007
00008
00009 class Node;
00010
00011
00017 class SAHKDTree : public KDTree
00018 {
00019 public:
00026 SAHKDTree( float lambda = 0.8f,
00027 float kTraversal = 5.0f,
00028 float kIntersect = 1.0f )
00029 : KDTree(4, 64),
00030 mLambda(lambda),
00031 mKTraversal(kTraversal),
00032 mKIntersect(kIntersect)
00033 {
00034 }
00035
00038 virtual ~SAHKDTree();
00039
00045 virtual void buildTree( const PrimitiveList & list,
00046 const Box & bbox );
00047
00048 private:
00050 typedef std::vector<Event> EventList;
00051
00052
00054 const float mLambda;
00055
00057 const float mKTraversal;
00058
00060 const float mKIntersect;
00061
00062
00070 void buildTree( Node * node,
00071 const Box & bbox,
00072 EventList & events,
00073 unsigned int depth ) const;
00074
00085 virtual bool terminate( const Node * current,
00086 float minCost,
00087 unsigned int depth ) const;
00088
00098 float cost( float probability_left,
00099 float probability_right,
00100 unsigned int number_left,
00101 unsigned int number_right ) const
00102 {
00103 float result = mKTraversal + mKIntersect*(probability_left*number_left + probability_right*number_right);
00104 if (number_left == 0 || number_right == 0)
00105 return mLambda * result;
00106 else
00107 return result;
00108 }
00109
00121 float SAH( Side & side,
00122 const Box & bbox,
00123 const SplitPlane & plane,
00124 unsigned int number_left,
00125 unsigned int number_right,
00126 unsigned int number_middle ) const;
00127
00138 SplitPlane findBestPlane( float & minCost,
00139 const Node * node,
00140 const Box & bbox,
00141 const EventList & events ) const;
00142
00155 std::pair<Box, Box> classifyAndSplice( EventList & eLeft,
00156 EventList & eRight,
00157 Node * node,
00158 const Box & bbox,
00159 const EventList & events,
00160 const SplitPlane & plane ) const;
00161
00168 void generateEvents( EventList & events,
00169 Primitive * primitive,
00170 const Box & bbox ) const;
00171 };
00172
00173