-
Notifications
You must be signed in to change notification settings - Fork 3
/
2d_poisson.rs
133 lines (102 loc) · 3.32 KB
/
2d_poisson.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
use std::fs;
use ndarray::{array, s};
use ndarray::{Array1, Array2};
use pointprocesses::spatial::*;
use plotters::prelude::*;
static IMG_SIZE: (u32, u32) = (480, 480);
static TITLE_FONT: &str = "Arial";
fn main() -> Result<(), Box<dyn std::error::Error>> {
fs::create_dir("lib/examples/images").unwrap_or_default();
square_example()?;
variable_circle_example()?;
Ok(())
}
/// The `plotlib` library expects vectors of tuples for input.
fn cast_to_tuples(arr: Array2<f64>) -> Vec<(f64,f64)> {
let n = arr.shape()[0];
(0..n).map(|i: usize| {
let v = arr.slice(s![i,..]);
(v[0], v[1])
}).collect()
}
fn square_example() -> Result<(), Box<dyn std::error::Error>> {
let lambda = 200.0;
let close = array![0.0,0.0];
let far = array![1.0,1.0];
let domain = Domain::new(close, far);
let events = poisson_process(lambda, &domain);
// println!("{:?}", events);
println!("Simulated {} events.", events.shape()[0]);
let d = events.shape()[1];
assert_eq!(d, 2);
let data = cast_to_tuples(events);
let root = BitMapBackend::new(
"lib/examples/images/2d_poisson_rect.png", IMG_SIZE)
.into_drawing_area();
root.fill(&WHITE)?;
let chart_title = "2D Poisson process";
let mut chart = ChartBuilder::on(&root)
.caption(chart_title, (TITLE_FONT, 16).into_font())
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_ranged(0.0..1.0, 0.0..1.0)?;
chart.configure_mesh().draw()?;
let size = 2;
chart.draw_series(
data.iter()
.map(|(x,y)| {
Circle::new((*x, *y), size,
RED.filled())
})
)?;
Ok(())
}
fn variable_circle_example() -> Result<(), Box<dyn std::error::Error>> {
let close: Array1<f64> = array![0.,0.];
let far = array![1.,1.];
let domain = Domain::new(close, far);
// the intensity function will be the distance to center
let lambda = |v: &Array1<f64>| {
use std::f64::consts::PI;
let center = array![0.5,0.5];
let radius = 1.;
let v = v - ¢er;
let distance = v.dot(&v).sqrt();
let x = v[0];
let y = v[1];
let angle = y.atan2(x);
let perturb = 0.04*(10.0*angle).cos();
if distance <= radius {
4000.0 * (6.0 * (distance + perturb) * PI).cos()
} else {
0.0
}
};
let events = variable_poisson(lambda, 4000.0, &domain);
// not printing this, the stdout gets flooded with all these evts
// println!("{:?}", events);
println!("Simulated {} events.", events.shape()[0]);
let data = cast_to_tuples(events);
let root = BitMapBackend::new(
"lib/examples/images/2d_poisson_circle.png", IMG_SIZE)
.into_drawing_area();
root.fill(&WHITE)?;
let chart_title = "2D Poisson process";
let mut chart = ChartBuilder::on(&root)
.caption(chart_title, (TITLE_FONT, 16).into_font())
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_ranged(0.0..1.0, 0.0..1.0)?;
chart.configure_mesh().draw()?;
let size = 2;
chart.draw_series(
data.iter()
.map(|(x,y)| {
Circle::new((*x, *y), size,
RED.filled())
})
)?;
Ok(())
}