Code

update to 2geom rev. 1168
[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 valueAndDerivative(double t, double &der) const {
90         double s = t*(1-t);
91         double p0 = 0, p1 = 0;
92         double sk = 1;
93 //TODO: rewrite as horner
94         for(unsigned k = 0; k < size(); k++) {
95             p0 += sk*(*this)[k][0];
96             p1 += sk*(*this)[k][1];
97             sk *= s;
98         }
99         // p0 and p1 at this point form a linear approximation at t
100         der = p1 - p0;
101         return (1-t)*p0 + t*p1;
102     }
103     double operator()(double t) const {
104         return valueAt(t);
105     }
107     std::vector<double> valueAndDerivatives(double /*t*/, unsigned /*n*/) const {
108         //TODO
109         throw NotImplemented();
110     }
112     SBasis toSBasis() const { return SBasis(*this); }
114     double tailError(unsigned tail) const;
116 // compute f(g)
117     SBasis operator()(SBasis const & g) const;
119     Linear operator[](unsigned i) const {
120         assert(i < size());
121         return std::vector<Linear>::operator[](i);
122     }
124 //MUTATOR PRISON
125     Linear& operator[](unsigned i) { return this->at(i); }
127     //remove extra zeros
128     void normalize() {
129         while(!empty() && 0 == back()[0] && 0 == back()[1])
130             pop_back();
131     }
132     void truncate(unsigned k) { if(k < size()) resize(k); }
133 };
135 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
136 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
138 //implemented in sbasis-roots.cpp
139 Interval bounds_exact(SBasis const &a);
140 Interval bounds_fast(SBasis const &a, int order = 0);
141 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
143 inline SBasis reverse(SBasis const &a) {
144     SBasis result;
145     result.reserve(a.size());
146     for(unsigned k = 0; k < a.size(); k++)
147        result.push_back(reverse(a[k]));
148     return result;
151 //IMPL: ScalableConcept
152 inline SBasis operator-(const SBasis& p) {
153     if(p.isZero()) return SBasis();
154     SBasis result;
155     result.reserve(p.size());
156         
157     for(unsigned i = 0; i < p.size(); i++) {
158         result.push_back(-p[i]);
159     }
160     return result;
162 SBasis operator*(SBasis const &a, double k);
163 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
164 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
165 SBasis& operator*=(SBasis& a, double b);
166 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
168 //IMPL: AddableConcept
169 SBasis operator+(const SBasis& a, const SBasis& b);
170 SBasis operator-(const SBasis& a, const SBasis& b);
171 SBasis& operator+=(SBasis& a, const SBasis& b);
172 SBasis& operator-=(SBasis& a, const SBasis& b);
174 //TODO: remove?
175 inline SBasis operator+(const SBasis & a, Linear const & b) {
176     if(b.isZero()) return a;
177     if(a.isZero()) return b;
178     SBasis result(a);
179     result[0] += b;
180     return result;
182 inline SBasis operator-(const SBasis & a, Linear const & b) {
183     if(b.isZero()) return a;
184     SBasis result(a);
185     result[0] -= b;
186     return result;
188 inline SBasis& operator+=(SBasis& a, const Linear& b) {
189     if(a.isZero())
190         a.push_back(b);
191     else
192         a[0] += b;
193     return a;
195 inline SBasis& operator-=(SBasis& a, const Linear& b) {
196     if(a.isZero())
197         a.push_back(-b);
198     else
199         a[0] -= b;
200     return a;
203 //IMPL: OffsetableConcept
204 inline SBasis operator+(const SBasis & a, double b) {
205     if(a.isZero()) return Linear(b, b);
206     SBasis result(a);
207     result[0] += b;
208     return result;
210 inline SBasis operator-(const SBasis & a, double b) {
211     if(a.isZero()) return Linear(-b, -b);
212     SBasis result(a);
213     result[0] -= b;
214     return result;
216 inline SBasis& operator+=(SBasis& a, double b) {
217     if(a.isZero())
218         a.push_back(Linear(b,b));
219     else
220         a[0] += b;
221     return a;
223 inline SBasis& operator-=(SBasis& a, double b) {
224     if(a.isZero())
225         a.push_back(Linear(-b,-b));
226     else
227         a[0] -= b;
228     return a;
231 SBasis shift(SBasis const &a, int sh);
232 SBasis shift(Linear const &a, int sh);
234 inline SBasis truncate(SBasis const &a, unsigned terms) {
235     SBasis c;
236     c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
237     return c;
240 SBasis multiply(SBasis const &a, SBasis const &b);
242 SBasis integral(SBasis const &c);
243 SBasis derivative(SBasis const &a);
245 SBasis sqrt(SBasis const &a, int k);
247 // return a kth order approx to 1/a)
248 SBasis reciprocal(Linear const &a, int k);
249 SBasis divide(SBasis const &a, SBasis const &b, int k);
251 inline SBasis operator*(SBasis const & a, SBasis const & b) {
252     return multiply(a, b);
255 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
256     a = multiply(a, b);
257     return a;
260 //valuation: degree of the first non zero coefficient.
261 inline unsigned 
262 valuation(SBasis const &a, double tol=0){
263     unsigned val=0;
264     while( val<a.size() &&
265            fabs(a[val][0])<tol &&
266            fabs(a[val][1])<tol ) 
267         val++;
268     return val;
271 // a(b(t))
272 SBasis compose(SBasis const &a, SBasis const &b);
273 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
274 SBasis inverse(SBasis a, int k);
275 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
276 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
277 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
279 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
281 // compute f(g)
282 inline SBasis
283 SBasis::operator()(SBasis const & g) const {
284     return compose(*this, g);
286  
287 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
288     out_file << "{" << bo[0] << ", " << bo[1] << "}";
289     return out_file;
292 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
293     for(unsigned i = 0; i < p.size(); i++) {
294         out_file << p[i] << "s^" << i << " + ";
295     }
296     return out_file;
299 SBasis sin(Linear bo, int k);
300 SBasis cos(Linear bo, int k);
302 std::vector<double> roots(SBasis const & s);
303 std::vector<std::vector<double> > multi_roots(SBasis const &f,
304                                  std::vector<double> const &levels,
305                                  double htol=1e-7,
306                                  double vtol=1e-7,
307                                  double a=0,
308                                  double b=1);
309     
312 /*
313   Local Variables:
314   mode:c++
315   c-file-style:"stroustrup"
316   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
317   indent-tabs-mode:nil
318   fill-column:99
319   End:
320 */
321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
322 #endif