|
| 1 | +use gtk::glib; |
| 2 | +use gtk::prelude::*; |
| 3 | +use gtk::subclass::prelude::*; |
| 4 | + |
| 5 | +use std::cell::Cell; |
| 6 | +use std::error::Error; |
| 7 | +use std::f64; |
| 8 | + |
| 9 | +use plotters::prelude::*; |
| 10 | +use plotters_cairo::CairoBackend; |
| 11 | + |
| 12 | +use once_cell::sync::Lazy; |
| 13 | + |
| 14 | +#[derive(Debug, Default)] |
| 15 | +pub struct GaussianPlot { |
| 16 | + pitch: Cell<f64>, |
| 17 | + yaw: Cell<f64>, |
| 18 | + mean_x: Cell<f64>, |
| 19 | + mean_y: Cell<f64>, |
| 20 | + std_x: Cell<f64>, |
| 21 | + std_y: Cell<f64>, |
| 22 | +} |
| 23 | + |
| 24 | +#[glib::object_subclass] |
| 25 | +impl ObjectSubclass for GaussianPlot { |
| 26 | + const NAME: &'static str = "GaussianPlot"; |
| 27 | + type Type = super::GaussianPlot; |
| 28 | + type ParentType = gtk::Widget; |
| 29 | +} |
| 30 | + |
| 31 | +impl ObjectImpl for GaussianPlot { |
| 32 | + fn properties() -> &'static [glib::ParamSpec] { |
| 33 | + static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| { |
| 34 | + vec![ |
| 35 | + glib::ParamSpecDouble::builder("pitch") |
| 36 | + .minimum(-f64::consts::PI) |
| 37 | + .maximum(f64::consts::PI) |
| 38 | + .build(), |
| 39 | + glib::ParamSpecDouble::builder("yaw") |
| 40 | + .minimum(0.0) |
| 41 | + .maximum(f64::consts::PI) |
| 42 | + .build(), |
| 43 | + glib::ParamSpecDouble::builder("mean-x") |
| 44 | + .minimum(-10.0) |
| 45 | + .maximum(10.0) |
| 46 | + .build(), |
| 47 | + glib::ParamSpecDouble::builder("mean-y") |
| 48 | + .minimum(-10.0) |
| 49 | + .maximum(10.0) |
| 50 | + .build(), |
| 51 | + glib::ParamSpecDouble::builder("std-x") |
| 52 | + .minimum(0.0) |
| 53 | + .maximum(10.0) |
| 54 | + .build(), |
| 55 | + glib::ParamSpecDouble::builder("std-y") |
| 56 | + .minimum(0.0) |
| 57 | + .maximum(10.0) |
| 58 | + .build(), |
| 59 | + ] |
| 60 | + }); |
| 61 | + PROPERTIES.as_ref() |
| 62 | + } |
| 63 | + |
| 64 | + fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { |
| 65 | + match pspec.name() { |
| 66 | + "pitch" => { |
| 67 | + self.pitch.set(value.get().unwrap()); |
| 68 | + } |
| 69 | + "yaw" => { |
| 70 | + self.yaw.set(value.get().unwrap()); |
| 71 | + } |
| 72 | + "mean-x" => { |
| 73 | + self.mean_x.set(value.get().unwrap()); |
| 74 | + } |
| 75 | + "mean-y" => { |
| 76 | + self.mean_y.set(value.get().unwrap()); |
| 77 | + } |
| 78 | + "std-x" => { |
| 79 | + self.std_x.set(value.get().unwrap()); |
| 80 | + } |
| 81 | + "std-y" => { |
| 82 | + self.std_y.set(value.get().unwrap()); |
| 83 | + } |
| 84 | + _ => unimplemented!(), |
| 85 | + } |
| 86 | + self.obj().queue_draw(); |
| 87 | + } |
| 88 | + |
| 89 | + fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { |
| 90 | + match pspec.name() { |
| 91 | + "pitch" => self.pitch.get().to_value(), |
| 92 | + "yaw" => self.yaw.get().to_value(), |
| 93 | + "mean-x" => self.mean_x.get().to_value(), |
| 94 | + "mean-y" => self.mean_y.get().to_value(), |
| 95 | + "std-x" => self.std_x.get().to_value(), |
| 96 | + "std-y" => self.std_y.get().to_value(), |
| 97 | + _ => unimplemented!(), |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl WidgetImpl for GaussianPlot { |
| 103 | + fn snapshot(&self, snapshot: >k::Snapshot) { |
| 104 | + let width = self.obj().width() as u32; |
| 105 | + let height = self.obj().height() as u32; |
| 106 | + if width == 0 || height == 0 { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + let bounds = gtk::graphene::Rect::new(0.0, 0.0, width as f32, height as f32); |
| 111 | + let cr = snapshot.append_cairo(&bounds); |
| 112 | + let backend = CairoBackend::new(&cr, (width, height)).unwrap(); |
| 113 | + self.plot_pdf(backend).unwrap(); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl GaussianPlot { |
| 118 | + fn gaussian_pdf(&self, x: f64, y: f64) -> f64 { |
| 119 | + let x_diff = (x - self.mean_x.get()) / self.std_x.get(); |
| 120 | + let y_diff = (y - self.mean_y.get()) / self.std_y.get(); |
| 121 | + let exponent = -(x_diff * x_diff + y_diff * y_diff) / 2.0; |
| 122 | + let denom = (2.0 * std::f64::consts::PI / self.std_x.get() / self.std_y.get()).sqrt(); |
| 123 | + let gaussian_pdf = 1.0 / denom; |
| 124 | + gaussian_pdf * exponent.exp() |
| 125 | + } |
| 126 | + |
| 127 | + fn plot_pdf<'a, DB: DrawingBackend + 'a>( |
| 128 | + &self, |
| 129 | + backend: DB, |
| 130 | + ) -> Result<(), Box<dyn Error + 'a>> { |
| 131 | + let root = backend.into_drawing_area(); |
| 132 | + |
| 133 | + root.fill(&WHITE)?; |
| 134 | + |
| 135 | + let mut chart = ChartBuilder::on(&root).build_cartesian_3d( |
| 136 | + -10.0f64..10.0, |
| 137 | + 0.0f64..1.2, |
| 138 | + -10.0f64..10.0, |
| 139 | + )?; |
| 140 | + |
| 141 | + chart.with_projection(|mut p| { |
| 142 | + p.pitch = self.pitch.get(); |
| 143 | + p.yaw = self.yaw.get(); |
| 144 | + p.scale = 0.7; |
| 145 | + p.into_matrix() // build the projection matrix |
| 146 | + }); |
| 147 | + |
| 148 | + chart |
| 149 | + .configure_axes() |
| 150 | + .light_grid_style(BLACK.mix(0.15)) |
| 151 | + .max_light_lines(3) |
| 152 | + .draw()?; |
| 153 | + chart.draw_series( |
| 154 | + SurfaceSeries::xoz( |
| 155 | + (-50..=50).map(|x| x as f64 / 5.0), |
| 156 | + (-50..=50).map(|x| x as f64 / 5.0), |
| 157 | + |x, y| self.gaussian_pdf(x, y), |
| 158 | + ) |
| 159 | + .style_func(&|&v| (&HSLColor(240.0 / 360.0 - 240.0 / 360.0 * v, 1.0, 0.7)).into()), |
| 160 | + )?; |
| 161 | + |
| 162 | + root.present()?; |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | +} |
0 commit comments