package org.simantics.district.network.ui.nodes; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import org.simantics.district.network.ModelledCRS; import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.utils.GeometryUtils; public class DistrictNetworkNodeUtils { public static Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) { if (result == null) result = new Rectangle2D.Double(); double mw = margin.getWidth(); double mh = margin.getHeight(); result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip); return result; } public static Point2D calculatePoint2D(Point2D point, Point2D result) { double x = ModelledCRS.longitudeToX(point.getX()); double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted if (result == null) result = new Point2D.Double(x, y); else result.setLocation(x, y); return result; } public static AffineTransform getTransformToRectangle(Rectangle2D toDraw, AffineTransform transform) { if (transform == null) transform = new AffineTransform(); transform.setTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY()); return transform; } public static double calculateScaleRecip(AffineTransform tr) { int zoomLevel = MapScalingTransform.zoomLevel(tr); double t = 1.0 / (getScale(tr) * Math.sqrt(zoomLevel)); return t; } static double getScale(AffineTransform tr) { double scale; scale = GeometryUtils.getScale(tr); scale = Math.max(4096, Math.min(scale, 32768)); return scale; } }