Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
angleutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_ANGLEUTILS_H_
19 #define ION_MATH_ANGLEUTILS_H_
20 
22 
23 #include <algorithm>
24 #include <cmath>
25 
26 #include "ion/math/angle.h"
27 #include "ion/math/utils.h"
28 #include "ion/math/vector.h"
29 #include "ion/math/vectorutils.h"
30 
31 namespace ion {
32 namespace math {
33 
35 template <typename T>
36 inline Angle<T> ArcCosine(T v) {
37  return Angle<T>::FromRadians(acos(v));
38 }
40 template <>
41 inline Angle<float> ArcCosine(float v) {
42  return Angle<float>::FromRadians(acosf(v));
43 }
44 
46 template <typename T>
47 inline Angle<T> ArcSine(T v) {
48  return Angle<T>::FromRadians(asin(v));
49 }
51 template <>
52 inline Angle<float> ArcSine(float v) {
53  return Angle<float>::FromRadians(asinf(v));
54 }
55 
57 template <typename T>
58 inline Angle<T> ArcTangent(T v) {
59  return Angle<T>::FromRadians(atan(v));
60 }
62 template <>
63 inline Angle<float> ArcTangent(float v) {
64  return Angle<float>::FromRadians(atanf(v));
65 }
66 
68 template <typename T>
69 inline Angle<T> ArcTangent2(T y, T x) {
70  return Angle<T>::FromRadians(atan2(y, x));
71 }
73 template <>
74 inline Angle<float> ArcTangent2(float y, float x) {
75  return Angle<float>::FromRadians(atan2f(y, x));
76 }
77 
79 template <typename T>
80 inline T Cosine(const ion::math::Angle<T>& angle) {
81  return Cosine(angle.Radians());
82 }
83 
85 template <typename T>
86 inline T Sine(const ion::math::Angle<T>& angle) {
87  return Sine(angle.Radians());
88 }
89 
91 template <typename T>
92 inline T Tangent(const ion::math::Angle<T>& angle) {
93  return Tangent(angle.Radians());
94 }
95 
97 template <int Dimension, typename T>
100  static const T kOne = static_cast<T>(1);
102  DCHECK_LE(Abs(LengthSquared(a) - kOne),
103  std::numeric_limits<T>::epsilon() * 100)
104  << "First input vector to AngleBetween must have unit length.";
105  DCHECK_LE(Abs(LengthSquared(b) - kOne),
106  std::numeric_limits<T>::epsilon() * 100)
107  << "Second input vector to AngleBetween must have unit length.";
108 
111  return ArcCosine(Clamp(Dot(a, b), -kOne, kOne));
112 }
113 
114 } // namespace math
115 } // namespace ion
116 
117 #endif // ION_MATH_ANGLEUTILS_H_
T Tangent(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Tangent.
Definition: angleutils.h:92
Angle< T > ArcTangent(T v)
Returns the inverse tangent of the given value.
Definition: angleutils.h:58
Angle< T > AngleBetween(const ion::math::Vector< Dimension, T > &a, const ion::math::Vector< Dimension, T > &b)
Returns the angle between two unit vectors.
Definition: angleutils.h:98
T Cosine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Cosine.
Definition: angleutils.h:80
T Dot(const Vector< Dimension, T > &v0, const Vector< Dimension, T > &v1)
Returns the dot (inner) product of two Vectors.
Definition: vectorutils.h:38
A simple class to represent angles.
Definition: angle.h:33
T Radians() const
Get the angle in degrees or radians.
Definition: angle.h:55
Angle< T > ArcSine(T v)
Returns the inverse sine of the given value.
Definition: angleutils.h:47
const T Clamp(const T &val, const T &min_val, const T &max_val)
Clamps a value to lie between a minimum and maximum, inclusive.
Definition: utils.h:204
Vector.
Definition: vector.h:180
Angle< T > ArcCosine(T v)
Returns the inverse cosine of the given value.
Definition: angleutils.h:36
Angle< T > ArcTangent2(T y, T x)
Returns the four-quadrant inverse tangent of the given values.
Definition: angleutils.h:69
static Angle FromRadians(const T &angle)
Create a angle from radians (no conversion).
Definition: angle.h:45
#define DCHECK_LE(val1, val2)
Definition: logging.h:334
T Sine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Sine.
Definition: angleutils.h:86
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Definition: utils.h:42
T LengthSquared(const Vector< Dimension, T > &v)
Returns the square of the length of a Vector.
Definition: vectorutils.h:64