Loading...
Searching...
No Matches
angle.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_ANGLE_H
#define BALL_MATHS_ANGLE_H
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_COMMON_DEBUG_H
# include <BALL/COMMON/debug.h>
#endif
#ifndef BALL_COMMON_CREATE_H
# include <BALL/COMMON/create.h>
#endif
#ifndef BALL_MATHS_COMMON_H
# include <BALL/MATHS/common.h>
#endif
namespace BALL
{
template <typename T>
class TAngle;
template <typename T>
BALL_INLINE
TAngle<T> operator * (const T& val, const TAngle<T>& angle);
template <typename T>
BALL_INLINE
TAngle<T> operator + (const T& val, const TAngle<T>& angle);
template <typename T>
BALL_INLINE
TAngle<T> operator - (const T& val, const TAngle<T>& angle);
template <typename T>
class TAngle
{
public:
BALL_CREATE(TAngle<T>)
enum Range
{
// no limitations
RANGE__UNLIMITED = 0,
// 0 <= angle <= 360, 0 <= angle <= (Constants::PI * 2)
RANGE__UNSIGNED = 1,
// -180 <= angle <= 180, -Constants::PI <= angle <= Constants::PI
RANGE__SIGNED = 2
};
TAngle();
TAngle(const TAngle& angle);
explicit TAngle(const T& new_value, bool radian = true);
virtual ~TAngle()
{
}
virtual void clear()
{
value = (T)0;
}
void swap(TAngle& angle);
void set(const T& new_value, bool radian = true);
void set(const TAngle& angle);
TAngle& operator = (const TAngle& angle);
TAngle& operator = (const T& new_value);
void get(TAngle& angle) const;
void get(T& val, bool radian = true) const;
operator T () const;
T toRadian() const
;
static T toRadian(const T& degree);
T toDegree() const;
static T toDegree(const T& radian);
void normalize(Range range);
void negate();
TAngle operator + () const;
TAngle operator - () const;
TAngle& operator += (const TAngle& angle);
TAngle& operator += (const T& val);
TAngle operator + (const TAngle& angle);
TAngle& operator -= (const TAngle& angle);
TAngle& operator -= (const T& val);
TAngle operator - (const TAngle& angle);
TAngle& operator *= (const TAngle& angle);
TAngle& operator *= (const T& val);
TAngle& operator /= (const TAngle& angle);
TAngle& operator /= (const T& val);
TAngle operator / (const TAngle& val);
bool operator == (const TAngle& angle) const;
bool operator != (const TAngle& angle) const;
bool operator < (const TAngle& angle) const;
bool operator < (const T& val) const;
bool operator <= (const TAngle& angle) const;
bool operator >= (const TAngle& angle) const;
bool operator > (const TAngle& angle) const;
bool isEquivalent(TAngle angle) const;
bool isValid () const;
void dump(std::ostream& s = std::cout, Size depth = 0) const;
T value;
};
template <typename T>
TAngle<T>::TAngle()
: value((T)0)
{
}
template <typename T>
TAngle<T>::TAngle(const TAngle& angle)
: value((T)angle.value)
{
}
template <typename T>
TAngle<T>::TAngle(const T& new_value, bool radian)
: value(radian ? new_value : toRadian(new_value))
{
}
template <typename T>
void TAngle<T>::swap(TAngle& angle)
{
T temp = value;
value = angle.value;
angle.value = temp;
}
template <typename T>
void TAngle<T>::set(const TAngle& angle)
{
value = angle.value;
}
template <typename T>
void TAngle<T>::set(const T& new_value, bool radian)
{
value = radian ? new_value : toRadian(new_value);
}
template <typename T>
TAngle<T>& TAngle<T>::operator = (const TAngle& angle)
{
value = angle.value;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator = (const T& new_value)
{
value = new_value;
return *this;
}
template <typename T>
void TAngle<T>::get(TAngle& angle) const
{
angle.value = value;
}
template <typename T>
void TAngle<T>::get(T& val, bool radian) const
{
val = radian ? value : toDegree(value);
}
template <typename T>
TAngle<T>::operator T () const
{
return value;
}
template <typename T>
T TAngle<T>::toRadian() const
{
return value;
}
template <typename T>
T TAngle<T>::toRadian(const T& degree) {
return Constants::PI / 180. * degree;
}
template <typename T>
T TAngle<T>::toDegree() const
{
return toDegree(value);
}
template <typename T>
T TAngle<T>::toDegree(const T& radian)
{
if (radian == (T) 0.0) return (T) 0.0;
return 180. / Constants::PI * radian;
}
template <typename T>
void TAngle<T>::normalize(Range range)
{
if (range == RANGE__UNLIMITED)
{
return;
}
long mod_factor = (long)(value / (2 * Constants::PI));
value -= mod_factor * (Constants::PI * 2);
while (Maths::isGreater(value, (Constants::PI * 2)))
{
value -= (Constants::PI * 2);
}
while (Maths::isLess(value, -(Constants::PI * 2)))
{
value += (Constants::PI * 2);
}
if (range == RANGE__SIGNED) // invariant: -180 to 180:
{
if (Maths::isGreater(value, Constants::PI))
{
value -= (Constants::PI * 2);
}
}
else
{ // invariant: 0 to 360:
if (Maths::isLess(value, 0))
{
value += (Constants::PI * 2);
}
}
}
template <typename T>
void TAngle<T>::negate()
{
value = -value;
}
template <typename T>
TAngle<T> TAngle<T>::operator + () const
{
return *this;
}
template <typename T>
TAngle<T> TAngle<T>::operator - () const
{
return TAngle(-value);
}
template <typename T>
TAngle<T>& TAngle<T>::operator += (const TAngle& angle)
{
value += angle.value;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator += (const T& val)
{
value += val;
return *this;
}
template <typename T>
TAngle<T> TAngle<T>::operator + (const TAngle& angle)
{
return TAngle(value + angle.value);
}
template <typename T>
TAngle<T>& TAngle<T>::operator -= (const TAngle& angle)
{
value -= angle.value;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator -= (const T& val)
{
value -= val;
return *this;
}
template <typename T>
TAngle<T> TAngle<T>::operator - (const TAngle& angle)
{
return TAngle(value - angle.value);
}
template <typename T>
TAngle<T>& TAngle<T>::operator *= (const TAngle& angle)
{
value *= angle.value;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator *= (const T& val)
{
value *= val;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator /= (const TAngle& angle)
{
if (angle.value == 0)
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
value /= angle.value;
return *this;
}
template <typename T>
TAngle<T>& TAngle<T>::operator /= (const T& val)
{
if (val == 0)
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
value /= val;
return *this;
}
template <typename T>
TAngle<T> TAngle<T>::operator / (const TAngle<T>& val)
{
if (val.value == 0)
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
return TAngle(value / val.value);
}
template <typename T>
bool TAngle<T>::operator == (const TAngle& angle) const
{
return Maths::isEqual(value, angle.value);
}
template <typename T>
bool TAngle<T>::operator != (const TAngle& angle) const
{
return Maths::isNotEqual(value, angle.value);
}
template <typename T>
bool TAngle<T>::operator < (const TAngle& angle) const
{
return Maths::isLess(value, angle.value);
}
template <typename T>
bool TAngle<T>::operator < (const T& val) const
{
return Maths::isLess(value, val);
}
template <typename T>
bool TAngle<T>::operator <= (const TAngle& angle) const
{
return Maths::isLessOrEqual(value, angle.value);
}
template <typename T>
bool TAngle<T>::operator >= (const TAngle& angle) const
{
return Maths::isGreaterOrEqual(value, angle.value);
}
template <typename T>
bool TAngle<T>::operator > (const TAngle& angle) const
{
return Maths::isGreater(value, angle.value);
}
template <typename T>
bool TAngle<T>::isEquivalent(TAngle angle) const
{
TAngle this_angle(*this);
this_angle.normalize(RANGE__UNSIGNED);
angle.normalize(RANGE__UNSIGNED);
return (this_angle == angle);
}
template <typename T>
bool TAngle<T>::isValid() const
{
return true;
}
template <typename T>
void TAngle<T>::dump(std::ostream& s, Size depth) const
{
BALL_DUMP_STREAM_PREFIX(s);
BALL_DUMP_HEADER(s, this, this);
BALL_DUMP_DEPTH(s, depth);
s << " value: " << value << std::endl;
BALL_DUMP_STREAM_SUFFIX(s);
}
typedef TAngle<float> Angle;
template <typename T>
BALL_INLINE
TAngle<T> operator * (const T& val, const TAngle<T>& angle)
{
return TAngle<T>(val * angle.value);
}
template <typename T>
BALL_INLINE
TAngle<T> operator + (const T& val, const TAngle<T>& angle)
{
return TAngle<T>(val + angle.value);
}
template <typename T>
BALL_INLINE
TAngle<T> operator - (const T& val, const TAngle<T>& angle)
{
return TAngle<T>(val - angle.value);
}
template <typename T>
std::istream& operator >> (std::istream& s, TAngle<T>& angle)
{
char c;
s >> c >> angle.value >> c;
return s;
}
template <typename T>
std::ostream& operator << (std::ostream& s, const TAngle<T>& angle)
{
s << '(' << angle.value << ')';
return s;
}
} // namespace BALL
#endif // BALL_MATHS_ANGLE_H
exception.h
common.h
BALL::Exception::DivisionByZero
Definition COMMON/exception.h:342
BALL::TAngle
Definition angle.h:53
BALL::TAngle::operator>
bool operator>(const TAngle &angle) const
Definition angle.h:657
BALL::TAngle::operator/
TAngle operator/(const TAngle &val)
Definition angle.h:610
BALL::TAngle::toRadian
T toRadian() const
Definition angle.h:454
BALL::TAngle::operator-
TAngle operator-() const
Definition angle.h:525
BALL::TAngle::isEquivalent
bool isEquivalent(TAngle angle) const
Definition angle.h:664
BALL::TAngle::toDegree
T toDegree() const
Definition angle.h:465
BALL::TAngle::operator+
TAngle operator+() const
Definition angle.h:519
BALL::TAngle::toRadian
static T toRadian(const T &degree)
Definition angle.h:460
BALL::TAngle::get
void get(T &val, bool radian=true) const
Definition angle.h:442
BALL::TAngle::operator!=
bool operator!=(const TAngle &angle) const
Definition angle.h:627
BALL::TAngle::Range
Range
Definition angle.h:68
BALL::TAngle< float >::RANGE__UNLIMITED
@ RANGE__UNLIMITED
Definition angle.h:70
BALL::TAngle< float >::RANGE__UNSIGNED
@ RANGE__UNSIGNED
Definition angle.h:72
BALL::TAngle< float >::RANGE__SIGNED
@ RANGE__SIGNED
Definition angle.h:74
BALL::TAngle::operator<=
bool operator<=(const TAngle &angle) const
Definition angle.h:645
BALL::TAngle::operator*=
TAngle & operator*=(const TAngle &angle)
Definition angle.h:571
BALL::TAngle::clear
virtual void clear()
Definition angle.h:111
BALL::TAngle::operator<
bool operator<(const TAngle &angle) const
Definition angle.h:633
BALL::TAngle::isValid
bool isValid() const
Definition angle.h:675
BALL::TAngle::TAngle
TAngle()
Definition angle.h:384
BALL::TAngle::negate
void negate()
Definition angle.h:513
BALL::TAngle::operator-=
TAngle & operator-=(const TAngle &angle)
Definition angle.h:551
BALL::TAngle::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
Definition angle.h:681
BALL::TAngle::TAngle
TAngle(const T &new_value, bool radian=true)
Definition angle.h:396
BALL::TAngle::toDegree
static T toDegree(const T &radian)
Definition angle.h:471
BALL::TAngle::~TAngle
virtual ~TAngle()
Definition angle.h:104
BALL::TAngle::set
void set(const T &new_value, bool radian=true)
Definition angle.h:416
BALL::TAngle::get
void get(TAngle &angle) const
Definition angle.h:436
BALL::TAngle::operator/=
TAngle & operator/=(const TAngle &angle)
Definition angle.h:585
BALL::TAngle::set
void set(const TAngle &angle)
Definition angle.h:410
BALL::TAngle::operator=
TAngle & operator=(const TAngle &angle)
Definition angle.h:422
BALL::TAngle< float >::value
float value
Definition angle.h:377
BALL::TAngle::operator+=
TAngle & operator+=(const TAngle &angle)
Definition angle.h:531
BALL::TAngle::normalize
void normalize(Range range)
Definition angle.h:478
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:621
BALL::TAngle::TAngle
TAngle(const TAngle &angle)
Definition angle.h:390
BALL::TAngle::swap
void swap(TAngle &angle)
Definition angle.h:402
create.h
BALL_CREATE
#define BALL_CREATE(name)
Definition create.h:62
debug.h
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
BALL::operator<<
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
BALL_DUMP_STREAM_PREFIX
#define BALL_DUMP_STREAM_PREFIX(os)
Definition macros.h:391
BALL_DUMP_STREAM_SUFFIX
#define BALL_DUMP_STREAM_SUFFIX(os)
Definition macros.h:395
BALL_DUMP_DEPTH
#define BALL_DUMP_DEPTH(os, depth)
Definition macros.h:390
BALL_DUMP_HEADER
#define BALL_DUMP_HEADER(os, cl, ob)
Definition macros.h:393
BALL::Constants::PI
BALL_EXTERN_VARIABLE const double PI
PI.
Definition constants.h:35
BALL::Maths::isNotEqual
bool isNotEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:224
BALL::Maths::isGreater
bool isGreater(const T1 &a, const T2 &b)
Definition MATHS/common.h:273
BALL::Maths::isGreaterOrEqual
bool isGreaterOrEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:261
BALL::Maths::isLess
bool isLess(const T1 &a, const T2 &b)
Definition MATHS/common.h:236
BALL::Maths::isLessOrEqual
bool isLessOrEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:249
BALL::Maths::isEqual
bool isEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:212
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::operator*
BALL_INLINE TAngle< T > operator*(const T &val, const TAngle< T > &angle)
Definition angle.h:704
BALL::operator+
ConstRandomAccessIterator< Container, DataType, Position, Traits > operator+(Distance distance, const ConstRandomAccessIterator< Container, DataType, Position, Traits > &iterator)
Definition randomAccessIterator.h:192
BALL::Angle
TAngle< float > Angle
Definition angle.h:697
BALL::operator>>
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
Definition regularData1D.h:802
BALL::operator-
BALL_INLINE TAngle< T > operator-(const T &val, const TAngle< T > &angle)
Definition angle.h:724