-
Notifications
You must be signed in to change notification settings - Fork 19
/
3d.rs
144 lines (119 loc) · 4.39 KB
/
3d.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
//! Simple 3d plot example derived from
//! [eframe](https://docs.rs/eframe/0.22.0/eframe/index.html#usage-native) and
//! [plotters](https://github.com/plotters-rs/plotters/blob/master/plotters/examples/3d-plot.rs)
use std::time::Duration;
use egui::{self, CentralPanel, Visuals};
use egui_plotter::EguiBackend;
use plotters::prelude::*;
const MOVE_SCALE: f32 = 0.01;
const SCROLL_SCALE: f32 = 0.001;
fn main() {
let native_options = eframe::NativeOptions::default();
eframe::run_native(
"3d Example",
native_options,
Box::new(|cc| Box::new(ThreeD::new(cc))),
)
.unwrap();
}
struct ThreeD {
chart_pitch: f32,
chart_yaw: f32,
chart_scale: f32,
chart_pitch_vel: f32,
chart_yaw_vel: f32,
}
impl ThreeD {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
// Disable feathering as it causes artifacts
let context = &cc.egui_ctx;
context.tessellation_options_mut(|tess_options| {
tess_options.feathering = false;
});
// Also enable light mode
context.set_visuals(Visuals::light());
Self {
chart_pitch: 0.3,
chart_yaw: 0.9,
chart_scale: 0.9,
chart_pitch_vel: 0.0,
chart_yaw_vel: 0.0,
}
}
}
impl eframe::App for ThreeD {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
CentralPanel::default().show(ctx, |ui| {
// First, get mouse data
let (pitch_delta, yaw_delta, scale_delta) = ui.input(|input| {
let pointer = &input.pointer;
let delta = pointer.delta();
let (pitch_delta, yaw_delta) = match pointer.primary_down() {
true => (delta.y * MOVE_SCALE, -delta.x * MOVE_SCALE),
false => (self.chart_pitch_vel, self.chart_yaw_vel),
};
let scale_delta = input.scroll_delta.y * SCROLL_SCALE;
(pitch_delta, yaw_delta, scale_delta)
});
self.chart_pitch_vel = pitch_delta;
self.chart_yaw_vel = yaw_delta;
self.chart_pitch += self.chart_pitch_vel;
self.chart_yaw += self.chart_yaw_vel;
self.chart_scale += scale_delta;
// Next plot everything
let root = EguiBackend::new(ui).into_drawing_area();
root.fill(&WHITE).unwrap();
let x_axis = (-3.0..3.0).step(0.1);
let z_axis = (-3.0..3.0).step(0.1);
let mut chart = ChartBuilder::on(&root)
.caption(format!("3D Plot Test"), (FontFamily::SansSerif, 20))
.build_cartesian_3d(x_axis, -3.0..3.0, z_axis)
.unwrap();
chart.with_projection(|mut pb| {
pb.yaw = self.chart_yaw as f64;
pb.pitch = self.chart_pitch as f64;
pb.scale = self.chart_scale as f64;
pb.into_matrix()
});
chart
.configure_axes()
.light_grid_style(BLACK.mix(0.15))
.max_light_lines(3)
.draw()
.unwrap();
chart
.draw_series(
SurfaceSeries::xoz(
(-30..30).map(|f| f as f64 / 10.0),
(-30..30).map(|f| f as f64 / 10.0),
|x, z| (x * x + z * z).cos(),
)
.style(BLUE.mix(0.2).filled()),
)
.unwrap()
.label("Surface")
.legend(|(x, y)| {
Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled())
});
chart
.draw_series(LineSeries::new(
(-100..100)
.map(|y| y as f64 / 40.0)
.map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
&BLACK,
))
.unwrap()
.label("Line")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLACK));
chart
.configure_series_labels()
.border_style(BLACK)
.draw()
.unwrap();
root.present().unwrap();
});
// Limit framerate to 100fps
std::thread::sleep(Duration::from_millis(10));
ctx.request_repaint();
}
}