ai-0/utility/src/gui/render.rs

67 lines
2.0 KiB
Rust

use crate::graph::{Edge, EdgesVec, VerticesVec};
use eframe::egui::{Color32, Pos2, Rect, Ui};
use eframe::epaint::{CornerRadius, Stroke};
pub fn render_graph<D>(
ui: &mut Ui,
vertices: &VerticesVec,
vertex_locations: &mut [(f32, f32)],
edges: &EdgesVec<D>,
normalized_intensity: impl Fn(&Edge<D>) -> f64,
) {
let canvas = ui.painter();
let rect = ui.available_size();
let start = ui.cursor();
let rect = Rect::from_min_max(start.min, rect.to_pos2());
canvas.rect_filled(rect, CornerRadius::same(0), Color32::from_rgb(0, 0, 0));
for e in edges.iter() {
let p1 = vertex_locations[e.vertex1_index];
let p2 = vertex_locations[e.vertex2_index];
canvas.line_segment(
[
Pos2::new(
p1.0 * rect.width() + rect.min.x,
p1.1 * rect.height() + rect.min.y,
),
Pos2::new(
p2.0 * rect.width() + rect.min.x,
p2.1 * rect.height() + rect.min.y,
),
],
Stroke::new(1.0, intensity2color(normalized_intensity(e))),
);
}
}
fn intensity2color(intensity: f64) -> Color32 {
let scale = |base: f64, coefficient: u8| -> u8 {
return ((intensity - base) * 5.0 * (coefficient as f64)) as u8;
};
match intensity {
f64::NEG_INFINITY..0.2 => {
return Color32::from_rgb(
50 - scale(0.0, 50),
50 - scale(0.0, 50),
50 + scale(0.0, 155),
);
}
0.2..0.4 => {
return Color32::from_rgb(0, scale(0.2, 255), 255);
}
0.4..0.6 => {
return Color32::from_rgb(0, 255, 255 - scale(0.4, 255));
}
0.6..0.8 => {
return Color32::from_rgb(scale(0.6, 255) as u8, 255, 0);
}
0.8..f64::INFINITY => {
return Color32::from_rgb(255, 255 - scale(0.8, 255), 0);
}
_ => unreachable!(),
}
}