Code

Merge from trunk
[inkscape.git] / src / 2geom / piecewise.h
1 /**
2  * \file
3  * \brief Piecewise function class
4  *
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, output 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_GEOM_PW_SB_H
33 #define SEEN_GEOM_PW_SB_H
35 #include <2geom/sbasis.h>
36 #include <vector>
37 #include <map>
39 #include <2geom/concepts.h>
40 #include <2geom/isnan.h>
41 #include <boost/concept_check.hpp>
43 namespace Geom {
44 /**
45  * %Piecewise function class.
46  * The Piecewise class manages a sequence of elements of a type as segments and
47  * the ’cuts’ between them. These cuts are time values which separate the pieces.
48  * This function representation allows for more interesting functions, as it provides
49  * a viable output for operations such as inversion, which may require multiple
50  * SBasis to properly invert the original.
51  * As for technical details, while the actual SBasis segments begin on the first
52  * cut and end on the last, the function is defined throughout all inputs by ex-
53  * tending the first and last segments. The exact switching between segments is
54  * arbitrarily such that beginnings (t=0) have preference over endings (t=1). This
55  * only matters if it is discontinuous at the location.
56  * \f[
57  *      f(t) \rightarrow \left\{ 
58  *      \begin{array}{cc}
59  *      s_1,& t <= c_2 \\
60  *      s_2,& c_2 <= t <= c_3\\
61  *      \ldots
62  *      s_n,& c_n <= t
63  *      \end{array}\right.
64  * \f]
65  */
66 template <typename T>
67 class Piecewise {
68   BOOST_CLASS_REQUIRE(T, Geom, FragmentConcept);
70   public:
71     std::vector<double> cuts;
72     std::vector<T> segs;
73     //segs[i] stretches from cuts[i] to cuts[i+1].
75     Piecewise() {}
77     explicit Piecewise(const T &s) {
78         push_cut(0.);
79         push_seg(s);
80         push_cut(1.);
81     }
83     typedef typename T::output_type output_type;
85     explicit Piecewise(const output_type & v) {
86         push_cut(0.);
87         push_seg(T(v));
88         push_cut(1.);
89     }
91     inline T operator[](unsigned i) const { return segs[i]; }
92     inline T &operator[](unsigned i) { return segs[i]; }
93     inline output_type operator()(double t) const { return valueAt(t); }
94     inline output_type valueAt(double t) const {
95         unsigned n = segN(t);
96         return segs[n](segT(t, n));
97     }
98     inline output_type firstValue() const {
99         return valueAt(cuts.front());
100     }
101     inline output_type lastValue() const {
102         return valueAt(cuts.back());
103     }
104     std::vector<output_type> valueAndDerivatives(double t, unsigned n_derivs) const {
105         unsigned n = segN(t);
106         std::vector<output_type> ret, val = segs[n].valueAndDerivatives(segT(t, n), n_derivs);
107         double mult = 1;
108         for(unsigned i = 0; i < val.size(); i++) {
109             ret.push_back(val[i] * mult);
110             mult /= cuts[n+1] - cuts[n];
111         }
112         return ret;
113     }
114     //TODO: maybe it is not a good idea to have this?
115     Piecewise<T> operator()(SBasis f);
116     Piecewise<T> operator()(Piecewise<SBasis>f);
118     inline unsigned size() const { return segs.size(); }
119     inline bool empty() const { return segs.empty(); }
120     inline void clear() {
121         segs.clear();
122         cuts.clear();
123     }
125     /**Convenience/implementation hiding function to add segment/cut pairs.
126      * Asserts that basic size and order invariants are correct
127      */
128     inline void push(const T &s, double to) {
129         assert(cuts.size() - segs.size() == 1);
130         push_seg(s);
131         push_cut(to);
132     }
133     //Convenience/implementation hiding function to add cuts.
134     inline void push_cut(double c) {
135         ASSERT_INVARIANTS(cuts.empty() || c > cuts.back());
136         cuts.push_back(c);
137     }
138     //Convenience/implementation hiding function to add segments.
139     inline void push_seg(const T &s) { segs.push_back(s); }
141     /**Returns the segment index which corresponds to a 'global' piecewise time.
142      * Also takes optional low/high parameters to expedite the search for the segment.
143      */
144     inline unsigned segN(double t, int low = 0, int high = -1) const {
145         high = (high == -1) ? size() : high;
146         if(t < cuts[0]) return 0;
147         if(t >= cuts[size()]) return size() - 1;
148         while(low < high) {
149             int mid = (high + low) / 2; //Lets not plan on having huge (> INT_MAX / 2) cut sequences
150             double mv = cuts[mid];
151             if(mv < t) {
152                 if(t < cuts[mid + 1]) return mid; else low = mid + 1;
153             } else if(t < mv) {
154                 if(cuts[mid - 1] < t) return mid - 1; else high = mid - 1;
155             } else {
156                 return mid;
157             }
158         }
159         return low;
160     }
162     /**Returns the time within a segment, given the 'global' piecewise time.
163      * Also takes an optional index parameter which may be used for efficiency or to find the time on a
164      * segment outside its range.  If it is left to its default, -1, it will call segN to find the index.
165      */
166     inline double segT(double t, int i = -1) const {
167         if(i == -1) i = segN(t);
168         assert(i >= 0);
169         return (t - cuts[i]) / (cuts[i+1] - cuts[i]);
170     }
172     inline double mapToDomain(double t, unsigned i) const {
173         return (1-t)*cuts[i] + t*cuts[i+1]; //same as: t * (cuts[i+1] - cuts[i]) + cuts[i]
174     }
176     //Offsets the piecewise domain
177     inline void offsetDomain(double o) {
178         assert(IS_FINITE(o));
179         if(o != 0)
180             for(unsigned i = 0; i <= size(); i++)
181                 cuts[i] += o;
182     }
184     //Scales the domain of the function by a value. 0 will result in an empty Piecewise.
185     inline void scaleDomain(double s) {
186         assert(s > 0);
187         if(s == 0) {
188             cuts.clear(); segs.clear();
189             return;
190         }
191         for(unsigned i = 0; i <= size(); i++)
192             cuts[i] *= s;
193     }
195     //Retrieves the domain in interval form
196     inline Interval domain() const { return Interval(cuts.front(), cuts.back()); }
198     //Transforms the domain into another interval
199     inline void setDomain(Interval dom) {
200         if(empty()) return;
201         if(dom.isEmpty()) {
202             cuts.clear(); segs.clear();
203             return;
204         }
205         double cf = cuts.front();
206         double o = dom.min() - cf, s = dom.extent() / (cuts.back() - cf);
207         for(unsigned i = 0; i <= size(); i++)
208             cuts[i] = (cuts[i] - cf) * s + o;
209     }
211     //Concatenates this Piecewise function with another, offseting time of the other to match the end.
212     inline void concat(const Piecewise<T> &other) {
213         if(other.empty()) return;
215         if(empty()) {
216             cuts = other.cuts; segs = other.segs;
217             return;
218         }
220         segs.insert(segs.end(), other.segs.begin(), other.segs.end());
221         double t = cuts.back() - other.cuts.front();
222         for(unsigned i = 0; i < other.size(); i++)
223             push_cut(other.cuts[i + 1] + t);
224     }
226     //Like concat, but ensures continuity.
227     inline void continuousConcat(const Piecewise<T> &other) {
228         boost::function_requires<AddableConcept<typename T::output_type> >();
229         if(other.empty()) return;
230         typename T::output_type y = segs.back().at1() - other.segs.front().at0();
232         if(empty()) {
233             for(unsigned i = 0; i < other.size(); i++)
234                 push_seg(other[i] + y);
235             cuts = other.cuts;
236             return;
237         }
239         double t = cuts.back() - other.cuts.front();
240         for(unsigned i = 0; i < other.size(); i++)
241             push(other[i] + y, other.cuts[i + 1] + t);
242     }
244     //returns true if the Piecewise<T> meets some basic invariants.
245     inline bool invariants() const {
246         // segs between cuts
247         if(!(segs.size() + 1 == cuts.size() || (segs.empty() && cuts.empty())))
248             return false;
249         // cuts in order
250         for(unsigned i = 0; i < segs.size(); i++)
251             if(cuts[i] >= cuts[i+1])
252                 return false;
253         return true;
254     }
256 };
258 template<typename T>
259 inline typename FragmentConcept<T>::BoundsType bounds_fast(const Piecewise<T> &f) {
260     boost::function_requires<FragmentConcept<T> >();
262     if(f.empty()) return typename FragmentConcept<T>::BoundsType();
263     typename FragmentConcept<T>::BoundsType ret(bounds_fast(f[0]));
264     for(unsigned i = 1; i < f.size(); i++)
265         ret.unionWith(bounds_fast(f[i]));
266     return ret;
269 template<typename T>
270 inline typename FragmentConcept<T>::BoundsType bounds_exact(const Piecewise<T> &f) {
271     boost::function_requires<FragmentConcept<T> >();
273     if(f.empty()) return typename FragmentConcept<T>::BoundsType();
274     typename FragmentConcept<T>::BoundsType ret(bounds_exact(f[0]));
275     for(unsigned i = 1; i < f.size(); i++)
276         ret.unionWith(bounds_exact(f[i]));
277     return ret;
280 template<typename T>
281 inline typename FragmentConcept<T>::BoundsType bounds_local(const Piecewise<T> &f, const Interval &m) {
282     boost::function_requires<FragmentConcept<T> >();
284     if(f.empty()) return typename FragmentConcept<T>::BoundsType();
285     if(m.isEmpty()) return typename FragmentConcept<T>::BoundsType(f(m.min()));
287     unsigned fi = f.segN(m.min()), ti = f.segN(m.max());
288     double ft = f.segT(m.min(), fi), tt = f.segT(m.max(), ti);
290     if(fi == ti) return bounds_local(f[fi], Interval(ft, tt));
292     typename FragmentConcept<T>::BoundsType ret(bounds_local(f[fi], Interval(ft, 1.)));
293     for(unsigned i = fi + 1; i < ti; i++)
294         ret.unionWith(bounds_exact(f[i]));
295     if(tt != 0.) ret.unionWith(bounds_local(f[ti], Interval(0., tt)));
297     return ret;
300 //returns a portion of a piece of a Piecewise<T>, given the piece's index and a to/from time.
301 template<typename T>
302 T elem_portion(const Piecewise<T> &a, unsigned i, double from, double to) {
303     assert(i < a.size());
304     double rwidth = 1 / (a.cuts[i+1] - a.cuts[i]);
305     return portion( a[i], (from - a.cuts[i]) * rwidth, (to - a.cuts[i]) * rwidth );
308 /**Piecewise<T> partition(const Piecewise<T> &pw, std::vector<double> const &c);
309  * Further subdivides the Piecewise<T> such that there is a cut at every value in c.
310  * Precondition: c sorted lower to higher.
311  *
312  * //Given Piecewise<T> a and b:
313  * Piecewise<T> ac = a.partition(b.cuts);
314  * Piecewise<T> bc = b.partition(a.cuts);
315  * //ac.cuts should be equivalent to bc.cuts
316  */
317 template<typename T>
318 Piecewise<T> partition(const Piecewise<T> &pw, std::vector<double> const &c) {
319     assert(pw.invariants());
320     if(c.empty()) return Piecewise<T>(pw);
322     Piecewise<T> ret = Piecewise<T>();
323     ret.cuts.reserve(c.size() + pw.cuts.size());
324     ret.segs.reserve(c.size() + pw.cuts.size() - 1);
326     if(pw.empty()) {
327         ret.cuts = c;
328         for(unsigned i = 0; i < c.size() - 1; i++)
329             ret.push_seg(T());
330         return ret;
331     }
333     unsigned si = 0, ci = 0;     //Segment index, Cut index
335     //if the cuts have something earlier than the Piecewise<T>, add portions of the first segment
336     while(c[ci] < pw.cuts.front() && ci < c.size()) {
337         bool isLast = (ci == c.size()-1 || c[ci + 1] >= pw.cuts.front());
338         ret.push_cut(c[ci]);
339         ret.push_seg( elem_portion(pw, 0, c[ci], isLast ? pw.cuts.front() : c[ci + 1]) );
340         ci++;
341     }
343     ret.push_cut(pw.cuts[0]);
344     double prev = pw.cuts[0];    //previous cut
345     //Loop which handles cuts within the Piecewise<T> domain
346     //Should have the cuts = segs + 1 invariant
347     while(si < pw.size() && ci <= c.size()) {
348         if(ci == c.size() && prev <= pw.cuts[si]) { //cuts exhausted, straight copy the rest
349             ret.segs.insert(ret.segs.end(), pw.segs.begin() + si, pw.segs.end());
350             ret.cuts.insert(ret.cuts.end(), pw.cuts.begin() + si + 1, pw.cuts.end());
351             return ret;
352         }else if(ci == c.size() || c[ci] >= pw.cuts[si + 1]) {  //no more cuts within this segment, finalize
353             if(prev > pw.cuts[si]) {      //segment already has cuts, so portion is required
354                 ret.push_seg(portion(pw[si], pw.segT(prev, si), 1.0));
355             } else {                     //plain copy is fine
356                 ret.push_seg(pw[si]);
357             }
358             ret.push_cut(pw.cuts[si + 1]);
359             prev = pw.cuts[si + 1];
360             si++;
361         } else if(c[ci] == pw.cuts[si]){                  //coincident
362             //Already finalized the seg with the code immediately above
363             ci++;
364         } else {                                         //plain old subdivision
365             ret.push(elem_portion(pw, si, prev, c[ci]), c[ci]);
366             prev = c[ci];
367             ci++;
368         }
369     }
371     //input cuts extend further than this Piecewise<T>, extend the last segment.
372     while(ci < c.size()) {
373         if(c[ci] > prev) {
374             ret.push(elem_portion(pw, pw.size() - 1, prev, c[ci]), c[ci]);
375             prev = c[ci];
376         }
377         ci++;
378     }
379     return ret;
382 /**Piecewise<T> portion(const Piecewise<T> &pw, double from, double to);
383  * Returns a Piecewise<T> with a defined domain of [min(from, to), max(from, to)].
384  */
385 template<typename T>
386 Piecewise<T> portion(const Piecewise<T> &pw, double from, double to) {
387     if(pw.empty() || from == to) return Piecewise<T>();
389     Piecewise<T> ret;
391     double temp = from;
392     from = std::min(from, to);
393     to = std::max(temp, to);
395     unsigned i = pw.segN(from);
396     ret.push_cut(from);
397     if(i == pw.size() - 1 || to <= pw.cuts[i + 1]) {    //to/from inhabit the same segment
398         ret.push(elem_portion(pw, i, from, to), to);
399         return ret;
400     }
401     ret.push_seg(portion( pw[i], pw.segT(from, i), 1.0 ));
402     i++;
403     unsigned fi = pw.segN(to, i);
404     if (to == pw.cuts[fi]) fi-=1;
406     ret.segs.insert(ret.segs.end(), pw.segs.begin() + i, pw.segs.begin() + fi);  //copy segs
407     ret.cuts.insert(ret.cuts.end(), pw.cuts.begin() + i, pw.cuts.begin() + fi + 1);  //and their cuts
409     ret.push_seg( portion(pw[fi], 0.0, pw.segT(to, fi)));
410     if(to != ret.cuts.back()) ret.push_cut(to);
411     ret.invariants();
412     return ret;
415 template<typename T>
416 Piecewise<T> remove_short_cuts(Piecewise<T> const &f, double tol) {
417     if(f.empty()) return f;
418     Piecewise<T> ret;
419     ret.push_cut(f.cuts[0]);
420     for(unsigned i=0; i<f.size(); i++){
421         if (f.cuts[i+1]-f.cuts[i] >= tol || i==f.size()-1) {
422             ret.push(f[i], f.cuts[i+1]);
423         }
424     }
425     return ret;
428 template<typename T>
429 Piecewise<T> remove_short_cuts_extending(Piecewise<T> const &f, double tol) {
430     if(f.empty()) return f;
431     Piecewise<T> ret;
432     ret.push_cut(f.cuts[0]);
433     double last = f.cuts[0]; // last cut included
434     for(unsigned i=0; i<f.size(); i++){
435         if (f.cuts[i+1]-f.cuts[i] >= tol) {
436             ret.push(elem_portion(f, i, last, f.cuts[i+1]), f.cuts[i+1]);
437             last = f.cuts[i+1];
438         }
439     }
440     return ret;
443 template<typename T>
444 std::vector<double> roots(const Piecewise<T> &pw) {
445     std::vector<double> ret;
446     for(unsigned i = 0; i < pw.size(); i++) {
447         std::vector<double> sr = roots(pw[i]);
448         for (unsigned j = 0; j < sr.size(); j++) ret.push_back(sr[j] * (pw.cuts[i + 1] - pw.cuts[i]) + pw.cuts[i]);
450     }
451     return ret;
454 //IMPL: OffsetableConcept
455 template<typename T>
456 Piecewise<T> operator+(Piecewise<T> const &a, typename T::output_type b) {
457     boost::function_requires<OffsetableConcept<T> >();
458 //TODO:empty
459     Piecewise<T> ret = Piecewise<T>();
460     ret.cuts = a.cuts;
461     for(unsigned i = 0; i < a.size();i++)
462         ret.push_seg(a[i] + b);
463     return ret;
465 template<typename T>
466 Piecewise<T> operator-(Piecewise<T> const &a, typename T::output_type b) {
467     boost::function_requires<OffsetableConcept<T> >();
468 //TODO: empty
469     Piecewise<T> ret = Piecewise<T>();
470     ret.cuts = a.cuts;
471     for(unsigned i = 0; i < a.size();i++)
472         ret.push_seg(a[i] - b);
473     return ret;
475 template<typename T>
476 Piecewise<T> operator+=(Piecewise<T>& a, typename T::output_type b) {
477     boost::function_requires<OffsetableConcept<T> >();
479     if(a.empty()) { a.push_cut(0.); a.push(T(b), 1.); return a; }
481     for(unsigned i = 0; i < a.size();i++)
482         a[i] += b;
483     return a;
485 template<typename T>
486 Piecewise<T> operator-=(Piecewise<T>& a, typename T::output_type b) {
487     boost::function_requires<OffsetableConcept<T> >();
489     if(a.empty()) { a.push_cut(0.); a.push(T(b), 1.); return a; }
491     for(unsigned i = 0;i < a.size();i++)
492         a[i] -= b;
493     return a;
496 //IMPL: ScalableConcept
497 template<typename T>
498 Piecewise<T> operator-(Piecewise<T> const &a) {
499     boost::function_requires<ScalableConcept<T> >();
501     Piecewise<T> ret;
502     ret.cuts = a.cuts;
503     for(unsigned i = 0; i < a.size();i++)
504         ret.push_seg(- a[i]);
505     return ret;
507 template<typename T>
508 Piecewise<T> operator*(Piecewise<T> const &a, double b) {
509     boost::function_requires<ScalableConcept<T> >();
511     if(a.empty()) return Piecewise<T>();
513     Piecewise<T> ret;
514     ret.cuts = a.cuts;
515     for(unsigned i = 0; i < a.size();i++)
516         ret.push_seg(a[i] * b);
517     return ret;
519 template<typename T>
520 Piecewise<T> operator/(Piecewise<T> const &a, double b) {
521     boost::function_requires<ScalableConcept<T> >();
523     //FIXME: b == 0?
524     if(a.empty()) return Piecewise<T>();
526     Piecewise<T> ret;
527     ret.cuts = a.cuts;
528     for(unsigned i = 0; i < a.size();i++)
529         ret.push_seg(a[i] / b);
530     return ret;
532 template<typename T>
533 Piecewise<T> operator*=(Piecewise<T>& a, double b) {
534     boost::function_requires<ScalableConcept<T> >();
536     if(a.empty()) return Piecewise<T>();
538     for(unsigned i = 0; i < a.size();i++)
539         a[i] *= b;
540     return a;
542 template<typename T>
543 Piecewise<T> operator/=(Piecewise<T>& a, double b) {
544     boost::function_requires<ScalableConcept<T> >();
546     //FIXME: b == 0?
547     if(a.empty()) return Piecewise<T>();
549     for(unsigned i = 0; i < a.size();i++)
550         a[i] /= b;
551     return a;
554 //IMPL: AddableConcept
555 template<typename T>
556 Piecewise<T> operator+(Piecewise<T> const &a, Piecewise<T> const &b) {
557     boost::function_requires<AddableConcept<T> >();
559     Piecewise<T> pa = partition(a, b.cuts), pb = partition(b, a.cuts);
560     Piecewise<T> ret = Piecewise<T>();
561     assert(pa.size() == pb.size());
562     ret.cuts = pa.cuts;
563     for (unsigned i = 0; i < pa.size(); i++)
564         ret.push_seg(pa[i] + pb[i]);
565     return ret;
567 template<typename T>
568 Piecewise<T> operator-(Piecewise<T> const &a, Piecewise<T> const &b) {
569     boost::function_requires<AddableConcept<T> >();
571     Piecewise<T> pa = partition(a, b.cuts), pb = partition(b, a.cuts);
572     Piecewise<T> ret = Piecewise<T>();
573     assert(pa.size() == pb.size());
574     ret.cuts = pa.cuts;
575     for (unsigned i = 0; i < pa.size(); i++)
576         ret.push_seg(pa[i] - pb[i]);
577     return ret;
579 template<typename T>
580 inline Piecewise<T> operator+=(Piecewise<T> &a, Piecewise<T> const &b) {
581     a = a+b;
582     return a;
584 template<typename T>
585 inline Piecewise<T> operator-=(Piecewise<T> &a, Piecewise<T> const &b) {
586     a = a-b;
587     return a;
590 template<typename T1,typename T2>
591 Piecewise<T2> operator*(Piecewise<T1> const &a, Piecewise<T2> const &b) {
592     //function_requires<MultiplicableConcept<T1> >();
593     //function_requires<MultiplicableConcept<T2> >();
595     Piecewise<T1> pa = partition(a, b.cuts);
596     Piecewise<T2> pb = partition(b, a.cuts);
597     Piecewise<T2> ret = Piecewise<T2>();
598     assert(pa.size() == pb.size());
599     ret.cuts = pa.cuts;
600     for (unsigned i = 0; i < pa.size(); i++)
601         ret.push_seg(pa[i] * pb[i]);
602     return ret;
605 template<typename T>
606 inline Piecewise<T> operator*=(Piecewise<T> &a, Piecewise<T> const &b) {
607     a = a * b;
608     return a;
611 Piecewise<SBasis> divide(Piecewise<SBasis> const &a, Piecewise<SBasis> const &b, unsigned k);
612 //TODO: replace divide(a,b,k) by divide(a,b,tol,k)?
613 //TODO: atm, relative error is ~(tol/a)%. Find a way to make it independant of a.
614 //Nota: the result is 'truncated' where b is smaller than 'zero': ~ a/max(b,zero).
615 Piecewise<SBasis>
616 divide(Piecewise<SBasis> const &a, Piecewise<SBasis> const &b, double tol, unsigned k, double zero=1.e-3);
617 Piecewise<SBasis>
618 divide(SBasis const &a, Piecewise<SBasis> const &b, double tol, unsigned k, double zero=1.e-3);
619 Piecewise<SBasis>
620 divide(Piecewise<SBasis> const &a, SBasis const &b, double tol, unsigned k, double zero=1.e-3);
621 Piecewise<SBasis>
622 divide(SBasis const &a, SBasis const &b, double tol, unsigned k, double zero=1.e-3);
624 //Composition: functions called compose_* are pieces of compose that are factored out in pw.cpp.
625 std::map<double,unsigned> compose_pullback(std::vector<double> const &cuts, SBasis const &g);
626 int compose_findSegIdx(std::map<double,unsigned>::iterator  const &cut,
627                        std::map<double,unsigned>::iterator  const &next,
628                        std::vector<double>  const &levels,
629                        SBasis const &g);
631 //TODO: add concept check
632 template<typename T>
633 Piecewise<T> compose(Piecewise<T> const &f, SBasis const &g){
634     Piecewise<T> result;
635     if (f.empty()) return result;
636     if (g.isZero()) return Piecewise<T>(f(0));
637     if (f.size()==1){
638         double t0 = f.cuts[0], width = f.cuts[1] - t0;
639         return (Piecewise<T>) compose(f.segs[0],compose(Linear(-t0 / width, (1-t0) / width), g));
640     }
642     //first check bounds...
643     Interval bs = bounds_fast(g);
644     if (f.cuts.front() > bs.max()  || bs.min() > f.cuts.back()){
645         int idx = (bs.max() < f.cuts[1]) ? 0 : f.cuts.size()-2;
646         double t0 = f.cuts[idx], width = f.cuts[idx+1] - t0;
647         return (Piecewise<T>) compose(f.segs[idx],compose(Linear(-t0 / width, (1-t0) / width), g));
648     }
650     std::vector<double> levels;//we can forget first and last cuts...
651     levels.insert(levels.begin(),f.cuts.begin()+1,f.cuts.end()-1);
652     //TODO: use a std::vector<pairs<double,unsigned> > instead of a map<double,unsigned>.
653     std::map<double,unsigned> cuts_pb = compose_pullback(levels,g);
655     //-- Compose each piece of g with the relevant seg of f.
656     result.cuts.push_back(0.);
657     std::map<double,unsigned>::iterator cut=cuts_pb.begin();
658     std::map<double,unsigned>::iterator next=cut; next++;
659     while(next!=cuts_pb.end()){
660         //assert(std::abs(int((*cut).second-(*next).second))<1);
661         //TODO: find a way to recover from this error? the root finder missed some root;
662         //  the levels/variations of f might be too close/fast...
663         int idx = compose_findSegIdx(cut,next,levels,g);
664         double t0=(*cut).first;
665         double t1=(*next).first;
667         SBasis sub_g=compose(g, Linear(t0,t1));
668         sub_g=compose(Linear(-f.cuts[idx]/(f.cuts[idx+1]-f.cuts[idx]),
669                              (1-f.cuts[idx])/(f.cuts[idx+1]-f.cuts[idx])),sub_g);
670         result.push(compose(f[idx],sub_g),t1);
671         cut++;
672         next++;
673     }
674     return(result);
677 //TODO: add concept check for following composition functions
678 template<typename T>
679 Piecewise<T> compose(Piecewise<T> const &f, Piecewise<SBasis> const &g){
680   Piecewise<T> result;
681   for(unsigned i = 0; i < g.segs.size(); i++){
682       Piecewise<T> fgi=compose(f, g.segs[i]);
683       fgi.setDomain(Interval(g.cuts[i], g.cuts[i+1]));
684       result.concat(fgi);
685   }
686   return result;
689 /*
690 Piecewise<D2<SBasis> > compose(D2<SBasis2d> const &sb2d, Piecewise<D2<SBasis> > const &pwd2sb){
691   Piecewise<D2<SBasis> > result;
692   result.push_cut(0.);
693   for(unsigned i = 0; i < pwd2sb.size(); i++){
694      result.push(compose_each(sb2d,pwd2sb[i]),i+1);
695   }
696   return result;
697 }*/
699 template <typename T>
700 Piecewise<T> Piecewise<T>::operator()(SBasis f){return compose((*this),f);}
701 template <typename T>
702 Piecewise<T> Piecewise<T>::operator()(Piecewise<SBasis>f){return compose((*this),f);}
704 template<typename T>
705 Piecewise<T> integral(Piecewise<T> const &a) {
706     Piecewise<T> result;
707     result.segs.resize(a.segs.size());
708     result.cuts = a.cuts;
709     typename T::output_type c = a.segs[0].at0();
710     for(unsigned i = 0; i < a.segs.size(); i++){
711         result.segs[i] = integral(a.segs[i])*(a.cuts[i+1]-a.cuts[i]);
712         result.segs[i]+= c-result.segs[i].at0();
713         c = result.segs[i].at1();
714     }
715     return result;
718 template<typename T>
719 Piecewise<T> derivative(Piecewise<T> const &a) {
720     Piecewise<T> result;
721     result.segs.resize(a.segs.size());
722     result.cuts = a.cuts;
723     for(unsigned i = 0; i < a.segs.size(); i++){
724         result.segs[i] = derivative(a.segs[i])/(a.cuts[i+1]-a.cuts[i]);
725     }
726     return result;
729 std::vector<double> roots(Piecewise<SBasis> const &f);
731 std::vector<std::vector<double> >multi_roots(Piecewise<SBasis> const &f, std::vector<double> const &values);
733 template<typename T>
734 Piecewise<T> reverse(Piecewise<T> const &f) {
735     Piecewise<T> ret = Piecewise<T>();
736     ret.cuts.resize(f.cuts.size());
737     ret.segs.resize(f.segs.size());
738     double start = f.cuts[0];
739     double end = f.cuts.back();
740     for (unsigned i = 0; i < f.cuts.size(); i++) {
741         double x = f.cuts[f.cuts.size() - 1 - i];
742         ret.cuts[i] = end - (x - start);
743     }
744     for (unsigned i = 0; i < f.segs.size(); i++)
745         ret.segs[i] = reverse(f[f.segs.size() - i - 1]);
746     return ret;
752 #endif //SEEN_GEOM_PW_SB_H
753 /*
754   Local Variables:
755   mode:c++
756   c-file-style:"stroustrup"
757   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
758   indent-tabs-mode:nil
759   fill-column:99
760   End:
761 */
762 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :