]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/splitting/SplittedRouteGraph.java
ae79dc019b3cb320f883315599a20952e2fb420d
[simantics/platform.git] / bundles / org.simantics.diagram.connection / src / org / simantics / diagram / connection / splitting / SplittedRouteGraph.java
1 package org.simantics.diagram.connection.splitting;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.awt.geom.Point2D;
6
7 import org.simantics.diagram.connection.RouteGraph;
8 import org.simantics.diagram.connection.RouteLine;
9 import org.simantics.diagram.connection.RouteNode;
10 import org.simantics.diagram.connection.RouteTerminal;
11
12 public class SplittedRouteGraph {
13     public final RouteLine splitLine;
14     public final THashSet<RouteNode> interfaceNodes1;
15     public final THashSet<RouteLine> lines1; // does not contain splitLine
16     public final THashSet<RouteTerminal> terminals1;
17     public final THashSet<RouteNode> interfaceNodes2;
18     public final THashSet<RouteLine> lines2; // does not contain splitLine
19     public final THashSet<RouteTerminal> terminals2;
20     
21     public SplittedRouteGraph(RouteLine splitLine,
22             THashSet<RouteNode> interfaceNodes1, THashSet<RouteLine> lines1,
23             THashSet<RouteTerminal> terminals1,
24             THashSet<RouteNode> interfaceNodes2, THashSet<RouteLine> lines2,
25             THashSet<RouteTerminal> terminals2) {
26         super();
27         this.splitLine = splitLine;
28         this.interfaceNodes1 = interfaceNodes1;
29         this.lines1 = lines1;
30         this.terminals1 = terminals1;
31         this.interfaceNodes2 = interfaceNodes2;
32         this.lines2 = lines2;
33         this.terminals2 = terminals2;
34     }
35     
36     @Override
37     public String toString() {
38         StringBuilder b = new StringBuilder();
39         
40         b.append("splitLine = " + splitLine.getData() + "\n");
41         
42         b.append("interfaceNodes1 =");
43         for(RouteNode node : interfaceNodes1)
44             b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");
45         b.append("\n");
46         
47         b.append("lines1 =");
48         for(RouteLine node : lines1)
49             b.append(" " + node.getData());
50         b.append("\n");
51         
52         b.append("terminals1 =");
53         for(RouteTerminal node : terminals1)
54             b.append(" " + node.getData());
55         b.append("\n");
56         
57         b.append("interfaceNodes2 =");
58         for(RouteNode node : interfaceNodes2)
59             b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");
60         b.append("\n");
61         
62         b.append("lines2 =");
63         for(RouteLine node : lines2)
64             b.append(" " + node.getData());
65         b.append("\n");
66         
67         b.append("terminals2 =");
68         for(RouteTerminal node : terminals2)
69             b.append(" " + node.getData());
70         b.append("\n");
71         
72         return b.toString();
73     }
74
75     public static RouteLine findNearestLine(RouteGraph rg, Point2D splitCanvasPos) {
76         double hi = 1000, lo = 1;
77         RouteLine nearestLine = null;
78         final double LIMIT = 0.5;
79         while (true) {
80             double tolerance = (hi + lo) * 0.5;
81             RouteLine line = rg.pickLine(splitCanvasPos.getX(), splitCanvasPos.getY(), tolerance);
82             double delta = (hi - lo) * 0.5;
83             if (delta < LIMIT)
84                 return nearestLine;
85             if (line == null) {
86                 lo = tolerance;
87             } else {
88                 nearestLine = line;
89                 hi = tolerance;
90             }
91         }
92     }
93
94     /**
95      * @param point
96      * @param line
97      * @return the specified point instance snapped to the specified line
98      */
99     public static Point2D snapToLine(Point2D point, RouteLine line) {
100 //        line.print(System.out);
101 //        for (RoutePoint rp : line.getPoints())
102 //            System.out.println("RP: " + rp.getX() + ", " + rp.getY());
103         // Get exact intersection point on the line
104         if (line.isHorizontal()) {
105             point.setLocation( point.getX(), line.getPosition() );
106         } else {
107             point.setLocation( line.getPosition(), point.getY() );
108         }
109         return point;
110     }
111
112     /**
113      * @param point
114      * @param line
115      * @return new {@link Point2D} instance with specified point snapped to specified line
116      */
117     public static Point2D snappedToLine(Point2D point, RouteLine line) {
118         Point2D result = (Point2D) point.clone();
119         return snapToLine(result, line);
120     }
121
122 }