split_heuristics.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_SPLIT_HEURISTICS_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_SPLIT_HEURISTICS_H
00045
00046 #include "spat_common.h"
00047
00048 namespace lass
00049 {
00050 namespace spat
00051 {
00052
00053 template <typename ObjectTraits>
00054 struct SplitInfo
00055 {
00056 typedef typename ObjectTraits::TAabb TAabb;
00057 typedef typename ObjectTraits::TValue TValue;
00058 typedef int TAxis;
00059
00060 SplitInfo(const TAabb& aabb, TValue x, TAxis axis):
00061 aabb(aabb), x(x), axis(axis)
00062 {
00063 }
00064
00065 TAabb aabb;
00066 TValue x;
00067 TAxis axis;
00068 };
00069
00070 template <int numObjectsPerLeaf = 1>
00071 struct DefaultSplitHeuristics
00072 {
00073 LASS_META_ASSERT(numObjectsPerLeaf > 0, numObjectsPerLeaf_must_be_strict_positive);
00074
00075 template <typename ObjectTraits, typename RandomIterator>
00076 static SplitInfo<ObjectTraits> split(RandomIterator first, RandomIterator last)
00077 {
00078 typedef typename ObjectTraits::TAabb TAabb;
00079 typedef typename ObjectTraits::TPoint TPoint;
00080 typedef typename ObjectTraits::TValue TValue;
00081
00082 LASS_ASSERT(numObjectsPerLeaf > 0);
00083
00084 TAabb aabb = ObjectTraits::aabbEmpty();
00085 for (RandomIterator i = first; i != last; ++i)
00086 {
00087 aabb = ObjectTraits::aabbJoin(aabb, i->aabb);
00088 }
00089
00090 const int n = static_cast<int>(last - first);
00091 if (n <= numObjectsPerLeaf)
00092 {
00093 return SplitInfo<ObjectTraits>(aabb, 0, -1);
00094 }
00095
00096 const TPoint min = ObjectTraits::aabbMin(aabb);
00097 const TPoint max = ObjectTraits::aabbMax(aabb);
00098 int axis = 0;
00099 TValue maxDistance = ObjectTraits::coord(max, 0) - ObjectTraits::coord(min, 0);
00100 for (int k = 1; k < ObjectTraits::dimension; ++k)
00101 {
00102 const TValue distance = ObjectTraits::coord(max, k) - ObjectTraits::coord(min, k);
00103 if (distance > maxDistance)
00104 {
00105 axis = k;
00106 maxDistance = distance;
00107 }
00108 }
00109
00110 const TValue x = (ObjectTraits::coord(min, axis) + ObjectTraits::coord(max, axis)) / 2;
00111 return SplitInfo<ObjectTraits>(aabb, x, axis);
00112 }
00113 };
00114
00115 namespace impl
00116 {
00117
00118 template <typename ObjectTraits>
00119 class Splitter
00120 {
00121 public:
00122 Splitter(const SplitInfo<ObjectTraits>& split): split_(split) {}
00123 template <typename Input> bool operator()(const Input& input) const
00124 {
00125 typedef ObjectTraits OT;
00126 const typename OT::TValue x =
00127 (OT::coord(OT::aabbMin(input.aabb), split_.axis) + OT::coord(OT::aabbMax(input.aabb), split_.axis)) / 2;
00128 return x < split_.x;
00129 }
00130 private:
00131 SplitInfo<ObjectTraits> split_;
00132 };
00133
00134 template <typename ObjectTraits>
00135 class LessAxis
00136 {
00137 public:
00138 LessAxis(int iAxis): axis_(iAxis) {}
00139 template <typename Input> bool operator()(const Input& a, const Input& b) const
00140 {
00141 typedef ObjectTraits OT;
00142 const typename OT::TValue xa =
00143 (OT::coord(OT::aabbMin(a.aabb), axis_) + OT::coord(OT::aabbMax(a.aabb), axis_)) / 2;
00144 const typename OT::TValue xb =
00145 (OT::coord(OT::aabbMin(b.aabb), axis_) + OT::coord(OT::aabbMax(b.aabb), axis_)) / 2;
00146 return xa < xb;
00147 }
00148 private:
00149 int axis_;
00150 };
00151
00152 }
00153
00154 }
00155
00156 }
00157
00158 #endif
00159
00160