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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use super::{BoundingBox, ClipBehavior, Point, Voronoi};
use super::utils::calculate_approximated_cetroid;
#[derive(Default)]
pub struct VoronoiBuilder {
sites: Option<Vec<Point>>,
lloyd_iterations: usize,
bounding_box: BoundingBox,
clip_behavior: ClipBehavior,
}
impl VoronoiBuilder {
pub fn set_bounding_box(mut self, bounding_box: BoundingBox) -> Self {
self.bounding_box = bounding_box;
self
}
pub fn set_clip_behavior(mut self, clip_behavior: ClipBehavior) -> Self {
self.clip_behavior = clip_behavior;
self
}
pub fn set_sites(mut self, sites: Vec<Point>) -> Self {
self.sites.replace(sites);
self
}
pub fn set_lloyd_relaxation_iterations(mut self, iterations: usize) -> Self {
self.lloyd_iterations = iterations;
self
}
pub fn build(mut self) -> Option<Voronoi> {
let v = Voronoi::new(
self.sites.take().expect("Cannot build voronoi without sites. Call set_sites() first."),
self.bounding_box.clone(),
self.clip_behavior,
);
self.perform_lloyd_relaxation(v)
}
fn perform_lloyd_relaxation(&mut self, mut v: Option<Voronoi>) -> Option<Voronoi> {
for _ in 0..self.lloyd_iterations {
if let Some(voronoi) = v {
let new_sites = voronoi.iter_cells().map(|c| calculate_approximated_cetroid(c.vertices()))
.collect::<Vec<Point>>();
v = Self::create_builder_from_voronoi_without_sites(&voronoi)
.set_sites(new_sites)
.build();
} else {
break;
}
}
v
}
pub fn generate_circle_sites(self, size: usize, radius: f64) -> Self {
let len = size;
let r = radius;
let mut sites = vec![];
sites.push(Point { x: 0.0, y: 0.0 });
for i in 0..len {
let a = (i as f64 * 360.0 / len as f64).to_radians();
sites.push(Point {
x: r * a.sin(),
y: r * a.cos()
});
}
self.set_sites(sites)
}
pub fn generate_rect_sites(self, width: usize, height: usize) -> Self {
let mut sites = vec![];
let fwidth = width as f64;
let fheight = height as f64;
for i in 0..width {
for j in 0..height {
sites.push(Point {
x: i as f64 / fwidth - 0.5,
y: j as f64/ fheight - 0.5
});
}
}
self.set_sites(sites)
}
pub fn generate_square_sites(self, width: usize) -> Self {
self.generate_rect_sites(width, width)
}
fn create_builder_from_voronoi_without_sites(v: &Voronoi) -> Self {
Self {
bounding_box: v.bounding_box.clone(),
clip_behavior: v.clip_behavior,
lloyd_iterations: 0,
sites: None,
}
}
}
impl From<&Voronoi> for VoronoiBuilder {
fn from(v: &Voronoi) -> Self {
let mut builder = Self::create_builder_from_voronoi_without_sites(v);
builder.sites = Some(v.sites.clone());
builder
}
}
impl From<Voronoi> for VoronoiBuilder {
fn from(v: Voronoi) -> Self {
let mut builder = Self::create_builder_from_voronoi_without_sites(&v);
builder.sites = Some(v.sites);
builder
}
}