X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Frouting%2FConnectionDirectionUtil.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Frouting%2FConnectionDirectionUtil.java;h=5c86382a897bda76561a4507bbd2a64e491efa81;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git
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
index 000000000..5c86382a8
--- /dev/null
+++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/routing/ConnectionDirectionUtil.java
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * 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;
+
+import java.awt.geom.Rectangle2D;
+import java.util.Arrays;
+
+import org.simantics.g2d.routing.IConnection.Connector;
+
+public class ConnectionDirectionUtil {
+
+ public static class Dir implements Comparable
{
+ int id;
+ double dist;
+
+ public Dir(int id, double dist) {
+ this.id = id;
+ this.dist = dist;
+ }
+
+ @Override
+ public int compareTo(Dir o) {
+ if(dist < o.dist)
+ return -1;
+ if(o.dist < dist)
+ return 1;
+ return 0;
+ }
+
+ }
+
+ public static Terminal createTerminal(Rectangle2D shape, double x, double y) {
+ Dir[] dirs = new Dir[] {
+ new Dir(0, shape.getMaxX() - x),
+ new Dir(1, shape.getMaxY() - y),
+ new Dir(2, x - shape.getMinX()),
+ new Dir(3, y - shape.getMinY()),
+ };
+ Arrays.sort(dirs);
+ double[] dist = new double[4];
+ int flags = 0;
+ int i=0;
+ do {
+ flags |= 1 << dirs[i].id;
+ dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
+ ++i;
+ } while(i < 4 && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist));
+ return new Terminal(x, y, flags, dist, shape);
+ }
+
+ public static int reverseDirections(int flags) {
+ return ((flags & Constants.EAST_FLAG) == 0 ? 0 : Constants.WEST_FLAG)
+ | ((flags & Constants.SOUTH_FLAG) == 0 ? 0 : Constants.NORTH_FLAG)
+ | ((flags & Constants.WEST_FLAG) == 0 ? 0 : Constants.EAST_FLAG)
+ | ((flags & Constants.NORTH_FLAG) == 0 ? 0 : Constants.SOUTH_FLAG);
+ }
+
+ public static void determineAllowedDirections(Connector c) {
+ if(c.parentObstacle == null) {
+ c.allowedDirections = 0xf;
+ return;
+ }
+ Dir[] dirs = new Dir[] {
+ new Dir(0, c.parentObstacle.getMaxX() - c.x),
+ new Dir(1, c.parentObstacle.getMaxY() - c.y),
+ new Dir(2, c.x - c.parentObstacle.getMinX()),
+ new Dir(3, c.y - c.parentObstacle.getMinY()),
+ };
+ Arrays.sort(dirs);
+ double[] dist = new double[4];
+ c.allowedDirections = 0;
+ int i=0;
+ do {
+ c.allowedDirections |= 1 << dirs[i].id;
+ dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
+ ++i;
+ } while(i < 4 &&
+ ((dirs[i].id == ((dirs[0].id + 2)&3) && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist)) ||
+ (dirs[i].dist <= 0.0 || dirs[i].dist*0.95 < dirs[0].dist))
+ );
+ }
+}