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;
}

53
src/rectangletree.hpp Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
#include "godot_cpp/classes/wrapped.hpp"
#include "godot_cpp/variant/packed_int32_array.hpp"
#include "godot_cpp/variant/packed_vector2_array.hpp"
#include "godot_cpp/variant/vector2.hpp"
#include "rtree.h"
#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/classes/polygon2d.hpp>
using namespace godot;
template <class CoordType = float> struct RectTemplate {
RectTemplate() {}
RectTemplate(CoordType a_minX, CoordType a_minY, CoordType a_maxX,
CoordType a_maxY) {
min[0] = a_minX;
min[1] = a_minY;
max[0] = a_maxX;
max[1] = a_maxY;
}
bool operator==(const RectTemplate &o) const {
return min[0] == o.min[0] && min[1] == o.min[1] && max[0] == o.max[0] &&
max[1] == o.max[1];
}
CoordType min[2];
CoordType max[2];
};
// returns an index, accepts 2 dimension floats
typedef RTree<int, float, 2> RecTree;
typedef RectTemplate<float> Rect;
class RectangleTree : public Object {
GDCLASS(RectangleTree, Object);
protected:
RecTree tree;
static void _bind_methods();
public:
RectangleTree();
void add(const Vector2 &min_bound, const Vector2 &max_bound,
const int &polgon_index);
void remove(const int &polygon_index);
PackedInt32Array query(const Vector2 &point);
};

BIN
src/rectangletree.os Normal file

Binary file not shown.

View File

@@ -1,5 +1,6 @@
#include "register_types.h"
#include "godot_cpp/core/class_db.hpp"
#include "rectangletree.hpp"
#include "triangulization.hpp"
#include <gdextension_interface.h>
@@ -13,7 +14,8 @@ void initialize_triangulation_module(ModuleInitializationLevel p_level) {
return;
}
GDREGISTER_CLASS(Triangulization)
GDREGISTER_CLASS(Triangulization);
GDREGISTER_CLASS(RectangleTree);
}
void uninitialize_triangulation_module(ModuleInitializationLevel p_level) {

Binary file not shown.