public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
- private ThreadLocal<Path2D> path = new ThreadLocal<Path2D>() {
- 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);
@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);
}
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;
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;
private static double endX;
private static double endY;
+ private Path2D arrowPath;
+
@Override
public void init() {
}
}
}
+ 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);
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);
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);
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);
}
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());
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
bounds = calculateBounds(oldBounds);
}
+ /**
+ * Calculates conservative bounds of the node provided by
+ * <code>detailedPath</code>. These might be larger than the simplified bounds
+ * of <code>path</code> 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();
}
public void setDynamicColor(Color color) {
this.dynamicColor = color;
}
-
+
@PropertySetter(value = "arrowLength")
public void setArrowLength(Double length) {
arrowLength = length;
if (nn instanceof SVGNode)
((SVGNode)nn).setData(value);
}
-
@PropertySetter(value = "hidden")
public void setHidden(Boolean value) {
this.hidden = value;
}
+
}
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;
import org.simantics.maps.MapScalingTransform;
import org.simantics.scenegraph.utils.GeometryUtils;
+import gnu.trove.list.array.TDoubleArrayList;
+
public class DistrictNetworkNodeUtils {
public static ThreadLocal<AffineTransform> sharedTransform = new ThreadLocal<AffineTransform>() {
scale = Math.max(4096, scale); //Math.min(scale, 32768));
return scale;
}
-}
+
+ /**
+ * Finds the longest line segment from the provided <code>path</code> and sets
+ * <code>centerPoint</code> to the middle of that line and
+ * <code>direction</code> as the normalized direction vector of that line
+ * segment.
+ *
+ * <p>
+ * If the path has no points, both <code>centerPoint</code> and
+ * <code>direction</code> are set to ({@link Double#NaN}, {@link Double#NaN}).
+ * If the path has only one point then <code>centerPoint</code> is set to that
+ * single point and <code>direction</code> is set to <code>(1,0)</code>.
+ *
+ * @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
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);
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);
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
public void setInfo(String info) {
this.info = info;
}
+
+ public void setEdgeNode(DistrictNetworkEdgeNode n) {
+ this.edgeNode = n;
+ }
}
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;
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<DistrictNetworkStaticInfoStyle.StyleResult> {
Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
node.setLocation(origin, direction);
+
+ for (IG2DNode n : ((ConnectionNode)parent).getNodes()) {
+ if (n instanceof DistrictNetworkEdgeNode) {
+ node.setEdgeNode((DistrictNetworkEdgeNode) n);
+ }
+ }
}
node.setInfo(result.info);