Code

Make curvature work again by fixing a minor omission
[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   virtual OptRect boundsFast() const { return bounds_fast(inner); }
108   virtual OptRect boundsExact() const { return bounds_exact(inner); }
109   virtual OptRect boundsLocal(OptInterval i, unsigned deg) const {
110       if (!i) return OptRect();
111       if(i->min() == 0 && i->max() == 1) return boundsFast();
112       if(deg == 0) return bounds_local(inner, i);
113       // TODO: UUUUUUGGGLLY
114       if(deg == 1 && order > 1) return OptRect(bounds_local(Geom::derivative(inner[X]), i),
115                                                bounds_local(Geom::derivative(inner[Y]), i));
116       return OptRect();
117   }
118 //TODO: local
120 //TODO: implement next 3 natively
121   int winding(Point p) const {
122     return SBasisCurve(toSBasis()).winding(p);
123   }
124   
125   virtual int degreesOfFreedom() const {
126     return 2*order;
127   }
129   std::vector<double>
130   roots(double v, Dim2 d) const {
131       return (inner[d] - v).roots();
132   }
133   
134   double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
135   {
136           return Curve::nearestPoint(p, from, to);
137   }
138   
139   void setPoints(std::vector<Point> ps) {
140     for(unsigned i = 0; i <= order; i++) {
141       setPoint(i, ps[i]);
142     }
143   }
144   std::vector<Point> points() const { return bezier_points(inner); }
146   std::pair<BezierCurve<order>, BezierCurve<order> > subdivide(Coord t) const {
147     std::pair<Bezier, Bezier > sx = inner[X].subdivide(t), sy = inner[Y].subdivide(t);
148     return std::pair<BezierCurve<order>, BezierCurve<order> >(
149                BezierCurve<order>(sx.first, sy.first),
150                BezierCurve<order>(sx.second, sy.second));
151   }
153   Curve *portion(double f, double t) const {
154     return new BezierCurve(Geom::portion(inner, f, t));
155   }
157   Curve *reverse() const {
158     return new BezierCurve(Geom::reverse(inner));
159   }
161   Curve *transformed(Matrix const &m) const {
162     BezierCurve *ret = new BezierCurve();
163     std::vector<Point> ps = points();
164     for(unsigned i = 0;  i <= order; i++) ps[i] = ps[i] * m;
165     ret->setPoints(ps);
166     return ret;
167   }
169     Curve *derivative() const;
170     
171   Point pointAt(double t) const { return inner.valueAt(t); }
172   std::vector<Point> pointAndDerivatives(Coord t, unsigned n) const { return inner.valueAndDerivatives(t, n); }
174   double valueAt(double t, Dim2 d) const { return inner[d].valueAt(t); }
176   D2<SBasis> toSBasis() const {return inner.toSBasis(); }
178 protected:
179   BezierCurve(Point c[]) {
180     Coord x[order+1], y[order+1];
181     for(unsigned i = 0; i <= order; i++) {
182         x[i] = c[i][X]; y[i] = c[i][Y];
183     }
184     inner = Bezier(x, y);
185   }
186 };
188 // BezierCurve<0> is meaningless; specialize it out
189 template<> class BezierCurve<0> : public BezierCurve<1> { public: BezierCurve();};
191 typedef BezierCurve<1> LineSegment;
192 typedef BezierCurve<2> QuadraticBezier;
193 typedef BezierCurve<3> CubicBezier;
196 template<>
197 inline
198 double LineSegment::nearestPoint(Point const& p, double from, double to) const
200         if ( from > to ) std::swap(from, to);
201         Point ip = pointAt(from);
202         Point fp = pointAt(to);
203         Point v = fp - ip;
204         double l2v = L2sq(v);
205         if(l2v == 0) return 0;
206         double t = dot( p - ip, v ) / l2v;
207         if ( t <= 0 )           return from;
208         else if ( t >= 1 )  return to;
209         else                            return from + t*(to-from);
212 inline
213 Point middle_point(LineSegment const& _segment)
215         return ( _segment.initialPoint() + _segment.finalPoint() ) / 2;
218 inline
219 double length(LineSegment const& _segment)
221         return distance(_segment.initialPoint(), _segment.finalPoint());
224 template <unsigned order>
225 inline
226 Curve *BezierCurve<order>::derivative() const {
227     return new BezierCurve<order-1>(Geom::derivative(inner[X]), Geom::derivative(inner[Y]));
230 template <>
231 inline
232 Curve *BezierCurve<1>::derivative() const {
233     double dx = inner[X][1] - inner[X][0], dy = inner[Y][1] - inner[Y][0];
234     return new BezierCurve<1>(Point(dx,dy),Point(dx,dy));
238 } // end namespace Geom
241 #endif // _2GEOM_BEZIER_CURVE_H_
246 /*
247   Local Variables:
248   mode:c++
249   c-file-style:"stroustrup"
250   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
251   indent-tabs-mode:nil
252   fill-column:99
253   End:
254 */
255 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :