48 #include <type_traits> 58 #ifndef _MIRA_ANGLE_H_ 59 #define _MIRA_ANGLE_H_ 66 template <
typename Param,
typename Result>
67 inline Result
deg2rad(Param value) {
68 return static_cast<Result
>(value * pi_div_deg180<Result>());
70 template <
typename Param,
typename Result>
71 inline Result
rad2deg(Param value) {
72 return static_cast<Result
>(value * deg180_div_pi<Result>());
80 typename std::enable_if<std::is_floating_point<T>::value, T>
::type 82 return Private::deg2rad<T, T>(value);
88 typename std::enable_if<std::is_integral<T>::value,
double>
::type 90 return Private::deg2rad<T, double>(value);
95 typename std::enable_if<!std::is_arithmetic<T>::value>
::type 97 static_assert(
sizeof(T)==0,
98 "deg2rad must be used with arithmetic types");
102 template <
typename T>
104 typename std::enable_if<std::is_floating_point<T>::value, T>
::type 106 return Private::rad2deg<T, T>(value);
110 template <
typename T>
112 typename std::enable_if<std::is_integral<T>::value,
double>
::type 114 return Private::rad2deg<T, double>(value);
118 template <
typename T>
119 typename std::enable_if<!std::is_arithmetic<T>::value>
::type 121 static_assert(
sizeof(T)==0,
122 "rad2deg must be used with arithmetic types");
130 struct DegreeUnitTag {
132 static const char* unit() {
return "deg"; }
135 struct RadianUnitTag {
137 static const char* unit() {
return "rad"; }
141 template <
typename T,
typename FromTag,
typename ToTag>
142 struct AngleConverter {
143 static_assert(
sizeof(FromTag)==0,
"Angle conversion not specialized!");
144 static T convert(T value) {
return static_cast<T
>(0); }
149 template <
typename T>
150 struct AngleConverter<T,DegreeUnitTag,RadianUnitTag> {
154 template <
typename T>
155 struct AngleConverter<T,RadianUnitTag,DegreeUnitTag> {
159 template <
typename T,
typename SameTag>
160 struct AngleConverter<T,SameTag,SameTag> {
161 static T convert(T value) {
return value; }
168 template <
typename A,
typename B>
169 struct AnglePromoteHelper {
170 typedef decltype(A(0) + B(0))
type;
181 template <typename T, typename UnitTag, typename Derived>
193 template <
typename OtherT,
typename OtherUnitTag,
typename OtherDerived>
195 setValue(conv(other));
201 template <
typename OtherUnitTag,
typename OtherDerived>
203 mValue = conv(other);
212 template <
typename Reflector>
224 static T
upper() {
return Derived::lower() + Derived::turn(); }
233 if(mValue < Derived::upper() && mValue >= Derived::lower())
240 mValue -= Derived::turn() * floor((mValue - Derived::lower())/ Derived::turn());
243 mValue -= Derived::turn() * floor((
double)(mValue - Derived::lower())/ Derived::turn());
246 if(mValue < Derived::lower())
247 mValue += Derived::turn();
251 const T&
value()
const {
return mValue; }
264 explicit operator T()
const {
return mValue; }
275 setValue(Derived::convertFromSerialized(value));
292 T
deg()
const {
return This()->toDeg(); }
295 T
rad()
const {
return This()->toRad();}
305 template <
typename OtherUnitTag,
typename OtherDerived>
307 return Derived( a.mValue + conv(b) );
311 return Derived( a.
mValue + b);
315 return Derived( a + b.
mValue);
319 template <
typename OtherUnitTag,
typename OtherDerived>
321 return Derived( a.mValue - conv(b) );
325 return Derived( a.
mValue - b);
329 return Derived( a - b.
mValue);
334 return Derived(-mValue);
338 friend Derived
operator*(
const Derived& a,
const T& b) {
339 return Derived( a.mValue * b);
342 friend Derived
operator*(
const T& a,
const Derived& b) {
343 return Derived( a * b.mValue);
347 friend Derived
operator/(
const Derived& a,
const T& b) {
348 return Derived( a.mValue / b);
353 template <
typename OtherUnitTag,
typename OtherDerived>
355 setValue(mValue+conv(a));
365 template <
typename OtherUnitTag,
typename OtherDerived>
367 setValue(mValue-conv(a));
407 T d = mValue - other.mValue;
409 static const T halfTurn = Derived::turn() / 2;
411 d -= Derived::turn();
412 else if(d <= -halfTurn)
413 d += Derived::turn();
424 if (min.mValue <= max.mValue)
425 return mValue >= min.mValue && mValue <= max.mValue;
426 return mValue >= min.mValue || mValue <= max.mValue;
433 o << v.
value() <<
" " << UnitTag::unit();
444 return UnitTag::unit();
450 template <
typename OtherT,
typename OtherUnitTag,
typename OtherDerived>
453 return Private::AngleConverter<type,OtherUnitTag,UnitTag>::convert(other.
value());
458 Derived* This() {
return static_cast<Derived*
>(
this); }
461 const Derived* This()
const {
return static_cast<const Derived*
>(
this); }
470 #define MIRA_ANGLE_CONSTRUCTORS_AND_ASSIGNOPS(Type) \ 472 Type(const T& value, int) : Base(value,1) {} \ 475 explicit Type(T value) : Base(value) {} \ 476 template <typename OtherT, typename OtherUnitTag, typename OtherDerived> \ 477 Type(const AngleBase<OtherT,OtherUnitTag,OtherDerived>& other) : Base(other) {} \ 478 template <typename OtherUnitTag, typename OtherDerived> \ 479 Type& operator=(const AngleBase<T,OtherUnitTag,OtherDerived>& other) { \ 480 Base::operator=(other); \ 482 Type& operator=(const T& other) { \ 483 Base::operator=(other); \ 492 template <
typename T,
typename Derived>
495 friend class AngleBase<T, Private::DegreeUnitTag, Derived>;
502 static T turn() {
return (T)360; }
512 T toDeg()
const {
return this->mValue; }
515 T toRad()
const {
return deg2rad(this->mValue); }
521 template <
typename T,
typename Derived>
524 static_assert(std::is_floating_point<T>::value,
525 "Radians make sense with floating point types only. " 526 "Use Degree if you want to represent angles with integral types");
528 friend class AngleBase<T, Private::RadianUnitTag, Derived>;
536 static T turn() {
return two_pi<T>(); }
546 T toDeg()
const {
return rad2deg(this->mValue); }
549 T toRad()
const {
return this->mValue; }
556 template <
typename T>
569 template <
typename T>
582 static T lower() {
return static_cast<T
>(-180); }
593 template <
typename T,
typename SerializerTag>
607 template <
typename T>
619 static T lower() {
return static_cast<T
>(0); }
630 template <
typename T,
typename SerializerTag>
636 template <
typename T>
649 template <
typename T>
662 static T lower() {
return -pi<T>(); }
673 template <
typename T,
typename SerializerTag>
686 template <
typename T>
698 static T lower() {
return static_cast<T
>(0); }
709 template <
typename T,
typename SerializerTag>
715 template <
typename T>
731 template <
typename T>
739 template <
typename Reflector>
741 r.delegate(getter<T>(convertToSerialized, this->mValue),
742 setter<T>(convertFromSerialized, this->mValue));
750 static T
lower() {
return -pi<T>(); }
779 template <
typename T,
typename SerializerTag>
796 template <
typename T>
803 template <
typename Reflector>
805 r.delegate(getter<T>(convertToSerialized, this->mValue),
806 setter<T>(convertFromSerialized, this->mValue));
814 static T
lower() {
return static_cast<T
>(0); }
842 template <
typename T,
typename SerializerTag>
923 template <
typename T>
926 return getter<T>([&]{
return rad2deg<T>(cref);});
942 template <
typename T>
945 return setter<T>([&ref](
const T& in){ ref = deg2rad<T>(in);});
961 template <
typename T>
967 return setter<T>([&ref](
const T& in){
970 "deg2radSetter: input value must be non-negative.");
972 ref = deg2rad<T>(in);
986 template <
typename T>
1002 template <
typename T>
1015 template <
typename T,
typename UnitTag,
typename Derived>
void operator=(const T &other)
Definition: Angle.h:206
MIRA_DEPRECATED("use Radian<T>(value)", static Angle fromRad(T value))
deprecated, use Radian<T>(value) instead
Definition: Angle.h:825
SignedAngle< double > SignedAngled
Double precision signed angle.
Definition: Angle.h:873
INTERNAL std::enable_if< std::is_floating_point< T >::value, T >::type deg2rad(T value)
Convert degree to radian, for floating point arguments (return type = argument type) ...
Definition: Angle.h:81
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:508
Base class for angle classes that represent angles using radians.
Definition: Angle.h:522
friend bool operator<=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:390
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
friend Derived operator-(const Derived &a, const AngleBase< T, OtherUnitTag, OtherDerived > &b)
Subtract two angles.
Definition: Angle.h:320
Radian< float > Radianf
Float precision angle.
Definition: Angle.h:866
Derived operator-=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Subtract other angle from this angle.
Definition: Angle.h:366
Includes often needed math headers and methods and provides additional constants. ...
Degree< double > Degreed
Double precision angle.
Definition: Angle.h:859
SignedDegree< T > smallestDifference(const Degree &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:625
Derived & operator*=(const T &s)
Multiply this angle with scalar.
Definition: Angle.h:377
static const char * unit()
Returns the unit of this angle representation as string, e.g.
Definition: Angle.h:443
friend Derived operator/(const Derived &a, const T &b)
Divide by scalar.
Definition: Angle.h:347
void reflect(Reflector &r)
Definition: Angle.h:804
Getter< T > rad2degGetter(const T &cref)
Create a getter for serializing radians as degrees.
Definition: Angle.h:924
Derived & operator-=(const T &a)
Subtract float value from this angle.
Definition: Angle.h:371
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
MIRA_DEPRECATED("use Degree<T>(value)", static SignedAngle fromDeg(T value))
deprecated, use Degree<T>(value) instead
Definition: Angle.h:766
MIRA_DEPRECATED("use Degree<T>(value)", static Angle fromDeg(T value))
deprecated, use Degree<T>(value) instead
Definition: Angle.h:829
Degree< int > Degreei
Integer precision angle.
Definition: Angle.h:855
friend bool operator<(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:389
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:773
Signed angle that is represented using degrees.
Definition: Angle.h:570
Holds a boost::function object to a special setter function that must meet the signature "void method...
Definition: GetterSetter.h:395
Base class for angle classes that represent angles using degrees.
Definition: Angle.h:493
A tag type used as parameter type in deg2radSetter, signalling that negative values are not permitted...
Definition: Angle.h:933
AngleBase()
Definition: Angle.h:189
SignedRadian< double > SignedRadiand
Double precision signed angle.
Definition: Angle.h:864
Contains internal accessor class that abstracts from the underlying getter and setter classes or dire...
static T lower()
Returns the lower limit of the defined angle interval.
Definition: Angle.h:814
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:539
const T & value() const
Returns the raw angle value given in the native unit of the angle class.
Definition: Angle.h:251
SignedAngle< T > smallestDifference(const SignedAngle &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:756
AngleBase(const AngleBase &other)
Definition: Angle.h:191
SignedRadian< T > smallestDifference(const SignedRadian &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:668
Setter< T > deg2radSetter(T &ref)
Create setter for deserializing radians from degrees. See rad2degGetter.
Definition: Angle.h:943
Derived & operator/=(const T &s)
Divide this angle by scalar.
Definition: Angle.h:383
Duration abs(const Duration &duration)
Get the absolute duration from a duration.
Definition: Time.h:399
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:776
Commonly used exception classes.
AngleBase(T value, int)
Definition: Angle.h:187
void setSerializedValue(const T &value)
Sets the value in the unit that is used for serialization.
Definition: Angle.h:274
SignedDegree< double > SignedDegreed
Double precision signed angle.
Definition: Angle.h:853
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:505
SignedAngle< float > SignedAnglef
Float precision signed angle.
Definition: Angle.h:871
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
T mValue
the actual value
Definition: Angle.h:466
Degree< float > Degreef
Float precision angle.
Definition: Angle.h:857
friend Derived operator-(const T &a, const AngleBase &b)
Subtract two angles.
Definition: Angle.h:328
Derived & operator+=(const T &a)
Add float value to this angle.
Definition: Angle.h:359
Unsigned angle that is represented using degrees.
Definition: Angle.h:557
constexpr Deg2RadNonNegativeType Deg2RadNonNegative
Use this constant as second parameter when calling deg2radSetter, in order to prohibit negative value...
Definition: Angle.h:939
Signed angle that is represented using radians.
Definition: Angle.h:650
AngleBase(const AngleBase< OtherT, OtherUnitTag, OtherDerived > &other)
Definition: Angle.h:194
SignedRadian< T > smallestDifference(const Radian &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:704
friend std::ostream & operator<<(std::ostream &o, const AngleBase &v)
stream operator
Definition: Angle.h:432
friend Derived operator+(const Derived &a, const AngleBase< T, OtherUnitTag, OtherDerived > &b)
Add two angles.
Definition: Angle.h:306
friend Derived operator+(const T &a, const AngleBase &b)
Add two angles.
Definition: Angle.h:314
bool inAngleInterval(T value, T min, T max)
Definition: Angle.h:894
Derived abs(const mira::AngleBase< T, UnitTag, Derived > &other)
Definition: Angle.h:1016
friend Derived operator*(const T &a, const Derived &b)
Multiply with scalar.
Definition: Angle.h:342
void operator=(const AngleBase< T, OtherUnitTag, OtherDerived > &other)
Definition: Angle.h:202
T deg() const
Returns the value of the angle in degrees.
Definition: Angle.h:292
Derived operator+=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Add other angle to this angle.
Definition: Angle.h:354
The Accessor class is used as an adapter to reduce the code bloat within the reflection and serializa...
Definition: Accessor.h:244
T rad() const
Returns the value of the angle in radian.
Definition: Angle.h:295
Angle< double > Angled
Double precision angle.
Definition: Angle.h:877
T smallestAngleDifference(const T &a, const T &b)
Returns the signed difference angle between the specified angles (in radian) that has the smallest ab...
Definition: Angle.h:887
Unsigned angle that is represented using radians.
Definition: Angle.h:637
SignedDegree< int > SignedDegreei
Integer precision signed angle.
Definition: Angle.h:849
void reflect(Reflector &r)
Definition: Angle.h:213
friend bool operator>=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:392
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:836
T serializedValue() const
Returns the value in the unit that is used for serialization.
Definition: Angle.h:286
Holds a boost::function object to a special getter function that must meet the signature "T method()"...
Definition: GetterSetter.h:87
Accessor< Getter< T >, Setter< T > > radAsDegAccessor(T &ref)
Create an accessor consisting of getter + setter for serializing radians as degrees.
Definition: Angle.h:987
MIRA_DEPRECATED("use Radian<T>(value)", static SignedAngle fromRad(T value))
deprecated, use Radian<T>(value) instead
Definition: Angle.h:762
friend Derived operator*(const Derived &a, const T &b)
Multiply with scalar.
Definition: Angle.h:338
void reflect(Reflector &r)
Definition: Angle.h:740
void setValue(const T &value)
Set the angle value. The input value is mapped into the angle interval.
Definition: Angle.h:230
AngleBase(T value)
Definition: Angle.h:190
#define MIRA_ANGLE_CONSTRUCTORS_AND_ASSIGNOPS(Type)
Definition: Angle.h:470
Signed angle that is represented using radians.
Definition: Angle.h:732
static T upper()
Returns the upper limit of the defined angle interval.
Definition: Angle.h:224
bool isInInterval(const Derived &min, const Derived &max) const
Returns true, if the angle is in the given interval [min,max].
Definition: Angle.h:422
std::enable_if< std::is_floating_point< T >::value, T >::type rad2deg(T value)
Convert radian to degree, for floating point arguments (return type = argument type) ...
Definition: Angle.h:105
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:839
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:542
friend Derived operator-(const AngleBase &a, const T &b)
Subtract two angles.
Definition: Angle.h:324
Derived operator-() const
Unary minus operator.
Definition: Angle.h:333
SignedDegree< T > smallestDifference(const SignedDegree &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:588
SignedDegree< float > SignedDegreef
Float precision signed angle.
Definition: Angle.h:851
friend Derived operator+(const AngleBase &a, const T &b)
Add two angles.
Definition: Angle.h:310
friend bool operator==(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:393
Accessor< Getter, Setter > makeAccessor(const Getter &getter, const Setter &setter)
Helper method that creates an accessor from a different combination of either direct access to a vari...
Definition: Accessor.h:300
SignedAngle< T > smallestDifference(const Angle &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:820
T smallestDifferenceValue(const Derived &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:405
SignedRadian< float > SignedRadianf
Float precision signed angle.
Definition: Angle.h:862
static T lower()
Returns the lower limit of the defined angle interval.
Definition: Angle.h:750
Unsigned angle that is represented using radians.
Definition: Angle.h:716
Angle< float > Anglef
Float precision angle.
Definition: Angle.h:875
Base class template for derived Angle implementations.
Definition: Angle.h:182
bool isInAngleInterval(T value, T min, T max)
Returns true, if the given angle (in radian) is in the given interval [min,max].
Definition: Angle.h:902
friend bool operator>(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:391
friend bool operator!=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:394
Radian< double > Radiand
Double precision angle.
Definition: Angle.h:868