Code

a27d4bfcee4512c1a58b3a4a4cffdd43d021dde7
[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"
43 namespace Geom {
45 /*** An empty SBasis is identically 0. */
46 class SBasis : public std::vector<Linear>{
47 public:
48     SBasis() {}
49     explicit SBasis(double a) {
50         push_back(Linear(a,a));
51     }
52     SBasis(SBasis const & a) :
53         std::vector<Linear>(a)
54     {}
55     SBasis(Linear const & bo) {
56         push_back(bo);
57     }
59     //IMPL: FragmentConcept
60     typedef double output_type;
61     inline bool isZero() const {
62         if(empty()) return true;
63         for(unsigned i = 0; i < size(); i++) {
64             if(!(*this)[i].isZero()) return false;
65         }
66         return true;
67     }
68     bool isFinite() const;
69     inline double at0() const { 
70         if(empty()) return 0; else return (*this)[0][0];
71     }
72     inline double at1() const{
73         if(empty()) return 0; else return (*this)[0][1];
74     }
76     double valueAt(double t) const {
77         double s = t*(1-t);
78         double p0 = 0, p1 = 0;
79         double sk = 1;
80 //TODO: rewrite as horner
81         for(unsigned k = 0; k < size(); k++) {
82             p0 += sk*(*this)[k][0];
83             p1 += sk*(*this)[k][1];
84             sk *= s;
85         }
86         return (1-t)*p0 + t*p1;
87     }
88     double operator()(double t) const {
89         return valueAt(t);
90     }
91     SBasis toSBasis() const { return SBasis(*this); }
93     double tailError(unsigned tail) const;
95 // compute f(g)
96     SBasis operator()(SBasis const & g) const;
98     Linear operator[](unsigned i) const {
99         assert(i < size());
100         return std::vector<Linear>::operator[](i);
101     }
103 //MUTATOR PRISON
104     Linear& operator[](unsigned i) { return this->at(i); }
106     //remove extra zeros
107     void normalize() {
108         while(!empty() && 0 == back()[0] && 0 == back()[1])
109             pop_back();
110     }
111     void truncate(unsigned k) { if(k < size()) resize(k); }
112 };
114 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
115 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
117 //implemented in sbasis-roots.cpp
118 Interval bounds_exact(SBasis const &a);
119 Interval bounds_fast(SBasis const &a, int order = 0);
120 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
122 inline SBasis reverse(SBasis const &a) {
123     SBasis result;
124     result.reserve(a.size());
125     for(unsigned k = 0; k < a.size(); k++)
126        result.push_back(reverse(a[k]));
127     return result;
130 //IMPL: ScalableConcept
131 inline SBasis operator-(const SBasis& p) {
132     if(p.isZero()) return SBasis();
133     SBasis result;
134     result.reserve(p.size());
135         
136     for(unsigned i = 0; i < p.size(); i++) {
137         result.push_back(-p[i]);
138     }
139     return result;
141 SBasis operator*(SBasis const &a, double k);
142 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
143 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
144 SBasis& operator*=(SBasis& a, double b);
145 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
147 //IMPL: AddableConcept
148 SBasis operator+(const SBasis& a, const SBasis& b);
149 SBasis operator-(const SBasis& a, const SBasis& b);
150 SBasis& operator+=(SBasis& a, const SBasis& b);
151 SBasis& operator-=(SBasis& a, const SBasis& b);
153 //TODO: remove?
154 inline SBasis operator+(const SBasis & a, Linear const & b) {
155     if(b.isZero()) return a;
156     if(a.isZero()) return b;
157     SBasis result(a);
158     result[0] += b;
159     return result;
161 inline SBasis operator-(const SBasis & a, Linear const & b) {
162     if(b.isZero()) return a;
163     SBasis result(a);
164     result[0] -= b;
165     return result;
167 inline SBasis& operator+=(SBasis& a, const Linear& b) {
168     if(a.isZero())
169         a.push_back(b);
170     else
171         a[0] += b;
172     return a;
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;
182 //IMPL: OffsetableConcept
183 inline SBasis operator+(const SBasis & a, double b) {
184     if(a.isZero()) return Linear(b, b);
185     SBasis result(a);
186     result[0] += b;
187     return result;
189 inline SBasis operator-(const SBasis & a, double b) {
190     if(a.isZero()) return Linear(-b, -b);
191     SBasis result(a);
192     result[0] -= b;
193     return result;
195 inline SBasis& operator+=(SBasis& a, double b) {
196     if(a.isZero())
197         a.push_back(Linear(b,b));
198     else
199         a[0] += b;
200     return a;
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;
210 SBasis shift(SBasis const &a, int sh);
211 SBasis shift(Linear const &a, int sh);
213 inline SBasis truncate(SBasis const &a, unsigned terms) {
214     SBasis c;
215     c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
216     return c;
219 SBasis multiply(SBasis const &a, SBasis const &b);
221 SBasis integral(SBasis const &c);
222 SBasis derivative(SBasis const &a);
224 SBasis sqrt(SBasis const &a, int k);
226 // return a kth order approx to 1/a)
227 SBasis reciprocal(Linear const &a, int k);
228 SBasis divide(SBasis const &a, SBasis const &b, int k);
230 inline SBasis operator*(SBasis const & a, SBasis const & b) {
231     return multiply(a, b);
234 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
235     a = multiply(a, b);
236     return a;
239 //valuation: degree of the first non zero coefficient.
240 inline unsigned 
241 valuation(SBasis const &a, double tol=0){
242     unsigned val=0;
243     while( val<a.size() &&
244            fabs(a[val][0])<tol &&
245            fabs(a[val][1])<tol ) 
246         val++;
247     return val;
250 // a(b(t))
251 SBasis compose(SBasis const &a, SBasis const &b);
252 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
253 SBasis inverse(SBasis a, int k);
254 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
255 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
256 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
258 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
260 // compute f(g)
261 inline SBasis
262 SBasis::operator()(SBasis const & g) const {
263     return compose(*this, g);
265  
266 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
267     out_file << "{" << bo[0] << ", " << bo[1] << "}";
268     return out_file;
271 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
272     for(unsigned i = 0; i < p.size(); i++) {
273         out_file << p[i] << "s^" << i << " + ";
274     }
275     return out_file;
278 SBasis sin(Linear bo, int k);
279 SBasis cos(Linear bo, int k);
281 std::vector<double> roots(SBasis const & s);
282 std::vector<std::vector<double> > multi_roots(SBasis const &f,
283                                  std::vector<double> const &levels,
284                                  double htol=1e-7,
285                                  double vtol=1e-7,
286                                  double a=0,
287                                  double b=1);
288     
291 /*
292   Local Variables:
293   mode:c++
294   c-file-style:"stroustrup"
295   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
296   indent-tabs-mode:nil
297   fill-column:99
298   End:
299 */
300 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
301 #endif