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 //mental: review these
87 virtual Curve *portion(double f, double t) const = 0;
88 virtual Curve *reverse() const { return portion(1, 0); }
89 virtual Curve *derivative() const = 0;
91 virtual void setInitial(Point v) = 0;
92 virtual void setFinal(Point v) = 0;
94 virtual
95 double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
96 {
97 return nearest_point(p, toSBasis(), from, to);
98 }
100 virtual
101 std::vector<double>
102 allNearestPoints( Point const& p, double from = 0, double to = 1 ) const
103 {
104 return all_nearest_points(p, toSBasis(), from, to);
105 }
107 /*
108 Path operator*=(Matrix)
109 This is not possible, because:
110 A Curve can be many things, for example a HLineSegment.
111 Such a segment cannot be transformed and stay a HLineSegment in general (take for example rotations).
112 This means that these curves become a different type of curve, hence one should use "transformed(Matrix).
113 */
115 virtual Curve *transformed(Matrix const &m) const = 0;
117 virtual Point pointAt(Coord t) const { return pointAndDerivatives(t, 0).front(); }
118 virtual Coord valueAt(Coord t, Dim2 d) const { return pointAt(t)[d]; }
119 virtual Point operator() (double t) const { return pointAt(t); }
121 /* pointAndDerivatives returns a vector that looks like the following:
122 * [ point at t, 1st derivative at t, 2nd derivative at t, ... , n'th derivative at t] */
123 virtual std::vector<Point> pointAndDerivatives(Coord t, unsigned n) const = 0;
125 /* unitTangentAt returns the unit vector tangent to the curve at position t
126 * (in the direction of increasing t). The method uses l'Hopital's rule when the derivative
127 * is (0,0), parameter n determines the maximum nr of iterations (for when higher derivatives are also (0,0) ).
128 * Point(0,0) is returned if no non-zero derivative could be found.
129 * Note that unitTangentAt(1) will probably not give the desired result. Probably one should do:
130 * Curve * c_reverse = c.reverse();
131 * Point tangent = - c_reverse->unitTangentAt(0);
132 * delete c_reverse;
133 */
134 virtual Point unitTangentAt(Coord t, unsigned n = 3) const
135 {
136 std::vector<Point> derivs = pointAndDerivatives(t, n);
137 for (unsigned deriv_n = 1; deriv_n < derivs.size(); deriv_n++) {
138 Coord length = derivs[deriv_n].length();
139 if ( ! are_near(length, 0) ) {
140 // length of derivative is non-zero, so return unit vector
141 derivs[deriv_n] / length;
142 }
143 }
144 return Point (0,0);
145 };
147 virtual D2<SBasis> toSBasis() const = 0;
148 virtual bool operator==(Curve const &c) const { return this == &c;}
149 };
151 inline
152 Coord nearest_point(Point const& p, Curve const& c)
153 {
154 return c.nearestPoint(p);
155 }
157 } // end namespace Geom
160 #endif // _2GEOM_CURVE_H_
164 /*
165 Local Variables:
166 mode:c++
167 c-file-style:"stroustrup"
168 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
169 indent-tabs-mode:nil
170 fill-column:99
171 End:
172 */
173 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :