Debian Codegraph @

src/lib/CDRTransforms.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "CDRTransforms.h"
11
12 #include "libcdr_utils.h"
13
14
15 libcdr::CDRTransform::CDRTransform()
16 : m_v0(1.0), m_v1(0.0), m_x0(0.0),
17 m_v3(0.0), m_v4(1.0), m_y0(0.0)
18 {
19 }
20
21 libcdr::CDRTransform::CDRTransform(double v0, double v1, double x0, double v3, double v4, double y0)
22 : m_v0(v0), m_v1(v1), m_x0(x0), m_v3(v3), m_v4(v4), m_y0(y0)
23 {
24 }
25
26 void libcdr::CDRTransform::applyToPoint(double &x, double &y) const
27 {
28 double tmpX = m_v0*x + m_v1*y+m_x0;
29 y = m_v3*x + m_v4*y+m_y0;
30 x = tmpX;
31 }
32
33 void libcdr::CDRTransform::applyToArc(double &rx, double &ry, double &rotation, bool &sweep, double &endx, double &endy) const
34 {
35 // Transform the end-point, which is the easiest
36 applyToPoint(endx, endy);
37
38 // Determine whether sweep should change
39 double determinant = m_v0*m_v4 - m_v1*m_v3;
40 if (determinant < 0.0)
41 sweep = !sweep;
42
43 // Transform a centered ellipse
44
45 // rx = 0 and ry = 0
46 if (CDR_ALMOST_ZERO(rx) && CDR_ALMOST_ZERO(ry))
47 {
48 rotation = rx = ry = 0.0;
49 return;
50 }
51
52 // rx > 0, ry = 0
53 if (CDR_ALMOST_ZERO(ry))
54 {
55 double x = m_v0*cos(rotation) + m_v1*sin(rotation);
56 double y = m_v3*cos(rotation) + m_v4*sin(rotation);
57 rx *= sqrt(x*x + y*y);
58 if (CDR_ALMOST_ZERO(rx))
59 {
60 rotation = rx = ry = 0;
61 return;
62 }
63 rotation = atan2(y, x);
64 return;
65 }
66
67 // rx = 0, ry > 0
68 if (CDR_ALMOST_ZERO(rx))
69 {
70 double x = -m_v0*sin(rotation) + m_v1*cos(rotation);
71 double y = -m_v3*sin(rotation) + m_v4*cos(rotation);
72 ry *= sqrt(x*x + y*y);
73 if (CDR_ALMOST_ZERO(ry))
74 {
75 rotation = rx = ry = 0;
76 return;
77 }
78 rotation = atan2(y, x) - M_PI/2.0;
79 return;
80 }
81
82 double v0, v1, v2, v3;
83 if (!CDR_ALMOST_ZERO(determinant))
84 {
85 v0 = ry*(m_v4*cos(rotation)- m_v3*sin(rotation));
86 v1 = ry*(m_v0*sin(rotation)- m_v1*cos(rotation));
87 v2 = -rx*(m_v4*sin(rotation) + m_v3*cos(rotation));
88 v3 = rx*(m_v1*sin(rotation) + m_v0*cos(rotation));
89
90 // Transformed ellipse
91 double A = v0*v0 + v2*v2;
92 double B = 2.0*(v0*v1 + v2*v3);
93 double C = v1*v1 + v3*v3;
94
95 // Rotate the transformed ellipse
96 if (CDR_ALMOST_ZERO(B))
97 rotation = 0;
98 else
99 {
100 rotation = atan2(B, A-C) / 2.0;
101 double c = cos(rotation);
102 double s = sin(rotation);
103 double cc = c*c;
104 double ss = s*s;
105 double sc = B*s*c;
106 B = A;
107 A = B*cc + sc + C*ss;
108 C = B*ss - sc + C*cc;
109 }
110
111 // Compute rx and ry from A and C
112 if (!CDR_ALMOST_ZERO(A) && !CDR_ALMOST_ZERO(C))
113 {
114 double abdet = fabs(rx*ry*determinant);
115 A = sqrt(fabs(A));
116 C = sqrt(fabs(C));
117 rx = abdet / A;
118 ry = abdet / C;
119 return;
120 }
121 }
122
123 // Special case of a close to singular transformation
124 v0 = ry*(m_v4*cos(rotation) - m_v3*sin(rotation));
125 v1 = ry*(m_v1*cos(rotation) - m_v0*sin(rotation));
126 v2 = rx*(m_v3*cos(rotation) + m_v4*sin(rotation));
127 v3 = rx*(m_v0*cos(rotation) + m_v1*sin(rotation));
128
129 // The result of transformation is a point
130 if (CDR_ALMOST_ZERO(v3*v3 + v1*v1) && CDR_ALMOST_ZERO(v2*v2 + v0*v0))
131 {
132 rotation = rx = ry = 0;
133 return;
134 }
135 else // The result of transformation is a line
136 {
137 double x = sqrt(v3*v3 + v1*v1);
138 double y = sqrt(v2*v2 + v0*v0);
139 if (v3*v3 + v1*v1 >= v2*v2 + v0*v0)
140 y = (v2*v2 + v0*v0) / x;
141 else
142 x = (v3*v3 + v1*v1) / y;
143 rx = sqrt(x*x + y*y);
144 ry = 0;
145 rotation = atan2(y, x);
146 }
147 }
148
149 double libcdr::CDRTransform::_getScaleX() const
150 {
151 double x0 = 0.0;
152 double x1 = 1.0;
153 double y0 = 0.0;
154 double y1 = 0.0;
155 applyToPoint(x0, y0);
156 applyToPoint(x1, y1);
157 return x1 - x0;
158 }
159
160 double libcdr::CDRTransform::getScaleX() const
161 {
162 return fabs(_getScaleX());
163 }
164
165 bool libcdr::CDRTransform::getFlipX() const
166 {
167 return (0 > _getScaleX());
168 }
169
170 double libcdr::CDRTransform::_getScaleY() const
171 {
172 double x0 = 0.0;
173 double x1 = 0.0;
174 double y0 = 0.0;
175 double y1 = 1.0;
176 applyToPoint(x0, y0);
177 applyToPoint(x1, y1);
178 return y1 - y0;
179 }
180
181 double libcdr::CDRTransform::getScaleY() const
182 {
183 return fabs(_getScaleY());
184 }
185
186 bool libcdr::CDRTransform::getFlipY() const
187 {
188 return (0 > _getScaleY());
189 }
190
191 double libcdr::CDRTransform::getTranslateX() const
192 {
193 double x = 0.0;
194 double y = 0.0;
195 applyToPoint(x, y);
196 return x;
197 }
198
199 double libcdr::CDRTransform::getTranslateY() const
200 {
201 double x = 0.0;
202 double y = 0.0;
203 applyToPoint(x, y);
204 return y;
205 }
206
207 librevenge::RVNGString libcdr::CDRTransform::toString() const
208 {
209 librevenge::RVNGString output;
210 output.sprintf("[%f %f %f | %f %f %f]", m_v0, m_v1, m_x0, m_v3, m_v4, m_y0);
211 return output;
212 }
213
214
215 libcdr::CDRTransforms::CDRTransforms()
216 : m_trafos()
217 {
218 }
219
220 libcdr::CDRTransforms::~CDRTransforms()
221 {
222 }
223
224 void libcdr::CDRTransforms::append(double v0, double v1, double x0, double v3, double v4, double y0)
225 {
226 append(CDRTransform(v0, v1, x0, v3, v4, y0));
227 }
228
229 void libcdr::CDRTransforms::append(const CDRTransform &trafo)
230 {
231 m_trafos.push_back(trafo);
232 }
233
234 void libcdr::CDRTransforms::clear()
235 {
236 m_trafos.clear();
237 }
238
239 bool libcdr::CDRTransforms::empty() const
240 {
241 return m_trafos.empty();
242 }
243
244 void libcdr::CDRTransforms::applyToPoint(double &x, double &y) const
245 {
246 for (const auto &trafo : m_trafos)
247 trafo.applyToPoint(x,y);
248 }
249
250 void libcdr::CDRTransforms::applyToArc(double &rx, double &ry, double &rotation, bool &sweep, double &x, double &y) const
251 {
252 for (const auto &trafo : m_trafos)
253 trafo.applyToArc(rx, ry, rotation, sweep, x, y);
254 }
255
256 double libcdr::CDRTransforms::_getScaleX() const
257 {
258 double x0 = 0.0;
259 double x1 = 1.0;
260 double y0 = 0.0;
261 double y1 = 0.0;
262 applyToPoint(x0, y0);
263 applyToPoint(x1, y1);
264 return x1 - x0;
265 }
266
267 double libcdr::CDRTransforms::getScaleX() const
268 {
269 return fabs(_getScaleX());
270 }
271
272 bool libcdr::CDRTransforms::getFlipX() const
273 {
274 return (0 > _getScaleX());
275 }
276
277 double libcdr::CDRTransforms::_getScaleY() const
278 {
279 double x0 = 0.0;
280 double x1 = 0.0;
281 double y0 = 0.0;
282 double y1 = 1.0;
283 applyToPoint(x0, y0);
284 applyToPoint(x1, y1);
285 return y1 - y0;
286 }
287
288 double libcdr::CDRTransforms::getScaleY() const
289 {
290 return fabs(_getScaleY());
291 }
292
293 bool libcdr::CDRTransforms::getFlipY() const
294 {
295 return (0 > _getScaleY());
296 }
297
298 double libcdr::CDRTransforms::getTranslateX() const
299 {
300 double x = 0.0;
301 double y = 0.0;
302 applyToPoint(x, y);
303 return x;
304 }
305
306 double libcdr::CDRTransforms::getTranslateY() const
307 {
308 double x = 0.0;
309 double y = 0.0;
310 applyToPoint(x, y);
311 return y;
312 }
313
314
315 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */