~ubuntu-branches/ubuntu/oneiric/inkscape/oneiric-updates

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/2geom/curve.h

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
34
 
 
35
 
 
36
 
 
37
 
 
38
#ifndef _2GEOM_CURVE_H_
 
39
#define _2GEOM_CURVE_H_
 
40
 
 
41
 
 
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>
 
50
 
 
51
#include <vector>
 
52
 
 
53
 
 
54
namespace Geom 
 
55
{
 
56
 
 
57
class Curve;
 
58
 
 
59
struct CurveHelpers {
 
60
protected:
 
61
  static int root_winding(Curve const &c, Point p);
 
62
};
 
63
 
 
64
class Curve : private CurveHelpers {
 
65
public:
 
66
  virtual ~Curve() {}
 
67
 
 
68
  virtual Point initialPoint() const = 0;
 
69
  virtual Point finalPoint() const = 0;
 
70
 
 
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;
 
74
 
 
75
  virtual Curve *duplicate() const = 0;
 
76
 
 
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); }
 
81
 
 
82
  virtual std::vector<double> roots(double v, Dim2 d) const = 0;
 
83
 
 
84
  virtual int winding(Point p) const { return root_winding(*this, p); }
 
85
 
 
86
  virtual int degreesOfFreedom() const { return 0;}
 
87
 
 
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;
 
92
 
 
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
  }
 
108
 
 
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
  */
 
116
 
 
117
  virtual Curve *transformed(Matrix const &m) const = 0;
 
118
 
 
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;
 
126
 
 
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
  };
 
148
 
 
149
  virtual D2<SBasis> toSBasis() const = 0;
 
150
  virtual bool operator==(Curve const &c) const { return this == &c;}
 
151
};
 
152
 
 
153
inline
 
154
Coord nearest_point(Point const& p, Curve const& c)
 
155
{
 
156
        return c.nearestPoint(p);
 
157
}
 
158
 
 
159
} // end namespace Geom
 
160
 
 
161
 
 
162
#endif // _2GEOM_CURVE_H_
 
163
 
 
164
 
 
165
 
 
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:encoding=utf-8:textwidth=99 :