]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RoutePencil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm1 / RoutePencil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.routing.algorithm1;
13
14 import java.awt.geom.Path2D;
15
16 import org.simantics.g2d.routing.Constants;
17
18 public class RoutePencil extends Rectangle {
19
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;
26
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;
32                 this.parent = parent;
33                 if(parent == null) {
34                         distanceConstant = 0.0;
35                         distanceX = 0.0;
36                         distanceY = 0.0;
37                 }
38                 else {
39                         switch(direction.getId()) {
40                         case Constants.EAST: {
41                                 distanceX = 1.0;
42                                 distanceY = parent.distanceY;
43                                 double x = Math.min(parent.x1, x0);
44                                 distanceConstant = parent.distanceConstant + 
45                                         x * (parent.distanceX - distanceX);
46                         } break;
47                         case Constants.SOUTH: {
48                                 distanceX = parent.distanceX;
49                                 distanceY = 1.0;                                
50                                 double y = Math.min(parent.y1, y0);
51                                 distanceConstant = parent.distanceConstant + 
52                                         y * (parent.distanceY - distanceY);
53                         } break;
54                         case Constants.WEST: {
55                                 distanceX = -1.0;
56                                 distanceY = parent.distanceY;
57                                 double x = Math.max(parent.x0, x1);
58                                 distanceConstant = parent.distanceConstant + 
59                                         x * (parent.distanceX - distanceX);
60                         } break;
61                         case Constants.NORTH: {
62                                 distanceX = parent.distanceX;
63                                 distanceY = -1.0;                               
64                                 double y = Math.max(parent.y0, y1);
65                                 distanceConstant = parent.distanceConstant + 
66                                         y * (parent.distanceY - distanceY);
67                         } break;
68                         }
69                 }
70         }
71         
72         double distance(double x, double y) {
73                 double result = distanceConstant;
74                 if(x < x0)
75                         result += distanceX * x0 + (x0 - x);
76                 else if(x > x1)
77                         result += distanceX * x1 + (x - x1);
78                 else
79                         result += distanceX * x;
80                 if(y < y0)
81                         result += distanceY * y0 + (y0 - y);
82                 else if(x > x1)
83                         result += distanceY * y1 + (y - y1);
84                 else
85                         result += distanceY * y;
86                 return result;
87         }
88         
89         double distance() {
90                 return distanceConstant + distanceX * x0 + distanceY * y0;
91         }
92         
93         void createPath(Path2D path, double x, double y) {
94                 if(parent == null) {
95                         /*switch(direction.getId()) {
96                         case Constants.EAST:
97                                 path.moveTo(x0, y);
98                                 break;
99                         case Constants.SOUTH:
100                                 path.moveTo(x, y0);
101                                 break;
102                         case Constants.WEST:
103                                 path.moveTo(x1, y);
104                                 break;
105                         case Constants.NORTH:
106                                 path.moveTo(x, y1);
107                                 break;
108                         }               
109                         */
110                         path.moveTo(makeFinite(x), makeFinite(y));
111                 }
112                 else {
113                         if(parent.contains(x, y))
114                                 parent.createPath(path, x, y);
115                         else {
116                                 switch(direction.getId()) {
117                                 case Constants.EAST:
118                                         if(parent.distanceX < 1.0)
119                                                 parent.createPath(path, parent.x1, y);
120                                         else 
121                                                 parent.createPath(path, (parent.x0+parent.x1)*0.5, y);
122                                         break;
123                                 case Constants.SOUTH:
124                                         if(parent.distanceY < 1.0)
125                                                 parent.createPath(path, x, parent.y1);                                          
126                                         else 
127                                                 parent.createPath(path, x, (parent.y0+parent.y1)*0.5);
128                                         break;
129                                 case Constants.WEST:
130                                         if(parent.distanceX > -1.0)
131                                                 parent.createPath(path, parent.x0, y);
132                                         else 
133                                                 parent.createPath(path, (parent.x0+parent.x1)*0.5, y);
134                                         break;
135                                 case Constants.NORTH:
136                                         if(parent.distanceY > -1.0)
137                                                 parent.createPath(path, x, parent.y0);
138                                         else 
139                                                 parent.createPath(path, x, (parent.y0+parent.y1)*0.5);
140                                         break;
141                                 }                       
142                                 path.lineTo(makeFinite(x), makeFinite(y));
143                         }
144                 }               
145         }
146         
147         static double makeFinite(double x) {
148                 if(x == Double.POSITIVE_INFINITY)
149                         return 10000.0;
150                 if(x == Double.NEGATIVE_INFINITY)
151                         return -10000.0;
152                 return x;
153         }
154         
155         public Path2D createPath(double x, double y) {
156                 Path2D path = new Path2D.Double();
157                 createPath(path, x, y);
158                 return path;
159         }
160         
161         @Override
162         public String toString() {
163                 if(parent == null)
164                         return Integer.toString(direction.getId());
165                 else
166                         return parent.toString() + " " + Integer.toString(direction.getId());
167         }
168 }