00001
00002 #ifndef vgl_rtree_h_
00003 #define vgl_rtree_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013 #include <vcl_vector.h>
00014
00015 template <class V, class B, class C> class vgl_rtree_probe;
00016 template <class V, class B, class C> class vgl_rtree_node;
00017 template <class V, class B, class C> class vgl_rtree_iterator_base;
00018 template <class V, class B, class C> class vgl_rtree_iterator;
00019 template <class V, class B, class C> class vgl_rtree_const_iterator;
00020 template <class V, class B, class C> class vgl_rtree;
00021
00022
00023 template <class V, class B, class C>
00024 class vgl_rtree_probe
00025 {
00026 public:
00027 virtual ~vgl_rtree_probe() { }
00028
00029 virtual bool meets(V const &v) const { B b; C::init(b, v); return meets(b); }
00030 virtual bool meets(B const &b) const =0;
00031 };
00032
00033
00034
00035 #define vgl_rtree_MAX_VERTICES (8)
00036
00037
00038
00039 #define vgl_rtree_MAX_CHILDREN (8)
00040
00041
00042 template <class V, class B, class C>
00043 class vgl_rtree_node
00044 {
00045 public:
00046 typedef vgl_rtree_node<V, B, C> node;
00047
00048
00049 B bounds;
00050
00051
00052 node *parent;
00053
00054
00055
00056 unsigned total_vts;
00057
00058
00059 unsigned local_vts;
00060
00061
00062 V vts[vgl_rtree_MAX_VERTICES];
00063
00064
00065
00066 unsigned total_chs;
00067
00068
00069 unsigned local_chs;
00070
00071
00072 node *chs[vgl_rtree_MAX_CHILDREN];
00073
00074
00075
00076 vgl_rtree_node(node *parent, V const &v);
00077 ~vgl_rtree_node();
00078
00079
00080 void get(B const ®ion, vcl_vector<V> &) const;
00081
00082
00083 void get(vgl_rtree_probe<V, B, C> const ®ion, vcl_vector<V> &) const;
00084
00085
00086 void get_all(vcl_vector<V> &vs) const;
00087
00088
00089 bool find(V const &v, node **n, int *i) const;
00090 bool find(B const &b, V const &v, node **n, int *i) const;
00091
00092
00093 node *add(V const &v);
00094
00095
00096 void erase(unsigned int i);
00097
00098 void print() const;
00099
00100 private:
00101 friend class vgl_rtree_iterator_base<V, B, C>;
00102
00103
00104 unsigned int find_index_in_parent() const;
00105 void compute_bounds();
00106 void update_total_vts(int diff);
00107 void update_total_chs(int diff);
00108 void update_vertex_count(int diff);
00109 void update_child_count (int diff);
00110 };
00111
00112
00113
00114
00115 template <class V, class B, class C>
00116 class vgl_rtree_iterator_base
00117 {
00118 public:
00119 typedef vgl_rtree_node<V, B, C> node;
00120 node *current;
00121 unsigned int i;
00122
00123 vgl_rtree_iterator_base(node *root) : current(root), i(0) { }
00124 vgl_rtree_iterator_base() : current(0), i(0) { }
00125
00126 void operator_pp();
00127 void operator_mm();
00128 };
00129
00130 template <class V, class B, class C>
00131 bool operator==(vgl_rtree_iterator_base<V, B, C> const &a,
00132 vgl_rtree_iterator_base<V, B, C> const &b);
00133
00134 template <class V, class B, class C>
00135 inline bool operator!=(vgl_rtree_iterator_base<V, B, C> const &a,
00136 vgl_rtree_iterator_base<V, B, C> const &b)
00137 { return !( a == b ); }
00138
00139
00140 template <class V, class B, class C>
00141 class vgl_rtree_iterator : public vgl_rtree_iterator_base<V, B, C>
00142 {
00143 public:
00144 typedef vgl_rtree_iterator_base<V, B, C> base;
00145 typedef vgl_rtree_iterator<V, B, C> self;
00146 typedef vgl_rtree_node<V, B, C> node;
00147
00148 vgl_rtree_iterator(node *root) : base(root) { }
00149 vgl_rtree_iterator() { }
00150
00151 V &operator*() const { return base::current->vts[base::i]; }
00152
00153 self &operator++() { base::operator_pp(); return *this; }
00154 self &operator--() { base::operator_mm(); return *this; }
00155
00156 self operator++(int) { self tmp = *this; base::operator_pp(); return tmp; }
00157 self operator--(int) { self tmp = *this; base::operator_mm(); return tmp; }
00158 };
00159
00160
00161 template <class V, class B, class C>
00162 class vgl_rtree_const_iterator : public vgl_rtree_iterator_base<V, B, C>
00163 {
00164 public:
00165 typedef vgl_rtree_iterator_base<V, B, C> base;
00166 typedef vgl_rtree_const_iterator<V, B, C> self;
00167 typedef vgl_rtree_node<V, B, C> node;
00168
00169 vgl_rtree_const_iterator(node *root) : base(root) { }
00170 vgl_rtree_const_iterator(vgl_rtree_iterator<V, B, C> const &that) : base(that) { }
00171 vgl_rtree_const_iterator() { }
00172
00173 V const &operator*() const { return base::current->vts[base::i]; }
00174
00175 self &operator++() { base::operator_pp(); return *this; }
00176 self &operator--() { base::operator_mm(); return *this; }
00177
00178 self operator++(int) { self tmp = *this; base::operator_pp(); return tmp; }
00179 self operator--(int) { self tmp = *this; base::operator_mm(); return tmp; }
00180 };
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 template <class V, class B, class C>
00243 class vgl_rtree
00244 {
00245 public:
00246 vgl_rtree() : root(0) { }
00247 ~vgl_rtree() {
00248 if (root)
00249 delete root;
00250 root = 0;
00251 }
00252
00253
00254 typedef vgl_rtree_probe<V, B, C> probe;
00255
00256
00257 typedef vgl_rtree_iterator <V, B, C> iterator;
00258 typedef vgl_rtree_const_iterator<V, B, C> const_iterator;
00259
00260 iterator begin() { return root ? iterator(root) : iterator(); }
00261 const_iterator begin() const { return root ? const_iterator(root) : const_iterator(); }
00262
00263 iterator end() { return iterator(); }
00264 const_iterator end() const { return const_iterator(); }
00265
00266
00267 void add(V const &v) {
00268 if (root)
00269 root->add(v);
00270 else
00271 root = new node(0, v);
00272 }
00273
00274
00275 void remove(V const &v) {
00276 if (root) {
00277 B tmp;
00278 C::init(tmp, v);
00279 node *n;
00280 int i;
00281 if (root->find(tmp, v, &n, &i))
00282 n->erase(i);
00283
00284 if (root->total_vts == 0) {
00285 delete root;
00286 root = 0;
00287 }
00288 }
00289 }
00290
00291
00292 bool contains(V const &v) const {
00293 node *n;
00294 int i;
00295 return root ? root->find(v, &n, &i) : false;
00296 }
00297 #if 0
00298 iterator find(V const &v);
00299 const_iterator find(V const &v) const;
00300 #endif
00301
00302
00303
00304 void erase(iterator i) {
00305 i.current->erase(i.i);
00306 if (root->total_vts == 0) {
00307 delete root;
00308 root = 0;
00309 }
00310 }
00311
00312
00313 void get(B const ®ion, vcl_vector<V> &vs) const {
00314 if (root)
00315 root->get(region, vs);
00316 }
00317
00318
00319 void get(vgl_rtree_probe<V, B, C> const ®ion, vcl_vector<V> &vs) const {
00320 if (root)
00321 root->get(region, vs);
00322 }
00323
00324
00325 void get_all(vcl_vector<V> &vs) const {
00326 if (root)
00327 root->get_all(vs);
00328 }
00329
00330
00331 bool empty() const {
00332 return root ? root->total_vts==0 : true;
00333 }
00334
00335
00336 unsigned size() const {
00337 return root ? root->total_vts : 0;
00338 }
00339
00340
00341 unsigned nodes() const {
00342 return root ? root->total_chs : 0;
00343 }
00344
00345
00346 typedef vgl_rtree_node<V, B, C> node;
00347 node *secret_get_root() const { return root; }
00348
00349 void print() const {
00350 root->print();
00351 }
00352
00353 private:
00354 node *root;
00355
00356 vgl_rtree<V, B, C>& operator=(vgl_rtree<V, B, C> const &) { return *this; }
00357 vgl_rtree(vgl_rtree<V, B, C> const &) { }
00358 };
00359
00360 #define VGL_RTREE_INSTANTIATE(V, B, C) extern "you must include vgl_rtree.txx first"
00361
00362 #endif // vgl_rtree_h_