1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.awt.geom.AffineTransform;
|
---|
5 | import java.awt.geom.Point2D;
|
---|
6 | import org.openstreetmap.gui.jmapviewer.interfaces.IProjected;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Class that fixes the position of a tile in a given coordinate space.
|
---|
10 | *
|
---|
11 | * This is done by storing the coordinates of the tile origin and the opposite
|
---|
12 | * tile corner.
|
---|
13 | * <p>
|
---|
14 | * It may represent a reprojected tile, i.e. the tile is rotated / deformed in an
|
---|
15 | * arbitrary way. In general, the tile origin cannot be expected to be the
|
---|
16 | * upper left corner of the rectangle that is spanned by the 2 points.
|
---|
17 | * <p>
|
---|
18 | * The coordinate space may be
|
---|
19 | * <ul>
|
---|
20 | * <li>pixel coordinates of the image file</li>
|
---|
21 | * <li>projected coordinates (east / north)</li>
|
---|
22 | * <li>screen pixel coordinates</li>
|
---|
23 | * </ul>
|
---|
24 | */
|
---|
25 | public class TileAnchor {
|
---|
26 |
|
---|
27 | protected final Point2D tileOrigin, nextTileOrigin;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create a new tile anchor.
|
---|
31 | * @param tileOrigin position of the tile origin
|
---|
32 | * @param nextTileOrigin position of the opposite tile corner, i.e. the
|
---|
33 | * origin of the tile with index (x+1,y+1), when current tile has index (x,y)
|
---|
34 | */
|
---|
35 | public TileAnchor(Point2D tileOrigin, Point2D nextTileOrigin) {
|
---|
36 | this.tileOrigin = tileOrigin;
|
---|
37 | this.nextTileOrigin = nextTileOrigin;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public TileAnchor(IProjected tileOrigin, IProjected nextTileOrigin) {
|
---|
41 | this.tileOrigin = new Point2D.Double(tileOrigin.getEast(), tileOrigin.getNorth());
|
---|
42 | this.nextTileOrigin = new Point2D.Double(nextTileOrigin.getEast(), nextTileOrigin.getNorth());
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Point2D getTileOrigin() {
|
---|
46 | return tileOrigin;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public Point2D getNextTileOrigin() {
|
---|
50 | return nextTileOrigin;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public String toString() {
|
---|
55 | return "TileAnchor{" + tileOrigin.toString() + "; " + nextTileOrigin.toString() + "}";
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Create a transformation that converts points from this coordinate space
|
---|
60 | * to another coordinate space.
|
---|
61 | * @param other tile anchor of the tile in the target coordinate space
|
---|
62 | * @return affine transformation from this coordinate space to the target
|
---|
63 | * coordinate space
|
---|
64 | */
|
---|
65 | public AffineTransform convert(TileAnchor other) {
|
---|
66 | Point2D src1 = this.getTileOrigin();
|
---|
67 | Point2D src2 = this.getNextTileOrigin();
|
---|
68 | Point2D dest1 = other.getTileOrigin();
|
---|
69 | Point2D dest2 = other.getNextTileOrigin();
|
---|
70 |
|
---|
71 | double scaleX = (dest2.getX() - dest1.getX()) / (src2.getX() - src1.getX());
|
---|
72 | double scaleY = (dest2.getY() - dest1.getY()) / (src2.getY() - src1.getY());
|
---|
73 | double offsetX0 = dest1.getX() - scaleX * src1.getX();
|
---|
74 | double offsetY0 = dest1.getY() - scaleY * src1.getY();
|
---|
75 | return new AffineTransform(scaleX, 0, 0, scaleY, offsetX0, offsetY0);
|
---|
76 | }
|
---|
77 | }
|
---|