Skip to main content

dear_implot3d/ui/
core.rs

1use crate::builder::Plot3DBuilder;
2use crate::plots::primitive_vertex_count;
3use crate::{
4    Line3DFlags, Plot3DContext, Plot3DDataLayout, Plot3DFlags, Plot3DScope, Quad3DFlags,
5    Scatter3DFlags, Triangle3DFlags, len_i32, sys,
6};
7use dear_imgui_rs::Ui;
8
9/// Frame access to an ImPlot3D context. Begin a plot to configure axes and submit items.
10///
11/// ```compile_fail,E0599
12/// use dear_implot3d::Plot3DUi;
13/// fn query_outside_plot(ui: &Plot3DUi<'_>) { ui.get_frame_size(); }
14/// ```
15#[derive(Clone, Copy)]
16pub struct Plot3DUi<'ui> {
17    pub(crate) imgui: &'ui Ui,
18    pub(crate) context: &'ui Plot3DContext,
19}
20
21impl<'ui> Plot3DUi<'ui> {
22    pub(crate) fn with_bound_context<R>(&self, f: impl FnOnce() -> R) -> R {
23        self.imgui
24            .with_bound_context(|| self.context.with_plot_context(f))
25    }
26
27    /// Configure a plot for the current window and table cell.
28    pub fn begin_plot(&self, title: impl AsRef<str>) -> Plot3DBuilder<'ui> {
29        Plot3DBuilder {
30            ui: *self,
31            title: title.as_ref().into(),
32            size: [0.0; 2],
33            flags: Plot3DFlags::empty(),
34        }
35    }
36}
37
38impl Plot3DScope<'_> {
39    /// Convenience: plot a simple 3D line (f32)
40    ///
41    /// This is a quick way to plot a line without using the builder pattern.
42    /// For more control, use the `plots::Line3D` builder.
43    ///
44    /// # Arguments
45    ///
46    /// * `label` - Label for the legend
47    /// * `xs` - X coordinates
48    /// * `ys` - Y coordinates
49    /// * `zs` - Z coordinates
50    /// * `flags` - Line flags (e.g., `Line3DFlags::SEGMENTS`, `Line3DFlags::LOOP`)
51    ///
52    /// # Example
53    ///
54    /// ```no_run
55    /// use dear_implot3d::*;
56    ///
57    /// # fn draw(plot_ui: &Plot3DScope<'_>) {
58    /// let xs = [0.0, 1.0, 2.0];
59    /// let ys = [0.0, 1.0, 0.0];
60    /// let zs = [0.0, 0.5, 1.0];
61    /// plot_ui.plot_line_f32("Line", &xs, &ys, &zs, Line3DFlags::NONE);
62    /// # }
63    /// ```
64    pub fn plot_line_f32<S: AsRef<str>>(
65        &self,
66        label: S,
67        xs: &[f32],
68        ys: &[f32],
69        zs: &[f32],
70        flags: Line3DFlags,
71    ) {
72        let label = label.as_ref();
73        self.with_bound_context(|| {
74            if xs.len() != ys.len() || ys.len() != zs.len() {
75                return;
76            }
77            let Some(count) = len_i32(xs.len()) else {
78                return;
79            };
80            if label.contains('\0') {
81                return;
82            }
83            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
84                let spec = self.next_item.spec(flags.bits(), Plot3DDataLayout::DEFAULT);
85                if count == i32::MAX && (spec.Flags as u32 & Line3DFlags::LOOP.bits()) != 0 {
86                    return;
87                }
88                sys::ImPlot3D_PlotLine_FloatPtr(
89                    label_ptr,
90                    xs.as_ptr(),
91                    ys.as_ptr(),
92                    zs.as_ptr(),
93                    count,
94                    spec,
95                );
96            })
97        })
98    }
99
100    /// Line plot (f32) with an explicit data layout.
101    ///
102    /// # Safety
103    ///
104    /// For every submitted index, `layout` must address an initialized, properly aligned `f32`
105    /// within each coordinate allocation. All three allocations must remain alive for this call.
106    pub unsafe fn plot_line_f32_raw<S: AsRef<str>>(
107        &self,
108        label: S,
109        xs: &[f32],
110        ys: &[f32],
111        zs: &[f32],
112        flags: Line3DFlags,
113        layout: Plot3DDataLayout,
114    ) {
115        let label = label.as_ref();
116        self.with_bound_context(|| {
117            if xs.len() != ys.len() || ys.len() != zs.len() {
118                return;
119            }
120            let Some(count) = layout.sample_count(xs.len()) else {
121                return;
122            };
123            if label.contains('\0') {
124                return;
125            }
126            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
127                let spec = self.next_item.spec(flags.bits(), layout);
128                if count == i32::MAX && (spec.Flags as u32 & Line3DFlags::LOOP.bits()) != 0 {
129                    return;
130                }
131                sys::ImPlot3D_PlotLine_FloatPtr(
132                    label_ptr,
133                    xs.as_ptr(),
134                    ys.as_ptr(),
135                    zs.as_ptr(),
136                    count,
137                    spec,
138                );
139            })
140        })
141    }
142
143    /// Convenience: plot a simple 3D line (f64)
144    pub fn plot_line_f64<S: AsRef<str>>(
145        &self,
146        label: S,
147        xs: &[f64],
148        ys: &[f64],
149        zs: &[f64],
150        flags: Line3DFlags,
151    ) {
152        let label = label.as_ref();
153        self.with_bound_context(|| {
154            if xs.len() != ys.len() || ys.len() != zs.len() {
155                return;
156            }
157            let Some(count) = len_i32(xs.len()) else {
158                return;
159            };
160            if label.contains('\0') {
161                return;
162            }
163            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
164                let spec = self.next_item.spec(flags.bits(), Plot3DDataLayout::DEFAULT);
165                if count == i32::MAX && (spec.Flags as u32 & Line3DFlags::LOOP.bits()) != 0 {
166                    return;
167                }
168                sys::ImPlot3D_PlotLine_doublePtr(
169                    label_ptr,
170                    xs.as_ptr(),
171                    ys.as_ptr(),
172                    zs.as_ptr(),
173                    count,
174                    spec,
175                );
176            })
177        })
178    }
179
180    /// Line plot (f64) with an explicit data layout.
181    ///
182    /// # Safety
183    ///
184    /// For every submitted index, `layout` must address an initialized, properly aligned `f64`
185    /// within each coordinate allocation. All three allocations must remain alive for this call.
186    pub unsafe fn plot_line_f64_raw<S: AsRef<str>>(
187        &self,
188        label: S,
189        xs: &[f64],
190        ys: &[f64],
191        zs: &[f64],
192        flags: Line3DFlags,
193        layout: Plot3DDataLayout,
194    ) {
195        let label = label.as_ref();
196        self.with_bound_context(|| {
197            if xs.len() != ys.len() || ys.len() != zs.len() {
198                return;
199            }
200            let Some(count) = layout.sample_count(xs.len()) else {
201                return;
202            };
203            if label.contains('\0') {
204                return;
205            }
206            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
207                let spec = self.next_item.spec(flags.bits(), layout);
208                if count == i32::MAX && (spec.Flags as u32 & Line3DFlags::LOOP.bits()) != 0 {
209                    return;
210                }
211                sys::ImPlot3D_PlotLine_doublePtr(
212                    label_ptr,
213                    xs.as_ptr(),
214                    ys.as_ptr(),
215                    zs.as_ptr(),
216                    count,
217                    spec,
218                );
219            })
220        })
221    }
222
223    /// Convenience: plot a 3D scatter (f32)
224    pub fn plot_scatter_f32<S: AsRef<str>>(
225        &self,
226        label: S,
227        xs: &[f32],
228        ys: &[f32],
229        zs: &[f32],
230        flags: Scatter3DFlags,
231    ) {
232        let label = label.as_ref();
233        self.with_bound_context(|| {
234            if xs.len() != ys.len() || ys.len() != zs.len() {
235                return;
236            }
237            let Some(count) = len_i32(xs.len()) else {
238                return;
239            };
240            if label.contains('\0') {
241                return;
242            }
243            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
244                let spec = self.next_item.spec(flags.bits(), Plot3DDataLayout::DEFAULT);
245                sys::ImPlot3D_PlotScatter_FloatPtr(
246                    label_ptr,
247                    xs.as_ptr(),
248                    ys.as_ptr(),
249                    zs.as_ptr(),
250                    count,
251                    spec,
252                );
253            })
254        })
255    }
256
257    /// Scatter plot (f32) with an explicit data layout.
258    ///
259    /// # Safety
260    ///
261    /// For every submitted index, `layout` must address an initialized, properly aligned `f32`
262    /// within each coordinate allocation. All three allocations must remain alive for this call.
263    pub unsafe fn plot_scatter_f32_raw<S: AsRef<str>>(
264        &self,
265        label: S,
266        xs: &[f32],
267        ys: &[f32],
268        zs: &[f32],
269        flags: Scatter3DFlags,
270        layout: Plot3DDataLayout,
271    ) {
272        let label = label.as_ref();
273        self.with_bound_context(|| {
274            if xs.len() != ys.len() || ys.len() != zs.len() {
275                return;
276            }
277            let Some(count) = layout.sample_count(xs.len()) else {
278                return;
279            };
280            if label.contains('\0') {
281                return;
282            }
283            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
284                let spec = self.next_item.spec(flags.bits(), layout);
285                sys::ImPlot3D_PlotScatter_FloatPtr(
286                    label_ptr,
287                    xs.as_ptr(),
288                    ys.as_ptr(),
289                    zs.as_ptr(),
290                    count,
291                    spec,
292                );
293            })
294        })
295    }
296
297    /// Convenience: plot a 3D scatter (f64)
298    pub fn plot_scatter_f64<S: AsRef<str>>(
299        &self,
300        label: S,
301        xs: &[f64],
302        ys: &[f64],
303        zs: &[f64],
304        flags: Scatter3DFlags,
305    ) {
306        let label = label.as_ref();
307        self.with_bound_context(|| {
308            if xs.len() != ys.len() || ys.len() != zs.len() {
309                return;
310            }
311            let Some(count) = len_i32(xs.len()) else {
312                return;
313            };
314            if label.contains('\0') {
315                return;
316            }
317            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
318                let spec = self.next_item.spec(flags.bits(), Plot3DDataLayout::DEFAULT);
319                sys::ImPlot3D_PlotScatter_doublePtr(
320                    label_ptr,
321                    xs.as_ptr(),
322                    ys.as_ptr(),
323                    zs.as_ptr(),
324                    count,
325                    spec,
326                );
327            })
328        })
329    }
330
331    /// Scatter plot (f64) with an explicit data layout.
332    ///
333    /// # Safety
334    ///
335    /// For every submitted index, `layout` must address an initialized, properly aligned `f64`
336    /// within each coordinate allocation. All three allocations must remain alive for this call.
337    pub unsafe fn plot_scatter_f64_raw<S: AsRef<str>>(
338        &self,
339        label: S,
340        xs: &[f64],
341        ys: &[f64],
342        zs: &[f64],
343        flags: Scatter3DFlags,
344        layout: Plot3DDataLayout,
345    ) {
346        let label = label.as_ref();
347        self.with_bound_context(|| {
348            if xs.len() != ys.len() || ys.len() != zs.len() {
349                return;
350            }
351            let Some(count) = layout.sample_count(xs.len()) else {
352                return;
353            };
354            if label.contains('\0') {
355                return;
356            }
357            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
358                let spec = self.next_item.spec(flags.bits(), layout);
359                sys::ImPlot3D_PlotScatter_doublePtr(
360                    label_ptr,
361                    xs.as_ptr(),
362                    ys.as_ptr(),
363                    zs.as_ptr(),
364                    count,
365                    spec,
366                );
367            })
368        })
369    }
370
371    /// Convenience: plot triangles from separate X/Y/Z arrays (count must be multiple of 3)
372    pub fn plot_triangles_f32<S: AsRef<str>>(
373        &self,
374        label: S,
375        xs: &[f32],
376        ys: &[f32],
377        zs: &[f32],
378        flags: Triangle3DFlags,
379    ) {
380        // The default stride addresses one element of each slice per vertex.
381        unsafe { self.plot_triangles_f32_raw(label, xs, ys, zs, flags, Plot3DDataLayout::DEFAULT) }
382    }
383
384    /// Plot f32 triangle coordinates with an explicit data layout.
385    ///
386    /// # Safety
387    ///
388    /// For every submitted index, `layout` must address an initialized, properly aligned `f32`
389    /// within each coordinate allocation. All three allocations must remain alive for this call.
390    pub unsafe fn plot_triangles_f32_raw<S: AsRef<str>>(
391        &self,
392        label: S,
393        xs: &[f32],
394        ys: &[f32],
395        zs: &[f32],
396        flags: Triangle3DFlags,
397        layout: Plot3DDataLayout,
398    ) {
399        let label = label.as_ref();
400        self.with_bound_context(|| {
401            if xs.len() != ys.len() || ys.len() != zs.len() {
402                return;
403            }
404            let Ok(count) = primitive_vertex_count(
405                xs.len(),
406                3,
407                "triangles",
408                layout,
409                !flags.contains(Triangle3DFlags::NO_LINES),
410            ) else {
411                return;
412            };
413            if label.contains('\0') {
414                return;
415            }
416            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
417                let spec = self.next_item.spec(flags.bits(), layout);
418                sys::ImPlot3D_PlotTriangle_FloatPtr(
419                    label_ptr,
420                    xs.as_ptr(),
421                    ys.as_ptr(),
422                    zs.as_ptr(),
423                    count,
424                    spec,
425                );
426            })
427        })
428    }
429
430    /// Convenience: plot quads from separate X/Y/Z arrays (count must be multiple of 4)
431    pub fn plot_quads_f32<S: AsRef<str>>(
432        &self,
433        label: S,
434        xs: &[f32],
435        ys: &[f32],
436        zs: &[f32],
437        flags: Quad3DFlags,
438    ) {
439        // The default stride addresses one element of each slice per vertex.
440        unsafe { self.plot_quads_f32_raw(label, xs, ys, zs, flags, Plot3DDataLayout::DEFAULT) }
441    }
442
443    /// Plot f32 quad coordinates with an explicit data layout.
444    ///
445    /// # Safety
446    ///
447    /// For every submitted index, `layout` must address an initialized, properly aligned `f32`
448    /// within each coordinate allocation. All three allocations must remain alive for this call.
449    pub unsafe fn plot_quads_f32_raw<S: AsRef<str>>(
450        &self,
451        label: S,
452        xs: &[f32],
453        ys: &[f32],
454        zs: &[f32],
455        flags: Quad3DFlags,
456        layout: Plot3DDataLayout,
457    ) {
458        let label = label.as_ref();
459        self.with_bound_context(|| {
460            if xs.len() != ys.len() || ys.len() != zs.len() {
461                return;
462            }
463            let Ok(count) = primitive_vertex_count(
464                xs.len(),
465                4,
466                "quads",
467                layout,
468                !flags.contains(Quad3DFlags::NO_LINES),
469            ) else {
470                return;
471            };
472            if label.contains('\0') {
473                return;
474            }
475            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
476                let spec = self.next_item.spec(flags.bits(), layout);
477                sys::ImPlot3D_PlotQuad_FloatPtr(
478                    label_ptr,
479                    xs.as_ptr(),
480                    ys.as_ptr(),
481                    zs.as_ptr(),
482                    count,
483                    spec,
484                );
485            })
486        })
487    }
488
489    /// Convenience: plot triangles from separate X/Y/Z arrays (f64)
490    pub fn plot_triangles_f64<S: AsRef<str>>(
491        &self,
492        label: S,
493        xs: &[f64],
494        ys: &[f64],
495        zs: &[f64],
496        flags: Triangle3DFlags,
497    ) {
498        // The default stride addresses one element of each slice per vertex.
499        unsafe { self.plot_triangles_f64_raw(label, xs, ys, zs, flags, Plot3DDataLayout::DEFAULT) }
500    }
501
502    /// Plot f64 triangle coordinates with an explicit data layout.
503    ///
504    /// # Safety
505    ///
506    /// For every submitted index, `layout` must address an initialized, properly aligned `f64`
507    /// within each coordinate allocation. All three allocations must remain alive for this call.
508    pub unsafe fn plot_triangles_f64_raw<S: AsRef<str>>(
509        &self,
510        label: S,
511        xs: &[f64],
512        ys: &[f64],
513        zs: &[f64],
514        flags: Triangle3DFlags,
515        layout: Plot3DDataLayout,
516    ) {
517        let label = label.as_ref();
518        self.with_bound_context(|| {
519            if xs.len() != ys.len() || ys.len() != zs.len() {
520                return;
521            }
522            let Ok(count) = primitive_vertex_count(
523                xs.len(),
524                3,
525                "triangles",
526                layout,
527                !flags.contains(Triangle3DFlags::NO_LINES),
528            ) else {
529                return;
530            };
531            if label.contains('\0') {
532                return;
533            }
534            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
535                let spec = self.next_item.spec(flags.bits(), layout);
536                sys::ImPlot3D_PlotTriangle_doublePtr(
537                    label_ptr,
538                    xs.as_ptr(),
539                    ys.as_ptr(),
540                    zs.as_ptr(),
541                    count,
542                    spec,
543                );
544            })
545        })
546    }
547
548    /// Convenience: plot quads from separate X/Y/Z arrays (f64)
549    pub fn plot_quads_f64<S: AsRef<str>>(
550        &self,
551        label: S,
552        xs: &[f64],
553        ys: &[f64],
554        zs: &[f64],
555        flags: Quad3DFlags,
556    ) {
557        // The default stride addresses one element of each slice per vertex.
558        unsafe { self.plot_quads_f64_raw(label, xs, ys, zs, flags, Plot3DDataLayout::DEFAULT) }
559    }
560
561    /// Plot f64 quad coordinates with an explicit data layout.
562    ///
563    /// # Safety
564    ///
565    /// For every submitted index, `layout` must address an initialized, properly aligned `f64`
566    /// within each coordinate allocation. All three allocations must remain alive for this call.
567    pub unsafe fn plot_quads_f64_raw<S: AsRef<str>>(
568        &self,
569        label: S,
570        xs: &[f64],
571        ys: &[f64],
572        zs: &[f64],
573        flags: Quad3DFlags,
574        layout: Plot3DDataLayout,
575    ) {
576        let label = label.as_ref();
577        self.with_bound_context(|| {
578            if xs.len() != ys.len() || ys.len() != zs.len() {
579                return;
580            }
581            let Ok(count) = primitive_vertex_count(
582                xs.len(),
583                4,
584                "quads",
585                layout,
586                !flags.contains(Quad3DFlags::NO_LINES),
587            ) else {
588                return;
589            };
590            if label.contains('\0') {
591                return;
592            }
593            dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
594                let spec = self.next_item.spec(flags.bits(), layout);
595                sys::ImPlot3D_PlotQuad_doublePtr(
596                    label_ptr,
597                    xs.as_ptr(),
598                    ys.as_ptr(),
599                    zs.as_ptr(),
600                    count,
601                    spec,
602                );
603            })
604        })
605    }
606}