6 # include <tpl_nodePool.H>
16 template <
typename T = Empty_Class>
26 Node(
const Point & __point) : point(__point), lb(NULL), rt(NULL)
31 void set_rect(
const Geom_Number & xmin,
const Geom_Number & ymin,
32 const Geom_Number & xmax,
const Geom_Number & ymax)
34 rect.set_rect(xmin, ymin, xmax, ymax);
37 void set_rect(
const Point & pmin,
const Point & pmax)
42 const Geom_Number & xmin()
const {
return rect.get_xmin(); }
43 const Geom_Number & ymin()
const {
return rect.get_ymin(); }
44 const Geom_Number & xmax()
const {
return rect.get_xmax(); }
45 const Geom_Number & ymax()
const {
return rect.get_ymax(); }
46 const Geom_Number & x()
const {
return point.get_x(); }
47 const Geom_Number & y()
const {
return point.get_y(); }
55 static const size_t dim = 19;
60 K2Tree() : pmin(0, 0), pmax(0, 0), node_pool(dim) { }
63 : pmin(__pmin), pmax(__pmax), node_pool(dim)
68 K2Tree(
const Geom_Number & xmin,
const Geom_Number & ymin,
69 const Geom_Number & xmax,
const Geom_Number & ymax)
70 : pmin(xmin, ymin), pmax(xmax, ymax), node_pool(dim)
79 size_t size()
const {
return N; }
83 static Node * lr_insert(Node * root,
const Point & p)
88 Node * ret_val = NULL;
89 if (p.
get_x() == root->x())
91 if (p.
get_y() == root->y())
94 ret_val = bu_insert(root->rt, p);
98 root->rt->set_rect(root->x(), root->ymin(),
99 root->xmax(), root->ymax());
107 if (p.
get_x() < root->x())
109 ret_val = bu_insert(root->lb, p);
113 root->lb->set_rect(root->xmin(), root->ymin(),
114 root->x(), root->ymax());
122 ret_val = bu_insert(root->rt, p);
126 root->rt->set_rect(root->x(), root->ymin(),
127 root->xmax(), root->ymax());
135 static Node * bu_insert(Node * root,
const Point & p)
140 Node * ret_val = NULL;
141 if (p.
get_y() == root->y())
143 if (p.
get_x() == root->x())
146 ret_val = lr_insert(root->rt, p);
150 root->rt->set_rect(root->xmin(), root->y(),
151 root->xmax(), root->ymax());
159 if (p.
get_y() < root->y())
161 ret_val = lr_insert(root->lb, p);
165 root->lb->set_rect(root->xmin(), root->ymin(),
166 root->xmax(), root->y());
173 ret_val = lr_insert(root->rt, p);
177 root->rt->set_rect(root->xmin(), root->y(),
178 root->xmax(), root->ymax());
199 root->set_rect(pmin, pmax);
205 Node * ret = lr_insert(root, p);
216 static Node * bu_search(Node * root,
const Point & p)
221 if (root->y() == p.
get_y())
223 if (root->x() == p.
get_x())
226 return lr_search(root->rt, p);
229 if (p.
get_y() < root->y())
230 return lr_search(root->lb, p);
232 return lr_search(root->rt, p);
235 static Node * lr_search(Node * root,
const Point & p)
240 if (root->x() == p.
get_x())
242 if (root->y() == p.
get_y())
245 return bu_search(root->rt, p);
248 if (p.
get_x() < root->x())
249 return bu_search(root->lb, p);
251 return bu_search(root->rt, p);
259 return lr_search(root, p) != NULL;
269 if (not root->rect.intersects(rect))
272 const Point & point = root->point;
273 if (rect.contains(point))
276 range(root->lb, rect, q);
277 range(root->rt, rect, q);
293 range(root, rect, l);
299 Geom_Number min_dist2;
301 void lr_nearest(Node * root,
const Point & p)
306 if (root->rect.distance_squared_to(p) > min_dist2)
309 Geom_Number d2 = root->point.distance_squared_to(p);
316 if (p.
get_x() < root->x())
318 bu_nearest(root->lb, p);
319 bu_nearest(root->rt, p);
323 bu_nearest(root->rt, p);
324 bu_nearest(root->lb, p);
328 void bu_nearest(Node * root,
const Point & p)
333 if (root->rect.distance_squared_to(p) > min_dist2)
336 Geom_Number d2 = root->point.distance_squared_to(p);
343 if (p.
get_y() < root->y())
345 lr_nearest(root->lb, p);
346 lr_nearest(root->rt, p);
350 lr_nearest(root->rt, p);
351 lr_nearest(root->lb, p);
366 return min_node->point;
370 # endif // TPL_2DTREE_H
void range(const Rectangle &rect, DynList< Point > *l)
Definition: tpl_2dtree.H:288
Definition: tpl_nodePool.H:19
Point nearest(const Point &p)
Retorna el punto más cercano al punto p.
Definition: tpl_2dtree.H:358
bool contains(const Point &p)
Retorna true si el árbol contiene exactamente al punto p.
Definition: tpl_2dtree.H:257
const Geom_Number & get_y() const
retorna el valor de la coordenada y
Definition: point.H:166
const Geom_Number & get_x() const
retorna el valor de la coordenada x
Definition: point.H:161
Point * insert(const Point &p)
Definition: tpl_2dtree.H:194
Definition: tpl_2dtree.H:17
size_t size() const
retorna el número de puntos que contiene el árbol
Definition: tpl_2dtree.H:79
bool is_empty() const
retorna true si el árbol no contiene ningún punto
Definition: tpl_2dtree.H:76
T & append(const T &item)
Inserta un item como último elemento.
Definition: htlist.H:685