]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/algorithm1/RouterOld.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / algorithm1 / RouterOld.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 import java.awt.geom.Rectangle2D;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.simantics.g2d.routing.Connection;
25 import org.simantics.g2d.routing.IRouterOld;
26 import org.simantics.g2d.routing.Obstacle;
27
28 public class RouterOld implements IRouterOld {
29
30         Map<Connection, Path2D> connections = new HashMap<Connection, Path2D>();
31         Set<Obstacle> obstacles = new HashSet<Obstacle>();
32         boolean dirty = false;  
33
34         @Override
35         public void addObstacle(Obstacle obstacle) {
36                 obstacles.add(obstacle);
37                 dirty = true;
38         }
39         
40         @Override
41         public void removeObstacle(Obstacle obstacle) {
42                 obstacles.remove(obstacle);
43                 dirty = true;           
44         }
45         
46         @Override
47         public void addConnection(Connection connection) {
48                 connections.put(connection, null);
49                 dirty = true;
50         }
51         
52         @Override
53         public void removeConnection(Connection connection) {
54                 connections.remove(connection);
55                 dirty = true;           
56         }       
57
58         @Override
59         public Path2D getPath(Connection connection) {
60                 if(dirty) {
61                         synchronized(this) {
62                                 if(dirty) {
63                                         long beginTime = System.nanoTime();
64                                         update();
65                                         long endTime = System.nanoTime();
66                                         System.out.println((endTime-beginTime)*1e-6 + "ms");
67                                         dirty = false;
68                                 }
69                         }
70                 }
71                 return connections.get(connection);
72         }
73
74         private void update() {
75                 ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(obstacles.size());
76                 for(Obstacle obs : obstacles) {
77                         Rectangle2D shape = obs.shape;
78                         rectangles.add(new Rectangle(
79                                 shape.getMinX(),
80                                 shape.getMinY(),
81                                 shape.getMaxX(),
82                                 shape.getMaxY()));
83                 }
84                 Connection[] carray = 
85                         connections.keySet().toArray(new Connection[connections.size()]);
86                 Arrays.sort(carray, new Comparator<Connection>() {
87
88                         @Override
89                         public int compare(Connection o1, Connection o2) {
90                                 double l1 = o1.minLength();
91                                 double l2 = o2.minLength();
92                                 if(l1 < l2)
93                                         return -1;
94                                 if(l1 > l2)
95                                         return 1;
96                                 if(o1.routePath.length < o2.routePath.length)
97                                         return -1;
98                                 if(o1.routePath.length > o2.routePath.length)
99                                         return 1;
100                                 for(int i=0;i<o1.routePath.length;++i) {
101                                         if(o1.routePath[i] < o2.routePath[i])
102                                                 return -1;
103                                         if(o1.routePath[i] > o2.routePath[i])
104                                                 return 1;
105                                 }
106                                 return 0;
107                         }
108                         
109                 });
110                 //System.out.println("----------------------------------------------------------");
111                 ArrayList<Path2D> lines = new ArrayList<Path2D>(connections.size());            
112                 for(Connection c : carray) {
113 //                      StaticRouter router = new StaticRouter(rectangles, lines);
114 //                      Path2D path = router.route(c);
115 //                      connections.put(c, path);
116 //                      if(path != null)
117 //                              lines.add(path);
118                 }
119         }
120 }