dear_implot/plots/
inf_lines.rs

1//! Infinite lines plot implementation
2
3use super::{Plot, PlotError, safe_cstring};
4use crate::{InfLinesFlags, sys};
5
6/// Builder for infinite lines plots
7pub struct InfLinesPlot<'a> {
8    label: &'a str,
9    positions: &'a [f64],
10    flags: InfLinesFlags,
11    offset: i32,
12    stride: i32,
13}
14
15impl<'a> InfLinesPlot<'a> {
16    /// Create a new infinite lines plot with the given label and positions (vertical by default)
17    pub fn new(label: &'a str, positions: &'a [f64]) -> Self {
18        Self {
19            label,
20            positions,
21            flags: InfLinesFlags::NONE,
22            offset: 0,
23            stride: std::mem::size_of::<f64>() as i32,
24        }
25    }
26
27    /// Make lines horizontal instead of vertical
28    pub fn horizontal(mut self) -> Self {
29        self.flags |= InfLinesFlags::HORIZONTAL;
30        self
31    }
32
33    /// Set data offset for partial plotting
34    pub fn with_offset(mut self, offset: i32) -> Self {
35        self.offset = offset;
36        self
37    }
38
39    /// Set data stride for non-contiguous data
40    pub fn with_stride(mut self, stride: i32) -> Self {
41        self.stride = stride;
42        self
43    }
44
45    /// Validate the plot data
46    pub fn validate(&self) -> Result<(), PlotError> {
47        if self.positions.is_empty() {
48            return Err(PlotError::EmptyData);
49        }
50        Ok(())
51    }
52}
53
54impl<'a> Plot for InfLinesPlot<'a> {
55    fn plot(&self) {
56        if self.validate().is_err() {
57            return;
58        }
59        let label_c = safe_cstring(self.label);
60        unsafe {
61            sys::ImPlot_PlotInfLines_doublePtr(
62                label_c.as_ptr(),
63                self.positions.as_ptr(),
64                self.positions.len() as i32,
65                self.flags.bits() as i32,
66                self.offset,
67                self.stride,
68            );
69        }
70    }
71
72    fn label(&self) -> &str {
73        self.label
74    }
75}
76
77/// Convenience functions for quick inf-lines plotting
78impl<'ui> crate::PlotUi<'ui> {
79    /// Plot vertical infinite lines at given x positions
80    pub fn inf_lines_vertical(&self, label: &str, xs: &[f64]) -> Result<(), PlotError> {
81        let plot = InfLinesPlot::new(label, xs);
82        plot.validate()?;
83        plot.plot();
84        Ok(())
85    }
86
87    /// Plot horizontal infinite lines at given y positions
88    pub fn inf_lines_horizontal(&self, label: &str, ys: &[f64]) -> Result<(), PlotError> {
89        let plot = InfLinesPlot::new(label, ys).horizontal();
90        plot.validate()?;
91        plot.plot();
92        Ok(())
93    }
94}