ruviz 0.4.0

High-performance 2D plotting library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Step plot implementations
//!
//! Provides step function visualization for discrete/piecewise-constant data.
//!
//! # Trait-Based API
//!
//! Step plots implement the core plot traits:
//! - [`PlotConfig`] for `StepConfig`
//! - [`PlotCompute`] for `Step` marker struct
//! - [`PlotData`] for `StepData`
//! - [`PlotRender`] for `StepData`

use crate::core::Result;
use crate::plots::traits::{PlotArea, PlotCompute, PlotConfig, PlotData, PlotRender};
use crate::render::skia::SkiaRenderer;
use crate::render::{Color, LineStyle, Theme};

/// Where to place the step
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepWhere {
    /// Step at the left of each point (pre)
    Pre,
    /// Step at the right of each point (post)
    Post,
    /// Step at the middle between points (mid)
    Mid,
}

/// Configuration for step plot
#[derive(Debug, Clone)]
pub struct StepConfig {
    /// Where to place the step transition
    pub where_step: StepWhere,
    /// Line color (None for auto)
    pub color: Option<Color>,
    /// Line width
    pub line_width: f32,
    /// Fill under the step
    pub fill: bool,
    /// Fill alpha
    pub fill_alpha: f32,
    /// Baseline for fill
    pub baseline: f64,
}

impl Default for StepConfig {
    fn default() -> Self {
        Self {
            where_step: StepWhere::Pre,
            color: None,
            line_width: 1.5,
            fill: false,
            fill_alpha: 0.3,
            baseline: 0.0,
        }
    }
}

impl StepConfig {
    /// Create new config
    pub fn new() -> Self {
        Self::default()
    }

    /// Set step position
    pub fn where_step(mut self, where_step: StepWhere) -> Self {
        self.where_step = where_step;
        self
    }

    /// Set line color
    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Set line width
    pub fn line_width(mut self, width: f32) -> Self {
        self.line_width = width.max(0.1);
        self
    }

    /// Enable fill
    pub fn fill(mut self, fill: bool) -> Self {
        self.fill = fill;
        self
    }

    /// Set fill alpha
    pub fn fill_alpha(mut self, alpha: f32) -> Self {
        self.fill_alpha = alpha.clamp(0.0, 1.0);
        self
    }

    /// Set baseline
    pub fn baseline(mut self, baseline: f64) -> Self {
        self.baseline = baseline;
        self
    }
}

// Implement PlotConfig marker trait
impl PlotConfig for StepConfig {}

/// Marker struct for Step plot type
pub struct Step;

/// Compute step line vertices
///
/// # Arguments
/// * `x` - X coordinates
/// * `y` - Y coordinates
/// * `where_step` - Where to place the step
///
/// # Returns
/// Step line vertices as (x, y) pairs
pub fn step_line(x: &[f64], y: &[f64], where_step: StepWhere) -> Vec<(f64, f64)> {
    if x.is_empty() || y.is_empty() {
        return vec![];
    }

    let n = x.len().min(y.len());
    let mut vertices = Vec::with_capacity(n * 2);

    match where_step {
        StepWhere::Pre => {
            // Vertical step before horizontal
            for i in 0..n {
                if i > 0 {
                    vertices.push((x[i], y[i - 1]));
                }
                vertices.push((x[i], y[i]));
            }
        }
        StepWhere::Post => {
            // Horizontal then vertical step
            for i in 0..n {
                vertices.push((x[i], y[i]));
                if i < n - 1 {
                    vertices.push((x[i + 1], y[i]));
                }
            }
        }
        StepWhere::Mid => {
            // Step at midpoint
            for i in 0..n {
                if i > 0 {
                    let mid_x = (x[i - 1] + x[i]) / 2.0;
                    vertices.push((mid_x, y[i - 1]));
                    vertices.push((mid_x, y[i]));
                }
                vertices.push((x[i], y[i]));
            }
        }
    }

    vertices
}

