Пример #1
0
 public Node split(EmbeddedGraph embeddedGraph, Edge edge) {
   Graph originalGraph = embeddedGraph.getGraph();
   Node newNode = originalGraph.createDummyNode();
   originalGraph.removeEdge(edge);
   List<Edge> newEdges = ListSequence.fromList(new ArrayList<Edge>());
   ListSequence.fromList(newEdges).addElement(originalGraph.connect(edge.getSource(), newNode));
   ListSequence.fromList(newEdges).addElement(originalGraph.connect(newNode, edge.getTarget()));
   MapSequence.fromMap(mySplittedEdges).put(edge, newEdges);
   List<Face> facesToProcess = ListSequence.fromList(new ArrayList<Face>());
   ListSequence.fromList(facesToProcess)
       .addSequence(ListSequence.fromList(embeddedGraph.getAdjacentFaces(edge)));
   for (Face face : ListSequence.fromList(facesToProcess)) {
     List<Dart> darts = face.getDarts();
     int pos = 0;
     while (ListSequence.fromList(darts).getElement(pos).getEdge() != edge) {
       pos++;
     }
     Dart dartToReplace = ListSequence.fromList(darts).getElement(pos);
     for (Edge newEdge : ListSequence.fromList(newEdges)) {
       if (ListSequence.fromList(newEdge.getAdjacentNodes()).contains(dartToReplace.getSource())) {
         embeddedGraph.setDart(face, pos, new Dart(newEdge, dartToReplace.getSource()));
       }
     }
     for (Edge newEdge : ListSequence.fromList(newEdges)) {
       if (ListSequence.fromList(newEdge.getAdjacentNodes()).contains(dartToReplace.getTarget())) {
         embeddedGraph.insertDart(face, pos + 1, new Dart(newEdge, newNode));
       }
     }
   }
   return newNode;
 }
Пример #2
0
 protected GraphLayout doLayoutCopy(LayoutInfo patchInfo) {
   Graph graph = patchInfo.getGraph();
   final Map<Node, Integer> components = ConnectivityComponents.getComponents(graph);
   int numComponents = 0;
   for (Node node : ListSequence.fromList(graph.getNodes())) {
     numComponents = Math.max(numComponents, MapSequence.fromMap(components).get(node));
   }
   numComponents++;
   List<LayoutInfoCopier> copiers =
       ListSequence.fromList(new ArrayList<LayoutInfoCopier>(numComponents));
   List<GraphLayout> subgraphLayouts =
       ListSequence.fromList(new ArrayList<GraphLayout>(numComponents));
   for (int num = 0; num < numComponents; num++) {
     LayoutInfoCopier copier = new LayoutInfoCopier(patchInfo);
     ListSequence.fromList(copiers).addElement(copier);
     final int curComponent = num;
     copier.copySubgraph(
         new Filter<INode>() {
           public boolean accept(INode node) {
             Node myNode = ((Node) node);
             return (Integer) MapSequence.fromMap(components).get(myNode) == curComponent;
           }
         });
     LayoutInfo subgraphInfo = copier.getLayoutInfoCopy();
     ListSequence.fromList(subgraphLayouts).addElement(doLayoutConnectedGraph(subgraphInfo));
   }
   List<Rectangle> subgraphLayoutRects =
       ListSequence.fromList(new ArrayList<Rectangle>(numComponents));
   for (GraphLayout layout : ListSequence.fromList(subgraphLayouts)) {
     ListSequence.fromList(subgraphLayoutRects).addElement(layout.getContainingRectangle());
   }
   List<Dimension> subgraphLayoutShifts = getSubgraphLayoutShifts(subgraphLayoutRects);
   GraphLayout graphLayout = GraphLayoutFactory.createGraphLayout(graph);
   for (int num = 0; num < numComponents; num++) {
     LayoutInfoCopier copier = ListSequence.fromList(copiers).getElement(num);
     GraphLayout layout = ListSequence.fromList(subgraphLayouts).getElement(num);
     Dimension shift = ListSequence.fromList(subgraphLayoutShifts).getElement(num);
     for (INode node : SetSequence.fromSet(copier.getCopiedNodes())) {
       Rectangle rect = layout.getNodeLayout(copier.getNodeCopy(node));
       rect.translate(shift.width, shift.height);
       graphLayout.setLayoutFor(node, rect);
     }
     for (IEdge edge : SetSequence.fromSet(copier.getCopiedEdges())) {
       List<Point> path = layout.getEdgeLayout(copier.getEdgeCopy(edge));
       for (Point point : ListSequence.fromList(path)) {
         point.translate(shift.width, shift.height);
       }
       graphLayout.setLayoutFor(edge, path);
       if (SetSequence.fromSet(patchInfo.getLabeledEdges()).contains(edge)) {
         Rectangle rect = layout.getLabelLayout(copier.getEdgeCopy(edge));
         rect.translate(shift.width, shift.height);
         graphLayout.setLabelLayout(edge, rect);
       }
     }
   }
   return graphLayout;
 }
