Skip to content

Commit

Permalink
Apply clippy hints and fix some columns/row mixup
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesProgrammiert committed Nov 6, 2022
1 parent fe9d67d commit b83d623
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
102 changes: 54 additions & 48 deletions crates/egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ops::RangeInclusive;

use epaint::util::FloatOrd;
use epaint::{Mesh, RectShape};
use epaint::Mesh;

use crate::*;

Expand Down Expand Up @@ -1561,14 +1561,19 @@ impl PlotItem for BoxPlot {
#[derive(Debug)]
pub enum HeatmapErr {
ZeroColumns,
ZeroRows,
BadLength,
}

impl std::fmt::Display for HeatmapErr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ZeroColumns => write!(f, "number of columns must not be zero"),
Self::BadLength => write!(f, "length of value vector is not divisible by number of columns")
Self::ZeroRows => write!(f, "number of rows must not be zero"),
Self::BadLength => write!(
f,
"length of value vector is not divisible by number of columns"
),
}
}
}
Expand Down Expand Up @@ -1601,8 +1606,6 @@ pub struct Heatmap<const RESOLUTION: usize> {
palette: [Color32; RESOLUTION],
/// is widget is highlighted
highlight: bool,
/// Line with and color
stroke: Stroke,
/// plot name
name: String,
/// Size of one tile in plot coordinates
Expand All @@ -1627,12 +1630,15 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
/// Returns error type if provided parameters are inconsistent
pub fn new(values: Vec<f64>, cols: usize) -> Result<Self, HeatmapErr> {
if cols == 0 {
return Err(HeatmapErr::ZeroColumns)
return Err(HeatmapErr::ZeroColumns);
}
if (values.len() % cols) != 0 {
return Err(HeatmapErr::BadLength)
return Err(HeatmapErr::BadLength);
}
let rows = values.len() / cols;
if rows == 0 {
return Err(HeatmapErr::ZeroRows);
}

// determine range
let mut min = f64::MAX;
Expand All @@ -1651,25 +1657,22 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
Color32::BLACK,
Color32::DARK_RED,
Color32::YELLOW,
Color32::BLACK
Color32::BLACK,
];
Ok(Self {
pos: PlotPoint {x: 0.0, y: 0.0},
pos: PlotPoint { x: 0.0, y: 0.0 },
values,
cols,
rows,
min,
max,
formatter: Box::new(|v| {
format!("{:.1}", v)
}),
formatter: Box::new(|v| format!("{:.1}", v)),
custom_mapping: None,
show_labels: true,
palette: Heatmap::linear_gradient_from_base_colors(base_colors),
highlight: false,
stroke: Stroke::none(),
name: String::new(),
tile_size: Vec2 {x: 1.0, y: 1.0},
tile_size: Vec2 { x: 1.0, y: 1.0 },
})
}

