From 74891cac2f45f844a41dee17f184f701d1f33ca6 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Tue, 20 Feb 2024 16:26:47 +0100 Subject: [PATCH] egui_plot: fix panic when the base step size is set to 0 (#4078) This can happen e.g. when the user forces some axis range to (0.0, 0.0) like in rerun-io/rerun#5239 --- crates/egui_plot/src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 692399e8bd3..4238c7b3258 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -1391,6 +1391,11 @@ pub struct GridMark { pub fn log_grid_spacer(log_base: i64) -> GridSpacer { let log_base = log_base as f64; let step_sizes = move |input: GridInput| -> Vec { + // handle degenerate cases + if input.base_step_size.abs() < f64::EPSILON { + return Vec::new(); + } + // The distance between two of the thinnest grid lines is "rounded" up // to the next-bigger power of base let smallest_visible_unit = next_power(input.base_step_size, log_base); @@ -1693,7 +1698,7 @@ impl PreparedPlot { /// assert_eq!(next_power(0.2, 10.0), 1); /// ``` fn next_power(value: f64, base: f64) -> f64 { - assert_ne!(value, 0.0); // can be negative (typical for Y axis) + debug_assert_ne!(value, 0.0); // can be negative (typical for Y axis) base.powi(value.abs().log(base).ceil() as i32) } @@ -1708,7 +1713,7 @@ fn generate_marks(step_sizes: [f64; 3], bounds: (f64, f64)) -> Vec { /// Fill in all values between [min, max] which are a multiple of `step_size` fn fill_marks_between(out: &mut Vec, step_size: f64, (min, max): (f64, f64)) { - assert!(max > min); + debug_assert!(max > min); let first = (min / step_size).ceil() as i64; let last = (max / step_size).ceil() as i64;