Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / ETL / tags / ETL_0_04_10_rc3 / ETL / _hermite.h
1 /*! ========================================================================
2 ** Extended Template Library
3 ** Hermite Template Class 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 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL_HERMITE_H
28 #define __ETL_HERMITE_H
29
30 /* === H E A D E R S ======================================================= */
31
32 #include "bezier"
33
34 /* === M A C R O S ========================================================= */
35
36 /* === T Y P E D E F S ===================================================== */
37
38 /* === C L A S S E S & S T R U C T S ======================================= */
39
40 _ETL_BEGIN_NAMESPACE
41
42 /*
43 template <typename T>
44 class hermite_base : std::unary_function<float,T>
45 {
46 public:
47         typedef T value_type;
48         typedef float time_type;
49 private:
50         affine_combo<value_type,time_type> affine_func;
51         value_type a,b,c,d;
52         time_type r,s;
53
54         value_type _coeff[3];
55         time_type drs; // reciprocal of (s-r)
56 public:
57         hermite_base():r(0.0),s(1.0) { drs=1.0/(s-r); }
58         hermite_base(
59                 const value_type &a, const value_type &b, const value_type &c, const value_type &d,
60                 const time_type &r=0.0, const time_type &s=1.0):
61                 a(a),b(b),c(c),d(d),r(r),s(s) { sync(); }
62
63         void sync(void)
64         {
65                 drs=1.0/(s-r);
66                 _coeff[0]=           c;
67                 _coeff[1]=-d*1 - c*2 + b*3 - a*3;
68                 _coeff[2]= d*1 + c*1 - b*2 + a*2;
69         }
70
71         inline value_type
72         operator()(time_type t)const
73         { t-=r; t*=drs; return a + (_coeff[0]+(_coeff[1]+(_coeff[2])*t)*t)*t; }
74
75         void set_rs(time_type new_r, time_type new_s) { r=new_r; s=new_s; drs=1.0/(s-r); }
76         void set_r(time_type new_r) { r=new_r; drs=1.0/(s-r); }
77         void set_s(time_type new_s) { s=new_s; drs=1.0/(s-r); }
78         const time_type &get_r(void)const { return r; }
79         const time_type &get_s(void)const { return s; }
80         time_type get_dt(void)const { return s-r; }
81
82         value_type &
83         operator[](int i)
84         { return (&a)[i]; }
85
86         const value_type &
87         operator[](int i) const
88         { return (&a)[i]; }
89 };
90
91
92 template <typename T>
93 class hermite : public hermite_base<T>
94 {
95 public:
96         typedef T value_type;
97         typedef float time_type;
98
99
100
101 public:
102         hermite() { }
103         hermite(const value_type &p1, const value_type &p2, const value_type &t1, const value_type &t2):
104         P1(p1),P2(p2),T1(t1),T2(t2) { sync(); }
105         hermite(const value_type &p1, const value_type &p2):
106         P1(p1),P2(p2),T1(p2-p1),T2(p2-p1) { sync(); }
107
108         value_type P1,P2,T1,T2;
109
110         value_type &p1(void) { return P1; }
111         value_type &p2(void) { return P2; }
112         value_type &t1(void) { return T1; }
113         value_type &t2(void) { return T2; }
114
115         void sync(void)
116         {
117 //              hermite_base<T>::operator[](0)=P1;
118 //              bezier<T>::operator[](1)=P1+T1/3;
119 //              bezier<T>::operator[](2)=P2-T2/3;
120 //              bezier<T>::operator[](3)=P2;
121
122                 hermite_base<T>::operator[](0)=P1;
123                 hermite_base<T>::operator[](1)=P2;
124                 hermite_base<T>::operator[](2)=T1;
125                 hermite_base<T>::operator[](3)=T2;
126
127                 hermite_base<T>::sync();
128         }
129
130 };
131
132 */
133
134 template <typename V,typename T=float>
135 class hermite : public bezier<V,T>
136 {
137 public:
138         typedef V value_type;
139         typedef T time_type;
140
141
142
143 public:
144         hermite() { }
145         hermite(const value_type &p1, const value_type &p2, const value_type &t1, const value_type &t2):
146         P1(p1),P2(p2),T1(t1),T2(t2) { sync(); }
147         hermite(const value_type &p1, const value_type &p2):
148         P1(p1),P2(p2),T1(p2-p1),T2(p2-p1) { sync(); }
149
150         value_type P1,P2,T1,T2;
151
152         value_type &p1() { return P1; }
153         value_type &p2() { return P2; }
154         value_type &t1() { return T1; }
155         value_type &t2() { return T2; }
156
157         void sync()
158         {
159                 bezier<V,T>::operator[](0)=P1;
160                 bezier<V,T>::operator[](1)=P1+T1/3;
161                 bezier<V,T>::operator[](2)=P2-T2/3;
162                 bezier<V,T>::operator[](3)=P2;
163
164                 bezier<V,T>::sync();
165         }
166 };
167
168 _ETL_END_NAMESPACE
169
170 /* === E X T E R N S ======================================================= */
171
172 /* === E N D =============================================================== */
173
174 #endif