1 /* ========================================================================
2 ** Extended Template and Library
3 ** Angle Abstraction Class Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
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.
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.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
30 /* === H E A D E R S ======================================================= */
35 /* === M A C R O S ========================================================= */
38 # define PI (3.1415926535897932384626433832795029L)
39 # define HALF_PI (PI/2)
42 /* === T Y P E D E F S ===================================================== */
44 /* === C L A S S E S & S T R U C T S ======================================= */
48 // ========================================================================
49 /*! \class angle _angle.h ETL/angle
50 ** \brief Abstraction of the concept of an angle
51 ** \see angle::deg, angle::rad, angle::rot, angle::sin, angle::cos, angle::tan, fastangle
57 typedef float value_type;
60 typedef value_type unit;
62 unit v; //! Stored in rotations
67 ** Arithmetic Operators
71 operator+=(const angle &rhs)
72 { v+=rhs.v; return *this; }
75 operator-=(const angle &rhs)
76 { v-=rhs.v; return *this; }
79 operator*=(const unit &rhs)
80 { v*=rhs; return *this; }
83 operator/=(const unit &rhs)
84 { v/=rhs; return *this; }
86 //! Angle Addition Operator
88 operator+(const angle &rhs)const
89 { return angle(*this)+=rhs; }
91 //! Angle Subtraction Operator
92 /*! \sa angle dist(const angle &) */
94 operator-(const angle &rhs)const
95 { return angle(*this)-=rhs; }
97 //! Angle Scalar Multiplication Operator
98 /*! This operator will multiply the given
99 angle by the given scalar value. */
101 operator*(const unit &rhs)const
102 { return angle(*this)*=rhs; }
105 operator/(const unit &rhs)const
106 { return angle(*this)/=rhs; }
117 //! 180 degree rotation operator
118 /*! Returns the angle directly opposite of
119 the given angle, and will yield a result
125 ret.v=(value_type)std::floor(v+0.5f);
129 /*! Returns true if the shortest
130 angle between the left-hand and
131 right-hand side is clockwise */
133 operator<(const angle &rhs)const
135 // { return dist(rhs).v<(value_type)0.0; }
137 /*! Returns true if the shortest
138 angle between the left-hand and
139 right-hand side is counter-clockwise */
141 operator>(const angle &rhs)const
143 // { return dist(rhs).v>(value_type)0.0; }
145 /*! Returns true if the shortest
146 angle between the left-hand and
147 right-hand side is clockwise,
148 or if the angles are refer to the same
149 point on the unit circle. */
151 operator<=(const angle &rhs)const
153 // { return dist(rhs).v<=(value_type)0.0; }
155 /*! Returns true if the shortest
156 angle between the left-hand and
157 right-hand side is counter-clockwise,
158 or if the angles are refer to the same
159 point on the unit circle. */
161 operator>=(const angle &rhs)const
163 // { return dist(rhs).v>=(value_type)0.0; }
165 /*! Returns true if the angles
166 are refer to the same point
167 on the unit circle. */
169 operator==(const angle &rhs)const
171 // { return dist(rhs).v==(value_type)0.0; }
173 /*! Returns false if the angles
174 are refer to the same point
175 on the unit circle. */
177 operator!=(const angle &rhs)const
179 // { return dist(rhs).v!=(value_type)0.0; }
181 //! Angle Difference Function
182 /*! This function will return the
183 shortest physical distance between
184 two angles, from -PI/2 to PI/2
185 \sa angle operator-(const angle &) */
187 dist(const angle &rhs)const
193 ret.v-=rot_floor(ret.v+PI);
199 /*! This function will return the
200 value of the angle between 0 and 2PI */
205 ret.v-=rot_floor(ret.v);
233 bool operator!()const { return v==0; }
237 static value_type rot_floor(value_type x)
238 { return static_cast<value_type>(std::floor(x/(PI*2))*PI*2); }
242 ** Converstion Classes
250 ** Trigometric Classes
272 #ifndef ETL_NO_DEPRECATED
275 typedef rot rotations;
277 }; // END of class angle
279 // ========================================================================
280 /*! \class angle::rad _angle.h ETL/angle
281 ** \brief Angle representation in radians
285 class angle::rad : public angle
288 explicit rad(const value_type &x) { v=x; }
289 rad(const angle &a):angle(a) { }
290 rad mod()const { return angle::mod(); }
291 rad dist(const angle &rhs)const { return angle::dist(rhs); }
292 #ifndef ETL_NO_DEPRECATED
293 operator value_type()const ETL_DEPRECATED_FUNCTION;
295 value_type get()const { return v; }
296 }; // END of class angle::radians
297 inline angle::rad::operator angle::value_type()const { return get(); }
299 // ========================================================================
300 /*! \class angle::deg _angle.h ETL/angle
301 ** \brief Angle representation in degrees
305 class angle::deg : public angle
308 explicit deg(const value_type &x) { v=x*((PI*2)/360); }
309 deg(const angle &a):angle(a) { }
310 deg mod()const { return angle::mod(); }
311 deg dist(const angle &rhs)const { return angle::dist(rhs); }
312 value_type get()const { return v*360/(PI*2); }
313 #ifndef ETL_NO_DEPRECATED
314 operator value_type()const ETL_DEPRECATED_FUNCTION;
316 }; // END of class angle::degrees
317 inline angle::deg::operator angle::value_type()const { return get(); }
319 // ========================================================================
320 /*! \class angle::rot _angle.h ETL/angle
321 ** \brief Angle representation in rotations
325 class angle::rot : public angle
328 explicit rot(const value_type &x) { v=x*(PI*2); }
329 rot(const angle &a):angle(a) { }
330 rot mod()const { return angle::mod(); }
331 rot dist(const angle &rhs)const { return angle::dist(rhs); }
332 value_type get()const { return v/(PI*2); }
333 #ifndef ETL_NO_DEPRECATED
334 operator value_type()const ETL_DEPRECATED_FUNCTION;
336 }; // END of class angle::rotations
337 inline angle::rot::operator angle::value_type()const { return get(); }
339 // ========================================================================
340 /*! \class angle::sin _angle.h ETL/angle
341 ** \brief Angle representation as a sine function
345 class angle::sin : public angle
348 explicit sin(const value_type &x) { v=static_cast<value_type>(std::asin(x)); }
349 sin(const angle &a):angle(a) { }
350 sin mod()const { return angle::mod(); }
351 sin dist(const angle &rhs)const { return angle::dist(rhs); }
352 value_type get()const { return static_cast<value_type>(std::sin(v)); }
353 #ifndef ETL_NO_DEPRECATED
354 operator value_type()const ETL_DEPRECATED_FUNCTION;
356 }; // END of class angle::sin
357 inline angle::sin::operator angle::value_type()const { return get(); }
359 // ========================================================================
360 /*! \class angle::cos _angle.h ETL/angle
361 ** \brief Angle representation as a cosine function
365 class angle::cos : public angle
368 explicit cos(const value_type &x) { v=(value_type)(std::acos(x)); }
369 cos(const angle &a):angle(a) { }
370 cos mod()const { return angle::mod(); }
371 cos dist(const angle &rhs)const { return angle::dist(rhs); }
372 operator value_type()const ETL_DEPRECATED_FUNCTION;
373 #ifndef ETL_NO_DEPRECATED
374 value_type get()const { return (value_type)std::cos(v); }
376 }; // END of class angle::cos
377 inline angle::cos::operator angle::value_type()const { return get(); }
379 // ========================================================================
380 /*! \class angle::tan _angle.h ETL/angle
381 ** \brief Angle representation as a tangent function
385 class angle::tan : public angle
388 explicit tan(const value_type &x) { v=(value_type)(std::atan(x)); }
389 tan(const value_type &y,const value_type &x) { v=(value_type)(std::atan2(y,x)); }
390 tan(const angle &a):angle(a) { }
391 tan mod()const { return angle::mod(); }
392 tan dist(const angle &rhs)const { return angle::dist(rhs); }
393 #ifndef ETL_NO_DEPRECATED
394 operator value_type()const ETL_DEPRECATED_FUNCTION;
396 value_type get()const { return (value_type)std::tan(v); }
397 }; // END of class angle::tan
398 inline angle::tan::operator angle::value_type()const { return get(); }
402 //#include <iostream>
404 template <typename T>
405 struct affine_combo<etl::angle, T>
409 //affine_combo() { std::cerr<<"affine_combo<etl::angle,float>: I was created!"<<std::endl; }
410 //~affine_combo() { std::cerr<<"affine_combo<etl::angle,float>: I was DELETED!"<<std::endl; }
412 etl::angle operator()(const etl::angle &a,const etl::angle &b,const time_type &t)const
414 return b.dist(a)*(float)t+a;
417 etl::angle reverse(const etl::angle &x, const etl::angle &b, const time_type &t)const
419 return x.dist(b*(float)t)*(float)(time_type(1)/(time_type(1)-t));
424 struct distance_func<etl::angle> : public std::binary_function<etl::angle, etl::angle, etl::angle>
426 etl::angle operator()(const etl::angle &a,const etl::angle &b)const
428 etl::angle delta=b.dist(a);
429 //if(delta<etl::angle::zero())
430 // return delta+etl::angle::one();
434 etl::angle cook(const etl::angle &x)const { return x; }
435 etl::angle uncook(const etl::angle &x)const { return x; }
439 /* === E N D =============================================================== */