]> gerrit.simantics Code Review - simantics/district.git/blob
0d300d058bb1df991ae6cbca6726d9d4332fcfeb
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
6
7 import org.simantics.district.network.ModelledCRS;
8 import org.simantics.maps.MapScalingTransform;
9 import org.simantics.scenegraph.utils.GeometryUtils;
10
11 public class DistrictNetworkNodeUtils {
12
13     public static Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
14         if (result == null)
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);
19         return result;
20     }
21
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
25         if (result == null)
26             result = new Point2D.Double(x, y);
27         else
28             result.setLocation(x, y);
29         return result;
30     }
31
32     public static AffineTransform getTransformToRectangle(Rectangle2D toDraw, AffineTransform transform) {
33         if (transform == null)
34             transform = new AffineTransform();
35
36         transform.setTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY());
37         return transform;
38     }
39
40     public static double calculateScaleRecip(AffineTransform tr) {
41         int zoomLevel = MapScalingTransform.zoomLevel(tr);
42         return 1.0 / (getScale(tr) * Math.sqrt(zoomLevel));
43     }
44
45     static double getScale(AffineTransform tr) {
46         double scale;
47         scale = GeometryUtils.getScale(tr);
48         scale = Math.max(4096, scale); //Math.min(scale, 32768));
49         return scale;
50     }
51 }