1 /*
2 * Abstract Curve Type
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 */
37 #ifndef _2GEOM_CURVE_H_
38 #define _2GEOM_CURVE_H_
41 #include <2geom/coord.h>
42 #include <2geom/point.h>
43 #include <2geom/interval.h>
44 #include <2geom/nearest-point.h>
45 #include <2geom/sbasis.h>
46 #include <2geom/d2.h>
47 #include <2geom/matrix.h>
48 #include <2geom/exception.h>
50 #include <vector>
53 namespace Geom
54 {
56 class Curve;
58 struct CurveHelpers {
59 protected:
60 static int root_winding(Curve const &c, Point p);
61 };
63 class Curve : private CurveHelpers {
64 public:
65 virtual ~Curve() {}
67 virtual Point initialPoint() const = 0;
68 virtual Point finalPoint() const = 0;
70 virtual bool isDegenerate() const = 0;
72 virtual Curve *duplicate() const = 0;
74 virtual Rect boundsFast() const = 0;
75 virtual Rect boundsExact() const = 0;
76 virtual Rect boundsLocal(Interval i, unsigned deg) const = 0;
77 Rect boundsLocal(Interval i) const { return boundsLocal(i, 0); }
79 virtual std::vector<double> roots(double v, Dim2 d) const = 0;
81 virtual int winding(Point p) const { return root_winding(*this, p); }
83 //mental: review these
84 virtual Curve *portion(double f, double t) const = 0;
85 virtual Curve *reverse() const { return portion(1, 0); }
86 virtual Curve *derivative() const = 0;
88 virtual void setInitial(Point v) = 0;
89 virtual void setFinal(Point v) = 0;
91 virtual
92 double nearestPoint( Point const& p, double from = 0, double to = 1 ) const
93 {
94 return nearest_point(p, toSBasis(), from, to);
95 }
97 virtual
98 std::vector<double>
99 allNearestPoints( Point const& p, double from = 0, double to = 1 ) const
100 {
101 return all_nearest_points(p, toSBasis(), from, to);
102 }
104 /*
105 Path operator*=(Matrix)
106 This is not possible, because:
107 A Curve can be many things, for example a HLineSegment.
108 Such a segment cannot be transformed and stay a HLineSegment in general (take for example rotations).
109 This means that these curves become a different type of curve, hence one should use "transformed(Matrix).
110 */
112 virtual Curve *transformed(Matrix const &m) const = 0;
114 virtual Point pointAt(Coord t) const { return pointAndDerivatives(t, 0).front(); }
115 virtual Coord valueAt(Coord t, Dim2 d) const { return pointAt(t)[d]; }
116 virtual Point operator() (double t) const { return pointAt(t); }
118 /* pointAndDerivatives returns a vector that looks like the following:
119 * [ point at t, 1st derivative at t, 2nd derivative at t, ... , n'th derivative at t] */
120 virtual std::vector<Point> pointAndDerivatives(Coord t, unsigned n) const = 0;
122 /* unitTangentAt returns the unit vector tangent to the curve at position t
123 * (in the direction of increasing t). The method uses l'Hopital's rule when the derivative
124 * is (0,0), parameter n determines the maximum nr of iterations (for when higher derivatives are also (0,0) ).
125 * Point(0,0) is returned if no non-zero derivative could be found. */
126 virtual Point unitTangentAt(Coord t, unsigned n = 3) const
127 {
128 for (unsigned deriv_n = 1; deriv_n <= n; deriv_n++) {
129 Point deriv = pointAndDerivatives(t, deriv_n)[deriv_n];
130 Coord length = deriv.length();
131 if ( ! are_near(length, 0) ) {
132 // length of derivative is non-zero, so return unit vector
133 return deriv / length;
134 }
135 }
136 return Point (0,0);
137 };
139 virtual D2<SBasis> toSBasis() const = 0;
140 virtual bool operator==(Curve const &c) const { return this == &c;}
141 };
143 inline
144 Coord nearest_point(Point const& p, Curve const& c)
145 {
146 return c.nearestPoint(p);
147 }
149 } // end namespace Geom
152 #endif // _2GEOM_CURVE_H_
156 /*
157 Local Variables:
158 mode:c++
159 c-file-style:"stroustrup"
160 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
161 indent-tabs-mode:nil
162 fill-column:99
163 End:
164 */
165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :