]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java
Add edge geometry for detailed rendering in closer zoom levels
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / adapters / DistrictNetworkEdgeElement.java
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.Color;
4 import java.awt.Shape;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.Collection;
10 import java.util.Collections;
11
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.DistrictNetworkEdge;
14 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
15 import org.simantics.g2d.connection.handler.ConnectionHandler;
16 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
17 import org.simantics.g2d.diagram.handler.Topology.Connection;
18 import org.simantics.g2d.element.ElementClass;
19 import org.simantics.g2d.element.ElementUtils;
20 import org.simantics.g2d.element.IElement;
21 import org.simantics.g2d.element.SceneGraphNodeKey;
22 import org.simantics.g2d.element.handler.InternalSize;
23 import org.simantics.g2d.element.handler.Outline;
24 import org.simantics.g2d.element.handler.Pick;
25 import org.simantics.g2d.element.handler.SceneGraph;
26 import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
27 import org.simantics.g2d.element.handler.impl.DefaultTransform;
28 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
29 import org.simantics.maps.MapScalingTransform;
30 import org.simantics.scenegraph.g2d.G2DParentNode;
31 import org.simantics.scenegraph.g2d.nodes.SVGNode;
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class DistrictNetworkEdgeElement {
38
39     public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
40     public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
41     public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE");
42
43     public static final ElementClass CLASS =
44             ElementClass.compile(
45                     DefaultTransform.INSTANCE,
46                     DNEdgeInternalSize.INSTANCE,
47                     DNEdgeSceneGraph.INSTANCE,
48                     DNEdgeConnectionHandler.INSTANCE,
49                     SimpleElementLayers.INSTANCE,
50                     // TODO: do we need this and does it work?
51                     ConnectionSelectionOutline.INSTANCE,
52                     DistrictNetworkAdditionalColor.INSTANCE
53             ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
54
55     static final class DNEdgeSceneGraph implements SceneGraph {
56
57         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
58
59         private static final long serialVersionUID = 68135568495835923L;
60
61         @Override
62         public void init(IElement edgeElement, G2DParentNode parent) {
63             DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
64             if (edge == null) {
65                 cleanup(edgeElement);
66             } else {
67                 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
68                 if (node == null) {
69                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
70                     edgeElement.setHint(KEY_DN_EDGE_NODE, node);
71                     
72                     SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
73                     edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
74                 }
75                 
76                 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
77                 
78                 node.setDNEdge(edge);
79                 AffineTransform at = ElementUtils.getTransform(edgeElement);
80                 if (at != null)
81                     node.setTransform(at);
82             }
83         }
84
85         @Override
86         public void cleanup(IElement edge) {
87             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE);
88             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
89             edge.removeHint(KEY_DN_EDGE_NODE);
90             edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE);
91         }
92     }
93
94     static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
95
96         private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
97
98         private static final long serialVersionUID = -7346653820911240628L;
99
100         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
101
102         @Override
103         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
104             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
105             if (size == null)
106                 size = new Rectangle2D.Double();
107             if (edge != null)
108                 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null, false).getBounds2D());
109             else
110                 LOGGER.debug("Element {} does not have edge!", e);
111
112             return size;
113         }
114
115         @Override
116         public Shape getElementShape(IElement e) {
117             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
118             if (edge != null) {
119                 return DistrictNetworkEdgeNode.calculatePath(edge, null, false);
120             } else {
121                 return getBounds(e, null);
122             }
123         }
124
125         @Override
126         public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
127             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
128             if (edge != null) {
129                 Rectangle2D bounds = getBounds(s);
130                 switch (policy) {
131                 case PICK_CONTAINED_OBJECTS:    return pickContainedObjects(edge, bounds);
132                 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds);
133                 }
134                 return false;
135             }
136
137             return false;
138         }
139
140         private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
141             double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
142             double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
143             double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
144             double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
145
146             double bsminx = ModelledCRS.xToLongitude(bminx);
147             double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
148             double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
149             double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
150
151             double boundsMinY = Math.min(bsminy, bsmaxy);
152             double boundsMaxY = Math.max(bsminy, bsmaxy);
153
154             Point2D start = edge.getStartPoint();
155             Point2D end = edge.getEndPoint();
156
157             double eminx = Math.min(start.getX(), end.getX());
158             double eminy = Math.min(start.getY(), end.getY());
159             double emaxx = Math.max(start.getX(), end.getX());
160             double emaxy = Math.max(start.getY(), end.getY());
161
162             return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
163         }
164
165         private boolean pickIntersectingObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
166             double tolerance = (bounds.getHeight() + bounds.getHeight()) * 1 / MapScalingTransform.getScaleX();
167             Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint());
168             double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
169             double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
170             double ssx = ModelledCRS.xToLongitude(sx);
171             double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system
172             double distSq = line.ptSegDistSq(ssx, ssy);
173 //            System.out.println("s: " + sx + ", " + sy);
174 //            System.out.println("ss: " + ssx + ", " + ssy);
175 //            System.out.println("p1: " + edge.getStartPoint());
176 //            System.out.println("p2: " + edge.getEndPoint());
177 //            System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
178 //            System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
179             return distSq <= tolerance * tolerance;
180         }
181
182         private Rectangle2D getBounds(Shape shape) {
183             if (shape instanceof Rectangle2D)
184                 return (Rectangle2D) shape;
185             return shape.getBounds2D();
186         }
187
188     }
189
190     static class DNEdgeConnectionHandler implements ConnectionHandler {
191
192         private static final long serialVersionUID = -6882671891381761687L;
193
194         public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
195
196         @Override
197         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
198             return Collections.emptyList();
199         }
200
201         @Override
202         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
203             return Collections.emptyList();
204         }
205
206         @Override
207         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
208             return Collections.emptyList();
209         }
210
211         @Override
212         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
213             return Collections.emptyList();
214         }
215     }
216
217 }