Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
PlanarVelocity.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_PLANAR_VELOCITY_HPP
26 #define PHQ_PLANAR_VELOCITY_HPP
27 
28 #include <array>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 
33 #include "Angle.hpp"
35 #include "Frequency.hpp"
36 #include "PlanarDirection.hpp"
37 #include "PlanarDisplacement.hpp"
38 #include "PlanarVector.hpp"
39 #include "Speed.hpp"
40 #include "Time.hpp"
41 #include "Unit/Speed.hpp"
42 
43 namespace PhQ {
44 
45 // Forward declaration for class PhQ::PlanarVelocity.
46 template <typename NumericType>
47 class PlanarAcceleration;
48 
49 /// \brief Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in
50 /// Cartesian coordinates: x and y. For a three-dimensional Euclidean velocity vector, see
51 /// PhQ::Velocity. For scalar velocity components or for the magnitude of a velocity vector, see
52 /// PhQ::Speed.
53 template <typename NumericType = double>
54 class PlanarVelocity : public DimensionalPlanarVector<Unit::Speed, NumericType> {
55 public:
56  /// \brief Default constructor. Constructs a planar velocity vector with an uninitialized value.
57  PlanarVelocity() = default;
58 
59  /// \brief Constructor. Constructs a planar velocity vector with a given value expressed in a
60  /// given speed unit.
62  : DimensionalPlanarVector<Unit::Speed, NumericType>(value, unit) {}
63 
64  /// \brief Constructor. Constructs a planar velocity vector from a given set of speed components.
66  : PlanarVelocity<NumericType>({x.Value(), y.Value()}) {}
67 
68  /// \brief Constructor. Constructs a planar velocity vector from a given speed and planar
69  /// direction.
70  constexpr PlanarVelocity(
71  const Speed<NumericType>& speed, const PlanarDirection<NumericType>& planar_direction)
72  : PlanarVelocity<NumericType>(speed.Value() * planar_direction.Value()) {}
73 
74  /// \brief Constructor. Constructs a planar velocity vector from a given velocity vector by
75  /// projecting the velocity vector onto the XY plane.
76  explicit constexpr PlanarVelocity(const Velocity<NumericType>& velocity);
77 
78  /// \brief Constructor. Constructs a planar velocity vector from a given planar displacement
79  /// vector and time using the definition of velocity.
80  constexpr PlanarVelocity(
81  const PlanarDisplacement<NumericType>& planar_displacement, const Time<NumericType>& time)
82  : PlanarVelocity<NumericType>(planar_displacement.Value() / time.Value()) {}
83 
84  /// \brief Constructor. Constructs a planar velocity vector from a given planar displacement
85  /// vector and frequency using the definition of velocity.
86  constexpr PlanarVelocity(const PlanarDisplacement<NumericType>& planar_displacement,
87  const Frequency<NumericType>& frequency)
88  : PlanarVelocity<NumericType>(planar_displacement.Value() * frequency.Value()) {}
89 
90  /// \brief Constructor. Constructs a planar velocity vector from a given planar acceleration
91  /// vector and time using the definition of acceleration.
92  constexpr PlanarVelocity(
93  const PlanarAcceleration<NumericType>& planar_acceleration, const Time<NumericType>& time);
94 
95  /// \brief Constructor. Constructs a planar velocity vector from a given planar acceleration
96  /// vector and frequency using the definition of acceleration.
97  constexpr PlanarVelocity(const PlanarAcceleration<NumericType>& planar_acceleration,
98  const Frequency<NumericType>& frequency);
99 
100  /// \brief Destructor. Destroys this planar velocity vector.
101  ~PlanarVelocity() noexcept = default;
102 
103  /// \brief Copy constructor. Constructs a planar velocity vector by copying another one.
104  constexpr PlanarVelocity(const PlanarVelocity<NumericType>& other) = default;
105 
106  /// \brief Copy constructor. Constructs a planar velocity by copying another one.
107  template <typename OtherNumericType>
108  explicit constexpr PlanarVelocity(const PlanarVelocity<OtherNumericType>& other)
109  : PlanarVelocity(static_cast<PlanarVector<NumericType>>(other.Value())) {}
110 
111  /// \brief Move constructor. Constructs a planar velocity vector by moving another one.
112  constexpr PlanarVelocity(PlanarVelocity<NumericType>&& other) noexcept = default;
113 
114  /// \brief Copy assignment operator. Assigns this planar velocity vector by copying another one.
116  const PlanarVelocity<NumericType>& other) = default;
117 
118  /// \brief Copy assignment operator. Assigns this planar velocity by copying another one.
119  template <typename OtherNumericType>
121  this->value = static_cast<PlanarVector<NumericType>>(other.Value());
122  return *this;
123  }
124 
125  /// \brief Move assignment operator. Assigns this planar velocity vector by moving another one.
127  PlanarVelocity<NumericType>&& other) noexcept = default;
128 
129  /// \brief Statically creates a planar velocity vector of zero.
130  [[nodiscard]] static constexpr PlanarVelocity<NumericType> Zero() {
132  }
133 
134  /// \brief Statically creates a planar velocity vector from the given x and y Cartesian
135  /// components expressed in a given speed unit.
136  template <Unit::Speed Unit>
137  [[nodiscard]] static constexpr PlanarVelocity<NumericType> Create(
138  const NumericType x, const NumericType y) {
139  return PlanarVelocity<NumericType>{ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(
141  }
142 
143  /// \brief Statically creates a planar velocity vector from the given x and y Cartesian
144  /// components expressed in a given speed unit.
145  template <Unit::Speed Unit>
146  [[nodiscard]] static constexpr PlanarVelocity<NumericType> Create(
147  const std::array<NumericType, 2>& x_y) {
148  return PlanarVelocity<NumericType>{ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(
150  }
151 
152  /// \brief Statically creates a planar velocity vector with a given value expressed in a given
153  /// speed unit.
154  template <Unit::Speed Unit>
155  [[nodiscard]] static constexpr PlanarVelocity<NumericType> Create(
158  ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(value)};
159  }
160 
161  /// \brief Returns the x Cartesian component of this planar velocity vector.
162  [[nodiscard]] constexpr Speed<NumericType> x() const noexcept {
163  return Speed<NumericType>{this->value.x()};
164  }
165 
166  /// \brief Returns the y Cartesian component of this planar velocity vector.
167  [[nodiscard]] constexpr Speed<NumericType> y() const noexcept {
168  return Speed<NumericType>{this->value.y()};
169  }
170 
171  /// \brief Returns the magnitude of this planar velocity vector.
172  [[nodiscard]] Speed<NumericType> Magnitude() const {
173  return Speed<NumericType>{this->value.Magnitude()};
174  }
175 
176  /// \brief Returns the planar direction of this planar velocity vector.
178  return this->value.PlanarDirection();
179  }
180 
181  /// \brief Returns the angle between this planar velocity vector and another one.
183  const PlanarVelocity<NumericType>& planar_velocity) const {
184  return PhQ::Angle<NumericType>{*this, planar_velocity};
185  }
186 
188  const PlanarVelocity<NumericType>& planar_velocity) const {
189  return PlanarVelocity<NumericType>{this->value + planar_velocity.value};
190  }
191 
193  const PlanarVelocity<NumericType>& planar_velocity) const {
194  return PlanarVelocity<NumericType>{this->value - planar_velocity.value};
195  }
196 
197  constexpr PlanarVelocity<NumericType> operator*(const NumericType number) const {
198  return PlanarVelocity<NumericType>{this->value * number};
199  }
200 
202  return PlanarDisplacement<NumericType>{*this, time};
203  }
204 
206  const Frequency<NumericType>& frequency) const;
207 
208  constexpr PlanarVelocity<NumericType> operator/(const NumericType number) const {
209  return PlanarVelocity<NumericType>{this->value / number};
210  }
211 
212  constexpr PlanarAcceleration<NumericType> operator/(const Time<NumericType>& time) const;
213 
215  const Frequency<NumericType>& frequency) const {
216  return PlanarDisplacement<NumericType>{*this, frequency};
217  }
218 
219  constexpr void operator+=(const PlanarVelocity<NumericType>& planar_velocity) noexcept {
220  this->value += planar_velocity.value;
221  }
222 
223  constexpr void operator-=(const PlanarVelocity<NumericType>& planar_velocity) noexcept {
224  this->value -= planar_velocity.value;
225  }
226 
227  constexpr void operator*=(const NumericType number) noexcept {
228  this->value *= number;
229  }
230 
231  constexpr void operator/=(const NumericType number) noexcept {
232  this->value /= number;
233  }
234 
235 private:
236  /// \brief Constructor. Constructs a planar velocity vector with a given value expressed in the
237  /// standard speed unit.
238  explicit constexpr PlanarVelocity(const PlanarVector<NumericType>& value)
239  : DimensionalPlanarVector<Unit::Speed, NumericType>(value) {}
240 };
241 
242 template <typename NumericType>
243 inline constexpr bool operator==(
244  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
245  return left.Value() == right.Value();
246 }
247 
248 template <typename NumericType>
249 inline constexpr bool operator!=(
250  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
251  return left.Value() != right.Value();
252 }
253 
254 template <typename NumericType>
255 inline constexpr bool operator<(
256  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
257  return left.Value() < right.Value();
258 }
259 
260 template <typename NumericType>
261 inline constexpr bool operator>(
262  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
263  return left.Value() > right.Value();
264 }
265 
266 template <typename NumericType>
267 inline constexpr bool operator<=(
268  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
269  return left.Value() <= right.Value();
270 }
271 
272 template <typename NumericType>
273 inline constexpr bool operator>=(
274  const PlanarVelocity<NumericType>& left, const PlanarVelocity<NumericType>& right) noexcept {
275  return left.Value() >= right.Value();
276 }
277 
278 template <typename NumericType>
279 inline std::ostream& operator<<(
280  std::ostream& stream, const PlanarVelocity<NumericType>& planar_velocity) {
281  stream << planar_velocity.Print();
282  return stream;
283 }
284 
285 template <typename NumericType>
287  const NumericType number, const PlanarVelocity<NumericType>& planar_velocity) {
288  return planar_velocity * number;
289 }
290 
291 template <typename NumericType>
293  const PlanarVelocity<NumericType>& planar_velocity)
294  : PlanarDirection<NumericType>(planar_velocity.Value()) {}
295 
296 template <typename NumericType>
297 inline Angle<NumericType>::Angle(const PlanarVelocity<NumericType>& planar_velocity_1,
298  const PlanarVelocity<NumericType>& planar_velocity_2)
299  : Angle<NumericType>(planar_velocity_1.Value(), planar_velocity_2.Value()) {}
300 
301 template <typename NumericType>
303  const PlanarVelocity<NumericType>& planar_velocity, const Time<NumericType>& time)
304  : PlanarDisplacement<NumericType>(planar_velocity.Value() * time.Value()) {}
305 
306 template <typename NumericType>
308  const PlanarVelocity<NumericType>& planar_velocity, const Frequency<NumericType>& frequency)
309  : PlanarDisplacement<NumericType>(planar_velocity.Value() / frequency.Value()) {}
310 
311 template <typename NumericType>
313  const Speed<NumericType>& speed) const {
314  return PlanarVelocity<NumericType>{speed, *this};
315 }
316 
317 template <typename NumericType>
319  const PlanarVelocity<NumericType>& planar_velocity) const {
320  return PlanarDisplacement<NumericType>{planar_velocity, *this};
321 }
322 
323 template <typename NumericType>
324 inline constexpr PlanarVelocity<NumericType> Speed<NumericType>::operator*(
325  const PlanarDirection<NumericType>& planar_direction) const {
326  return PlanarVelocity<NumericType>{*this, planar_direction};
327 }
328 
329 template <typename NumericType>
330 inline constexpr PlanarVelocity<NumericType> Frequency<NumericType>::operator*(
331  const PlanarDisplacement<NumericType>& planar_displacement) const {
332  return PlanarVelocity<NumericType>{planar_displacement, *this};
333 }
334 
335 template <typename NumericType>
337  const Frequency<NumericType>& frequency) const {
338  return PlanarVelocity<NumericType>{*this, frequency};
339 }
340 
341 template <typename NumericType>
343  const Time<NumericType>& time) const {
344  return PlanarVelocity<NumericType>{*this, time};
345 }
346 
347 } // namespace PhQ
348 
349 namespace std {
350 
351 template <typename NumericType>
352 struct hash<PhQ::PlanarVelocity<NumericType>> {
353  inline size_t operator()(const PhQ::PlanarVelocity<NumericType>& planar_velocity) const {
354  return hash<PhQ::PlanarVector<NumericType>>()(planar_velocity.Value());
355  }
356 };
357 
358 } // namespace std
359 
360 #endif // PHQ_PLANAR_VELOCITY_HPP
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.
Abstract base class that represents any dimensional planar vector physical quantity....
PhQ::PlanarVector< double > value
Value of this physical quantity expressed in its standard unit of measure.
constexpr const PhQ::PlanarVector< double > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr Unit::Speed Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
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 acceleration vector in the XY plane. Contains two components in Cartesian c...
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
constexpr PlanarAcceleration< NumericType > operator*(const ScalarAcceleration< NumericType > &scalar_acceleration) const
constexpr PlanarDirection()
Default constructor. Initializes a planar direction to the zero planar vector.
Two-dimensional Euclidean displacement vector in the XY plane. Contains two components in Cartesian c...
constexpr PlanarDisplacement< NumericType > operator*(const NumericType number) const
constexpr PlanarDisplacement< NumericType > operator/(const NumericType number) const
PlanarDisplacement()=default
Default constructor. Constructs a planar displacement vector with an uninitialized value.
Two-dimensional Euclidean vector in the XY plane. Contains two components in Cartesian coordinates: x...
constexpr NumericType x() const noexcept
Returns this two-dimensional planar vector's x Cartesian component.
static constexpr PlanarVector< NumericType > Zero()
Statically creates a two-dimensional planar vector with its x and y Cartesian components initialized ...
PhQ::PlanarDirection< NumericType > PlanarDirection() const
Returns the planar direction of this two-dimensional planar vector.
NumericType Magnitude() const noexcept
Returns the magnitude (also known as the L2 norm) of this two-dimensional planar vector.
constexpr NumericType y() const noexcept
Returns this two-dimensional planar vector's y Cartesian component.
Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in Cartesian coord...
PlanarVelocity(const Speed< NumericType > &x, const Speed< NumericType > &y)
Constructor. Constructs a planar velocity vector from a given set of speed components.
static constexpr PlanarVelocity< NumericType > Create(const std::array< NumericType, 2 > &x_y)
Statically creates a planar velocity vector from the given x and y Cartesian components expressed in ...
PhQ::Angle< NumericType > Angle(const PlanarVelocity< NumericType > &planar_velocity) const
Returns the angle between this planar velocity vector and another one.
constexpr PlanarVelocity< NumericType > operator/(const NumericType number) const
static constexpr PlanarVelocity< NumericType > Create(const PlanarVector< NumericType > &value)
Statically creates a planar velocity vector with a given value expressed in a given speed unit.
constexpr PlanarDisplacement< NumericType > operator*(const Time< NumericType > &time) const
constexpr Speed< NumericType > x() const noexcept
Returns the x Cartesian component of this planar velocity vector.
~PlanarVelocity() noexcept=default
Destructor. Destroys this planar velocity vector.
constexpr void operator-=(const PlanarVelocity< NumericType > &planar_velocity) noexcept
Speed< NumericType > Magnitude() const
Returns the magnitude of this planar velocity vector.
constexpr PlanarVelocity< NumericType > operator-(const PlanarVelocity< NumericType > &planar_velocity) const
constexpr void operator+=(const PlanarVelocity< NumericType > &planar_velocity) noexcept
constexpr PlanarVelocity(PlanarVelocity< NumericType > &&other) noexcept=default
Move constructor. Constructs a planar velocity vector by moving another one.
constexpr PlanarVelocity< NumericType > & operator=(const PlanarVelocity< OtherNumericType > &other)
Copy assignment operator. Assigns this planar velocity by copying another one.
static constexpr PlanarVelocity< NumericType > Zero()
Statically creates a planar velocity vector of zero.
constexpr PlanarVelocity< NumericType > & operator=(const PlanarVelocity< NumericType > &other)=default
Copy assignment operator. Assigns this planar velocity vector by copying another one.
constexpr void operator*=(const NumericType number) noexcept
constexpr PlanarVelocity(const Speed< NumericType > &speed, const PlanarDirection< NumericType > &planar_direction)
Constructor. Constructs a planar velocity vector from a given speed and planar direction.
constexpr PlanarDisplacement< NumericType > operator/(const Frequency< NumericType > &frequency) const
constexpr PlanarVelocity(const PlanarDisplacement< NumericType > &planar_displacement, const Time< NumericType > &time)
Constructor. Constructs a planar velocity vector from a given planar displacement vector and time usi...
constexpr PlanarVelocity< NumericType > operator*(const NumericType number) const
constexpr PlanarVelocity(const PlanarDisplacement< NumericType > &planar_displacement, const Frequency< NumericType > &frequency)
Constructor. Constructs a planar velocity vector from a given planar displacement vector and frequenc...
constexpr Speed< NumericType > y() const noexcept
Returns the y Cartesian component of this planar velocity vector.
PhQ::PlanarDirection< NumericType > PlanarDirection() const
Returns the planar direction of this planar velocity vector.
PlanarVelocity(const PlanarVector< NumericType > &value, const Unit::Speed unit)
Constructor. Constructs a planar velocity vector with a given value expressed in a given speed unit.
constexpr PlanarVelocity(const PlanarVector< NumericType > &value)
Constructor. Constructs a planar velocity vector with a given value expressed in the standard speed u...
constexpr PlanarVelocity< NumericType > & operator=(PlanarVelocity< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this planar velocity vector by moving another one.
constexpr PlanarVelocity< NumericType > operator+(const PlanarVelocity< NumericType > &planar_velocity) const
constexpr void operator/=(const NumericType number) noexcept
PlanarVelocity()=default
Default constructor. Constructs a planar velocity vector with an uninitialized value.
static constexpr PlanarVelocity< NumericType > Create(const NumericType x, const NumericType y)
Statically creates a planar velocity vector from the given x and y Cartesian components expressed in ...
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 velocity vector. Contains three components in Cartesian coordinates: x,...
Definition: Velocity.hpp:55
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)