From 7ebf5a4d72468b19d55cb79c0494d0538081d2ff Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Wed, 20 Nov 2019 17:20:11 +0200 Subject: [PATCH] Use detailed geometry in drawing info labels, symbols and picking. gitlab #70 gitlab #47 Change-Id: Ie8189751f130c47fbd1c9686eb46252efc038412 --- .../adapters/DistrictNetworkEdgeElement.java | 16 +-- .../ui/nodes/DistrictNetworkEdgeNode.java | 129 ++++++++++-------- .../ui/nodes/DistrictNetworkNodeUtils.java | 71 +++++++++- .../nodes/DistrictNetworkStaticInfoNode.java | 72 +++++----- .../DistrictNetworkStaticInfoStyle.java | 11 +- 5 files changed, 197 insertions(+), 102 deletions(-) diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java index 8af6b20a..20c86d8d 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java @@ -102,17 +102,13 @@ public class DistrictNetworkEdgeElement { public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize(); - private ThreadLocal path = new ThreadLocal() { - protected Path2D initialValue() { return new Path2D.Double(); } - }; - @Override public Rectangle2D getBounds(IElement e, Rectangle2D size) { - DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); + DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE); if (size == null) size = new Rectangle2D.Double(); - if (edge != null) - size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, path.get(), true).getBounds2D()); + if (edgeNode != null) + size.setFrame(edgeNode.getBoundsInLocal()); else LOGGER.debug("Element {} does not have edge!", e); @@ -121,9 +117,9 @@ public class DistrictNetworkEdgeElement { @Override public Shape getElementShape(IElement e) { - DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); - if (edge != null) { - return DistrictNetworkEdgeNode.calculatePath(edge, null, true); + DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE); + if (edgeNode != null) { + return edgeNode.getPath(); } else { return getBounds(e, null); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java index e112c536..1c677316 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -32,6 +32,9 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection private DistrictNetworkEdge edge; private Rectangle2D bounds; private transient Path2D path; + private transient Path2D detailedPath; + + private transient int zoomLevel = 0; private static boolean scaleStroke = true; private Color color; @@ -47,11 +50,15 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height); private transient Point2D centerPoint; + private transient Point2D detailedCenterPoint; + private transient Point2D direction; + private transient Point2D detailedDirection; + private transient Rectangle2D symbolRect; private transient AffineTransform symbolTransform; private boolean hidden = false; - + private Double arrowLength; private static double startX; @@ -59,6 +66,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection private static double endX; private static double endY; + private Path2D arrowPath; + @Override public void init() { } @@ -82,6 +91,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection } } + zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL); + if (!hidden) { Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); @@ -96,8 +107,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection bs = STROKE; } - int zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL); - path = calculatePath(edge, path, zoomLevel > 13); + Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path; if (isSelected()) { g2d.setColor(SELECTION_COLOR); @@ -114,56 +124,50 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection g2d.setColor(Color.BLACK); float lw = STROKE.getLineWidth() / (float)scale; g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); - + double l = arrowLength; double w = 2 * (double) lw * Math.signum(l); if (Math.abs(w) > Math.abs(l)) w = l; double offset = 2 * (double) lw; - - double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2; - double deltaX = endX - startX, deltaY = endY - startY; - double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY); - deltaX /= length; - deltaY /= length; - + + Point2D centerPoint = getCenterPoint(zoomLevel); + Point2D direction = getDirection(zoomLevel); + double centerX = centerPoint.getX(), centerY = centerPoint.getY(); + double deltaX = direction.getX(), deltaY = direction.getY(); + double x0 = centerX - l/2 * deltaX + offset * deltaY; double y0 = centerY - l/2 * deltaY - offset * deltaX; double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY; double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX; - + g2d.draw(new Line2D.Double(x0, y0, x1, y1)); - - Path2D path = new Path2D.Double(); - path.moveTo(x1 + w * deltaX, y1 + w * deltaY); - path.lineTo(x1 + w * deltaY, y1 - w * deltaX); - path.lineTo(x1 - w * deltaY, y1 + w * deltaX); - path.closePath(); - g2d.fill(path); + + arrowPath = new Path2D.Double(); + arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY); + arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX); + arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX); + arrowPath.closePath(); + g2d.fill(arrowPath); } - + // Reset g2d.setStroke(oldStroke); g2d.setColor(oldColor); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); } + Point2D centerP = getCenterPoint(zoomLevel); for (INode nn : getNodes()) { - G2DNode g2dNode = (G2DNode)nn; + G2DNode g2dNode = (G2DNode) nn; if (g2dNode instanceof SVGNode) { - // Render SVG symbol - double viewScaleRecip = 10; - if (scaleStroke) { - viewScaleRecip /= scale; - } - // If the node is hidden from the start, then the center point cannot be calculated yet. - Point2D p = getCenterPoint(); - if (p == null) + if (!isDefined(centerP)) break; - symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip); + // Render SVG symbol + double viewScaleRecip = scaleStroke ? 10 / scale : 10; + symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip); symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform); - g2dNode.setTransform(symbolTransform); } g2dNode.render(g2d); @@ -173,6 +177,14 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection g2d.setTransform(ot); } + static boolean isDefined(Point2D p) { + return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY())); + } + + boolean renderDetailed(int zoomLevel) { + return zoomLevel > 13; + } + public float getStrokeWidth(AffineTransform tr, boolean selection) { double scale = DistrictNetworkNodeUtils.getScale(tr); float width = STROKE.getLineWidth() * getStrokeWidth(scale); @@ -192,31 +204,16 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection } public Path2D getPath() { - return path; + return renderDetailed(zoomLevel) ? detailedPath : path; } - - private Point2D getCenterPoint() { - if (path == null) - return null; - if (centerPoint == null) - centerPoint = new Point2D.Double(); - Rectangle2D bounds = path.getBounds2D(); - centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY()); - return centerPoint; + + public Point2D getCenterPoint(int zoomLevel) { + return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint; } -// public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) { -// // Convert to screen coordinates -// double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX()); -// double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics -// double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX()); -// double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics -// -// if (result == null) -// result = new Line2D.Double(); -// result.setLine(startX, startY, endX, endY); -// return result; -// } + public Point2D getDirection(int zoomLevel) { + return renderDetailed(zoomLevel) ? detailedDirection : direction; + } public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) { startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX()); @@ -234,7 +231,6 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection double[] detailedGeometry = edge.getGeometry(); if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) { // ok, lets do this - for (int i = 0; i < detailedGeometry.length; i += 2) { double x = ModelledCRS.longitudeToX(detailedGeometry[i]); double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics @@ -262,12 +258,31 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection bounds = calculateBounds(oldBounds); } + /** + * Calculates conservative bounds of the node provided by + * detailedPath. These might be larger than the simplified bounds + * of path but the conservative bounds ensure that spatial + * optimizations work properly. + */ private Rectangle2D calculateBounds(Rectangle2D rect) { - return calculatePath(edge, null, true).getBounds2D(); + if (detailedPath == null) + detailedPath = calculatePath(edge, detailedPath, true); + return detailedPath.getBounds2D(); } public void setDNEdge(DistrictNetworkEdge edge) { this.edge = edge; + path = calculatePath(edge, path, false); + detailedPath = calculatePath(edge, detailedPath, true); + + centerPoint = new Point2D.Double(); + detailedCenterPoint = new Point2D.Double(); + direction = new Point2D.Double(); + detailedDirection = new Point2D.Double(); + + DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction); + DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection); + updateBounds(); } @@ -288,7 +303,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection public void setDynamicColor(Color color) { this.dynamicColor = color; } - + @PropertySetter(value = "arrowLength") public void setArrowLength(Double length) { arrowLength = length; @@ -300,10 +315,10 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection if (nn instanceof SVGNode) ((SVGNode)nn).setData(value); } - @PropertySetter(value = "hidden") public void setHidden(Boolean value) { this.hidden = value; } + } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java index 0efab431..2cc0ee96 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkNodeUtils.java @@ -1,6 +1,8 @@ package org.simantics.district.network.ui.nodes; import java.awt.geom.AffineTransform; +import java.awt.geom.Path2D; +import java.awt.geom.PathIterator; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; @@ -8,6 +10,8 @@ import org.simantics.district.network.ModelledCRS; import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.utils.GeometryUtils; +import gnu.trove.list.array.TDoubleArrayList; + public class DistrictNetworkNodeUtils { public static ThreadLocal sharedTransform = new ThreadLocal() { @@ -54,4 +58,69 @@ public class DistrictNetworkNodeUtils { scale = Math.max(4096, scale); //Math.min(scale, 32768)); return scale; } -} + + /** + * Finds the longest line segment from the provided path and sets + * centerPoint to the middle of that line and + * direction as the normalized direction vector of that line + * segment. + * + *

