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(
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}