From c0db71d2ae6bb798b3936a3abfa7dbff34463c48 Mon Sep 17 00:00:00 2001 From: Josh Leverette Date: Wed, 23 Aug 2017 22:03:13 -0400 Subject: [PATCH 1/4] get the image backend started --- src/draw_image.rs | 150 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 9 ++- 2 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/draw_image.rs diff --git a/src/draw_image.rs b/src/draw_image.rs new file mode 100644 index 0000000..babe037 --- /dev/null +++ b/src/draw_image.rs @@ -0,0 +1,150 @@ +use image::{self, ImageBuffer}; +use draw::*; + + +pub struct DrawImage { + context: ImageBuffer, + screenspace: Range2d, + realspace: Range2d, + color: [u8; 4], +} +#![allow(dead_code)] +#![allow(unused_variables)] + +impl DrawImage { + pub fn new(sdlh: Sdl2Mt) -> Box { + let window_id = sdlh.create_simple_window("2D plot", 720, 720).unwrap(); + + let default_s = Range { min: 0.0, max: 0.0 }; + + let default_r = Range { + min: 0.0, + max: 720.0, + }; + + Box::new(DrawSDL { + sdlh, + window_id, + screenspace: Range2d(default_s, default_s), + realspace: Range2d(default_r, default_r), + color: pixels::Color::RGBA(0, 0, 0, 255), + }) + } +} + +impl Drawable for DrawImage { + /// Sets the visible range of worldspace + fn set_view(&mut self, view: Range2d) { + self.screenspace = view; + } + + /// Gets the visible range of worldspace + fn get_view(&self) -> Range2d { + self.screenspace + } + + /// Set color for various drawing actions + fn set_color(&mut self, color: [u8; 4]) { + self.color = pixels::Color::RGBA(color[0], color[1], color[2], color[3]); + } + + /// Clears the output surface + fn clear(&mut self) { + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.clear(); +// })) +// .unwrap(); + } + + /// Draws a line from (x, y) -> (x, y) in worldspace + fn line(&mut self, p1: (f64, f64), p2: (f64, f64)) { + self.thick_line(p1, p2, 1); + } + + /// Draws a line from (x, y) -> (x, y) in worldspace + fn thick_line(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64), thickness: u16) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, true); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, true); + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas +// .draw_line((x1 as i32, y1 as i32), (x2 as i32, y2 as i32)) +// .unwrap(); +// })) +// .unwrap(); + } + + /// Draws a rectangle bounded by two corners + fn rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false); + + let x1 = x1 as i32; + let y1 = y1 as i32; + let w = (x2 as i32 - x1) as u32; + let h = (y2 as i32 - y1) as u32; + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.fill_rect(Rect::new(x1, y1, w, h)).unwrap(); +// })) +// .unwrap(); + } + + /// Draws a rectangle bounded by two corners + fn unfilled_rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false); + + let x1 = x1 as i32; + let y1 = y1 as i32; + let w = (x2 as i32 - x1) as u32; + let h = (y2 as i32 - y1) as u32; + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.draw_rect(Rect::new(x1, y1, w, h)).unwrap(); +// })) +// .unwrap(); + } + + fn present(&mut self) { +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.present(); +// })) +// .unwrap(); + } + + /// Returns the next pending event + fn get_events(&mut self) -> Vec { + vec![Event::Quit] + } +} diff --git a/src/lib.rs b/src/lib.rs index 125e1e8..edb8fd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ //! use dataplotlib::util::{linspace, zip2}; //! use dataplotlib::plotbuilder::PlotBuilder2D; //! use dataplotlib::plotter::Plotter; +//! use dataplotlib::draw_sdl::DrawSDL; //! //! fn main() { //! let x = linspace(0, 10, 100); @@ -23,9 +24,11 @@ //! pb.add_color_xy(xy_sin, [1.0, 0.0, 0.0, 1.0]); //! pb.add_color_xy(xy_lin, [0.0, 0.0, 1.0, 1.0]); //! +//! let sdlh = dataplotlib::sdl2_init(); +//! let sdl2_window = DrawSDL::new(sdlh); +//! //! let mut plt = Plotter::new(); -//! plt.plot2d(pb); -//! plt.join(); +//! plt.plot2d(pb, sdl2_window); //! } //! ``` @@ -38,6 +41,8 @@ pub mod draw_sdl; #[cfg(feature = "use-image")] extern crate image; +#[cfg(feature = "use-image")] +pub mod draw_image; pub mod draw; mod plot; From 53c8fa59444f1d8b02578c7ae10c43d83a866319 Mon Sep 17 00:00:00 2001 From: Josh Leverette Date: Wed, 23 Aug 2017 22:14:51 -0400 Subject: [PATCH 2/4] more changes to image backend --- src/draw_image.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/draw_image.rs b/src/draw_image.rs index babe037..d39c55b 100644 --- a/src/draw_image.rs +++ b/src/draw_image.rs @@ -8,13 +8,9 @@ pub struct DrawImage { realspace: Range2d, color: [u8; 4], } -#![allow(dead_code)] -#![allow(unused_variables)] impl DrawImage { - pub fn new(sdlh: Sdl2Mt) -> Box { - let window_id = sdlh.create_simple_window("2D plot", 720, 720).unwrap(); - + pub fn new() -> Box { let default_s = Range { min: 0.0, max: 0.0 }; let default_r = Range { @@ -22,12 +18,10 @@ impl DrawImage { max: 720.0, }; - Box::new(DrawSDL { - sdlh, - window_id, + Box::new(DrawImage { screenspace: Range2d(default_s, default_s), realspace: Range2d(default_r, default_r), - color: pixels::Color::RGBA(0, 0, 0, 255), + color: [0, 0, 0, 255], }) } } @@ -45,7 +39,7 @@ impl Drawable for DrawImage { /// Set color for various drawing actions fn set_color(&mut self, color: [u8; 4]) { - self.color = pixels::Color::RGBA(color[0], color[1], color[2], color[3]); + self.color = color; } /// Clears the output surface From d55153b783f438cf003a1e2ccb127f9b1e87bfb6 Mon Sep 17 00:00:00 2001 From: Josh Leverette Date: Fri, 13 Oct 2017 23:00:13 -0400 Subject: [PATCH 3/4] switch the default feature to use-image for this branch --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f922eb1..12de74d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ name = "dataplotlib" version = "0.1.3" [features] -default = ["use-sdl2"] +default = ["use-image"] use-sdl2 = ["sdl2_mt"] use-image = ["image"] From 47490f4fc3ab2da807c6df1ff5f34ae7a7521b93 Mon Sep 17 00:00:00 2001 From: Josh Leverette Date: Fri, 13 Oct 2017 23:08:52 -0400 Subject: [PATCH 4/4] update travis script --- .travis.yml | 2 +- src/plotter.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a861c8d..d9f426d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ install: - (cd SDL2-2.0.5 && ./configure && make && sudo make install) script: - cargo build -v - - cargo test --no-run \ No newline at end of file + - cargo test --no-run --features="use-sdl2 use-image" \ No newline at end of file diff --git a/src/plotter.rs b/src/plotter.rs index b285e84..9f5a9fc 100644 --- a/src/plotter.rs +++ b/src/plotter.rs @@ -55,6 +55,7 @@ impl Drop for Plotter { } #[cfg(test)] +#[cfg(feature="use-sdl2")] mod test { use super::*; use plotbuilder::*;