Expand All @@ -1681,21 +1684,22 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {

/// Interpolate linear gradient with `RESOLUTION` steps from an arbitrary number of base colors.
fn linear_gradient_from_base_colors(base_colors: Vec<Color32>) -> [Color32; RESOLUTION] {
let mut ret = [Color32::TRANSPARENT; RESOLUTION];
let mut interpolated = [Color32::TRANSPARENT; RESOLUTION];
if base_colors.is_empty() {
return ret;
return interpolated;
}
if base_colors.len() == 1 { // single color, no gradient
ret = [base_colors[0]; RESOLUTION];
return ret;
if base_colors.len() == 1 {
// single color, no gradient
interpolated = [base_colors[0]; RESOLUTION];
return interpolated;
}
for i in 0..RESOLUTION {
let i_rel: f64 = i as f64 / (RESOLUTION-1) as f64;
if i_rel == 1.0 { // last element
ret[i] = *base_colors.last().unwrap();
}
else {
let base_index_float: f64 = i_rel * (base_colors.len()-1) as f64;
for (i, color) in interpolated.iter_mut().enumerate() {
let i_rel: f64 = i as f64 / (RESOLUTION - 1) as f64;
if i_rel == 1.0 {
// last element
*color = *base_colors.last().unwrap();
} else {
let base_index_float: f64 = i_rel * (base_colors.len() - 1) as f64;
let base_index: usize = base_index_float as usize;
let start_color = base_colors[base_index];
let end_color = base_colors[base_index + 1];
Expand All @@ -1709,10 +1713,10 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
let r = (start_color.r() as f64 + delta_r).round() as u8;
let g = (start_color.g() as f64 + delta_g).round() as u8;
let b = (start_color.b() as f64 + delta_b).round() as u8;
ret[i] = Color32::from_rgb(r, g, b);
*color = Color32::from_rgb(r, g, b);
}
}
return ret;
interpolated
}

/// Specify custom range of values to map onto color palette.
Expand Down Expand Up @@ -1772,7 +1776,7 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
pub fn size(mut self, x: f32, y: f32) -> Self {
self.tile_size = Vec2 {
x: x / self.cols as f32,
y: y / self.rows as f32
y: y / self.rows as f32,
};
self
}
Expand All @@ -1799,20 +1803,24 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
shapes.extend(labels);
}
}
fn tile_view_info(&self, ui: &Ui, transform: &ScreenTransform, index: usize) -> (Rect, Color32, Shape) {
fn tile_view_info(
&self,
ui: &Ui,
transform: &ScreenTransform,
index: usize,
) -> (Rect, Color32, Shape) {
let v = self.values[index];

// calculate color value
let mut fill_color: Color32;
if let Some(mapping) = &self.custom_mapping {
fill_color = mapping(v);
}
else {
} else {
// convert to value in [0.0, 1.0]
let v_rel = (v - self.min) / (self.max - self.min);

// convert to color palette index
let palette_index = (v_rel * (self.palette.len()-1) as f64).round() as usize;
let palette_index = (v_rel * (self.palette.len() - 1) as f64).round() as usize;

fill_color = self.palette[palette_index];
}
Expand All @@ -1829,12 +1837,12 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
let tile_rect: Rect = transform.rect_from_values(
&PlotPoint {
x: self.pos.x + self.tile_size.x as f64 * x as f64,
y: self.pos.y + self.tile_size.y as f64 * y as f64
y: self.pos.y + self.tile_size.y as f64 * y as f64,
},
&PlotPoint {
x: self.pos.x + self.tile_size.x as f64 * (x + 1) as f64,
y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64
}
y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64,
},
);
// Text

Expand All @@ -1847,8 +1855,7 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {

let inverted_color = if luminance < 140.0 {
Color32::WHITE
}
else {
} else {
Color32::BLACK
};

Expand All @@ -1857,7 +1864,7 @@ impl<const RESOLUTION: usize> Heatmap<RESOLUTION> {
let text_pos = tile_rect.center() - galley.size() / 2.0;

let text = Shape::galley(text_pos, galley.galley().clone());
return (tile_rect, fill_color, text);
(tile_rect, fill_color, text)
}
}

Expand Down Expand Up @@ -1893,32 +1900,31 @@ impl<const RESOLUTION: usize> PlotItem for Heatmap<RESOLUTION> {

fn bounds(&self) -> PlotBounds {
PlotBounds {
min: [
self.pos.x, self.pos.y,
],
min: [self.pos.x, self.pos.y],
max: [
self.pos.x + self.tile_size.x as f64 * self.cols as f64,
self.pos.y + self.tile_size.y as f64 * self.rows as f64,
]
],
}
}

fn find_closest(&self, point: Pos2, transform: &ScreenTransform) -> Option<ClosestElem> {
self.values.clone() // FIXME: is there a better solution that cloning?
self.values
.clone() // FIXME: is there a better solution that cloning?
.into_iter()
.enumerate()
.map(|(index, _)| {
let x = index % self.rows;
let y = index / self.rows;
let x = index % self.cols;
let y = index / self.cols;
let tile_rect: Rect = transform.rect_from_values(
&PlotPoint {
x: self.pos.x + self.tile_size.x as f64 * x as f64,
y: self.pos.y + self.tile_size.y as f64 * y as f64
y: self.pos.y + self.tile_size.y as f64 * y as f64,
},
&PlotPoint {
x: self.pos.x + self.tile_size.x as f64 * (x + 1) as f64,
y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64
}
y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64,
},
);
let dist_sq = tile_rect.distance_sq_to_pos(point);

Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use legend::LegendWidget;
use transform::ScreenTransform;

pub use items::{
Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, Heatmap, HLine, Line, LineStyle,
Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, HLine, Heatmap, Line, LineStyle,
MarkerShape, Orientation, PlotImage, PlotPoint, PlotPoints, Points, Polygon, Text, VLine,
};
pub use legend::{Corner, Legend};
Expand Down Expand Up @@ -1193,7 +1193,7 @@ impl PlotUi {
}

/// Add a heatmap.
pub fn heatmap<const RESOLUTION: usize>(&mut self, mut heatmap: Heatmap<RESOLUTION>) {
pub fn heatmap<const RESOLUTION: usize>(&mut self, heatmap: Heatmap<RESOLUTION>) {
if heatmap.values.is_empty() {
return;
}
Expand Down

0 comments on commit b83d623

Please sign in to comment.