Modify Earcut and Triangulization Library to Work With Godot::Vector2
Allows use to ease some pain when converting PackedVector2Array to std::vector<Vector2>.
This commit is contained in:
@@ -20,6 +20,12 @@ template <std::size_t I, typename T> struct nth {
|
||||
return std::get<I>(t);
|
||||
};
|
||||
};
|
||||
template <> struct nth<0, godot::Vector2> {
|
||||
inline static auto get(const godot::Vector2 &t) { return t.x; };
|
||||
};
|
||||
template <> struct nth<1, godot::Vector2> {
|
||||
inline static auto get(const godot::Vector2 &t) { return t.y; };
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
|
||||
|
||||
BIN
src/gdexample.os
BIN
src/gdexample.os
Binary file not shown.
@@ -1,15 +1,12 @@
|
||||
#include "triangulization.hpp"
|
||||
#include "earcut.hpp"
|
||||
#include "godot_cpp/classes/polygon2d.hpp"
|
||||
#include "godot_cpp/core/class_db.hpp"
|
||||
#include "godot_cpp/variant/array.hpp"
|
||||
#include "godot_cpp/variant/packed_int32_array.hpp"
|
||||
#include "godot_cpp/variant/packed_vector2_array.hpp"
|
||||
#include "godot_cpp/variant/string.hpp"
|
||||
#include "godot_cpp/variant/typed_array.hpp"
|
||||
#include "godot_cpp/variant/utility_functions.hpp"
|
||||
#include "godot_cpp/variant/vector2.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
@@ -21,39 +18,29 @@ void Triangulization::_bind_methods() {
|
||||
&Triangulization::triangulate_with_holes);
|
||||
}
|
||||
|
||||
std::vector<std::array<double, 2>>
|
||||
_convert_to_std_vec(const godot::PackedVector2Array &packed_array) {
|
||||
std::vector<std::array<double, 2>> std_vec;
|
||||
std::vector<Vector2>
|
||||
_convert_to_std_vec2(const godot::PackedVector2Array &packed_array) {
|
||||
std::vector<Vector2> std_vec;
|
||||
std_vec.reserve(packed_array.size());
|
||||
|
||||
for (int i = 0; i < packed_array.size(); i++) {
|
||||
Vector2 tmp = packed_array[i];
|
||||
std_vec.push_back({tmp.x, tmp.y});
|
||||
std_vec.push_back(packed_array.get(i));
|
||||
}
|
||||
|
||||
return std_vec;
|
||||
}
|
||||
|
||||
PackedInt32Array
|
||||
Triangulization::triangulate_with_holes(const Polygon2D *outer,
|
||||
const TypedArray<Polygon2D> &inner) {
|
||||
std::vector<std::vector<std::array<double, 2>>> p;
|
||||
PackedInt32Array Triangulization::triangulate_with_holes(
|
||||
const PackedVector2Array &outer,
|
||||
const TypedArray<PackedVector2Array> &inner) {
|
||||
std::vector<std::vector<Vector2>> p;
|
||||
|
||||
p.push_back(_convert_to_std_vec(outer->get_polygon()));
|
||||
p.push_back(_convert_to_std_vec2(outer));
|
||||
|
||||
for (int i = 0; i < inner.size(); i++) {
|
||||
Polygon2D *polygon = Object::cast_to<Polygon2D>(inner[i]);
|
||||
p.push_back(_convert_to_std_vec(polygon->get_polygon()));
|
||||
p.push_back(_convert_to_std_vec2(inner[i]));
|
||||
}
|
||||
|
||||
// for (int i = 0; i < p.size(); i++) {
|
||||
// for (int j = 0; j < p[i].size(); j++) {
|
||||
// UtilityFunctions::print(String("Value: "),
|
||||
// String::num_uint64(p[i][j][0]),
|
||||
// String(","), String::num_uint64(p[i][j][1]));
|
||||
// }
|
||||
// }
|
||||
|
||||
std::vector<uint32_t> indices = mapbox::earcut<uint32_t>(p);
|
||||
|
||||
PackedInt32Array return_values;
|
||||
|
||||
@@ -16,6 +16,7 @@ protected:
|
||||
public:
|
||||
Triangulization();
|
||||
|
||||
PackedInt32Array triangulate_with_holes(const Polygon2D *inner,
|
||||
const TypedArray<Polygon2D> &outer);
|
||||
PackedInt32Array
|
||||
triangulate_with_holes(const PackedVector2Array &outer,
|
||||
const TypedArray<PackedVector2Array> &inner);
|
||||
};
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user