Code

Commit LivePathEffect branch to trunk!
[inkscape.git] / src / 2geom / bezier.h
1 /*
2  * bezier.h
3  *
4  * Copyright 2007  MenTaLguY <mental@rydia.net>
5  * Copyright 2007  Michael Sloan <mgsloan@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  */
32 #ifndef SEEN_BEZIER_H
33 #define SEEN_BEZIER_H
35 #include "coord.h"
36 #include "isnan.h"
37 #include "bezier-to-sbasis.h"
39 namespace Geom {
41 template <unsigned order>
42 class Bezier {
43 private:
44   Coord c_[order+1];
46 protected:
47   Bezier(Coord c[]) {
48     std::copy(c, c+order+1, c_);
49   }
51 public:
52   template <unsigned required_order>
53   static void assert_order(Bezier<required_order> const *) {}
55   Bezier() {}
57   //Construct an order-0 bezier (constant Bézier)
58   explicit Bezier(Coord c0) {
59     assert_order<0>(this);
60     c_[0] = c0;
61   }
63   //Construct an order-1 bezier (linear Bézier)
64   Bezier(Coord c0, Coord c1) {
65     assert_order<1>(this);
66     c_[0] = c0; c_[1] = c1;
67   }
69   //Construct an order-2 bezier (quadratic Bézier)
70   Bezier(Coord c0, Coord c1, Coord c2) {
71     assert_order<2>(this);
72     c_[0] = c0; c_[1] = c1; c_[2] = c2;
73   }
75   //Construct an order-3 bezier (cubic Bézier)
76   Bezier(Coord c0, Coord c1, Coord c2, Coord c3) {
77     assert_order<3>(this);
78     c_[0] = c0; c_[1] = c1; c_[2] = c2; c_[3] = c3;
79   }
81   inline unsigned degree() const { return order; }
83   //IMPL: FragmentConcept
84   typedef Coord output_type;
85   inline bool isZero() const { 
86      for(int i = 0; i <= order; i++) {
87        if(c_[i] != 0) return false;
88      }
89      return true;
90   }
91   inline bool isFinite() const {
92     for(int i = 0; i <= order; i++) {
93       if(!is_finite(c_[i])) return false;
94     }
95     return true;
96   }
97   inline Coord at0() const { return c_[0]; }
98   inline Coord at1() const { return c_[order]; }
100   inline SBasis toSBasis() const { return bezier_to_sbasis<order>(c_); }
102   inline Interval bounds_fast() const { return Interval::fromArray(c_, order+1); }
103   //TODO: better bounds exact
104   inline Interval bounds_exact() const { return toSBasis().bounds_exact(); }
105   inline Interval bounds_local(double u, double v) const { return toSBasis().bounds_local(u, v); }
107   //Only mutator
108   inline Coord &operator[](int index) { return c_[index]; }
109   inline Coord const &operator[](int index) const { return c_[index]; }
111   Maybe<int> winding(Point p) const {
112     return sbasis_winding(toSBasis(), p);
113   }
114   
115   Point pointAndDerivativesAt(Coord t, unsigned n_derivs, Point *derivs) const {
116     //TODO
117     return Point(0,0);
118   }
119 };
121 template<unsigned order>
122 Bezier<order> reverse(const Bezier<order> & a) {
123   Bezier<order> result;
124   for(int i = 0; i <= order; i++)
125     result[i] = a[order - i];
126   return result;
129 template<unsigned order>
130 vector<Point> bezier_points(const D2<Bezier<order> > & a) {
131   vector<Point> result;
132   for(int i = 0; i <= order; i++) {
133     Point p;
134     for(unsigned d = 0; d < 2; d++) p[d] = a[d][i];
135     result[i] = p;
136   }
137   return result;
141 #endif //SEEN_BEZIER_H