Creation of the RTree Godot Class

Basic defintions, we are going to see if it fits the use case.
This commit is contained in:
2025-11-21 13:55:37 -05:00
parent b7891f58da
commit 36c648cfe5
11 changed files with 130 additions and 2 deletions

43
src/rectangletree.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "rectangletree.hpp"
#include "godot_cpp/core/class_db.hpp"
#include "godot_cpp/variant/packed_int32_array.hpp"
#include "godot_cpp/variant/packed_vector2_array.hpp"
#include "godot_cpp/variant/typed_array.hpp"
RectangleTree::RectangleTree() {}
void RectangleTree::_bind_methods() {
ClassDB::bind_method(
godot::D_METHOD("add", "min_bound", "max_bound", "polygon_index"),
&RectangleTree::add);
ClassDB::bind_method(godot::D_METHOD("remove", "point"),
&RectangleTree::remove);
ClassDB::bind_method(godot::D_METHOD("query", "point"),
&RectangleTree::query);
}
void RectangleTree::add(const Vector2 &min_bound, const Vector2 &max_bound,
const int &polygon_index) {
// convert into c++ types from godot types
Rect rect = Rect(min_bound.x, min_bound.y, max_bound.x, max_bound.y);
tree.Insert(rect.min, rect.max, polygon_index);
}
void RectangleTree::remove(const int &polygon_index) {
tree.Remove(polygon_index);
}
PackedInt32Array RectangleTree::query(const Vector2 &point) {
Rect rect = Rect(point.x, point.y, point.x, point.y);
PackedInt32Array return_value;
auto callbackLambda = [&return_value](int index) {
return_value.push_back(index);
return true;
};
tree.Search(rect.min, rect.max, callbackLambda);
return return_value;
}