OpenMPCD
Vector2D.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::Vector2D` class template.
4  */
5 
6 #ifndef OPENMPCD_VECTOR2D_HPP
7 #define OPENMPCD_VECTOR2D_HPP
8 
10 #include <OpenMPCD/Exceptions.hpp>
11 #include <OpenMPCD/Scalar.hpp>
12 #include <OpenMPCD/Types.hpp>
13 #include <OpenMPCD/TypeTraits.hpp>
17 
18 #include <boost/static_assert.hpp>
19  #include <boost/type_traits/is_same.hpp>
20  #include <boost/type_traits/remove_cv.hpp>
21 
22 #include <cmath>
23 #include <complex>
24 #include <ostream>
25 
26 namespace OpenMPCD
27 {
28 
29 /**
30  * 2-dimensional vector.
31  *
32  * @tparam T The underlying floating-point type.
33  */
34 template<typename T> class Vector2D
35 {
36 public:
38  ///< The real-value type matching T.
39 
40 public:
41  /**
42  * Constructs a vector from the default values of the `T` type.
43  */
45  Vector2D() : x(), y()
46  {
47  }
48 
49  /**
50  * Constructs a vector from its coordinates.
51  *
52  * @param[in] x_ The x-coordinate.
53  * @param[in] y_ The y-coordinate.
54  */
56  Vector2D(const T x_, const T y_)
57  : x(x_), y(y_)
58  {
59  }
60 
61 public:
62  /**
63  * Returns the x coordinate.
64  */
66  T getX() const
67  {
68  return x;
69  }
70 
71  /**
72  * Returns the y coordinate.
73  */
75  T getY() const
76  {
77  return y;
78  }
79 
80 public:
81  /**
82  * Returns the scalar product of this vector with the given vector.
83  *
84  * The scalar product is defines such that the left-hand-side's components
85  * are complex-conjugated prior to multiplication with the right-hand-side's
86  * components.
87  *
88  * @param[in] rhs The right-hand-side.
89  */
91  const T dot(const Vector2D& rhs) const
92  {
93  return Implementation_Vector2D::Dot<T>::dot(*this, rhs);
94  }
95 
96  /**
97  * Returns the square of the magnitude of this vector.
98  */
101  {
102  return Scalar::getRealPart(dot(*this));
103  }
104 
105  /**
106  * Returns the magnitude of this vector.
107  */
110  {
111  return
114  }
115 
116  /**
117  * Returns the cosine of the angle between this vector and the given one.
118  *
119  * @tparam Result The result type.
120  *
121  * @param[in] rhs The right-hand-side vector.
122  */
124  T getCosineOfAngle(const Vector2D& rhs) const
125  {
126  BOOST_STATIC_ASSERT(TypeTraits<T>::isStandardFloatingPoint);
127 
128  //This function tends to produce results that lie outside the
129  //permissible range [-1, 1] when used with floats.
130  BOOST_STATIC_ASSERT(
131  !boost::is_same<
132  float,
133  typename boost::remove_cv<T>::type
134  >::value);
135 
136  const T divisor =
139  const T ret = dot(rhs) / divisor;
140 
141  OPENMPCD_DEBUG_ASSERT(-1 <= ret);
142  OPENMPCD_DEBUG_ASSERT(ret <= 1);
143 
144  return ret;
145  }
146 
147  /**
148  * Returns the the angle between this vector and the given one.
149  *
150  * @param[in] rhs The right-hand-side vector.
151  */
153  T getAngle(const Vector2D& rhs) const
154  {
155  BOOST_STATIC_ASSERT(TypeTraits<T>::isStandardFloatingPoint);
156 
157  return
159  getCosineOfAngle(rhs));
160  }
161 
162 public:
163  /**
164  * Equality operator.
165  *
166  * @param[in] rhs The right-hand-side vector.
167  */
169  bool operator==(const Vector2D& rhs) const
170  {
171  return x == rhs.x && y == rhs.y;
172  }
173 
174  /**
175  * Inequality operator.
176  *
177  * @param[in] rhs The right-hand-side vector.
178  */
180  bool operator!=(const Vector2D& rhs) const
181  {
182  return !operator==(rhs);
183  }
184 
185 private:
186  T x; ///< The x-coordinate.
187  T y; ///< The y-coordinate.
188 }; //class Vector2D
189 } //namespace OpenMPCD
190 
192 
193 #endif //OPENMPCD_VECTOR2D_HPP
OpenMPCD::Vector2D::getY
OPENMPCD_CUDA_HOST_AND_DEVICE T getY() const
Returns the y coordinate.
Definition: Vector2D.hpp:75
PlatformDetection.hpp
Exceptions.hpp
OpenMPCD::Vector2D::getAngle
OPENMPCD_CUDA_HOST_AND_DEVICE T getAngle(const Vector2D &rhs) const
Returns the the angle between this vector and the given one.
Definition: Vector2D.hpp:153
OpenMPCD::Vector2D::getX
OPENMPCD_CUDA_HOST_AND_DEVICE T getX() const
Returns the x coordinate.
Definition: Vector2D.hpp:66
OpenMPCD::TypeTraits
Contains information on certain types.
Definition: TypeTraits.hpp:46
OPENMPCD_DEBUG_ASSERT
#define OPENMPCD_DEBUG_ASSERT(assertion)
Asserts that the given expression evaluates to true, but only if OPENMPCD_DEBUG is defined.
Definition: OPENMPCD_DEBUG_ASSERT.hpp:88
OpenMPCD::Vector2D::dot
const OPENMPCD_CUDA_HOST_AND_DEVICE T dot(const Vector2D &rhs) const
Returns the scalar product of this vector with the given vector.
Definition: Vector2D.hpp:91
OPENMPCD_CUDA_HOST_AND_DEVICE
#define OPENMPCD_CUDA_HOST_AND_DEVICE
Denotes a function to be callable both from the Host and from a CUDA Device.
Definition: Macros.hpp:15
OpenMPCD::Vector2D::Vector2D
OPENMPCD_CUDA_HOST_AND_DEVICE Vector2D(const T x_, const T y_)
Constructs a vector from its coordinates.
Definition: Vector2D.hpp:56
OpenMPCD::Vector2D::getCosineOfAngle
OPENMPCD_CUDA_HOST_AND_DEVICE T getCosineOfAngle(const Vector2D &rhs) const
Returns the cosine of the angle between this vector and the given one.
Definition: Vector2D.hpp:124
Vector2D_Implementation1.hpp
OpenMPCD::Vector2D::getMagnitudeSquared
OPENMPCD_CUDA_HOST_AND_DEVICE RealType getMagnitudeSquared() const
Returns the square of the magnitude of this vector.
Definition: Vector2D.hpp:100
TypeTraits.hpp
OpenMPCD::Vector2D::RealType
TypeTraits< T >::RealType RealType
The real-value type matching T.
Definition: Vector2D.hpp:37
OpenMPCD::Vector2D
2-dimensional vector.
Definition: Vector2D.hpp:34
Types.hpp
MathematicalFunctions.hpp
Macros.hpp
OpenMPCD::Vector2D::getMagnitude
OPENMPCD_CUDA_HOST_AND_DEVICE RealType getMagnitude() const
Returns the magnitude of this vector.
Definition: Vector2D.hpp:109
Vector2D_Implementation2.hpp
OpenMPCD::Utility::MathematicalFunctions::sqrt
OPENMPCD_CUDA_HOST_AND_DEVICE T sqrt(const T x)
Returns the sqaure root of the argument.
OpenMPCD::Vector2D::operator==
OPENMPCD_CUDA_HOST_AND_DEVICE bool operator==(const Vector2D &rhs) const
Equality operator.
Definition: Vector2D.hpp:169
OpenMPCD::Vector2D::operator!=
OPENMPCD_CUDA_HOST_AND_DEVICE bool operator!=(const Vector2D &rhs) const
Inequality operator.
Definition: Vector2D.hpp:180
OpenMPCD::Scalar::getRealPart
OPENMPCD_CUDA_HOST_AND_DEVICE boost::enable_if< boost::is_floating_point< T >, T >::type getRealPart(const T &val)
Returns the real part of the given value.
Definition: Scalar.hpp:37
OpenMPCD::Vector2D::Vector2D
OPENMPCD_CUDA_HOST_AND_DEVICE Vector2D()
Constructs a vector from the default values of the T type.
Definition: Vector2D.hpp:45
OpenMPCD::Implementation_Vector2D::Dot::dot
static const OPENMPCD_CUDA_HOST_AND_DEVICE T dot(const Vector2D< T > &lhs, const Vector2D< T > &rhs)
Returns the scalar product two vectors.
OpenMPCD::Utility::MathematicalFunctions::acos
OPENMPCD_CUDA_HOST_AND_DEVICE T acos(const T x)
Returns the arc cosine of the argument.
Scalar.hpp