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())
240 mValue -= Derived::turn() * floor((mValue - Derived::lower())/ Derived::turn());
243 if(mValue < Derived::lower())
244 mValue += Derived::turn();
248 const T&
value()
const {
return mValue; }
261 explicit operator T()
const {
return mValue; }
272 setValue(Derived::convertFromSerialized(value));
289 T
deg()
const {
return This()->toDeg(); }
292 T
rad()
const {
return This()->toRad();}
302 template <
typename OtherUnitTag,
typename OtherDerived>
304 return Derived( a.mValue + conv(b) );
308 return Derived( a.
mValue + b);
312 return Derived( a + b.
mValue);
316 template <
typename OtherUnitTag,
typename OtherDerived>
318 return Derived( a.mValue - conv(b) );
322 return Derived( a.
mValue - b);
326 return Derived( a - b.
mValue);
331 return Derived(-mValue);
335 friend Derived
operator*(
const Derived& a,
const T& b) {
336 return Derived( a.mValue * b);
339 friend Derived
operator*(
const T& a,
const Derived& b) {
340 return Derived( a * b.mValue);
344 friend Derived
operator/(
const Derived& a,
const T& b) {
345 return Derived( a.mValue / b);
350 template <
typename OtherUnitTag,
typename OtherDerived>
352 setValue(mValue+conv(a));
362 template <
typename OtherUnitTag,
typename OtherDerived>
364 setValue(mValue-conv(a));
404 T d = mValue - other.mValue;
406 static const T halfTurn = Derived::turn() / 2;
408 d -= Derived::turn();
409 else if(d <= -halfTurn)
410 d += Derived::turn();
421 if (min.mValue <= max.mValue)
422 return mValue >= min.mValue && mValue <= max.mValue;
423 return mValue >= min.mValue || mValue <= max.mValue;
430 o << v.
value() <<
" " << UnitTag::unit();
441 return UnitTag::unit();
447 template <
typename OtherT,
typename OtherUnitTag,
typename OtherDerived>
450 return Private::AngleConverter<type,OtherUnitTag,UnitTag>::convert(other.
value());
455 Derived* This() {
return static_cast<Derived*
>(
this); }
458 const Derived* This()
const {
return static_cast<const Derived*
>(
this); }
467 #define MIRA_ANGLE_CONSTRUCTORS_AND_ASSIGNOPS(Type) \ 469 Type(const T& value, int) : Base(value,1) {} \ 472 explicit Type(T value) : Base(value) {} \ 473 template <typename OtherT, typename OtherUnitTag, typename OtherDerived> \ 474 Type(const AngleBase<OtherT,OtherUnitTag,OtherDerived>& other) : Base(other) {} \ 475 template <typename OtherUnitTag, typename OtherDerived> \ 476 Type& operator=(const AngleBase<T,OtherUnitTag,OtherDerived>& other) { \ 477 Base::operator=(other); \ 479 Type& operator=(const T& other) { \ 480 Base::operator=(other); \ 489 template <
typename T,
typename Derived>
492 friend class AngleBase<T, Private::DegreeUnitTag, Derived>;
499 static T turn() {
return (T)360; }
509 T toDeg()
const {
return this->mValue; }
512 T toRad()
const {
return deg2rad(this->mValue); }
518 template <
typename T,
typename Derived>
521 static_assert(std::is_floating_point<T>::value,
522 "Radians make sense with floating point types only. " 523 "Use Degree if you want to represent angles with integral types");
525 friend class AngleBase<T, Private::RadianUnitTag, Derived>;
533 static T turn() {
return two_pi<T>(); }
543 T toDeg()
const {
return rad2deg(this->mValue); }
546 T toRad()
const {
return this->mValue; }
553 template <
typename T>
566 template <
typename T>
579 static T lower() {
return static_cast<T
>(-180); }
590 template <
typename T,
typename SerializerTag>
604 template <
typename T>
616 static T lower() {
return static_cast<T
>(0); }
627 template <
typename T,
typename SerializerTag>
633 template <
typename T>
646 template <
typename T>
659 static T lower() {
return -pi<T>(); }
670 template <
typename T,
typename SerializerTag>
683 template <
typename T>
695 static T lower() {
return static_cast<T
>(0); }
706 template <
typename T,
typename SerializerTag>
712 template <
typename T>
728 template <
typename T>
736 template <
typename Reflector>
738 r.delegate(getter<T>(convertToSerialized, this->mValue),
739 setter<T>(convertFromSerialized, this->mValue));
747 static T
lower() {
return -pi<T>(); }
776 template <
typename T,
typename SerializerTag>
793 template <
typename T>
800 template <
typename Reflector>
802 r.delegate(getter<T>(convertToSerialized, this->mValue),
803 setter<T>(convertFromSerialized, this->mValue));
811 static T
lower() {
return static_cast<T
>(0); }
839 template <
typename T,
typename SerializerTag>
964 template <
typename T>
967 return getter<T>([&]{
return rad2deg<T>(cref);});
983 template <
typename T>
986 return setter<T>([&ref](
const T& in){ ref = deg2rad<T>(in);});
1002 template <
typename T>
1008 return setter<T>([&ref](
const T& in){
1011 "deg2radSetter: input value must be non-negative.");
1013 ref = deg2rad<T>(in);
1027 template <
typename T>
1043 template <
typename T>
1056 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:822
SignedAngle< double > SignedAngled
Double precision signed angle.
Definition: Angle.h:870
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:505
Base class for angle classes that represent angles using radians.
Definition: Angle.h:519
friend bool operator<=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:387
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:317
Radian< float > Radianf
Float precision angle.
Definition: Angle.h:863
Derived operator-=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Subtract other angle from this angle.
Definition: Angle.h:363
Includes often needed math headers and methods and provides additional constants. ...
Degree< double > Degreed
Double precision angle.
Definition: Angle.h:856
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:622
Derived & operator*=(const T &s)
Multiply this angle with scalar.
Definition: Angle.h:374
static const char * unit()
Returns the unit of this angle representation as string, e.g.
Definition: Angle.h:440
friend Derived operator/(const Derived &a, const T &b)
Divide by scalar.
Definition: Angle.h:344
void reflect(Reflector &r)
Definition: Angle.h:801
Getter< T > rad2degGetter(const T &cref)
Create a getter for serializing radians as degrees.
Definition: Angle.h:965
Derived & operator-=(const T &a)
Subtract float value from this angle.
Definition: Angle.h:368
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:763
MIRA_DEPRECATED("use Degree<T>(value)", static Angle fromDeg(T value))
deprecated, use Degree<T>(value) instead
Definition: Angle.h:826
Degree< int > Degreei
Integer precision angle.
Definition: Angle.h:852
friend bool operator<(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:386
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:770
Signed angle that is represented using degrees.
Definition: Angle.h:567
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:490
A tag type used as parameter type in deg2radSetter, signalling that negative values are not permitted...
Definition: Angle.h:974
AngleBase()
Definition: Angle.h:190
SignedRadian< double > SignedRadiand
Double precision signed angle.
Definition: Angle.h:861
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:811
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:536
const T & value() const
Returns the raw angle value given in the native unit of the angle class.
Definition: Angle.h:248
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:753
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:665
Setter< T > deg2radSetter(T &ref)
Create setter for deserializing radians from degrees. See rad2degGetter.
Definition: Angle.h:984
Derived & operator/=(const T &s)
Divide this angle by scalar.
Definition: Angle.h:380
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:773
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:271
SignedDegree< double > SignedDegreed
Double precision signed angle.
Definition: Angle.h:850
static T convertToSerialized(T value)
Converts own representation to serialized representation.
Definition: Angle.h:502
SignedAngle< float > SignedAnglef
Float precision signed angle.
Definition: Angle.h:868
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:463
Degree< float > Degreef
Float precision angle.
Definition: Angle.h:854
friend Derived operator-(const T &a, const AngleBase &b)
Subtract two angles.
Definition: Angle.h:325
Derived & operator+=(const T &a)
Add float value to this angle.
Definition: Angle.h:356
Unsigned angle that is represented using degrees.
Definition: Angle.h:554
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:980
Signed angle that is represented using radians.
Definition: Angle.h:647
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:701
friend std::ostream & operator<<(std::ostream &o, const AngleBase &v)
stream operator
Definition: Angle.h:429
friend Derived operator+(const Derived &a, const AngleBase< T, OtherUnitTag, OtherDerived > &b)
Add two angles.
Definition: Angle.h:303
friend Derived operator+(const T &a, const AngleBase &b)
Add two angles.
Definition: Angle.h:311
bool inAngleInterval(T value, T min, T max)
Definition: Angle.h:935
Derived abs(const mira::AngleBase< T, UnitTag, Derived > &other)
Definition: Angle.h:1057
friend Derived operator*(const T &a, const Derived &b)
Multiply with scalar.
Definition: Angle.h:339
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:289
Derived operator+=(const AngleBase< T, OtherUnitTag, OtherDerived > &a)
Add other angle to this angle.
Definition: Angle.h:351
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:292
Angle< double > Angled
Double precision angle.
Definition: Angle.h:874
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:928
Unsigned angle that is represented using radians.
Definition: Angle.h:634
SignedDegree< int > SignedDegreei
Integer precision signed angle.
Definition: Angle.h:846
void reflect(Reflector &r)
Definition: Angle.h:214
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:833
T serializedValue() const
Returns the value in the unit that is used for serialization.
Definition: Angle.h:283
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:1028
MIRA_DEPRECATED("use Radian<T>(value)", static SignedAngle fromRad(T value))
deprecated, use Radian<T>(value) instead
Definition: Angle.h:759
friend Derived operator*(const Derived &a, const T &b)
Multiply with scalar.
Definition: Angle.h:335
void reflect(Reflector &r)
Definition: Angle.h:737
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:467
Signed angle that is represented using radians.
Definition: Angle.h:729
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:419
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:836
static T convertFromSerialized(T value)
Converts serialized representation to own representation.
Definition: Angle.h:539
friend Derived operator-(const AngleBase &a, const T &b)
Subtract two angles.
Definition: Angle.h:321
Derived operator-() const
Unary minus operator.
Definition: Angle.h:330
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:585
SignedDegree< float > SignedDegreef
Float precision signed angle.
Definition: Angle.h:848
friend Derived operator+(const AngleBase &a, const T &b)
Add two angles.
Definition: Angle.h:307
friend bool operator==(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:390
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:817
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:402
SignedRadian< float > SignedRadianf
Float precision signed angle.
Definition: Angle.h:859
static T lower()
Returns the lower limit of the defined angle interval.
Definition: Angle.h:747
Unsigned angle that is represented using radians.
Definition: Angle.h:713
Angle< float > Anglef
Float precision angle.
Definition: Angle.h:872
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:943
friend bool operator>(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:388
friend bool operator!=(const AngleBase &a, const AngleBase &b)
Definition: Angle.h:391
Radian< double > Radiand
Double precision angle.
Definition: Angle.h:865