diff --git a/.sconsign.dblite b/.sconsign.dblite index 232604a..19a9b2d 100644 Binary files a/.sconsign.dblite and b/.sconsign.dblite differ diff --git a/demo/assets/scripts/environment/destructable/destructable_wall.gd b/demo/assets/scripts/environment/destructable/destructable_wall.gd index cdbc722..e5a4603 100644 --- a/demo/assets/scripts/environment/destructable/destructable_wall.gd +++ b/demo/assets/scripts/environment/destructable/destructable_wall.gd @@ -72,7 +72,6 @@ func _func_godot_apply_properties(entity_properties: Dictionary) -> void: depth_position_offset = (Vector3.UP * (depth/2)) - var outer_boudary = Geometry2D.convex_hull(verts_2d) verts2d = outer_boudary diff --git a/demo/assets/scripts/test.gd b/demo/assets/scripts/test.gd new file mode 100644 index 0000000..6a9131f --- /dev/null +++ b/demo/assets/scripts/test.gd @@ -0,0 +1,24 @@ +extends Node + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + var rtree = RectangleTree.new() + + var min1 = Vector2(0, 0) + var max1 = Vector2(1, 1) + var min2 = Vector2(0, 0) + var max2 = Vector2(0.5, 0.5) + + rtree.add(min1, max1, 0) + rtree.add(min2, max2, 1) + + var result = rtree.query(Vector2(0.25, 0.25)) + + print(result) + + rtree.remove(0) + + result = rtree.query(Vector2(0.25, 0.25)) + + print(result) diff --git a/demo/assets/scripts/test.gd.uid b/demo/assets/scripts/test.gd.uid new file mode 100644 index 0000000..f610b8b --- /dev/null +++ b/demo/assets/scripts/test.gd.uid @@ -0,0 +1 @@ +uid://18smkusl55no diff --git a/demo/bin/libgtriangulation.linux.template_debug.x86_64.so b/demo/bin/libgtriangulation.linux.template_debug.x86_64.so index f830e22..57eb8fd 100755 Binary files a/demo/bin/libgtriangulation.linux.template_debug.x86_64.so and b/demo/bin/libgtriangulation.linux.template_debug.x86_64.so differ diff --git a/demo/scenes/test.tscn b/demo/scenes/test.tscn new file mode 100644 index 0000000..3fbb327 --- /dev/null +++ b/demo/scenes/test.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://bip0c3ydxx12r"] + +[ext_resource type="Script" uid="uid://18smkusl55no" path="res://assets/scripts/test.gd" id="1_errlg"] + +[node name="Test" type="Node"] +script = ExtResource("1_errlg") diff --git a/src/rectangletree.cpp b/src/rectangletree.cpp new file mode 100644 index 0000000..e257f25 --- /dev/null +++ b/src/rectangletree.cpp @@ -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; +} diff --git a/src/rectangletree.hpp b/src/rectangletree.hpp new file mode 100644 index 0000000..b76a3a2 --- /dev/null +++ b/src/rectangletree.hpp @@ -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 +#include + +using namespace godot; + +template 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 RecTree; +typedef RectTemplate 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); +}; diff --git a/src/rectangletree.os b/src/rectangletree.os new file mode 100644 index 0000000..72b30f4 Binary files /dev/null and b/src/rectangletree.os differ diff --git a/src/register_types.cpp b/src/register_types.cpp index e24c4e2..5d3991d 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -1,5 +1,6 @@ #include "register_types.h" #include "godot_cpp/core/class_db.hpp" +#include "rectangletree.hpp" #include "triangulization.hpp" #include @@ -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) { diff --git a/src/register_types.os b/src/register_types.os index afedb4f..82085ea 100644 Binary files a/src/register_types.os and b/src/register_types.os differ