Code

Don't force focus on the canvas when the desktop is given
[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  * Copyright 2007  Nathan Hurst <njh@njhurst.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it either under the terms of the GNU Lesser General Public
10  * License version 2.1 as published by the Free Software Foundation
11  * (the "LGPL") or, at your option, under the terms of the Mozilla
12  * Public License Version 1.1 (the "MPL"). If you do not alter this
13  * notice, a recipient may use your version of this file under either
14  * the MPL or the LGPL.
15  *
16  * You should have received a copy of the LGPL along with this library
17  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  * You should have received a copy of the MPL along with this library
20  * in the file COPYING-MPL-1.1
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License at
25  * http://www.mozilla.org/MPL/
26  *
27  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29  * the specific language governing rights and limitations.
30  *
31  */
33 #ifndef SEEN_BEZIER_H
34 #define SEEN_BEZIER_H
36 #include "coord.h"
37 #include <valarray>
38 #include "isnan.h"
39 #include "bezier-to-sbasis.h"
40 #include "d2.h"
41 #include "solver.h"
42 #include <boost/optional/optional.hpp>
44 namespace Geom {
46 inline Coord subdivideArr(Coord t, Coord const *v, Coord *left, Coord *right, unsigned order) {
47     Coord vtemp[order+1][order+1];
49     /* Copy control points      */
50     std::copy(v, v+order+1, vtemp[0]);
52     /* Triangle computation     */
53     for (unsigned i = 1; i <= order; i++) {     
54         for (unsigned j = 0; j <= order - i; j++) {
55             vtemp[i][j] = lerp(t, vtemp[i-1][j], vtemp[i-1][j+1]);
56         }
57     }
58     if(left != NULL)
59         for (unsigned j = 0; j <= order; j++)
60             left[j]  = vtemp[j][0];
61     if(right != NULL)
62         for (unsigned j = 0; j <= order; j++)
63             right[j] = vtemp[order-j][j];
65     return (vtemp[order][0]);
66 }
69 class Bezier {
70 private:
71     std::valarray<Coord> c_;
73     friend Bezier portion(const Bezier & a, Coord from, Coord to);
75     friend Interval bounds_fast(Bezier const & b);
77     friend Bezier derivative(const Bezier & a);
79 protected:
80     Bezier(Coord const c[], unsigned ord) : c_(c, ord+1){
81         //std::copy(c, c+order()+1, &c_[0]);
82     }
84 public:
85     unsigned int order() const { return c_.size()-1;}
86     unsigned int size() const { return c_.size();}
87     
88     Bezier() :c_(0., 32) {}
89     Bezier(const Bezier& b) :c_(b.c_) {}
90     Bezier &operator=(Bezier const &other) {
91         if ( c_.size() != other.c_.size() ) {
92             c_.resize(other.c_.size());
93         }
94         c_ = other.c_;
95         return *this;
96     }
98     struct Order {
99         unsigned order;
100         explicit Order(Bezier const &b) : order(b.order()) {}
101         explicit Order(unsigned o) : order(o) {}
102         operator unsigned() const { return order; }
103     };
105     //Construct an arbitrary order bezier
106     Bezier(Order ord) : c_(0., ord.order+1) {
107         assert(ord.order ==  order());
108     }
110     explicit Bezier(Coord c0) : c_(0., 1) {
111         c_[0] = c0;
112     }
114     //Construct an order-1 bezier (linear Bézier)
115     Bezier(Coord c0, Coord c1) : c_(0., 2) {
116         c_[0] = c0; c_[1] = c1;
117     }
119     //Construct an order-2 bezier (quadratic Bézier)
120     Bezier(Coord c0, Coord c1, Coord c2) : c_(0., 3) {
121         c_[0] = c0; c_[1] = c1; c_[2] = c2;
122     }
124     //Construct an order-3 bezier (cubic Bézier)
125     Bezier(Coord c0, Coord c1, Coord c2, Coord c3) : c_(0., 4) {
126         c_[0] = c0; c_[1] = c1; c_[2] = c2; c_[3] = c3;
127     }
129     inline unsigned degree() const { return order(); }
131     //IMPL: FragmentConcept
132     typedef Coord output_type;
133     inline bool isZero() const { 
134         for(unsigned i = 0; i <= order(); i++) {
135             if(c_[i] != 0) return false;
136         }
137         return true;
138     }
139     inline bool isFinite() const {
140         for(unsigned i = 0; i <= order(); i++) {
141             if(!is_finite(c_[i])) return false;
142         }
143         return true;
144     }
145     inline Coord at0() const { return c_[0]; }
146     inline Coord at1() const { return c_[order()]; }
148     inline Coord valueAt(double t) const { 
149         return subdivideArr(t, &c_[0], NULL, NULL, order()); 
150     }
151     inline Coord operator()(double t) const { return valueAt(t); }
153     inline SBasis toSBasis() const { 
154         return bezier_to_sbasis(&c_[0], order());
155     }
157     //Only mutator
158     inline Coord &operator[](unsigned ix) { return c_[ix]; }
159     inline Coord const &operator[](unsigned ix) const { return c_[ix]; }
160     inline void setPoint(unsigned ix, double val) { c_[ix] = val; }
161     
162     /* This is inelegant, as it uses several extra stores.  I think there might be a way to
163      * evaluate roughly in situ. */
164     
165     std::vector<Coord> valueAndDerivatives(Coord t, unsigned n_derivs) const {
166         std::vector<Coord> val_n_der;
167         Coord d_[order()+1];
168         unsigned nn = n_derivs;
169         if(nn > order())
170             nn = order();
171         for(unsigned i = 0; i < size(); i++)
172             d_[i] = c_[i];
173         for(unsigned di = 0; di < nn; di++) {
174             val_n_der.push_back(subdivideArr(t, d_, NULL, NULL, order() - di));
175             for(unsigned i = 0; i < order() - di; i++) {
176                 d_[i] = (order()-di)*(d_[i+1] - d_[i]);
177             }
178         }
179         val_n_der.resize(n_derivs);
180         return val_n_der;
181     }
182   
183     std::pair<Bezier, Bezier > subdivide(Coord t) const {
184         Bezier a(Bezier::Order(*this)), b(Bezier::Order(*this));
185         subdivideArr(t, &c_[0], &a.c_[0], &b.c_[0], order());
186         return std::pair<Bezier, Bezier >(a, b);
187     }
189     std::vector<double> roots() const {
190         std::vector<double> solutions;
191         find_bernstein_roots(&c_[0], order(), solutions, 0, 0.0, 1.0);
192         return solutions;
193     }
194 };
196 //TODO: implement others
197 inline Bezier operator+(const Bezier & a, double v) {
198     Bezier result = Bezier(Bezier::Order(a));
199     for(unsigned i = 0; i <= a.order(); i++)
200         result[i] = a[i] + v;
201     return result;
204 inline Bezier operator-(const Bezier & a, double v) {
205     Bezier result = Bezier(Bezier::Order(a));
206     for(unsigned i = 0; i <= a.order(); i++)
207         result[i] = a[i] - v;
208     return result;
211 inline Bezier operator*(const Bezier & a, double v) {
212     Bezier result = Bezier(Bezier::Order(a));
213     for(unsigned i = 0; i <= a.order(); i++)
214         result[i] = a[i] * v;
215     return result;
218 inline Bezier operator/(const Bezier & a, double v) {
219     Bezier result = Bezier(Bezier::Order(a));
220     for(unsigned i = 0; i <= a.order(); i++)
221         result[i] = a[i] / v;
222     return result;
225 inline Bezier reverse(const Bezier & a) {
226     Bezier result = Bezier(Bezier::Order(a));
227     for(unsigned i = 0; i <= a.order(); i++)
228         result[i] = a[a.order() - i];
229     return result;
232 inline Bezier portion(const Bezier & a, double from, double to) {
233     //TODO: implement better?
234     Coord res[a.order()+1];
235     if(from == 0) {
236         if(to == 1) { return Bezier(a); }
237         subdivideArr(to, &a.c_[0], res, NULL, a.order());
238         return Bezier(res, a.order());
239     }
240     subdivideArr(from, &a.c_[0], NULL, res, a.order());
241     if(to == 1) return Bezier(res, a.order());
242     Coord res2[a.order()+1];
243     subdivideArr((to - from)/(1 - from), res, res2, NULL, a.order());
244     return Bezier(res2, a.order());
247 // XXX Todo: how to handle differing orders
248 inline std::vector<Point> bezier_points(const D2<Bezier > & a) {
249     std::vector<Point> result;
250     for(unsigned i = 0; i <= a[0].order(); i++) {
251         Point p;
252         for(unsigned d = 0; d < 2; d++) p[d] = a[d][i];
253         result.push_back(p);
254     }
255     return result;
258 inline Bezier derivative(const Bezier & a) {
259     if(a.order() == 1) return Bezier(0.0);
260     Bezier der(Bezier::Order(a.order()-1));
261     
262     for(unsigned i = 0; i < a.order(); i++) {
263         der.c_[i] = a.order()*(a.c_[i+1] - a.c_[i]);
264     }
265     return der;
268 inline Bezier integral(const Bezier & a) {
269     Bezier inte(Bezier::Order(a.order()+1));
270     
271     inte[0] = 0;
272     for(unsigned i = 0; i < inte.order(); i++) {
273         inte[i+1] = inte[i] + a[i]/(inte.order());
274     }
275     return inte;
278 inline Interval bounds_fast(Bezier const & b) {
279     return Interval::fromArray(&b.c_[0], b.size());
282 //TODO: better bounds exact
283 inline Interval bounds_exact(Bezier const & b) {
284     return bounds_exact(b.toSBasis());
287 inline Interval bounds_local(Bezier const & b, Interval i) {
288     return bounds_fast(portion(b, i.min(), i.max()));
289     //return bounds_local(b.toSBasis(), i);
292 inline std::ostream &operator<< (std::ostream &out_file, const Bezier & b) {
293     for(unsigned i = 0; i < b.size(); i++) {
294         out_file << b[i] << ", ";
295     }
296     return out_file;
300 #endif //SEEN_BEZIER_H
301 /*
302   Local Variables:
303   mode:c++
304   c-file-style:"stroustrup"
305   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
306   indent-tabs-mode:nil
307   fill-column:99
308   End:
309 */
310 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :