crisol-plot 1.10.0

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
Documentation
//! Decimation adapters: the math lives in `crisol-plot-core`; this file converts the
//! egui screen types.

use egui::{Pos2, Rect};

use crisol_plot_core::decimate as core;
pub(super) use crisol_plot_core::decimate::{LINE_SPACING, MARKER_SPACING, SIMPLIFY_TOLERANCE};
use crisol_plot_core::transform::{ScreenPoint, ScreenRect};

use super::axis::Transform;

/// Ramer–Douglas–Peucker on screen positions, in egui coordinates.
pub(super) fn simplify(points: Vec<Pos2>, tolerance: f32) -> Vec<Pos2> {
    core::simplify(points.into_iter().map(screen_point).collect(), tolerance)
        .into_iter()
        .map(pos2)
        .collect()
}

/// One marker per screen cell, keeping the first index that lands in it.
pub(super) fn one_per_cell(transform: &Transform, data: &[[f64; 2]], cell: f32) -> Vec<usize> {
    core::one_per_cell(&core_transform(*transform), data, cell)
}

/// Every point at least `spacing` points from the last kept one, plus the last.
pub(super) fn thin(transform: &Transform, data: &[[f64; 2]], spacing: f32) -> Vec<usize> {
    core::thin(&core_transform(*transform), data, spacing)
}

fn core_transform(transform: Transform) -> crisol_plot_core::transform::Transform {
    crisol_plot_core::transform::Transform {
        frame: screen_rect(transform.frame),
        view: transform.view,
        x_scale: transform.x_scale,
        y_scale: transform.y_scale,
        y2: transform.y2.map(|axis| crisol_plot_core::transform::SecondAxis {
            min: axis.min,
            max: axis.max,
            scale: axis.scale,
        }),
    }
}

fn screen_point(point: Pos2) -> ScreenPoint {
    ScreenPoint::new(point.x, point.y)
}

fn pos2(point: ScreenPoint) -> Pos2 {
    Pos2::new(point.x, point.y)
}

fn screen_rect(rect: Rect) -> ScreenRect {
    ScreenRect::from_min_max(screen_point(rect.min), screen_point(rect.max))
}