34bc9f515c496c92cb7bffd399f81d065831c356
[synfig.git] / ETL / trunk / ETL / _angle.h
1 #include <stdio.h>
2 /* ========================================================================
3 ** Extended Template and Library
4 ** Angle Abstraction Class Implementation
5 ** $Id$
6 **
7 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 **
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
13 **
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ** General Public License for more details.
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** This is an internal header file, included by other ETL headers.
22 ** You should not attempt to use it directly.
23 **
24 ** ========================================================================= */
25
26 /* === S T A R T =========================================================== */
27
28 #ifndef __ETL_ANGLE_H
29 #define __ETL_ANGLE_H
30
31 /* === H E A D E R S ======================================================= */
32
33 #include <cmath>
34 #include <functional>
35
36 /* === M A C R O S ========================================================= */
37
38 #ifndef PI
39 # define PI (3.1415926535897932384626433832795029L)
40 # define HALF_PI (PI/2)
41 #endif
42
43 /* === T Y P E D E F S ===================================================== */
44
45 /* === C L A S S E S & S T R U C T S ======================================= */
46
47 _ETL_BEGIN_NAMESPACE
48
49 // ========================================================================
50 /*!     \class  angle   _angle.h        ETL/angle
51 **      \brief  Abstraction of the concept of an angle
52 **  \see angle::deg, angle::rad, angle::rot, angle::sin, angle::cos, angle::tan, fastangle
53 **      \writeme
54 */
55 class angle
56 {
57 public:
58         typedef float value_type;
59
60 protected:
61         typedef value_type unit;
62
63         unit v; //! Stored in radians
64
65 public:
66
67         /*
68         ** Arithmetic Operators
69         */
70
71         const angle     &
72         operator+=(const angle &rhs)
73                 { v+=rhs.v; return *this; }
74
75         const angle     &
76         operator-=(const angle &rhs)
77                 { v-=rhs.v; return *this; }
78
79         const angle     &
80         operator*=(const unit &rhs)
81                 { v*=rhs; return *this; }
82
83         const angle     &
84         operator/=(const unit &rhs)
85                 { v/=rhs; return *this; }
86
87         //! Angle Addition Operator
88         angle
89         operator+(const angle &rhs)const
90                 { return angle(*this)+=rhs; }
91
92         //! Angle Subtraction Operator
93         /*! \sa angle dist(const angle &) */
94         angle
95         operator-(const angle &rhs)const
96                 { return angle(*this)-=rhs; }
97
98         //! Angle Scalar Multiplication Operator
99         /*! This operator will multiply the given
100                 angle by the given scalar value. */
101         angle
102         operator*(const unit &rhs)const
103                 { return angle(*this)*=rhs; }
104
105         angle
106         operator/(const unit &rhs)const
107                 { return angle(*this)/=rhs; }
108
109         //! Angle Negation
110         angle
111         operator-()const
112         {
113                 angle ret;
114                 ret.v=-v;
115                 return ret;
116         }
117
118         //! 180 degree rotation operator
119         /*! Returns the angle directly opposite of
120                 the given angle, and will yield a result
121                 between 0 and 2PI */
122         angle
123         operator~()const
124         {
125                 angle ret;
126                 ret.v = v+PI;
127                 return ret.mod();
128         }
129
130         /*! Returns true if the shortest
131                 angle between the left-hand and
132                 right-hand side is clockwise */
133         bool
134         operator<(const angle &rhs)const
135                 { return dist(rhs).v<(value_type)0.0; }
136
137         /*! Returns true if the shortest
138                 angle between the left-hand and
139                 right-hand side is counter-clockwise */
140         bool
141         operator>(const angle &rhs)const
142                 { return dist(rhs).v>(value_type)0.0; }
143
144         /*! Returns true if the shortest
145                 angle between the left-hand and
146                 right-hand side is clockwise,
147                 or if the angles are refer to the same
148                 point on the unit circle. */
149         bool
150         operator<=(const angle &rhs)const
151                 { return dist(rhs).v<=(value_type)0.0; }
152
153         /*! Returns true if the shortest
154                 angle between the left-hand and
155                 right-hand side is counter-clockwise,
156                 or if the angles are refer to the same
157                 point on the unit circle. */
158         bool
159         operator>=(const angle &rhs)const
160                 { return dist(rhs).v>=(value_type)0.0; }
161
162         /*! Returns true if the angles
163                 are refer to the same point
164                 on the unit circle. */
165         bool
166         operator==(const angle &rhs)const
167                 { return dist(rhs).v==(value_type)0.0; }
168
169         /*! Returns false if the angles
170                 are refer to the same point
171                 on the unit circle. */
172         bool
173         operator!=(const angle &rhs)const
174                 { return dist(rhs).v!=(value_type)0.0; }
175
176         //! Angle Difference Function
177         /*! This function will return the
178                 shortest physical distance between
179                 two angles, from -PI/2 to PI/2
180                 \sa angle operator-(const angle &) */
181         angle
182         dist(const angle &rhs)const
183         {
184                 angle ret;
185
186                 ret.v=v-rhs.v;
187
188                 ret.v-=rot_floor(ret.v+PI);
189
190                 return ret;
191         }
192
193         //! Rotation Modulus
194         /*! This function will return the
195                 value of the angle between 0 and 2PI */
196         angle
197         mod()const
198         {
199                 angle ret(*this);
200                 ret.v-=rot_floor(ret.v);
201                 return ret;
202         }
203
204         static angle
205         zero()
206         {
207                 angle ret;
208                 ret.v=0;
209                 return ret;
210         }
211
212         static angle
213         one()
214         {
215                 angle ret;
216                 ret.v=PI;
217                 return ret;
218         }
219
220         static angle
221         half()
222         {
223                 angle ret;
224                 ret.v=PI*0.5;
225                 return ret;
226         }
227
228         bool operator!()const { return v==0; }
229
230 private:
231
232         static value_type rot_floor(value_type x)
233         { return static_cast<value_type>(std::floor(x/(PI*2))*PI*2); }
234
235 public:
236         /*
237         ** Conversion Classes
238         */
239
240         class rad;
241         class deg;
242         class rot;
243
244         /*
245         ** Trigonometric Classes
246         */
247
248         class sin;
249         class cos;
250         class tan;
251
252         /*
253         ** Friend classes
254         */
255
256         friend class rad;
257         friend class deg;
258         friend class rot;
259         friend class sin;
260         friend class cos;
261         friend class tan;
262
263         /*
264         ** Deprecated
265         */
266
267 #ifndef ETL_NO_DEPRECATED
268         typedef rad             radians;
269         typedef deg             degrees;
270         typedef rot             rotations;
271 #endif
272 }; // END of class angle
273
274 // ========================================================================
275 /*!     \class  angle::rad      _angle.h        ETL/angle
276 **      \brief  Angle representation in radians
277 **      \see angle
278 **      \writeme
279 */
280 class angle::rad : public angle
281 {
282 public:
283         explicit rad(const value_type &x) { v=x; }
284         rad(const angle &a):angle(a) { }
285         rad     mod()const { return angle::mod(); }
286         rad dist(const angle &rhs)const { return angle::dist(rhs); }
287 #ifndef ETL_NO_DEPRECATED
288         operator value_type()const ETL_DEPRECATED_FUNCTION;
289 #endif
290         value_type get()const { return v; }
291 }; // END of class angle::radians
292 inline angle::rad::operator angle::value_type()const { return get(); }
293
294 // ========================================================================
295 /*!     \class  angle::deg      _angle.h        ETL/angle
296 **      \brief  Angle representation in degrees
297 **      \see angle
298 **      \writeme
299 */
300 class angle::deg : public angle
301 {
302 public:
303         explicit deg(const value_type &x) { v=x*((PI*2)/360); }
304         deg(const angle &a):angle(a) { }
305         deg     mod()const { return angle::mod(); }
306         deg dist(const angle &rhs)const { return angle::dist(rhs); }
307         value_type get()const { return v*360/(PI*2); }
308 #ifndef ETL_NO_DEPRECATED
309         operator value_type()const ETL_DEPRECATED_FUNCTION;
310 #endif
311 }; // END of class angle::degrees
312 inline angle::deg::operator angle::value_type()const { return get(); }
313
314 // ========================================================================
315 /*!     \class  angle::rot      _angle.h        ETL/angle
316 **      \brief  Angle representation in rotations
317 **      \see angle
318 **      \writeme
319 */
320 class angle::rot : public angle
321 {
322 public:
323         explicit rot(const value_type &x) { v=x*(PI*2); }
324         rot(const angle &a):angle(a) { }
325         rot mod()const { return angle::mod(); }
326         rot dist(const angle &rhs)const { return angle::dist(rhs); }
327         value_type get()const { return v/(PI*2); }
328 #ifndef ETL_NO_DEPRECATED
329         operator value_type()const ETL_DEPRECATED_FUNCTION;
330 #endif
331 }; // END of class angle::rotations
332 inline angle::rot::operator angle::value_type()const { return get(); }
333
334 // ========================================================================
335 /*!     \class  angle::sin      _angle.h        ETL/angle
336 **      \brief  Angle representation as a sine function
337 **      \see angle
338 **      \writeme
339 */
340 class angle::sin : public angle
341 {
342 public:
343         explicit sin(const value_type &x) { v=static_cast<value_type>(std::asin(x)); }
344         sin(const angle &a):angle(a) { }
345         sin     mod()const { return angle::mod(); }
346         sin dist(const angle &rhs)const { return angle::dist(rhs); }
347         value_type get()const { return static_cast<value_type>(std::sin(v)); }
348 #ifndef ETL_NO_DEPRECATED
349         operator value_type()const ETL_DEPRECATED_FUNCTION;
350 #endif
351 }; // END of class angle::sin
352 inline angle::sin::operator angle::value_type()const { return get(); }
353
354 // ========================================================================
355 /*!     \class  angle::cos      _angle.h        ETL/angle
356 **      \brief  Angle representation as a cosine function
357 **      \see angle
358 **      \writeme
359 */
360 class angle::cos : public angle
361 {
362 public:
363         explicit cos(const value_type &x)       { v=(value_type)(std::acos(x)); }
364         cos(const angle &a):angle(a) { }
365         cos     mod()const { return angle::mod(); }
366         cos dist(const angle &rhs)const { return angle::dist(rhs); }
367         operator value_type()const ETL_DEPRECATED_FUNCTION;
368 #ifndef ETL_NO_DEPRECATED
369         value_type get()const { return (value_type)std::cos(v); }
370 #endif
371 }; // END of class angle::cos
372 inline angle::cos::operator angle::value_type()const { return get(); }
373
374 // ========================================================================
375 /*!     \class  angle::tan      _angle.h        ETL/angle
376 **      \brief  Angle representation as a tangent function
377 **      \see angle
378 **      \writeme
379 */
380 class angle::tan : public angle
381 {
382 public:
383         explicit tan(const value_type &x)       { v=(value_type)(std::atan(x)); }
384         tan(const value_type &y,const value_type &x) { v=(value_type)(std::atan2(y,x)); }
385         tan(const angle &a):angle(a) { }
386         tan     mod()const { return angle::mod(); }
387         tan dist(const angle &rhs)const { return angle::dist(rhs); }
388 #ifndef ETL_NO_DEPRECATED
389         operator value_type()const ETL_DEPRECATED_FUNCTION;
390 #endif
391         value_type get()const { return (value_type)std::tan(v); }
392 }; // END of class angle::tan
393 inline angle::tan::operator angle::value_type()const { return get(); }
394
395 _ETL_END_NAMESPACE
396
397 //#include <iostream>
398
399 template <typename T>
400 struct affine_combo<etl::angle, T>
401 {
402         typedef T time_type;
403
404         //affine_combo() { std::cerr<<"affine_combo<etl::angle,float>: I was created!"<<std::endl; }
405         //~affine_combo() { std::cerr<<"affine_combo<etl::angle,float>: I was DELETED!"<<std::endl; }
406
407         etl::angle operator()(const etl::angle &a,const etl::angle &b,const time_type &t)const
408         {
409                 return b.dist(a)*(float)t+a;
410         }
411
412         etl::angle reverse(const etl::angle &x, const etl::angle &b, const time_type &t)const
413         {
414                 return x.dist(b*(float)t)*(float)(time_type(1)/(time_type(1)-t));
415         }
416 };
417
418 template <>
419 struct distance_func<etl::angle> : public std::binary_function<etl::angle, etl::angle, etl::angle>
420 {
421         etl::angle operator()(const etl::angle &a,const etl::angle &b)const
422         {
423                 etl::angle delta=b.dist(a);
424                 //if(delta<etl::angle::zero())
425                 //      return delta+etl::angle::one();
426                 return delta;
427         }
428
429         etl::angle cook(const etl::angle &x)const { return x; }
430         etl::angle uncook(const etl::angle &x)const { return x; }
431 };
432
433
434 /* === E N D =============================================================== */
435
436 #endif