--- /dev/null
+/*******************************************************************************\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