forked from rakai93/godot_voronoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoronoi.h
150 lines (108 loc) · 2.97 KB
/
voronoi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef VORONOI_H
#define VORONOI_H
#include <cstdint>
#include <map>
#include <type_traits>
#include <vector>
#include <core/math/vector2.h>
#include <core/math/rect2.h>
#include <core/object.h>
#include <core/reference.h>
#include <core/variant.h>
#include <core/vector.h>
#include "lib/src/jc_voronoi.h"
namespace voronoi_detail {
template<typename T>
struct GodotAllocator {
using value_type = T;
constexpr GodotAllocator() noexcept {}
template<typename U>
GodotAllocator(const GodotAllocator<U>&) noexcept {}
template<typename U>
bool operator==(const GodotAllocator<U>&) const noexcept {
return true;
}
template<typename U>
bool operator!=(const GodotAllocator<U>&) const noexcept {
return false;
}
inline T* allocate(size_t n) const {
return reinterpret_cast<T*>(memalloc(sizeof(T) * n));
}
inline void deallocate(T* ptr, size_t) const noexcept {
memfree(ptr);
}
};
template<typename K, typename V>
using map = std::map<K, V, std::less<K>, GodotAllocator<std::pair<const K, V>>>;
template<typename T>
using vector = std::vector<T, GodotAllocator<T>>;
} // namespace voronoi_detail
class VoronoiEdge;
class VoronoiSite;
class VoronoiDiagram;
class VoronoiEdge : public Object {
GDCLASS(VoronoiEdge, Object)
public:
const jcv_edge* _edge;
const VoronoiDiagram* _diagram;
VoronoiEdge() = default;
inline VoronoiEdge(const jcv_edge* edge, const VoronoiDiagram* diagram)
: _edge(edge), _diagram(diagram) {
}
~VoronoiEdge() = default;
Vector<Variant> sites() const;
Vector2 start() const;
Vector2 end() const;
protected:
static void _bind_methods();
};
class VoronoiSite : public Object {
GDCLASS(VoronoiSite, Object)
public:
const jcv_site* _site;
const VoronoiDiagram* _diagram;
VoronoiSite() = default;
inline VoronoiSite(const jcv_site* site, const VoronoiDiagram* diagram)
: _site(site), _diagram(diagram) {
}
~VoronoiSite() = default;
int index() const;
Vector2 center() const;
Vector<Variant> edges() const;
Vector<Variant> neighbors() const;
protected:
static void _bind_methods();
};
class VoronoiDiagram : public Reference {
GDCLASS(VoronoiDiagram, Reference)
public:
jcv_diagram _diagram;
voronoi_detail::vector<Variant> _edges;
voronoi_detail::vector<Variant> _sites;
voronoi_detail::map<std::uintptr_t, VoronoiEdge*> _edges_by_address;
voronoi_detail::map<int, VoronoiSite*> _sites_by_index;
VoronoiDiagram();
~VoronoiDiagram();
void build_objects();
Vector<Variant> edges() const;
Vector<Variant> sites() const;
protected:
static void _bind_methods();
};
class Voronoi : public Reference {
GDCLASS(Voronoi, Reference)
jcv_rect _boundaries;
bool _has_boundaries;
voronoi_detail::vector<jcv_point> _points;
public:
Voronoi() = default;
~Voronoi() = default;
void set_points(Vector<Vector2> points);
void set_boundaries(Rect2 boundaries);
void relax_points(int iterations);
Ref<VoronoiDiagram> generate_diagram() const;
protected:
static void _bind_methods();
};
#endif // VORONOI_H