Code

Update to 2Geom rev. 1113
[inkscape.git] / src / 2geom / sbasis.h
1 /*
2  *  sbasis.h - S-power basis function class
3  *
4  *  Authors:
5  *   Nathan Hurst <njh@mail.csse.monash.edu.au>
6  *   Michael Sloan <mgsloan@gmail.com>
7  *
8  * Copyright (C) 2006-2007 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  */
34 #ifndef SEEN_SBASIS_H
35 #define SEEN_SBASIS_H
36 #include <vector>
37 #include <cassert>
38 #include <iostream>
40 #include "linear.h"
41 #include "interval.h"
42 #include "utils.h"
44 namespace Geom {
46 /*** An empty SBasis is identically 0. */
47 class SBasis : public std::vector<Linear>{
48 public:
49     SBasis() {}
50     explicit SBasis(double a) {
51         push_back(Linear(a,a));
52     }
53     SBasis(SBasis const & a) :
54         std::vector<Linear>(a)
55     {}
56     SBasis(Linear const & bo) {
57         push_back(bo);
58     }
60     //IMPL: FragmentConcept
61     typedef double output_type;
62     inline bool isZero() const {
63         if(empty()) return true;
64         for(unsigned i = 0; i < size(); i++) {
65             if(!(*this)[i].isZero()) return false;
66         }
67         return true;
68     }
69     bool isFinite() const;
70     inline double at0() const { 
71         if(empty()) return 0; else return (*this)[0][0];
72     }
73     inline double at1() const{
74         if(empty()) return 0; else return (*this)[0][1];
75     }
77     double valueAt(double t) const {
78         double s = t*(1-t);
79         double p0 = 0, p1 = 0;
80         double sk = 1;
81 //TODO: rewrite as horner
82         for(unsigned k = 0; k < size(); k++) {
83             p0 += sk*(*this)[k][0];
84             p1 += sk*(*this)[k][1];
85             sk *= s;
86         }
87         return (1-t)*p0 + t*p1;
88     }
89     double operator()(double t) const {
90         return valueAt(t);
91     }
93     std::vector<double> valueAndDerivatives(double t, unsigned n) const {
94         //TODO
95         throw NotImplemented();
96     }
98     SBasis toSBasis() const { return SBasis(*this); }
100     double tailError(unsigned tail) const;
102 // compute f(g)
103     SBasis operator()(SBasis const & g) const;
105     Linear operator[](unsigned i) const {
106         assert(i < size());
107         return std::vector<Linear>::operator[](i);
108     }
110 //MUTATOR PRISON
111     Linear& operator[](unsigned i) { return this->at(i); }
113     //remove extra zeros
114     void normalize() {
115         while(!empty() && 0 == back()[0] && 0 == back()[1])
116             pop_back();
117     }
118     void truncate(unsigned k) { if(k < size()) resize(k); }
119 };
121 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
122 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
124 //implemented in sbasis-roots.cpp
125 Interval bounds_exact(SBasis const &a);
126 Interval bounds_fast(SBasis const &a, int order = 0);
127 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
129 inline SBasis reverse(SBasis const &a) {
130     SBasis result;
131     result.reserve(a.size());
132     for(unsigned k = 0; k < a.size(); k++)
133        result.push_back(reverse(a[k]));
134     return result;
137 //IMPL: ScalableConcept
138 inline SBasis operator-(const SBasis& p) {
139     if(p.isZero()) return SBasis();
140     SBasis result;
141     result.reserve(p.size());
142         
143     for(unsigned i = 0; i < p.size(); i++) {
144         result.push_back(-p[i]);
145     }
146     return result;
148 SBasis operator*(SBasis const &a, double k);
149 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
150 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
151 SBasis& operator*=(SBasis& a, double b);
152 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
154 //IMPL: AddableConcept
155 SBasis operator+(const SBasis& a, const SBasis& b);
156 SBasis operator-(const SBasis& a, const SBasis& b);
157 SBasis& operator+=(SBasis& a, const SBasis& b);
158 SBasis& operator-=(SBasis& a, const SBasis& b);
160 //TODO: remove?
161 inline SBasis operator+(const SBasis & a, Linear const & b) {
162     if(b.isZero()) return a;
163     if(a.isZero()) return b;
164     SBasis result(a);
165     result[0] += b;
166     return result;
168 inline SBasis operator-(const SBasis & a, Linear const & b) {
169     if(b.isZero()) return a;
170     SBasis result(a);
171     result[0] -= b;
172     return result;
174 inline SBasis& operator+=(SBasis& a, const Linear& b) {
175     if(a.isZero())
176         a.push_back(b);
177     else
178         a[0] += b;
179     return a;
181 inline SBasis& operator-=(SBasis& a, const Linear& b) {
182     if(a.isZero())
183         a.push_back(-b);
184     else
185         a[0] -= b;
186     return a;
189 //IMPL: OffsetableConcept
190 inline SBasis operator+(const SBasis & a, double b) {
191     if(a.isZero()) return Linear(b, b);
192     SBasis result(a);
193     result[0] += b;
194     return result;
196 inline SBasis operator-(const SBasis & a, double b) {
197     if(a.isZero()) return Linear(-b, -b);
198     SBasis result(a);
199     result[0] -= b;
200     return result;
202 inline SBasis& operator+=(SBasis& a, double b) {
203     if(a.isZero())
204         a.push_back(Linear(b,b));
205     else
206         a[0] += b;
207     return a;
209 inline SBasis& operator-=(SBasis& a, double b) {
210     if(a.isZero())
211         a.push_back(Linear(-b,-b));
212     else
213         a[0] -= b;
214     return a;
217 SBasis shift(SBasis const &a, int sh);
218 SBasis shift(Linear const &a, int sh);
220 inline SBasis truncate(SBasis const &a, unsigned terms) {
221     SBasis c;
222     c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
223     return c;
226 SBasis multiply(SBasis const &a, SBasis const &b);
228 SBasis integral(SBasis const &c);
229 SBasis derivative(SBasis const &a);
231 SBasis sqrt(SBasis const &a, int k);
233 // return a kth order approx to 1/a)
234 SBasis reciprocal(Linear const &a, int k);
235 SBasis divide(SBasis const &a, SBasis const &b, int k);
237 inline SBasis operator*(SBasis const & a, SBasis const & b) {
238     return multiply(a, b);
241 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
242     a = multiply(a, b);
243     return a;
246 //valuation: degree of the first non zero coefficient.
247 inline unsigned 
248 valuation(SBasis const &a, double tol=0){
249     unsigned val=0;
250     while( val<a.size() &&
251            fabs(a[val][0])<tol &&
252            fabs(a[val][1])<tol ) 
253         val++;
254     return val;
257 // a(b(t))
258 SBasis compose(SBasis const &a, SBasis const &b);
259 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
260 SBasis inverse(SBasis a, int k);
261 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
262 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
263 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
265 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
267 // compute f(g)
268 inline SBasis
269 SBasis::operator()(SBasis const & g) const {
270     return compose(*this, g);
272  
273 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
274     out_file << "{" << bo[0] << ", " << bo[1] << "}";
275     return out_file;
278 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
279     for(unsigned i = 0; i < p.size(); i++) {
280         out_file << p[i] << "s^" << i << " + ";
281     }
282     return out_file;
285 SBasis sin(Linear bo, int k);
286 SBasis cos(Linear bo, int k);
288 std::vector<double> roots(SBasis const & s);
289 std::vector<std::vector<double> > multi_roots(SBasis const &f,
290                                  std::vector<double> const &levels,
291                                  double htol=1e-7,
292                                  double vtol=1e-7,
293                                  double a=0,
294                                  double b=1);
295     
298 /*
299   Local Variables:
300   mode:c++
301   c-file-style:"stroustrup"
302   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
303   indent-tabs-mode:nil
304   fill-column:99
305   End:
306 */
307 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
308 #endif