]> gerrit.simantics Code Review - simantics/district.git/blob
0efab43173f112d4521667f4af00919434c3f7d2
[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 ThreadLocal<AffineTransform> sharedTransform = new ThreadLocal<AffineTransform>() {
14         protected AffineTransform initialValue() {
15             return new AffineTransform();
16         }
17     };
18
19     public static Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
20         if (result == null)
21             result = new Rectangle2D.Double();
22         double mw = margin.getWidth();
23         double mh = margin.getHeight();
24         result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip);
25         return result;
26     }
27
28     public static Point2D calculatePoint2D(Point2D point, Point2D result) {
29         double x = ModelledCRS.longitudeToX(point.getX());
30         double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
31         if (result == null)
32             result = new Point2D.Double(x, y);
33         else
34             result.setLocation(x, y);
35         return result;
36     }
37
38     public static AffineTransform getTransformToRectangle(Rectangle2D toDraw, AffineTransform transform) {
39         if (transform == null)
40             transform = new AffineTransform();
41
42         transform.setTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY());
43         return transform;
44     }
45
46     public static double calculateScaleRecip(AffineTransform tr) {
47         int zoomLevel = MapScalingTransform.zoomLevel(tr);
48         return 1.0 / (getScale(tr) * Math.sqrt(zoomLevel));
49     }
50
51     static double getScale(AffineTransform tr) {
52         double scale;
53         scale = GeometryUtils.getScale(tr);
54         scale = Math.max(4096, scale); //Math.min(scale, 32768));
55         return scale;
56     }
57 }