134template <
typename T = Empty_Class>
216 :
pmin_(std::move(other.pmin_)),
pmax_(std::move(other.pmax_)),
217 N_(other.N_),
root_(other.root_)
220 other.root_ =
nullptr;
234 other.root_ =
nullptr;
240 [[nodiscard]]
constexpr bool is_empty() const noexcept {
return N_ == 0; }
243 [[nodiscard]]
constexpr size_t size() const noexcept {
return N_; }
259 Node *ret_val =
nullptr;
266 if (ret_val !=
nullptr)
281 if (ret_val !=
nullptr)
293 if (ret_val !=
nullptr)
318 Node *ret_val =
nullptr;
325 if (ret_val !=
nullptr)
340 if (ret_val !=
nullptr)
351 if (ret_val !=
nullptr)
416 if (
root_ ==
nullptr)
446 if (
root->y() == p.get_y())
448 if (
root->x() == p.get_x())
454 if (p.get_y() <
root->y())
465 if (
root->x() == p.get_x())
467 if (
root->y() == p.get_y())
473 if (p.get_x() <
root->x())
497 if (not
root->rect_.intersects(rect))
544 if (
root->rect_.distance_squared_to(p) > st.dist2)
547 if (
const Geom_Number d2 =
root->point_.distance_squared_to(p); d2 < st.dist2)
553 if (p.get_x() <
root->x())
577 if (
root->rect_.distance_squared_to(p) > st.dist2)
580 if (
const Geom_Number d2 =
root->point_.distance_squared_to(p); d2 < st.dist2)
586 if (p.get_y() <
root->y())
607 [[nodiscard]] std::optional<Point>
nearest(
const Point & p)
const noexcept
618 return st.
node !=
nullptr ? std::optional<Point>(st.
node->
point_) : std::nullopt;
626 template <
typename Op>
655 if (points.
size() > 1)
665 for (
size_t r = 1;
r < points.
size(); ++
r)
666 if (points(
r) != points(
w - 1))
667 points(
w++) = points(
r);
669 while (points.
size() >
w)
677 for (
size_t i = 0; i < points.
size(); ++i)
695 const size_t lo,
const size_t hi,
702 const size_t mid = lo + (hi - lo) / 2;
705 std::nth_element(&pts(lo), &pts(lo) + (mid - lo), &pts(lo) + (hi - lo),
711 std::nth_element(&pts(lo), &pts(lo) + (mid - lo), &pts(lo) + (hi - lo),
720 const Geom_Number mv = split_x ? pts(mid).get_x() : pts(mid).get_y();
722 size_t strict_end = lo;
723 for (
size_t i = lo; i < mid; ++i)
724 if (
const Geom_Number v = split_x ? pts(i).get_x() : pts(i).get_y(); v < mv)
728 const Point tmp = pts(i);
729 pts(i) = pts(strict_end);
730 pts(strict_end) = tmp;
738 size_t root_pos = mid;
739 if (strict_end < mid)
741 const Point median_pt = pts(mid);
742 pts(mid) = pts(strict_end);
743 pts(strict_end) = median_pt;
744 root_pos = strict_end;
747 Node *node =
new Node(pts(root_pos));
769 template <
typename Op>
Core definitions, constants, and utility macros for Aleph-w.
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Represents a point with rectangular coordinates in a 2D plane.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Geom_Number distance_squared_to(const Point &that) const
Calculates the squared Euclidean distance to another point.
An axis-aligned rectangle.
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
void set_rect(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Sets the coordinates of the rectangle.
const Geom_Number & get_ymax() const
Gets the maximum y-coordinate.
const Geom_Number & get_ymin() const
Gets the minimum y-coordinate.
const Geom_Number & get_xmax() const
Gets the maximum x-coordinate.
bool contains(const Point &p) const noexcept
Checks if this axis-aligned rectangle contains a point.
2D k-d tree for efficient spatial point operations.
bool insert(const Point &p)
Insert a point into the tree.
static Node * lr_search(Node *root, const Point &p) noexcept
Search with left-right (X) split pattern.
Point pmax_
Upper-right corner of reference rectangle.
K2Tree()
Default constructor - creates an empty tree with zero-sized region.
constexpr size_t size() const noexcept
Get the number of points in the tree.
K2Tree(Point pmin, Point pmax)
Construct a k-d tree with specified bounding region.
Node * root_
Root of the tree.
static void lr_nearest(Node *root, const Point &p, NearestState &st) noexcept
Recursively find the nearest neighbor (X-split level).
void range(const Rectangle &rect, DynList< Point > *l) const
Find all points within a rectangle.
void extend_region(const Point &p)
Extend the region so that it contains p, and the rectangles of the nodes, which bound range() and nea...
static void range(Node *root, const Rectangle &rect, DynList< Point > *q)
Recursively find points within a rectangle.
K2Tree(const K2Tree &)=delete
Point pmin_
Lower-left corner of reference rectangle.
K2Tree(K2Tree &&other) noexcept
Move constructor — transfers ownership of all nodes.
void for_each(Op &&op) const
Apply an operation to every point in the tree (inorder).
static K2Tree build(Array< Point > points, const Point &pmin, const Point &pmax)
Build a balanced k-d tree from an array of points.
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.
K2Tree & operator=(const K2Tree &)=delete
Node * bu_insert(Node *root, const Point &p)
Insert into tree with bottom-up (Y) split.
bool contains(const Point &p) const noexcept
Check if the tree contains a point.
static void bu_nearest(Node *root, const Point &p, NearestState &st) noexcept
Recursively find the nearest neighbor (Y-split level).
std::optional< Point > nearest(const Point &p) const noexcept
Find the nearest point to a query point.
Node * lr_insert(Node *root, const Point &p)
Insert into tree with left-right (X) split.
static void for_each_node(const Node *node, Op &&op)
Recursive inorder traversal helper for for_each.
constexpr bool is_empty() const noexcept
Check if the tree is empty.
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()
Destructor - frees all nodes.
bool is_outside_region(const Point &p) const noexcept
Whether p lies outside the region [pmin_, pmax_].
static void set_rects(Node *node, const Point &rmin, const Point &rmax, const bool split_x)
Give node the rectangle [rmin, rmax], and its subtrees the halves that its point splits,...
void destroy_tree(Node *node) noexcept
Recursively delete all nodes.
static Node * bu_search(Node *root, const Point &p) noexcept
Search with bottom-up (Y) split pattern.
size_t N_
Number of points in the tree.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Singly linked list implementations with head-tail access.
auto pmin(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel minimum element.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
auto pmax(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel maximum element.
2D point and geometric utilities.
Empty placeholder class with no data members.
Thread-local state for nearest-neighbor recursion.
Internal node structure for the k-d tree.
Point point_
The point stored at this node.
const Geom_Number & ymin() const noexcept
const Geom_Number & xmin() const noexcept
const Geom_Number & x() const noexcept
Node * rt_
Right/top subtree.
void set_rect(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax) noexcept
Set the rectangular region for this node.
const Geom_Number & y() const noexcept
Node(Point p) noexcept
Construct a node with a given point.
const Geom_Number & ymax() const noexcept
void set_rect(const Point &pmin, const Point &pmax) noexcept
Set the rectangular region using corner points.
const Geom_Number & xmax() const noexcept
Node * lb_
Left/bottom subtree.
Rectangle rect_
The axis-aligned rectangle for this node's region.
Dynamic array container with automatic resizing.