]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm2/Router4.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm2 / Router4.java
index c03ec3e4d90b64dc8eb97fd3451e44b6a7e0fac4..e2f5d1dbeb9e02e063ced717cdbc593b125cfb49 100644 (file)
-/*******************************************************************************\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 gnu.trove.map.hash.TObjectIntHashMap;\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.Collection;\r
-\r
-import org.simantics.g2d.routing.Constants;\r
-import org.simantics.g2d.routing.IConnection;\r
-import org.simantics.g2d.routing.IConnection.Connector;\r
-import org.simantics.g2d.routing.IRouter2;\r
-\r
-public class Router4 implements IRouter2 {\r
-\r
-    LocalRouter localRouter;\r
-\r
-    public Router4() {\r
-        this(false);\r
-    }\r
-\r
-    public Router4(boolean roundCorners) {\r
-        this.localRouter = new LocalRouter(roundCorners);\r
-    }\r
-\r
-    private Path2D route(double beginX, double beginY, int sDir, Rectangle2D beginObstacle,\r
-            double endX, double endY, int tDir, Rectangle2D endObstacle) {\r
-        localRouter.sx = beginX;\r
-        localRouter.sy = beginY;\r
-        if(beginObstacle == null) {\r
-            localRouter.aMinX = beginX;\r
-            localRouter.aMinY = beginY;\r
-            localRouter.aMaxX = beginX;\r
-            localRouter.aMaxY = beginY;\r
-        }\r
-        else {\r
-            localRouter.aMinX = beginObstacle.getMinX();\r
-            localRouter.aMinY = beginObstacle.getMinY();\r
-            localRouter.aMaxX = beginObstacle.getMaxX();\r
-            localRouter.aMaxY = beginObstacle.getMaxY();\r
-        }\r
-        localRouter.sourceDirection = sDir;\r
-\r
-        localRouter.tx = endX;\r
-        localRouter.ty = endY;\r
-        if(endObstacle == null) {\r
-            localRouter.bMinX = endX;\r
-            localRouter.bMinY = endY;\r
-            localRouter.bMaxX = endX;\r
-            localRouter.bMaxY = endY;\r
-        }\r
-        else {\r
-            localRouter.bMinX = endObstacle.getMinX();\r
-            localRouter.bMinY = endObstacle.getMinY();\r
-            localRouter.bMaxX = endObstacle.getMaxX();\r
-            localRouter.bMaxY = endObstacle.getMaxY();\r
-        }\r
-        localRouter.targetDirection = tDir;\r
-\r
-        localRouter.route();\r
-\r
-        return localRouter.path;\r
-    }\r
-\r
-    @Override\r
-    public void route(IConnection connection) {\r
-        Collection<?> segments = connection.getSegments();\r
-        if(segments.size() == 1)\r
-            for(Object seg : segments) {\r
-                Connector begin = connection.getBegin(seg);\r
-                Connector end = connection.getEnd(seg);\r
-\r
-                double bestLength = Double.POSITIVE_INFINITY;\r
-                Path2D bestPath = null;\r
-\r
-                for(int sDir : Constants.POSSIBLE_DIRECTIONS[begin.allowedDirections])\r
-                    for(int tDir : Constants.POSSIBLE_DIRECTIONS[end.allowedDirections]) {\r
-                        Path2D path = route(begin.x, begin.y, sDir, begin.parentObstacle,\r
-                                end.x, end.y, tDir, end.parentObstacle);\r
-\r
-                        double length = pathCost(path);\r
-                        if(length < bestLength) {\r
-                            bestLength = length;\r
-                            bestPath = localRouter.path;\r
-                        }\r
-                    }\r
-\r
-                if(bestPath != null)\r
-                    connection.setPath(seg, bestPath);\r
-            }\r
-        else {\r
-            TObjectIntHashMap<Connector> leftSegments = new TObjectIntHashMap<Connector>();\r
-            TObjectIntHashMap<Connector> rightSegments = new TObjectIntHashMap<Connector>();\r
-            TObjectIntHashMap<Connector> upSegments = new TObjectIntHashMap<Connector>();\r
-            TObjectIntHashMap<Connector> downSegments = new TObjectIntHashMap<Connector>();\r
-            TObjectIntHashMap<Connector> horizontalCount = new TObjectIntHashMap<Connector>();\r
-            for(Object seg : segments) {\r
-                Connector begin = connection.getBegin(seg);\r
-                Connector end = connection.getEnd(seg);\r
-                if(begin.x < end.x) {\r
-                    leftSegments.adjustOrPutValue(end, 1, 1);\r
-                    rightSegments.adjustOrPutValue(begin, 1, 1);\r
-                }\r
-                else {\r
-                    leftSegments.adjustOrPutValue(begin, 1, 1);\r
-                    rightSegments.adjustOrPutValue(end, 1, 1);\r
-                }\r
-                if(begin.y < end.y) {\r
-                    upSegments.adjustOrPutValue(end, 1, 1);\r
-                    downSegments.adjustOrPutValue(begin, 1, 1);\r
-                }\r
-                else {\r
-                    upSegments.adjustOrPutValue(begin, 1, 1);\r
-                    downSegments.adjustOrPutValue(end, 1, 1);\r
-                }\r
-                if((begin.allowedDirections & 5) != 0)\r
-                    horizontalCount.adjustOrPutValue(end, 1, 1);\r
-                if((begin.allowedDirections & 10) != 0)\r
-                    horizontalCount.adjustOrPutValue(end, -1, -1);\r
-                if((end.allowedDirections & 5) != 0)\r
-                    horizontalCount.adjustOrPutValue(begin, 1, 1);\r
-                if((end.allowedDirections & 10) != 0)\r
-                    horizontalCount.adjustOrPutValue(begin, -1, -1);\r
-            }\r
-            for(Object seg : segments) {\r
-                Connector begin = connection.getBegin(seg);\r
-                Connector end = connection.getEnd(seg);\r
-                int allowedBegin = begin.allowedDirections;\r
-                int allowedEnd = end.allowedDirections;\r
-\r
-                if(horizontalCount.get(begin) + horizontalCount.get(end) >= 0) {\r
-                    //System.out.println("horizontal");\r
-                    if(begin.x < end.x) {\r
-                        if(allowedBegin == 0xf) {\r
-                            if(rightSegments.get(begin) <= 1)\r
-                                allowedBegin = 1;\r
-                            else\r
-                                allowedBegin = 11;\r
-                        }\r
-                        if(allowedEnd == 0xf) {\r
-                            if(leftSegments.get(end) <= 1)\r
-                                allowedEnd = 4;\r
-                            else\r
-                                allowedEnd = 14;\r
-                        }\r
-                    }\r
-                    else {\r
-                        if(allowedBegin == 0xf) {\r
-                            if(leftSegments.get(begin) <= 1)\r
-                                allowedBegin = 4;\r
-                            else\r
-                                allowedBegin = 14;\r
-                        }\r
-                        if(allowedEnd == 0xf) {\r
-                            if(rightSegments.get(end) <= 1)\r
-                                allowedEnd = 1;\r
-                            else\r
-                                allowedEnd = 11;\r
-                        }\r
-                    }\r
-                }\r
-                else {\r
-                    //System.out.println("vertical");\r
-                    if(begin.y < end.y) {\r
-                        if(allowedBegin == 0xf) {\r
-                            if(downSegments.get(begin) <= 1)\r
-                                allowedBegin = 2;\r
-                            else\r
-                                allowedBegin = 7;\r
-                        }\r
-                        if(allowedEnd == 0xf) {\r
-                            if(upSegments.get(end) <= 1)\r
-                                allowedEnd = 8;\r
-                            else\r
-                                allowedEnd = 13;\r
-                        }\r
-                    }\r
-                    else {\r
-                        if(allowedBegin == 0xf) {\r
-                            if(upSegments.get(begin) <= 1)\r
-                                allowedBegin = 8;\r
-                            else\r
-                                allowedBegin = 13;\r
-                        }\r
-                        if(allowedEnd == 0xf) {\r
-                            if(downSegments.get(end) <= 1)\r
-                                allowedEnd = 2;\r
-                            else\r
-                                allowedEnd = 7;\r
-                        }\r
-                    }\r
-                }\r
-\r
-                //System.out.println(allowedBegin + " " + allowedEnd);\r
-\r
-                double bestLength = Double.POSITIVE_INFINITY;\r
-                Path2D bestPath = null;\r
-\r
-                for(int sDir : Constants.POSSIBLE_DIRECTIONS[allowedBegin])\r
-                    for(int tDir : Constants.POSSIBLE_DIRECTIONS[allowedEnd]) {\r
-                        Path2D path = route(begin.x, begin.y, sDir, begin.parentObstacle,\r
-                                end.x, end.y, tDir, end.parentObstacle);\r
-\r
-                        double length = pathCost(path);\r
-                        if(length < bestLength) {\r
-                            bestLength = length;\r
-                            bestPath = localRouter.path;\r
-                        }\r
-                    }\r
-\r
-                if(bestPath != null)\r
-                    connection.setPath(seg, bestPath);\r
-            }\r
-        }\r
-    }\r
-\r
-    final static AffineTransform IDENTITY = new AffineTransform();\r
-\r
-    static double pathCost(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
-        return bendCount - 1.0 / length;\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.routing.algorithm2;
+
+import gnu.trove.map.hash.TObjectIntHashMap;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Path2D;
+import java.awt.geom.PathIterator;
+import java.awt.geom.Rectangle2D;
+import java.util.Collection;
+
+import org.simantics.g2d.routing.Constants;
+import org.simantics.g2d.routing.IConnection;
+import org.simantics.g2d.routing.IConnection.Connector;
+import org.simantics.g2d.routing.IRouter2;
+
+public class Router4 implements IRouter2 {
+
+    LocalRouter localRouter;
+
+    public Router4() {
+        this(false);
+    }
+
+    public Router4(boolean roundCorners) {
+        this.localRouter = new LocalRouter(roundCorners);
+    }
+
+    private Path2D route(double beginX, double beginY, int sDir, Rectangle2D beginObstacle,
+            double endX, double endY, int tDir, Rectangle2D endObstacle) {
+        localRouter.sx = beginX;
+        localRouter.sy = beginY;
+        if(beginObstacle == null) {
+            localRouter.aMinX = beginX;
+            localRouter.aMinY = beginY;
+            localRouter.aMaxX = beginX;
+            localRouter.aMaxY = beginY;
+        }
+        else {
+            localRouter.aMinX = beginObstacle.getMinX();
+            localRouter.aMinY = beginObstacle.getMinY();
+            localRouter.aMaxX = beginObstacle.getMaxX();
+            localRouter.aMaxY = beginObstacle.getMaxY();
+        }
+        localRouter.sourceDirection = sDir;
+
+        localRouter.tx = endX;
+        localRouter.ty = endY;
+        if(endObstacle == null) {
+            localRouter.bMinX = endX;
+            localRouter.bMinY = endY;
+            localRouter.bMaxX = endX;
+            localRouter.bMaxY = endY;
+        }
+        else {
+            localRouter.bMinX = endObstacle.getMinX();
+            localRouter.bMinY = endObstacle.getMinY();
+            localRouter.bMaxX = endObstacle.getMaxX();
+            localRouter.bMaxY = endObstacle.getMaxY();
+        }
+        localRouter.targetDirection = tDir;
+
+        localRouter.route();
+
+        return localRouter.path;
+    }
+
+    @Override
+    public void route(IConnection connection) {
+        Collection<?> segments = connection.getSegments();
+        if(segments.size() == 1)
+            for(Object seg : segments) {
+                Connector begin = connection.getBegin(seg);
+                Connector end = connection.getEnd(seg);
+
+                double bestLength = Double.POSITIVE_INFINITY;
+                Path2D bestPath = null;
+
+                for(int sDir : Constants.POSSIBLE_DIRECTIONS[begin.allowedDirections])
+                    for(int tDir : Constants.POSSIBLE_DIRECTIONS[end.allowedDirections]) {
+                        Path2D path = route(begin.x, begin.y, sDir, begin.parentObstacle,
+                                end.x, end.y, tDir, end.parentObstacle);
+
+                        double length = pathCost(path);
+                        if(length < bestLength) {
+                            bestLength = length;
+                            bestPath = localRouter.path;
+                        }
+                    }
+
+                if(bestPath != null)
+                    connection.setPath(seg, bestPath);
+            }
+        else {
+            TObjectIntHashMap<Connector> leftSegments = new TObjectIntHashMap<Connector>();
+            TObjectIntHashMap<Connector> rightSegments = new TObjectIntHashMap<Connector>();
+            TObjectIntHashMap<Connector> upSegments = new TObjectIntHashMap<Connector>();
+            TObjectIntHashMap<Connector> downSegments = new TObjectIntHashMap<Connector>();
+            TObjectIntHashMap<Connector> horizontalCount = new TObjectIntHashMap<Connector>();
+            for(Object seg : segments) {
+                Connector begin = connection.getBegin(seg);
+                Connector end = connection.getEnd(seg);
+                if(begin.x < end.x) {
+                    leftSegments.adjustOrPutValue(end, 1, 1);
+                    rightSegments.adjustOrPutValue(begin, 1, 1);
+                }
+                else {
+                    leftSegments.adjustOrPutValue(begin, 1, 1);
+                    rightSegments.adjustOrPutValue(end, 1, 1);
+                }
+                if(begin.y < end.y) {
+                    upSegments.adjustOrPutValue(end, 1, 1);
+                    downSegments.adjustOrPutValue(begin, 1, 1);
+                }
+                else {
+                    upSegments.adjustOrPutValue(begin, 1, 1);
+                    downSegments.adjustOrPutValue(end, 1, 1);
+                }
+                if((begin.allowedDirections & 5) != 0)
+                    horizontalCount.adjustOrPutValue(end, 1, 1);
+                if((begin.allowedDirections & 10) != 0)
+                    horizontalCount.adjustOrPutValue(end, -1, -1);
+                if((end.allowedDirections & 5) != 0)
+                    horizontalCount.adjustOrPutValue(begin, 1, 1);
+                if((end.allowedDirections & 10) != 0)
+                    horizontalCount.adjustOrPutValue(begin, -1, -1);
+            }
+            for(Object seg : segments) {
+                Connector begin = connection.getBegin(seg);
+                Connector end = connection.getEnd(seg);
+                int allowedBegin = begin.allowedDirections;
+                int allowedEnd = end.allowedDirections;
+
+                if(horizontalCount.get(begin) + horizontalCount.get(end) >= 0) {
+                    //System.out.println("horizontal");
+                    if(begin.x < end.x) {
+                        if(allowedBegin == 0xf) {
+                            if(rightSegments.get(begin) <= 1)
+                                allowedBegin = 1;
+                            else
+                                allowedBegin = 11;
+                        }
+                        if(allowedEnd == 0xf) {
+                            if(leftSegments.get(end) <= 1)
+                                allowedEnd = 4;
+                            else
+                                allowedEnd = 14;
+                        }
+                    }
+                    else {
+                        if(allowedBegin == 0xf) {
+                            if(leftSegments.get(begin) <= 1)
+                                allowedBegin = 4;
+                            else
+                                allowedBegin = 14;
+                        }
+                        if(allowedEnd == 0xf) {
+                            if(rightSegments.get(end) <= 1)
+                                allowedEnd = 1;
+                            else
+                                allowedEnd = 11;
+                        }
+                    }
+                }
+                else {
+                    //System.out.println("vertical");
+                    if(begin.y < end.y) {
+                        if(allowedBegin == 0xf) {
+                            if(downSegments.get(begin) <= 1)
+                                allowedBegin = 2;
+                            else
+                                allowedBegin = 7;
+                        }
+                        if(allowedEnd == 0xf) {
+                            if(upSegments.get(end) <= 1)
+                                allowedEnd = 8;
+                            else
+                                allowedEnd = 13;
+                        }
+                    }
+                    else {
+                        if(allowedBegin == 0xf) {
+                            if(upSegments.get(begin) <= 1)
+                                allowedBegin = 8;
+                            else
+                                allowedBegin = 13;
+                        }
+                        if(allowedEnd == 0xf) {
+                            if(downSegments.get(end) <= 1)
+                                allowedEnd = 2;
+                            else
+                                allowedEnd = 7;
+                        }
+                    }
+                }
+
+                //System.out.println(allowedBegin + " " + allowedEnd);
+
+                double bestLength = Double.POSITIVE_INFINITY;
+                Path2D bestPath = null;
+
+                for(int sDir : Constants.POSSIBLE_DIRECTIONS[allowedBegin])
+                    for(int tDir : Constants.POSSIBLE_DIRECTIONS[allowedEnd]) {
+                        Path2D path = route(begin.x, begin.y, sDir, begin.parentObstacle,
+                                end.x, end.y, tDir, end.parentObstacle);
+
+                        double length = pathCost(path);
+                        if(length < bestLength) {
+                            bestLength = length;
+                            bestPath = localRouter.path;
+                        }
+                    }
+
+                if(bestPath != null)
+                    connection.setPath(seg, bestPath);
+            }
+        }
+    }
+
+    final static AffineTransform IDENTITY = new AffineTransform();
+
+    static double pathCost(Path2D path) {
+        double length = 0.0;
+        PathIterator it = path.getPathIterator(IDENTITY);
+        double[] temp = new double[6];
+        double x=0.0, y=0.0;
+        double bendCount = 0.0;
+        while(!it.isDone()) {
+            bendCount += 1.0;
+            if(it.currentSegment(temp) != PathIterator.SEG_MOVETO)
+                length += Math.abs(x - temp[0] + y - temp[1]);
+            x = temp[0];
+            y = temp[1];
+            it.next();
+        }
+        //return length * (6.0 + bendCount);
+        return bendCount - 1.0 / length;
+    }
+}