|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
QuadTree - Hierarchical spatial index for 2D points. More...
#include <quadtree.H>
Public Types | |
| using | Node = QuadNode |
Public Member Functions | |
| QuadTree () | |
| Default constructor - creates an uninitialized tree. | |
| QuadTree (const Geom_Number &min_x, const Geom_Number &max_x, const Geom_Number &min_y, const Geom_Number &max_y, const size_t &_max_num_points_per_node=1) | |
| Construct a quadtree with specified region and capacity. | |
| QuadTree (const QuadTree &tree) | |
| Copy constructor - creates a deep copy of the tree. | |
| QuadTree (QuadTree &&tree) noexcept | |
| Move constructor - transfers ownership. | |
| QuadTree & | operator= (const QuadTree &tree) |
| Copy assignment operator - creates a deep copy. | |
| QuadTree & | operator= (QuadTree &&tree) noexcept |
| Move assignment operator - transfers ownership. | |
| ~QuadTree () | |
| Destructor - frees all nodes. | |
| Node * | get_root () noexcept |
| Get the root node. | |
| const Node * | get_root () const noexcept |
| Get the root node (const version). | |
| void | set_max_num_points_per_node (const size_t &_max_num_points_per_node) |
| Set the maximum points per leaf node. | |
| size_t | get_max_num_points_per_node () const noexcept |
| Get the maximum points per leaf node. | |
| bool | contains (const Point &p) const noexcept |
| Check if a point is within the tree's region. | |
| Point * | insert (const Point &p) |
| Insert a point into the tree. | |
| Point * | insert (const Geom_Number &x, const Geom_Number &y) |
| Insert a point given by coordinates. | |
| Point * | search (const Point &p) noexcept |
| Search for a point in the tree. | |
| Node * | search_container_node (const Point &p) noexcept |
| Find the leaf node containing a point. | |
| void | remove (const Point &p) |
| Remove a point from the tree. | |
| void | empty () |
| Remove all points from the tree, keeping only the root. | |
| void | clear () |
| Alias for empty(). | |
| template<class Op > | |
| void | for_each (Op &op) |
| Apply an operation to each node in the tree. | |
| template<class Op > | |
| void | for_each (Op &&op=Op()) |
| Apply operation to each node (rvalue reference version). | |
Private Member Functions | |
| void | split (Node *node) |
| Subdivide a leaf node into four children. | |
| void | join (Node *node) |
| Merge four child nodes back into their parent. | |
| Point * | insert (Node *&r, const Point &p) |
| Recursive insert helper. | |
| void | empty (Node *&r) noexcept |
| Recursively delete all nodes. | |
| template<class Op > | |
| void | operate_on_nodes (Node *r, Op &op) |
| Apply operation to all nodes in the tree. | |
| void | copy_tree (QuadNode *src, QuadNode *&tgt, QuadNode *tgt_parent=nullptr) |
| Deep copy a tree structure. | |
Static Private Member Functions | |
| static Node * | make_root (const Geom_Number &min_x, const Geom_Number &max_x, const Geom_Number &min_y, const Geom_Number &max_y, const size_t capacity) |
| Validate constructor arguments and allocate the root node. | |
| static bool | contains_only (Node *node, const Point &p) |
Return true when every point stored in a leaf equals p. | |
| static bool | has_four_leaf_children (Node *node) noexcept |
| Return true when an internal node has four leaf children. | |
| static size_t | leaf_children_size (Node *node) noexcept |
| Return the number of points directly stored in four leaf children. | |
Private Attributes | |
| Node * | root |
| Root node of the tree. | |
| size_t | max_num_points_per_node |
| Maximum points per leaf node before splitting. | |
QuadTree - Hierarchical spatial index for 2D points.
QuadTree is a tree data structure where each internal node has exactly four children corresponding to quadrants: NW, NE, SW, and SE. The tree adapts to point distribution by subdividing regions that exceed a specified point capacity.
Definition at line 125 of file quadtree.H.
Definition at line 128 of file quadtree.H.
|
inline |
Default constructor - creates an uninitialized tree.
Definition at line 365 of file quadtree.H.
|
inline |
Construct a quadtree with specified region and capacity.
| min_x | Minimum X coordinate of the region |
| max_x | Maximum X coordinate of the region |
| min_y | Minimum Y coordinate of the region |
| max_y | Maximum Y coordinate of the region |
| _max_num_points_per_node | Maximum points per leaf before splitting (default: 1) |
| std::domain_error | if capacity is zero or bounds are invalid |
Definition at line 380 of file quadtree.H.
Copy constructor - creates a deep copy of the tree.
| tree | Tree to copy |
Definition at line 394 of file quadtree.H.
References copy_tree(), and root.
|
inlinenoexcept |
Move constructor - transfers ownership.
| tree | Tree to move from |
Definition at line 404 of file quadtree.H.
|
inline |
Destructor - frees all nodes.
Definition at line 445 of file quadtree.H.
|
inline |
Check if a point is within the tree's region.
| p | Point to check |
Definition at line 487 of file quadtree.H.
References Aleph::and, QuadNode::contains(), and root.
Referenced by demo_basic_operations(), demo_collision_detection(), demo_geographic_points(), demo_performance(), TEST(), TEST_F(), TEST_F(), and TEST_F().
Return true when every point stored in a leaf equals p.
Definition at line 151 of file quadtree.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), QuadNode::get_points_set(), and QuadNode::is_leaf().
Referenced by insert().
|
inlineprivate |
Deep copy a tree structure.
Definition at line 330 of file quadtree.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), COLOR, copy_tree(), QuadNode::get_max_x(), QuadNode::get_max_y(), QuadNode::get_min_x(), QuadNode::get_min_y(), QuadNode::get_points_set(), LEVEL, NE_CHILD, NW_CHILD, PARENT, SE_CHILD, and SW_CHILD.
Referenced by QuadTree(), copy_tree(), and operator=().
|
inline |
Remove all points from the tree, keeping only the root.
Definition at line 589 of file quadtree.H.
References empty(), QuadNode::empty(), NE_CHILD, NW_CHILD, root, SE_CHILD, and SW_CHILD.
Referenced by ~QuadTree(), clear(), empty(), empty(), operator=(), and operator=().
Apply operation to each node (rvalue reference version).
Definition at line 619 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), and root.
Apply an operation to each node in the tree.
Performs preorder traversal, visiting internal nodes before their children.
| Op | Function object type with signature void(Node*) |
| op | Operation to apply |
Definition at line 612 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), and root.
Referenced by demo_traversal(), TEST(), and TEST().
|
inlinenoexcept |
Get the maximum points per leaf node.
Definition at line 477 of file quadtree.H.
References max_num_points_per_node.
|
inlinenoexcept |
Return true when an internal node has four leaf children.
Definition at line 161 of file quadtree.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), NE_CHILD, NW_CHILD, SE_CHILD, and SW_CHILD.
Referenced by leaf_children_size(), and remove().
|
inline |
Insert a point given by coordinates.
| x | X coordinate |
| y | Y coordinate |
Definition at line 514 of file quadtree.H.
Insert a point into the tree.
If the point is outside the tree's region, insertion fails. If the point already exists, it will still be inserted (duplicates allowed).
| p | Point to insert |
Definition at line 500 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), QuadNode::contains(), insert(), and root.
Recursive insert helper.
Definition at line 281 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), contains_only(), QuadNode::get_child_to(), insert(), max_num_points_per_node, r, and split().
Referenced by demo_basic_operations(), demo_collision_detection(), demo_geographic_points(), demo_performance(), demo_traversal(), demo_tree_structure(), insert(), insert(), insert(), RankTreeTest< Tree >::insert_all(), 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(), 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(), 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(), 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), test(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), test_map_tree(), and test_map_tree().
Merge four child nodes back into their parent.
Deletes the four child nodes and transfers all their points to the parent node. Applies only to internal nodes whose children are all leaves.
| node | Internal node whose children should be merged |
Definition at line 234 of file quadtree.H.
References Aleph::DynList< T >::append(), QuadNode::Black, Aleph::blossom_maximum_cardinality_matching(), COLOR, QuadNode::get_points_set(), Aleph::HTList::is_empty(), QuadNode::is_leaf(), Aleph::is_leaf(), NE_CHILD, NW_CHILD, SE_CHILD, SW_CHILD, and QuadNode::White.
Referenced by remove().
Return the number of points directly stored in four leaf children.
Definition at line 171 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), has_four_leaf_children(), NE_CHILD, NW_CHILD, SE_CHILD, and SW_CHILD.
Referenced by remove().
|
inlinestaticprivate |
Validate constructor arguments and allocate the root node.
| std::domain_error | if capacity is zero or bounds are invalid. |
Definition at line 140 of file quadtree.H.
References ah_domain_error_if.
Apply operation to all nodes in the tree.
Definition at line 316 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), NE_CHILD, NW_CHILD, r, SE_CHILD, and SW_CHILD.
Copy assignment operator - creates a deep copy.
| tree | Tree to assign from |
Definition at line 415 of file quadtree.H.
References copy_tree(), empty(), max_num_points_per_node, and root.
Move assignment operator - transfers ownership.
| tree | Tree to move from |
Definition at line 432 of file quadtree.H.
References empty(), max_num_points_per_node, and root.
Remove a point from the tree.
Removes the point and potentially merges nodes if the total point count falls below the threshold.
| p | Point to remove |
Definition at line 565 of file quadtree.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), QuadNode::contains(), QuadNode::get_child_to(), has_four_leaf_children(), QuadNode::is_leaf(), join(), leaf_children_size(), max_num_points_per_node, PARENT, QuadNode::remove_point(), and root.
Referenced by demo_basic_operations(), destroy_tree(), main(), RankTreeTest< Tree >::TearDown(), 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
Search for a point in the tree.
| p | Point to search for |
Definition at line 524 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), QuadNode::contains(), QuadNode::get_child_to(), QuadNode::is_leaf(), root, and QuadNode::search_point().
Referenced by demo_basic_operations(), demo_geographic_points(), 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), test(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
Find the leaf node containing a point.
| p | Point to locate |
Definition at line 542 of file quadtree.H.
References Aleph::blossom_maximum_cardinality_matching(), QuadNode::contains(), QuadNode::get_child_to(), QuadNode::is_leaf(), root, and QuadNode::search_point().
Referenced by TEST().
Set the maximum points per leaf node.
Existing nodes are not immediately repartitioned.
| _max_num_points_per_node | New positive leaf capacity |
| std::domain_error | if the requested capacity is zero |
Definition at line 469 of file quadtree.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), and max_num_points_per_node.
Referenced by TEST().
Subdivide a leaf node into four children.
Creates four child nodes (NW, NE, SW, SE) and redistributes the node's points among them. Only applies to leaf nodes.
| node | Leaf node to split |
Definition at line 187 of file quadtree.H.
References QuadNode::add_point(), Aleph::blossom_maximum_cardinality_matching(), COLOR, QuadNode::get_child_to(), QuadNode::get_max_x(), QuadNode::get_max_y(), QuadNode::get_mid_x(), QuadNode::get_mid_y(), QuadNode::get_min_x(), QuadNode::get_min_y(), QuadNode::get_points_set(), QuadNode::Gray, Aleph::HTList::is_empty(), QuadNode::is_leaf(), LEVEL, NE_CHILD, NW_CHILD, Aleph::DynList< T >::remove_first(), SE_CHILD, and SW_CHILD.
Referenced by insert().
|
private |
Maximum points per leaf node before splitting.
Definition at line 134 of file quadtree.H.
Referenced by get_max_num_points_per_node(), insert(), operator=(), operator=(), remove(), and set_max_num_points_per_node().
|
private |
Root node of the tree.
Definition at line 131 of file quadtree.H.
Referenced by QuadTree(), ~QuadTree(), contains(), empty(), for_each(), for_each(), get_root(), get_root(), insert(), operator=(), operator=(), remove(), search(), and search_container_node().