Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / 2geom / curve.h
1 /**
2  * \file
3  * \brief   Abstract Curve Type
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_CURVE_H_
39 #define _2GEOM_CURVE_H_
42 #include <2geom/coord.h>
43 #include <2geom/point.h>
44 #include <2geom/interval.h>
45 #include <2geom/nearest-point.h>
46 #include <2geom/sbasis.h>
47 #include <2geom/d2.h>
48 #include <2geom/matrix.h>
49 #include <2geom/exception.h>
51 #include <vector>
54 namespace Geom 
55 {
57 class Curve;
59 struct CurveHelpers {
60 protected:
61   static int root_winding(Curve const &c, Point p);
62 };
64 class Curve : private CurveHelpers {
65 public:
66   virtual ~Curve() {}
68   virtual Point initialPoint() const = 0;
69   virtual Point finalPoint() const = 0;
71   /* isDegenerate returns true if the curve has "zero length".
72    * For a bezier curve this means for example that all handles are at the same point */
73   virtual bool isDegenerate() const = 0;
75   virtual Curve *duplicate() const = 0;
77   virtual OptRect boundsFast() const = 0;
78   virtual OptRect boundsExact() const = 0;
79   virtual OptRect boundsLocal(OptInterval i, unsigned deg) const = 0;
80   OptRect boundsLocal(OptInterval i) const { return boundsLocal(i, 0); }
82   virtual std::vector<double> roots(double v, Dim2 d) const = 0;
84   virtual int winding(Point p) const { return root_winding(*this, p); }
86   virtual int degreesOfFreedom() const { return 0;}
88   //mental: review these
89   virtual Curve *portion(double f, double t) const = 0;
90   virtual Curve *reverse() const { return portion(1, 0); }
91   virtual Curve *derivative() const = 0;
93   virtual void setInitial(Point v) = 0;
94   virtual void setFinal(Point v) = 0;
95   
96   virtual
97   double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
98   {
99           return nearest_point(p, toSBasis(), from, to);
100   }
101   
102   virtual
103   std::vector<double> 
104   allNearestPoints( Point const& p, double from = 0, double to = 1 ) const
105   {
106           return all_nearest_points(p, toSBasis(), from, to);
107   }
109   /*
110   Path operator*=(Matrix)
111   This is not possible, because:
112   A Curve can be many things, for example a HLineSegment.
113   Such a segment cannot be transformed and stay a HLineSegment in general (take for example rotations).
114   This means that these curves become a different type of curve, hence one should use "transformed(Matrix).
115   */
117   virtual Curve *transformed(Matrix const &m) const = 0;
119   virtual Point pointAt(Coord t) const { return pointAndDerivatives(t, 0).front(); }
120   virtual Coord valueAt(Coord t, Dim2 d) const { return pointAt(t)[d]; }
121   virtual Point operator() (double t)  const { return pointAt(t); }
122   
123   /* pointAndDerivatives returns a vector that looks like the following:
124    *  [ point at t, 1st derivative at t, 2nd derivative at t, ... , n'th derivative at t] */
125   virtual std::vector<Point> pointAndDerivatives(Coord t, unsigned n) const = 0;
127   /* unitTangentAt returns the unit vector tangent to the curve at position t
128    * (in the direction of increasing t). The method uses l'Hopital's rule when the derivative
129    * is (0,0), parameter n determines the maximum nr of iterations (for when higher derivatives are also (0,0) ).
130    * Point(0,0) is returned if no non-zero derivative could be found. 
131    * Note that unitTangentAt(1) will probably not give the desired result. Probably one should do:
132    *    Curve * c_reverse = c.reverse();
133    *    Point tangent = - c_reverse->unitTangentAt(0);
134    *    delete c_reverse;
135    */
136   virtual Point unitTangentAt(Coord t, unsigned n = 3) const
137   {
138     std::vector<Point> derivs = pointAndDerivatives(t, n);
139     for (unsigned deriv_n = 1; deriv_n < derivs.size(); deriv_n++) {
140       Coord length = derivs[deriv_n].length();
141       if ( ! are_near(length, 0) ) {
142          // length of derivative is non-zero, so return unit vector
143           return derivs[deriv_n] / length;
144       }
145     }
146     return Point (0,0);
147   };
149   virtual D2<SBasis> toSBasis() const = 0;
150   virtual bool operator==(Curve const &c) const { return this == &c;}
151 };
153 inline
154 Coord nearest_point(Point const& p, Curve const& c)
156         return c.nearestPoint(p);
159 } // end namespace Geom
162 #endif // _2GEOM_CURVE_H_
166 /*
167   Local Variables:
168   mode:c++
169   c-file-style:"stroustrup"
170   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
171   indent-tabs-mode:nil
172   fill-column:99
173   End:
174 */
175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :