-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathTransform.java
More file actions
250 lines (207 loc) · 7.4 KB
/
Transform.java
File metadata and controls
250 lines (207 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package nodebox.graphics;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
public class Transform implements Cloneable {
public enum Mode {
CORNER, CENTER
}
private AffineTransform affineTransform;
public static Transform translated(double tx, double ty) {
Transform t = new Transform();
t.translate(tx, ty);
return t;
}
public static Transform translated(Point t) {
return translated(t.x, t.y);
}
public static Transform rotated(double degrees) {
Transform t = new Transform();
t.rotate(degrees);
return t;
}
public static Transform rotatedRadians(double radians) {
Transform t = new Transform();
t.rotateRadians(radians);
return t;
}
public static Transform scaled(double scale) {
Transform t = new Transform();
t.scale(scale);
return t;
}
public static Transform scaled(double sx, double sy) {
Transform t = new Transform();
t.scale(sx, sy);
return t;
}
public static Transform scaled(Point s) {
return scaled(s.x, s.y);
}
public static Transform skewed(double skew) {
Transform t = new Transform();
t.skew(skew);
return t;
}
public static Transform skewed(double kx, double ky) {
Transform t = new Transform();
t.skew(kx, ky);
return t;
}
public Transform() {
affineTransform = new AffineTransform();
}
public Transform(double m00, double m10, double m01, double m11, double m02, double m12) {
affineTransform = new AffineTransform(m00, m10, m01, m11, m02, m12);
}
public Transform(AffineTransform affineTransform) {
this.affineTransform = affineTransform;
}
public Transform(Transform other) {
this.affineTransform = (AffineTransform) other.affineTransform.clone();
}
//// Transform changes ////
public void translate(Point point) {
affineTransform.translate(point.x, point.y);
}
public void translate(double tx, double ty) {
affineTransform.translate(tx, ty);
}
public void rotate(double degrees) {
double radians = degrees * Math.PI / 180;
affineTransform.rotate(radians);
}
public void rotateRadians(double radians) {
affineTransform.rotate(radians);
}
public void scale(double scale) {
affineTransform.scale(scale, scale);
}
public void scale(double sx, double sy) {
affineTransform.scale(sx, sy);
}
public void skew(double skew) {
skew(skew, skew);
}
public void skew(double kx, double ky) {
kx = Math.PI * kx / 180.0;
ky = Math.PI * ky / 180.0;
affineTransform.concatenate(new AffineTransform(1, Math.tan(ky), -Math.tan(kx), 1, 0, 0));
}
public boolean invert() {
try {
affineTransform = affineTransform.createInverse();
return true;
} catch (NoninvertibleTransformException e) {
return false;
}
}
public void append(Transform t) {
affineTransform.concatenate(t.affineTransform);
}
public void prepend(Transform t) {
affineTransform.preConcatenate(t.affineTransform);
}
//// Operations ////
public Point map(Point p) {
Point2D.Double p2 = new Point2D.Double();
affineTransform.transform(p.toPoint2D(), p2);
return new Point(p2);
}
public Rect map(Rect r) {
// TODO: The size conversion might be incorrect. (using deltaTransform) In that case, make topLeft and bottomRight points.
Point2D origin = new Point2D.Double(r.getX(), r.getY());
Point2D size = new Point2D.Double(r.getWidth(), r.getHeight());
Point2D transformedOrigin = new Point2D.Double();
Point2D transformedSize = new Point2D.Double();
affineTransform.transform(origin, transformedOrigin);
affineTransform.deltaTransform(size, transformedSize);
return new Rect(transformedOrigin.getX(), transformedOrigin.getY(), transformedSize.getX(), transformedSize.getY());
}
public IGeometry map(IGeometry shape) {
if (shape instanceof Path) {
return map((Path) shape);
} else if (shape instanceof Geometry) {
return map((Geometry) shape);
} else {
throw new RuntimeException("Unsupported geometry type " + shape);
}
}
public Path map(Path p) {
Path newPath = new Path(p, false);
for (Contour c : p.getContours()) {
Contour newContour = new Contour(map(c.getPoints()), c.isClosed());
newPath.add(newContour);
}
return newPath;
}
public Geometry map(Geometry g) {
Geometry newGeometry = new Geometry();
for (Path p : g.getPaths()) {
Path newPath = map(p);
newGeometry.add(newPath);
}
return newGeometry;
}
/**
* Transform all the given points and return a list of transformed points.
* Points are immutable, so they can not be transformed in-place.
*
* @param points The points to transform.
* @return The list of transformed points.
*/
public List<Point> map(List<Point> points) {
// Prepare the points for the AffineTransform transformation.
double[] coords = new double[points.size() * 2];
int i = 0;
for (Point pt : points) {
coords[i++] = pt.x;
coords[i++] = pt.y;
}
affineTransform.transform(coords, 0, coords, 0, points.size());
// Convert the transformed points into a new List.
List<Point> transformed = new ArrayList<Point>(points.size());
int pointIndex = 0;
for (i = 0; i < coords.length; i += 2) {
transformed.add(new Point(coords[i], coords[i + 1], points.get(pointIndex).type));
pointIndex++;
}
return transformed;
}
public Rect convertBoundsToFrame(Rect bounds) {
AffineTransform t = fullTransform(bounds);
Point2D transformedOrigin = new Point2D.Double();
Point2D transformedSize = new Point2D.Double();
t.transform(new Point2D.Double(bounds.getX(), bounds.getY()), transformedOrigin);
t.deltaTransform(new Point2D.Double(bounds.getWidth(), bounds.getHeight()), transformedSize);
return new Rect(transformedOrigin.getX(), transformedOrigin.getY(), transformedSize.getX(), transformedSize.getY());
}
private AffineTransform fullTransform(Rect bounds) {
double cx = bounds.getX() + bounds.getWidth() / 2;
double cy = bounds.getY() + bounds.getHeight() / 2;
AffineTransform t = new AffineTransform();
t.translate(cx, cy);
t.preConcatenate(affineTransform);
t.translate(-cx, -cy);
return t;
}
@Override
protected Transform clone() {
return new Transform(this);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Transform)) return false;
return getAffineTransform().equals(((Transform) obj).getAffineTransform());
}
public void apply(Graphics2D g, Rect bounds) {
AffineTransform t = fullTransform(bounds);
g.transform(t);
}
public AffineTransform getAffineTransform() {
return affineTransform;
}
}