]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm2/Router2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm2 / Router2.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.algorithm2;
13
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Path2D;
16 import java.awt.geom.PathIterator;
17 import java.awt.geom.Rectangle2D;
18 import java.util.Arrays;
19
20 import org.simantics.g2d.routing.Constants;
21 import org.simantics.g2d.routing.IGraphModel;
22 import org.simantics.g2d.routing.IRouter;
23 import org.simantics.g2d.routing.Terminal;
24
25 public class Router2 implements IRouter {
26
27     static int getSomeDirection(int mask) {
28         if(mask == 0)
29             return 0;
30         int result = 0;
31         while((mask & 1) == 0) {
32             ++result;
33             mask >>= 1;
34         }
35         return result;
36     }
37     
38     @Override
39     public void update(IGraphModel model) {
40         LocalRouter localRouter = new LocalRouter(false);
41         for(Object c : model.getConnections()) {
42             Terminal begin = model.getBeginTerminal(c);        
43             Terminal end = model.getEndTerminal(c);
44             double[] points = model.getRoutePoints(c);
45             
46             if(begin == null) {
47                 if(points.length < 2)
48                         continue;
49                         begin = new Terminal(points[0], points[1], 0x1, Terminal.ZEROS, 
50                                 new Rectangle2D.Double(points[0], points[1], 0.0, 0.0));
51                         points = Arrays.copyOfRange(points, 2, points.length);
52                 }
53                 if(end == null) {
54                         if(points.length < 2)
55                         continue;
56                         end = new Terminal(points[points.length-2], points[points.length-1], 0xf, Terminal.ZEROS,
57                                 new Rectangle2D.Double(points[points.length-2], points[points.length-1], 0.0, 0.0));
58                         points = Arrays.copyOf(points, points.length-2);
59                 }
60
61                 double bestLength = Double.POSITIVE_INFINITY;
62                 Path2D bestPath = null;
63                 
64                 for(int sDir : Constants.POSSIBLE_DIRECTIONS[begin.directions])
65                         for(int tDir : Constants.POSSIBLE_DIRECTIONS[end.directions]) {
66                                 localRouter.sx = begin.x;
67                                 localRouter.sy = begin.y;
68                                 localRouter.aMinX = begin.parentObstacle.getMinX();
69                                 localRouter.aMinY = begin.parentObstacle.getMinY();
70                                 localRouter.aMaxX = begin.parentObstacle.getMaxX();
71                                 localRouter.aMaxY = begin.parentObstacle.getMaxY();
72                                 localRouter.sourceDirection = sDir;
73
74                                 localRouter.tx = end.x;
75                                 localRouter.ty = end.y;
76                                 localRouter.bMinX = end.parentObstacle.getMinX();
77                                 localRouter.bMinY = end.parentObstacle.getMinY();
78                                 localRouter.bMaxX = end.parentObstacle.getMaxX();
79                                 localRouter.bMaxY = end.parentObstacle.getMaxY();
80                                 localRouter.targetDirection = tDir;
81
82                                 localRouter.route();
83
84                                 double length = pathLength(localRouter.path);
85                                 if(length < bestLength) {
86                                         bestLength = length;
87                                         bestPath = localRouter.path;
88                                 }  
89                         }
90             
91                 if(bestPath != null)
92                         model.setPath(c, bestPath);
93         }
94     }
95     
96     final static AffineTransform IDENTITY = new AffineTransform();
97
98     static double pathLength(Path2D path) {
99         double length = 0.0;
100         PathIterator it = path.getPathIterator(IDENTITY);
101         double[] temp = new double[6];
102         double x=0.0, y=0.0;
103         double bendCount = 0.0;
104         while(!it.isDone()) {
105                 bendCount += 1.0;
106                 if(it.currentSegment(temp) != PathIterator.SEG_MOVETO)
107                         length += Math.abs(x - temp[0] + y - temp[1]);
108                 x = temp[0];
109                         y = temp[1];
110                         it.next();
111         }
112         return length * (6.0 + bendCount);
113     }
114     
115 }