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 scale = GeometryUtils.getScale(tr); double sqrt = Math.sqrt(scale / zoomLevel); double viewScaleRecip = (sqrt / zoomLevel); return viewScaleRecip; } }