ruviz 0.4.12

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
use super::*;
use crate::core::types::Point2f;

#[derive(Clone, Copy, Debug)]
struct BucketPoint {
    index: usize,
    point: Point2f,
}

#[derive(Clone, Copy, Debug)]
struct ColumnBucket {
    column: i32,
    first: BucketPoint,
    last: BucketPoint,
    min_y: BucketPoint,
    max_y: BucketPoint,
}

pub(super) fn should_reduce_line_series(
    series: &PlotSeries,
    point_count: usize,
    plot_width: f32,
) -> bool {
    if !matches!(series.series_type, SeriesType::Line { .. }) {
        return false;
    }

    if series.marker_style.is_some() || series.x_errors.is_some() || series.y_errors.is_some() {
        return false;
    }

    if series
        .line_style
        .as_ref()
        .is_some_and(|style| !matches!(style, LineStyle::Solid))
    {
        return false;
    }

    point_count > (plot_width.max(1.0).ceil() as usize * 4)
}

pub(super) fn canonicalize_line_points_exact(points: &[Point2f]) -> Option<Vec<Point2f>> {
    if points.len() < 3 || !points.iter().all(is_finite_point) {
        return None;
    }

    let mut canonical = Vec::with_capacity(points.len());
    for point in points.iter().copied() {
        if canonical
            .last()
            .is_some_and(|last: &Point2f| last.x == point.x && last.y == point.y)
        {
            continue;
        }

        while canonical.len() >= 2 {
            let previous = canonical[canonical.len() - 2];
            let current = canonical[canonical.len() - 1];
            if !is_exactly_redundant_line_point(previous, current, point) {
                break;
            }
            canonical.pop();
        }

        canonical.push(point);
    }

    if canonical.len() >= points.len() {
        None
    } else {
        Some(canonical)
    }
}

pub(super) fn reduce_line_points_for_raster(
    points: &[Point2f],
    plot_left: f32,
    plot_width: f32,
) -> Option<Vec<Point2f>> {
    let column_count = plot_width.max(1.0).ceil() as usize;
    if points.len() <= column_count * 4 || !points.iter().all(is_finite_point) {
        return None;
    }

    if !is_monotonic_x(points) {
        return None;
    }

    let max_column = column_count.saturating_sub(1) as i32;
    let mut reduced = Vec::with_capacity(column_count.saturating_mul(4));
    let mut active_bucket: Option<ColumnBucket> = None;

    for (index, point) in points.iter().copied().enumerate() {
        let column = ((point.x - plot_left).floor() as i32).clamp(0, max_column);
        let bucket_point = BucketPoint { index, point };

        match active_bucket.as_mut() {
            Some(bucket) if bucket.column == column => update_bucket(bucket, bucket_point),
            Some(bucket) => {
                flush_bucket(bucket, &mut reduced);
                active_bucket = Some(ColumnBucket {
                    column,
                    first: bucket_point,
                    last: bucket_point,
                    min_y: bucket_point,
                    max_y: bucket_point,
                });
            }
            None => {
                active_bucket = Some(ColumnBucket {
                    column,
                    first: bucket_point,
                    last: bucket_point,
                    min_y: bucket_point,
                    max_y: bucket_point,
                });
            }
        }
    }

    if let Some(bucket) = active_bucket {
        flush_bucket(&bucket, &mut reduced);
    }

    if reduced.len() >= points.len() {
        None
    } else {
        Some(reduced)
    }
}

fn is_finite_point(point: &Point2f) -> bool {
    point.x.is_finite() && point.y.is_finite()
}

fn is_exactly_redundant_line_point(previous: Point2f, current: Point2f, next: Point2f) -> bool {
    let ab_x = current.x - previous.x;
    let ab_y = current.y - previous.y;
    let bc_x = next.x - current.x;
    let bc_y = next.y - current.y;
    if (ab_x == 0.0 && ab_y == 0.0) || (bc_x == 0.0 && bc_y == 0.0) {
        return false;
    }

    let cross = (ab_x as f64) * (bc_y as f64) - (ab_y as f64) * (bc_x as f64);
    if cross != 0.0 {
        return false;
    }

    let dot = (ab_x as f64) * (bc_x as f64) + (ab_y as f64) * (bc_y as f64);
    dot >= 0.0
}

