Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/spatial/geometry/bbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ struct Box {
return (min + max) / 2;
}

// Minimum squared distance from a point to the nearest edge of this box.
// Returns 0 if the point is inside the box.
VALUE_TYPE MinDistanceSquared(const V &point) const {
VALUE_TYPE dx = 0, dy = 0;
if (point.x < min.x) {
dx = min.x - point.x;
} else if (point.x > max.x) {
dx = point.x - max.x;
}
if (point.y < min.y) {
dy = min.y - point.y;
} else if (point.y > max.y) {
dy = point.y - max.y;
}
return dx * dx + dy * dy;
}

// Minimum squared distance between two boxes.
// Returns 0 if the boxes overlap.
VALUE_TYPE MinDistanceSquared(const Box &other) const {
VALUE_TYPE dx = 0, dy = 0;
if (other.max.x < min.x) {
dx = min.x - other.max.x;
} else if (other.min.x > max.x) {
dx = other.min.x - max.x;
}
if (other.max.y < min.y) {
dy = min.y - other.max.y;
} else if (other.min.y > max.y) {
dy = other.min.y - max.y;
}
return dx * dx + dy * dy;
}

bool operator==(const Box &other) const {
return min == other.min && max == other.max;
}
Expand Down
2 changes: 1 addition & 1 deletion src/spatial/modules/main/spatial_functions_scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5784,7 +5784,7 @@ struct ST_Distance_Sphere {
unique_ptr<FunctionData> Copy() const override {
auto copy = make_uniq<BindData>();
copy->always_xy = always_xy;
return copy;
return std::move(copy);
}
bool Equals(const FunctionData &other) const override {
auto &other_bind = other.Cast<BindData>();
Expand Down
Loading