Code

make spcurve::first_point and last_point boost::optional
[inkscape.git] / src / 2geom / svg-elliptical-arc.h
1 /*
2  * Elliptical Arc - implementation of the svg elliptical arc path element
3  *
4  * Authors:
5  *      MenTaLguY <mental@rydia.net>
6  *      Marco Cecchetti <mrcekets at gmail.com>
7  *
8  * Copyright 2007-2008  authors
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it either under the terms of the GNU Lesser General Public
12  * License version 2.1 as published by the Free Software Foundation
13  * (the "LGPL") or, at your option, under the terms of the Mozilla
14  * Public License Version 1.1 (the "MPL"). If you do not alter this
15  * notice, a recipient may use your version of this file under either
16  * the MPL or the LGPL.
17  *
18  * You should have received a copy of the LGPL along with this library
19  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  * You should have received a copy of the MPL along with this library
22  * in the file COPYING-MPL-1.1
23  *
24  * The contents of this file are subject to the Mozilla Public License
25  * Version 1.1 (the "License"); you may not use this file except in
26  * compliance with the License. You may obtain a copy of the License at
27  * http://www.mozilla.org/MPL/
28  *
29  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31  * the specific language governing rights and limitations.
32  */
35 #ifndef _2GEOM_SVG_ELLIPTICAL_ARC_H_
36 #define _2GEOM_SVG_ELLIPTICAL_ARC_H_
39 #include <2geom/curve.h>
40 #include <2geom/angle.h>
41 #include <2geom/utils.h>
42 #include <2geom/bezier-curve.h>
43 #include <2geom/sbasis-curve.h>  // for non-native methods
44 #include <2geom/numeric/vector.h>
45 #include <2geom/numeric/fitting-tool.h>
46 #include <2geom/numeric/fitting-model.h>
49 #include <algorithm>
53 namespace Geom
54 {
56 class SVGEllipticalArc : public Curve
57 {
58   public:
59     SVGEllipticalArc(bool _svg_compliant = true)
60         : m_initial_point(Point(0,0)), m_final_point(Point(0,0)),
61           m_rx(0), m_ry(0), m_rot_angle(0),
62           m_large_arc(true), m_sweep(true),
63           m_svg_compliant(_svg_compliant)
64     {
65         m_start_angle = m_end_angle = 0;
66         m_center = Point(0,0);
67     }
69     SVGEllipticalArc( Point _initial_point, double _rx, double _ry,
70                       double _rot_angle, bool _large_arc, bool _sweep,
71                       Point _final_point,
72                       bool _svg_compliant = true
73                     )
74         : m_initial_point(_initial_point), m_final_point(_final_point),
75           m_rx(_rx), m_ry(_ry), m_rot_angle(_rot_angle),
76           m_large_arc(_large_arc), m_sweep(_sweep),
77           m_svg_compliant(_svg_compliant)
78     {
79             calculate_center_and_extreme_angles();
80     }
82     void set( Point _initial_point, double _rx, double _ry,
83               double _rot_angle, bool _large_arc, bool _sweep,
84               Point _final_point
85              )
86     {
87         m_initial_point = _initial_point;
88         m_final_point = _final_point;
89         m_rx = _rx;
90         m_ry = _ry;
91         m_rot_angle = _rot_angle;
92         m_large_arc = _large_arc;
93         m_sweep = _sweep;
94         calculate_center_and_extreme_angles();
95     }
97     Curve* duplicate() const
98     {
99         return new SVGEllipticalArc(*this);
100     }
102     double center(unsigned int i) const
103     {
104         return m_center[i];
105     }
107     Point center() const
108     {
109         return m_center;
110     }
112     Point initialPoint() const
113     {
114         return m_initial_point;
115     }
117     Point finalPoint() const
118     {
119         return m_final_point;
120     }
122     double start_angle() const
123     {
124         return m_start_angle;
125     }
127     double end_angle() const
128     {
129         return m_end_angle;
130     }
132     double ray(unsigned int i) const
133     {
134         return (i == 0) ? m_rx : m_ry;
135     }
137     bool large_arc_flag() const
138     {
139         return m_large_arc;
140     }
142     bool sweep_flag() const
143     {
144         return m_sweep;
145     }
147     double rotation_angle() const
148     {
149         return m_rot_angle;
150     }
152     void setInitial( const Point _point)
153     {
154         m_initial_point = _point;
155         calculate_center_and_extreme_angles();
156     }
158     void setFinal( const Point _point)
159     {
160         m_final_point = _point;
161         calculate_center_and_extreme_angles();
162     }
164     void setExtremes( const Point& _initial_point, const Point& _final_point )
165     {
166         m_initial_point = _initial_point;
167         m_final_point = _final_point;
168         calculate_center_and_extreme_angles();
169     }
171     bool isDegenerate() const
172     {
173         return ( are_near(ray(X), 0) || are_near(ray(Y), 0) );
174     }
176     bool is_svg_compliant() const
177     {
178         return m_svg_compliant;
179     }
181     Rect boundsFast() const
182     {
183         return boundsExact();
184     }
186     Rect boundsExact() const;
188     // TODO: native implementation of the following methods
189     Rect boundsLocal(Interval i, unsigned int deg) const
190     {
191         if (isDegenerate() && is_svg_compliant())
192             return chord().boundsLocal(i, deg);
193         else
194             return SBasisCurve(toSBasis()).boundsLocal(i, deg);
195     }
197     std::vector<double> roots(double v, Dim2 d) const;
199     std::vector<double>
200     allNearestPoints( Point const& p, double from = 0, double to = 1 ) const;
202     double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
203     {
204         if ( are_near(ray(X), ray(Y)) && are_near(center(), p) )
205         {
206             return from;
207         }
208         return allNearestPoints(p, from, to).front();
209     }
211     // TODO: native implementation of the following methods
212     int winding(Point p) const
213     {
214         if (isDegenerate() && is_svg_compliant())
215             return chord().winding(p);
216         else
217             return SBasisCurve(toSBasis()).winding(p);
218     }
220     Curve *derivative() const;
222     Curve *transformed(Matrix const &m) const;
224     std::vector<Point> pointAndDerivatives(Coord t, unsigned int n) const;
226     D2<SBasis> toSBasis() const;
228     bool containsAngle(Coord angle) const;
230     double valueAtAngle(Coord t, Dim2 d) const;
232     Point pointAtAngle(Coord t) const
233     {
234         double sin_rot_angle = std::sin(rotation_angle());
235         double cos_rot_angle = std::cos(rotation_angle());
236         Matrix m( ray(X) * cos_rot_angle, ray(X) * sin_rot_angle,
237                  -ray(Y) * sin_rot_angle, ray(Y) * cos_rot_angle,
238                   center(X),              center(Y) );
239         Point p( std::cos(t), std::sin(t) );
240         return p * m;
241     }
243     double valueAt(Coord t, Dim2 d) const
244     {
245         if (isDegenerate() && is_svg_compliant())
246             return chord().valueAt(t, d);
248         Coord tt = map_to_02PI(t);
249         return valueAtAngle(tt, d);
250     }
252     Point pointAt(Coord t) const
253     {
254         if (isDegenerate() && is_svg_compliant())
255             return chord().pointAt(t);
257         Coord tt = map_to_02PI(t);
258         return pointAtAngle(tt);
259     }
261     std::pair<SVGEllipticalArc, SVGEllipticalArc>
262     subdivide(Coord t) const
263     {
264         SVGEllipticalArc* arc1 = static_cast<SVGEllipticalArc*>(portion(0, t));
265         SVGEllipticalArc* arc2 = static_cast<SVGEllipticalArc*>(portion(t, 1));
266         assert( arc1 != NULL && arc2 != NULL);
267         std::pair<SVGEllipticalArc, SVGEllipticalArc> arc_pair(*arc1, *arc2);
268         delete arc1;
269         delete arc2;
270         return arc_pair;
271     }
273     Curve* portion(double f, double t) const;
275     // the arc is the same but traversed in the opposite direction
276     Curve* reverse() const
277     {
278         SVGEllipticalArc* rarc = new SVGEllipticalArc( *this );
279         rarc->m_sweep = !m_sweep;
280         rarc->m_initial_point = m_final_point;
281         rarc->m_final_point = m_initial_point;
282         rarc->m_start_angle = m_end_angle;
283         rarc->m_end_angle = m_start_angle;
284         return rarc;
285     }
287     double sweep_angle() const
288     {
289         Coord d = end_angle() - start_angle();
290         if ( !sweep_flag() ) d = -d;
291         if ( d < 0 )
292             d += 2*M_PI;
293         return d;
294     }
296     LineSegment chord() const
297     {
298         return LineSegment(initialPoint(), finalPoint());
299     }
301   private:
302     Coord map_to_02PI(Coord t) const;
303     Coord map_to_01(Coord angle) const;
304     void calculate_center_and_extreme_angles();
306   private:
307     Point m_initial_point, m_final_point;
308     double m_rx, m_ry, m_rot_angle;
309     bool m_large_arc, m_sweep;
310     double m_start_angle, m_end_angle;
311     Point m_center;
312     bool m_svg_compliant;
314 }; // end class SVGEllipticalArc
316 template< class charT >
317 inline
318 std::basic_ostream<charT> &
319 operator<< (std::basic_ostream<charT> & os, const SVGEllipticalArc & ea)
321     os << "{ cx: " << ea.center(X) << ", cy: " <<  ea.center(Y)
322        << ", rx: " << ea.ray(X) << ", ry: " << ea.ray(Y)
323        << ", rot angle: " << decimal_round(rad_to_deg(ea.rotation_angle()),2)
324        << ", start angle: " << decimal_round(rad_to_deg(ea.start_angle()),2)
325        << ", end angle: " << decimal_round(rad_to_deg(ea.end_angle()),2)
326        << " }";
328     return os;
334 namespace detail
336     struct ellipse_equation;
340 class make_elliptical_arc
342   public:
343     typedef D2<SBasis> curve_type;
345     make_elliptical_arc( SVGEllipticalArc& _ea,
346                          curve_type const& _curve,
347                          unsigned int _total_samples,
348                          double _tolerance );
350   private:
351     bool bound_exceeded( unsigned int k, detail::ellipse_equation const & ee,
352                          double e1x, double e1y, double e2 );
354     bool check_bound(double A, double B, double C, double D, double E, double F);
356     void fit();
358     bool make_elliptiarc();
360     void print_bound_error(unsigned int k)
361     {
362         std::cerr
363             << "tolerance error" << std::endl
364             << "at point: " << k << std::endl
365             << "error value: "<< dist_err << std::endl
366             << "bound: " << dist_bound << std::endl
367             << "angle error: " << angle_err
368             << " (" << angle_tol << ")" << std::endl;
369     }
371   public:
372     bool operator()()
373     {
374         const NL::Vector & coeff = fitter.result();
375         fit();
376         if ( !check_bound(1, coeff[0], coeff[1], coeff[2], coeff[3], coeff[4]) )
377             return false;
378         if ( !(make_elliptiarc()) ) return false;
379         return true;
380     }
382     bool svg_compliant_flag() const
383     {
384         return svg_compliant;
385     }
387     void svg_compliant_flag(bool _svg_compliant)
388     {
389         svg_compliant = _svg_compliant;
390     }
392   private:
393       SVGEllipticalArc& ea;
394       const curve_type & curve;
395       Piecewise<D2<SBasis> > dcurve;
396       NL::LFMEllipse model;
397       NL::least_squeares_fitter<NL::LFMEllipse> fitter;
398       double tolerance, tol_at_extr, tol_at_center, angle_tol;
399       Point initial_point, final_point;
400       unsigned int N;
401       unsigned int last; // N-1
402       double partitions; // N-1
403       std::vector<Point> p; // sample points
404       double dist_err, dist_bound, angle_err;
405       bool svg_compliant;
406 };
409 } // end namespace Geom
414 #endif /* _2GEOM_SVG_ELLIPTICAL_ARC_H_ */
416 /*
417   Local Variables:
418   mode:c++
419   c-file-style:"stroustrup"
420   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
421   indent-tabs-mode:nil
422   fill-column:99
423   End:
424 */
425 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :