Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rall2d.h « utilities « kdl « itasc « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbd9e70b04f047a11ec84641b07e4ac281484b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538

/*****************************************************************************
 * \file  
 *      class for automatic differentiation on scalar values and 1st 
 *      derivatives and 2nd derivative.
 *       
 *  \author 
 *      Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven
 *
 *  \version 
 *      ORO_Geometry V0.2
 *
 *  \par Note
 *      VC6++ contains a bug, concerning the use of inlined friend functions 
 *      in combination with namespaces.  So, try to avoid inlined friend 
 *      functions !  
 *
 *  \par History
 *      - $log$ 
 *
 *  \par Release
 *      $Id$
 *      $Name:  $ 
 ****************************************************************************/

#ifndef Rall2D_H
#define Rall2D_H

#include <math.h>
#include <assert.h>
#include "utility.h"


namespace KDL {

/**
 * Rall2d contains a value, and its gradient and its 2nd derivative, and defines an algebraic 
 * structure on this pair.
 *  This template class has 3 template parameters :
 *  -   T contains the type of the value.
 *  -   V contains the type of the gradient (can be a vector-like type).  
 *  -   S defines a scalar type that can operate on Rall1d.  This is the type that 
 *      is used to give back values of Norm() etc. 
 *
 * S is usefull when you recurse a Rall1d object into itself to create a 2nd, 3th, 4th,.. 
 * derivatives. (e.g. Rall1d< Rall1d<double>, Rall1d<double>, double> ).
 *
 * S is always passed by value. 
 *
 * \par Class Type
 * Concrete implementation
 */
template <class T,class V=T,class S=T>
class Rall2d                      
    {
    public :
        T t;    //!< value
        V d;    //!< 1st derivative
        V dd;   //!< 2nd derivative
    public :
        // = Constructors
        INLINE Rall2d() {}

        explicit INLINE  Rall2d(typename TI<T>::Arg c)
            {t=c;SetToZero(d);SetToZero(dd);}

        INLINE Rall2d(typename TI<T>::Arg tn,const V& afg):t(tn),d(afg) {SetToZero(dd);}

        INLINE Rall2d(typename TI<T>::Arg tn,const V& afg,const V& afg2):t(tn),d(afg),dd(afg2) {}

        // = Copy Constructor
        INLINE Rall2d(const Rall2d<T,V,S>& r):t(r.t),d(r.d),dd(r.dd) {}
            //if one defines this constructor, it's better optimized then the
            //automatically generated one ( that one set's up a loop to copy
            // word by word.
        
        // = Member functions to access internal structures :
        INLINE T& Value() {
            return t;
        }

        INLINE V& D() {
            return d;
        }

        INLINE V& DD() {
            return dd;
        }
        INLINE static Rall2d<T,V,S> Zero() {
            Rall2d<T,V,S> tmp;
            SetToZero(tmp);
            return tmp;
        }
        INLINE static Rall2d<T,V,S> Identity() {
            Rall2d<T,V,S> tmp;
            SetToIdentity(tmp);
            return tmp;
        }

        // = assignment operators
        INLINE Rall2d<T,V,S>& operator =(S c)
            {t=c;SetToZero(d);SetToZero(dd);return *this;}

        INLINE Rall2d<T,V,S>& operator =(const Rall2d<T,V,S>& r)
            {t=r.t;d=r.d;dd=r.dd;return *this;}

        INLINE Rall2d<T,V,S>& operator /=(const Rall2d<T,V,S>& rhs)
            {
            t /= rhs.t;
            d = (d-t*rhs.d)/rhs.t;
            dd= (dd - S(2)*d*rhs.d-t*rhs.dd)/rhs.t;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator *=(const Rall2d<T,V,S>& rhs)
            {
            t *= rhs.t;
            d  = (d*rhs.t+t*rhs.d);
            dd = (dd*rhs.t+S(2)*d*rhs.d+t*rhs.dd);
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator +=(const Rall2d<T,V,S>& rhs)
            {
            t    +=rhs.t;
            d +=rhs.d;
            dd+=rhs.dd;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator -=(const Rall2d<T,V,S>& rhs)
            {
            t     -= rhs.t;
            d     -= rhs.d;
            dd    -= rhs.dd;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator /=(S rhs)
            {
            t    /= rhs;
            d    /= rhs;
            dd   /= rhs;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator *=(S rhs)
            {
            t    *= rhs;
            d    *= rhs;
            dd   *= rhs;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator -=(S rhs)
            {
            t    -= rhs;
            return *this;
            }

        INLINE Rall2d<T,V,S>& operator +=(S rhs)
            {
            t    += rhs;
            return *this;
            }

        // = Operators between Rall2d objects
/*
         friend INLINE Rall2d<T,V,S> operator /(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs);
         friend INLINE Rall2d<T,V,S> operator *(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs);
         friend INLINE Rall2d<T,V,S> operator +(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs);
         friend INLINE Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs);
         friend INLINE Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> operator *(S s,const Rall2d<T,V,S>& v);
         friend INLINE Rall2d<T,V,S> operator *(const Rall2d<T,V,S>& v,S s);
         friend INLINE Rall2d<T,V,S> operator +(S s,const Rall2d<T,V,S>& v);
         friend INLINE Rall2d<T,V,S> operator +(const Rall2d<T,V,S>& v,S s);
         friend INLINE Rall2d<T,V,S> operator -(S s,const Rall2d<T,V,S>& v);
         friend INLINE INLINE Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& v,S s);
         friend INLINE Rall2d<T,V,S> operator /(S s,const Rall2d<T,V,S>& v);
         friend INLINE Rall2d<T,V,S> operator /(const Rall2d<T,V,S>& v,S s);

        // = Mathematical functions that operate on Rall2d objects

         friend INLINE Rall2d<T,V,S> exp(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> log(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> sin(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> cos(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> tan(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> sinh(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> cosh(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> tanh(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> sqr(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> pow(const Rall2d<T,V,S>& arg,double m) ;
         friend INLINE Rall2d<T,V,S> sqrt(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> asin(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> acos(const Rall2d<T,V,S>& arg);
         friend INLINE Rall2d<T,V,S> atan(const Rall2d<T,V,S>& x);
         friend INLINE Rall2d<T,V,S> atan2(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x);
         friend INLINE Rall2d<T,V,S> abs(const Rall2d<T,V,S>& x);
         friend INLINE Rall2d<T,V,S> hypot(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x);
        // returns sqrt(y*y+x*x), but is optimized for accuracy and speed.
         friend INLINE S Norm(const Rall2d<T,V,S>& value) ;
        // returns Norm( value.Value() ).

        // = Some utility functions to improve performance
        // (should also be declared on primitive types to improve uniformity
         friend INLINE Rall2d<T,V,S> LinComb(S alfa,const Rall2d<T,V,S>& a,
            TI<T>::Arg beta,const Rall2d<T,V,S>& b );
         friend INLINE void LinCombR(S alfa,const Rall2d<T,V,S>& a,
            TI<T>::Arg beta,const Rall2d<T,V,S>& b,Rall2d<T,V,S>& result );
        // = Setting value of a Rall2d object to 0 or 1
         friend INLINE void SetToZero(Rall2d<T,V,S>& value);
         friend INLINE void SetToOne(Rall2d<T,V,S>& value);
        // = Equality in an eps-interval
         friend INLINE bool Equal(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x,double eps);
         */
    };





// = Operators between Rall2d objects
template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator /(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs)
    {
    Rall2d<T,V,S> tmp;
    tmp.t = lhs.t/rhs.t;
    tmp.d = (lhs.d-tmp.t*rhs.d)/rhs.t;
    tmp.dd= (lhs.dd-S(2)*tmp.d*rhs.d-tmp.t*rhs.dd)/rhs.t;
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator *(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs)
    {
    Rall2d<T,V,S> tmp;
    tmp.t  = lhs.t*rhs.t;
    tmp.d  = (lhs.d*rhs.t+lhs.t*rhs.d);
    tmp.dd = (lhs.dd*rhs.t+S(2)*lhs.d*rhs.d+lhs.t*rhs.dd);
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator +(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs)
    {
    return Rall2d<T,V,S>(lhs.t+rhs.t,lhs.d+rhs.d,lhs.dd+rhs.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& lhs,const Rall2d<T,V,S>& rhs)
    {
    return Rall2d<T,V,S>(lhs.t-rhs.t,lhs.d-rhs.d,lhs.dd-rhs.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& arg)
    {
    return Rall2d<T,V,S>(-arg.t,-arg.d,-arg.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator *(S s,const Rall2d<T,V,S>& v)
    {
    return Rall2d<T,V,S>(s*v.t,s*v.d,s*v.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator *(const Rall2d<T,V,S>& v,S s)
    {
    return Rall2d<T,V,S>(v.t*s,v.d*s,v.dd*s);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator +(S s,const Rall2d<T,V,S>& v)
    {
    return Rall2d<T,V,S>(s+v.t,v.d,v.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator +(const Rall2d<T,V,S>& v,S s)
    {
    return Rall2d<T,V,S>(v.t+s,v.d,v.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator -(S s,const Rall2d<T,V,S>& v)
    {
    return Rall2d<T,V,S>(s-v.t,-v.d,-v.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator -(const Rall2d<T,V,S>& v,S s)
    {
    return Rall2d<T,V,S>(v.t-s,v.d,v.dd);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator /(S s,const Rall2d<T,V,S>& rhs)
    {
    Rall2d<T,V,S> tmp;
    tmp.t = s/rhs.t;
    tmp.d = (-tmp.t*rhs.d)/rhs.t;
    tmp.dd= (-S(2)*tmp.d*rhs.d-tmp.t*rhs.dd)/rhs.t;
    return tmp;
}


template <class T,class V,class S>
INLINE  Rall2d<T,V,S> operator /(const Rall2d<T,V,S>& v,S s)
    {
    return Rall2d<T,V,S>(v.t/s,v.d/s,v.dd/s);
    }


template <class T,class V,class S>
INLINE  Rall2d<T,V,S> exp(const Rall2d<T,V,S>& arg)
    {
    Rall2d<T,V,S> tmp;
    tmp.t  = exp(arg.t);
    tmp.d  = tmp.t*arg.d;
    tmp.dd = tmp.d*arg.d+tmp.t*arg.dd;
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> log(const Rall2d<T,V,S>& arg)
    {
    Rall2d<T,V,S> tmp;
    tmp.t  = log(arg.t);
    tmp.d  = arg.d/arg.t;
    tmp.dd = (arg.dd-tmp.d*arg.d)/arg.t;
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> sin(const Rall2d<T,V,S>& arg)
    {
    T v1 = sin(arg.t);
    T v2 = cos(arg.t);
    return Rall2d<T,V,S>(v1,v2*arg.d,v2*arg.dd - (v1*arg.d)*arg.d );
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> cos(const Rall2d<T,V,S>& arg)
    {
    T v1 = cos(arg.t);
    T v2 = -sin(arg.t);
    return Rall2d<T,V,S>(v1,v2*arg.d, v2*arg.dd - (v1*arg.d)*arg.d);
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> tan(const Rall2d<T,V,S>& arg)
    {
    T v1 = tan(arg.t);
    T v2 = S(1)+sqr(v1);
    return Rall2d<T,V,S>(v1,v2*arg.d, v2*(arg.dd+(S(2)*v1*sqr(arg.d))));
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> sinh(const Rall2d<T,V,S>& arg)
    {
    T v1 = sinh(arg.t);
    T v2 = cosh(arg.t);
    return Rall2d<T,V,S>(v1,v2*arg.d,v2*arg.dd + (v1*arg.d)*arg.d );
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> cosh(const Rall2d<T,V,S>& arg)
    {
    T v1 = cosh(arg.t);
    T v2 = sinh(arg.t);
    return Rall2d<T,V,S>(v1,v2*arg.d,v2*arg.dd + (v1*arg.d)*arg.d );
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> tanh(const Rall2d<T,V,S>& arg)
    {       
    T v1 = tanh(arg.t);
    T v2 = S(1)-sqr(v1);
    return Rall2d<T,V,S>(v1,v2*arg.d, v2*(arg.dd-(S(2)*v1*sqr(arg.d))));
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> sqr(const Rall2d<T,V,S>& arg)
    {
    return Rall2d<T,V,S>(arg.t*arg.t,
                        (S(2)*arg.t)*arg.d,
                        S(2)*(sqr(arg.d)+arg.t*arg.dd)
                        );
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> pow(const Rall2d<T,V,S>& arg,double m) 
    {
    Rall2d<T,V,S> tmp;
    tmp.t = pow(arg.t,m);
    T v2  = (m/arg.t)*tmp.t;
    tmp.d  = v2*arg.d;
    tmp.dd = (S((m-1))/arg.t)*tmp.d*arg.d + v2*arg.dd;
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> sqrt(const Rall2d<T,V,S>& arg)
    {
    /* By inversion of sqr(x) :*/
    Rall2d<T,V,S> tmp;
    tmp.t  = sqrt(arg.t);
    tmp.d  = (S(0.5)/tmp.t)*arg.d;
    tmp.dd = (S(0.5)*arg.dd-sqr(tmp.d))/tmp.t;
    return tmp;
    }

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> asin(const Rall2d<T,V,S>& arg)
{
    /* By inversion of sin(x) */
    Rall2d<T,V,S> tmp;
    tmp.t  = asin(arg.t);
    T v = cos(tmp.t);
    tmp.d  = arg.d/v;
    tmp.dd = (arg.dd+arg.t*sqr(tmp.d))/v;
    return tmp;
}

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> acos(const Rall2d<T,V,S>& arg)
{
    /* By inversion of cos(x) */
    Rall2d<T,V,S> tmp;
    tmp.t  = acos(arg.t);
    T v = -sin(tmp.t);
    tmp.d  = arg.d/v;
    tmp.dd = (arg.dd+arg.t*sqr(tmp.d))/v;
    return tmp;

}

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> atan(const Rall2d<T,V,S>& x)
{
    /* By inversion of tan(x) */
    Rall2d<T,V,S> tmp;
    tmp.t  = atan(x.t);
    T v    = S(1)+sqr(x.t);
    tmp.d  = x.d/v;
    tmp.dd = x.dd/v-(S(2)*x.t)*sqr(tmp.d);
    return tmp;
}

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> atan2(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x)
{
    Rall2d<T,V,S> tmp;
    tmp.t  = atan2(y.t,x.t);
    T v    = sqr(y.t)+sqr(x.t);
    tmp.d  = (x.t*y.d-x.d*y.t)/v;
    tmp.dd = ( x.t*y.dd-x.dd*y.t-S(2)*(x.t*x.d+y.t*y.d)*tmp.d ) / v;
    return tmp;
}

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> abs(const Rall2d<T,V,S>& x)
{
    T v(Sign(x));
    return Rall2d<T,V,S>(v*x,v*x.d,v*x.dd);
}

template <class T,class V,class S>
INLINE  Rall2d<T,V,S> hypot(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x)
{
    Rall2d<T,V,S> tmp;
    tmp.t  = hypot(y.t,x.t);
    tmp.d  = (x.t*x.d+y.t*y.d)/tmp.t;
    tmp.dd = (sqr(x.d)+x.t*x.dd+sqr(y.d)+y.t*y.dd-sqr(tmp.d))/tmp.t;
    return tmp;
}
// returns sqrt(y*y+x*x), but is optimized for accuracy and speed.

template <class T,class V,class S>
INLINE  S Norm(const Rall2d<T,V,S>& value) 
{
    return Norm(value.t);
}
// returns Norm( value.Value() ).


// (should also be declared on primitive types to improve uniformity
template <class T,class V,class S>
INLINE  Rall2d<T,V,S> LinComb(S alfa,const Rall2d<T,V,S>& a,
    const T& beta,const Rall2d<T,V,S>& b ) {
        return Rall2d<T,V,S>(
            LinComb(alfa,a.t,beta,b.t),
            LinComb(alfa,a.d,beta,b.d),
            LinComb(alfa,a.dd,beta,b.dd) 
        );
}

template <class T,class V,class S>
INLINE  void LinCombR(S alfa,const Rall2d<T,V,S>& a,
    const T& beta,const Rall2d<T,V,S>& b,Rall2d<T,V,S>& result ) {
            LinCombR(alfa, a.t,       beta, b.t,      result.t);
            LinCombR(alfa, a.d,    beta, b.d,   result.d);
            LinCombR(alfa, a.dd,    beta, b.dd,   result.dd);
}

template <class T,class V,class S>
INLINE  void SetToZero(Rall2d<T,V,S>& value)
    {
    SetToZero(value.t);
    SetToZero(value.d);
    SetToZero(value.dd);
    }

template <class T,class V,class S>
INLINE  void SetToIdentity(Rall2d<T,V,S>& value)
    {
    SetToZero(value.d);
    SetToIdentity(value.t);
    SetToZero(value.dd);
    }

template <class T,class V,class S>
INLINE  bool Equal(const Rall2d<T,V,S>& y,const Rall2d<T,V,S>& x,double eps=epsilon)
{
    return (Equal(x.t,y.t,eps)&&
            Equal(x.d,y.d,eps)&&
            Equal(x.dd,y.dd,eps)
            );
}


}


#endif