Skip to content

Commit

Permalink
Merge pull request #40 from cpmech/impl-surface-edge-colors
Browse files Browse the repository at this point in the history
Impl options to configure surface lines
  • Loading branch information
cpmech authored Mar 23, 2024
2 parents 4280acb + 7def4fc commit fce96a8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"cstride",
"dtype",
"edgecolor",
"edgecolors",
"facecolor",
"fileio",
"fontsize",
Expand All @@ -45,6 +46,7 @@
"joinstyle",
"labelpad",
"larrow",
"limegreen",
"lineplot",
"linestyle",
"linestyles",
Expand Down
52 changes: 50 additions & 2 deletions src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -240,7 +268,7 @@ impl Surface {
self
}

/// Sets the style of wireframe line
/// Sets the style of wireframe lines
///
/// Options:
///
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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]
Expand Down
30 changes: 30 additions & 0 deletions tests/test_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fce96a8

Please sign in to comment.