polyscope_rs/
slice_plane.rs1use crate::{Vec3, Vec4, with_context, with_context_mut};
29
30pub fn add_slice_plane(name: impl Into<String>) -> SlicePlaneHandle {
38 let name = name.into();
39 with_context_mut(|ctx| {
40 let length_scale = ctx.length_scale;
41 let center = (ctx.bounding_box.0 + ctx.bounding_box.1) * 0.5;
43 let plane = ctx.add_slice_plane(&name);
44 plane.set_plane_size(length_scale * 0.25);
47 plane.set_origin(center);
49 });
50 SlicePlaneHandle { name }
51}
52
53pub fn add_slice_plane_with_pose(
55 name: impl Into<String>,
56 origin: Vec3,
57 normal: Vec3,
58) -> SlicePlaneHandle {
59 let name = name.into();
60 with_context_mut(|ctx| {
61 let plane = ctx.add_slice_plane(&name);
62 plane.set_pose(origin, normal);
63 });
64 SlicePlaneHandle { name }
65}
66
67pub fn add_slice_plane_auto() -> SlicePlaneHandle {
78 let name = with_context_mut(|ctx| {
79 let mut i = 0usize;
80 let candidate = loop {
81 let c = format!("Scene Slice Plane {i}");
82 if !ctx.has_slice_plane(&c) {
83 break c;
84 }
85 i += 1;
86 };
87 let length_scale = ctx.length_scale;
88 let center = (ctx.bounding_box.0 + ctx.bounding_box.1) * 0.5;
89 let plane = ctx.add_slice_plane(&candidate);
90 plane.set_plane_size(length_scale * 0.25);
91 plane.set_origin(center);
92 candidate
93 });
94 SlicePlaneHandle { name }
95}
96
97#[must_use]
99pub fn get_slice_plane(name: &str) -> Option<SlicePlaneHandle> {
100 with_context(|ctx| {
101 if ctx.has_slice_plane(name) {
102 Some(SlicePlaneHandle {
103 name: name.to_string(),
104 })
105 } else {
106 None
107 }
108 })
109}
110
111pub fn remove_slice_plane(name: &str) {
113 with_context_mut(|ctx| {
114 ctx.remove_slice_plane(name);
115 });
116}
117
118pub fn remove_all_slice_planes() {
120 with_context_mut(|ctx| {
121 ctx.slice_planes.clear();
122 });
123}
124
125#[must_use]
127pub fn get_all_slice_planes() -> Vec<String> {
128 with_context(|ctx| {
129 ctx.slice_plane_names()
130 .into_iter()
131 .map(std::string::ToString::to_string)
132 .collect()
133 })
134}
135
136#[derive(Clone)]
138pub struct SlicePlaneHandle {
139 name: String,
140}
141
142impl SlicePlaneHandle {
143 #[must_use]
145 pub fn name(&self) -> &str {
146 &self.name
147 }
148
149 pub fn remove(self) {
154 remove_slice_plane(&self.name);
155 }
156
157 pub fn set_pose(&self, origin: Vec3, normal: Vec3) -> &Self {
159 with_context_mut(|ctx| {
160 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
161 plane.set_pose(origin, normal);
162 }
163 });
164 self
165 }
166
167 pub fn set_origin(&self, origin: Vec3) -> &Self {
169 with_context_mut(|ctx| {
170 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
171 plane.set_origin(origin);
172 }
173 });
174 self
175 }
176
177 #[must_use]
179 pub fn origin(&self) -> Vec3 {
180 with_context(|ctx| {
181 ctx.get_slice_plane(&self.name)
182 .map_or(Vec3::ZERO, polyscope_core::SlicePlane::origin)
183 })
184 }
185
186 pub fn set_normal(&self, normal: Vec3) -> &Self {
188 with_context_mut(|ctx| {
189 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
190 plane.set_normal(normal);
191 }
192 });
193 self
194 }
195
196 #[must_use]
198 pub fn normal(&self) -> Vec3 {
199 with_context(|ctx| {
200 ctx.get_slice_plane(&self.name)
201 .map_or(Vec3::Y, polyscope_core::SlicePlane::normal)
202 })
203 }
204
205 pub fn set_enabled(&self, enabled: bool) -> &Self {
207 with_context_mut(|ctx| {
208 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
209 plane.set_enabled(enabled);
210 }
211 });
212 self
213 }
214
215 #[must_use]
217 pub fn is_enabled(&self) -> bool {
218 with_context(|ctx| {
219 ctx.get_slice_plane(&self.name)
220 .is_some_and(polyscope_core::SlicePlane::is_enabled)
221 })
222 }
223
224 pub fn set_draw_plane(&self, draw: bool) -> &Self {
226 with_context_mut(|ctx| {
227 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
228 plane.set_draw_plane(draw);
229 }
230 });
231 self
232 }
233
234 #[must_use]
236 pub fn draw_plane(&self) -> bool {
237 with_context(|ctx| {
238 ctx.get_slice_plane(&self.name)
239 .is_some_and(polyscope_core::SlicePlane::draw_plane)
240 })
241 }
242
243 pub fn set_draw_widget(&self, draw: bool) -> &Self {
245 with_context_mut(|ctx| {
246 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
247 plane.set_draw_widget(draw);
248 }
249 });
250 self
251 }
252
253 #[must_use]
255 pub fn draw_widget(&self) -> bool {
256 with_context(|ctx| {
257 ctx.get_slice_plane(&self.name)
258 .is_some_and(polyscope_core::SlicePlane::draw_widget)
259 })
260 }
261
262 pub fn set_color(&self, color: Vec3) -> &Self {
264 with_context_mut(|ctx| {
265 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
266 plane.set_color(color);
267 }
268 });
269 self
270 }
271
272 #[must_use]
274 pub fn color(&self) -> Vec4 {
275 with_context(|ctx| {
276 ctx.get_slice_plane(&self.name).map_or(
277 Vec4::new(0.5, 0.5, 0.5, 1.0),
278 polyscope_core::SlicePlane::color,
279 )
280 })
281 }
282
283 pub fn set_transparency(&self, transparency: f32) -> &Self {
285 with_context_mut(|ctx| {
286 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
287 plane.set_transparency(transparency);
288 }
289 });
290 self
291 }
292
293 #[must_use]
295 pub fn transparency(&self) -> f32 {
296 with_context(|ctx| {
297 ctx.get_slice_plane(&self.name)
298 .map_or(0.3, polyscope_core::SlicePlane::transparency)
299 })
300 }
301
302 pub fn set_plane_size(&self, size: f32) -> &Self {
304 with_context_mut(|ctx| {
305 if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
306 plane.set_plane_size(size);
307 }
308 });
309 self
310 }
311
312 #[must_use]
314 pub fn plane_size(&self) -> f32 {
315 with_context(|ctx| {
316 ctx.get_slice_plane(&self.name)
317 .map_or(0.1, polyscope_core::SlicePlane::plane_size)
318 })
319 }
320}