Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Acceleration.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_ACCELERATION_HPP
26 #define PHQ_ACCELERATION_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 "Frequency.hpp"
37 #include "PlanarAcceleration.hpp"
38 #include "ScalarAcceleration.hpp"
39 #include "Time.hpp"
40 #include "Unit/Acceleration.hpp"
41 #include "Vector.hpp"
42 #include "Velocity.hpp"
43 
44 namespace PhQ {
45 
46 /// \brief Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian
47 /// coordinates: x, y, and z. For a two-dimensional Euclidean acceleration vector in the XY plane,
48 /// see PhQ::PlanarAcceleration. For scalar acceleration components or for the magnitude of an
49 /// acceleration vector, see PhQ::ScalarAcceleration.
50 template <typename NumericType = double>
51 class Acceleration : public DimensionalVector<Unit::Acceleration, NumericType> {
52 public:
53  /// \brief Default constructor. Constructs an acceleration vector with an uninitialized value.
54  Acceleration() = default;
55 
56  /// \brief Constructor. Constructs an acceleration vector with a given value expressed in a given
57  /// acceleration unit.
59  : DimensionalVector<Unit::Acceleration, NumericType>(value, unit) {}
60 
61  /// \brief Constructor. Constructs an acceleration vector from a given set of scalar acceleration
62  /// components.
65  : Acceleration<NumericType>({x.Value(), y.Value(), z.Value()}) {}
66 
67  /// \brief Constructor. Constructs an acceleration vector from a given scalar acceleration
68  /// magnitude and direction.
69  constexpr Acceleration(const ScalarAcceleration<NumericType>& scalar_acceleration,
70  const Direction<NumericType>& direction)
71  : Acceleration<NumericType>(scalar_acceleration.Value() * direction.Value()) {}
72 
73  /// \brief Constructor. Constructs an acceleration vector from a given planar acceleration vector
74  /// in the XY plane. This acceleration vector's z-component is initialized to zero.
75  explicit constexpr Acceleration(const PlanarAcceleration<NumericType>& planar_acceleration)
76  : Acceleration<NumericType>(Vector<NumericType>{planar_acceleration.Value()}) {}
77 
78  /// \brief Constructor. Constructs an acceleration vector from a given velocity and time using the
79  /// definition of acceleration.
80  constexpr Acceleration(const Velocity<NumericType>& velocity, const Time<NumericType>& time)
81  : Acceleration<NumericType>(velocity.Value() / time.Value()) {}
82 
83  /// \brief Constructor. Constructs an acceleration vector from a given velocity and frequency
84  /// using the definition of acceleration.
85  constexpr Acceleration(
86  const Velocity<NumericType>& velocity, const Frequency<NumericType>& frequency)
87  : Acceleration<NumericType>(velocity.Value() * frequency.Value()) {}
88 
89  /// \brief Destructor. Destroys this acceleration vector.
90  ~Acceleration() noexcept = default;
91 
92  /// \brief Copy constructor. Constructs an acceleration vector by copying another one.
93  constexpr Acceleration(const Acceleration<NumericType>& other) = default;
94 
95  /// \brief Copy constructor. Constructs an acceleration vector by copying another one.
96  template <typename OtherNumericType>
97  explicit constexpr Acceleration(const Acceleration<OtherNumericType>& other)
98  : Acceleration(static_cast<Vector<NumericType>>(other.Value())) {}
99 
100  /// \brief Move constructor. Constructs an acceleration vector by moving another one.
101  constexpr Acceleration(Acceleration<NumericType>&& other) noexcept = default;
102 
103  /// \brief Copy assignment operator. Assigns this acceleration vector by copying another one.
105 
106  /// \brief Copy assignment operator. Assigns this acceleration vector by copying another one.
107  template <typename OtherNumericType>
109  this->value = static_cast<Vector<NumericType>>(other.Value());
110  return *this;
111  }
112 
113  /// \brief Move assignment operator. Assigns this acceleration vector by moving another one.
115  Acceleration<NumericType>&& other) noexcept = default;
116 
117  /// \brief Statically creates an acceleration vector of zero.
118  [[nodiscard]] static constexpr Acceleration<NumericType> Zero() {
120  }
121 
122  /// \brief Statically creates an acceleration vector from the given x, y, and z Cartesian
123  /// components expressed in a given acceleration unit.
124  template <Unit::Acceleration Unit>
125  [[nodiscard]] static constexpr Acceleration<NumericType> Create(
126  const NumericType x, const NumericType y, const NumericType z) {
128  ConvertStatically<Unit::Acceleration, Unit, Standard<Unit::Acceleration>>(
129  Vector<NumericType>{x, y, z})};
130  }
131 
132  /// \brief Statically creates an acceleration vector from the given x, y, and z Cartesian
133  /// components expressed in a given acceleration unit.
134  template <Unit::Acceleration Unit>
135  [[nodiscard]] static constexpr Acceleration<NumericType> Create(
136  const std::array<NumericType, 3>& x_y_z) {
138  ConvertStatically<Unit::Acceleration, Unit, Standard<Unit::Acceleration>>(
139  Vector<NumericType>{x_y_z})};
140  }
141 
142  /// \brief Statically creates an acceleration vector with a given value expressed in a given
143  /// acceleration unit.
144  template <Unit::Acceleration Unit>
145  [[nodiscard]] static constexpr Acceleration<NumericType> Create(
146  const Vector<NumericType>& value) {
148  ConvertStatically<Unit::Acceleration, Unit, Standard<Unit::Acceleration>>(value)};
149  }
150 
151  /// \brief Returns the x Cartesian component of this acceleration vector.
152  [[nodiscard]] constexpr ScalarAcceleration<NumericType> x() const noexcept {
153  return ScalarAcceleration<NumericType>{this->value.x()};
154  }
155 
156  /// \brief Returns the y Cartesian component of this acceleration vector.
157  [[nodiscard]] constexpr ScalarAcceleration<NumericType> y() const noexcept {
158  return ScalarAcceleration<NumericType>{this->value.y()};
159  }
160 
161  /// \brief Returns the z Cartesian component of this acceleration vector.
162  [[nodiscard]] constexpr ScalarAcceleration<NumericType> z() const noexcept {
163  return ScalarAcceleration<NumericType>{this->value.z()};
164  }
165 
166  /// \brief Returns the magnitude of this acceleration vector.
169  }
170 
171  /// \brief Returns the direction of this acceleration vector.
172  [[nodiscard]] PhQ::Direction<NumericType> Direction() const {
173  return this->value.Direction();
174  }
175 
176  /// \brief Returns the angle between this acceleration vector and another one.
177  [[nodiscard]] PhQ::Angle<NumericType> Angle(const Acceleration<NumericType>& other) const {
178  return PhQ::Angle<NumericType>{*this, other};
179  }
180 
182  return Acceleration<NumericType>{this->value + other.value};
183  }
184 
186  return Acceleration<NumericType>{this->value - other.value};
187  }
188 
189  constexpr Acceleration<NumericType> operator*(const NumericType number) const {
190  return Acceleration<NumericType>{this->value * number};
191  }
192 
193  constexpr Velocity<NumericType> operator*(const Time<NumericType>& time) const {
194  return Velocity<NumericType>{*this, time};
195  }
196 
197  constexpr Acceleration<NumericType> operator/(const NumericType number) const {
198  return Acceleration<NumericType>{this->value / number};
199  }
200 
201  constexpr Velocity<NumericType> operator/(const Frequency<NumericType>& frequency) const {
202  return Velocity<NumericType>{*this, frequency};
203  }
204 
205  constexpr void operator+=(const Acceleration<NumericType>& other) noexcept {
206  this->value += other.value;
207  }
208 
209  constexpr void operator-=(const Acceleration<NumericType>& other) noexcept {
210  this->value -= other.value;
211  }
212 
213  constexpr void operator*=(const NumericType number) noexcept {
214  this->value *= number;
215  }
216 
217  constexpr void operator/=(const NumericType number) noexcept {
218  this->value /= number;
219  }
220 
221 private:
222  /// \brief Constructor. Constructs an acceleration vector with a given value expressed in the
223  /// standard acceleration unit.
224  explicit constexpr Acceleration(const Vector<NumericType>& value)
225  : DimensionalVector<Unit::Acceleration, NumericType>(value) {}
226 };
227 
228 template <typename NumericType>
229 inline constexpr bool operator==(
230  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
231  return left.Value() == right.Value();
232 }
233 
234 template <typename NumericType>
235 inline constexpr bool operator!=(
236  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
237  return left.Value() != right.Value();
238 }
239 
240 template <typename NumericType>
241 inline constexpr bool operator<(
242  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
243  return left.Value() < right.Value();
244 }
245 
246 template <typename NumericType>
247 inline constexpr bool operator>(
248  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
249  return left.Value() > right.Value();
250 }
251 
252 template <typename NumericType>
253 inline constexpr bool operator<=(
254  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
255  return left.Value() <= right.Value();
256 }
257 
258 template <typename NumericType>
259 inline constexpr bool operator>=(
260  const Acceleration<NumericType>& left, const Acceleration<NumericType>& right) noexcept {
261  return left.Value() >= right.Value();
262 }
263 
264 template <typename NumericType>
265 inline std::ostream& operator<<(
266  std::ostream& stream, const Acceleration<NumericType>& acceleration) {
267  stream << acceleration.Print();
268  return stream;
269 }
270 
271 template <typename NumericType>
273  const NumericType number, const Acceleration<NumericType>& acceleration) {
274  return acceleration * number;
275 }
276 
277 template <typename NumericType>
279  : Direction<NumericType>(acceleration.Value()) {}
280 
281 template <typename NumericType>
282 inline Angle<NumericType>::Angle(const Acceleration<NumericType>& acceleration_1,
283  const Acceleration<NumericType>& acceleration_2)
284  : Angle<NumericType>(acceleration_1.Value(), acceleration_2.Value()) {}
285 
286 template <typename NumericType>
288  const Acceleration<NumericType>& acceleration)
289  : PlanarAcceleration(PlanarVector<NumericType>{acceleration.Value()}) {}
290 
291 template <typename NumericType>
293  const Acceleration<NumericType>& acceleration, const Time<NumericType>& time)
294  : Velocity<NumericType>(acceleration.Value() * time.Value()) {}
295 
296 template <typename NumericType>
298  const Acceleration<NumericType>& acceleration, const Frequency<NumericType>& frequency)
299  : Velocity<NumericType>(acceleration.Value() / frequency.Value()) {}
300 
301 template <typename NumericType>
303  const ScalarAcceleration<NumericType>& scalar_acceleration) const {
304  return Acceleration<NumericType>{scalar_acceleration, *this};
305 }
306 
307 template <typename NumericType>
309  const Acceleration<NumericType>& acceleration) const {
310  return Velocity<NumericType>{acceleration, *this};
311 }
312 
313 template <typename NumericType>
315  const Direction<NumericType>& direction) const {
316  return Acceleration<NumericType>{*this, direction};
317 }
318 
319 template <typename NumericType>
321  const Frequency<NumericType>& frequency) const {
322  return Acceleration<NumericType>{*this, frequency};
323 }
324 
325 template <typename NumericType>
327  const Velocity<NumericType>& velocity) const {
328  return Acceleration<NumericType>{velocity, *this};
329 }
330 
331 template <typename NumericType>
333  const Time<NumericType>& time) const {
334  return Acceleration<NumericType>{*this, time};
335 }
336 
337 } // namespace PhQ
338 
339 namespace std {
340 
341 template <typename NumericType>
342 struct hash<PhQ::Acceleration<NumericType>> {
343  inline size_t operator()(const PhQ::Acceleration<NumericType>& acceleration) const {
344  return hash<PhQ::Vector<NumericType>>()(acceleration.Value());
345  }
346 };
347 
348 } // namespace std
349 
350 #endif // PHQ_ACCELERATION_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
constexpr Acceleration< NumericType > & operator=(Acceleration< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this acceleration vector by moving another one.
PhQ::Direction< NumericType > Direction() const
Returns the direction of this acceleration vector.
Acceleration()=default
Default constructor. Constructs an acceleration vector with an uninitialized value.
Acceleration(const Vector< NumericType > &value, const Unit::Acceleration unit)
Constructor. Constructs an acceleration vector with a given value expressed in a given acceleration u...
constexpr ScalarAcceleration< NumericType > y() const noexcept
Returns the y Cartesian component of this acceleration vector.
ScalarAcceleration< NumericType > Magnitude() const
Returns the magnitude of this acceleration vector.
constexpr Acceleration< NumericType > & operator=(const Acceleration< OtherNumericType > &other)
Copy assignment operator. Assigns this acceleration vector by copying another one.
static constexpr Acceleration< NumericType > Zero()
Statically creates an acceleration vector of zero.
Acceleration(const ScalarAcceleration< NumericType > &x, const ScalarAcceleration< NumericType > &y, const ScalarAcceleration< NumericType > &z)
Constructor. Constructs an acceleration vector from a given set of scalar acceleration components.
constexpr Velocity< NumericType > operator/(const Frequency< NumericType > &frequency) const
constexpr void operator/=(const NumericType number) noexcept
static constexpr Acceleration< NumericType > Create(const Vector< NumericType > &value)
Statically creates an acceleration vector with a given value expressed in a given acceleration unit.
constexpr void operator*=(const NumericType number) noexcept
constexpr void operator-=(const Acceleration< NumericType > &other) noexcept
static constexpr Acceleration< NumericType > Create(const std::array< NumericType, 3 > &x_y_z)
Statically creates an acceleration vector from the given x, y, and z Cartesian components expressed i...
static constexpr Acceleration< NumericType > Create(const NumericType x, const NumericType y, const NumericType z)
Statically creates an acceleration vector from the given x, y, and z Cartesian components expressed i...
constexpr Acceleration< NumericType > operator+(const Acceleration< NumericType > &other) const
constexpr Acceleration< NumericType > operator/(const NumericType number) const
constexpr Acceleration< NumericType > operator*(const NumericType number) const
constexpr void operator+=(const Acceleration< NumericType > &other) noexcept
PhQ::Angle< NumericType > Angle(const Acceleration< NumericType > &other) const
Returns the angle between this acceleration vector and another one.
constexpr Acceleration< NumericType > & operator=(const Acceleration< NumericType > &other)=default
Copy assignment operator. Assigns this acceleration vector by copying another one.
constexpr Acceleration(const Vector< NumericType > &value)
Constructor. Constructs an acceleration vector with a given value expressed in the standard accelerat...
constexpr Acceleration(const ScalarAcceleration< NumericType > &scalar_acceleration, const Direction< NumericType > &direction)
Constructor. Constructs an acceleration vector from a given scalar acceleration magnitude and directi...
constexpr Acceleration(Acceleration< NumericType > &&other) noexcept=default
Move constructor. Constructs an acceleration vector by moving another one.
constexpr Acceleration(const PlanarAcceleration< NumericType > &planar_acceleration)
Constructor. Constructs an acceleration vector from a given planar acceleration vector in the XY plan...
constexpr Acceleration(const Velocity< NumericType > &velocity, const Frequency< NumericType > &frequency)
Constructor. Constructs an acceleration vector from a given velocity and frequency using the definiti...
~Acceleration() noexcept=default
Destructor. Destroys this acceleration vector.
constexpr Acceleration< NumericType > operator-(const Acceleration< NumericType > &other) const
constexpr Velocity< NumericType > operator*(const Time< NumericType > &time) const
constexpr ScalarAcceleration< NumericType > z() const noexcept
Returns the z Cartesian component of this acceleration vector.
constexpr Acceleration(const Velocity< NumericType > &velocity, const Time< NumericType > &time)
Constructor. Constructs an acceleration vector from a given velocity and time using the definition of...
constexpr ScalarAcceleration< NumericType > x() const noexcept
Returns the x Cartesian component of this acceleration vector.
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::Acceleration 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
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...
PlanarAcceleration()=default
Default constructor. Constructs a planar acceleration vector with an uninitialized value.
Two-dimensional Euclidean vector in the XY plane. Contains two components in Cartesian coordinates: x...
Scalar acceleration component or magnitude of an acceleration vector. For a three-dimensional Euclide...
constexpr ScalarAcceleration< NumericType > operator*(const NumericType number) const
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
Velocity()=default
Default constructor. Constructs a velocity vector with an uninitialized value.
constexpr Velocity< NumericType > operator/(const NumericType number) const
Definition: Velocity.hpp:204
constexpr Velocity< NumericType > operator*(const NumericType number) const
Definition: Velocity.hpp:194
Acceleration
Acceleration units.
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)