Skip to content

Commit

Permalink
Fix plot bounds of empty plots (#4741)
Browse files Browse the repository at this point in the history
We would sometimes hit a `debug_assert` when plots were empty

* Closes rerun-io/rerun#6681
  • Loading branch information
emilk authored Jun 30, 2024
1 parent dc1f032 commit 31716d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ fn cmp_f64(a: f64, b: f64) -> Ordering {

/// Fill in all values between [min, max] which are a multiple of `step_size`
fn fill_marks_between(out: &mut Vec<GridMark>, step_size: f64, (min, max): (f64, f64)) {
debug_assert!(max > min);
debug_assert!(min <= max, "Bad plot bounds: min: {min}, max: {max}");
let first = (min / step_size).ceil() as i64;
let last = (max / step_size).ceil() as i64;

Expand Down
14 changes: 12 additions & 2 deletions crates/egui_plot/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ pub struct PlotTransform {

impl PlotTransform {
pub fn new(frame: Rect, bounds: PlotBounds, x_centered: bool, y_centered: bool) -> Self {
debug_assert!(
0.0 <= frame.width() && 0.0 <= frame.height(),
"Bad plot frame: {frame:?}"
);

// Since the current Y bounds an affect the final X bounds and vice versa, we need to keep
// the original version of the `bounds` before we start modifying it.
let mut new_bounds = bounds;
Expand All @@ -291,7 +296,7 @@ impl PlotTransform {
// axis, and default to +/- 1.0 otherwise.
if !bounds.is_finite_x() {
new_bounds.set_x(&PlotBounds::new_symmetrical(1.0));
} else if bounds.width() == 0.0 {
} else if bounds.width() <= 0.0 {
new_bounds.set_x_center_width(
bounds.center().x,
if bounds.is_valid_y() {
Expand All @@ -304,7 +309,7 @@ impl PlotTransform {

if !bounds.is_finite_y() {
new_bounds.set_y(&PlotBounds::new_symmetrical(1.0));
} else if bounds.height() == 0.0 {
} else if bounds.height() <= 0.0 {
new_bounds.set_y_center_height(
bounds.center().y,
if bounds.is_valid_x() {
Expand All @@ -323,6 +328,11 @@ impl PlotTransform {
new_bounds.make_y_symmetrical();
};

debug_assert!(
new_bounds.is_valid(),
"Bad final plot bounds: {new_bounds:?}"
);

Self {
frame,
bounds: new_bounds,
Expand Down

0 comments on commit 31716d7

Please sign in to comment.