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
251
252
253
254
255
256
257
258
259
260
261
|
/*****
* transform.h
* Andy Hammerlindl 2002/05/22
*
* The transform datatype stores an affine transformation on the plane
* The datamembers are x, y, xx, xy, yx, and yy. A pair (x,y) is
* transformed as
* x' = t.x + t.xx * x + t.xy * y
* y' = t.y + t.yx * x + t.yy * y
*****/
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <iostream>
#include "pair.h"
namespace camp {
class transform : public gc {
double x;
double y;
double xx;
double xy;
double yx;
double yy;
public:
transform()
: x(0.0), y(0.0), xx(1.0), xy(0.0), yx(0.0), yy(1.0) {}
virtual ~transform() {}
transform(double x, double y,
double xx, double xy,
double yx, double yy)
: x(x), y(y), xx(xx), xy(xy), yx(yx), yy(yy) {}
double getx() const { return x; }
double gety() const { return y; }
double getxx() const { return xx; }
double getxy() const { return xy; }
double getyx() const { return yx; }
double getyy() const { return yy; }
friend transform operator+ (const transform& t, const transform& s)
{
return transform(t.x + s.x, t.y + s.y,
t.xx + s.xx, t.xy + s.xy,
t.yx + s.yx, t.yy + s.yy);
}
friend transform operator- (const transform& t, const transform& s)
{
return transform(t.x - s.x, t.y - s.y,
t.xx - s.xx, t.xy - s.xy,
t.yx - s.yx, t.yy - s.yy);
}
friend transform operator- (const transform& t)
{
return transform(-t.x, -t.y,
-t.xx, -t.xy,
-t.yx, -t.yy);
}
friend pair operator* (const transform& t, const pair& z)
{
double x = z.getx(), y = z.gety();
return pair(t.x + t.xx * x + t.xy * y, t.y + t.yx * x + t.yy * y);
}
// Calculates the composition of t and s, so for a pair, z,
// t * (s * z) == (t * s) * z
// Can be thought of as matrix multiplication.
friend transform operator* (const transform& t, const transform& s)
{
return transform(t.x + t.xx * s.x + t.xy * s.y,
t.y + t.yx * s.x + t.yy * s.y,
t.xx * s.xx + t.xy * s.yx,
t.xx * s.xy + t.xy * s.yy,
t.yx * s.xx + t.yy * s.yx,
t.yx * s.xy + t.yy * s.yy);
}
friend bool operator== (const transform& t1, const transform& t2)
{
return t1.x == t2.x && t1.y == t2.y &&
t1.xx == t2.xx && t1.xy == t2.xy &&
t1.yx == t2.yx && t1.yy == t2.yy;
}
friend bool operator!= (const transform& t1, const transform& t2)
{
return !(t1 == t2);
}
bool isIdentity() const
{
return x == 0.0 && y == 0.0 &&
xx == 1.0 && xy == 0.0 && yx == 0.0 && yy == 1.0;
}
bool isIsometry() const
{
return xx*xx+xy*xy == 1.0 && xx*yx+xy*yy == 0.0 && yx*yx+yy*yy == 1.0;
}
bool isNull() const
{
return x == 0.0 && y == 0.0 &&
xx == 0.0 && xy == 0.0 && yx == 0.0 && yy == 0.0;
}
// Calculates the determinant, as if it were a matrix.
friend double det(const transform& t)
{
return t.xx * t.yy - t.xy * t.yx;
}
// Tells if the transformation is invertible (bijective).
bool invertible() const
{
return det(*this) != 0.0;
}
friend transform inverse(const transform& t)
{
double d = det(t);
if (d == 0.0)
reportError("inverting singular transform");
d=1.0/d;
return transform((t.xy * t.y - t.yy * t.x)*d,
(t.yx * t.x - t.xx * t.y)*d,
t.yy*d, -t.xy*d, -t.yx*d, t.xx*d);
}
friend ostream& operator<< (ostream& out, const transform& t)
{
return out << "(" << t.x << ","
<< t.y << ","
<< t.xx << ","
<< t.xy << ","
<< t.yx << ","
<< t.yy << ")";
}
};
// The common transforms
static const transform identity;
inline transform shift(pair z)
{
return transform (z.getx(), z.gety(), 1.0, 0.0, 0.0, 1.0);
}
inline transform xscale(double s)
{
return transform (0.0, 0.0, s, 0.0, 0.0, 1.0);
}
inline transform yscale(double s)
{
return transform (0.0, 0.0, 1.0, 0.0, 0.0, s);
}
inline transform scale(double s)
{
return transform (0.0, 0.0, s, 0.0, 0.0, s);
}
inline transform scale(double x, double y)
{
return transform (0.0, 0.0, x, 0.0, 0.0, y);
}
inline transform scale(pair z)
{
// Equivalent to multiplication by z.
double x = z.getx(), y = z.gety();
return transform (0.0, 0.0, x, -y, y, x);
}
inline transform slant(double s)
{
return transform (0.0, 0.0, 1.0, s, 0.0, 1.0);
}
inline transform rotate(double theta)
{
double s = sin(theta), c = cos(theta);
return transform (0.0, 0.0, c, -s, s, c);
}
// return rotate(angle(v)) if z != (0,0); otherwise return identity.
inline transform rotate(pair z)
{
double d=z.length();
if(d == 0.0) return identity;
d=1.0/d;
return transform (0.0, 0.0, d*z.getx(), -d*z.gety(), d*z.gety(), d*z.getx());
}
inline transform rotatearound(pair z, double theta)
{
// Notice the operators are applied from right to left.
// Could be optimized.
return shift(z) * rotate(theta) * shift(-z);
}
inline transform reflectabout(pair z, pair w)
{
if (z == w)
reportError("points determining line to reflect about must be distinct");
// Also could be optimized.
transform basis = shift(z) * scale(w-z);
transform flip = yscale(-1.0);
return basis * flip * inverse(basis);
}
// Return the rotational part of t.
inline transform rotation(transform t)
{
pair z(2.0*t.getxx()*t.getyy(),t.getyx()*t.getyy()-t.getxx()*t.getxy());
if(t.getxx() < 0) z=-z;
return rotate(atan2(z.gety(),z.getx()));
}
// Remove the x and y components, so that the new transform maps zero to zero.
inline transform shiftless(transform t)
{
return transform(0, 0, t.getxx(), t.getxy(), t.getyx(), t.getyy());
}
// Return the translational component of t.
inline transform shift(transform t)
{
return transform(t.getx(), t.gety(), 1.0, 0, 0, 1.0);
}
// Return the translational pair of t.
inline pair shiftpair(transform t)
{
return pair(t.getx(), t.gety());
}
inline transform matrix(pair lb, pair rt)
{
pair size=rt-lb;
return transform(lb.getx(),lb.gety(),size.getx(),0,0,size.gety());
}
} //namespace camp
GC_DECLARE_PTRFREE(camp::transform);
#endif
|