Modify GUI for better readability, clean up

This commit is contained in:
2025-03-20 15:29:19 +01:00
parent 5d81549ac0
commit fdcb646bd2
7 changed files with 428 additions and 72 deletions

View File

@@ -1,6 +1,6 @@
use petgraph::stable_graph::{StableGraph, NodeIndex};
use petgraph::{Directed};
use egui_graphs::{Graph};
use egui_graphs::{default_edge_transform, default_node_transform, to_graph_custom, DefaultEdgeShape, DefaultNodeShape, Graph};
use egui::Pos2;
use rand::thread_rng;
use rand::Rng;
@@ -9,15 +9,18 @@ use itertools::Itertools;
use geo::{Line, coord, line_intersection::{line_intersection, LineIntersection}};
use egui::Color32;
use crate::layout::CustomEdgeShape;
pub struct MaxflowProblem {
pub g: Graph<(), u64, Directed, u32, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape>,
pub g: StableGraph<(f32, f32), (u64, u64)>,
pub s: NodeIndex,
pub t: NodeIndex,
}
impl MaxflowProblem {
pub fn new(num_nodes: u64, max_capacity: u64) -> Self {
let mut graph: Graph<_, u64> = Graph::new(StableGraph::default());
// Node Type: (x,y) Edge Type: (current_flow, capacity)
let mut graph: StableGraph<(f32, f32), (u64, u64)> = StableGraph::default();
let mut nodes: Vec<NodeIndex> = Vec::new();
let mut rng = thread_rng();
@@ -25,30 +28,27 @@ impl MaxflowProblem {
for _i in 0..num_nodes {
let x: f32 = rng.gen_range(0.0..=400.0);
let y: f32 = rng.gen_range(0.0..=300.0);
let pos = Pos2::new(x,y);
let n = graph.add_node_with_label_and_location((), String::new(), pos);
let n = graph.add_node((x,y));
nodes.push(n);
}
// select source (s) and sink (t)
let s = *nodes.choose(&mut rng).expect("no nodes found");
let t = *nodes.choose(&mut rng).expect("no nodes found");
graph.node_mut(s).expect("node index not found").set_label(String::from("s"));
graph.node_mut(t).expect("node index not found").set_label(String::from("t"));
graph.node_mut(s).expect("node index not found").set_color(Color32::RED);
graph.node_mut(t).expect("node index not found").set_color(Color32::GREEN);
let (s,t) = nodes.choose_multiple(&mut rng, 2).copied().collect_tuple::<(NodeIndex, NodeIndex)>().expect("not enough nodes");
// iterate over all possible edges
let mut possible_edges: Vec<(NodeIndex, NodeIndex, f32)> = Vec::new();
for pair in nodes.clone().into_iter().combinations(2) {
// calculate distance & append tuple (node_a, node_b, distance) to possible_edges
let distance = Self::distance(graph.node(pair[0]).expect("node index not found").location(), graph.node(pair[1]).expect("node index not found").location());
let distance = Self::distance(
*graph.node_weight(pair[0]).expect("node index not found"),
*graph.node_weight(pair[1]).expect("node index not found")
);
//let distance = Self::distance(graph.node(pair[0]).expect("node index not found").location(), graph.node(pair[1]).expect("node index not found").location());
possible_edges.push((pair[0], pair[1], distance));
}
// sort mapping by distance
possible_edges.sort_by(|a, b| a.2.partial_cmp(&b.2).unwrap());
println!("possible_edges len: {}", possible_edges.len());
// insert the edge if possible
let mut inserted_edges: Vec<(NodeIndex, NodeIndex)> = Vec::new();
@@ -57,10 +57,10 @@ impl MaxflowProblem {
let mut intersects = false;
for (node3, node4) in &inserted_edges {
if Self::intersects(
graph.node(node1).expect("node index not found").location(),
graph.node(node2).expect("node index not found").location(),
graph.node(*node3).expect("node index not found").location(),
graph.node(*node4).expect("node index not found").location(),
*graph.node_weight(node1).expect("node index not found"),
*graph.node_weight(node2).expect("node index not found"),
*graph.node_weight(*node3).expect("node index not found"),
*graph.node_weight(*node4).expect("node index not found"),
) {
intersects = true;
break;
@@ -71,29 +71,54 @@ impl MaxflowProblem {
inserted_edges.push((node1, node2));
inserted_edges.push((node2, node1));
let capacity: u64 = rng.gen_range(1..=max_capacity);
graph.add_edge_with_label(node1, node2, capacity, capacity.to_string());
graph.add_edge_with_label(node2, node1, capacity, capacity.to_string());
graph.add_edge(node1, node2, (0, capacity));
graph.add_edge(node2, node1, (0, capacity));
}
}
Self { g: graph, s: s, t: t }
}
pub fn from(g: StableGraph<(f32, f32), (u64, u64)>, s: NodeIndex, t: NodeIndex) -> Self{
Self {g: g, s: s, t: t}
}
// returns the eudclidean distance between two points
fn distance(a: Pos2, b: Pos2) -> f32 {
let delta_x = a.x - b.x;
let delta_y = a.y - b.y;
fn distance(a: (f32, f32), b: (f32, f32)) -> f32 {
let delta_x = a.0 - b.0;
let delta_y = a.1 - b.1;
return f32::sqrt(delta_x.powf(2.) + delta_y.powf(2.));
}
// checks if the lines between a-b and c-d intersect
fn intersects(a: Pos2, b: Pos2, c: Pos2, d: Pos2) -> bool {
let line1 = Line::new(coord! {x: a.x, y: a.y }, coord! {x: b.x, y: b.y } );
let line2 = Line::new(coord! {x: c.x, y: c.y }, coord! {x: d.x, y: d.y } );
fn intersects(a: (f32, f32), b: (f32, f32), c: (f32, f32), d: (f32, f32)) -> bool {
let line1 = Line::new(coord! {x: a.0, y: a.1 }, coord! {x: b.0, y: b.1 } );
let line2 = Line::new(coord! {x: c.0, y: c.1 }, coord! {x: d.0, y: d.1 } );
match line_intersection(line1, line2) {
// only return true for 'proper' intersections which are not at the end of a line
Some(LineIntersection::SinglePoint { is_proper: true, .. }) => true,
_ => false
}
}
pub fn to_gui_graph(&self) -> Graph<(f32, f32), (u64, u64), Directed, u32, DefaultNodeShape, CustomEdgeShape> {
let mut graph = to_graph_custom(&self.g,
|n| {
let (x, y) = *n.payload();
default_node_transform(n);
n.set_location(Pos2::new(x, y));
n.set_label(String::from(""));
},
|e| {
let (flow, capacity): (u64, u64) = *e.payload();
default_edge_transform(e);
e.set_label(format!("{flow}:{capacity}"));
}
);
graph.node_mut(self.s).expect("node index not found").set_label(String::from("s"));
graph.node_mut(self.t).expect("node index not found").set_label(String::from("t"));
graph.node_mut(self.s).expect("node index not found").set_color(Color32::RED);
graph.node_mut(self.t).expect("node index not found").set_color(Color32::GREEN);
graph
}
}