1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.routing;
14 import java.awt.geom.Rectangle2D;
15 import java.util.Arrays;
17 import org.simantics.g2d.routing.IConnection.Connector;
19 public class ConnectionDirectionUtil {
21 public static class Dir implements Comparable<Dir> {
25 public Dir(int id, double dist) {
31 public int compareTo(Dir o) {
41 public static Terminal createTerminal(Rectangle2D shape, double x, double y) {
42 Dir[] dirs = new Dir[] {
43 new Dir(0, shape.getMaxX() - x),
44 new Dir(1, shape.getMaxY() - y),
45 new Dir(2, x - shape.getMinX()),
46 new Dir(3, y - shape.getMinY()),
49 double[] dist = new double[4];
53 flags |= 1 << dirs[i].id;
54 dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
56 } while(i < 4 && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist));
57 return new Terminal(x, y, flags, dist, shape);
60 public static int reverseDirections(int flags) {
61 return ((flags & Constants.EAST_FLAG) == 0 ? 0 : Constants.WEST_FLAG)
62 | ((flags & Constants.SOUTH_FLAG) == 0 ? 0 : Constants.NORTH_FLAG)
63 | ((flags & Constants.WEST_FLAG) == 0 ? 0 : Constants.EAST_FLAG)
64 | ((flags & Constants.NORTH_FLAG) == 0 ? 0 : Constants.SOUTH_FLAG);
67 public static void determineAllowedDirections(Connector c) {
68 if(c.parentObstacle == null) {
69 c.allowedDirections = 0xf;
72 Dir[] dirs = new Dir[] {
73 new Dir(0, c.parentObstacle.getMaxX() - c.x),
74 new Dir(1, c.parentObstacle.getMaxY() - c.y),
75 new Dir(2, c.x - c.parentObstacle.getMinX()),
76 new Dir(3, c.y - c.parentObstacle.getMinY()),
79 double[] dist = new double[4];
80 c.allowedDirections = 0;
83 c.allowedDirections |= 1 << dirs[i].id;
84 dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
87 ((dirs[i].id == ((dirs[0].id + 2)&3) && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist)) ||
88 (dirs[i].dist <= 0.0 || dirs[i].dist*0.95 < dirs[0].dist))