-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplot.rs
More file actions
290 lines (236 loc) · 8.06 KB
/
plot.rs
File metadata and controls
290 lines (236 loc) · 8.06 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! **plot** is the backend that actually renders the plots.
//!
//! Users of **dataplotlib** should not need to access **plot**.
use std::time::Duration;
use std::{mem, thread, f64};
use plotbuilder::*;
use draw::{Drawable, Event, Range, Range2d};
pub struct Plot {}
fn get_max(user_max: Option<f64>, values: &Vec<f64>) -> f64 {
if let Some(max) = user_max {
max
} else {
let mut max = *values.first().unwrap();
for val in values {
if *val > max {
max = *val;
}
}
max
}
}
fn get_min(user_min: Option<f64>, values: &Vec<f64>) -> f64 {
if let Some(min) = user_min {
min
} else {
let mut min = *values.first().unwrap();
for val in values {
if *val < min {
min = *val;
}
}
min
}
}
fn f32_4_to_color(col: [f32; 4]) -> [u8; 4] {
[
(col[0] * 255f32) as u8,
(col[1] * 255f32) as u8,
(col[2] * 255f32) as u8,
(col[3] * 255f32) as u8,
]
}
fn draw_borders(bordercol: [u8; 4], bgcol: [u8; 4], space: (f64, f64), m: (f64, f64), renderer: &mut Drawable) {
renderer.set_color(bordercol);
renderer.clear();
renderer.set_color(bgcol);
renderer.rectangle(space, m);
renderer.set_color([0, 0, 255, 255]);
renderer.unfilled_rectangle(space, m);
}
fn set_xy(xy: &Vec<(f64, f64)>, x_vector: &mut Vec<Vec<f64>>, y_vector: &mut Vec<Vec<f64>>) {
x_vector.push(Vec::new());
y_vector.push(Vec::new());
let last_index = x_vector.len() - 1;
for &(x, y) in xy {
x_vector[last_index].push(x);
y_vector[last_index].push(y);
}
}
fn clip_line(mut a: (f64, f64), mut b: (f64, f64), view: Range2d) -> Option<((f64, f64), (f64, f64))> {
//trivial accept
if view.contains(a) && view.contains(b) {
return Some((a, b));
}
//trivial reject:
// check if both are to the right of the graph
if a.0 > view.0.max && b.0 > view.0.max
// check if both are to the left
|| a.0 < view.0.min && b.0 < view.0.min
// check if both are above
|| a.1 > view.1.max && b.1 > view.1.max
// check if both are below
|| a.1 < view.1.min && b.1 < view.1.min
{
return None;
}
let slope = (b.1 - a.1) / (b.0 - a.0);
let ymin = slope * (view.0.min - a.0) + a.1;
let ymax = slope * (view.0.max - a.0) + a.1;
if a.0 < view.0.min {
a = (view.0.min, ymin);
} else if b.0 < view.0.min {
b = (view.0.min, ymin);
} else if a.0 > view.0.max {
a = (view.0.max, ymax);
} else if b.0 > view.0.max {
b = (view.0.max, ymax);
}
let slope = 1. / slope;
let xmin = slope * (view.1.min - a.1) + a.0;
let xmax = slope * (view.1.max - a.1) + a.0;
if a.1 < view.1.min {
a = (xmin, view.1.min);
} else if b.1 < view.1.min {
b = (xmin, view.1.min);
} else if a.1 > view.1.max {
a = (xmax, view.1.max);
} else if b.1 > view.1.max {
b = (xmax, view.1.max);
}
return clip_line(a, b, view);
}
fn draw_plots(renderer: &mut Drawable, xs: &Vec<Vec<f64>>, ys: &Vec<Vec<f64>>, colors: &Vec<[f32; 4]>, plot_bounds: [f64; 4]) {
let bordercol = f32_4_to_color([0.95, 0.95, 0.95, 1.0]);
let bgcol = f32_4_to_color([1.0, 1.0, 1.0, 1.0]);
let margin = 0.05;
let w = Range {
min: plot_bounds[2],
max: plot_bounds[0],
};
let h = Range {
min: plot_bounds[3],
max: plot_bounds[1],
};
renderer.set_view(Range2d(w, h));
let update_frame = |renderer: &mut Drawable| {
let Range2d(w, h) = renderer.get_view();
// calculate margins around plot
let w_marg = w.size() * margin;
let h_marg = h.size() * margin;
// set up a "fake" plot view that has extra margins
let w_fake = Range {
min: w.min - w_marg,
max: w.max + w_marg,
};
let h_fake = Range {
min: h.min - h_marg,
max: h.max + h_marg,
};
renderer.set_view(Range2d(w_fake, h_fake));
// the borders are just the edges of the real view
let border_min = (w.min, h.min);
let border_max = (w.max, h.max);
draw_borders(bordercol, bgcol, border_min, border_max, renderer);
for i in 0..colors.len() {
let color = colors[i];
let color_rgba = f32_4_to_color(color);
renderer.set_color(color_rgba);
let yt = &ys[i];
let xt = &xs[i];
// The number of points
let len = xs[i].len();
for j in 0..len - 1 {
let a = (xt[j + 0], yt[j + 0]);
let b = (xt[j + 1], yt[j + 1]);
if let Some(((xa, ya), (xb, yb))) = clip_line(a, b, Range2d(w, h)) {
renderer.thick_line((xa, ya), (xb, yb), 2);
}
}
}
renderer.present();
// reset the view to the real view
renderer.set_view(Range2d(w, h));
};
update_frame(renderer);
'main: loop {
let mut update = false;
for event in renderer.get_events() {
match event {
Event::Quit { .. } => break 'main,
Event::KeyDown(keycode) => {
if keycode == 1 {
//Keycode::Escape {
break 'main;
}
}
Event::MouseScroll(_x, y) => {
let multiplier = (y as f64) / 10.0;
let Range2d(w, h) = renderer.get_view();
let w_offset = w.size() * multiplier;
let new_w = Range {
min: w.min - w_offset,
max: w.max + w_offset,
};
let h_offset = h.size() * multiplier;
let new_h = Range {
min: h.min - h_offset,
max: h.max + h_offset,
};
renderer.set_view(Range2d(new_w, new_h));
update = true;
}
Event::Resize(_, _) => {
update = true;
}
_ => {}
}
}
if update {
update_frame(renderer);
}
thread::sleep(Duration::from_millis(16));
}
}
fn get_plot_bounds(plot_builder: &PlotBuilder2D, xs: &Vec<Vec<f64>>, ys: &Vec<Vec<f64>>) -> [f64; 4] {
let mut max_xs: Vec<f64> = Vec::new();
let mut max_ys: Vec<f64> = Vec::new();
let mut min_xs: Vec<f64> = Vec::new();
let mut min_ys: Vec<f64> = Vec::new();
// Get the plot extremities
for i in 0..xs.len() {
max_xs.push(get_max(plot_builder.max_x, &xs[i]));
max_ys.push(get_max(plot_builder.max_y, &ys[i]));
min_xs.push(get_min(plot_builder.min_x, &xs[i]));
min_ys.push(get_min(plot_builder.min_y, &ys[i]));
}
let plot_bounds: [f64; 4] = [
// Apply the plot extremities to the global extremities
max_xs.iter().cloned().fold(0. / 0., f64::max),
max_ys.iter().cloned().fold(0. / 0., f64::max),
min_xs.iter().cloned().fold(0. / 0., f64::min),
min_ys.iter().cloned().fold(0. / 0., f64::min),
];
plot_bounds
}
impl Plot {
pub fn new2d(mut plot_builder: PlotBuilder2D, mut renderer: Box<Drawable>) {
let mut pvs = Vec::new();
mem::swap(&mut plot_builder.pvs, &mut pvs);
let mut colors: Vec<[f32; 4]> = Vec::new();
let mut x_points: Vec<Vec<f64>> = Vec::new();
let mut y_points: Vec<Vec<f64>> = Vec::new();
for pv in pvs.drain(..) {
match pv {
PlotVals2D::XyColor(ref col, ref xy) => {
set_xy(xy, &mut x_points, &mut y_points);
colors.push(col.clone());
}
_ => (),
}
}
// [MAX_X, MAX_Y, MIN_X, MIN_Y]
let plot_bounds: [f64; 4] = get_plot_bounds(&plot_builder, &x_points, &y_points);
draw_plots(&mut *renderer, &x_points, &y_points, &colors, plot_bounds);
}
}