From 7def4fcdbd1ff32c3f7211c915cda52c2aa779b6 Mon Sep 17 00:00:00 2001 From: Dorival Pedroso Date: Sat, 23 Mar 2024 17:54:27 +1000 Subject: [PATCH] Impl options to configure surface lines --- .vscode/settings.json | 2 ++ src/surface.rs | 52 +++++++++++++++++++++++++++++++++++++++++-- tests/test_surface.rs | 30 +++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d7ca886..afcb6c5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -32,6 +32,7 @@ "cstride", "dtype", "edgecolor", + "edgecolors", "facecolor", "fileio", "fontsize", @@ -45,6 +46,7 @@ "joinstyle", "labelpad", "larrow", + "limegreen", "lineplot", "linestyle", "linestyles", diff --git a/src/surface.rs b/src/surface.rs index 3e75c3d..1c2096e 100644 --- a/src/surface.rs +++ b/src/surface.rs @@ -54,6 +54,9 @@ pub struct Surface { colorbar_label: String, // Colorbar label number_format_cb: String, // Number format for labels in colorbar surf_color: String, // Const color of surface (when not using colormap) + surf_line_color: String, // Color of surface lines + surf_line_style: String, // Style of surface lines + surf_line_width: f64, // Width of surface lines wire_line_color: String, // Color of wireframe lines wire_line_style: String, // Style of wireframe line wire_line_width: f64, // Width of wireframe line @@ -80,6 +83,9 @@ impl Surface { colorbar_label: String::new(), number_format_cb: String::new(), surf_color: String::new(), + surf_line_color: String::new(), + surf_line_style: String::new(), + surf_line_width: 0.0, wire_line_color: "black".to_string(), wire_line_style: String::new(), wire_line_width: 0.0, @@ -232,6 +238,28 @@ impl Surface { self } + /// Sets the color of surface lines + pub fn set_surf_line_color(&mut self, color: &str) -> &mut Self { + self.surf_line_color = String::from(color); + self + } + + /// Sets the style of surface lines + /// + /// Options: + /// + /// * "`-`", "`:`", "`--`", "`-.`" + pub fn set_surf_line_style(&mut self, style: &str) -> &mut Self { + self.surf_line_style = String::from(style); + self + } + + /// Sets the width of surface lines + pub fn set_surf_line_width(&mut self, width: f64) -> &mut Self { + self.surf_line_width = width; + self + } + // -- wireframe ------------------------------------------------------------------------------ /// Sets the color of wireframe lines @@ -240,7 +268,7 @@ impl Surface { self } - /// Sets the style of wireframe line + /// Sets the style of wireframe lines /// /// Options: /// @@ -250,7 +278,7 @@ impl Surface { self } - /// Sets the width of wireframe line + /// Sets the width of wireframe lines pub fn set_wire_line_width(&mut self, width: f64) -> &mut Self { self.wire_line_width = width; self @@ -318,6 +346,15 @@ impl Surface { write!(&mut opt, ",cmap=plt.get_cmap('{}')", self.colormap_name).unwrap(); } } + if self.surf_line_color != "" { + write!(&mut opt, ",edgecolors='{}'", self.surf_line_color).unwrap(); + } + if self.surf_line_style != "" { + write!(&mut opt, ",linestyle='{}'", self.surf_line_style).unwrap(); + } + if self.surf_line_width > 0.0 { + write!(&mut opt, ",linewidth={}", self.surf_line_width).unwrap(); + } opt } @@ -482,6 +519,17 @@ mod tests { surface.set_surf_color("blue"); let opt = surface.options_surface(); assert_eq!(opt, ",rstride=3,cstride=4,color='blue'"); + + let mut surface = Surface::new(); + surface + .set_surf_line_color("red") + .set_surf_line_style("--") + .set_surf_line_width(2.5); + let opt = surface.options_surface(); + assert_eq!( + opt, + ",cmap=plt.get_cmap('bwr'),edgecolors='red',linestyle='--',linewidth=2.5" + ); } #[test] diff --git a/tests/test_surface.rs b/tests/test_surface.rs index 0c12b4e..05821de 100644 --- a/tests/test_surface.rs +++ b/tests/test_surface.rs @@ -68,6 +68,36 @@ fn test_surface_color() -> Result<(), StrError> { Ok(()) } +#[test] +fn test_surface_lines() -> Result<(), StrError> { + let mut surface = Surface::new(); + surface + .set_surf_line_color("limegreen") + .set_surf_line_style("--") + .set_surf_line_width(0.75); + + // draw surface + let n = 9; + let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y); + surface.draw(&x, &y, &z); + + // add surface to plot + let mut plot = Plot::new(); + plot.add(&surface); + + // save figure + let path = Path::new(OUT_DIR).join("integ_surface_lines.svg"); + plot.save(&path)?; + + // check number of lines + let file = File::open(path).map_err(|_| "cannot open file")?; + let buffered = BufReader::new(file); + let lines_iter = buffered.lines(); + let n_lines = lines_iter.count(); + assert!(n_lines > 1000 && n_lines < 1200); + Ok(()) +} + #[test] fn test_surface_wireframe() -> Result<(), StrError> { let mut surface = Surface::new();