// Points are expected to be in pixel-space after coordinate projection.
// f32::EPSILON is appropriate here; do not reuse this monotonicity check on
// data-space coordinates where adjacent x-values may legitimately differ by
// less than 1e-7.
fn is_monotonic_x(points: &[Point2f]) -> bool {
    if points.len() < 2 {
        return true;
    }

    let mut direction = 0_i8;
    for window in points.windows(2) {
        let delta = window[1].x - window[0].x;
        if delta.abs() <= f32::EPSILON {
            continue;
        }

        let current = if delta.is_sign_positive() { 1 } else { -1 };
        if direction == 0 {
            direction = current;
        } else if direction != current {
            return false;
        }
    }

    true
}

fn update_bucket(bucket: &mut ColumnBucket, point: BucketPoint) {
    bucket.last = point;
    if point.point.y < bucket.min_y.point.y {
        bucket.min_y = point;
    }
    if point.point.y > bucket.max_y.point.y {
        bucket.max_y = point;
    }
}

fn flush_bucket(bucket: &ColumnBucket, output: &mut Vec<Point2f>) {
    let mut candidates = [bucket.first, bucket.min_y, bucket.max_y, bucket.last];
    candidates.sort_by_key(|candidate| candidate.index);

    let mut last_index = None;
    for candidate in candidates {
        if last_index == Some(candidate.index) {
            continue;
        }
        if output
            .last()
            .is_some_and(|last| last.x == candidate.point.x && last.y == candidate.point.y)
        {
            last_index = Some(candidate.index);
            continue;
        }
        output.push(candidate.point);
        last_index = Some(candidate.index);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::render::{Color, LineStyle, SkiaRenderer, Theme};

    fn render_polyline(points: &[Point2f], clip_rect: (f32, f32, f32, f32)) -> Image {
        let width = 680;
        let height = 220;
        let mut renderer = SkiaRenderer::new(width, height, Theme::default()).expect("renderer");
        renderer.clear();
        renderer
            .draw_polyline_points_clipped(points, Color::BLACK, 2.0, LineStyle::Solid, clip_rect)
            .expect("polyline render");
        renderer.into_image()
    }

    fn mean_normalized_channel_diff(lhs: &Image, rhs: &Image) -> f64 {
        assert_eq!(lhs.width, rhs.width);
        assert_eq!(lhs.height, rhs.height);

        lhs.pixels
            .iter()
            .zip(&rhs.pixels)
            .map(|(left, right)| (*left as f64 - *right as f64).abs() / 255.0)
            .sum::<f64>()
            / lhs.pixels.len() as f64
    }

    fn image_has_dark_pixel_near(image: &Image, x: u32, y: u32, radius: u32) -> bool {
        let x_start = x.saturating_sub(radius);
        let x_end = (x + radius).min(image.width.saturating_sub(1));
        let y_start = y.saturating_sub(radius);
        let y_end = (y + radius).min(image.height.saturating_sub(1));

        for sample_y in y_start..=y_end {
            for sample_x in x_start..=x_end {
                let idx = ((sample_y * image.width + sample_x) * 4) as usize;
                let pixel = &image.pixels[idx..idx + 4];
                if pixel[3] > 0 && pixel[0] < 80 && pixel[1] < 80 && pixel[2] < 80 {
                    return true;
                }
            }
        }

        false
    }

    #[test]
    fn test_reduce_line_points_preserves_monotonic_envelope() {
        let points = vec![
            Point2f::new(0.1, 10.0),
            Point2f::new(0.2, 2.0),
            Point2f::new(0.3, 8.0),
            Point2f::new(0.4, 4.0),
            Point2f::new(1.2, 5.0),
            Point2f::new(1.3, 1.0),
            Point2f::new(1.4, 9.0),
            Point2f::new(1.5, 6.0),
            Point2f::new(2.2, 3.0),
            Point2f::new(2.3, 7.0),
            Point2f::new(2.4, 2.0),
            Point2f::new(2.5, 8.0),
        ];

        let reduced = reduce_line_points_for_raster(&points, 0.0, 2.0).expect("expected reduction");

        assert!(reduced.len() < points.len());
        assert_eq!(reduced.first().copied(), points.first().copied());
        assert_eq!(reduced.last().copied(), points.last().copied());
        assert!(
            reduced
                .iter()
                .any(|point| (point.y - 1.0).abs() < f32::EPSILON)
        );
        assert!(
            reduced
                .iter()
                .any(|point| (point.y - 9.0).abs() < f32::EPSILON)
        );
    }

    #[test]
    fn test_reduce_line_points_rejects_non_monotonic_x() {
        let points = vec![
            Point2f::new(0.0, 0.0),
            Point2f::new(1.0, 1.0),
            Point2f::new(0.5, 2.0),
            Point2f::new(1.5, 3.0),
            Point2f::new(2.0, 4.0),
            Point2f::new(2.5, 5.0),
            Point2f::new(3.0, 6.0),
            Point2f::new(3.5, 7.0),
            Point2f::new(4.0, 8.0),
        ];

        assert!(reduce_line_points_for_raster(&points, 0.0, 1.0).is_none());
    }

    #[test]
    fn test_exact_line_canonicalization_removes_duplicate_and_collinear_points() {
        let points = vec![
            Point2f::new(0.0, 0.0),
            Point2f::new(1.0, 1.0),
            Point2f::new(2.0, 2.0),
            Point2f::new(2.0, 2.0),
            Point2f::new(3.0, 3.0),
            Point2f::new(3.0, 4.0),
        ];

        let canonical = canonicalize_line_points_exact(&points).expect("expected canonicalization");

        assert_eq!(
            canonical,
            vec![
                Point2f::new(0.0, 0.0),
                Point2f::new(3.0, 3.0),
                Point2f::new(3.0, 4.0),
            ]
        );
    }

    #[test]
    fn test_exact_line_canonicalization_preserves_turns() {
        let points = vec![
            Point2f::new(0.0, 0.0),
            Point2f::new(1.0, 1.0),
            Point2f::new(1.0, 1.0),
            Point2f::new(2.0, 1.0),
            Point2f::new(3.0, 1.0),
        ];

        let canonical = canonicalize_line_points_exact(&points).expect("expected dedupe");

        assert_eq!(
            canonical,
            vec![
                Point2f::new(0.0, 0.0),
                Point2f::new(1.0, 1.0),
                Point2f::new(3.0, 1.0),
            ]
        );
    }

    #[test]
    fn test_reduce_line_points_matches_original_raster_with_small_deviation() {
        let plot_left = 20.0;
        let plot_top = 20.0;
        let plot_width = 640.0;
        let plot_height = 180.0;
        let samples_per_column = 10usize;
        let clip_rect = (plot_left, plot_top, plot_width, plot_height);
        let mut points = Vec::with_capacity(plot_width as usize * samples_per_column);

        for column in 0..plot_width as usize {
            for sample in 0..samples_per_column {
                let phase = (column * samples_per_column + sample) as f32;
                let x = plot_left + column as f32 + sample as f32 / samples_per_column as f32;
                let mut y =
                    plot_top + plot_height * 0.52 + phase.sin() * 18.0 + (phase / 7.0).cos() * 11.0;

                if column % 61 == 17 && sample == samples_per_column / 2 {
                    y = plot_top + 8.0;
                } else if column % 79 == 41 && sample == samples_per_column / 3 {
                    y = plot_top + plot_height - 8.0;
                }

                y = y.clamp(plot_top + 2.0, plot_top + plot_height - 2.0);
                points.push(Point2f::new(x, y));
            }
        }

        let reduced = reduce_line_points_for_raster(&points, plot_left, plot_width)
            .expect("expected point reduction for dense monotonic line");

        let original_image = render_polyline(&points, clip_rect);
        let reduced_image = render_polyline(&reduced, clip_rect);
        let diff = mean_normalized_channel_diff(&original_image, &reduced_image);

        assert!(
            diff <= 0.01,
            "reduced line render deviated too much from original: {diff:.6}"
        );
        assert!(
            image_has_dark_pixel_near(
                &reduced_image,
                (plot_left + 17.0).round() as u32,
                (plot_top + 8.0).round() as u32,
                4,
            ),
            "reduced line should preserve top spike detail"
        );
        assert!(
            image_has_dark_pixel_near(
                &reduced_image,
                (plot_left + 41.0).round() as u32,
                (plot_top + plot_height - 8.0).round() as u32,
                4,
            ),
            "reduced line should preserve bottom spike detail"
        );
    }
}