Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
piecewise.cpp
Go to the documentation of this file.
1/*
2 * piecewise.cpp - Piecewise function class
3 *
4 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
5 * Copyright 2007 JF Barraud
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 */
31
32#include <2geom/piecewise.h>
33#include <iterator>
34#include <map>
35
36namespace Geom {
37
39 Piecewise<SBasis> pa = partition(a, b.cuts), pb = partition(b, a.cuts);
41 assert(pa.size() == pb.size());
42 ret.cuts = pa.cuts;
43 for (unsigned i = 0; i < pa.size(); i++)
44 ret.push_seg(divide(pa[i], pb[i], k));
45 return ret;
46}
47
48Piecewise<SBasis>
49divide(Piecewise<SBasis> const &a, Piecewise<SBasis> const &b, double tol, unsigned k, double zero) {
50 Piecewise<SBasis> pa = partition(a, b.cuts), pb = partition(b, a.cuts);
52 assert(pa.size() == pb.size());
53 for (unsigned i = 0; i < pa.size(); i++){
54 Piecewise<SBasis> divi = divide(pa[i], pb[i], tol, k, zero);
55 divi.setDomain(Interval(pa.cuts[i],pa.cuts[i+1]));
56 ret.concat(divi);
57 }
58 return ret;
59}
60Piecewise<SBasis> divide(Piecewise<SBasis> const &a, SBasis const &b, double tol, unsigned k, double zero){
61 return divide(a,Piecewise<SBasis>(b),tol,k,zero);
62}
63Piecewise<SBasis> divide(SBasis const &a, Piecewise<SBasis> const &b, double tol, unsigned k, double zero){
64 return divide(Piecewise<SBasis>(a),b,tol,k,zero);
65}
66Piecewise<SBasis> divide(SBasis const &a, SBasis const &b, double tol, unsigned k, double zero) {
67 if (b.tailError(0)<2*zero){
68 //TODO: have a better look at sgn(b).
69 double sgn= (b(.5)<0.)?-1.:1;
70 return Piecewise<SBasis>(Linear(sgn/zero)*a);
71 }
72
73 if (fabs(b.at0())>zero && fabs(b.at1())>zero ){
74 SBasis c,r=a;
75 //TODO: what is a good relative tol? atm, c=a/b +/- (tol/a)%...
76
77 k+=1;
78 r.resize(k, Linear(0,0));
79 c.resize(k, Linear(0,0));
80
81 //assert(b.at0()!=0 && b.at1()!=0);
82 for (unsigned i=0; i<k; i++){
83 Linear ci = Linear(r[i][0]/b[0][0],r[i][1]/b[0][1]);
84 c[i]=ci;
85 r-=shift(ci*b,i);
86 }
87
88 if (r.tailError(k)<tol) return Piecewise<SBasis>(c);
89 }
90
92 c0 = divide(compose(a,Linear(0.,.5)),compose(b,Linear(0.,.5)),tol,k);
93 c1 = divide(compose(a,Linear(.5,1.)),compose(b,Linear(.5,1.)),tol,k);
94 c0.setDomain(Interval(0.,.5));
95 c1.setDomain(Interval(.5,1.));
96 c0.concat(c1);
97 return c0;
98}
99
100
101//-- compose(pw<T>,SBasis) ---------------
102/*
103 the purpose of the following functions is only to reduce the code in piecewise.h
104 TODO: use a vector<pairs<double,unsigned> > instead of a map<double,unsigned>.
105 */
106
107std::map<double,unsigned> compose_pullback(std::vector<double> const &values, SBasis const &g){
108 std::map<double,unsigned> result;
109
110 std::vector<std::vector<double> > roots = multi_roots(g, values);
111 for(unsigned i=0; i<roots.size(); i++){
112 for(unsigned j=0; j<roots[i].size();j++){
113 result[roots[i][j]]=i;
114 }
115 }
116 // Also map 0 and 1 to the first value above(or =) g(0) and g(1).
117 if(result.count(0.)==0){
118 unsigned i=0;
119 while (i<values.size()&&(g.at0()>values[i])) i++;
120 result[0.]=i;
121 }
122 if(result.count(1.)==0){
123 unsigned i=0;
124 while (i<values.size()&&(g.at1()>values[i])) i++;
125 result[1.]=i;
126 }
127 return(result);
128}
129
130int compose_findSegIdx(std::map<double,unsigned>::iterator const &cut,
131 std::map<double,unsigned>::iterator const &next,
132 std::vector<double> const &levels,
133 SBasis const &g){
134 double t0=(*cut).first;
135 unsigned idx0=(*cut).second;
136 double t1=(*next).first;
137 unsigned idx1=(*next).second;
138 assert(t0<t1);
139 int idx; //idx of the relevant f.segs
140 if (std::max(idx0,idx1)==levels.size()){ //g([t0,t1]) is above the top level,
141 idx=levels.size()-1;
142 } else if (idx0 != idx1){ //g([t0,t1]) crosses from level idx0 to idx1,
143 idx=std::min(idx0,idx1);
144 } else if(g((t0+t1)/2) < levels[idx0]) { //g([t0,t1]) is a 'U' under level idx0,
145 idx=idx0-1;
146 } else if(g((t0+t1)/2) > levels[idx0]) { //g([t0,t1]) is a 'bump' over level idx0,
147 idx=idx0;
148 } else { //g([t0,t1]) is contained in level idx0!...
149 idx = (idx0==levels.size())? idx0-1:idx0;
150 }
151
152 //move idx back from levels f.cuts
153 idx+=1;
154 return idx;
155}
156
157
158Piecewise<SBasis> pw_compose_inverse(SBasis const &f, SBasis const &g, unsigned order, double zero){
160
161 assert( f.size()>0 && g.size()>0);
162 SBasis g01 = g;
163 bool flip = ( g01.at0() > g01.at1() );
164
165 //OptInterval g_range = bounds_exact(g);
166 OptInterval g_range( Interval( g.at0(), g.at1() ));
167
168 g01 -= g_range->min();
169 g01 /= g_range->extent();
170 if ( flip ){
171 g01 *= -1.;
172 g01 += 1.;
173 }
174#if 1
175 assert( std::abs( g01.at0() - 0. ) < zero );
176 assert( std::abs( g01.at1() - 1. ) < zero );
177 //g[0][0] = 0.;
178 //g[0][1] = 1.;
179#endif
180
181 SBasis foginv = compose_inverse( f, g01, order, zero );
182 SBasis err = compose( foginv, g01) - f;
183
184 if ( err.tailError(0) < zero ){
185 result = Piecewise<SBasis> (foginv);
186 }else{
187 SBasis g_portion = portion( g01, Interval(0.,.5) );
188 SBasis f_portion = portion( f, Interval(0.,.5) );
189 result = pw_compose_inverse(f_portion, g_portion, order, zero);
190
191 g_portion = portion( g01, Interval(.5, 1.) );
192 f_portion = portion( f, Interval(.5, 1.) );
193 Piecewise<SBasis> result_next;
194 result_next = pw_compose_inverse(f_portion, g_portion, order, zero);
195 result.concat( result_next );
196 }
197 if (flip) {
199 }
200 result.setDomain(*g_range);
201 return result;
202}
203
204
205std::vector<double> roots(Piecewise<SBasis> const &f){
206 std::vector<double> result;
207 for (unsigned i=0; i<f.size(); i++){
208 std::vector<double> rts=roots(f.segs[i]);
209
210 for (double rt : rts){
211 result.push_back(f.mapToDomain(rt, i));
212 }
213 }
214 return result;
215}
216
217std::vector<std::vector<double> > multi_roots(Piecewise<SBasis> const &f, std::vector<double> const &values) {
218 std::vector<std::vector<double> > result(values.size());
219 for (unsigned i=0; i<f.size(); i++) {
220 std::vector<std::vector<double> > rts = multi_roots(f.segs[i], values);
221 for(unsigned j=0; j<rts.size(); j++) {
222 for(unsigned r=0; r<rts[j].size(); r++){
223 result[j].push_back(f.mapToDomain(rts[j][r], i));
224 }
225 }
226 }
227 return result;
228}
229
230
231std::vector<Interval> level_set(Piecewise<SBasis> const &f, Interval const &level, double tol){
232 std::vector<Interval> result;
233 for (unsigned i=0; i<f.size(); i++){
234 std::vector<Interval> resulti = level_set( f[i], level, 0., 1., tol);
235 for (unsigned j=0; j<resulti.size(); j++){
236 double a = f.cuts[i] + resulti[j].min() * ( f.cuts[i+1] - f.cuts[i] );
237 double b = f.cuts[i] + resulti[j].max() * ( f.cuts[i+1] - f.cuts[i] );
238 Interval domj( a, b );
239 //Interval domj( f.mapToDomain(resulti[j].min(), i ), f.mapToDomain(resulti[j].max(), i ) );
240
241 if ( j==0 && !result.empty() && result.back().intersects(domj) ){
242 result.back().unionWith(domj);
243 }else{
244 result.push_back(domj);
245 }
246 }
247 }
248 return result;
249}
250std::vector<Interval> level_set(Piecewise<SBasis> const &f, double v, double vtol, double tol){
251 Interval level ( v-vtol, v+vtol );
252 return level_set( f, level, tol);
253}
254
255
256}
257/*
258 Local Variables:
259 mode:c++
260 c-file-style:"stroustrup"
261 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
262 indent-tabs-mode:nil
263 fill-column:99
264 End:
265*/
266// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Range of real numbers that is never empty.
Definition interval.h:59
Function that interpolates linearly between two values.
Definition linear.h:55
Range of real numbers that can be empty.
Definition interval.h:199
Function defined as discrete pieces.
Definition piecewise.h:71
unsigned size() const
Definition piecewise.h:131
void push_seg(const T &s)
Definition piecewise.h:157
std::vector< double > cuts
Definition piecewise.h:75
void concat(const Piecewise< T > &other)
Definition piecewise.h:235
std::vector< T > segs
Definition piecewise.h:76
void setDomain(Interval dom)
Definition piecewise.h:218
double mapToDomain(double t, unsigned i) const
Definition piecewise.h:191
Polynomial in symmetric power basis.
Definition sbasis.h:70
size_t size() const
Definition sbasis.h:76
Coord at1() const
Definition sbasis.h:214
double tailError(unsigned tail) const
bound the error from term truncation
Definition sbasis.cpp:49
void resize(unsigned n)
Definition sbasis.h:98
Coord at0() const
Definition sbasis.h:212
Css & result
double c[8][4]
const unsigned order
Various utility functions.
Definition affine.h:22
Bezier reverse(const Bezier &a)
Definition bezier.h:342
SBasisN< n > divide(SBasisN< n > const &a, SBasisN< n > const &b, int k)
SBasisN< n > compose_inverse(SBasisN< n > const &f, SBasisN< n > const &g, unsigned order=2, double tol=1e-3)
int sgn(const T &x)
Sign function - indicates the sign of a numeric type.
Definition math-utils.h:51
int compose_findSegIdx(std::map< double, unsigned >::iterator const &cut, std::map< double, unsigned >::iterator const &next, std::vector< double > const &levels, SBasis const &g)
SBasisOf< T > shift(SBasisOf< T > const &a, int sh)
Definition sbasis-of.h:435
D2< T > compose(D2< T > const &a, T const &b)
Definition d2.h:405
std::vector< double > roots(SBasis const &s)
Bezier portion(const Bezier &a, double from, double to)
Definition bezier.cpp:250
Piecewise< SBasis > pw_compose_inverse(SBasis const &f, SBasis const &g, unsigned order, double zero)
std::vector< std::vector< double > > multi_roots(SBasis const &f, std::vector< double > const &levels, double htol=1e-7, double vtol=1e-7, double a=0, double b=1)
std::map< double, unsigned > compose_pullback(std::vector< double > const &cuts, SBasis const &g)
std::vector< Interval > level_set(D2< SBasis > const &f, Rect region)
Piecewise function class.