Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Velocity.hpp
Go to the documentation of this file.
1 // Copyright © 2020-2024 Alexandre Coderre-Chabot
2 //
3 // This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4 // models, and units of measure for scientific computing.
5 //
6 // Physical Quantities is hosted at:
7 // https://github.com/acodcha/phq
8 //
9 // Physical Quantities is licensed under the MIT License:
10 // https://mit-license.org
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13 // associated documentation files (the "Software"), to deal in the Software without restriction,
14 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
15 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // - The above copyright notice and this permission notice shall be included in all copies or
18 // substantial portions of the Software.
19 // - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 #ifndef PHQ_VELOCITY_HPP
26 #define PHQ_VELOCITY_HPP
27 
28 #include <array>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 
33 #include "Angle.hpp"
34 #include "DimensionalVector.hpp"
35 #include "Direction.hpp"
36 #include "Displacement.hpp"
37 #include "Frequency.hpp"
38 #include "PlanarVelocity.hpp"
39 #include "Speed.hpp"
40 #include "Time.hpp"
41 #include "Unit/Speed.hpp"
42 #include "Vector.hpp"
43 
44 namespace PhQ {
45 
46 // Forward declaration for class PhQ::Velocity.
47 template <typename NumericType>
48 class Acceleration;
49 
50 /// \brief Three-dimensional Euclidean velocity vector. Contains three components in Cartesian
51 /// coordinates: x, y, and z. For a two-dimensional Euclidean velocity vector in the XY plane, see
52 /// PhQ::PlanarVelocity. For scalar velocity components or for the magnitude of a velocity vector,
53 /// see PhQ::Speed.
54 template <typename NumericType = double>
55 class Velocity : public DimensionalVector<Unit::Speed, NumericType> {
56 public:
57  /// \brief Default constructor. Constructs a velocity vector with an uninitialized value.
58  Velocity() = default;
59 
60  /// \brief Constructor. Constructs a velocity vector with a given value expressed in a given speed
61  /// unit.
63  : DimensionalVector<Unit::Speed, NumericType>(value, unit) {}
64 
65  /// \brief Constructor. Constructs a velocity vector from a given set of speed components.
67  : Velocity<NumericType>({x.Value(), y.Value(), z.Value()}) {}
68 
69  /// \brief Constructor. Constructs a velocity vector from a given speed and direction.
70  constexpr Velocity(const Speed<NumericType>& speed, const Direction<NumericType>& direction)
71  : Velocity<NumericType>(speed.Value() * direction.Value()) {}
72 
73  /// \brief Constructor. Constructs a velocity vector from a given planar velocity vector in the XY
74  /// plane. This velocity vector's z-component is initialized to zero.
75  explicit constexpr Velocity(const PlanarVelocity<NumericType>& planar_velocity)
76  : Velocity<NumericType>(Vector<NumericType>{planar_velocity.Value()}) {}
77 
78  /// \brief Constructor. Constructs a velocity vector from a given displacement vector and time
79  /// using the definition of velocity.
80  constexpr Velocity(const Displacement<NumericType>& displacement, const Time<NumericType>& time)
81  : Velocity<NumericType>(displacement.Value() / time.Value()) {}
82 
83  /// \brief Constructor. Constructs a velocity vector from a given displacement vector and
84  /// frequency using the definition of velocity.
85  constexpr Velocity(
86  const Displacement<NumericType>& displacement, const Frequency<NumericType>& frequency)
87  : Velocity<NumericType>(displacement.Value() * frequency.Value()) {}
88 
89  /// \brief Constructor. Constructs a velocity vector from a given acceleration vector and time
90  /// using the definition of acceleration.
91  constexpr Velocity(const Acceleration<NumericType>& acceleration, const Time<NumericType>& time);
92 
93  /// \brief Constructor. Constructs a velocity vector from a given acceleration vector and
94  /// frequency using the definition of acceleration.
95  constexpr Velocity(
96  const Acceleration<NumericType>& acceleration, const Frequency<NumericType>& frequency);
97 
98  /// \brief Destructor. Destroys this velocity vector.
99  ~Velocity() noexcept = default;
100 
101  /// \brief Copy constructor. Constructs a velocity vector by copying another one.
102  constexpr Velocity(const Velocity<NumericType>& other) = default;
103 
104  /// \brief Copy constructor. Constructs a velocity by copying another one.
105  template <typename OtherNumericType>
106  explicit constexpr Velocity(const Velocity<OtherNumericType>& other)
107  : Velocity(static_cast<Vector<NumericType>>(other.Value())) {}
108 
109  /// \brief Move constructor. Constructs a velocity vector by moving another one.
110  constexpr Velocity(Velocity<NumericType>&& other) noexcept = default;
111 
112  /// \brief Copy assignment operator. Assigns this velocity vector by copying another one.
113  constexpr Velocity<NumericType>& operator=(const Velocity<NumericType>& other) = default;
114 
115  /// \brief Copy assignment operator. Assigns this velocity by copying another one.
116  template <typename OtherNumericType>
118  this->value = static_cast<Vector<NumericType>>(other.Value());
119  return *this;
120  }
121 
122  /// \brief Move assignment operator. Assigns this velocity vector by moving another one.
123  constexpr Velocity<NumericType>& operator=(Velocity<NumericType>&& other) noexcept = default;
124 
125  /// \brief Statically creates a velocity vector of zero.
126  [[nodiscard]] static constexpr Velocity<NumericType> Zero() {
128  }
129 
130  /// \brief Statically creates a velocity vector from the given x, y, and z Cartesian components
131  /// expressed in a given speed unit.
132  template <Unit::Speed Unit>
133  [[nodiscard]] static constexpr Velocity<NumericType> Create(
134  const NumericType x, const NumericType y, const NumericType z) {
135  return Velocity<NumericType>{
136  ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(Vector<NumericType>{x, y, z})};
137  }
138 
139  /// \brief Statically creates a velocity vector from the given x, y, and z Cartesian components
140  /// expressed in a given speed unit.
141  template <Unit::Speed Unit>
142  [[nodiscard]] static constexpr Velocity<NumericType> Create(
143  const std::array<NumericType, 3>& x_y_z) {
144  return Velocity<NumericType>{
145  ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(Vector<NumericType>{x_y_z})};
146  }
147 
148  /// \brief Statically creates a velocity vector with a given value expressed in a given speed
149  /// unit.
150  template <Unit::Speed Unit>
151  [[nodiscard]] static constexpr Velocity<NumericType> Create(const Vector<NumericType>& value) {
152  return Velocity<NumericType>{
153  ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(value)};
154  }
155 
156  /// \brief Returns the x Cartesian component of this velocity vector.
157  [[nodiscard]] constexpr Speed<NumericType> x() const noexcept {
158  return Speed<NumericType>{this->value.x()};
159  }
160 
161  /// \brief Returns the y Cartesian component of this velocity vector.
162  [[nodiscard]] constexpr Speed<NumericType> y() const noexcept {
163  return Speed<NumericType>{this->value.y()};
164  }
165 
166  /// \brief Returns the z Cartesian component of this velocity vector.
167  [[nodiscard]] constexpr Speed<NumericType> z() const noexcept {
168  return Speed<NumericType>{this->value.z()};
169  }
170 
171  /// \brief Returns the magnitude of this velocity vector.
172  [[nodiscard]] Speed<NumericType> Magnitude() const {
173  return Speed<NumericType>{this->value.Magnitude()};
174  }
175 
176  /// \brief Returns the direction of this velocity vector.
177  [[nodiscard]] PhQ::Direction<NumericType> Direction() const {
178  return this->value.Direction();
179  }
180 
181  /// \brief Returns the angle between this velocity vector and another one.
182  [[nodiscard]] PhQ::Angle<NumericType> Angle(const Velocity<NumericType>& velocity) const {
183  return PhQ::Angle<NumericType>{*this, velocity};
184  }
185 
186  constexpr Velocity<NumericType> operator+(const Velocity<NumericType>& velocity) const {
187  return Velocity<NumericType>{this->value + velocity.value};
188  }
189 
190  constexpr Velocity<NumericType> operator-(const Velocity<NumericType>& velocity) const {
191  return Velocity<NumericType>{this->value - velocity.value};
192  }
193 
194  constexpr Velocity<NumericType> operator*(const NumericType number) const {
195  return Velocity<NumericType>{this->value * number};
196  }
197 
199  return Displacement<NumericType>{*this, time};
200  }
201 
202  constexpr Acceleration<NumericType> operator*(const Frequency<NumericType>& frequency) const;
203 
204  constexpr Velocity<NumericType> operator/(const NumericType number) const {
205  return Velocity<NumericType>{this->value / number};
206  }
207 
208  constexpr Acceleration<NumericType> operator/(const Time<NumericType>& time) const;
209 
210  constexpr Displacement<NumericType> operator/(const Frequency<NumericType>& frequency) const {
211  return Displacement<NumericType>{*this, frequency};
212  }
213 
214  constexpr void operator+=(const Velocity<NumericType>& velocity) noexcept {
215  this->value += velocity.value;
216  }
217 
218  constexpr void operator-=(const Velocity<NumericType>& velocity) noexcept {
219  this->value -= velocity.value;
220  }
221 
222  constexpr void operator*=(const NumericType number) noexcept {
223  this->value *= number;
224  }
225 
226  constexpr void operator/=(const NumericType number) noexcept {
227  this->value /= number;
228  }
229 
230 private:
231  /// \brief Constructor. Constructs a velocity vector with a given value expressed in the standard
232  /// speed unit.
233  explicit constexpr Velocity(const Vector<NumericType>& value)
234  : DimensionalVector<Unit::Speed, NumericType>(value) {}
235 };
236 
237 template <typename NumericType>
238 inline constexpr bool operator==(
239  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
240  return left.Value() == right.Value();
241 }
242 
243 template <typename NumericType>
244 inline constexpr bool operator!=(
245  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
246  return left.Value() != right.Value();
247 }
248 
249 template <typename NumericType>
250 inline constexpr bool operator<(
251  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
252  return left.Value() < right.Value();
253 }
254 
255 template <typename NumericType>
256 inline constexpr bool operator>(
257  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
258  return left.Value() > right.Value();
259 }
260 
261 template <typename NumericType>
262 inline constexpr bool operator<=(
263  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
264  return left.Value() <= right.Value();
265 }
266 
267 template <typename NumericType>
268 inline constexpr bool operator>=(
269  const Velocity<NumericType>& left, const Velocity<NumericType>& right) noexcept {
270  return left.Value() >= right.Value();
271 }
272 
273 template <typename NumericType>
274 inline std::ostream& operator<<(std::ostream& stream, const Velocity<NumericType>& velocity) {
275  stream << velocity.Print();
276  return stream;
277 }
278 
279 template <typename NumericType>
281  const NumericType number, const Velocity<NumericType>& velocity) {
282  return velocity * number;
283 }
284 
285 template <typename NumericType>
287  : Direction<NumericType>(velocity.Value()) {}
288 
289 template <typename NumericType>
291  const Velocity<NumericType>& velocity1, const Velocity<NumericType>& velocity2)
292  : Angle<NumericType>(velocity1.Value(), velocity2.Value()) {}
293 
294 template <typename NumericType>
296  const Velocity<NumericType>& velocity, const Time<NumericType>& time)
297  : Displacement<NumericType>(velocity.Value() * time.Value()) {}
298 
299 template <typename NumericType>
301  const Velocity<NumericType>& velocity, const Frequency<NumericType>& frequency)
302  : Displacement<NumericType>(velocity.Value() / frequency.Value()) {}
303 
304 template <typename NumericType>
306  : PlanarVelocity(PlanarVector<NumericType>{velocity.Value()}) {}
307 
308 template <typename NumericType>
310  const Speed<NumericType>& speed) const {
311  return Velocity<NumericType>{speed, *this};
312 }
313 
314 template <typename NumericType>
316  const Velocity<NumericType>& velocity) const {
317  return Displacement<NumericType>{velocity, *this};
318 }
319 
320 template <typename NumericType>
321 inline constexpr Velocity<NumericType> Speed<NumericType>::operator*(
322  const Direction<NumericType>& direction) const {
323  return Velocity<NumericType>{*this, direction};
324 }
325 
326 template <typename NumericType>
327 inline constexpr Velocity<NumericType> Frequency<NumericType>::operator*(
328  const Displacement<NumericType>& displacement) const {
329  return Velocity<NumericType>{displacement, *this};
330 }
331 
332 template <typename NumericType>
334  const Frequency<NumericType>& frequency) const {
335  return Velocity<NumericType>{*this, frequency};
336 }
337 
338 template <typename NumericType>
340  const Time<NumericType>& time) const {
341  return Velocity<NumericType>{*this, time};
342 }
343 
344 } // namespace PhQ
345 
346 namespace std {
347 
348 template <typename NumericType>
349 struct hash<PhQ::Velocity<NumericType>> {
350  inline size_t operator()(const PhQ::Velocity<NumericType>& velocity) const {
351  return hash<PhQ::Vector<NumericType>>()(velocity.Value());
352  }
353 };
354 
355 } // namespace std
356 
357 #endif // PHQ_VELOCITY_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
Plane angle between two lines or dihedral angle between two planes.
Definition: Angle.hpp:130
Angle()=default
Default constructor. Constructs an angle with an uninitialized value.
constexpr const PhQ::PlanarVector< NumericType > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
Abstract base class that represents any dimensional vector physical quantity. Such a physical quantit...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
static constexpr Unit::Speed Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
PhQ::Vector< double > value
Value of this physical quantity expressed in its standard unit of measure.
constexpr const PhQ::Vector< double > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
Three-dimensional Euclidean direction vector. Contains three components in Cartesian coordinates: x,...
Definition: Direction.hpp:115
constexpr Direction()
Default constructor. Initializes a direction to the zero vector.
Definition: Direction.hpp:118
constexpr Acceleration< NumericType > operator*(const ScalarAcceleration< NumericType > &scalar_acceleration) const
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
constexpr Displacement< NumericType > operator*(const NumericType number) const
constexpr Displacement< NumericType > operator/(const NumericType number) const
Displacement()=default
Default constructor. Constructs a displacement vector with an uninitialized value.
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
constexpr Frequency< NumericType > operator*(const NumericType number) const
Definition: Frequency.hpp:154
Two-dimensional Euclidean vector in the XY plane. Contains two components in Cartesian coordinates: x...
Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in Cartesian coord...
PlanarVelocity()=default
Default constructor. Constructs a planar velocity vector with an uninitialized value.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
constexpr Speed< NumericType > operator*(const NumericType number) const
Definition: Speed.hpp:205
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
constexpr Time< NumericType > operator*(const NumericType number) const
Definition: Time.hpp:278
Three-dimensional Euclidean vector. Contains three components in Cartesian coordinates: x,...
Definition: Vector.hpp:60
NumericType Magnitude() const noexcept
Returns the magnitude (also known as the L2 norm) of this three-dimensional vector.
Definition: Vector.hpp:209
static constexpr Vector< NumericType > Zero()
Statically creates a three-dimensional vector with its x, y, and z Cartesian components initialized t...
Definition: Vector.hpp:126
constexpr NumericType x() const noexcept
Returns this three-dimensional vector's x Cartesian component.
Definition: Vector.hpp:139
PhQ::Direction< NumericType > Direction() const
Returns the direction of this three-dimensional vector.
Definition: Direction.hpp:377
constexpr NumericType y() const noexcept
Returns this three-dimensional vector's y Cartesian component.
Definition: Vector.hpp:144
constexpr NumericType z() const noexcept
Returns this three-dimensional vector's z Cartesian component.
Definition: Vector.hpp:149
Three-dimensional Euclidean velocity vector. Contains three components in Cartesian coordinates: x,...
Definition: Velocity.hpp:55
constexpr Velocity(const Displacement< NumericType > &displacement, const Time< NumericType > &time)
Constructor. Constructs a velocity vector from a given displacement vector and time using the definit...
Definition: Velocity.hpp:80
Velocity(const Vector< NumericType > &value, const Unit::Speed unit)
Constructor. Constructs a velocity vector with a given value expressed in a given speed unit.
Definition: Velocity.hpp:62
Velocity()=default
Default constructor. Constructs a velocity vector with an uninitialized value.
constexpr Velocity(const Displacement< NumericType > &displacement, const Frequency< NumericType > &frequency)
Constructor. Constructs a velocity vector from a given displacement vector and frequency using the de...
Definition: Velocity.hpp:85
static constexpr Velocity< NumericType > Create(const Vector< NumericType > &value)
Statically creates a velocity vector with a given value expressed in a given speed unit.
Definition: Velocity.hpp:151
constexpr void operator-=(const Velocity< NumericType > &velocity) noexcept
Definition: Velocity.hpp:218
constexpr Velocity< NumericType > operator/(const NumericType number) const
Definition: Velocity.hpp:204
constexpr Velocity< NumericType > & operator=(Velocity< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this velocity vector by moving another one.
constexpr Velocity< NumericType > & operator=(const Velocity< NumericType > &other)=default
Copy assignment operator. Assigns this velocity vector by copying another one.
constexpr Velocity(Velocity< NumericType > &&other) noexcept=default
Move constructor. Constructs a velocity vector by moving another one.
constexpr Velocity< NumericType > operator*(const NumericType number) const
Definition: Velocity.hpp:194
constexpr Velocity(const PlanarVelocity< NumericType > &planar_velocity)
Constructor. Constructs a velocity vector from a given planar velocity vector in the XY plane....
Definition: Velocity.hpp:75
PhQ::Angle< NumericType > Angle(const Velocity< NumericType > &velocity) const
Returns the angle between this velocity vector and another one.
Definition: Velocity.hpp:182
constexpr Speed< NumericType > z() const noexcept
Returns the z Cartesian component of this velocity vector.
Definition: Velocity.hpp:167
static constexpr Velocity< NumericType > Create(const NumericType x, const NumericType y, const NumericType z)
Statically creates a velocity vector from the given x, y, and z Cartesian components expressed in a g...
Definition: Velocity.hpp:133
Speed< NumericType > Magnitude() const
Returns the magnitude of this velocity vector.
Definition: Velocity.hpp:172
constexpr Displacement< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition: Velocity.hpp:210
~Velocity() noexcept=default
Destructor. Destroys this velocity vector.
constexpr Velocity< NumericType > & operator=(const Velocity< OtherNumericType > &other)
Copy assignment operator. Assigns this velocity by copying another one.
Definition: Velocity.hpp:117
Velocity(const Speed< NumericType > &x, const Speed< NumericType > &y, const Speed< NumericType > &z)
Constructor. Constructs a velocity vector from a given set of speed components.
Definition: Velocity.hpp:66
constexpr Velocity< NumericType > operator+(const Velocity< NumericType > &velocity) const
Definition: Velocity.hpp:186
constexpr Velocity(const Speed< NumericType > &speed, const Direction< NumericType > &direction)
Constructor. Constructs a velocity vector from a given speed and direction.
Definition: Velocity.hpp:70
constexpr void operator/=(const NumericType number) noexcept
Definition: Velocity.hpp:226
constexpr Velocity< NumericType > operator-(const Velocity< NumericType > &velocity) const
Definition: Velocity.hpp:190
constexpr Speed< NumericType > y() const noexcept
Returns the y Cartesian component of this velocity vector.
Definition: Velocity.hpp:162
PhQ::Direction< NumericType > Direction() const
Returns the direction of this velocity vector.
Definition: Velocity.hpp:177
constexpr Displacement< NumericType > operator*(const Time< NumericType > &time) const
Definition: Velocity.hpp:198
constexpr Velocity(const Vector< NumericType > &value)
Constructor. Constructs a velocity vector with a given value expressed in the standard speed unit.
Definition: Velocity.hpp:233
constexpr Speed< NumericType > x() const noexcept
Returns the x Cartesian component of this velocity vector.
Definition: Velocity.hpp:157
static constexpr Velocity< NumericType > Zero()
Statically creates a velocity vector of zero.
Definition: Velocity.hpp:126
constexpr void operator*=(const NumericType number) noexcept
Definition: Velocity.hpp:222
static constexpr Velocity< NumericType > Create(const std::array< NumericType, 3 > &x_y_z)
Statically creates a velocity vector from the given x, y, and z Cartesian components expressed in a g...
Definition: Velocity.hpp:142
constexpr void operator+=(const Velocity< NumericType > &velocity) noexcept
Definition: Velocity.hpp:214
Acceleration
Acceleration units.
Speed
Speed units.
Definition: Speed.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.
constexpr bool operator<(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator<=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator==(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)