]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/ConnectionDirectionUtil.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / ConnectionDirectionUtil.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;
13
14 import java.awt.geom.Rectangle2D;
15 import java.util.Arrays;
16
17 import org.simantics.g2d.routing.IConnection.Connector;
18
19 public class ConnectionDirectionUtil {
20         
21         public static class Dir implements Comparable<Dir> {
22                 int id;
23                 double dist;
24                 
25                 public Dir(int id, double dist) {
26                         this.id = id;
27                         this.dist = dist;
28                 }
29
30                 @Override
31                 public int compareTo(Dir o) {
32                         if(dist < o.dist)
33                                 return -1;
34                         if(o.dist < dist)
35                                 return 1;
36                         return 0;
37                 }       
38                 
39         }
40         
41         public static Terminal createTerminal(Rectangle2D shape, double x, double y) {
42                 Dir[] dirs = new Dir[] {
43                         new Dir(0, shape.getMaxX() - x),
44                         new Dir(1, shape.getMaxY() - y),
45                         new Dir(2, x - shape.getMinX()),
46                         new Dir(3, y - shape.getMinY()),
47                 };
48                 Arrays.sort(dirs);
49                 double[] dist = new double[4];
50                 int flags = 0;
51                 int i=0;
52                 do {
53                         flags |= 1 << dirs[i].id;
54                         dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
55                         ++i;
56                 } while(i < 4 && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist));
57                 return new Terminal(x, y, flags, dist, shape);
58         }
59         
60         public static int reverseDirections(int flags) {
61                 return ((flags & Constants.EAST_FLAG) == 0 ? 0 : Constants.WEST_FLAG)
62                          | ((flags & Constants.SOUTH_FLAG) == 0 ? 0 : Constants.NORTH_FLAG)
63                          | ((flags & Constants.WEST_FLAG) == 0 ? 0 : Constants.EAST_FLAG)
64                          | ((flags & Constants.NORTH_FLAG) == 0 ? 0 : Constants.SOUTH_FLAG);
65         }
66         
67         public static void determineAllowedDirections(Connector c) {
68             if(c.parentObstacle == null) {
69                 c.allowedDirections = 0xf;
70                 return;
71             }
72             Dir[] dirs = new Dir[] {
73             new Dir(0, c.parentObstacle.getMaxX() - c.x),
74             new Dir(1, c.parentObstacle.getMaxY() - c.y),
75             new Dir(2, c.x - c.parentObstacle.getMinX()),
76             new Dir(3, c.y - c.parentObstacle.getMinY()),
77         };
78         Arrays.sort(dirs);
79         double[] dist = new double[4];
80         c.allowedDirections = 0;
81         int i=0;
82         do {
83             c.allowedDirections |= 1 << dirs[i].id;
84             dist[dirs[i].id] = Math.max(0.0, dirs[i].dist+1e-6);
85             ++i;
86         } while(i < 4 &&
87                         ((dirs[i].id == ((dirs[0].id + 2)&3) && (dirs[i].dist <= 0.0 || dirs[i].dist*0.9 < dirs[0].dist)) ||
88                         (dirs[i].dist <= 0.0 || dirs[i].dist*0.95 < dirs[0].dist))
89                         );
90         }
91 }