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.algorithm1;
14 import java.awt.geom.Path2D;
16 import org.simantics.g2d.routing.Constants;
18 public class RoutePencil extends Rectangle {
20 public final Direction direction;
21 public final RoutePencil parent;
22 public double penalty;
23 public double distanceConstant;
24 public double distanceX;
25 public double distanceY;
27 public RoutePencil(double x0, double y0, double x1, double y1,
28 double penalty, Direction direction, RoutePencil parent) {
29 super(x0, y0, x1, y1);
30 this.penalty = penalty;
31 this.direction = direction;
34 distanceConstant = 0.0;
39 switch(direction.getId()) {
40 case Constants.EAST: {
42 distanceY = parent.distanceY;
43 double x = Math.min(parent.x1, x0);
44 distanceConstant = parent.distanceConstant +
45 x * (parent.distanceX - distanceX);
47 case Constants.SOUTH: {
48 distanceX = parent.distanceX;
50 double y = Math.min(parent.y1, y0);
51 distanceConstant = parent.distanceConstant +
52 y * (parent.distanceY - distanceY);
54 case Constants.WEST: {
56 distanceY = parent.distanceY;
57 double x = Math.max(parent.x0, x1);
58 distanceConstant = parent.distanceConstant +
59 x * (parent.distanceX - distanceX);
61 case Constants.NORTH: {
62 distanceX = parent.distanceX;
64 double y = Math.max(parent.y0, y1);
65 distanceConstant = parent.distanceConstant +
66 y * (parent.distanceY - distanceY);
72 double distance(double x, double y) {
73 double result = distanceConstant;
75 result += distanceX * x0 + (x0 - x);
77 result += distanceX * x1 + (x - x1);
79 result += distanceX * x;
81 result += distanceY * y0 + (y0 - y);
83 result += distanceY * y1 + (y - y1);
85 result += distanceY * y;
90 return distanceConstant + distanceX * x0 + distanceY * y0;
93 void createPath(Path2D path, double x, double y) {
95 /*switch(direction.getId()) {
105 case Constants.NORTH:
110 path.moveTo(makeFinite(x), makeFinite(y));
113 if(parent.contains(x, y))
114 parent.createPath(path, x, y);
116 switch(direction.getId()) {
118 if(parent.distanceX < 1.0)
119 parent.createPath(path, parent.x1, y);
121 parent.createPath(path, (parent.x0+parent.x1)*0.5, y);
123 case Constants.SOUTH:
124 if(parent.distanceY < 1.0)
125 parent.createPath(path, x, parent.y1);
127 parent.createPath(path, x, (parent.y0+parent.y1)*0.5);
130 if(parent.distanceX > -1.0)
131 parent.createPath(path, parent.x0, y);
133 parent.createPath(path, (parent.x0+parent.x1)*0.5, y);
135 case Constants.NORTH:
136 if(parent.distanceY > -1.0)
137 parent.createPath(path, x, parent.y0);
139 parent.createPath(path, x, (parent.y0+parent.y1)*0.5);
142 path.lineTo(makeFinite(x), makeFinite(y));
147 static double makeFinite(double x) {
148 if(x == Double.POSITIVE_INFINITY)
150 if(x == Double.NEGATIVE_INFINITY)
155 public Path2D createPath(double x, double y) {
156 Path2D path = new Path2D.Double();
157 createPath(path, x, y);
162 public String toString() {
164 return Integer.toString(direction.getId());
166 return parent.toString() + " " + Integer.toString(direction.getId());