|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
2D k-d tree for efficient spatial point operations. More...
#include <tpl_2dtree.H>
Classes | |
| struct | Node |
| Internal node structure for the k-d tree. More... | |
Public Member Functions | |
| K2Tree () | |
| Default constructor - creates an empty tree with zero-sized region. | |
| K2Tree (const Point &__pmin, const Point &__pmax) | |
| Construct a k-d tree with specified bounding region. | |
| K2Tree (const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax) | |
| Construct a k-d tree with specified coordinate bounds. | |
| K2Tree (const K2Tree &)=delete | |
| K2Tree & | operator= (const K2Tree &)=delete |
| constexpr bool | is_empty () const noexcept |
| Check if the tree is empty. | |
| constexpr size_t | size () const noexcept |
| Get the number of points in the tree. | |
| Point * | insert (const Point &p) |
| Insert a point into the tree. | |
| bool | contains (const Point &p) const noexcept |
| Check if the tree contains a point. | |
| void | range (const Rectangle &rect, DynList< Point > *l) |
| Find all points within a rectangle. | |
| Point | nearest (const Point &p) noexcept |
| Find the nearest point to a query point. | |
| ~K2Tree () | |
| Destructor - frees all nodes. | |
Private Member Functions | |
| Node * | lr_insert (Node *root, const Point &p) |
| Insert into tree with left-right (X) split. | |
| Node * | bu_insert (Node *root, const Point &p) |
| Insert into tree with bottom-up (Y) split. | |
| void | lr_nearest (Node *root, const Point &p) noexcept |
| Recursively find nearest neighbor (X-split level). | |
| void | bu_nearest (Node *root, const Point &p) noexcept |
| Recursively find nearest neighbor (Y-split level). | |
| void | destroy_tree (Node *node) noexcept |
| Recursively delete all nodes. | |
Static Private Member Functions | |
| static Node * | bu_search (Node *root, const Point &p) noexcept |
| Search with bottom-up (Y) split pattern. | |
| static Node * | lr_search (Node *root, const Point &p) noexcept |
| Search with left-right (X) split pattern. | |
| static void | range (Node *root, const Rectangle &rect, DynList< Point > *q) |
| Recursively find points within a rectangle. | |
Private Attributes | |
| Point | pmin |
| Lower-left corner of reference rectangle. | |
| Point | pmax |
| Upper-right corner of reference rectangle. | |
| size_t | N |
| Number of points in the tree. | |
| Node * | root |
| Root of the tree. | |
| Node * | min_node |
| Current nearest node (for nearest()) | |
| Geom_Number | min_dist2 |
| Current minimum squared distance. | |
2D k-d tree for efficient spatial point operations.
K2Tree is a binary space partitioning tree that recursively divides 2D space using alternating vertical (X) and horizontal (Y) splits. Each node represents a point and implicitly defines a rectangular region.
| T | Optional user data type associated with points (default: Empty_Class) |
Definition at line 131 of file tpl_2dtree.H.
Default constructor - creates an empty tree with zero-sized region.
Definition at line 176 of file tpl_2dtree.H.
Construct a k-d tree with specified bounding region.
| __pmin | Lower-left corner of the region |
| __pmax | Upper-right corner of the region |
Definition at line 186 of file tpl_2dtree.H.
|
inline |
Construct a k-d tree with specified coordinate bounds.
| xmin | Minimum X coordinate |
| ymin | Minimum Y coordinate |
| xmax | Maximum X coordinate |
| ymax | Maximum Y coordinate |
Definition at line 199 of file tpl_2dtree.H.
Destructor - frees all nodes.
Definition at line 551 of file tpl_2dtree.H.
References K2Tree< T >::destroy_tree(), and K2Tree< T >::root.
Insert into tree with bottom-up (Y) split.
Recursively inserts a point, splitting on Y coordinate at this level.
| root | Current node |
| p | Point to insert |
Definition at line 285 of file tpl_2dtree.H.
References K2Tree< T >::Node::lb, K2Tree< T >::lr_insert(), Aleph::maps(), K2Tree< T >::root, K2Tree< T >::Node::rt, K2Tree< T >::Node::set_rect(), K2Tree< T >::Node::x(), K2Tree< T >::Node::xmax(), K2Tree< T >::Node::xmin(), K2Tree< T >::Node::y(), K2Tree< T >::Node::ymax(), and K2Tree< T >::Node::ymin().
Referenced by K2Tree< T >::lr_insert().
Recursively find nearest neighbor (Y-split level).
| root | Current node |
| p | Query point |
Definition at line 489 of file tpl_2dtree.H.
References Rectangle::distance_squared_to(), K2Tree< T >::Node::lb, K2Tree< T >::lr_nearest(), K2Tree< T >::min_dist2, K2Tree< T >::min_node, K2Tree< T >::Node::point, K2Tree< T >::Node::rect, K2Tree< T >::root, K2Tree< T >::Node::rt, and K2Tree< T >::Node::y().
Referenced by K2Tree< T >::lr_nearest().
Search with bottom-up (Y) split pattern.
Definition at line 365 of file tpl_2dtree.H.
References K2Tree< T >::Node::lb, K2Tree< T >::lr_search(), K2Tree< T >::root, K2Tree< T >::Node::rt, K2Tree< T >::Node::x(), and K2Tree< T >::Node::y().
Referenced by K2Tree< T >::lr_search().
Check if the tree contains a point.
Definition at line 404 of file tpl_2dtree.H.
References K2Tree< T >::lr_search(), and K2Tree< T >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Recursively delete all nodes.
Definition at line 539 of file tpl_2dtree.H.
References K2Tree< T >::destroy_tree().
Referenced by K2Tree< T >::~K2Tree(), and K2Tree< T >::destroy_tree().
Insert a point into the tree.
The point is inserted only if it's not already present (no duplicates).
| p | Point to insert |
Definition at line 343 of file tpl_2dtree.H.
References K2Tree< T >::lr_insert(), Aleph::maps(), K2Tree< T >::N, K2Tree< T >::pmax, K2Tree< T >::pmin, K2Tree< T >::Node::point, K2Tree< T >::root, and K2Tree< T >::Node::set_rect().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Insert into tree with left-right (X) split.
Recursively inserts a point, splitting on X coordinate at this level.
| root | Current node |
| p | Point to insert |
Definition at line 225 of file tpl_2dtree.H.
References K2Tree< T >::bu_insert(), K2Tree< T >::Node::lb, Aleph::maps(), K2Tree< T >::root, K2Tree< T >::Node::rt, K2Tree< T >::Node::set_rect(), K2Tree< T >::Node::x(), K2Tree< T >::Node::xmax(), K2Tree< T >::Node::xmin(), K2Tree< T >::Node::y(), K2Tree< T >::Node::ymax(), and K2Tree< T >::Node::ymin().
Referenced by K2Tree< T >::bu_insert(), and K2Tree< T >::insert().
Recursively find nearest neighbor (X-split level).
| root | Current node |
| p | Query point |
Definition at line 458 of file tpl_2dtree.H.
References K2Tree< T >::bu_nearest(), Rectangle::distance_squared_to(), K2Tree< T >::Node::lb, K2Tree< T >::min_dist2, K2Tree< T >::min_node, K2Tree< T >::Node::point, K2Tree< T >::Node::rect, K2Tree< T >::root, K2Tree< T >::Node::rt, and K2Tree< T >::Node::x().
Referenced by K2Tree< T >::bu_nearest(), and K2Tree< T >::nearest().
Search with left-right (X) split pattern.
Definition at line 384 of file tpl_2dtree.H.
References K2Tree< T >::bu_search(), K2Tree< T >::Node::lb, K2Tree< T >::root, K2Tree< T >::Node::rt, K2Tree< T >::Node::x(), and K2Tree< T >::Node::y().
Referenced by K2Tree< T >::bu_search(), and K2Tree< T >::contains().
Find the nearest point to a query point.
Performs a nearest neighbor search using branch-and-bound pruning.
| p | Query point |
Definition at line 523 of file tpl_2dtree.H.
References K2Tree< T >::lr_nearest(), K2Tree< T >::min_dist2, K2Tree< T >::min_node, K2Tree< T >::N, NullPoint, K2Tree< T >::Node::point, and K2Tree< T >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Find all points within a rectangle.
Performs a range query to find all points that lie within the specified rectangle.
| rect | Query rectangle |
| l | Output list where matching points are appended |
Definition at line 441 of file tpl_2dtree.H.
References l, K2Tree< T >::N, K2Tree< T >::range(), and K2Tree< T >::root.
|
inlinestaticprivate |
Recursively find points within a rectangle.
| root | Current node |
| rect | Query rectangle |
| q | Output list for matching points |
Definition at line 416 of file tpl_2dtree.H.
References Aleph::DynList< T >::append(), Rectangle::contains(), Rectangle::intersects(), K2Tree< T >::Node::lb, Aleph::maps(), K2Tree< T >::Node::point, K2Tree< T >::range(), K2Tree< T >::Node::rect, K2Tree< T >::root, and K2Tree< T >::Node::rt.
Referenced by main(), K2Tree< T >::range(), K2Tree< T >::range(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
private |
Current minimum squared distance.
Definition at line 451 of file tpl_2dtree.H.
Referenced by K2Tree< T >::bu_nearest(), K2Tree< T >::lr_nearest(), and K2Tree< T >::nearest().
Current nearest node (for nearest())
Definition at line 450 of file tpl_2dtree.H.
Referenced by K2Tree< T >::bu_nearest(), K2Tree< T >::lr_nearest(), and K2Tree< T >::nearest().
Number of points in the tree.
Definition at line 171 of file tpl_2dtree.H.
Referenced by K2Tree< T >::insert(), K2Tree< T >::is_empty(), K2Tree< T >::nearest(), K2Tree< T >::range(), and K2Tree< T >::size().
Upper-right corner of reference rectangle.
Definition at line 170 of file tpl_2dtree.H.
Referenced by K2Tree< T >::insert(), and K2Tree< T >::Node::set_rect().
Lower-left corner of reference rectangle.
Definition at line 169 of file tpl_2dtree.H.
Referenced by K2Tree< T >::insert(), and K2Tree< T >::Node::set_rect().
Root of the tree.
Definition at line 172 of file tpl_2dtree.H.
Referenced by K2Tree< T >::~K2Tree(), K2Tree< T >::bu_insert(), K2Tree< T >::bu_nearest(), K2Tree< T >::bu_search(), K2Tree< T >::contains(), K2Tree< T >::insert(), K2Tree< T >::lr_insert(), K2Tree< T >::lr_nearest(), K2Tree< T >::lr_search(), K2Tree< T >::nearest(), K2Tree< T >::range(), and K2Tree< T >::range().