Skip to content

Commit

Permalink
incremental layout drawing + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed Oct 29, 2024
1 parent 89c1cad commit c23574d
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 239 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/animated_nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<N: Clone + IsClockwise> From<NodeProps<N>> for NodeShapeAnimated {
fn from(node_props: NodeProps<N>) -> Self {
Self {
label: node_props.label.clone(),
loc: node_props.location(),
loc: node_props.location,
dragged: node_props.dragged,
clockwise: node_props.payload.get_is_clockwise(),

Expand Down Expand Up @@ -145,7 +145,7 @@ impl<N: Clone + IsClockwise, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location();
self.loc = state.location;
self.dragged = state.dragged;
self.clockwise = state.payload.get_is_clockwise();
}
Expand Down
26 changes: 16 additions & 10 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use drawers::ValuesSectionDebug;
use eframe::{run_native, App, CreationContext};
use egui::{CollapsingHeader, Context, Pos2, ScrollArea, Ui, Vec2};
use egui_graphs::events::Event;
use egui_graphs::{random_graph, DefaultGraphView, Edge, Graph, Node};
use egui_graphs::{random_graph, to_graph, DefaultGraphView, Edge, Graph, Node};
use fdg::fruchterman_reingold::{FruchtermanReingold, FruchtermanReingoldConfiguration};
use fdg::nalgebra::{Const, OPoint};
use fdg::{Force, ForceGraph};
Expand Down Expand Up @@ -50,12 +50,15 @@ impl DemoApp {
let settings_graph = settings::SettingsGraph::default();
let settings_simulation = settings::SettingsSimulation::default();

let mut g = random_graph(settings_graph.count_node, settings_graph.count_edge);
let mut g = to_graph(&random_graph(
settings_graph.count_node,
settings_graph.count_edge,
));

let mut force = init_force(&settings_simulation);
let mut sim = fdg::init_force_graph_uniform(g.g.clone(), 1.0);
let mut sim = fdg::init_force_graph_uniform(g.graph().clone(), 1.0);
force.apply(&mut sim);
g.g.node_weights_mut().for_each(|node| {
g.nodes_mut().for_each(|node| {
let point: fdg::nalgebra::OPoint<f32, fdg::nalgebra::Const<2>> =
sim.node_weight(node.id()).unwrap().1;
node.set_location(Pos2::new(point.coords.x, point.coords.y));
Expand Down Expand Up @@ -102,7 +105,7 @@ impl DemoApp {

/// sync locations computed by the simulation with egui_graphs::Graph nodes.
fn sync(&mut self) {
self.g.g.node_weights_mut().for_each(|node| {
self.g.nodes_mut().for_each(|node| {
let sim_computed_point: OPoint<f32, Const<2>> =
self.sim.node_weight(node.id()).unwrap().1;
node.set_location(Pos2::new(
Expand Down Expand Up @@ -151,7 +154,7 @@ impl DemoApp {
}

let random_n_idx = rand::thread_rng().gen_range(0..nodes_cnt);
self.g.g.node_indices().nth(random_n_idx)
self.g.node_indices().nth(random_n_idx)
}

fn random_edge_idx(&self) -> Option<EdgeIndex> {
Expand All @@ -161,7 +164,7 @@ impl DemoApp {
}

let random_e_idx = rand::thread_rng().gen_range(0..edges_cnt);
self.g.g.edge_indices().nth(random_e_idx)
self.g.edge_indices().nth(random_e_idx)
}

fn remove_random_node(&mut self) {
Expand Down Expand Up @@ -447,12 +450,15 @@ impl DemoApp {
let settings_graph = settings::SettingsGraph::default();
let settings_simulation = settings::SettingsSimulation::default();

let mut g = random_graph(settings_graph.count_node, settings_graph.count_edge);
let mut g = to_graph(&random_graph(
settings_graph.count_node,
settings_graph.count_edge,
));

let mut force = init_force(&self.settings_simulation);
let mut sim = fdg::init_force_graph_uniform(g.g.clone(), 1.0);
let mut sim = fdg::init_force_graph_uniform(g.graph().clone(), 1.0);
force.apply(&mut sim);
g.g.node_weights_mut().for_each(|node| {
g.nodes_mut().for_each(|node| {
let point: fdg::nalgebra::OPoint<f32, fdg::nalgebra::Const<2>> =
sim.node_weight(node.id()).unwrap().1;
node.set_location(Pos2::new(point.coords.x, point.coords.y));
Expand Down
4 changes: 2 additions & 2 deletions examples/flex_nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<N: Clone> From<NodeProps<N>> for NodeShapeFlex {
fn from(node_props: NodeProps<N>) -> Self {
Self {
label: node_props.label.clone(),
loc: node_props.location(),
loc: node_props.location,

size_x: 0.,
size_y: 0.,
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location();
self.loc = state.location;
}
}

Expand Down
1 change: 1 addition & 0 deletions examples/layouts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ egui_graphs = { path = "../../" }
egui = "0.29"
eframe = "0.29"
petgraph = "0.6"
rand = "0.8.5"
130 changes: 72 additions & 58 deletions examples/layouts/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use eframe::{run_native, App, CreationContext, NativeOptions};
use egui::Context;
use egui_graphs::{
random_graph, DefaultEdgeShape, DefaultNodeShape, Graph, GraphView, LayoutHierarchical,
LayoutRandom, LayoutStateHierarchical, LayoutStateRandom,
random_graph, to_graph, DefaultEdgeShape, DefaultNodeShape, Graph, GraphView,
LayoutHierarchical, LayoutRandom, LayoutStateHierarchical, LayoutStateRandom,
};
use petgraph::{stable_graph::DefaultIx, Directed};
use rand::Rng;

#[derive(Clone, PartialEq)]
enum Layout {
Expand All @@ -21,6 +22,7 @@ struct Settings {
pub struct LayoutsApp {
settings: Settings,
g: Graph,
reset_cache: bool,
}

impl LayoutsApp {
Expand All @@ -30,40 +32,13 @@ impl LayoutsApp {
num_nodes: 25,
num_edges: 25,
};
let g = to_graph(&random_graph(settings.num_nodes, settings.num_edges));
Self {
g,
settings: settings.clone(),
g: random_graph(settings.num_nodes, settings.num_edges),
reset_cache: false,
}
}

fn clear_cache(&mut self, ui: &mut egui::Ui) {
match self.settings.layout {
Layout::Hierarchical => {
GraphView::<
(),
(),
Directed,
DefaultIx,
DefaultNodeShape,
DefaultEdgeShape,
LayoutStateHierarchical,
LayoutHierarchical,
>::clear_cache(ui);
}
Layout::Random => {
GraphView::<
(),
(),
Directed,
DefaultIx,
DefaultNodeShape,
DefaultEdgeShape,
LayoutStateRandom,
LayoutRandom,
>::clear_cache(ui);
}
};
}
}

impl App for LayoutsApp {
Expand All @@ -82,41 +57,76 @@ impl App for LayoutsApp {
)
.changed()
{
self.clear_cache(ui);
self.reset_cache = true;
};
if ui
.radio_value(&mut self.settings.layout, Layout::Random, "Random")
.changed()
{
self.clear_cache(ui);
self.reset_cache = true;
};
});
ui.horizontal(|ui| {
ui.label("Number of nodes");
if ui
.add(egui::Slider::new(&mut self.settings.num_nodes, 1..=250))
.changed()
{
self.clear_cache(ui);
self.g = random_graph(self.settings.num_nodes, self.settings.num_edges);
let mut value = self.settings.num_nodes;
if ui.add(egui::Slider::new(&mut value, 1..=250)).changed() {
let delta = value as isize - self.settings.num_nodes as isize;
if delta > 0 {
for _ in 0..delta {
self.g.add_node(());
}
} else {
for _ in 0..-delta {
let idx = self.g.node_indices().last().unwrap();
self.g.remove_node(idx);
}
}

self.settings.num_nodes = value;
self.settings.num_edges = self.g.edge_count();
};
});
ui.horizontal(|ui| {
ui.label("Number of edges");
if ui
.add(egui::Slider::new(&mut self.settings.num_edges, 1..=250))
.changed()
{
self.clear_cache(ui);
self.g = random_graph(self.settings.num_nodes, self.settings.num_edges);
let mut value = self.settings.num_edges;
if ui.add(egui::Slider::new(&mut value, 1..=250)).changed() {
let delta = value as isize - self.settings.num_edges as isize;
if delta > 0 {
for _ in 0..delta {
let mut rng = rand::thread_rng();
let start = self
.g
.node_indices()
.nth(rng.gen_range(0..self.g.node_count()))
.unwrap();
let end = self
.g
.node_indices()
.nth(rng.gen_range(0..self.g.node_count()))
.unwrap();
self.g.add_edge(start, end, ());
}
} else {
for _ in 0..-delta {
let idx = self.g.edge_indices().last().unwrap();
self.g.remove_edge(idx);
}
}

self.settings.num_edges = value;
};
});
ui.horizontal(|ui| {
if ui.button("redraw").changed() {
self.reset_cache = true;
};
});
});
});
egui::CentralPanel::default().show(ctx, |ui| {
match self.settings.layout {
Layout::Hierarchical => {
ui.add(&mut GraphView::<
let w = &mut GraphView::<
_,
_,
_,
Expand All @@ -125,19 +135,23 @@ impl App for LayoutsApp {
_,
LayoutStateHierarchical,
LayoutHierarchical,
>::new(&mut self.g));
>::new(&mut self.g);
if self.reset_cache {
w.clear_cache(ui);
self.reset_cache = false;
}
ui.add(w);
}
Layout::Random => {
ui.add(&mut GraphView::<
_,
_,
_,
_,
_,
_,
LayoutStateRandom,
LayoutRandom,
>::new(&mut self.g));
let w =
&mut GraphView::<_, _, _, _, _, _, LayoutStateRandom, LayoutRandom>::new(
&mut self.g,
);
if self.reset_cache {
w.clear_cache(ui);
self.reset_cache = false;
}
ui.add(w);
}
};
});
Expand Down
4 changes: 2 additions & 2 deletions examples/wasm_custom_draw/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<N: Clone> From<NodeProps<N>> for NodeShapeAnimated {
fn from(node_props: NodeProps<N>) -> Self {
Self {
label: node_props.label.clone(),
loc: node_props.location(),
loc: node_props.location,
dragged: node_props.dragged,

angle_rad: Default::default(),
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location();
self.loc = state.location;
self.dragged = state.dragged;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/draw/displays_default/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct DefaultNodeShape {
impl<N: Clone> From<NodeProps<N>> for DefaultNodeShape {
fn from(node_props: NodeProps<N>) -> Self {
DefaultNodeShape {
pos: node_props.location(),
pos: node_props.location,
selected: node_props.selected,
dragged: node_props.dragged,
label_text: node_props.label.to_string(),
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>
}

fn update(&mut self, state: &NodeProps<N>) {
self.pos = state.location();
self.pos = state.location;
self.selected = state.selected;
self.dragged = state.dragged;
self.label_text = state.label.to_string();
Expand Down
2 changes: 0 additions & 2 deletions src/draw/drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ where

fn draw_nodes(&mut self) {
self.g
.g
.node_indices()
.collect::<Vec<_>>()
.into_iter()
Expand All @@ -99,7 +98,6 @@ where

fn draw_edges(&mut self) {
self.g
.g
.edge_indices()
.collect::<Vec<_>>()
.into_iter()
Expand Down
Loading

0 comments on commit c23574d

Please sign in to comment.