Skip to main content

dear_implot3d/
axis.rs

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