Skip to content

Commit 324000b

Browse files
refactor: extract FlatRTree into shared header with KNN search
Move FlatRTree from anonymous namespace in spatial_join_physical.cpp to a shared header (flat_rtree.hpp) so it can be reused by the upcoming KNN join operator. Changes: - Extract FlatRTree class to src/spatial/operators/flat_rtree.hpp - Add KNNSearch method using Hjaltason-Samet priority queue traversal - Add FlatRTreeKNNState for KNN search state management - Add Box2D::MinDistanceSquared for KNN distance lower bound - Use Allocator instead of BufferManager (in-memory only) - No functional changes to existing SPATIAL_JOIN behavior
1 parent 5aa989b commit 324000b

3 files changed

Lines changed: 453 additions & 360 deletions

File tree

src/spatial/geometry/bbox.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,40 @@ struct Box {
7979
return (min + max) / 2;
8080
}
8181

82+
// Minimum squared distance from a point to the nearest edge of this box.
83+
// Returns 0 if the point is inside the box.
84+
VALUE_TYPE MinDistanceSquared(const V &point) const {
85+
VALUE_TYPE dx = 0, dy = 0;
86+
if (point.x < min.x) {
87+
dx = min.x - point.x;
88+
} else if (point.x > max.x) {
89+
dx = point.x - max.x;
90+
}
91+
if (point.y < min.y) {
92+
dy = min.y - point.y;
93+
} else if (point.y > max.y) {
94+
dy = point.y - max.y;
95+
}
96+
return dx * dx + dy * dy;
97+
}
98+
99+
// Minimum squared distance between two boxes.
100+
// Returns 0 if the boxes overlap.
101+
VALUE_TYPE MinDistanceSquared(const Box &other) const {
102+
VALUE_TYPE dx = 0, dy = 0;
103+
if (other.max.x < min.x) {
104+
dx = min.x - other.max.x;
105+
} else if (other.min.x > max.x) {
106+
dx = other.min.x - max.x;
107+
}
108+
if (other.max.y < min.y) {
109+
dy = min.y - other.max.y;
110+
} else if (other.min.y > max.y) {
111+
dy = other.min.y - max.y;
112+
}
113+
return dx * dx + dy * dy;
114+
}
115+
82116
bool operator==(const Box &other) const {
83117
return min == other.min && max == other.max;
84118
}

0 commit comments

Comments
 (0)