mesh3d/
mesh3d.rs

1use plotlars::{ColorBar, IntensityMode, Lighting, Mesh3D, Palette, Plot, Rgb, Text};
2use polars::prelude::*;
3
4fn main() {
5    example_basic_mesh();
6    example_with_indices();
7    example_with_intensity();
8    example_with_lighting();
9}
10
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(&ColorBar::new().x(0.85).title("Intensity"))
109        .opacity(0.95)
110        .plot_title(
111            Text::from("Mesh3D with Intensity Coloring")
112                .font("Arial")
113                .size(20),
114        )
115        .build()
116        .plot();
117}
118
119fn example_with_lighting() {
120    // Create a simple wavy surface mesh without explicit indices
121    // The mesh will be auto-triangulated
122    let mut x = Vec::new();
123    let mut y = Vec::new();
124    let mut z = Vec::new();
125
126    let n = 20;
127    for i in 0..n {
128        for j in 0..n {
129            let xi = (i as f64 / (n - 1) as f64) * 2.0 - 1.0;
130            let yj = (j as f64 / (n - 1) as f64) * 2.0 - 1.0;
131            x.push(xi);
132            y.push(yj);
133            // Create a wavy surface
134            z.push(0.3 * ((xi * 3.0).sin() + (yj * 3.0).cos()));
135        }
136    }
137
138    let dataset = DataFrame::new(vec![
139        Column::new("x".into(), x),
140        Column::new("y".into(), y),
141        Column::new("z".into(), z),
142    ])
143    .unwrap();
144
145    Mesh3D::builder()
146        .data(&dataset)
147        .x("x")
148        .y("y")
149        .z("z")
150        .color(Rgb(200, 200, 255))
151        .lighting(
152            &Lighting::new()
153                .ambient(0.5)
154                .diffuse(0.8)
155                .specular(0.5)
156                .roughness(0.2)
157                .fresnel(0.2),
158        )
159        .light_position((1, 1, 2))
160        .opacity(1.0)
161        .flat_shading(false)
162        .contour(true)
163        .plot_title(Text::from("Mesh 3D").font("Arial").size(22))
164        .build()
165        .plot();
166}