48 #ifndef _MIRA_ANGLE_H_ 49 #define _MIRA_ANGLE_H_ 51 #include <type_traits> 67 template <
typename Param,
typename Result>
68 inline Result
deg2rad(Param value) {
69 return static_cast<Result
>(value * pi_div_deg180<Result>());
71 template <
typename Param,
typename Result>
72 inline Result
rad2deg(Param value) {
73 return static_cast<Result
>(value * deg180_div_pi<Result>());
81 typename std::enable_if<std::is_floating_point<T>::value, T>
::type 83 return Private::deg2rad<T, T>(value);
89 typename std::enable_if<std::is_integral<T>::value,
double>
::type 91 return Private::deg2rad<T, double>(value);
96 typename std::enable_if<!std::is_arithmetic<T>::value>
::type 98 static_assert(
sizeof(T)==0,
99 "deg2rad must be used with arithmetic types");
103 template <
typename T>
105 typename std::enable_if<std::is_floating_point<T>::value, T>
::type 107 return Private::rad2deg<T, T>(value);
111 template <
typename T>
113 typename std::enable_if<std::is_integral<T>::value,
double>
::type 115 return Private::rad2deg<T, double>(value);
119 template <
typename T>
120 typename std::enable_if<!std::is_arithmetic<T>::value>
::type 122 static_assert(
sizeof(T)==0,
123 "rad2deg must be used with arithmetic types");
131 struct DegreeUnitTag {
133 static const char* unit() {
return "deg"; }
136 struct RadianUnitTag {
138 static const char* unit() {
return "rad"; }
142 template <
typename T,
typename FromTag,
typename ToTag>
143 struct AngleConverter {
144 static_assert(
sizeof(FromTag)==0,
"Angle conversion not specialized!");
145 static T convert(T value) {
return static_cast<T
>(0); }
150 template <
typename T>
151 struct AngleConverter<T,DegreeUnitTag,RadianUnitTag> {
155 template <
typename T>
156 struct AngleConverter<T,RadianUnitTag,DegreeUnitTag> {
160 template <
typename T,
typename SameTag>
161 struct AngleConverter<T,SameTag,SameTag> {
162 static T convert(T value) {
return value; }
169 template <
typename A,
typename B>
170 struct AnglePromoteHelper {
171 typedef decltype(A(0) + B(0))
type;
182 template <typename T, typename UnitTag, typename Derived>
194 template <
typename OtherT,
typename OtherUnitTag,
typename OtherDerived>
196 setValue(conv(other));
202 template <
typename OtherUnitTag,
typename OtherDerived>
204 mValue = conv(other);
213 template <
typename Reflector>
225 static T
upper() {
return Derived::lower() + Derived::turn(); }
234 if(mValue < Derived::upper() && mValue >= Derived::lower())
241 mValue -= Derived::turn() * floor((mValue - Derived::lower())/ Derived::turn());
244 mValue -= Derived::turn() * floor((
double)(mValue - Derived::lower())/ Derived::turn());
247 if(mValue < Derived::lower())
248 mValue += Derived::turn();
252 const T&
value()
const {
return mValue; }
265 explicit operator T()
const {
return mValue; }
276 setValue(Derived::convertFromSerialized(value));
293 T
deg()
const {
return This()->toDeg(); }
296 T
rad()
const {
return This()->toRad();}
306 template <
typename OtherUnitTag,
typename OtherDerived>
308 return Derived( a.mValue + conv(b) );
312 return Derived( a.
mValue + b);
316 return Derived( a + b.
mValue);
320 template <
typename OtherUnitTag,
typename OtherDerived>
322 return Derived( a.mValue - conv(b) );
326 return Derived( a.
mValue - b);
330 return Derived( a - b.
mValue);
335 return Derived(-mValue);
339 friend Derived
operator*(
const Derived& a,
const T& b) {
340 return Derived( a.mValue * b);
343 friend Derived
operator*(
const T& a,
const Derived& b) {
344 return Derived( a * b.mValue);
348 friend Derived
operator/(
const Derived& a,
const T& b) {
349 return Derived( a.mValue / b);
354 template <
typename OtherUnitTag,
typename OtherDerived>
356 setValue(mValue+conv(a));
366 template <
typename OtherUnitTag,
typename OtherDerived>
368 setValue(mValue-conv(a));
408 T d = mValue - other.mValue;
410 static const T halfTurn = Derived::turn() / 2;
412 d -= Derived::turn();
413 else if(d <= -halfTurn)
414 d += Derived::turn();
425 if (min.mValue <= max.mValue)
426 return mValue >= min.mValue && mValue <= max.mValue;
427 return mValue >= min.mValue || mValue <= max.mValue;
434 o << v.
value() <<
" " << UnitTag::unit();
445 return UnitTag::unit();
451 template <
typename OtherT,
typename OtherUnitTag,
typename OtherDerived>
454 return Private::AngleConverter<type,OtherUnitTag,UnitTag>::convert(other.
value());
459 Derived* This() {
return static_cast<Derived*
>(
this); }
462 const Derived* This()
const {
return static_cast<const Derived*
>(
this); }
471 #define MIRA_ANGLE_CONSTRUCTORS_AND_ASSIGNOPS(Type) \ 473 Type(const T& value, int) : Base(value,1) {} \ 476 explicit Type(T value) : Base(value) {} \ 477 template <typename OtherT, typename OtherUnitTag, typename OtherDerived> \ 478 Type(const AngleBase<OtherT,OtherUnitTag,OtherDerived>& other) : Base(other) {} \ 479 template <typename OtherUnitTag, typename OtherDerived> \ 480 Type& operator=(const AngleBase<T,OtherUnitTag,OtherDerived>& other) { \ 481 Base::operator=(other); \ 483 Type& operator=(const T& other) { \ 484 Base::operator=(other); \ 493 template <
typename T,
typename Derived>
496 friend class AngleBase<T, Private::DegreeUnitTag, Derived>;
503 static T turn() {
return (T)360; }
513 T toDeg()
const {
return this->mValue; }
516 T toRad()
const {
return deg2rad(this->mValue); }
522 template <
typename T,
typename Derived>
525 static_assert(std::is_floating_point<T>::value,
526 "Radians make sense with floating point types only. " 527 "Use Degree if you want to represent angles with integral types");
529 friend class AngleBase<T, Private::RadianUnitTag, Derived>;
537 static T turn() {
return two_pi<T>(); }
547 T toDeg()
const {
return rad2deg(this->mValue); }
550 T toRad()
const {
return this->mValue; }
557 template <
typename T>
570 template <
typename T>
583 static T lower() {
return static_cast<T
>(-180); }
594 template <
typename T,
typename SerializerTag>
608 template <
typename T>
620 static T lower() {
return static_cast<T
>(0); }
631 template <
typename T,
typename SerializerTag>
637 template <
typename T>
650 template <
typename T>
663 static T lower() {
return -pi<T>(); }
674 template <
typename T,
typename SerializerTag>
687 template <
typename T>
699 static T lower() {
return static_cast<T
>(0); }
710 template <
typename T,
typename SerializerTag>
716 template <
typename T>
732 template <
typename T>
740 template <
typename Reflector>
742 r.delegate(getter<T>(convertToSerialized, this->mValue),
743 setter<T>(convertFromSerialized, this->mValue));
751 static T
lower() {
return -pi<T>(); }
780 template <
typename T,
typename SerializerTag>
797 template <
typename T>
804 template <
typename Reflector>
806 r.delegate(getter<T>(convertToSerialized, this->mValue),
807 setter<T>(convertFromSerialized, this->mValue));
815 static T
lower() {
return static_cast<T
>(0); }
843 template <
typename T,
typename SerializerTag>
968 template <
typename T>
971 return getter<T>([&]{
return rad2deg<T>(cref);});
987 template <
typename T>
990 return setter<T>([&ref](
const T& in){ ref = deg2rad<T>(in);});
1006 template <
typename T>
1012 return setter<T>([&ref](
const T& in){
1015 "deg2radSetter: input value must be non-negative.");
1017 ref = deg2rad<T>(in);
1031 template <
typename T>
1047 template <
typename T>
1060 template <
typename T,
typename UnitTag,
typename Derived>
void operator=(const T &other)
Definition: Angle.h:207
MIRA_DEPRECATED("use Radian<T>(value)", static Angle fromRad(T value))
deprecated, use Radian<T>(value) instead
Definition: Angle.h:826
SignedAngle< double > SignedAngled
Double precision signed angle.
Definition: Angle.h:874
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:82
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:509
Base class for angle classes that represent angles using radians.
Definition: Angle.h:523
friend bool operator<=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:391
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:321
Radian< float > Radianf
Float precision angle.
Definition: Angle.h:867
Derived operator-=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Subtract other angle from this angle.
Definition: Angle.h:367
Includes often needed math headers and methods and provides additional constants. ...
Degree< double > Degreed
Double precision angle.
Definition: Angle.h:860
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:626
Derived & operator*=(const T &s)
Multiply this angle with scalar.
Definition: Angle.h:378
static const char * unit()
Returns the unit of this angle representation as string, e.g.
Definition: Angle.h:444
friend Derived operator/(const Derived &a, const T &b)
Divide by scalar.
Definition: Angle.h:348
void reflect(Reflector &r)
Definition: Angle.h:805
Getter< T > rad2degGetter(const T &cref)
Create a getter for serializing radians as degrees.
Definition: Angle.h:969
Derived & operator-=(const T &a)
Subtract float value from this angle.
Definition: Angle.h:372
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:767
MIRA_DEPRECATED("use Degree<T>(value)", static Angle fromDeg(T value))
deprecated, use Degree<T>(value) instead
Definition: Angle.h:830
Degree< int > Degreei
Integer precision angle.
Definition: Angle.h:856
friend bool operator<(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:390
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:774
Signed angle that is represented using degrees.
Definition: Angle.h:571
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:494
A tag type used as parameter type in deg2radSetter, signalling that negative values are not permitted...
Definition: Angle.h:978
AngleBase()
Definition: Angle.h:190
SignedRadian< double > SignedRadiand
Double precision signed angle.
Definition: Angle.h:865
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:815
#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:540
const T & value() const
Returns the raw angle value given in the native unit of the angle class.
Definition: Angle.h:252
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:757
AngleBase(const AngleBase &other)
Definition: Angle.h:192
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:669
Setter< T > deg2radSetter(T &ref)
Create setter for deserializing radians from degrees. See rad2degGetter.
Definition: Angle.h:988
Derived & operator/=(const T &s)
Divide this angle by scalar.
Definition: Angle.h:384
Duration abs(const Duration &duration)
Get the absolute duration from a duration.
Definition: Time.h:401
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:777
Commonly used exception classes.
AngleBase(T value, int)
Definition: Angle.h:188
void setSerializedValue(const T &value)
Sets the value in the unit that is used for serialization.
Definition: Angle.h:275
SignedDegree< double > SignedDegreed
Double precision signed angle.
Definition: Angle.h:854
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:506
SignedAngle< float > SignedAnglef
Float precision signed angle.
Definition: Angle.h:872
By default, IsCheapToCopy<T>::value evaluates to true for fundamental types T, false for all other ty...
Definition: IsCheapToCopy.h:63
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:467
Degree< float > Degreef
Float precision angle.
Definition: Angle.h:858
friend Derived operator-(const T &a, const AngleBase &b)
Subtract two angles.
Definition: Angle.h:329
Derived & operator+=(const T &a)
Add float value to this angle.
Definition: Angle.h:360
Unsigned angle that is represented using degrees.
Definition: Angle.h:558
Type trait to define if a class is cheap to copy.
constexpr Deg2RadNonNegativeType Deg2RadNonNegative
Use this constant as second parameter when calling deg2radSetter, in order to prohibit negative value...
Definition: Angle.h:984
Signed angle that is represented using radians.
Definition: Angle.h:651
AngleBase(const AngleBase< OtherT, OtherUnitTag, OtherDerived > &other)
Definition: Angle.h:195
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:705
friend std::ostream & operator<<(std::ostream &o, const AngleBase &v)
stream operator
Definition: Angle.h:433
friend Derived operator+(const Derived &a, const AngleBase< T, OtherUnitTag, OtherDerived > &b)
Add two angles.
Definition: Angle.h:307
friend Derived operator+(const T &a, const AngleBase &b)
Add two angles.
Definition: Angle.h:315
bool inAngleInterval(T value, T min, T max)
Definition: Angle.h:939
Derived abs(const mira::AngleBase< T, UnitTag, Derived > &other)
Definition: Angle.h:1061
friend Derived operator*(const T &a, const Derived &b)
Multiply with scalar.
Definition: Angle.h:343
void operator=(const AngleBase< T, OtherUnitTag, OtherDerived > &other)
Definition: Angle.h:203
T deg() const
Returns the value of the angle in degrees.
Definition: Angle.h:293
Derived operator+=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Add other angle to this angle.
Definition: Angle.h:355
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:296
Angle< double > Angled
Double precision angle.
Definition: Angle.h:878
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:932
Unsigned angle that is represented using radians.
Definition: Angle.h:638
SignedDegree< int > SignedDegreei
Integer precision signed angle.
Definition: Angle.h:850
void reflect(Reflector &r)
Definition: Angle.h:214
friend bool operator>=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:393
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:837
T serializedValue() const
Returns the value in the unit that is used for serialization.
Definition: Angle.h:287
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:1032
MIRA_DEPRECATED("use Radian<T>(value)", static SignedAngle fromRad(T value))
deprecated, use Radian<T>(value) instead
Definition: Angle.h:763
friend Derived operator*(const Derived &a, const T &b)
Multiply with scalar.
Definition: Angle.h:339
void reflect(Reflector &r)
Definition: Angle.h:741
void setValue(const T &value)
Set the angle value. The input value is mapped into the angle interval.
Definition: Angle.h:231
AngleBase(T value)
Definition: Angle.h:191
#define MIRA_ANGLE_CONSTRUCTORS_AND_ASSIGNOPS(Type)
Definition: Angle.h:471
Signed angle that is represented using radians.
Definition: Angle.h:733
static T upper()
Returns the upper limit of the defined angle interval.
Definition: Angle.h:225
bool isInInterval(const Derived &min, const Derived &max) const
Returns true, if the angle is in the given interval [min,max].
Definition: Angle.h:423
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:106
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:840
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:543
friend Derived operator-(const AngleBase &a, const T &b)
Subtract two angles.
Definition: Angle.h:325
Derived operator-() const
Unary minus operator.
Definition: Angle.h:334
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:589
SignedDegree< float > SignedDegreef
Float precision signed angle.
Definition: Angle.h:852
friend Derived operator+(const AngleBase &a, const T &b)
Add two angles.
Definition: Angle.h:311
friend bool operator==(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:394
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:821
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:406
SignedRadian< float > SignedRadianf
Float precision signed angle.
Definition: Angle.h:863
static T lower()
Returns the lower limit of the defined angle interval.
Definition: Angle.h:751
Unsigned angle that is represented using radians.
Definition: Angle.h:717
Angle< float > Anglef
Float precision angle.
Definition: Angle.h:876
Base class template for derived Angle implementations.
Definition: Angle.h:183
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:947
friend bool operator>(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:392
friend bool operator!=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:395
Radian< double > Radiand
Double precision angle.
Definition: Angle.h:869