Esempio n. 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;
 }
Esempio n. 2
0
 public void splitFace(EmbeddedGraph embeddedGraph, Face face, Edge newEdge) {
   List<Node> nodes = newEdge.getAdjacentNodes();
   Graph originalGraph = embeddedGraph.getGraph();
   List<Face> newFaces = ListSequence.fromList(new ArrayList<Face>());
   ListSequence.fromList(newFaces).addElement(new Face(originalGraph));
   ListSequence.fromList(newFaces).addElement(new Face(originalGraph));
   Iterator<Dart> dartItr = ListSequence.fromList(face.getDarts()).iterator();
   Dart cur;
   do {
     cur = dartItr.next();
   } while (!(ListSequence.fromList(nodes).contains(cur.getSource())));
   Dart first = cur;
   Node found = cur.getSource();
   Node toFind = ListSequence.fromList(nodes).getElement(0);
   if (toFind == found) {
     toFind = ListSequence.fromList(nodes).getElement(1);
   }
   do {
     ListSequence.fromList(newFaces).getElement(0).addLast(cur);
     cur = dartItr.next();
   } while (cur.getSource() != toFind);
   ListSequence.fromList(newFaces).getElement(0).addLast(new Dart(newEdge, cur.getSource()));
   ListSequence.fromList(newFaces).getElement(1).addLast(new Dart(newEdge, first.getSource()));
   ListSequence.fromList(newFaces).getElement(1).addLast(cur);
   while (dartItr.hasNext()) {
     cur = dartItr.next();
     ListSequence.fromList(newFaces).getElement(1).addLast(cur);
   }
   dartItr = ListSequence.fromList(face.getDarts()).iterator();
   cur = dartItr.next();
   while (cur != first) {
     ListSequence.fromList(newFaces).getElement(1).addLast(cur);
     cur = dartItr.next();
   }
   embeddedGraph.removeFace(face);
   for (Face newFace : ListSequence.fromList(newFaces)) {
     embeddedGraph.addFace(newFace);
   }
   if (embeddedGraph.isOuterFace(face)) {
     embeddedGraph.setOuterFace(ListSequence.fromList(newFaces).getElement(1));
   }
 }
 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);
   }
 }
 public static void replaceBendsByNodes(
     EmbeddedGraph embeddedGraph, Map<Dart, Integer> bends, Map<Dart, Integer> angles) {
   for (Edge edge : ListSequence.fromList(embeddedGraph.getGraph().getEdges())) {
     List<Dart> darts = embeddedGraph.getDarts(edge);
     final Wrappers._T<Dart> dartToSplit = new Wrappers._T<Dart>(null);
     Dart opposite = null;
     if (MapSequence.fromMap(bends).get(ListSequence.fromList(darts).getElement(0)) > 0) {
       dartToSplit.value = ListSequence.fromList(darts).getElement(0);
       opposite = ListSequence.fromList(darts).getElement(1);
     }
     if (MapSequence.fromMap(bends).get(ListSequence.fromList(darts).getElement(1)) > 0) {
       dartToSplit.value = ListSequence.fromList(darts).getElement(1);
       opposite = ListSequence.fromList(darts).getElement(0);
     }
     if (dartToSplit.value != null) {
       while (MapSequence.fromMap(bends).get(dartToSplit.value) > 0) {
         List<Edge> newEdges = ListSequence.fromList(new ArrayList<Edge>());
         final Node addedNode = embeddedGraph.splitEdge(dartToSplit.value.getEdge(), newEdges);
         Edge first =
             ListSequence.fromList(newEdges)
                 .findFirst(
                     new IWhereFilter<Edge>() {
                       public boolean accept(Edge it) {
                         return it.getOpposite(addedNode) == dartToSplit.value.getSource();
                       }
                     });
         Edge second =
             ListSequence.fromList(newEdges)
                 .findFirst(
                     new IWhereFilter<Edge>() {
                       public boolean accept(Edge it) {
                         return it.getOpposite(addedNode) == dartToSplit.value.getTarget();
                       }
                     });
         for (Dart firstDart : ListSequence.fromList(embeddedGraph.getDarts(first))) {
           MapSequence.fromMap(bends).put(firstDart, 0);
           if (firstDart.getSource() == addedNode) {
             MapSequence.fromMap(angles).put(firstDart, 3);
           } else {
             MapSequence.fromMap(angles)
                 .put(firstDart, MapSequence.fromMap(angles).get(dartToSplit.value));
           }
         }
         Dart nextToSplit = null;
         Dart nextOpposite = null;
         for (Dart secondDart : ListSequence.fromList(embeddedGraph.getDarts(second))) {
           if (secondDart.getSource() == addedNode) {
             nextToSplit = secondDart;
             MapSequence.fromMap(bends)
                 .put(secondDart, MapSequence.fromMap(bends).get(dartToSplit.value) - 1);
             MapSequence.fromMap(angles).put(secondDart, 1);
           } else {
             nextOpposite = secondDart;
             MapSequence.fromMap(bends).put(secondDart, 0);
             MapSequence.fromMap(angles)
                 .put(secondDart, MapSequence.fromMap(angles).get(opposite));
           }
         }
         MapSequence.fromMap(bends).removeKey(dartToSplit.value);
         MapSequence.fromMap(angles).removeKey(dartToSplit.value);
         MapSequence.fromMap(bends).removeKey(opposite);
         MapSequence.fromMap(angles).removeKey(opposite);
         dartToSplit.value = nextToSplit;
         opposite = nextOpposite;
       }
     }
   }
 }