Пример #3
0
 private void restoreEdge(EmbeddedGraph embeddedGraph, Edge removedEdge) {
   MapSequence.fromMap(mySplittedEdges)
       .put(removedEdge, ListSequence.fromList(new ArrayList<Edge>()));
   Graph graph = embeddedGraph.getGraph();
   DualGraph dualGraph = new DualGraph(embeddedGraph);
   List<Node> newNodes = ListSequence.fromList(new ArrayList<Node>());
   for (Node node : ListSequence.fromList(removedEdge.getAdjacentNodes())) {
     Node newNode = dualGraph.createDummyNode();
     for (Edge nodeEdge : ListSequence.fromList(node.getEdges(Edge.Direction.BOTH))) {
       for (Face face : ListSequence.fromList(embeddedGraph.getAdjacentFaces(nodeEdge))) {
         dualGraph.connect(newNode, MapSequence.fromMap(dualGraph.getNodesMap()).get(face));
       }
     }
     ListSequence.fromList(newNodes).addElement(newNode);
   }
   List<Edge> path =
       ShortestPath.getPath(
           dualGraph,
           ListSequence.fromList(newNodes).getElement(0),
           ListSequence.fromList(newNodes).getElement(1),
           Edge.Direction.BOTH);
   List<Node> nodePath = ListSequence.fromList(new ArrayList<Node>());
   List<Face> facePath = ListSequence.fromList(new ArrayList<Face>());
   ListSequence.fromList(nodePath)
       .addElement(ListSequence.fromList(removedEdge.getAdjacentNodes()).getElement(0));
   Node cur = ListSequence.fromList(newNodes).getElement(0);
   for (Edge edge : ListSequence.fromList(path)) {
     Edge realEdge = MapSequence.fromMap(dualGraph.getEdgesMap()).get(edge);
     if (embeddedGraph.getAdjacentFaces(realEdge) != null) {
       ListSequence.fromList(nodePath)
           .addElement(
               split(embeddedGraph, MapSequence.fromMap(dualGraph.getEdgesMap()).get(edge)));
     }
     cur = edge.getOpposite(cur);
     Face curFace = MapSequence.fromMap(dualGraph.getFacesMap()).get(cur);
     if (curFace != null) {
       ListSequence.fromList(facePath).addElement(curFace);
     }
   }
   ListSequence.fromList(nodePath)
       .addElement(ListSequence.fromList(removedEdge.getAdjacentNodes()).getElement(1));
   for (int i = 0; i < ListSequence.fromList(nodePath).count() - 1; i++) {
     Edge newEdge =
         graph.connect(
             ListSequence.fromList(nodePath).getElement(i),
             ListSequence.fromList(nodePath).getElement(i + 1));
     ListSequence.fromList(MapSequence.fromMap(mySplittedEdges).get(removedEdge))
         .addElement(newEdge);
     splitFace(embeddedGraph, ListSequence.fromList(facePath).getElement(i), newEdge);
   }
 }
Пример #4
0
 public EmbeddedGraph find(Graph graph) {
   mySplittedEdges = MapSequence.fromMap(new HashMap<Edge, List<Edge>>());
   EmbeddedGraph embeddedGraph = new EmbeddedGraph(graph);
   List<Edge> removed = ListSequence.fromList(new ArrayList<Edge>());
   Face outerFace = getOuterTreeFace(graph, removed);
   for (Edge edge : ListSequence.fromList(removed)) {
     graph.removeEdge(edge);
   }
   embeddedGraph.addFace(outerFace);
   embeddedGraph.setOuterFace(outerFace);
   for (Edge removedEdge : ListSequence.fromList(removed)) {
     this.restoreEdge(embeddedGraph, removedEdge);
     CheckEmbeddedGraph.checkEmbeddedGraph(embeddedGraph, false);
   }
   mergeEdges();
   return embeddedGraph;
 }
 public static void getRepresentation(
     EmbeddedGraph embeddedGraph,
     Set<Edge> edgesToBeStraight,
     Map<Dart, Integer> bends,
     Map<Dart, Integer> angles) {
   Graph graph = embeddedGraph.getGraph();
   Graph network = new Graph();
   Node center = network.createNode();
   Map<Edge, Integer> low = MapSequence.fromMap(new HashMap<Edge, Integer>());
   Map<Edge, Integer> capacity = MapSequence.fromMap(new HashMap<Edge, Integer>());
   Map<Edge, Integer> cost = MapSequence.fromMap(new HashMap<Edge, Integer>());
   Map<Node, Node> nodeMap = MapSequence.fromMap(new HashMap<Node, Node>());
   for (Node node : ListSequence.fromList(graph.getNodes())) {
     Node networkNode = network.createNode();
     MapSequence.fromMap(nodeMap).put(node, networkNode);
     Edge edge = network.connect(center, networkNode);
     MapSequence.fromMap(low).put(edge, 4);
     MapSequence.fromMap(capacity).put(edge, MapSequence.fromMap(low).get(edge));
     MapSequence.fromMap(cost).put(edge, 0);
   }
   Map<Face, Node> faceMap = MapSequence.fromMap(new HashMap<Face, Node>());
   for (Face face : ListSequence.fromList(embeddedGraph.getFaces())) {
     Node node = network.createNode();
     MapSequence.fromMap(faceMap).put(face, node);
     Edge edge = network.connect(node, center);
     if (embeddedGraph.isOuterFace(face)) {
       MapSequence.fromMap(low).put(edge, 2 * ListSequence.fromList(face.getDarts()).count() + 4);
     } else {
       MapSequence.fromMap(low).put(edge, 2 * ListSequence.fromList(face.getDarts()).count() - 4);
     }
     MapSequence.fromMap(capacity).put(edge, MapSequence.fromMap(low).get(edge));
     MapSequence.fromMap(cost).put(edge, 0);
   }
   Map<Dart, Edge> dartBendMap = MapSequence.fromMap(new HashMap<Dart, Edge>());
   Map<Dart, Edge> dartAngleMap = MapSequence.fromMap(new HashMap<Dart, Edge>());
   for (Face face : ListSequence.fromList(embeddedGraph.getFaces())) {
     Node faceNode = MapSequence.fromMap(faceMap).get(face);
     for (Dart dart : ListSequence.fromList(face.getDarts())) {
       Edge edge = network.connect(MapSequence.fromMap(nodeMap).get(dart.getSource()), faceNode);
       MapSequence.fromMap(dartAngleMap).put(dart, edge);
       MapSequence.fromMap(low).put(edge, 1);
       MapSequence.fromMap(capacity).put(edge, 4);
       MapSequence.fromMap(cost).put(edge, 0);
       List<Face> faces = embeddedGraph.getAdjacentFaces(dart.getEdge());
       Node oppositeFaceNode;
       if (ListSequence.fromList(faces).getElement(0) == face) {
         oppositeFaceNode =
             MapSequence.fromMap(faceMap).get(ListSequence.fromList(faces).getElement(1));
       } else {
         oppositeFaceNode =
             MapSequence.fromMap(faceMap).get(ListSequence.fromList(faces).getElement(0));
       }
       edge = network.connect(faceNode, oppositeFaceNode);
       MapSequence.fromMap(dartBendMap).put(dart, edge);
       MapSequence.fromMap(low).put(edge, 0);
       if (SetSequence.fromSet(edgesToBeStraight).contains(dart.getEdge())) {
         MapSequence.fromMap(capacity).put(edge, 0);
       } else {
         MapSequence.fromMap(capacity).put(edge, Integer.MAX_VALUE / 2);
       }
       MapSequence.fromMap(cost).put(edge, 1);
     }
   }
   if (SHOW_INFO > 0) {
     System.out.println("Constructed network:");
     for (Node node : ListSequence.fromList(graph.getNodes())) {
       System.out.println("for node " + node + ": " + MapSequence.fromMap(nodeMap).get(node));
     }
     for (Face face : ListSequence.fromList(embeddedGraph.getFaces())) {
       System.out.println("for face " + face + ": " + MapSequence.fromMap(faceMap).get(face));
     }
     for (Edge edge : ListSequence.fromList(network.getEdges())) {
       System.out.println(
           "edge "
               + edge
               + ": low = "
               + MapSequence.fromMap(low).get(edge)
               + ", cap = "
               + MapSequence.fromMap(capacity).get(edge)
               + ", cost = "
               + MapSequence.fromMap(cost).get(edge));
     }
   }
   Map<Edge, Integer> circulation =
       MinCostCirculation.getCirculation(network, low, capacity, cost);
   for (Dart dart : SetSequence.fromSet(MapSequence.fromMap(dartBendMap).keySet())) {
     MapSequence.fromMap(bends)
         .put(
             dart,
             MapSequence.fromMap(circulation).get(MapSequence.fromMap(dartBendMap).get(dart)));
     MapSequence.fromMap(angles)
         .put(
             dart,
             MapSequence.fromMap(circulation).get(MapSequence.fromMap(dartAngleMap).get(dart)));
   }
   if (SHOW_INFO > 0) {
     System.out.println("bends: " + bends);
     System.out.println("angles: " + angles);
   }
 }