Mesh3D

Struct Mesh3D 

Source
pub struct Mesh3D { /* private fields */ }
Expand description

A structure representing a 3D mesh plot.

The Mesh3D struct is designed to create and customize 3D mesh visualizations with support for explicit triangulation, intensity-based coloring, and various lighting effects. It can handle both auto-triangulated point clouds and explicitly defined mesh connectivity through triangle indices.

§Arguments

  • data - A reference to the DataFrame containing the mesh data.
  • x - A string slice specifying the column name for x-axis vertex coordinates.
  • y - A string slice specifying the column name for y-axis vertex coordinates.
  • z - A string slice specifying the column name for z-axis vertex coordinates.
  • i - An optional string slice specifying the column name for first vertex indices of triangles.
  • j - An optional string slice specifying the column name for second vertex indices of triangles.
  • k - An optional string slice specifying the column name for third vertex indices of triangles.
  • intensity - An optional string slice specifying the column name for intensity values used in gradient coloring.
  • intensity_mode - An optional IntensityMode specifying whether intensity applies to vertices or cells.
  • color - An optional Rgb value for uniform mesh coloring.
  • color_bar - An optional reference to a ColorBar for customizing the color legend.
  • color_scale - An optional Palette defining the color gradient for intensity mapping.
  • reverse_scale - An optional boolean to reverse the color scale direction.
  • show_scale - An optional boolean to show/hide the color bar.
  • opacity - An optional f64 value specifying mesh transparency (range: 0.0 to 1.0).
  • flat_shading - An optional boolean for angular (true) vs smooth (false) shading.
  • lighting - An optional reference to a Lighting struct for custom lighting settings.
  • light_position - An optional tuple (x, y, z) specifying the light source position.
  • delaunay_axis - An optional string specifying the axis for Delaunay triangulation (“x”, “y”, or “z”).
  • contour - An optional boolean to enable contour lines on the mesh.
  • plot_title - An optional Text struct specifying the plot title.
  • x_title - An optional Text struct for the x-axis title.
  • y_title - An optional Text struct for the y-axis title.
  • z_title - An optional Text struct for the z-axis title.
  • legend - An optional reference to a Legend struct for legend customization.

§Example

use plotlars::{Lighting, Mesh3D, Plot, Rgb, Text};
use polars::prelude::*;

let mut x = Vec::new();
let mut y = Vec::new();
let mut z = Vec::new();

let n = 20;
for i in 0..n {
    for j in 0..n {
        let xi = (i as f64 / (n - 1) as f64) * 2.0 - 1.0;
        let yj = (j as f64 / (n - 1) as f64) * 2.0 - 1.0;
        x.push(xi);
        y.push(yj);
        z.push(0.3 * ((xi * 3.0).sin() + (yj * 3.0).cos()));
    }
}

let dataset = DataFrame::new(vec![
    Column::new("x".into(), x),
    Column::new("y".into(), y),
    Column::new("z".into(), z),
])
.unwrap();

Mesh3D::builder()
    .data(&dataset)
    .x("x")
    .y("y")
    .z("z")
    .color(Rgb(200, 200, 255))
    .lighting(
        &Lighting::new()
            .ambient(0.5)
            .diffuse(0.8)
            .specular(0.5)
            .roughness(0.2)
            .fresnel(0.2),
    )
    .light_position((1, 1, 2))
    .opacity(1.0)
    .flat_shading(false)
    .contour(true)
    .plot_title(
        Text::from("Wavy Surface with Custom Lighting")
            .font("Arial")
            .size(22),
    )
    .build()
    .plot();

Example

Implementations§

Source§

impl Mesh3D

Source

pub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10, 'f11, 'f12>() -> Mesh3DBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10, 'f11, 'f12>

Examples found in repository?
examples/mesh3d.rs (line 23)
11fn example_basic_mesh() {
12    let x = vec![0.0, 1.0, 2.0, 0.0, 1.0, 2.0];
13    let y = vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0];
14    let z = vec![0.0, 0.5, 0.0, 0.0, 0.8, 0.0];
15
16    let dataset = DataFrame::new(vec![
17        Column::new("x".into(), x),
18        Column::new("y".into(), y),
19        Column::new("z".into(), z),
20    ])
21    .unwrap();
22
23    Mesh3D::builder()
24        .data(&dataset)
25        .x("x")
26        .y("y")
27        .z("z")
28        .color(Rgb(100, 150, 200))
29        .opacity(0.8)
30        .plot_title("Basic Mesh3D")
31        .build()
32        .plot();
33}
34
35fn example_with_indices() {
36    let x = vec![0.0, 1.0, 0.5, 0.5];
37    let y = vec![0.0, 0.0, 0.866, 0.289];
38    let z = vec![0.0, 0.0, 0.0, 0.816];
39    let i = vec![0, 0, 0, 1];
40    let j = vec![1, 2, 3, 2];
41    let k = vec![2, 3, 1, 3];
42
43    let dataset = DataFrame::new(vec![
44        Column::new("x".into(), x),
45        Column::new("y".into(), y),
46        Column::new("z".into(), z),
47        Column::new("i".into(), i),
48        Column::new("j".into(), j),
49        Column::new("k".into(), k),
50    ])
51    .unwrap();
52
53    Mesh3D::builder()
54        .data(&dataset)
55        .x("x")
56        .y("y")
57        .z("z")
58        .i("i")
59        .j("j")
60        .k("k")
61        .color(Rgb(255, 100, 100))
62        .opacity(0.9)
63        .flat_shading(true)
64        .plot_title("Tetrahedron with Explicit Indices")
65        .build()
66        .plot();
67}
68
69fn example_with_intensity() {
70    let mut x = Vec::new();
71    let mut y = Vec::new();
72    let mut z = Vec::new();
73    let mut intensity = Vec::new();
74
75    for i in 0..10 {
76        for j in 0..10 {
77            let xi = i as f64 * 0.1;
78            let yj = j as f64 * 0.1;
79            x.push(xi);
80            y.push(yj);
81            z.push(
82                (xi * 2.0 * std::f64::consts::PI).sin()
83                    * (yj * 2.0 * std::f64::consts::PI).cos()
84                    * 0.3,
85            );
86            intensity.push(xi * yj);
87        }
88    }
89
90    let dataset = DataFrame::new(vec![
91        Column::new("x".into(), x),
92        Column::new("y".into(), y),
93        Column::new("z".into(), z),
94        Column::new("intensity".into(), intensity),
95    ])
96    .unwrap();
97
98    Mesh3D::builder()
99        .data(&dataset)
100        .x("x")
101        .y("y")
102        .z("z")
103        .intensity("intensity")
104        .intensity_mode(IntensityMode::Vertex)
105        .color_scale(Palette::Viridis)
106        .reverse_scale(false)
107        .show_scale(true)
108        .color_bar(
109            &ColorBar::new()
110                .x(0.85) // Move color bar very close to the plot
111                .title("Intensity"),
112        )
113        .opacity(0.95)
114        .plot_title(
115            Text::from("Mesh3D with Intensity Coloring")
116                .font("Arial")
117                .size(20),
118        )
119        .build()
120        .plot();
121}
122
123fn example_with_lighting() {
124    // Create a simple wavy surface mesh without explicit indices
125    // The mesh will be auto-triangulated
126    let mut x = Vec::new();
127    let mut y = Vec::new();
128    let mut z = Vec::new();
129
130    let n = 20;
131    for i in 0..n {
132        for j in 0..n {
133            let xi = (i as f64 / (n - 1) as f64) * 2.0 - 1.0;
134            let yj = (j as f64 / (n - 1) as f64) * 2.0 - 1.0;
135            x.push(xi);
136            y.push(yj);
137            // Create a wavy surface
138            z.push(0.3 * ((xi * 3.0).sin() + (yj * 3.0).cos()));
139        }
140    }
141
142    let dataset = DataFrame::new(vec![
143        Column::new("x".into(), x),
144        Column::new("y".into(), y),
145        Column::new("z".into(), z),
146    ])
147    .unwrap();
148
149    Mesh3D::builder()
150        .data(&dataset)
151        .x("x")
152        .y("y")
153        .z("z")
154        .color(Rgb(200, 200, 255))
155        .lighting(
156            &Lighting::new()
157                .ambient(0.5)
158                .diffuse(0.8)
159                .specular(0.5)
160                .roughness(0.2)
161                .fresnel(0.2),
162        )
163        .light_position((1, 1, 2))
164        .opacity(1.0)
165        .flat_shading(false)
166        .contour(true)
167        .plot_title(
168            Text::from("Wavy Surface with Custom Lighting")
169                .font("Arial")
170                .size(22),
171        )
172        .build()
173        .plot();
174}

Trait Implementations§

Source§

impl Clone for Mesh3D

Source§

fn clone(&self) -> Mesh3D

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Serialize for Mesh3D

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Mesh3D

§

impl !RefUnwindSafe for Mesh3D

§

impl !Send for Mesh3D

§

impl !Sync for Mesh3D

§

impl Unpin for Mesh3D

§

impl !UnwindSafe for Mesh3D

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Plot for T
where T: PlotHelper + Serialize + Clone,

Source§

fn plot(&self)

Source§

fn write_html(&self, path: impl Into<String>)

Source§

fn to_json(&self) -> Result<String, Error>

Source§

fn to_html(&self) -> String

Source§

fn to_inline_html(&self, plot_div_id: Option<&str>) -> String

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T