Code

135aa4f71e574785dcd2d3de05411b33b96d600f
[inkscape.git] / src / 2geom / bezier-curve.h
1 /**
2  * \file
3  * \brief Bezier-Curve
4  *
5  * Authors:
6  *              MenTaLguY <mental@rydia.net>
7  *              Marco Cecchetti <mrcekets at gmail.com>
8  * 
9  * Copyright 2007-2008  authors
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it either under the terms of the GNU Lesser General Public
13  * License version 2.1 as published by the Free Software Foundation
14  * (the "LGPL") or, at your option, under the terms of the Mozilla
15  * Public License Version 1.1 (the "MPL"). If you do not alter this
16  * notice, a recipient may use your version of this file under either
17  * the MPL or the LGPL.
18  *
19  * You should have received a copy of the LGPL along with this library
20  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  * You should have received a copy of the MPL along with this library
23  * in the file COPYING-MPL-1.1
24  *
25  * The contents of this file are subject to the Mozilla Public License
26  * Version 1.1 (the "License"); you may not use this file except in
27  * compliance with the License. You may obtain a copy of the License at
28  * http://www.mozilla.org/MPL/
29  *
30  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
32  * the specific language governing rights and limitations.
33  */
38 #ifndef _2GEOM_BEZIER_CURVE_H_
39 #define _2GEOM_BEZIER_CURVE_H_
42 #include <2geom/curve.h>
43 #include <2geom/sbasis-curve.h> // for non-native winding method
44 #include <2geom/bezier.h>
46 #include <algorithm>
49 namespace Geom 
50 {
54 template <unsigned order>
55 class BezierCurve : public Curve {
56         
57 private:
58   D2<Bezier > inner;
59   
60 public:
61   template <unsigned required_degree>
62   static void assert_degree(BezierCurve<required_degree> const *) {}
64   BezierCurve() : inner(Bezier::Order(order), Bezier::Order(order)) {
65   }
67   explicit BezierCurve(D2<Bezier > const &x) : inner(x) {}
69   BezierCurve(Bezier x, Bezier y) : inner(x, y) {}
71   // default copy
72   // default assign
74   BezierCurve(Point c0, Point c1) {
75     assert_degree<1>(this);
76     for(unsigned d = 0; d < 2; d++)
77         inner[d] = Bezier(c0[d], c1[d]);
78   }
80   BezierCurve(Point c0, Point c1, Point c2) {
81     assert_degree<2>(this);
82     for(unsigned d = 0; d < 2; d++)
83         inner[d] = Bezier(c0[d], c1[d], c2[d]);
84   }
86   BezierCurve(Point c0, Point c1, Point c2, Point c3) {
87     assert_degree<3>(this);
88     for(unsigned d = 0; d < 2; d++)
89         inner[d] = Bezier(c0[d], c1[d], c2[d], c3[d]);
90   }
92   unsigned degree() const { return order; }
94   Curve *duplicate() const { return new BezierCurve(*this); }
96   Point initialPoint() const { return inner.at0(); }
97   Point finalPoint() const { return inner.at1(); }
99   bool isDegenerate() const { return inner.isConstant(); }
101   void setInitial(Point v) { setPoint(0, v); }
102   void setFinal(Point v)   { setPoint(order, v); }
104   void setPoint(unsigned ix, Point v) { inner[X].setPoint(ix, v[X]); inner[Y].setPoint(ix, v[Y]); }
105   Point const operator[](unsigned ix) const { return Point(inner[X][ix], inner[Y][ix]); }
107   Rect boundsFast() const { return bounds_fast(inner); }
108   Rect boundsExact() const { return bounds_exact(inner); }
109   Rect boundsLocal(Interval i, unsigned deg) const {
110       if(i.min() == 0 && i.max() == 1) return boundsFast();
111       if(deg == 0) return bounds_local(inner, i);
112       // TODO: UUUUUUGGGLLY
113       if(deg == 1 && order > 1) return Rect(bounds_local(Geom::derivative(inner[X]), i),
114                                             bounds_local(Geom::derivative(inner[Y]), i));
115       return Rect(Interval(0,0), Interval(0,0));
116   }
117 //TODO: local
119 //TODO: implement next 3 natively
120   int winding(Point p) const {
121     return SBasisCurve(toSBasis()).winding(p);
122   }
124   std::vector<double>
125   roots(double v, Dim2 d) const {
126       return (inner[d] - v).roots();
127   }
128   
129   double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
130   {
131           return Curve::nearestPoint(p, from, to);
132   }
133   
134   void setPoints(std::vector<Point> ps) {
135     for(unsigned i = 0; i <= order; i++) {
136       setPoint(i, ps[i]);
137     }
138   }
139   std::vector<Point> points() const { return bezier_points(inner); }
141   std::pair<BezierCurve<order>, BezierCurve<order> > subdivide(Coord t) const {
142     std::pair<Bezier, Bezier > sx = inner[X].subdivide(t), sy = inner[Y].subdivide(t);
143     return std::pair<BezierCurve<order>, BezierCurve<order> >(
144                BezierCurve<order>(sx.first, sy.first),
145                BezierCurve<order>(sx.second, sy.second));
146   }
148   Curve *portion(double f, double t) const {
149     return new BezierCurve(Geom::portion(inner, f, t));
150   }
152   Curve *reverse() const {
153     return new BezierCurve(Geom::reverse(inner));
154   }
156   Curve *transformed(Matrix const &m) const {
157     BezierCurve *ret = new BezierCurve();
158     std::vector<Point> ps = points();
159     for(unsigned i = 0;  i <= order; i++) ps[i] = ps[i] * m;
160     ret->setPoints(ps);
161     return ret;
162   }
164   Curve *derivative() const {
165      if(order > 1)
166         return new BezierCurve<order-1>(Geom::derivative(inner[X]), Geom::derivative(inner[Y]));
167      else if (order == 1) {
168         double dx = inner[X][1] - inner[X][0], dy = inner[Y][1] - inner[Y][0];
169         return new BezierCurve<1>(Point(dx,dy),Point(dx,dy));
170      }
171   }
173   Point pointAt(double t) const { return inner.valueAt(t); }
174   std::vector<Point> pointAndDerivatives(Coord t, unsigned n) const { return inner.valueAndDerivatives(t, n); }
176   double valueAt(double t, Dim2 d) const { return inner[d].valueAt(t); }
178   D2<SBasis> toSBasis() const {return inner.toSBasis(); }
180 protected:
181   BezierCurve(Point c[]) {
182     Coord x[order+1], y[order+1];
183     for(unsigned i = 0; i <= order; i++) {
184         x[i] = c[i][X]; y[i] = c[i][Y];
185     }
186     inner = Bezier(x, y);
187   }
188 };
190 // BezierCurve<0> is meaningless; specialize it out
191 template<> class BezierCurve<0> : public BezierCurve<1> { public: BezierCurve(); BezierCurve(Bezier x, Bezier y); };
193 typedef BezierCurve<1> LineSegment;
194 typedef BezierCurve<2> QuadraticBezier;
195 typedef BezierCurve<3> CubicBezier;
198 template<>
199 inline
200 double LineSegment::nearestPoint(Point const& p, double from, double to) const
202         if ( from > to ) std::swap(from, to);
203         Point ip = pointAt(from);
204         Point fp = pointAt(to);
205         Point v = fp - ip;
206         double l2v = L2sq(v);
207         if(l2v == 0) return 0;
208         double t = dot( p - ip, v ) / l2v;
209         if ( t <= 0 )           return from;
210         else if ( t >= 1 )  return to;
211         else                            return from + t*(to-from);
214 inline
215 Point middle_point(LineSegment const& _segment)
217         return ( _segment.initialPoint() + _segment.finalPoint() ) / 2;
220 inline
221 double length(LineSegment const& _segment)
223         return distance(_segment.initialPoint(), _segment.finalPoint());
226 } // end namespace Geom
229 #endif // _2GEOM_BEZIER_CURVE_H_
234 /*
235   Local Variables:
236   mode:c++
237   c-file-style:"stroustrup"
238   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239   indent-tabs-mode:nil
240   fill-column:99
241   End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :