]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RouterOld.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm1 / RouterOld.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RouterOld.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RouterOld.java
new file mode 100644 (file)
index 0000000..aa82c5c
--- /dev/null
@@ -0,0 +1,120 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.routing.algorithm1;\r
+\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Comparator;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.g2d.routing.Connection;\r
+import org.simantics.g2d.routing.IRouterOld;\r
+import org.simantics.g2d.routing.Obstacle;\r
+\r
+public class RouterOld implements IRouterOld {\r
+\r
+       Map<Connection, Path2D> connections = new HashMap<Connection, Path2D>();\r
+       Set<Obstacle> obstacles = new HashSet<Obstacle>();\r
+       boolean dirty = false;  \r
+\r
+       @Override\r
+       public void addObstacle(Obstacle obstacle) {\r
+               obstacles.add(obstacle);\r
+               dirty = true;\r
+       }\r
+       \r
+       @Override\r
+       public void removeObstacle(Obstacle obstacle) {\r
+               obstacles.remove(obstacle);\r
+               dirty = true;           \r
+       }\r
+       \r
+       @Override\r
+       public void addConnection(Connection connection) {\r
+               connections.put(connection, null);\r
+               dirty = true;\r
+       }\r
+       \r
+       @Override\r
+       public void removeConnection(Connection connection) {\r
+               connections.remove(connection);\r
+               dirty = true;           \r
+       }       \r
+\r
+       @Override\r
+       public Path2D getPath(Connection connection) {\r
+               if(dirty) {\r
+                       synchronized(this) {\r
+                               if(dirty) {\r
+                                       long beginTime = System.nanoTime();\r
+                                       update();\r
+                                       long endTime = System.nanoTime();\r
+                                       System.out.println((endTime-beginTime)*1e-6 + "ms");\r
+                                       dirty = false;\r
+                               }\r
+                       }\r
+               }\r
+               return connections.get(connection);\r
+       }\r
+\r
+       private void update() {\r
+               ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(obstacles.size());\r
+               for(Obstacle obs : obstacles) {\r
+                       Rectangle2D shape = obs.shape;\r
+                       rectangles.add(new Rectangle(\r
+                               shape.getMinX(),\r
+                               shape.getMinY(),\r
+                               shape.getMaxX(),\r
+                               shape.getMaxY()));\r
+               }\r
+               Connection[] carray = \r
+                       connections.keySet().toArray(new Connection[connections.size()]);\r
+               Arrays.sort(carray, new Comparator<Connection>() {\r
+\r
+                       @Override\r
+                       public int compare(Connection o1, Connection o2) {\r
+                               double l1 = o1.minLength();\r
+                               double l2 = o2.minLength();\r
+                               if(l1 < l2)\r
+                                       return -1;\r
+                               if(l1 > l2)\r
+                                       return 1;\r
+                               if(o1.routePath.length < o2.routePath.length)\r
+                                       return -1;\r
+                               if(o1.routePath.length > o2.routePath.length)\r
+                                       return 1;\r
+                               for(int i=0;i<o1.routePath.length;++i) {\r
+                                       if(o1.routePath[i] < o2.routePath[i])\r
+                                               return -1;\r
+                                       if(o1.routePath[i] > o2.routePath[i])\r
+                                               return 1;\r
+                               }\r
+                               return 0;\r
+                       }\r
+                       \r
+               });\r
+               //System.out.println("----------------------------------------------------------");\r
+               ArrayList<Path2D> lines = new ArrayList<Path2D>(connections.size());            \r
+               for(Connection c : carray) {\r
+//                     StaticRouter router = new StaticRouter(rectangles, lines);\r
+//                     Path2D path = router.route(c);\r
+//                     connections.put(c, path);\r
+//                     if(path != null)\r
+//                             lines.add(path);\r
+               }\r
+       }\r
+}\r