Improve the derivative class for hermites. Compare http://synfig.org/images/a/a8...
[synfig.git] / ETL / trunk / ETL / _calculus.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Calculus Functional Classes Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __ETL__CALCULUS_H
25 #define __ETL__CALCULUS_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <functional>
30
31 #include "hermite"
32
33 /* === M A C R O S ========================================================= */
34
35 //#ifndef _EPSILON
36 //#define _EPSILON              0.0000001
37 //#endif
38
39 /* === T Y P E D E F S ===================================================== */
40
41 /* === C L A S S E S & S T R U C T S ======================================= */
42
43 _ETL_BEGIN_NAMESPACE
44
45 template <typename T>
46 class derivative : public std::unary_function<typename T::argument_type,typename T::result_type>
47 {
48         T func;
49         typename T::argument_type epsilon;
50 public:
51         explicit derivative(const T &x, const typename T::argument_type &epsilon=0.000001):func(x),epsilon(epsilon) { }
52
53         typename T::result_type
54         operator()(const typename T::argument_type &x)const
55         {
56                 return (func(x+epsilon)-func(x))/epsilon;
57         }
58 };
59
60 template <typename T>
61 class derivative<hermite<T> > : public std::unary_function<typename hermite<T>::argument_type,typename hermite<T>::result_type>
62 {
63         hermite<T> func;
64 public:
65         explicit derivative(const hermite<T> &x):func(x) { }
66
67         typename hermite<T>::result_type
68         operator()(const typename hermite<T>::argument_type &x)const
69         {
70                 T a = func[0], b = func[1], c = func[2], d = func[3];
71                 typename hermite<T>::argument_type y(1-x);
72                 return ((b-a)*y*y + (c-b)*x*y*2 + (d-c)*x*x) * 3;
73         }
74 };
75
76 template <typename T>
77 class integral : public std::binary_function<typename T::argument_type,typename T::argument_type,typename T::result_type>
78 {
79         T func;
80         int samples;
81 public:
82         explicit integral(const T &x, const int &samples=500):func(x),samples(samples) { }
83
84         typename T::result_type
85         operator()(typename T::argument_type x,typename T::argument_type y)const
86         {
87                 typename T::result_type ret=0;
88                 int i=samples;
89                 const typename T::argument_type increment=(y-x)/i;
90
91                 for(;i;i--,x+=increment)
92                         ret+=(func(x)+func(x+increment))*increment/2;
93                 return ret;
94         }
95 };
96
97 _ETL_END_NAMESPACE
98
99 /* === E N D =============================================================== */
100
101 #endif