]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/ConnectionDirectionUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / ConnectionDirectionUtil.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/ConnectionDirectionUtil.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/ConnectionDirectionUtil.java
new file mode 100644 (file)
index 0000000..5c86382
--- /dev/null
@@ -0,0 +1,91 @@
+/*******************************************************************************\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;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Arrays;\r
+\r
+import org.simantics.g2d.routing.IConnection.Connector;\r
+\r
+public class ConnectionDirectionUtil {\r
+       \r
+       public static class Dir implements Comparable<Dir> {\r
+               int id;\r
+               double dist;\r
+               \r
+               public Dir(int id, double dist) {\r
+                       this.id = id;\r
+                       this.dist = dist;\r
+               }\r
+\r
+               @Override\r
+               public int compareTo(Dir o) {\r
+                       if(dist < o.dist)\r
+                               return -1;\r
+                       if(o.dist < dist)\r
+                               return 1;\r
+                       return 0;\r
+               }       \r
+               \r
+       }\r
+       \r
+       public static Terminal createTerminal(Rectangle2D shape, double x, double y) {\r
+               Dir[] dirs = new Dir[] {\r
+                       new Dir(0, shape.getMaxX() - x),\r
+                       new Dir(1, shape.getMaxY() - y),\r
+                       new Dir(2, x - shape.getMinX()),\r
+                       new Dir(3, y - shape.getMinY()),\r
+               };\r
+               Arrays.sort(dirs);\r
+               double[] dist = new double[4];\r
+               int flags = 0;\r
+               int i=0;\r
+               do {\r
+                       flags |= 1 << dirs[i].id;\r
+                       dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);\r
+                       ++i;\r
+               } while(i < 4 && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist));\r
+               return new Terminal(x, y, flags, dist, shape);\r
+       }\r
+       \r
+       public static int reverseDirections(int flags) {\r
+               return ((flags & Constants.EAST_FLAG) == 0 ? 0 : Constants.WEST_FLAG)\r
+                        | ((flags & Constants.SOUTH_FLAG) == 0 ? 0 : Constants.NORTH_FLAG)\r
+                        | ((flags & Constants.WEST_FLAG) == 0 ? 0 : Constants.EAST_FLAG)\r
+                        | ((flags & Constants.NORTH_FLAG) == 0 ? 0 : Constants.SOUTH_FLAG);\r
+       }\r
+       \r
+       public static void determineAllowedDirections(Connector c) {\r
+           if(c.parentObstacle == null) {\r
+               c.allowedDirections = 0xf;\r
+               return;\r
+           }\r
+           Dir[] dirs = new Dir[] {\r
+            new Dir(0, c.parentObstacle.getMaxX() - c.x),\r
+            new Dir(1, c.parentObstacle.getMaxY() - c.y),\r
+            new Dir(2, c.x - c.parentObstacle.getMinX()),\r
+            new Dir(3, c.y - c.parentObstacle.getMinY()),\r
+        };\r
+        Arrays.sort(dirs);\r
+        double[] dist = new double[4];\r
+        c.allowedDirections = 0;\r
+        int i=0;\r
+        do {\r
+            c.allowedDirections |= 1 << dirs[i].id;\r
+            dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);\r
+            ++i;\r
+        } while(i < 4 &&\r
+                       ((dirs[i].id == ((dirs[0].id + 2)&3) && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist)) ||\r
+                       (dirs[i].dist <= 0.0 || dirs[i].dist*0.95 < dirs[0].dist))\r
+                       );\r
+       }\r
+}\r