1 package org.simantics.district.network.ui.nodes;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
7 import org.simantics.district.network.ModelledCRS;
8 import org.simantics.maps.MapScalingTransform;
9 import org.simantics.scenegraph.utils.GeometryUtils;
11 public class DistrictNetworkNodeUtils {
13 public static Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
15 result = new Rectangle2D.Double();
16 double mw = margin.getWidth();
17 double mh = margin.getHeight();
18 result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip);
22 public static Point2D calculatePoint2D(Point2D point, Point2D result) {
23 double x = ModelledCRS.longitudeToX(point.getX());
24 double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
26 result = new Point2D.Double(x, y);
28 result.setLocation(x, y);
32 public static AffineTransform getTransformToRectangle(Rectangle2D toDraw, AffineTransform transform) {
33 if (transform == null)
34 transform = new AffineTransform();
36 transform.setTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY());
40 public static double calculateScaleRecip(AffineTransform tr) {
41 int zoomLevel = MapScalingTransform.zoomLevel(tr);
42 double t = 1.0 / (getScale(tr) * Math.sqrt(zoomLevel));
46 static double getScale(AffineTransform tr) {
48 scale = GeometryUtils.getScale(tr);
49 scale = Math.max(4096, Math.min(scale, 32768));