/// Generate step polygon for filled step plot
///
/// # Arguments
/// * `x` - X coordinates
/// * `y` - Y coordinates
/// * `where_step` - Where to place the step
/// * `baseline` - Y value for baseline
///
/// # Returns
/// Polygon vertices as (x, y) pairs
pub fn step_polygon(x: &[f64], y: &[f64], where_step: StepWhere, baseline: f64) -> Vec<(f64, f64)> {
    let line_vertices = step_line(x, y, where_step);
    if line_vertices.is_empty() {
        return vec![];
    }

    let mut polygon = line_vertices;

    // Add baseline to close the polygon
    if let Some(&(last_x, _)) = polygon.last() {
        polygon.push((last_x, baseline));
    }
    if let Some(&(first_x, _)) = polygon.first() {
        polygon.push((first_x, baseline));
    }

    polygon
}

/// Compute step data range
pub fn step_range(x: &[f64], y: &[f64], baseline: f64) -> ((f64, f64), (f64, f64)) {
    if x.is_empty() || y.is_empty() {
        return ((0.0, 1.0), (0.0, 1.0));
    }

    let x_min = x.iter().copied().fold(f64::INFINITY, f64::min);
    let x_max = x.iter().copied().fold(f64::NEG_INFINITY, f64::max);

    let y_min = y
        .iter()
        .copied()
        .fold(f64::INFINITY, f64::min)
        .min(baseline);
    let y_max = y
        .iter()
        .copied()
        .fold(f64::NEG_INFINITY, f64::max)
        .max(baseline);

    ((x_min, x_max), (y_min, y_max))
}

// ============================================================================
// Trait-Based API
// ============================================================================

/// Computed step plot data
#[derive(Debug, Clone)]
pub struct StepData {
    /// Line vertices
    pub line: Vec<(f64, f64)>,
    /// Polygon vertices (if fill enabled)
    pub polygon: Vec<(f64, f64)>,
    /// Original X coordinates
    pub x: Vec<f64>,
    /// Original Y coordinates
    pub y: Vec<f64>,
    /// Data bounds
    pub bounds: ((f64, f64), (f64, f64)),
    /// Configuration used
    pub(crate) config: StepConfig,
}

/// Input for step plot computation
pub struct StepInput<'a> {
    /// X coordinates
    pub x: &'a [f64],
    /// Y coordinates
    pub y: &'a [f64],
}

impl<'a> StepInput<'a> {
    /// Create new step input
    pub fn new(x: &'a [f64], y: &'a [f64]) -> Self {
        Self { x, y }
    }
}

impl PlotCompute for Step {
    type Input<'a> = StepInput<'a>;
    type Config = StepConfig;
    type Output = StepData;

    fn compute(input: Self::Input<'_>, config: &Self::Config) -> Result<Self::Output> {
        if input.x.is_empty() || input.y.is_empty() {
            return Err(crate::core::PlottingError::EmptyDataSet);
        }

        let line = step_line(input.x, input.y, config.where_step);
        let polygon = if config.fill {
            step_polygon(input.x, input.y, config.where_step, config.baseline)
        } else {
            vec![]
        };
        let bounds = step_range(input.x, input.y, config.baseline);

        Ok(StepData {
            line,
            polygon,
            x: input.x.to_vec(),
            y: input.y.to_vec(),
            bounds,
            config: config.clone(),
        })
    }
}

impl PlotData for StepData {
    fn data_bounds(&self) -> ((f64, f64), (f64, f64)) {
        self.bounds
    }

    fn is_empty(&self) -> bool {
        self.line.is_empty()
    }
}

impl PlotRender for StepData {
    fn render(
        &self,
        renderer: &mut SkiaRenderer,
        area: &PlotArea,
        _theme: &Theme,
        color: Color,
    ) -> Result<()> {
        if self.line.is_empty() {
            return Ok(());
        }

        let config = &self.config;
        let line_color = config.color.unwrap_or(color);

        // Draw fill if enabled
        if config.fill && !self.polygon.is_empty() {
            let fill_color = line_color.with_alpha(config.fill_alpha);
            let screen_polygon: Vec<(f32, f32)> = self
                .polygon
                .iter()
                .map(|(x, y)| area.data_to_screen(*x, *y))
                .collect();
            renderer.draw_filled_polygon(&screen_polygon, fill_color)?;
        }

        // Draw step line
        let screen_line: Vec<(f32, f32)> = self
            .line
            .iter()
            .map(|(x, y)| area.data_to_screen(*x, *y))
            .collect();
        renderer.draw_polyline(
            &screen_line,
            line_color,
            config.line_width,
            LineStyle::Solid,
        )?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_step_pre() {
        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 2.0, 1.5];
        let vertices = step_line(&x, &y, StepWhere::Pre);

        // Pre: first point, then (x1, y0), (x1, y1), (x2, y1), (x2, y2)
        assert!(!vertices.is_empty());
        assert_eq!(vertices[0], (0.0, 1.0));
    }

