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
|
// -*- related-file-name: "../include/lcdf/transform.hh" -*-
/* transform.{cc,hh} -- planar affine transformations
*
* Copyright (c) 2000-2006 Eddie Kohler
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <lcdf/transform.hh>
#include <lcdf/straccum.hh>
#include <math.h>
Transform::Transform()
{
_m[0] = _m[3] = 1;
_m[1] = _m[2] = _m[4] = _m[5] = 0;
_null = true;
}
Transform::Transform(const double m[6])
{
_m[0] = m[0];
_m[1] = m[1];
_m[2] = m[2];
_m[3] = m[3];
_m[4] = m[4];
_m[5] = m[5];
check_null(0);
}
Transform::Transform(double m0, double m1, double m2,
double m3, double m4, double m5)
{
_m[0] = m0;
_m[1] = m1;
_m[2] = m2;
_m[3] = m3;
_m[4] = m4;
_m[5] = m5;
check_null(0);
}
void
Transform::check_null(double tolerance)
{
_null = (fabs(_m[0] - 1) < tolerance && fabs(_m[1]) < tolerance
&& fabs(_m[2]) < tolerance && fabs(_m[3] - 1) < tolerance
&& fabs(_m[4]) < tolerance && fabs(_m[5]) < tolerance);
}
void
Transform::scale(double x, double y)
{
_m[0] *= x;
_m[1] *= x;
_m[2] *= y;
_m[3] *= y;
if (x != 1 || y != 1)
_null = false;
}
void
Transform::rotate(double r)
{
double c = cos(r);
double s = sin(r);
double a = _m[0], b = _m[2];
_m[0] = a*c + b*s;
_m[2] = b*c - a*s;
a = _m[1], b = _m[3];
_m[1] = a*c + b*s;
_m[3] = b*c - a*s;
if (r != 0)
_null = false;
}
void
Transform::translate(double x, double y)
{
_m[4] += _m[0]*x + _m[2]*y;
_m[5] += _m[1]*x + _m[3]*y;
if (x != 0 || y != 0)
_null = false;
}
void
Transform::shear(double s)
{
*this *= Transform(1, 0, s, 1, 0, 0);
}
Transform
Transform::transformed(const Transform &t) const
{
return Transform(_m[0] * t._m[0] + _m[2] * t._m[1],
_m[1] * t._m[0] + _m[3] * t._m[1],
_m[0] * t._m[2] + _m[2] * t._m[3],
_m[1] * t._m[2] + _m[3] * t._m[3],
_m[0] * t._m[4] + _m[2] * t._m[5] + _m[4],
_m[1] * t._m[4] + _m[3] * t._m[5] + _m[5]);
}
void
Transform::real_apply_to(Point &p) const
{
double x = p.x;
p.x = x*_m[0] + p.y*_m[2] + _m[4];
p.y = x*_m[1] + p.y*_m[3] + _m[5];
}
Point
Transform::real_apply(const Point &p) const
{
return Point(p.x*_m[0] + p.y*_m[2] + _m[4],
p.x*_m[1] + p.y*_m[3] + _m[5]);
}
Bezier &
operator*=(Bezier &b, const Transform &t)
{
if (!t.null()) {
b.mpoint(0) *= t;
b.mpoint(1) *= t;
b.mpoint(2) *= t;
b.mpoint(3) *= t;
}
return b;
}
Bezier
operator*(const Bezier &b, const Transform &t)
{
return (t.null()
? b
: Bezier(b.point(0) * t, b.point(1) * t, b.point(2) * t, b.point(3) * t));
}
String
Transform::unparse() const
{
StringAccum sa;
sa << '[';
for (int i = 0; i < 6; i++) {
if (i)
sa << ',' << ' ';
sa << _m[i];
}
sa << ']';
return sa.take_string();
}
|