在OGDF中使用GraphCopy :: initByCC维护GraphAttributes

时间:2016-06-09 14:17:32

标签: c++ gml-geographic-markup-lan ogdf

我正在尝试使用OGDF对从GML文件加载的图形执行某些处理。这些图仅在维护节点标签时才有意义。遗憾的是,OGDF不容易保留标签之类的节点属性,因为它们是在一个名为import javafx.animation.*; import javafx.application.Application; import javafx.beans.property.SimpleIntegerProperty; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.effect.GaussianBlur; import javafx.scene.layout.*; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.scene.text.Font; import javafx.scene.transform.Rotate; import javafx.stage.Popup; import javafx.stage.Stage; import javafx.util.Duration; import static java.lang.Math.cos; import static java.lang.Math.sin; public class Main extends Application { public static final double CIRCLES_CENTER_X = 600; public static final double CIRCLES_CENTER_Y = 450; public static final double CIRCLES_RADIUS = 15; public static final double CIRCLES_DISTANCE = 300; public static final double GAME_HEIGHT = 700; public static final double GAME_WIDTH = 1200; private Stage primaryStage; public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; primaryStage.setMinWidth(GAME_WIDTH); primaryStage.setMinHeight(GAME_HEIGHT); final Scene scene; BorderPane root = new BorderPane(); scene = new Scene(root, Main.GAME_WIDTH, Main.GAME_HEIGHT); Circle orangeCircle = new Circle(Main.CIRCLES_CENTER_X + Main.CIRCLES_DISTANCE / 2 * cos(0), Main.CIRCLES_CENTER_Y + Main.CIRCLES_DISTANCE / 2 * sin(0), Main.CIRCLES_RADIUS, Color.ORANGE); Circle yellowCircle = new Circle(Main.CIRCLES_CENTER_X - Main.CIRCLES_DISTANCE / 2 * cos(0), Main.CIRCLES_CENTER_Y - Main.CIRCLES_DISTANCE / 2 * sin(0), Main.CIRCLES_RADIUS, Color.YELLOW); Pane game = new Pane(orangeCircle, yellowCircle); root.setCenter(game); SimpleIntegerProperty angle = new SimpleIntegerProperty(0); root.setOnKeyPressed(ke -> { if (ke.getCode().toString().equals("RIGHT")) { angle.set(360); } if (ke.getCode().toString().equals("LEFT")) { angle.set(-360); } }); root.setOnKeyReleased(ke -> { if (ke.getCode().toString().equals("RIGHT")) { angle.set(0); } if (ke.getCode().toString().equals("LEFT")) { angle.set(0); } }); Rotate orangeCircleRotation = new Rotate(0, Main.CIRCLES_CENTER_X, Main.CIRCLES_CENTER_Y); orangeCircle.getTransforms().add(orangeCircleRotation); Rotate yellowCircleRotation = new Rotate(0, Main.CIRCLES_CENTER_X, Main.CIRCLES_CENTER_Y); yellowCircle.getTransforms().add(yellowCircleRotation); Timeline rotationAnimation = new Timeline(); rotationAnimation.setCycleCount(Timeline.INDEFINITE); angle.addListener((ov, old_val, new_val) -> { System.out.println("fk"); rotationAnimation.stop(); while (rotationAnimation.getKeyFrames().size() > 0) { rotationAnimation.getKeyFrames().remove(0); } rotationAnimation.getKeyFrames().add( new KeyFrame(Duration.millis(2000), new KeyValue(orangeCircleRotation.angleProperty(), angle.getValue()))); rotationAnimation.getKeyFrames().add( new KeyFrame(Duration.millis(2000), new KeyValue(yellowCircleRotation.angleProperty(), angle.getValue()))); rotationAnimation.play(); } ); primaryStage.setScene(scene); primaryStage.show(); } public Stage getPrimaryStage() { return primaryStage; } } 的单独数据结构中维护的。我的问题是GraphAttributes将节点标签与节点索引相关联,而这些节点标签不是由我需要使用的某些图形转换维护的。

我需要在Graphs上执行的一个转换是将每个连接的子图拆分为GML文件。加载图形及其节点标签很简单:

GraphAttributes

同样,OGDF提供ogdf::Graph graph; ogdf::GraphAttributes attributes(graph, ogdf::GraphAttributes::nodeLabel); ogdf::GraphIO::readGML(attributes, graph, FILENAME); // this gives the correct label of the first node in the graph attributes.label(graph.firstNode()); 类来查找图的连通子图。由于我想独立使用这些子图,我使用CCsInfo方法创建单独的GraphCopy::initByCC个实例。

Graph

这样可行,ogdf::CCsInfo info(graph); ogdf::GraphCopy copy(graph); ogdf::EdgeArray< ogdf::edge > edgeArray(graph); // where i (int) is the number of the connected subgraph to copy copy.initByCC(info, i, edgeArray); // this now gives the wrong label for the first node in copy attributes.label(copy.firstNode()); 仅包含已连接子图的节点和边。但是,副本中节点的索引与原始图中节点的索引不同。这意味着标签到copy对象中节点的映射不适用于attributes中的节点。

有没有办法在copy对象上执行相同的转换,以便我可以为复制的连接子图中的节点获取正确的标签?

1 个答案:

答案 0 :(得分:0)

事实证明这并不像我想象的那么困难。我缺少的关键部分是你可以使用GraphCopy::original方法从原始图中获取带有索引的节点,然后使用该节点获取标签。

// get the correct label for the first node of the GraphCopy object copy
attributes.label(copy.original(copy.firstNode()));