    #[test]
    fn test_step_post() {
        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 2.0, 1.5];
        let vertices = step_line(&x, &y, StepWhere::Post);

        // Post: (x0, y0), (x1, y0), (x1, y1), (x2, y1), (x2, y2)
        assert!(!vertices.is_empty());
        assert_eq!(vertices[0], (0.0, 1.0));
        assert_eq!(vertices[1], (1.0, 1.0));
    }

    #[test]
    fn test_step_mid() {
        let x = vec![0.0, 2.0, 4.0];
        let y = vec![1.0, 2.0, 1.5];
        let vertices = step_line(&x, &y, StepWhere::Mid);

        // Mid: steps at midpoints
        assert!(!vertices.is_empty());
        // Should contain midpoint x=1.0
        assert!(vertices.iter().any(|(xi, _)| (*xi - 1.0).abs() < 1e-10));
    }

    #[test]
    fn test_step_polygon() {
        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 2.0, 1.5];
        let polygon = step_polygon(&x, &y, StepWhere::Post, 0.0);

        // Should be closed polygon including baseline
        assert!(!polygon.is_empty());
        // Last points should be on baseline
        let n = polygon.len();
        assert!((polygon[n - 1].1 - 0.0).abs() < 1e-10);
        assert!((polygon[n - 2].1 - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_step_range() {
        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 3.0, 2.0];
        let ((x_min, x_max), (y_min, y_max)) = step_range(&x, &y, 0.0);

        assert!((x_min - 0.0).abs() < 1e-10);
        assert!((x_max - 2.0).abs() < 1e-10);
        assert!((y_min - 0.0).abs() < 1e-10); // baseline
        assert!((y_max - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_step_empty() {
        let x: Vec<f64> = vec![];
        let y: Vec<f64> = vec![];
        let vertices = step_line(&x, &y, StepWhere::Pre);
        assert!(vertices.is_empty());
    }

    #[test]
    fn test_step_config_implements_plot_config() {
        fn assert_plot_config<T: PlotConfig>() {}
        assert_plot_config::<StepConfig>();
    }

    #[test]
    fn test_step_plot_compute_trait() {
        use crate::plots::traits::PlotCompute;

        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 2.0, 1.5];
        let config = StepConfig::default();
        let input = StepInput::new(&x, &y);
        let result = Step::compute(input, &config);

        assert!(result.is_ok());
        let step_data = result.unwrap();
        assert!(!step_data.line.is_empty());
    }

    #[test]
    fn test_step_plot_compute_with_fill() {
        use crate::plots::traits::PlotCompute;

        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 2.0, 1.5];
        let config = StepConfig::default().fill(true);
        let input = StepInput::new(&x, &y);
        let result = Step::compute(input, &config);

        assert!(result.is_ok());
        let step_data = result.unwrap();
        assert!(!step_data.polygon.is_empty());
    }

    #[test]
    fn test_step_plot_compute_empty() {
        use crate::plots::traits::PlotCompute;

        let x: Vec<f64> = vec![];
        let y: Vec<f64> = vec![];
        let config = StepConfig::default();
        let input = StepInput::new(&x, &y);
        let result = Step::compute(input, &config);

        assert!(result.is_err());
    }

    #[test]
    fn test_step_plot_data_trait() {
        use crate::plots::traits::{PlotCompute, PlotData};

        let x = vec![0.0, 1.0, 2.0];
        let y = vec![1.0, 3.0, 2.0];
        let config = StepConfig::default();
        let input = StepInput::new(&x, &y);
        let step_data = Step::compute(input, &config).unwrap();

        // Test data_bounds
        let ((x_min, x_max), (y_min, y_max)) = step_data.data_bounds();
        assert!((x_min - 0.0).abs() < 1e-10);
        assert!((x_max - 2.0).abs() < 1e-10);
        assert!(y_min <= y_max);

        // Test is_empty
        assert!(!step_data.is_empty());
    }
}