--- /dev/null
+package org.simantics.diagram.connection.splitting;\r
+\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import java.awt.geom.Point2D;\r
+\r
+import org.simantics.diagram.connection.RouteGraph;\r
+import org.simantics.diagram.connection.RouteLine;\r
+import org.simantics.diagram.connection.RouteNode;\r
+import org.simantics.diagram.connection.RouteTerminal;\r
+\r
+public class SplittedRouteGraph {\r
+ public final RouteLine splitLine;\r
+ public final THashSet<RouteNode> interfaceNodes1;\r
+ public final THashSet<RouteLine> lines1; // does not contain splitLine\r
+ public final THashSet<RouteTerminal> terminals1;\r
+ public final THashSet<RouteNode> interfaceNodes2;\r
+ public final THashSet<RouteLine> lines2; // does not contain splitLine\r
+ public final THashSet<RouteTerminal> terminals2;\r
+ \r
+ public SplittedRouteGraph(RouteLine splitLine,\r
+ THashSet<RouteNode> interfaceNodes1, THashSet<RouteLine> lines1,\r
+ THashSet<RouteTerminal> terminals1,\r
+ THashSet<RouteNode> interfaceNodes2, THashSet<RouteLine> lines2,\r
+ THashSet<RouteTerminal> terminals2) {\r
+ super();\r
+ this.splitLine = splitLine;\r
+ this.interfaceNodes1 = interfaceNodes1;\r
+ this.lines1 = lines1;\r
+ this.terminals1 = terminals1;\r
+ this.interfaceNodes2 = interfaceNodes2;\r
+ this.lines2 = lines2;\r
+ this.terminals2 = terminals2;\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ StringBuilder b = new StringBuilder();\r
+ \r
+ b.append("splitLine = " + splitLine.getData() + "\n");\r
+ \r
+ b.append("interfaceNodes1 =");\r
+ for(RouteNode node : interfaceNodes1)\r
+ b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");\r
+ b.append("\n");\r
+ \r
+ b.append("lines1 =");\r
+ for(RouteLine node : lines1)\r
+ b.append(" " + node.getData());\r
+ b.append("\n");\r
+ \r
+ b.append("terminals1 =");\r
+ for(RouteTerminal node : terminals1)\r
+ b.append(" " + node.getData());\r
+ b.append("\n");\r
+ \r
+ b.append("interfaceNodes2 =");\r
+ for(RouteNode node : interfaceNodes2)\r
+ b.append(" " + node.getData() + "(" + node.getClass().getSimpleName() + ")");\r
+ b.append("\n");\r
+ \r
+ b.append("lines2 =");\r
+ for(RouteLine node : lines2)\r
+ b.append(" " + node.getData());\r
+ b.append("\n");\r
+ \r
+ b.append("terminals2 =");\r
+ for(RouteTerminal node : terminals2)\r
+ b.append(" " + node.getData());\r
+ b.append("\n");\r
+ \r
+ return b.toString();\r
+ }\r
+\r
+ public static RouteLine findNearestLine(RouteGraph rg, Point2D splitCanvasPos) {\r
+ double hi = 1000, lo = 1;\r
+ RouteLine nearestLine = null;\r
+ final double LIMIT = 0.5;\r
+ while (true) {\r
+ double tolerance = (hi + lo) * 0.5;\r
+ RouteLine line = rg.pickLine(splitCanvasPos.getX(), splitCanvasPos.getY(), tolerance);\r
+ double delta = (hi - lo) * 0.5;\r
+ if (delta < LIMIT)\r
+ return nearestLine;\r
+ if (line == null) {\r
+ lo = tolerance;\r
+ } else {\r
+ nearestLine = line;\r
+ hi = tolerance;\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @param point\r
+ * @param line\r
+ * @return the specified point instance snapped to the specified line\r
+ */\r
+ public static Point2D snapToLine(Point2D point, RouteLine line) {\r
+// line.print(System.out);\r
+// for (RoutePoint rp : line.getPoints())\r
+// System.out.println("RP: " + rp.getX() + ", " + rp.getY());\r
+ // Get exact intersection point on the line\r
+ if (line.isHorizontal()) {\r
+ point.setLocation( point.getX(), line.getPosition() );\r
+ } else {\r
+ point.setLocation( line.getPosition(), point.getY() );\r
+ }\r
+ return point;\r
+ }\r
+\r
+ /**\r
+ * @param point\r
+ * @param line\r
+ * @return new {@link Point2D} instance with specified point snapped to specified line\r
+ */\r
+ public static Point2D snappedToLine(Point2D point, RouteLine line) {\r
+ Point2D result = (Point2D) point.clone();\r
+ return snapToLine(result, line);\r
+ }\r
+\r
+}\r