+
+ @Override
+ public Shape getElementShape(IElement e) {
+ DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
+ if (edgeNode != null) {
+ return edgeNode.getPath();
+ } else {
+ return getBounds(e, null);
+ }
+ }
+
+ @Override
+ public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
+ DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
+ if (edge != null) {
+ Rectangle2D bounds = getBounds(s);
+ switch (policy) {
+ case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
+ case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds);
+ }
+ return false;
+ }
+
+ return false;
+ }
+
+ private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
+ double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
+ double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
+ double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
+ double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
+
+ double bsminx = ModelledCRS.xToLongitude(bminx);
+ double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
+ double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
+ double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
+
+ double boundsMinY = Math.min(bsminy, bsmaxy);
+ double boundsMaxY = Math.max(bsminy, bsmaxy);
+
+ Point2D start = edge.getStartPoint();
+ Point2D end = edge.getEndPoint();
+
+ double eminx = Math.min(start.getX(), end.getX());
+ double eminy = Math.min(start.getY(), end.getY());
+ double emaxx = Math.max(start.getX(), end.getX());
+ double emaxy = Math.max(start.getY(), end.getY());
+
+ return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
+ }
+
+ private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) {
+ double dx = bounds.getWidth() / MapScalingTransform.getScaleX();
+ double dy = bounds.getHeight() / MapScalingTransform.getScaleY();
+
+ // Half the diagonal + half of the line width
+ DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE);
+ AffineTransform at = NodeUtil.getLocalToGlobalTransform(node);
+
+ Path2D path = node.getPath();
+ if (path == null)
+ return false;
+
+ double lineWidth = node.getStrokeWidth(at, true);
+ double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2;
+
+ double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
+ double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
+
+ double coords[] = new double[6];
+ Point2D prevPoint = new Point2D.Double(), curPoint = new Point2D.Double();
+ Line2D line = new Line2D.Double();
+ for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) {
+ int type = it.currentSegment(coords);
+ switch (type) {
+ case PathIterator.SEG_MOVETO:
+ curPoint.setLocation(coords[0], coords[1]);
+ break;
+ case PathIterator.SEG_LINETO:
+ prevPoint.setLocation(curPoint);
+ curPoint.setLocation(coords[0], coords[1]);
+ line.setLine(prevPoint, curPoint);
+ double distSq = line.ptSegDistSq(sx, sy);
+// System.out.println("s: " + sx + ", " + sy);
+// System.out.println("ss: " + ssx + ", " + ssy);
+// System.out.println("p1: " + edge.getStartPoint());
+// System.out.println("p2: " + edge.getEndPoint());
+// System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
+// System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
+ if (distSq <= tolerance * tolerance)
+ return true;
+ break;
+ default:
+ LOGGER.error("Invalid edge path", new IllegalStateException());
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ private Rectangle2D getBounds(Shape shape) {
+ if (shape instanceof Rectangle2D)
+ return (Rectangle2D) shape;
+ return shape.getBounds2D();
+ }
+