]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RouterOld.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm1 / RouterOld.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.routing.algorithm1;\r
13 \r
14 import java.awt.geom.Path2D;\r
15 import java.awt.geom.Rectangle2D;\r
16 import java.util.ArrayList;\r
17 import java.util.Arrays;\r
18 import java.util.Comparator;\r
19 import java.util.HashMap;\r
20 import java.util.HashSet;\r
21 import java.util.Map;\r
22 import java.util.Set;\r
23 \r
24 import org.simantics.g2d.routing.Connection;\r
25 import org.simantics.g2d.routing.IRouterOld;\r
26 import org.simantics.g2d.routing.Obstacle;\r
27 \r
28 public class RouterOld implements IRouterOld {\r
29 \r
30         Map<Connection, Path2D> connections = new HashMap<Connection, Path2D>();\r
31         Set<Obstacle> obstacles = new HashSet<Obstacle>();\r
32         boolean dirty = false;  \r
33 \r
34         @Override\r
35         public void addObstacle(Obstacle obstacle) {\r
36                 obstacles.add(obstacle);\r
37                 dirty = true;\r
38         }\r
39         \r
40         @Override\r
41         public void removeObstacle(Obstacle obstacle) {\r
42                 obstacles.remove(obstacle);\r
43                 dirty = true;           \r
44         }\r
45         \r
46         @Override\r
47         public void addConnection(Connection connection) {\r
48                 connections.put(connection, null);\r
49                 dirty = true;\r
50         }\r
51         \r
52         @Override\r
53         public void removeConnection(Connection connection) {\r
54                 connections.remove(connection);\r
55                 dirty = true;           \r
56         }       \r
57 \r
58         @Override\r
59         public Path2D getPath(Connection connection) {\r
60                 if(dirty) {\r
61                         synchronized(this) {\r
62                                 if(dirty) {\r
63                                         long beginTime = System.nanoTime();\r
64                                         update();\r
65                                         long endTime = System.nanoTime();\r
66                                         System.out.println((endTime-beginTime)*1e-6 + "ms");\r
67                                         dirty = false;\r
68                                 }\r
69                         }\r
70                 }\r
71                 return connections.get(connection);\r
72         }\r
73 \r
74         private void update() {\r
75                 ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(obstacles.size());\r
76                 for(Obstacle obs : obstacles) {\r
77                         Rectangle2D shape = obs.shape;\r
78                         rectangles.add(new Rectangle(\r
79                                 shape.getMinX(),\r
80                                 shape.getMinY(),\r
81                                 shape.getMaxX(),\r
82                                 shape.getMaxY()));\r
83                 }\r
84                 Connection[] carray = \r
85                         connections.keySet().toArray(new Connection[connections.size()]);\r
86                 Arrays.sort(carray, new Comparator<Connection>() {\r
87 \r
88                         @Override\r
89                         public int compare(Connection o1, Connection o2) {\r
90                                 double l1 = o1.minLength();\r
91                                 double l2 = o2.minLength();\r
92                                 if(l1 < l2)\r
93                                         return -1;\r
94                                 if(l1 > l2)\r
95                                         return 1;\r
96                                 if(o1.routePath.length < o2.routePath.length)\r
97                                         return -1;\r
98                                 if(o1.routePath.length > o2.routePath.length)\r
99                                         return 1;\r
100                                 for(int i=0;i<o1.routePath.length;++i) {\r
101                                         if(o1.routePath[i] < o2.routePath[i])\r
102                                                 return -1;\r
103                                         if(o1.routePath[i] > o2.routePath[i])\r
104                                                 return 1;\r
105                                 }\r
106                                 return 0;\r
107                         }\r
108                         \r
109                 });\r
110                 //System.out.println("----------------------------------------------------------");\r
111                 ArrayList<Path2D> lines = new ArrayList<Path2D>(connections.size());            \r
112                 for(Connection c : carray) {\r
113 //                      StaticRouter router = new StaticRouter(rectangles, lines);\r
114 //                      Path2D path = router.route(c);\r
115 //                      connections.put(c, path);\r
116 //                      if(path != null)\r
117 //                              lines.add(path);\r
118                 }\r
119         }\r
120 }\r