1 package org.simantics.diagram.connection.splitting;
3 import gnu.trove.set.hash.THashSet;
5 import java.awt.geom.Point2D;
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;
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;
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) {
27 this.splitLine = splitLine;
28 this.interfaceNodes1 = interfaceNodes1;
30 this.terminals1 = terminals1;
31 this.interfaceNodes2 = interfaceNodes2;
33 this.terminals2 = terminals2;
37 public String toString() {
38 StringBuilder b = new StringBuilder();
40 b.append("splitLine = " + splitLine.getData() + "\n");
42 b.append("interfaceNodes1 =");
43 for(RouteNode node : interfaceNodes1)
44 b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");
48 for(RouteLine node : lines1)
49 b.append(" " + node.getData());
52 b.append("terminals1 =");
53 for(RouteTerminal node : terminals1)
54 b.append(" " + node.getData());
57 b.append("interfaceNodes2 =");
58 for(RouteNode node : interfaceNodes2)
59 b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");
63 for(RouteLine node : lines2)
64 b.append(" " + node.getData());
67 b.append("terminals2 =");
68 for(RouteTerminal node : terminals2)
69 b.append(" " + node.getData());
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;
80 double tolerance = (hi + lo) * 0.5;
81 RouteLine line = rg.pickLine(splitCanvasPos.getX(), splitCanvasPos.getY(), tolerance);
82 double delta = (hi - lo) * 0.5;
97 * @return the specified point instance snapped to the specified line
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() );
107 point.setLocation( line.getPosition(), point.getY() );
115 * @return new {@link Point2D} instance with specified point snapped to specified line
117 public static Point2D snappedToLine(Point2D point, RouteLine line) {
118 Point2D result = (Point2D) point.clone();
119 return snapToLine(result, line);