Subversion Repositories Scribus

Rev

Rev 12665 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12065 fschmid 1
/*
2
 *  sbasis.cpp - S-power basis function class + supporting classes
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
 */
33
 
34
#include <cmath>
35
 
36
#include "sbasis.h"
37
#include "isnan.h"
38
 
39
namespace Geom{
40
 
41
/*** At some point we should work on tighter bounds for the error.  It is clear that the error is
42
 * bounded by the L1 norm over the tail of the series, but this is very loose, leading to far too
43
 * many cubic beziers.  I've changed this to be \sum _i=tail ^\infty |hat a_i| 2^-i but I have no
44
 * evidence that this is correct.
45
 */
46
 
47
/*
48
double SBasis::tail_error(unsigned tail) const {
49
    double err = 0, s = 1./(1<<(2*tail)); // rough
50
    for(unsigned i = tail; i < size(); i++) {
51
        err += (fabs((*this)[i][0]) + fabs((*this)[i][1]))*s;
52
        s /= 4;
53
    }
54
    return err;
55
}
56
*/
57
 
58
double SBasis::tailError(unsigned tail) const {
59
  Interval bs = bounds_fast(*this, tail);
60
  return std::max(fabs(bs.min()),fabs(bs.max()));
61
}
62
 
63
bool SBasis::isFinite() const {
64
    for(unsigned i = 0; i < size(); i++) {
65
        if(!(*this)[i].isFinite())
66
            return false;
67
    }
68
    return true;
69
}
70
 
71
SBasis operator+(const SBasis& a, const SBasis& b) {
72
    SBasis result;
73
    const unsigned out_size = std::max(a.size(), b.size());
74
    const unsigned min_size = std::min(a.size(), b.size());
75
    result.reserve(out_size);
76
 
77
    for(unsigned i = 0; i < min_size; i++) {
78
        result.push_back(a[i] + b[i]);
79
    }
80
    for(unsigned i = min_size; i < a.size(); i++)
81
        result.push_back(a[i]);
82
    for(unsigned i = min_size; i < b.size(); i++)
83
        result.push_back(b[i]);
84
 
85
    assert(result.size() == out_size);
86
    return result;
87
}
88
 
89
SBasis operator-(const SBasis& a, const SBasis& b) {
90
    SBasis result;
91
    const unsigned out_size = std::max(a.size(), b.size());
92
    const unsigned min_size = std::min(a.size(), b.size());
93
    result.reserve(out_size);
94
 
95
    for(unsigned i = 0; i < min_size; i++) {
96
        result.push_back(a[i] - b[i]);
97
    }
98
    for(unsigned i = min_size; i < a.size(); i++)
99
        result.push_back(a[i]);
100
    for(unsigned i = min_size; i < b.size(); i++)
101
        result.push_back(-b[i]);
102
 
103
    assert(result.size() == out_size);
104
    return result;
105
}
106
 
107
SBasis& operator+=(SBasis& a, const SBasis& b) {
108
    const unsigned out_size = std::max(a.size(), b.size());
109
    const unsigned min_size = std::min(a.size(), b.size());
110
    a.reserve(out_size);
111
 
112
    for(unsigned i = 0; i < min_size; i++)
113
        a[i] += b[i];
114
    for(unsigned i = min_size; i < b.size(); i++)
115
        a.push_back(b[i]);
116
 
117
    assert(a.size() == out_size);
118
    return a;
119
}
120
 
121
SBasis& operator-=(SBasis& a, const SBasis& b) {
122
    const unsigned out_size = std::max(a.size(), b.size());
123
    const unsigned min_size = std::min(a.size(), b.size());
124
    a.reserve(out_size);
125
 
126
    for(unsigned i = 0; i < min_size; i++)
127
        a[i] -= b[i];
128
    for(unsigned i = min_size; i < b.size(); i++)
129
        a.push_back(-b[i]);
130
 
131
    assert(a.size() == out_size);
132
    return a;
133
}
134
 
135
SBasis operator*(SBasis const &a, double k) {
136
    SBasis c;
137
    c.reserve(a.size());
138
    for(unsigned i = 0; i < a.size(); i++)
139
        c.push_back(a[i] * k);
140
    return c;
141
}
142
 
143
SBasis& operator*=(SBasis& a, double b) {
144
    if (a.isZero()) return a;
145
    if (b == 0)
146
        a.clear();
147
    else
148
        for(unsigned i = 0; i < a.size(); i++)
149
            a[i] *= b;
150
    return a;
151
}
152
 
153
SBasis shift(SBasis const &a, int sh) {
154
    SBasis c = a;
155
    if(sh > 0) {
156
        c.insert(c.begin(), sh, Linear(0,0));
157
    } else {
158
        //TODO: truncate
159
    }
160
    return c;
161
}
162
 
163
SBasis shift(Linear const &a, int sh) {
164
    SBasis c;
165
    if(sh > 0) {
166
        c.insert(c.begin(), sh, Linear(0,0));
167
        c.push_back(a);
168
    }
169
    return c;
170
}
171
 
172
SBasis multiply(SBasis const &a, SBasis const &b) {
173
    // c = {a0*b0 - shift(1, a.Tri*b.Tri), a1*b1 - shift(1, a.Tri*b.Tri)}
174
 
175
    // shift(1, a.Tri*b.Tri)
176
    SBasis c;
177
    if(a.isZero() || b.isZero())
178
        return c;
179
    c.resize(a.size() + b.size(), Linear(0,0));
180
    c[0] = Linear(0,0);
181
    for(unsigned j = 0; j < b.size(); j++) {
182
        for(unsigned i = j; i < a.size()+j; i++) {
183
            double tri = Tri(b[j])*Tri(a[i-j]);
184
            c[i+1/*shift*/] += Linear(Hat(-tri));
185
        }
186
    }
187
    for(unsigned j = 0; j < b.size(); j++) {
188
        for(unsigned i = j; i < a.size()+j; i++) {
189
            for(unsigned dim = 0; dim < 2; dim++)
190
                c[i][dim] += b[j][dim]*a[i-j][dim];
191
        }
192
    }
193
    c.normalize();
194
    //assert(!(0 == c.back()[0] && 0 == c.back()[1]));
195
    return c;
196
}
197
 
198
SBasis integral(SBasis const &c) {
199
    SBasis a;
200
    a.resize(c.size() + 1, Linear(0,0));
201
    a[0] = Linear(0,0);
202
 
203
    for(unsigned k = 1; k < c.size() + 1; k++) {
204
        double ahat = -Tri(c[k-1])/(2*k);
205
        a[k] = Hat(ahat);
206
    }
207
    double aTri = 0;
208
    for(int k = c.size()-1; k >= 0; k--) {
209
        aTri = (Hat(c[k]).d + (k+1)*aTri/2)/(2*k+1);
210
        a[k][0] -= aTri/2;
211
        a[k][1] += aTri/2;
212
    }
213
    a.normalize();
214
    return a;
215
}
216
 
217
SBasis derivative(SBasis const &a) {
218
    SBasis c;
219
    c.resize(a.size(), Linear(0,0));
220
 
221
    for(unsigned k = 0; k < a.size(); k++) {
222
        double d = (2*k+1)*Tri(a[k]);
223
 
224
        for(unsigned dim = 0; dim < 2; dim++) {
225
            c[k][dim] = d;
226
            if(k+1 < a.size()) {
227
                if(dim)
228
                    c[k][dim] = d - (k+1)*a[k+1][dim];
229
                else
230
                    c[k][dim] = d + (k+1)*a[k+1][dim];
231
            }
232
        }
233
    }
234
 
235
    return c;
236
}
237
 
238
//TODO: convert int k to unsigned k, and remove cast
239
SBasis sqrt(SBasis const &a, int k) {
240
    SBasis c;
241
    if(a.isZero() || k == 0)
242
        return c;
243
    c.resize(k, Linear(0,0));
244
    c[0] = Linear(std::sqrt(a[0][0]), std::sqrt(a[0][1]));
245
    SBasis r = a - multiply(c, c); // remainder
246
 
247
    for(unsigned i = 1; i <= (unsigned)k and i<r.size(); i++) {
248
        Linear ci(r[i][0]/(2*c[0][0]), r[i][1]/(2*c[0][1]));
249
        SBasis cisi = shift(ci, i);
250
        r -= multiply(shift((c*2 + cisi), i), SBasis(ci));
251
        r.truncate(k+1);
252
        c += cisi;
253
        if(r.tailError(i) == 0) // if exact
254
            break;
255
    }
256
 
257
    return c;
258
}
259
 
260
// return a kth order approx to 1/a)
261
SBasis reciprocal(Linear const &a, int k) {
262
    SBasis c;
263
    assert(!a.isZero());
264
    c.resize(k, Linear(0,0));
265
    double r_s0 = (Tri(a)*Tri(a))/(-a[0]*a[1]);
266
    double r_s0k = 1;
267
    for(unsigned i = 0; i < (unsigned)k; i++) {
268
        c[i] = Linear(r_s0k/a[0], r_s0k/a[1]);
269
        r_s0k *= r_s0;
270
    }
271
    return c;
272
}
273
 
274
SBasis divide(SBasis const &a, SBasis const &b, int k) {
275
    SBasis c;
276
    assert(!a.isZero());
277
    SBasis r = a; // remainder
278
 
279
    k++;
280
    r.resize(k, Linear(0,0));
281
    c.resize(k, Linear(0,0));
282
 
283
    for(unsigned i = 0; i < (unsigned)k; i++) {
284
        Linear ci(r[i][0]/b[0][0], r[i][1]/b[0][1]); //H0
285
        c[i] += ci;
286
        r -= shift(multiply(ci,b), i);
287
        r.truncate(k+1);
288
        if(r.tailError(i) == 0) // if exact
289
            break;
290
    }
291
 
292
    return c;
293
}
294
 
295
// a(b)
296
// return a0 + s(a1 + s(a2 +...  where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
297
SBasis compose(SBasis const &a, SBasis const &b) {
298
    SBasis s = multiply((SBasis(Linear(1,1))-b), b);
299
    SBasis r;
300
 
301
    for(int i = a.size()-1; i >= 0; i--) {
302
        r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
303
    }
304
    return r;
305
}
306
 
307
// a(b)
308
// return a0 + s(a1 + s(a2 +...  where s = (1-u)u; ak =(1 - u)a^0_k + ua^1_k
309
SBasis compose(SBasis const &a, SBasis const &b, unsigned k) {
310
    SBasis s = multiply((SBasis(Linear(1,1))-b), b);
311
    SBasis r;
312
 
313
    for(int i = a.size()-1; i >= 0; i--) {
314
        r = SBasis(Linear(Hat(a[i][0]))) - b*a[i][0] + b*a[i][1] + multiply(r,s);
315
    }
316
    r.truncate(k);
317
    return r;
318
}
319
 
320
/*
321
Inversion algorithm. The notation is certainly very misleading. The
322
pseudocode should say:
323
 
324
c(v) := 0
325
r(u) := r_0(u) := u
326
for i:=0 to k do
327
  c_i(v) := H_0(r_i(u)/(t_1)^i; u)
328
  c(v) := c(v) + c_i(v)*t^i
329
  r(u) := r(u) ? c_i(u)*(t(u))^i
330
endfor
331
*/
332
 
333
//#define DEBUG_INVERSION 1
334
 
335
SBasis inverse(SBasis a, int k) {
336
    assert(a.size() > 0);
337
// the function should have 'unit range'("a00 = 0 and a01 = 1") and be monotonic.
338
    double a0 = a[0][0];
339
    if(a0 != 0) {
340
        a -= a0;
341
    }
342
    double a1 = a[0][1];
343
    assert(a1 != 0); // not invertable.
344
 
345
    if(a1 != 1) {
346
        a /= a1;
347
    }
348
    SBasis c;                           // c(v) := 0
349
    if(a.size() >= 2 && k == 2) {
350
        c.push_back(Linear(0,1));
351
        Linear t1(1+a[1][0], 1-a[1][1]);    // t_1
352
        c.push_back(Linear(-a[1][0]/t1[0], -a[1][1]/t1[1]));
353
    } else if(a.size() >= 2) {                      // non linear
354
        SBasis r = Linear(0,1);             // r(u) := r_0(u) := u
355
        Linear t1(1./(1+a[1][0]), 1./(1-a[1][1]));    // 1./t_1
356
        Linear one(1,1);
357
        Linear t1i = one;                   // t_1^0
358
        SBasis one_minus_a = SBasis(one) - a;
359
        SBasis t = multiply(one_minus_a, a); // t(u)
360
        SBasis ti(one);                     // t(u)^0
361
#ifdef DEBUG_INVERSION
362
        std::cout << "a=" << a << std::endl;
363
        std::cout << "1-a=" << one_minus_a << std::endl;
364
        std::cout << "t1=" << t1 << std::endl;
365
        //assert(t1 == t[1]);
366
#endif
367
 
368
        c.resize(k+1, Linear(0,0));
369
        for(unsigned i = 0; i < (unsigned)k; i++) {   // for i:=0 to k do
370
#ifdef DEBUG_INVERSION
371
            std::cout << "-------" << i << ": ---------" <<std::endl;
372
            std::cout << "r=" << r << std::endl
373
                      << "c=" << c << std::endl
374
                      << "ti=" << ti << std::endl
375
                      << std::endl;
376
#endif
377
            if(r.size() <= i)                // ensure enough space in the remainder, probably not needed
378
                r.resize(i+1, Linear(0,0));
379
            Linear ci(r[i][0]*t1i[0], r[i][1]*t1i[1]); // c_i(v) := H_0(r_i(u)/(t_1)^i; u)
380
#ifdef DEBUG_INVERSION
381
            std::cout << "t1i=" << t1i << std::endl;
382
            std::cout << "ci=" << ci << std::endl;
383
#endif
384
            for(int dim = 0; dim < 2; dim++) // t1^-i *= 1./t1
385
                t1i[dim] *= t1[dim];
386
            c[i] = ci; // c(v) := c(v) + c_i(v)*t^i
387
            // change from v to u parameterisation
388
            SBasis civ = one_minus_a*ci[0] + a*ci[1];
389
            // r(u) := r(u) - c_i(u)*(t(u))^i
390
            // We can truncate this to the number of final terms, as no following terms can
391
            // contribute to the result.
392
            r -= multiply(civ,ti);
393
            r.truncate(k);
394
            if(r.tailError(i) == 0)
395
                break; // yay!
396
            ti = multiply(ti,t);
397
        }
398
#ifdef DEBUG_INVERSION
399
        std::cout << "##########################" << std::endl;
400
#endif
401
    } else
402
        c = Linear(0,1); // linear
403
    c -= a0; // invert the offset
404
    c /= a1; // invert the slope
405
    return c;
406
}
407
 
408
SBasis sin(Linear b, int k) {
409
    SBasis s = Linear(std::sin(b[0]), std::sin(b[1]));
410
    Tri tr(s[0]);
411
    double t2 = Tri(b);
412
    s.push_back(Linear(std::cos(b[0])*t2 - tr, -std::cos(b[1])*t2 + tr));
413
 
414
    t2 *= t2;
415
    for(int i = 0; i < k; i++) {
416
        Linear bo(4*(i+1)*s[i+1][0] - 2*s[i+1][1],
417
                  -2*s[i+1][0] + 4*(i+1)*s[i+1][1]);
418
        bo -= s[i]*(t2/(i+1));
419
 
420
 
421
        s.push_back(bo/double(i+2));
422
    }
423
 
424
    return s;
425
}
426
 
427
SBasis cos(Linear bo, int k) {
428
    return sin(Linear(bo[0] + M_PI/2,
429
                      bo[1] + M_PI/2),
430
               k);
431
}
432
 
433
//compute fog^-1. ("zero" = double comparison threshold. *!*we might divide by "zero"*!*)
434
//TODO: compute order according to tol?
435
//TODO: requires g(0)=0 & g(1)=1 atm... adaptation to other cases should be obvious!
436
SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order, double zero){
437
    SBasis result; //result
438
    SBasis r=f; //remainder
439
    SBasis Pk=Linear(1)-g,Qk=g,sg=Pk*Qk;
440
    Pk.truncate(order);
441
    Qk.truncate(order);
442
    Pk.resize(order,Linear(0.));
443
    Qk.resize(order,Linear(0.));
444
    r.resize(order,Linear(0.));
445
 
446
    int vs= valuation(sg,zero);
447
 
448
    for (unsigned k=0; k<order; k+=vs){
449
        double p10 = Pk.at(k)[0];// we have to solve the linear system:
450
        double p01 = Pk.at(k)[1];//
451
        double q10 = Qk.at(k)[0];//   p10*a + q10*b = r10
452
        double q01 = Qk.at(k)[1];// &
453
        double r10 =  r.at(k)[0];//   p01*a + q01*b = r01
454
        double r01 =  r.at(k)[1];//
455
        double a,b;
456
        double det = p10*q01-p01*q10;
457
 
458
        //TODO: handle det~0!!
459
        if (fabs(det)<zero){
460
            det = zero;
461
            a=b=0;
462
        }else{
463
            a=( q01*r10-q10*r01)/det;
464
            b=(-p01*r10+p10*r01)/det;
465
        }
466
        result.push_back(Linear(a,b));
467
        r=r-Pk*a-Qk*b;
468
 
469
        Pk=Pk*sg;
470
        Qk=Qk*sg;
471
        Pk.truncate(order);
472
        Qk.truncate(order);
473
        r.truncate(order);
474
    }
475
    result.normalize();
476
    return result;
477
}
478
 
479
}
480
 
481
/*
482
  Local Variables:
483
  mode:c++
484
  c-file-style:"stroustrup"
485
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
486
  indent-tabs-mode:nil
487
  fill-column:99
488
  End:
489
*/
490
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :