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