BALL  1.5.0
angle.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_MATHS_ANGLE_H
6 #define BALL_MATHS_ANGLE_H
7 
8 #ifndef BALL_COMMON_EXCEPTION_H
9 # include <BALL/COMMON/exception.h>
10 #endif
11 
12 #ifndef BALL_COMMON_DEBUG_H
13 # include <BALL/COMMON/debug.h>
14 #endif
15 
16 #ifndef BALL_COMMON_CREATE_H
17 # include <BALL/COMMON/create.h>
18 #endif
19 
20 #ifndef BALL_MATHS_COMMON_H
21 # include <BALL/MATHS/common.h>
22 #endif
23 
24 namespace BALL
25 {
31  template <typename T>
32  class TAngle;
33 
34  template <typename T>
36  TAngle<T> operator * (const T& val, const TAngle<T>& angle);
37 
38  template <typename T>
40  TAngle<T> operator + (const T& val, const TAngle<T>& angle);
41 
42  template <typename T>
44  TAngle<T> operator - (const T& val, const TAngle<T>& angle);
45 
51  template <typename T>
52  class TAngle
53  {
54  public:
55 
57 
58 
61 
67  enum Range
68  {
69  // no limitations
71  // 0 <= angle <= 360, 0 <= angle <= (Constants::PI * 2)
73  // -180 <= angle <= 180, -Constants::PI <= angle <= Constants::PI
75  };
77 
80 
84  TAngle();
85 
91  TAngle(const TAngle& angle);
92 
100  explicit TAngle(const T& new_value, bool radian = true);
101 
104  virtual ~TAngle()
105  {
106  }
107 
111  virtual void clear()
112  {
113  value = (T)0;
114  }
116 
120 
123  void swap(TAngle& angle);
124 
131  void set(const T& new_value, bool radian = true);
132 
136  void set(const TAngle& angle);
137 
140  TAngle& operator = (const TAngle& angle);
141 
147  TAngle& operator = (const T& new_value);
148 
152  void get(TAngle& angle) const;
153 
158  void get(T& val, bool radian = true) const;
159 
161 
164 
168  operator T () const;
169 
173  T toRadian() const
174  ;
175 
180  static T toRadian(const T& degree);
181 
185  T toDegree() const;
186 
191  static T toDegree(const T& radian);
192 
199  void normalize(Range range);
200 
203  void negate();
204 
207  TAngle operator + () const;
208 
211  TAngle operator - () const;
212 
217  TAngle& operator += (const TAngle& angle);
218 
223  TAngle& operator += (const T& val);
224 
229  TAngle operator + (const TAngle& angle);
230 
235  TAngle& operator -= (const TAngle& angle);
236 
241  TAngle& operator -= (const T& val);
242 
247  TAngle operator - (const TAngle& angle);
248 
253  TAngle& operator *= (const TAngle& angle);
254 
259  TAngle& operator *= (const T& val);
260 
266  TAngle& operator /= (const TAngle& angle);
267 
273  TAngle& operator /= (const T& val);
274 
280  TAngle operator / (const TAngle& val);
281 
283 
286 
293  bool operator == (const TAngle& angle) const;
294 
301  bool operator != (const TAngle& angle) const;
302 
309  bool operator < (const TAngle& angle) const;
310 
317  bool operator < (const T& val) const;
318 
325  bool operator <= (const TAngle& angle) const;
326 
333  bool operator >= (const TAngle& angle) const;
334 
341  bool operator > (const TAngle& angle) const;
342 
349  bool isEquivalent(TAngle angle) const;
350 
352 
355 
360  bool isValid () const;
361 
368  void dump(std::ostream& s = std::cout, Size depth = 0) const;
369 
371 
374 
377  T value;
378 
380  };
382 
383  template <typename T>
385  : value((T)0)
386  {
387  }
388 
389  template <typename T>
390  TAngle<T>::TAngle(const TAngle& angle)
391  : value((T)angle.value)
392  {
393  }
394 
395  template <typename T>
396  TAngle<T>::TAngle(const T& new_value, bool radian)
397  : value(radian ? new_value : toRadian(new_value))
398  {
399  }
400 
401  template <typename T>
402  void TAngle<T>::swap(TAngle& angle)
403  {
404  T temp = value;
405  value = angle.value;
406  angle.value = temp;
407  }
408 
409  template <typename T>
410  void TAngle<T>::set(const TAngle& angle)
411  {
412  value = angle.value;
413  }
414 
415  template <typename T>
416  void TAngle<T>::set(const T& new_value, bool radian)
417  {
418  value = radian ? new_value : toRadian(new_value);
419  }
420 
421  template <typename T>
423  {
424  value = angle.value;
425  return *this;
426  }
427 
428  template <typename T>
429  TAngle<T>& TAngle<T>::operator = (const T& new_value)
430  {
431  value = new_value;
432  return *this;
433  }
434 
435  template <typename T>
436  void TAngle<T>::get(TAngle& angle) const
437  {
438  angle.value = value;
439  }
440 
441  template <typename T>
442  void TAngle<T>::get(T& val, bool radian) const
443  {
444  val = radian ? value : toDegree(value);
445  }
446 
447  template <typename T>
449  {
450  return value;
451  }
452 
453  template <typename T>
455  {
456  return value;
457  }
458 
459  template <typename T>
460  T TAngle<T>::toRadian(const T& degree) {
461  return Constants::PI / 180. * degree;
462  }
463 
464  template <typename T>
466  {
467  return toDegree(value);
468  }
469 
470  template <typename T>
471  T TAngle<T>::toDegree(const T& radian)
472  {
473  if (radian == (T) 0.0) return (T) 0.0;
474  return 180. / Constants::PI * radian;
475  }
476 
477  template <typename T>
479  {
480  if (range == RANGE__UNLIMITED)
481  {
482  return;
483  }
484 
485  long mod_factor = (long)(value / (2 * Constants::PI));
486  value -= mod_factor * (Constants::PI * 2);
487 
488  while (Maths::isGreater(value, (Constants::PI * 2)))
489  {
490  value -= (Constants::PI * 2);
491  }
492  while (Maths::isLess(value, -(Constants::PI * 2)))
493  {
494  value += (Constants::PI * 2);
495  }
496  if (range == RANGE__SIGNED) // invariant: -180 to 180:
497  {
498  if (Maths::isGreater(value, Constants::PI))
499  {
500  value -= (Constants::PI * 2);
501  }
502  }
503  else
504  { // invariant: 0 to 360:
505  if (Maths::isLess(value, 0))
506  {
507  value += (Constants::PI * 2);
508  }
509  }
510  }
511 
512  template <typename T>
514  {
515  value = -value;
516  }
517 
518  template <typename T>
520  {
521  return *this;
522  }
523 
524  template <typename T>
526  {
527  return TAngle(-value);
528  }
529 
530  template <typename T>
532  {
533  value += angle.value;
534  return *this;
535  }
536 
537  template <typename T>
539  {
540  value += val;
541  return *this;
542  }
543 
544  template <typename T>
546  {
547  return TAngle(value + angle.value);
548  }
549 
550  template <typename T>
552  {
553  value -= angle.value;
554  return *this;
555  }
556 
557  template <typename T>
559  {
560  value -= val;
561  return *this;
562  }
563 
564  template <typename T>
566  {
567  return TAngle(value - angle.value);
568  }
569 
570  template <typename T>
572  {
573  value *= angle.value;
574  return *this;
575  }
576 
577  template <typename T>
579  {
580  value *= val;
581  return *this;
582  }
583 
584  template <typename T>
586  {
587  if (angle.value == 0)
588  {
589  throw Exception::DivisionByZero(__FILE__, __LINE__);
590  }
591  value /= angle.value;
592  return *this;
593  }
594 
595 
596  template <typename T>
598  {
599  if (val == 0)
600  {
601  throw Exception::DivisionByZero(__FILE__, __LINE__);
602  }
603 
604  value /= val;
605  return *this;
606  }
607 
608 
609  template <typename T>
611  {
612  if (val.value == 0)
613  {
614  throw Exception::DivisionByZero(__FILE__, __LINE__);
615  }
616 
617  return TAngle(value / val.value);
618  }
619 
620  template <typename T>
621  bool TAngle<T>::operator == (const TAngle& angle) const
622  {
623  return Maths::isEqual(value, angle.value);
624  }
625 
626  template <typename T>
627  bool TAngle<T>::operator != (const TAngle& angle) const
628  {
629  return Maths::isNotEqual(value, angle.value);
630  }
631 
632  template <typename T>
633  bool TAngle<T>::operator < (const TAngle& angle) const
634  {
635  return Maths::isLess(value, angle.value);
636  }
637 
638  template <typename T>
639  bool TAngle<T>::operator < (const T& val) const
640  {
641  return Maths::isLess(value, val);
642  }
643 
644  template <typename T>
645  bool TAngle<T>::operator <= (const TAngle& angle) const
646  {
647  return Maths::isLessOrEqual(value, angle.value);
648  }
649 
650  template <typename T>
651  bool TAngle<T>::operator >= (const TAngle& angle) const
652  {
653  return Maths::isGreaterOrEqual(value, angle.value);
654  }
655 
656  template <typename T>
657  bool TAngle<T>::operator > (const TAngle& angle) const
658 
659  {
660  return Maths::isGreater(value, angle.value);
661  }
662 
663  template <typename T>
664  bool TAngle<T>::isEquivalent(TAngle angle) const
665  {
666  TAngle this_angle(*this);
667 
668  this_angle.normalize(RANGE__UNSIGNED);
669  angle.normalize(RANGE__UNSIGNED);
670 
671  return (this_angle == angle);
672  }
673 
674  template <typename T>
675  bool TAngle<T>::isValid() const
676  {
677  return true;
678  }
679 
680  template <typename T>
681  void TAngle<T>::dump(std::ostream& s, Size depth) const
682  {
684 
685  BALL_DUMP_HEADER(s, this, this);
686 
687  BALL_DUMP_DEPTH(s, depth);
688  s << " value: " << value << std::endl;
689 
691  }
692 
698 
702  template <typename T>
704  TAngle<T> operator * (const T& val, const TAngle<T>& angle)
705  {
706  return TAngle<T>(val * angle.value);
707  }
708 
712  template <typename T>
714  TAngle<T> operator + (const T& val, const TAngle<T>& angle)
715  {
716  return TAngle<T>(val + angle.value);
717  }
718 
722  template <typename T>
724  TAngle<T> operator - (const T& val, const TAngle<T>& angle)
725  {
726  return TAngle<T>(val - angle.value);
727  }
728 
732  template <typename T>
733  std::istream& operator >> (std::istream& s, TAngle<T>& angle)
734  {
735  char c;
736  s >> c >> angle.value >> c;
737  return s;
738  }
739 
745  template <typename T>
746  std::ostream& operator << (std::ostream& s, const TAngle<T>& angle)
747  {
748  s << '(' << angle.value << ')';
749 
750  return s;
751  }
752 
753 } // namespace BALL
754 
755 #endif // BALL_MATHS_ANGLE_H
BALL::TAngle::swap
void swap(TAngle &angle)
Definition: angle.h:402
BALL::TAngle::operator+=
TAngle & operator+=(const TAngle &angle)
Definition: angle.h:531
BALL_INLINE
#define BALL_INLINE
Definition: debug.h:15
BALL::TAngle
Definition: angle.h:32
BALL::TAngle::operator/
TAngle operator/(const TAngle &val)
Definition: angle.h:610
BALL::operator>>
std::istream & operator>>(std::istream &s, TAngle< T > &angle)
Definition: angle.h:733
BALL::TAngle::negate
void negate()
Definition: angle.h:513
BALL::Maths::isGreater
bool isGreater(const T1 &a, const T2 &b)
Definition: MATHS/common.h:273
BALL::operator-
BALL_INLINE TAngle< T > operator-(const T &val, const TAngle< T > &angle)
Definition: angle.h:724
BALL::Maths::isNotEqual
bool isNotEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:224
BALL::Constants::PI
const BALL_EXTERN_VARIABLE double PI
PI.
Definition: constants.h:35
BALL::TAngle::toRadian
T toRadian() const
Definition: angle.h:454
BALL::TAngle::RANGE__UNSIGNED
@ RANGE__UNSIGNED
Definition: angle.h:72
BALL::operator!=
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
BALL::TAngle< float >::Range
Range
Definition: angle.h:67
BALL::TAngle::RANGE__SIGNED
@ RANGE__SIGNED
Definition: angle.h:74
BALL_DUMP_HEADER
#define BALL_DUMP_HEADER(os, cl, ob)
Definition: macros.h:393
BALL::Maths::isGreaterOrEqual
bool isGreaterOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:261
BALL::TAngle::clear
virtual void clear()
Definition: angle.h:111
BALL::TAngle::operator<
bool operator<(const TAngle &angle) const
Definition: angle.h:633
create.h
BALL::operator<
BALL_EXPORT bool operator<(const String &s1, const String &s2)
BALL::TAngle::operator+
TAngle operator+() const
Definition: angle.h:519
BALL_DUMP_STREAM_PREFIX
#define BALL_DUMP_STREAM_PREFIX(os)
Definition: macros.h:391
BALL::Maths::isEqual
bool isEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:212
BALL::operator>
BALL_EXPORT bool operator>(const String &s1, const String &s2)
BALL::operator*
BALL_INLINE TAngle< T > operator*(const T &val, const TAngle< T > &angle)
Definition: angle.h:704
BALL::operator>=
BALL_EXPORT bool operator>=(const String &s1, const String &s2)
BALL
Definition: constants.h:12
BALL::operator+
BALL_INLINE TAngle< T > operator+(const T &val, const TAngle< T > &angle)
Definition: angle.h:714
BALL::TAngle::operator*=
TAngle & operator*=(const TAngle &angle)
Definition: angle.h:571
BALL::TAngle::TAngle
TAngle()
Definition: angle.h:384
BALL::Angle
TAngle< float > Angle
Definition: angle.h:697
BALL::Exception::DivisionByZero
Definition: COMMON/exception.h:340
BALL_DUMP_DEPTH
#define BALL_DUMP_DEPTH(os, depth)
Definition: macros.h:390
BALL_SIZE_TYPE
BALL::TAngle::get
void get(TAngle &angle) const
Definition: angle.h:436
BALL::Maths::isLessOrEqual
bool isLessOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:249
BALL::TAngle::toDegree
T toDegree() const
Definition: angle.h:465
BALL::TAngle::isEquivalent
bool isEquivalent(TAngle angle) const
Definition: angle.h:664
BALL::TAngle::operator/=
TAngle & operator/=(const TAngle &angle)
Definition: angle.h:585
BALL::Constants::c
const BALL_EXTERN_VARIABLE double c
Definition: constants.h:149
BALL::TAngle::operator!=
bool operator!=(const TAngle &angle) const
Definition: angle.h:627
common.h
BALL::TAngle::operator>=
bool operator>=(const TAngle &angle) const
Definition: angle.h:651
BALL::TAngle::operator<=
bool operator<=(const TAngle &angle) const
Definition: angle.h:645
BALL::TAngle::set
void set(const T &new_value, bool radian=true)
Definition: angle.h:416
BALL::TAngle::operator-=
TAngle & operator-=(const TAngle &angle)
Definition: angle.h:551
BALL::operator<<
std::ostream & operator<<(std::ostream &s, const TAngle< T > &angle)
Definition: angle.h:746
BALL::TAngle::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
Definition: angle.h:681
BALL::Maths::isLess
bool isLess(const T1 &a, const T2 &b)
Definition: MATHS/common.h:236
debug.h
BALL::TAngle::operator>
bool operator>(const TAngle &angle) const
Definition: angle.h:657
BALL::operator==
BALL_EXPORT bool operator==(const String &s1, const String &s2)
BALL::TAngle::RANGE__UNLIMITED
@ RANGE__UNLIMITED
Definition: angle.h:70
BALL::TAngle::value
T value
Definition: angle.h:377
BALL::TAngle::~TAngle
virtual ~TAngle()
Definition: angle.h:104
BALL::TAngle::operator==
bool operator==(const TAngle &angle) const
Definition: angle.h:621
BALL_DUMP_STREAM_SUFFIX
#define BALL_DUMP_STREAM_SUFFIX(os)
Definition: macros.h:395
BALL::TAngle::isValid
bool isValid() const
Definition: angle.h:675
BALL::operator+
ConstRandomAccessIterator< Container, DataType, Position, Traits > operator+(Distance distance, const ConstRandomAccessIterator< Container, DataType, Position, Traits > &iterator)
Definition: randomAccessIterator.h:192
BALL_CREATE
#define BALL_CREATE(name)
Definition: create.h:62
BALL::operator<=
BALL_EXPORT bool operator<=(const String &s1, const String &s2)
BALL::TAngle::normalize
void normalize(Range range)
Definition: angle.h:478
BALL::TAngle::operator-
TAngle operator-() const
Definition: angle.h:525
exception.h
BALL::TAngle::operator=
TAngle & operator=(const TAngle &angle)
Definition: angle.h:422