1 /*
2 * Ellipse Curve
3 *
4 * Authors:
5 * Marco Cecchetti <mrcekets at gmail.com>
6 *
7 * Copyright 2008 authors
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 */
34 #include <2geom/ellipse.h>
35 #include <2geom/svg-elliptical-arc.h>
36 #include <2geom/numeric/fitting-tool.h>
37 #include <2geom/numeric/fitting-model.h>
40 namespace Geom
41 {
43 void Ellipse::set(double A, double B, double C, double D, double E, double F)
44 {
45 double den = 4*A*C - B*B;
46 if ( den == 0 )
47 {
48 THROW_LOGICALERROR("den == 0, while computing ellipse centre");
49 }
50 m_centre[X] = (B*E - 2*C*D) / den;
51 m_centre[Y] = (B*D - 2*A*E) / den;
53 // evaluate the a coefficient of the ellipse equation in normal form
54 // E(x,y) = a*(x-cx)^2 + b*(x-cx)*(y-cy) + c*(y-cy)^2 = 1
55 // where b = a*B , c = a*C, (cx,cy) == centre
56 double num = A * sqr(m_centre[X])
57 + B * m_centre[X] * m_centre[Y]
58 + C * sqr(m_centre[Y])
59 - A * F;
62 //evaluate ellipse rotation angle
63 double rot = std::atan2( -B, -(A - C) )/2;
64 // std::cerr << "rot = " << rot << std::endl;
65 bool swap_axes = false;
66 if ( are_near(rot, 0) ) rot = 0;
67 if ( are_near(rot, M_PI/2) || rot < 0 )
68 {
69 swap_axes = true;
70 }
72 // evaluate the length of the ellipse rays
73 double cosrot = std::cos(rot);
74 double sinrot = std::sin(rot);
75 double cos2 = cosrot * cosrot;
76 double sin2 = sinrot * sinrot;
77 double cossin = cosrot * sinrot;
79 den = A * cos2 + B * cossin + C * sin2;
80 if ( den == 0 )
81 {
82 THROW_LOGICALERROR("den == 0, while computing 'rx' coefficient");
83 }
84 double rx2 = num/den;
85 if ( rx2 < 0 )
86 {
87 THROW_LOGICALERROR("rx2 < 0, while computing 'rx' coefficient");
88 }
89 double rx = std::sqrt(rx2);
91 den = C * cos2 - B * cossin + A * sin2;
92 if ( den == 0 )
93 {
94 THROW_LOGICALERROR("den == 0, while computing 'ry' coefficient");
95 }
96 double ry2 = num/den;
97 if ( ry2 < 0 )
98 {
99 THROW_LOGICALERROR("ry2 < 0, while computing 'rx' coefficient");
100 }
101 double ry = std::sqrt(ry2);
103 // the solution is not unique so we choose always the ellipse
104 // with a rotation angle between 0 and PI/2
105 if ( swap_axes ) std::swap(rx, ry);
106 if ( are_near(rot, M_PI/2)
107 || are_near(rot, -M_PI/2)
108 || are_near(rx, ry) )
109 {
110 rot = 0;
111 }
112 else if ( rot < 0 )
113 {
114 rot += M_PI/2;
115 }
117 m_ray[X] = rx;
118 m_ray[Y] = ry;
119 m_angle = rot;
120 }
123 void Ellipse::set(std::vector<Point> const& points)
124 {
125 size_t sz = points.size();
126 if (sz < 5)
127 {
128 THROW_RANGEERROR("fitting error: too few points passed");
129 }
130 NL::LFMEllipse model;
131 NL::least_squeares_fitter<NL::LFMEllipse> fitter(model, sz);
133 for (size_t i = 0; i < sz; ++i)
134 {
135 fitter.append(points[i]);
136 }
137 fitter.update();
139 NL::Vector z(sz, 0.0);
140 model.instance(*this, fitter.result(z));
141 }
144 SVGEllipticalArc
145 Ellipse::arc(Point const& initial, Point const& inner, Point const& final,
146 bool _svg_compliant)
147 {
148 Point sp_cp = initial - center();
149 Point ep_cp = final - center();
150 Point ip_cp = inner - center();
152 double angle1 = angle_between(sp_cp, ep_cp);
153 double angle2 = angle_between(sp_cp, ip_cp);
154 double angle3 = angle_between(ip_cp, ep_cp);
156 bool large_arc_flag = true;
157 bool sweep_flag = true;
159 if ( angle1 > 0 )
160 {
161 if ( angle2 > 0 && angle3 > 0 )
162 {
163 large_arc_flag = false;
164 sweep_flag = true;
165 }
166 else
167 {
168 large_arc_flag = true;
169 sweep_flag = false;
170 }
171 }
172 else
173 {
174 if ( angle2 < 0 && angle3 < 0 )
175 {
176 large_arc_flag = false;
177 sweep_flag = false;
178 }
179 else
180 {
181 large_arc_flag = true;
182 sweep_flag = true;
183 }
184 }
186 SVGEllipticalArc ea( initial, ray(X), ray(Y), rot_angle(),
187 large_arc_flag, sweep_flag, final, _svg_compliant);
188 return ea;
189 }
192 } // end namespace Geom
195 /*
196 Local Variables:
197 mode:c++
198 c-file-style:"stroustrup"
199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200 indent-tabs-mode:nil
201 fill-column:99
202 End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :