Skip to main content

dear_implot3d/
axis.rs

1use crate::{
2    Axis3D, Axis3DFlags, Plot3DCond, Plot3DUi, axis_tick_count_to_i32, debug_before_plot,
3    debug_before_setup, imvec2, len_i32, sys,
4};
5
6/// Axis helpers
7impl<'ui> Plot3DUi<'ui> {
8    pub fn setup_axes(
9        &self,
10        x_label: &str,
11        y_label: &str,
12        z_label: &str,
13        x_flags: Axis3DFlags,
14        y_flags: Axis3DFlags,
15        z_flags: Axis3DFlags,
16    ) {
17        self.with_bound_context(|| {
18            debug_before_setup();
19            if x_label.contains('\0') || y_label.contains('\0') || z_label.contains('\0') {
20                return;
21            }
22            dear_imgui_rs::with_scratch_txt_three(
23                x_label,
24                y_label,
25                z_label,
26                |x_ptr, y_ptr, z_ptr| unsafe {
27                    sys::ImPlot3D_SetupAxes(
28                        x_ptr,
29                        y_ptr,
30                        z_ptr,
31                        x_flags.bits() as i32,
32                        y_flags.bits() as i32,
33                        z_flags.bits() as i32,
34                    )
35                },
36            )
37        })
38    }
39
40    pub fn setup_axis(&self, axis: Axis3D, label: &str, flags: Axis3DFlags) {
41        self.with_bound_context(|| {
42            debug_before_setup();
43            if label.contains('\0') {
44                return;
45            }
46            dear_imgui_rs::with_scratch_txt(label, |ptr| unsafe {
47                sys::ImPlot3D_SetupAxis(axis as i32, ptr, flags.bits() as i32)
48            })
49        })
50    }
51
52    pub fn setup_axis_limits(&self, axis: Axis3D, min: f64, max: f64, cond: Plot3DCond) {
53        self.with_bound_context(|| {
54            debug_before_setup();
55            unsafe { sys::ImPlot3D_SetupAxisLimits(axis as i32, min, max, cond as i32) }
56        })
57    }
58
59    pub fn setup_axes_limits(
60        &self,
61        x_min: f64,
62        x_max: f64,
63        y_min: f64,
64        y_max: f64,
65        z_min: f64,
66        z_max: f64,
67        cond: Plot3DCond,
68    ) {
69        self.with_bound_context(|| {
70            debug_before_setup();
71            unsafe {
72                sys::ImPlot3D_SetupAxesLimits(x_min, x_max, y_min, y_max, z_min, z_max, cond as i32)
73            }
74        })
75    }
76
77    pub fn setup_axis_limits_constraints(&self, axis: Axis3D, v_min: f64, v_max: f64) {
78        self.with_bound_context(|| {
79            debug_before_setup();
80            unsafe { sys::ImPlot3D_SetupAxisLimitsConstraints(axis as i32, v_min, v_max) }
81        })
82    }
83
84    pub fn setup_axis_zoom_constraints(&self, axis: Axis3D, z_min: f64, z_max: f64) {
85        self.with_bound_context(|| {
86            debug_before_setup();
87            unsafe { sys::ImPlot3D_SetupAxisZoomConstraints(axis as i32, z_min, z_max) }
88        })
89    }
90
91    /// Setup axis ticks using explicit positions and optional labels.
92    ///
93    /// If `labels` is provided, it must have the same length as `values`.
94    pub fn setup_axis_ticks_values(
95        &self,
96        axis: Axis3D,
97        values: &[f64],
98        labels: Option<&[&str]>,
99        keep_default: bool,
100    ) {
101        self.with_bound_context(|| {
102            debug_before_setup();
103            let Some(n_ticks) = len_i32(values.len()) else {
104                return;
105            };
106            if let Some(lbls) = labels {
107                if lbls.len() != values.len() {
108                    return;
109                }
110                let cleaned: Vec<&str> = lbls
111                    .iter()
112                    .map(|&s| if s.contains('\0') { "" } else { s })
113                    .collect();
114                dear_imgui_rs::with_scratch_txt_slice(&cleaned, |ptrs| unsafe {
115                    sys::ImPlot3D_SetupAxisTicks_doublePtr(
116                        axis as i32,
117                        values.as_ptr(),
118                        n_ticks,
119                        ptrs.as_ptr(),
120                        keep_default,
121                    )
122                });
123            } else {
124                unsafe {
125                    sys::ImPlot3D_SetupAxisTicks_doublePtr(
126                        axis as i32,
127                        values.as_ptr(),
128                        n_ticks,
129                        std::ptr::null(),
130                        keep_default,
131                    )
132                };
133            }
134        })
135    }
136
137    /// Setup evenly spaced axis ticks over a range with optional labels.
138    ///
139    /// If `labels` is provided, `n_ticks` must be at least 2 and `labels` must
140    /// have length `n_ticks`. A label length mismatch leaves the axis unchanged.
141    /// Without labels, ImPlot3D clamps a count of 1 to 2 ticks.
142    ///
143    /// # Panics
144    ///
145    /// Panics if `n_ticks` is zero, exceeds `i32::MAX`, or is less than 2 when
146    /// `labels` is provided.
147    pub fn setup_axis_ticks_range(
148        &self,
149        axis: Axis3D,
150        v_min: f64,
151        v_max: f64,
152        n_ticks: usize,
153        labels: Option<&[&str]>,
154        keep_default: bool,
155    ) {
156        if labels.is_some() {
157            // ImPlot3D clamps the count to at least 2 before indexing labels.
158            assert!(
159                n_ticks >= 2,
160                "Plot3DUi::setup_axis_ticks_range() n_ticks must be at least 2 when labels are provided"
161            );
162        }
163        let n_ticks_i32 = axis_tick_count_to_i32("Plot3DUi::setup_axis_ticks_range()", n_ticks);
164        self.with_bound_context(|| {
165            debug_before_setup();
166            if let Some(lbls) = labels {
167                if lbls.len() != n_ticks {
168                    return;
169                }
170                let cleaned: Vec<&str> = lbls
171                    .iter()
172                    .map(|&s| if s.contains('\0') { "" } else { s })
173                    .collect();
174                dear_imgui_rs::with_scratch_txt_slice(&cleaned, |ptrs| unsafe {
175                    sys::ImPlot3D_SetupAxisTicks_double(
176                        axis as i32,
177                        v_min,
178                        v_max,
179                        n_ticks_i32,
180                        ptrs.as_ptr(),
181                        keep_default,
182                    )
183                });
184            } else {
185                unsafe {
186                    sys::ImPlot3D_SetupAxisTicks_double(
187                        axis as i32,
188                        v_min,
189                        v_max,
190                        n_ticks_i32,
191                        std::ptr::null(),
192                        keep_default,
193                    )
194                };
195            }
196        })
197    }
198
199    pub fn setup_box_scale(&self, x: f32, y: f32, z: f32) {
200        self.with_bound_context(|| {
201            debug_before_setup();
202            unsafe { sys::ImPlot3D_SetupBoxScale(x as f64, y as f64, z as f64) }
203        })
204    }
205
206    pub fn setup_box_rotation(
207        &self,
208        elevation: f32,
209        azimuth: f32,
210        animate: bool,
211        cond: Plot3DCond,
212    ) {
213        self.with_bound_context(|| {
214            debug_before_setup();
215            unsafe {
216                sys::ImPlot3D_SetupBoxRotation_double(
217                    elevation as f64,
218                    azimuth as f64,
219                    animate,
220                    cond as i32,
221                )
222            }
223        })
224    }
225
226    pub fn setup_box_initial_rotation(&self, elevation: f32, azimuth: f32) {
227        self.with_bound_context(|| {
228            debug_before_setup();
229            unsafe {
230                sys::ImPlot3D_SetupBoxInitialRotation_double(elevation as f64, azimuth as f64)
231            }
232        })
233    }
234
235    pub fn plot_text(&self, text: &str, x: f32, y: f32, z: f32, angle: f32, pix_offset: [f32; 2]) {
236        self.with_bound_context(|| {
237            if text.contains('\0') {
238                return;
239            }
240            dear_imgui_rs::with_scratch_txt(text, |text_ptr| unsafe {
241                debug_before_plot();
242                sys::ImPlot3D_PlotText(
243                    text_ptr,
244                    x as f64,
245                    y as f64,
246                    z as f64,
247                    angle as f64,
248                    imvec2(pix_offset[0], pix_offset[1]),
249                )
250            })
251        })
252    }
253
254    pub fn plot_to_pixels(&self, point: [f32; 3]) -> [f32; 2] {
255        self.with_bound_context(|| unsafe {
256            let out = sys::ImPlot3D_PlotToPixels_double(
257                point[0] as f64,
258                point[1] as f64,
259                point[2] as f64,
260            );
261            [out.x, out.y]
262        })
263    }
264
265    pub fn get_frame_pos(&self) -> [f32; 2] {
266        self.with_bound_context(|| unsafe {
267            let out = sys::ImPlot3D_GetPlotRectPos();
268            [out.x, out.y]
269        })
270    }
271
272    pub fn get_frame_size(&self) -> [f32; 2] {
273        self.with_bound_context(|| unsafe {
274            let out = sys::ImPlot3D_GetPlotRectSize();
275            [out.x, out.y]
276        })
277    }
278}