00001
00002 #ifndef vgl_rtree_c_h_
00003 #define vgl_rtree_c_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <vgl/vgl_point_2d.h>
00019 #include <vgl/vgl_box_2d.h>
00020 #include <vgl/vgl_polygon.h>
00021 #include <vgl/vgl_intersection.h>
00022
00023
00024 template <class T>
00025 class vgl_rtree_point_box_2d
00026 {
00027
00028 vgl_rtree_point_box_2d();
00029 ~vgl_rtree_point_box_2d();
00030
00031 public:
00032 typedef vgl_point_2d<T> v_type;
00033 typedef vgl_box_2d<T> b_type;
00034 typedef T t_type;
00035
00036 static void init (vgl_box_2d<T>& b, vgl_point_2d<T> const& p)
00037 { b = vgl_box_2d<T>(); b.add(p); }
00038
00039 static void update(vgl_box_2d<T>& b, vgl_point_2d<T> const& p)
00040 { b.add(p); }
00041
00042 static void update(vgl_box_2d<T>& b0, vgl_box_2d<T> const &b1)
00043 { b0.add(b1.min_point()); b0.add(b1.max_point()); }
00044
00045 static bool meet(vgl_box_2d<T> const& b, vgl_point_2d<T> const& p)
00046 { return b.contains(p); }
00047
00048 static bool meet(vgl_box_2d<T> const& b0, vgl_box_2d<T> const& b1) {
00049 vgl_point_2d<T> b0min = b0.min_point();
00050 vgl_point_2d<T> b1min = b1.min_point();
00051 vgl_point_2d<T> b0max = b0.max_point();
00052 vgl_point_2d<T> b1max = b1.max_point();
00053 vgl_point_2d<T> max_of_mins(b0min.x() > b1min.x() ? b0min.x() : b1min.x(), b0min.y() > b1min.y() ? b0min.y() : b1min.y());
00054 vgl_point_2d<T> min_of_maxs(b0min.x() < b1min.x() ? b0min.x() : b1min.x(), b0min.y() < b1min.y() ? b0min.y() : b1min.y());
00055
00056 return ( b0.contains(b1min) || b0.contains(b1max) ||
00057 b1.contains(b0min) || b1.contains(b0max) ||
00058 ( (b0.contains(max_of_mins) || b0.contains(min_of_maxs)) &&
00059 (b1.contains(max_of_mins) || b1.contains(min_of_maxs)) ) );
00060 }
00061
00062 static float volume(vgl_box_2d<T> const& b)
00063 { return static_cast<float>(b.area()); }
00064
00065
00066 static bool meets(vgl_point_2d<T> const& v, vgl_polygon<T> poly)
00067 { return poly.contains(v); }
00068
00069
00070 static bool meets(vgl_box_2d<T> const& b, vgl_polygon<T> poly)
00071 { return vgl_intersection<T>(b, poly); }
00072 };
00073
00074
00075
00076
00077
00078 template <class Type>
00079 class vgl_bbox_2d : public vgl_box_2d<Type>
00080 {
00081 public:
00082
00083 vgl_bbox_2d() {}
00084
00085
00086 vgl_bbox_2d(Type const min_position[2],
00087 Type const max_position[2])
00088 : vgl_box_2d<Type>(min_position[2], max_position[2]) {}
00089
00090
00091 vgl_bbox_2d(vgl_point_2d<Type> const& min_pos,
00092 vgl_point_2d<Type> const& max_pos)
00093 : vgl_box_2d<Type>(min_pos, max_pos) {}
00094
00095
00096 vgl_bbox_2d(Type xmin, Type xmax, Type ymin, Type ymax)
00097 : vgl_box_2d<Type>(xmin, xmax, ymin, ymax) {}
00098
00099
00100 inline bool operator==(vgl_bbox_2d<Type> const& b) const {
00101
00102 if (b.is_empty()) return this->is_empty();
00103 return this->min_x() == b.min_x() && this->min_y() == b.min_y()
00104 && this->max_x() == b.max_x() && this->max_y() == b.max_y();
00105 }
00106 };
00107
00108 template <class T>
00109 class vgl_rtree_box_box_2d
00110 {
00111
00112 vgl_rtree_box_box_2d();
00113 ~vgl_rtree_box_box_2d();
00114
00115 public:
00116 typedef vgl_box_2d<T> v_type;
00117 typedef vgl_bbox_2d<T> b_type;
00118 typedef T t_type;
00119
00120 static void init (vgl_bbox_2d<T>& b, vgl_box_2d<T> const& v)
00121 { b = vgl_bbox_2d<T>(); b.add(v.min_point()); b.add(v.max_point()); }
00122
00123 static void update(vgl_bbox_2d<T>& b, vgl_box_2d<T> const& v)
00124 { b.add(v.min_point()); b.add(v.max_point()); }
00125
00126 static void update(vgl_bbox_2d<T>& b0, vgl_bbox_2d<T> const &b1)
00127 { b0.add(b1.min_point()); b0.add(b1.max_point()); }
00128
00129 static bool meet(vgl_bbox_2d<T> const& b0, vgl_box_2d<T> const& v) {
00130 bool resultf =(b0.contains(v.min_point()) || b0.contains(v.max_point()));
00131 bool resultr =(v.contains(b0.min_point()) || v.contains(b0.max_point()));
00132 return resultf||resultr;
00133 }
00134
00135 static bool meet(vgl_bbox_2d<T> const& b0, vgl_bbox_2d<T> const& b1) {
00136 bool resultf =(b0.contains(b1.min_point()) || b0.contains(b1.max_point()));
00137 bool resultr =(b1.contains(b0.min_point()) || b1.contains(b0.max_point()));
00138 return resultf||resultr;
00139 }
00140
00141 static float volume(vgl_box_2d<T> const& b)
00142 { return static_cast<float>(b.area()); }
00143
00144
00145 static bool meets(vgl_box_2d<T> const& b, vgl_polygon<T> poly)
00146 { return vgl_rtree_point_box_2d<T>::meets(b, poly); }
00147
00148 static bool meets(vgl_bbox_2d<T> const& b, vgl_polygon<T> poly)
00149 { return vgl_rtree_point_box_2d<T>::meets(b, poly); }
00150 };
00151
00152 template <class V, class B, class C>
00153 class vgl_rtree_polygon_probe : public vgl_rtree_probe<V, B, C>
00154 {
00155 typedef typename C::t_type T;
00156 vgl_polygon<T> poly_;
00157 public:
00158 vgl_rtree_polygon_probe(vgl_polygon<T> const& poly): poly_(poly) {}
00159
00160
00161 virtual bool meets(V const &v) const {return C::meets(v, poly_); }
00162 virtual bool meets(B const &b) const {return C::meets(b, poly_); }
00163 };
00164
00165 #endif // vgl_rtree_c_h_