Skip to content

Commit

Permalink
Added delete state and more description for graph algorithms (#18)
Browse files Browse the repository at this point in the history

Co-authored-by: Komlan Kodoh <[email protected]>
  • Loading branch information
komlanKodoh and Komlan Kodoh authored Feb 10, 2024
1 parent e48ee18 commit c382aa1
Show file tree
Hide file tree
Showing 28 changed files with 2,103 additions and 579 deletions.
10 changes: 10 additions & 0 deletions wasm-lib/src/graph/suzaku_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl GraphWrapper {
return Uint32Array::from(&node_indexes[..]);
}

pub fn delete_vertex(&mut self, node_id: usize) {
let node_index = NodeIndex::<u32>::new(node_id);
self.graph.remove_node(node_index);
}

pub fn delete_edge(&mut self, edge_id: usize) {
let edge_index = petgraph::graph::EdgeIndex::<u32>::new(edge_id);
self.graph.remove_edge(edge_index);
}

pub fn edge(&self, first_node_id: usize, second_node_id: usize) -> Result<u32, String> {
let first_node = NodeIndex::<u32>::new(first_node_id);
let second_node = NodeIndex::<u32>::new(second_node_id);
Expand Down
171 changes: 91 additions & 80 deletions website/GraphUniverse/Embeddings/PhysicsBasedEmbedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,119 @@ import Matter, { Bodies, Body, Constraint, Engine, Sleeping, World } from "matte
import { Edge, getMeta, setMeta, Vertex } from "@/GraphUniverse/Graph/Graph";
import GraphUniverse from "../GraphUniverse";

export default class PhysicsBasedEmbedding<T, E> implements Embedding<T, E> {
private static readonly META_PROPERTY_NAME: string = "physics-render-object";
export default class PhysicsBasedEmbedding<V, E> implements Embedding<V, E> {
private static readonly META_PROPERTY_NAME: string = "physics-render-object";

private readonly engine: Engine;
private readonly universe: GraphUniverse<T, E>;
private readonly engine: Engine;
private readonly universe: GraphUniverse<V, E>;

constructor(graphUniverse: GraphUniverse<T, E>) {
this.engine = Engine.create({
gravity: { x: 0, y: 0 },
});
constructor(graphUniverse: GraphUniverse<V, E>) {
this.engine = Engine.create({
gravity: { x: 0, y: 0 },
});

this.universe = graphUniverse;
}
this.universe = graphUniverse;
}

initialize(): void {
this.universe.listener.addEventListener("vertexAddedEvent", event => {
const newPhysicVertex = this.addVertex(event.x, event.y);
initialize(): void {
this.universe.listener.addEventListener("vertexAddedEvent", (event) => {
const newPhysicVertex = this.addVertex(event.x, event.y);

setMeta(
event.vertex,
PhysicsBasedEmbedding.META_PROPERTY_NAME,
newPhysicVertex
);
});
setMeta(event.vertex, PhysicsBasedEmbedding.META_PROPERTY_NAME, newPhysicVertex);
});

this.universe.listener.addEventListener("edgeAdded", event => {
this.handleNewEdge(event.edge);
});
}
this.universe.listener.addEventListener("edgeAdded", (event) => {
const constraint = this.createContraint(event.edge);

control(target: Vertex<T>): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);
setMeta(event.edge, PhysicsBasedEmbedding.META_PROPERTY_NAME, constraint);
});

Body.setStatic(
physicsVertex,
false
);
}

free(target: Vertex<T>): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);
this.universe.listener.addEventListener("edgeDeletedEvent", (event) => {
this.removeEdgeConstraint(event.target);
});

Body.setStatic(
physicsVertex,
true
);
}
this.universe.listener.addEventListener("vertexDeletedEvent", (event) => {
this.removeVertex(event.target);
});
}

moveVertex(target: Vertex<T>, x: number, y: number): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);
control(target: Vertex<V>): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);

Body.setPosition(
physicsVertex,
{ x, y }
);
}
Body.setStatic(physicsVertex, false);
}

free(target: Vertex<V>): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);

private move(delta: number): void {
Engine.update(this.engine, delta * 400);
Body.setStatic(physicsVertex, true);
}

for (const graphNode of this.universe.graph.getAllVertices()) {
const body = getMeta<Matter.Body>(graphNode, PhysicsBasedEmbedding.META_PROPERTY_NAME);
moveVertex(target: Vertex<V>, x: number, y: number): void {
const physicsVertex = getMeta<Matter.Body>(target, PhysicsBasedEmbedding.META_PROPERTY_NAME);

this.universe.renderingController.moveVertex(
graphNode,
body.position.x,
body.position.y
);
};
}
Body.setPosition(physicsVertex, { x, y });
}

private handleNewEdge(newEdge : Edge<T, E>): void {
const firstBody = getMeta<Matter.Body>(newEdge.sourceVertex, PhysicsBasedEmbedding.META_PROPERTY_NAME);
const secondBody = getMeta<Matter.Body>(newEdge.targetVertex, PhysicsBasedEmbedding.META_PROPERTY_NAME);
private move(delta: number): void {
Engine.update(this.engine, delta * 400);

const constraint = Constraint.create({
bodyA: firstBody,
bodyB: secondBody,
damping: 0,
stiffness: 1 / 30,
length: 150
});
for (const graphNode of this.universe.graph.getAllVertices()) {
const body = getMeta<Matter.Body>(graphNode, PhysicsBasedEmbedding.META_PROPERTY_NAME);

World.addConstraint(this.engine.world, constraint);
this.universe.renderingController.moveVertex(graphNode, body.position.x, body.position.y);
}
}

private addVertex(x: number, y: number): Matter.Body {
const particle = Bodies.circle(
x,
y,
19
);
private createContraint(newEdge: Edge<V, E>): Constraint {
const firstBody = getMeta<Matter.Body>(
newEdge.sourceVertex,
PhysicsBasedEmbedding.META_PROPERTY_NAME
);
const secondBody = getMeta<Matter.Body>(
newEdge.targetVertex,
PhysicsBasedEmbedding.META_PROPERTY_NAME
);

particle.frictionAir = 0.5;
const constraint = Constraint.create({
bodyA: firstBody,
bodyB: secondBody,
damping: 0,
stiffness: 1 / 30,
length: 150,
});

World.add(this.engine.world, particle);
World.addConstraint(this.engine.world, constraint);

return particle;
}
return constraint;
}

public update(delta: number): void {
this.move(delta / 1000);
}
}
private removeEdgeConstraint(edge: Edge<V, E>) {
const constraint = getMeta<Constraint>(edge, PhysicsBasedEmbedding.META_PROPERTY_NAME);

console.log(edge, World);
World.remove(this.engine.world, constraint);
}

private removeVertex(vertex: Vertex<V>) {
const body = getMeta<Matter.Body>(vertex, PhysicsBasedEmbedding.META_PROPERTY_NAME);

World.remove(this.engine.world, body);
// TODO : Consider if you should remove the constraint yourself
}

private addVertex(x: number, y: number): Matter.Body {
const particle = Bodies.circle(x, y, 19);

particle.frictionAir = 0.5;

World.add(this.engine.world, particle);

return particle;
}

public update(delta: number): void {
this.move(delta / 1000);
}
}
Loading

0 comments on commit c382aa1

Please sign in to comment.