pub struct Surface { /* private fields */ }
Expand description

Generates a 3D a surface (or wireframe, or both)

Example

use plotpy::{Plot, StrError, Surface};
use russell_lab::generate3d;

fn main() -> Result<(), StrError> {
    // generate (x,y,z) matrices
    let n = 21;
    let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x - y * y);

    // configure and draw surface + wireframe
    let mut surface = Surface::new();
    surface.set_colormap_name("seismic")
        .set_with_colorbar(true)
        .set_with_wireframe(true)
        .set_line_width(0.3);

    // draw surface + wireframe
    surface.draw(&x, &y, &z);

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface)
        .set_title("horse saddle equation") // must be after add surface
        .set_camera(20.0, 35.0); // must be after add surface

    // save figure
    plot.save("/tmp/plotpy/doc_tests/doc_surface.svg")?;
    Ok(())
}

doc_surface.svg

See also integration tests in the tests directory

Output from some integration tests:

integ_surface_wireframe.svg

Implementations

Creates a new Surface object

Draws a surface, or wireframe, or both

Input
  • x – matrix with x values
  • y – matrix with y values
  • z – matrix with z values
Flags

The following flags control what features are not to be drawn:

  • surface – draws surface
  • wireframe – draws wireframe
Notes
  • The type U of the input matrices must be a number.

Sets the row stride

Sets the column stride

Sets option to generate surface

Sets option to generate wireframe

Sets the colormap index

Options:

  • 0 – bwr
  • 1 – RdBu
  • 2 – hsv
  • 3 – jet
  • 4 – terrain
  • 5 – pink
  • 6 – Greys
  • >6 – starts over from 0

Sets the colormap name

Options:

Sets option to use a colormap

Sets option to draw a colorbar

Sets the colorbar label

Sets the number format for the labels in the colorbar (cb)

Sets a solid color for the surface (disables colormap)

Sets the color of wireframe lines

Sets the style of wireframe line

Options:

  • -”, “:”, “--”, “-.

Sets the width of wireframe line

Draws a cylinder

Input
  • a – first point on the cylinder (centered) axis
  • b – second point on the cylinder (centered) axis
  • radius – the cylinder’s radius
  • ndiv_axis – number of divisions along the axis (≥ 1)
  • ndiv_perimeter – number of divisions along the cross-sectional circle perimeter (≥ 3)
Example
use plotpy::{Plot, StrError, Surface};
use std::path::Path;

fn main() -> Result<(), StrError> {
    // configure and draw surface
    let mut surface = Surface::new();
    let a = &[0.0, 0.0, 0.0];
    let b = &[0.0, 0.0, 1.0];
    surface.set_solid_color("#fcb827")
           .draw_cylinder(a, b, 0.25, 1, 20)?;

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface);

    // save figure
    plot.set_range_3d(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0)
        .set_equal_axes(true)
        .save("/tmp/plotpy/doc_tests/doc_cylinder.svg")?;
    Ok(())
}

doc_cylinder.svg

See also integration tests in the tests directory

Draws a plane that has a normal vector with a non-zero z (nzz) component

The plane may be perpendicular to z if n = (0,0,1)

Input
  • p – (len=3) point on plane
  • n – (len=3) normal vector
  • xmin and xmax – limits along x
  • ymin and ymax – limits along y
  • nx – number of divisions along x (must be ≥ 2)
  • ny – number of divisions along y (must be ≥ 2)
Output
  • x, y, z – the coordinates of all points as in a meshgrid
Example
use plotpy::{Plot, StrError, Surface};
use std::path::Path;

fn main() -> Result<(), StrError> {
    // configure and draw surface
    let mut surface = Surface::new();
    let p = &[0.0, 0.0, 0.0];
    let n = &[0.0, 0.0, 1.0];
    surface.set_solid_color("#5359e9")
           .draw_plane_nzz(p, n, -1.0, 1.0, -1.0, 1.0, 3, 3)?;

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface);

    // save figure
    plot.set_range_3d(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0)
        .set_equal_axes(true)
        .save("/tmp/plotpy/doc_tests/doc_plane_nzz.svg")?;
    Ok(())
}

