Skip to content

Commit

Permalink
Add support for directed edge collapsing
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Dec 9, 2023
1 parent 781a4cd commit 4841e12
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ee/ut/dendroloj/GenericGraphLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static Graph assembleGraph(GraphCanvas<?> graphCanvas) {
Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), v1, v2, edge.directed);
if (edge.label != null) graphEdge.setAttribute("label", edge.label);
if (edge.color != null) graphEdge.setAttribute("ui.color", edge.color);
if (edge.collapsed) graphEdge.setAttribute("ui._collapse");
if (edge.arrowOnly) graphEdge.setAttribute("ui.class", "arrowonly");
}
return graph;
}
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/ee/ut/dendroloj/GraphCanvas.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ee.ut.dendroloj;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.*;
import java.util.List;
import java.util.Set;

/**
* A helper class for drawing a graph.
Expand All @@ -14,6 +12,7 @@
*/
public final class GraphCanvas<V> {

private final Map<IdPair, Edge<V>> collapsibleEdges = new HashMap<>();
private final Set<String> drawnVertices = new HashSet<>();
final List<Vertex<V>> vertices = new ArrayList<>();
final List<Edge<V>> edges = new ArrayList<>();
Expand Down Expand Up @@ -116,7 +115,40 @@ public void drawDirectedEdge(V v1, V v2, String label) {
*/
public void drawDirectedEdge(V v1, V v2, String label, Color color) {
if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(true, v1, v2, label, color));
String v1id = IdHelper.getNodeId(v1);
String v2id = IdHelper.getNodeId(v2);
Edge<V> edge = new Edge<>(true, v1, v2, label, color);
Edge<V> collapsibleEdge = collapsibleEdges.get(new IdPair(v2id, v1id));
edges.add(edge);
collapsibleEdges.putIfAbsent(new IdPair(v1id, v2id), edge);
if (collapsibleEdge != null && !collapsibleEdge.collapsed && Objects.equals(collapsibleEdge.label, label)) {
edge.collapsed = true;
edge.arrowOnly = true;
collapsibleEdge.collapsed = true;
}
}

private static class IdPair {
public final String v1;
public final String v2;

public IdPair(String v1, String v2) {
this.v1 = v1;
this.v2 = v2;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdPair idPair = (IdPair) o;
return Objects.equals(v1, idPair.v1) && Objects.equals(v2, idPair.v2);
}

@Override
public int hashCode() {
return Objects.hash(v1, v2);
}
}

static final class Vertex<T> {
Expand Down Expand Up @@ -148,6 +180,8 @@ private Vertex(T vertex, String label, Color color, double x, double y) {

static final class Edge<T> {
public final boolean directed;
public boolean collapsed = false;
public boolean arrowOnly = false;
public final T v1;
public final T v2;
public final String label;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ee/ut/dendroloj/GraphGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.graphstream.ui.swing_viewer.SwingViewer;
import org.graphstream.ui.swing_viewer.util.DefaultMouseManager;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import org.graphstream.ui.view.camera.Camera;
import org.graphstream.ui.view.util.MouseManager;
import org.graphstream.ui.view.util.ShortcutManager;
Expand Down Expand Up @@ -37,6 +38,10 @@ public static void initGenericGUI(double uiScale, Graph graph, Layout layout) {
"edge.error {" +
" fill-color: rgb(255, 0, 0);" +
"}" +
"edge.arrowonly {" +
" text-mode: hidden;"+
" size: 0px;" +
"}" +
"node {" +
" size: %fpx;" +
" fill-mode: dyn-plain;" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ public void configureConnectorForElement(DefaultCamera2D camera, GraphicEdge ele
if (element.hasAttribute("ui.points")) {
skel.setPoly(element.getAttribute("ui.points"));
} else {
// DENDROLOJ EDIT:
// Disabled multi-edge rendering for edges that are marked with ui._collapse.
positionForLinesAndCurves(skel, element.from.getStyle(), element.from,
element.to, element.multi, element.getGroup());
element.to, element.multi, element.hasAttribute("ui._collapse") ? null : element.getGroup());
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/test/java/GraafKatsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ public static void main(String[] args) {
{12, -1, -1, 0, -1},
{-1, -1, 5, 4, 123}
};
Dendrologist.drawGraph(M, nimed);
// Dendrologist.drawGraph(M, null);
{
GraphCanvas<Integer> lõuend = new GraphCanvas<>();
for (int i = 0; i < M.length; i++) {
lõuend.drawVertex(i, nimed[i]);
}
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M.length; j++) {
if (i != j && M[i][j] != - 1) lõuend.drawDirectedEdge(i, j, String.valueOf(M[i][j]));
}
}
Dendrologist.drawGraph(lõuend);
}

List<Tipp> tipud = new ArrayList<>();
tipud.add(new Tipp("A", 1, 0));
Expand All @@ -39,14 +49,16 @@ public static void main(String[] args) {
tipud.get(4).kaared.add(new Kaar(0, tipud.get(4)));
tipud.get(4).kaared.add(new Kaar(7, tipud.get(2)));

GraphCanvas<Tipp> lõuend = new GraphCanvas<>();
for (Tipp tipp : tipud) {
lõuend.drawFixedVertex(tipp, tipp.tähis, tipp.x, tipp.y);
for (Kaar kaar : tipp.kaared) {
lõuend.drawEdge(tipp, kaar.lõppTipp, String.valueOf(kaar.kaal));
{
GraphCanvas<Tipp> lõuend = new GraphCanvas<>();
for (Tipp tipp : tipud) {
lõuend.drawFixedVertex(tipp, tipp.tähis, tipp.x, tipp.y);
for (Kaar kaar : tipp.kaared) {
lõuend.drawEdge(tipp, kaar.lõppTipp, String.valueOf(kaar.kaal));
}
}
Dendrologist.drawGraph(lõuend);
}
Dendrologist.drawGraph(lõuend);
}

private static class Tipp {
Expand Down

0 comments on commit 4841e12

Please sign in to comment.