+
+ @Override
+ public Shape getElementShape(IElement e) {
+ DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
+ if (edge != null) {
+ return DistrictNetworkEdgeNode.calculatePath(edge, null);
+ } 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(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(DistrictNetworkEdge edge, Rectangle2D bounds) {
+ double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX();
+ Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint());
+ double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
+ double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
+ double ssx = ModelledCRS.xToLongitude(sx);
+ double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system
+ double distSq = line.ptSegDistSq(ssx, ssy);
+// 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);
+ return distSq <= tolerance * tolerance;
+ }
+
+ private Rectangle2D getBounds(Shape shape) {
+ if (shape instanceof Rectangle2D)
+ return (Rectangle2D) shape;
+ return shape.getBounds2D();
+ }
+