doc_plane_nzz.svg

See also integration test in the tests directory.

Draws a hemisphere

Input
  • c – (len=3) center coordinates
  • r – radius
  • alpha_min – min α angle in [-180, 180) degrees
  • alpha_max – max α angle in (-180, 180] degrees
  • n_alpha – number of divisions along α (must be ≥ 2)
  • n_theta – number of divisions along θ (must be ≥ 2)
  • cup – upside-down; like a cup
Output
  • x, y, z – the coordinates of all points as in a meshgrid
Example
use plotpy::{Plot, StrError, Surface};
use std::path::Path;

fn main() -> Result<(), StrError> {
    // draw hat
    let mut hat = Surface::new();
    let c = &[-0.5, 0.0, 0.0];
    hat.set_solid_color("#17af14")
       .draw_hemisphere(c, 0.5, -180.0, 180.0, 20, 20, false)?;

    // draw cup
    let mut cup = Surface::new();
    let c = &[0.5, 0.0, 0.0];
    cup.set_solid_color("#ff8787")
       .draw_hemisphere(c, 0.5, -180.0, 180.0, 20, 20, true)?;

    // add surfaces to plot
    let mut plot = Plot::new();
    plot.add(&hat).add(&cup);

    // save figure
    plot.set_range_3d(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0)
        .set_equal_axes(true)
        .save("/tmp/plotpy/doc_tests/doc_hemisphere.svg")?;
    Ok(())
}

doc_hemisphere.svg

See also integration test in the tests directory.

Draws a superquadric (includes sphere, super-ellipsoid, and super-hyperboloid)

Input
  • c – (len=3) center coordinates
  • r – (len=3) radii
  • k – (len=3) exponents (must all be ≥ 0)
  • alpha_min – min α angle in [-180, 180) degrees
  • alpha_max – max α angle in (-180, 180] degrees
  • theta_min – min θ angle in [-90, 90) degrees
  • theta_max – max θ angle in (-90, 90] degrees
  • n_alpha – number of divisions along α (must be ≥ 2)
  • n_theta – number of divisions along θ (must be ≥ 2)
Output
  • x, y, z – the coordinates of all points as in a meshgrid

Reference: https://en.wikipedia.org/wiki/Superquadrics

Example
use plotpy::{Plot, StrError, Surface};
use std::path::Path;

fn main() -> Result<(), StrError> {
    // configure and draw surface
    let c = &[0.0, 0.0, 0.0];
    let r = &[1.0, 1.0, 1.0];
    let k = &[1.0, 2.0, 0.5];
    let mut surface = Surface::new();
    surface.set_solid_color("#cd0000")
        .draw_superquadric(c, r, k, -180.0, 180.0, -90.0, 90.0, 40, 20)?;

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface);

    // save figure
    plot.set_equal_axes(true)
        .save("/tmp/plotpy/doc_tests/doc_superquadric.svg")?;
    Ok(())
}

doc_superquadric.svg

See also integration test in the tests directory.

Draws a sphere

Input
  • c – (len=3) center coordinates
  • r – radius
  • n_alpha – number of divisions along α (must be ≥ 2)
  • n_theta – number of divisions along θ (must be ≥ 2)
Output:
  • x, y, z – the coordinates of all points as in a meshgrid
Example
use plotpy::{Plot, StrError, Surface};
use std::path::Path;

fn main() -> Result<(), StrError> {
    // configure and draw surface
    let mut surface = Surface::new();
    let c = &[0.0, 0.0, 0.0];
    surface.set_solid_color("#7812c3")
           .draw_sphere(c, 1.0, 20, 20)?;

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface);

    // save figure
    plot.set_equal_axes(true)
        .save("/tmp/plotpy/doc_tests/doc_sphere.svg")?;
    Ok(())
}

doc_sphere.svg

See also integration test in the tests directory.

Trait Implementations

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.