]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm2/Router2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm2 / Router2.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm2/Router2.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm2/Router2.java
new file mode 100644 (file)
index 0000000..8c95cc9
--- /dev/null
@@ -0,0 +1,115 @@
+/*******************************************************************************\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.algorithm2;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.PathIterator;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Arrays;\r
+\r
+import org.simantics.g2d.routing.Constants;\r
+import org.simantics.g2d.routing.IGraphModel;\r
+import org.simantics.g2d.routing.IRouter;\r
+import org.simantics.g2d.routing.Terminal;\r
+\r
+public class Router2 implements IRouter {\r
+\r
+    static int getSomeDirection(int mask) {\r
+        if(mask == 0)\r
+            return 0;\r
+        int result = 0;\r
+        while((mask & 1) == 0) {\r
+            ++result;\r
+            mask >>= 1;\r
+        }\r
+        return result;\r
+    }\r
+    \r
+    @Override\r
+    public void update(IGraphModel model) {\r
+        LocalRouter localRouter = new LocalRouter(false);\r
+        for(Object c : model.getConnections()) {\r
+            Terminal begin = model.getBeginTerminal(c);        \r
+            Terminal end = model.getEndTerminal(c);\r
+            double[] points = model.getRoutePoints(c);\r
+            \r
+            if(begin == null) {\r
+               if(points.length < 2)\r
+                       continue;\r
+                       begin = new Terminal(points[0], points[1], 0x1, Terminal.ZEROS, \r
+                               new Rectangle2D.Double(points[0], points[1], 0.0, 0.0));\r
+                       points = Arrays.copyOfRange(points, 2, points.length);\r
+               }\r
+               if(end == null) {\r
+                       if(points.length < 2)\r
+                       continue;\r
+                       end = new Terminal(points[points.length-2], points[points.length-1], 0xf, Terminal.ZEROS,\r
+                               new Rectangle2D.Double(points[points.length-2], points[points.length-1], 0.0, 0.0));\r
+                       points = Arrays.copyOf(points, points.length-2);\r
+               }\r
+\r
+               double bestLength = Double.POSITIVE_INFINITY;\r
+               Path2D bestPath = null;\r
+               \r
+               for(int sDir : Constants.POSSIBLE_DIRECTIONS[begin.directions])\r
+                       for(int tDir : Constants.POSSIBLE_DIRECTIONS[end.directions]) {\r
+                               localRouter.sx = begin.x;\r
+                               localRouter.sy = begin.y;\r
+                               localRouter.aMinX = begin.parentObstacle.getMinX();\r
+                               localRouter.aMinY = begin.parentObstacle.getMinY();\r
+                               localRouter.aMaxX = begin.parentObstacle.getMaxX();\r
+                               localRouter.aMaxY = begin.parentObstacle.getMaxY();\r
+                               localRouter.sourceDirection = sDir;\r
+\r
+                               localRouter.tx = end.x;\r
+                               localRouter.ty = end.y;\r
+                               localRouter.bMinX = end.parentObstacle.getMinX();\r
+                               localRouter.bMinY = end.parentObstacle.getMinY();\r
+                               localRouter.bMaxX = end.parentObstacle.getMaxX();\r
+                               localRouter.bMaxY = end.parentObstacle.getMaxY();\r
+                               localRouter.targetDirection = tDir;\r
+\r
+                               localRouter.route();\r
+\r
+                               double length = pathLength(localRouter.path);\r
+                               if(length < bestLength) {\r
+                                       bestLength = length;\r
+                                       bestPath = localRouter.path;\r
+                               }  \r
+                       }\r
+            \r
+               if(bestPath != null)\r
+                       model.setPath(c, bestPath);\r
+        }\r
+    }\r
+    \r
+    final static AffineTransform IDENTITY = new AffineTransform();\r
+\r
+    static double pathLength(Path2D path) {\r
+       double length = 0.0;\r
+       PathIterator it = path.getPathIterator(IDENTITY);\r
+       double[] temp = new double[6];\r
+       double x=0.0, y=0.0;\r
+       double bendCount = 0.0;\r
+       while(!it.isDone()) {\r
+               bendCount += 1.0;\r
+               if(it.currentSegment(temp) != PathIterator.SEG_MOVETO)\r
+                       length += Math.abs(x - temp[0] + y - temp[1]);\r
+               x = temp[0];\r
+                       y = temp[1];\r
+                       it.next();\r
+       }\r
+       return length * (6.0 + bendCount);\r
+    }\r
+    \r
+}\r