|
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 | NearestState |
| Thread-local state for nearest-neighbor recursion. More... | |
| 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 (Point pmin, 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 |
| K2Tree (K2Tree &&other) noexcept | |
| Move constructor — transfers ownership of all nodes. | |
| K2Tree & | operator= (K2Tree &&other) noexcept |
| Move assignment operator — transfers ownership of all nodes. | |
| 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. | |
| bool | 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) const |
| Find all points within a rectangle. | |
| std::optional< Point > | nearest (const Point &p) const noexcept |
| Find the nearest point to a query point. | |
| template<typename Op > | |
| void | for_each (Op &&op) const |
| Apply an operation to every point in the tree (inorder). | |
| ~K2Tree () | |
| Destructor - frees all nodes. | |
Static Public Member Functions | |
| static K2Tree | build (Array< Point > points, const Point &pmin, const Point &pmax) |
| Build a balanced k-d tree from an array of points. | |
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 | 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. | |
| static void | lr_nearest (Node *root, const Point &p, NearestState &st) noexcept |
| Recursively find the nearest neighbor (X-split level). | |
| static void | bu_nearest (Node *root, const Point &p, NearestState &st) noexcept |
| Recursively find the nearest neighbor (Y-split level). | |
| static Node * | build_balanced (Array< Point > &pts, const size_t lo, const size_t hi, const bool split_x, const Point &rmin, const Point &rmax) |
| Recursively build a balanced subtree selecting medians. | |
| template<typename Op > | |
| static void | for_each_node (const Node *node, Op &&op) |
| Recursive inorder traversal helper for for_each. | |
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. | |
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) |
build() factory which selects medians to produce an O(log n)-height tree.Definition at line 135 of file tpl_2dtree.H.
Default constructor - creates an empty tree with zero-sized region.
Definition at line 180 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 190 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 203 of file tpl_2dtree.H.
Move constructor — transfers ownership of all nodes.
Definition at line 215 of file tpl_2dtree.H.
References Aleph::divide_and_conquer_partition_dp().
Destructor - frees all nodes.
Definition at line 738 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 313 of file tpl_2dtree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Point::get_x(), Aleph::Point::get_y(), K2Tree< T >::lr_insert(), and root().
Referenced by K2Tree< T >::lr_insert().
|
inlinestaticprivatenoexcept |
Recursively find the nearest neighbor (Y-split level).
| root | Current node |
| p | Query point |
| st | Mutable search state |
Definition at line 521 of file tpl_2dtree.H.
References K2Tree< T >::lr_nearest(), and root().
Referenced by K2Tree< T >::lr_nearest().
Search with bottom-up (Y) split pattern.
Definition at line 391 of file tpl_2dtree.H.
References K2Tree< T >::lr_search(), and root().
Referenced by K2Tree< T >::lr_search().
|
inlinestatic |
Build a balanced k-d tree from an array of points.
Constructs a balanced tree by recursively selecting medians, alternating between X and Y coordinates. This avoids the degeneration to O(n) height that occurs with sorted insertions.
Duplicate points are removed internally.
| points | Array of points (copied internally). |
| pmin | Lower-left corner of the bounding region. |
| pmax | Upper-right corner of the bounding region. |
Definition at line 599 of file tpl_2dtree.H.
References K2Tree< T >::build_balanced(), Aleph::Point::get_x(), Aleph::Point::get_y(), Aleph::in_place_sort(), Aleph::Array< T >::is_empty(), K2Tree< T >::N_, Aleph::pmax(), Aleph::pmin(), r, Aleph::Array< T >::remove_last(), K2Tree< T >::root_, Aleph::Array< T >::size(), and w.
Referenced by Aleph::KDTreePointSearch::build(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinestaticprivate |
Recursively build a balanced subtree selecting medians.
After nth_element, elements with the same splitting coordinate as the median are moved to the right partition so that lr_search/bu_search (which send equal-coord queries right) can find them.
Definition at line 639 of file tpl_2dtree.H.
References K2Tree< T >::build_balanced(), Aleph::divide_and_conquer_partition_dp(), Aleph::Point::get_x(), Aleph::Point::get_y(), K2Tree< T >::Node::lb_, K2Tree< T >::Node::rt_, K2Tree< T >::Node::set_rect(), K2Tree< T >::Node::x(), and K2Tree< T >::Node::y().
Referenced by K2Tree< T >::build(), and K2Tree< T >::build_balanced().
Check if the tree contains a point.
Definition at line 430 of file tpl_2dtree.H.
References K2Tree< T >::lr_search(), and K2Tree< T >::root_.
Referenced by Aleph::KDTreePointSearch::contains(), main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Recursively delete all nodes.
Definition at line 726 of file tpl_2dtree.H.
References K2Tree< T >::destroy_tree().
Referenced by K2Tree< T >::~K2Tree(), K2Tree< T >::destroy_tree(), and K2Tree< T >::operator=().
Apply an operation to every point in the tree (inorder).
| Op | Callable with signature compatible with void(const Point &). |
| op | The operation to apply. |
Definition at line 577 of file tpl_2dtree.H.
References K2Tree< T >::for_each_node(), and K2Tree< T >::root_.
Referenced by Aleph::KDTreePointSearch::for_each(), TEST(), and TEST().
|
inlinestaticprivate |
Recursive inorder traversal helper for for_each.
Definition at line 715 of file tpl_2dtree.H.
References K2Tree< T >::for_each_node(), K2Tree< T >::Node::lb_, K2Tree< T >::Node::point_, and K2Tree< T >::Node::rt_.
Referenced by K2Tree< T >::for_each(), and K2Tree< T >::for_each_node().
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 371 of file tpl_2dtree.H.
References Aleph::divide_and_conquer_partition_dp(), K2Tree< T >::lr_insert(), K2Tree< T >::N_, K2Tree< T >::pmax_, K2Tree< T >::pmin_, K2Tree< T >::root_, and K2Tree< T >::Node::set_rect().
Referenced by Aleph::KDTreePointSearch::insert(), 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Check if the tree is empty.
Definition at line 240 of file tpl_2dtree.H.
References K2Tree< T >::N_.
Referenced by Aleph::KDTreePointSearch::is_empty(), 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 254 of file tpl_2dtree.H.
References K2Tree< T >::bu_insert(), Aleph::divide_and_conquer_partition_dp(), Aleph::Point::get_x(), Aleph::Point::get_y(), and root().
Referenced by K2Tree< T >::bu_insert(), and K2Tree< T >::insert().
|
inlinestaticprivatenoexcept |
Recursively find the nearest neighbor (X-split level).
| root | Current node |
| p | Query point |
| st | Mutable search state |
Definition at line 488 of file tpl_2dtree.H.
References K2Tree< T >::bu_nearest(), and root().
Referenced by K2Tree< T >::bu_nearest(), and K2Tree< T >::nearest().
Search with left-right (X) split pattern.
Definition at line 410 of file tpl_2dtree.H.
References K2Tree< T >::bu_search(), and root().
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. This method is const and thread-safe (no mutable members).
| p | Query point |
Definition at line 557 of file tpl_2dtree.H.
References K2Tree< T >::NearestState::dist2, Aleph::Point::distance_squared_to(), K2Tree< T >::lr_nearest(), K2Tree< T >::N_, K2Tree< T >::NearestState::node, K2Tree< T >::Node::point_, and K2Tree< T >::root_.
Referenced by main(), Aleph::KDTreePointSearch::nearest(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Move assignment operator — transfers ownership of all nodes.
Definition at line 224 of file tpl_2dtree.H.
References K2Tree< T >::destroy_tree(), Aleph::divide_and_conquer_partition_dp(), K2Tree< T >::N_, K2Tree< T >::pmax_, K2Tree< T >::pmin_, and K2Tree< T >::root_.
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 466 of file tpl_2dtree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 442 of file tpl_2dtree.H.
References Aleph::DynList< T >::append(), Aleph::divide_and_conquer_partition_dp(), K2Tree< T >::range(), and root().
Referenced by main(), Aleph::KDTreePointSearch::range(), K2Tree< T >::range(), K2Tree< T >::range(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Get the number of points in the tree.
Definition at line 243 of file tpl_2dtree.H.
References K2Tree< T >::N_.
Referenced by Aleph::KDTreePointSearch::size(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Number of points in the tree.
Definition at line 175 of file tpl_2dtree.H.
Referenced by K2Tree< T >::build(), K2Tree< T >::insert(), K2Tree< T >::is_empty(), K2Tree< T >::nearest(), K2Tree< T >::operator=(), K2Tree< T >::range(), and K2Tree< T >::size().
Upper-right corner of reference rectangle.
Definition at line 174 of file tpl_2dtree.H.
Referenced by K2Tree< T >::insert(), and K2Tree< T >::operator=().
Lower-left corner of reference rectangle.
Definition at line 173 of file tpl_2dtree.H.
Referenced by K2Tree< T >::insert(), and K2Tree< T >::operator=().
Root of the tree.
Definition at line 176 of file tpl_2dtree.H.
Referenced by K2Tree< T >::~K2Tree(), K2Tree< T >::build(), K2Tree< T >::contains(), K2Tree< T >::for_each(), K2Tree< T >::insert(), K2Tree< T >::nearest(), K2Tree< T >::operator=(), and K2Tree< T >::range().