-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
243 lines (205 loc) · 7.17 KB
/
lib.rs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![allow(dead_code)]
// Importer le module rand pour la génération de nombres aléatoires
use rand::*;
use std::f64::consts::PI;
use std::fmt;
// Point structure
#[derive(Default, Clone, Copy)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
fn new() -> Self {
Default::default()
}
fn sqr_dist(self: Self, point: Point) -> f64 {
(point.x - self.x).powi(2) + (point.y - self.y).powi(2)
}
}
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// HLSL style 'copy-paste'-able
write!(f, "float2({}, {})", self.x, self.y)
}
}
#[derive(Default)]
pub enum DomainType {
#[default]
Invalid,
Box2D {
w: f64,
h: f64,
},
Disk {
radius: f64,
},
}
// Domain structure which hold a grid and the points on it
#[derive(Default)]
pub struct Domain {
pub grid: Vec<Vec<usize>>,
pub data: Vec<Point>,
pub cell_size: f64,
pub width: f64,
pub height: f64,
pub grid_width: usize,
pub grid_height: usize,
pub domain_type: DomainType,
}
impl Domain {
pub fn box2d(width: f64, height: f64, sample_dist: f64) -> Self {
const N: u8 = 2;
let cell_size = sample_dist / (N as f64).sqrt();
let ncells_width = (width / cell_size).ceil() as usize;
let ncells_height = (height / cell_size).ceil() as usize;
Domain {
cell_size: cell_size,
width: width,
height: height,
grid_width: ncells_width,
grid_height: ncells_height,
grid: vec![vec![usize::MAX; ncells_width]; ncells_height],
domain_type: DomainType::Box2D { w: width, h: height },
..Default::default()
}
}
pub fn disk(radius: f64, sample_dist: f64) -> Self {
let mut domain = Domain {
domain_type: DomainType::Disk { radius: radius },
..Default::default()
};
let n = domain.get_dimension();
let cell_size = sample_dist / (n as f64).sqrt();
let ncells = (2.0 * radius / cell_size).ceil() as usize;
domain.cell_size = cell_size;
domain.grid_width = ncells;
domain.grid_height = ncells;
domain.grid = vec![vec![usize::MAX; ncells]; ncells];
domain
}
fn get_dimension(self: &Self) -> u8 {
#![allow(unused_variables)]
match self.domain_type {
DomainType::Box2D { w, h } => 2,
DomainType::Disk { radius } => 2,
DomainType::Invalid => 0,
}
}
fn extent(self: &Self) -> Point {
match self.domain_type {
DomainType::Box2D { w, h } => Point { x: w, y: h },
DomainType::Disk { radius } => Point {
x: 2.0 * radius,
y: 2.0 * radius,
},
DomainType::Invalid => Point::default(),
}
}
fn is_within_boundaries(self: &Self, point: Point) -> bool {
match self.domain_type {
DomainType::Box2D { w, h } => point.x.abs() < w / 2.0 && point.y.abs() < h / 2.0,
DomainType::Disk { radius } => (point.x.powi(2) + point.y.powi(2)) < radius.powi(2),
DomainType::Invalid => false,
}
}
pub fn gen_random_point_within_domain(self: &Self) -> Point {
match self.domain_type {
DomainType::Box2D { w, h } => Point {
x: rand::random::<f64>() * w - w / 2.0,
y: rand::random::<f64>() * h - h / 2.0,
},
DomainType::Disk { radius } => {
let sin_cos_theta = (rand::random::<f64>() * 2.0 * PI).sin_cos();
let r = rand::random::<f64>() * radius;
Point {
x: r * sin_cos_theta.0,
y: r * sin_cos_theta.1,
}
}
DomainType::Invalid => Point::default(),
}
}
pub fn insert_point(self: &mut Self, point: Point) {
if !self.is_within_boundaries(point) {
return;
}
let extent = self.extent();
let x_index = ((point.x + extent.x / 2.0) / self.cell_size).floor() as usize;
let y_index = ((point.y + extent.y / 2.0) / self.cell_size).floor() as usize;
//assert!(self.grid[y_index][x_index] == usize::MAX);
self.grid[y_index][x_index] = self.data.len();
self.data.push(point);
}
pub fn is_valid_point(self: &Self, point: Point, radius: f64) -> bool {
if !self.is_within_boundaries(point) {
return false;
}
let extent = self.extent();
let x_index = ((point.x + extent.x / 2.0) / self.cell_size).floor() as usize;
let y_index = ((point.y + extent.y / 2.0) / self.cell_size).floor() as usize;
let i0 = 0.max(x_index as i64 - 1) as usize;
let i1 = (self.grid_width - 1).min(x_index + 1);
let j0 = 0.max(y_index as i64 - 1) as usize;
let j1 = (self.grid_height - 1).min(y_index + 1);
// Rejects the point if too close to the others
for j in j0..=j1 {
for i in i0..=i1 {
if self.grid[j][i] != usize::MAX && point.sqr_dist(self.data[self.grid[j][i]]) < radius.powi(2) {
return false;
}
}
}
return true;
}
}
fn poisson_sample_internal(sample_dist: f64, k: usize, domain: &mut Domain) -> Vec<Point> {
// The final list of points
let mut points = Vec::<Point>::new();
// The current "active" list of points
let mut active = Vec::<Point>::new();
// Initial random point p0
let p0 = domain.gen_random_point_within_domain();
domain.insert_point(p0);
points.push(p0);
active.push(p0);
let mut rng = rand::thread_rng();
while active.len() > 0 {
// Pick a point 'p' from our active list
let idx = rng.gen_range(0..active.len());
let p_active = active[idx];
// Try up to 'k' times to find a point that satifies:
// - is at a distance between r and 2r from p
// - is at a distance > r from nearby points
let mut i = k;
while i > 0 {
let theta = rng.gen_range(0.0..2.0 * PI);
let r = rng.gen_range(sample_dist..2.0 * sample_dist);
let p = Point {
x: p_active.x + r * theta.cos(),
y: p_active.y + r * theta.sin(),
};
// If we succeed in finding a point, add to grid and list
if domain.is_valid_point(p, sample_dist) {
points.push(p);
domain.insert_point(p);
active.push(p);
break;
}
i -= 1;
}
// Otherwise, remove point 'p' from the active list
if i == 0 {
active.remove(idx);
}
}
points
}
pub fn poisson_sample_box2d(sample_dist: f64, k: usize, width: f64, height: f64) -> Vec<Point> {
let mut domain = Domain::box2d(width, height, sample_dist);
poisson_sample_internal(sample_dist, k, &mut domain)
}
pub fn poisson_sample_disk(sample_dist: f64, k: usize, domain_radius: f64) -> Vec<Point> {
let mut domain = Domain::disk(domain_radius, sample_dist);
poisson_sample_internal(sample_dist, k, &mut domain)
}