+ * If the path has no points, both centerPoint and + * direction are set to ({@link Double#NaN}, {@link Double#NaN}). + * If the path has only one point then centerPoint is set to that + * single point and direction is set to (1,0). + * + * @param path the path to process + * @param centerPoint point for writing the output center point + * @param direction point for writing the output direction vector + * @return the amount of points in the path + */ + public static int calculateCenterPointAndDirection(Path2D path, Point2D centerPoint, Point2D direction) { + PathIterator pi = path.getPathIterator(null); + TDoubleArrayList segments = new TDoubleArrayList(20); + double[] tmp = new double[6]; + while (!pi.isDone()) { + pi.currentSegment(tmp); + segments.add(tmp[0]); + segments.add(tmp[1]); + pi.next(); + } + + // Cover corner cases + int segCount = segments.size(); + if (segCount == 0) { + centerPoint.setLocation(Double.NaN, Double.NaN); + direction.setLocation(Double.NaN, Double.NaN); + return 0; + } else if (segCount == 2) { + centerPoint.setLocation(segments.getQuick(0), segments.getQuick(1)); + direction.setLocation(1, 0); + return 1; + } + + int longest = 1; + double distance = 0.0; + for (int i = 2; i < segCount; i += 2) { + double dx = segments.getQuick(i) - segments.getQuick(i-2); + double dy = segments.getQuick(i+1) - segments.getQuick(i-1); + + double d = dx * dx + dy * dy; + if (d > distance) { + distance = d; + longest = i; + } + } + + double x0 = segments.getQuick(longest - 2); + double y0 = segments.getQuick(longest - 1); + double x1 = segments.getQuick(longest); + double y1 = segments.getQuick(longest + 1); + + distance = Math.sqrt(distance); + centerPoint.setLocation((x0 + x1) / 2, (y0 + y1) / 2); + direction.setLocation((x1 - x0) / distance, (y1 - y0) / distance); + return segCount / 2; + } + +} \ No newline at end of file diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java index 94b4e896..4ce35a27 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java @@ -2,36 +2,38 @@ package org.simantics.district.network.ui.nodes; import java.awt.Color; import java.awt.Font; +import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; -import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle; -import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.ParentNode; import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode; import org.simantics.scenegraph.utils.DPIUtil; import org.simantics.scenegraph.utils.NodeUtil; public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode { - private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5)); - - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = -1723122278582600873L; + + private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10)); private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0); public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO"; - + String info = null; Point2D origin = new Point2D.Double(); Point2D direction = UNIT_X; - + + private DistrictNetworkEdgeNode edgeNode = null; + + private int prevZoomLevel = -1; + @Override public void render(Graphics2D g) { ParentNode root = (ParentNode) NodeUtil.getNearestParentOfType(this, RTreeNode.class); @@ -46,27 +48,25 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo public void renderDeferred(Graphics2D g) { if (info == null || "".equals(info)) return; - - AffineTransform oldTransform = g.getTransform(); - - // There has to be a better way to calculate the zoom level in this context... - AffineTransform baseTransform; - try { - baseTransform = ((G2DParentNode)getParent()).getTransform().createInverse(); - } catch (NoninvertibleTransformException e) { - baseTransform = new AffineTransform(); - } - - baseTransform.preConcatenate(oldTransform); - int zoomLevel = MapScalingTransform.zoomLevel(baseTransform); + int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL); if (zoomLevel < 17) return; - + + AffineTransform oldTransform = g.getTransform(); + Font oldFont = g.getFont(); Color oldColor = g.getColor(); Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - + + if (edgeNode != null && zoomLevel != prevZoomLevel) { + origin.setLocation(edgeNode.getCenterPoint(zoomLevel)); + Point2D dir = edgeNode.getDirection(zoomLevel); + double s = dir.getX() >= 0.0 ? 1.0 : -1.0; + direction.setLocation(s * dir.getX(), s * dir.getY()); + } + prevZoomLevel = zoomLevel; + doRender(g); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA); @@ -77,22 +77,24 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo private void doRender(Graphics2D g) { g.translate(origin.getX(), origin.getY()); - double scale = 1.5 / g.getTransform().getScaleX(); + double scale = 1.0 / g.getTransform().getScaleX(); g.scale(scale, scale); g.setFont(FONT); - int width1 = g.getFontMetrics().stringWidth(info); -// int height = g.getFontMetrics().getHeight(); - + FontMetrics fm = g.getFontMetrics(); + int width = fm.stringWidth(info); + int ascent = fm.getMaxAscent(); + g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY())); - g.translate(0, 10); - + g.translate(0, ascent); + +// int height = fm.getHeight(); // g.setColor(Color.WHITE); -// g.fillRect(-width1/2 - 5, -height-2, width1+10, height+4); - +// g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2); g.setColor(Color.BLACK); - - g.drawString(info, -width1/2, 0); +// g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2); + + g.drawString(info, -width/2, 0); } @Override @@ -108,4 +110,8 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo public void setInfo(String info) { this.info = info; } + + public void setEdgeNode(DistrictNetworkEdgeNode n) { + this.edgeNode = n; + } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java index 27c2fc84..ad722797 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java @@ -21,11 +21,14 @@ import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.profile.MidBranchEdgeSetRequest; import org.simantics.district.network.ui.nodes.DeferredRenderingNode; +import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode; import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils; import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode; import org.simantics.layer0.Layer0; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.ParentNode; +import org.simantics.scenegraph.g2d.IG2DNode; +import org.simantics.scenegraph.g2d.nodes.ConnectionNode; import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode; import org.simantics.scenegraph.profile.EvaluationContext; import org.simantics.scenegraph.profile.common.ProfileVariables; @@ -35,8 +38,8 @@ import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function1; import org.simantics.structural.stubs.StructuralResource2; -import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DistrictNetworkStaticInfoStyle extends StyleBase { @@ -160,6 +163,12 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase