Subversion Repositories Scribus

Rev

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