Skip to content

Commit

Permalink
Abstracted graph backend (#25)
Browse files Browse the repository at this point in the history
* Abstracted graph backend

* changed tests names
  • Loading branch information
blitzarx1 authored Apr 30, 2023
1 parent 966fcac commit 08b1271
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 214 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_graphs"
version = "0.1.3"
version = "0.1.4"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
homepage = "https://github.com/blitzarx1/egui_graphs"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basic"
version = "0.1.3"
version = "0.1.4"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/interactive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "interactive"
version = "0.1.3"
version = "0.1.4"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"
Expand Down
40 changes: 18 additions & 22 deletions examples/interactive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl InteractiveApp {
self.selected_edges = Default::default();

self.elements
.get_nodes_mut()
.nodes_mut()
.iter_mut()
.for_each(|(idx, node)| {
let sim_node = self
Expand All @@ -100,7 +100,7 @@ impl InteractiveApp {
};
});

self.elements.get_edges().iter().for_each(|(_, edges)| {
self.elements.edges().iter().for_each(|(_, edges)| {
edges.iter().for_each(|e| {
if e.selected {
self.selected_edges.push(*e);
Expand Down Expand Up @@ -382,19 +382,19 @@ impl InteractiveApp {
.get_graph_mut()
.add_edge(NodeIndex::new(*start), NodeIndex::new(*end), ());

self.elements.get_node_mut(start).unwrap().radius += 1.;
self.elements.get_node_mut(end).unwrap().radius += 1.;
self.elements.node_mut(start).unwrap().radius += 1.;
self.elements.node_mut(end).unwrap().radius += 1.;
self.elements.add_edge(start, end)
}

fn add_random_node(&mut self) {
let node_count = self.elements.get_nodes().len();
let node_count = self.elements.nodes().len();
let idx: usize = node_count;

// compute location for new node inside graph rect
let mut rng = rand::thread_rng();
let random_node_key = self.elements.get_random_node_idx().unwrap();
let random_node = self.elements.get_nodes().get(random_node_key).unwrap();
let random_node_key = self.elements.random_node_idx().unwrap();
let random_node = self.elements.nodes().get(random_node_key).unwrap();
let random_node_loc = random_node.location;
let location = Vec2::new(
random_node_loc.x + random_node.radius + rng.gen_range(0. ..(random_node.radius * 5.)),
Expand All @@ -408,15 +408,15 @@ impl InteractiveApp {
let sim_node_idx = graph.add_node(sim_node);

// add node to elements
self.elements.get_nodes_mut().insert(
self.elements.nodes_mut().insert(
sim_node_idx.index(),
Node::new(sim_node_idx.index(), location),
);
}

fn add_random_edge(&mut self) {
let random_start = *self.elements.get_random_node_idx().unwrap();
let random_end = *self.elements.get_random_node_idx().unwrap();
let random_start = *self.elements.random_node_idx().unwrap();
let random_end = *self.elements.random_node_idx().unwrap();

self.add_edge(&random_start, &random_end).unwrap();
}
Expand All @@ -426,18 +426,18 @@ impl InteractiveApp {
let edge_indx = graph.find_edge(NodeIndex::new(*start), NodeIndex::new(*end))?;
graph.remove_edge(edge_indx)?;

self.elements.get_node_mut(start).unwrap().radius -= 1.;
self.elements.get_node_mut(end).unwrap().radius -= 1.;
self.elements.node_mut(start).unwrap().radius -= 1.;
self.elements.node_mut(end).unwrap().radius -= 1.;
self.elements.remove_edge(start, end)
}

fn remove_random_edge(&mut self) {
let key = *self.elements.get_random_edge_idx().unwrap();
let key = *self.elements.random_edge_idx().unwrap();
self.remove_edge(&key.0, &key.1).unwrap();
}

fn remove_random_node(&mut self) {
let key = *self.elements.get_random_node_idx().unwrap();
let key = *self.elements.random_node_idx().unwrap();
self.remove_node(&key).unwrap();
}

Expand All @@ -461,7 +461,7 @@ impl InteractiveApp {
graph.remove_node(NodeIndex::new(*idx))?;

let res = self.elements.remove_node(idx, neighbors);
self.settings_graph.count_edge = self.elements.get_edges().len();
self.settings_graph.count_edge = self.elements.edges().len();

res
}
Expand Down Expand Up @@ -588,21 +588,17 @@ fn apply_changes(changes: &Changes, simulation: &mut Simulation<(), ()>, element
.neighbors(NodeIndex::new(*node_idx))
.for_each(|neighbour| {
// mark neighbour
elements.get_node_mut(&neighbour.index()).unwrap().selected = selected_change;
elements.node_mut(&neighbour.index()).unwrap().selected = selected_change;

// mark edges between selected node and neighbour
if let Some(edges) =
elements.get_edges_between_mut(&neighbour.index(), node_idx)
{
if let Some(edges) = elements.edges_between_mut(&neighbour.index(), node_idx) {
edges.iter_mut().for_each(|edge| {
edge.selected = selected_change;
});
}

// mark edges between neighbour and selected node
if let Some(edges) =
elements.get_edges_between_mut(node_idx, &neighbour.index())
{
if let Some(edges) = elements.edges_between_mut(node_idx, &neighbour.index()) {
edges.iter_mut().for_each(|edge| {
edge.selected = selected_change;
});
Expand Down
Loading

0 comments on commit 08b1271

Please sign in to comment.