Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
PlanarDisplacement.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_DISPLACEMENT_HPP
26 #define PHQ_PLANAR_DISPLACEMENT_HPP
27 
28 #include <array>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 
33 #include "Angle.hpp"
35 #include "Length.hpp"
36 #include "PlanarDirection.hpp"
37 #include "PlanarVector.hpp"
38 #include "Unit/Length.hpp"
39 
40 namespace PhQ {
41 
42 // Forward declaration for class PhQ::PlanarDisplacement.
43 template <typename NumericType>
44 class Frequency;
45 
46 // Forward declaration for class PhQ::PlanarDisplacement.
47 template <typename NumericType>
48 class PlanarPosition;
49 
50 // Forward declaration for class PhQ::PlanarDisplacement.
51 template <typename NumericType>
52 class PlanarVelocity;
53 
54 // Forward declaration for class PhQ::PlanarDisplacement.
55 template <typename NumericType>
56 class Time;
57 
58 /// \brief Two-dimensional Euclidean displacement vector in the XY plane. Contains two components in
59 /// Cartesian coordinates: x and y. Displacement is not to be confused with position; for a
60 /// two-dimensional Euclidean position vector in the XY plane, see PhQ::PlanarPosition. For a
61 /// three-dimensional Euclidean displacement vector, see PhQ::Displacement. For scalar displacement
62 /// components or for the magnitude of a displacement vector, see PhQ::Length.
63 template <typename NumericType = double>
64 class PlanarDisplacement : public DimensionalPlanarVector<Unit::Length, NumericType> {
65 public:
66  /// \brief Default constructor. Constructs a planar displacement vector with an uninitialized
67  /// value.
68  PlanarDisplacement() = default;
69 
70  /// \brief Constructor. Constructs a planar displacement vector with a given value expressed in a
71  /// given length unit.
73  : DimensionalPlanarVector<Unit::Length, NumericType>(value, unit) {}
74 
75  /// \brief Constructor. Constructs a planar displacement vector from a given set of length
76  /// components.
78  : PlanarDisplacement<NumericType>({x.Value(), y.Value()}) {}
79 
80  /// \brief Constructor. Constructs a planar displacement vector from a given length and planar
81  /// direction.
82  constexpr PlanarDisplacement(
83  const Length<NumericType>& length, const PlanarDirection<NumericType>& planar_direction)
84  : PlanarDisplacement<NumericType>(length.Value() * planar_direction.Value()) {}
85 
86  /// \brief Constructor. Constructs a planar displacement vector from a given displacement vector
87  /// by projecting the displacement vector onto the XY plane.
88  explicit constexpr PlanarDisplacement(const Displacement<NumericType>& displacement);
89 
90  /// \brief Constructor. Constructs a planar displacement vector from a given planar velocity
91  /// vector and time using the definition of velocity.
92  constexpr PlanarDisplacement(
93  const PlanarVelocity<NumericType>& planar_velocity, const Time<NumericType>& time);
94 
95  /// \brief Constructor. Constructs a planar displacement vector from a given planar velocity
96  /// vector and frequency using the definition of velocity.
97  constexpr PlanarDisplacement(
98  const PlanarVelocity<NumericType>& planar_velocity, const Frequency<NumericType>& frequency);
99 
100  /// \brief Constructor. Constructs a planar displacement vector between a given planar position
101  /// vector and the origin.
102  explicit constexpr PlanarDisplacement(const PlanarPosition<NumericType>& planar_position);
103 
104  /// \brief Destructor. Destroys this planar displacement vector.
105  ~PlanarDisplacement() noexcept = default;
106 
107  /// \brief Copy constructor. Constructs a planar displacement vector by copying another one.
108  constexpr PlanarDisplacement(const PlanarDisplacement<NumericType>& other) = default;
109 
110  /// \brief Copy constructor. Constructs a planar displacement by copying another one.
111  template <typename OtherNumericType>
112  explicit constexpr PlanarDisplacement(const PlanarDisplacement<OtherNumericType>& other)
113  : PlanarDisplacement(static_cast<PlanarVector<NumericType>>(other.Value())) {}
114 
115  /// \brief Move constructor. Constructs a planar displacement vector by moving another one.
116  constexpr PlanarDisplacement(PlanarDisplacement<NumericType>&& other) noexcept = default;
117 
118  /// \brief Copy assignment operator. Assigns this planar displacement vector by copying another
119  /// one.
121  const PlanarDisplacement<NumericType>& other) = default;
122 
123  /// \brief Copy assignment operator. Assigns this planar displacement vector by copying another
124  /// one.
125  template <typename OtherNumericType>
128  this->value = static_cast<PlanarVector<NumericType>>(other.Value());
129  return *this;
130  }
131 
132  /// \brief Move assignment operator. Assigns this planar displacement vector by moving another
133  /// one.
135  PlanarDisplacement<NumericType>&& other) noexcept = default;
136 
137  /// \brief Statically creates a planar displacement vector of zero.
138  [[nodiscard]] static constexpr PlanarDisplacement<NumericType> Zero() {
140  }
141 
142  /// \brief Statically creates a planar displacement vector from the given x and y Cartesian
143  /// components expressed in a given length unit.
144  template <Unit::Length Unit>
145  [[nodiscard]] static constexpr PlanarDisplacement<NumericType> Create(
146  const NumericType x, const NumericType y) {
148  ConvertStatically<Unit::Length, Unit, Standard<Unit::Length>>(
150  }
151 
152  /// \brief Statically creates a planar displacement vector from the given x and y Cartesian
153  /// components expressed in a given length unit.
154  template <Unit::Length Unit>
155  [[nodiscard]] static constexpr PlanarDisplacement<NumericType> Create(
156  const std::array<NumericType, 2>& x_y) {
158  ConvertStatically<Unit::Length, Unit, Standard<Unit::Length>>(
160  }
161 
162  /// \brief Statically creates a planar displacement vector with a given value expressed in a given
163  /// length unit.
164  template <Unit::Length Unit>
165  [[nodiscard]] static constexpr PlanarDisplacement<NumericType> Create(
168  ConvertStatically<Unit::Length, Unit, Standard<Unit::Length>>(value)};
169  }
170 
171  /// \brief Returns the x Cartesian component of this planar displacement vector.
172  [[nodiscard]] constexpr Length<NumericType> x() const noexcept {
173  return Length<NumericType>{this->value.x()};
174  }
175 
176  /// \brief Returns the y Cartesian component of this planar displacement vector.
177  [[nodiscard]] constexpr Length<NumericType> y() const noexcept {
178  return Length<NumericType>{this->value.y()};
179  }
180 
181  /// \brief Returns the z Cartesian component of this planar displacement vector.
182  [[nodiscard]] constexpr Length<NumericType> z() const noexcept {
183  return Length<NumericType>{this->value.z()};
184  }
185 
186  /// \brief Returns the magnitude of this planar displacement vector.
187  [[nodiscard]] Length<NumericType> Magnitude() const {
188  return Length<NumericType>{this->value.Magnitude()};
189  }
190 
191  /// \brief Returns the planar direction of this planar displacement vector.
193  return this->value.PlanarDirection();
194  }
195 
196  /// \brief Returns the angle between this planar displacement vector and another one.
198  const PlanarDisplacement<NumericType>& planar_displacement) const {
199  return PhQ::Angle<NumericType>{*this, planar_displacement};
200  }
201 
203  const PlanarPosition<NumericType>& planar_position) const;
204 
206  const PlanarDisplacement<NumericType>& planar_displacement) const {
207  return PlanarDisplacement<NumericType>{this->value + planar_displacement.value};
208  }
209 
211  const PlanarPosition<NumericType>& planar_position) const;
212 
214  const PlanarDisplacement<NumericType>& planar_displacement) const {
215  return PlanarDisplacement<NumericType>{this->value - planar_displacement.value};
216  }
217 
218  constexpr PlanarDisplacement<NumericType> operator*(const NumericType number) const {
219  return PlanarDisplacement<NumericType>{this->value * number};
220  }
221 
222  constexpr PlanarVelocity<NumericType> operator*(const Frequency<NumericType>& frequency) const;
223 
224  constexpr PlanarDisplacement<NumericType> operator/(const NumericType number) const {
225  return PlanarDisplacement<NumericType>{this->value / number};
226  }
227 
228  constexpr PlanarVelocity<NumericType> operator/(const Time<NumericType>& time) const;
229 
230  constexpr void operator+=(const PlanarDisplacement<NumericType>& planar_displacement) noexcept {
231  this->value += planar_displacement.value;
232  }
233 
234  constexpr void operator-=(const PlanarDisplacement<NumericType>& planar_displacement) noexcept {
235  this->value -= planar_displacement.value;
236  }
237 
238  constexpr void operator*=(const NumericType number) noexcept {
239  this->value *= number;
240  }
241 
242  constexpr void operator/=(const NumericType number) noexcept {
243  this->value /= number;
244  }
245 
246 private:
247  /// \brief Constructor. Constructs a planar displacement vector with a given value expressed in
248  /// the standard length unit.
250  : DimensionalPlanarVector<Unit::Length, NumericType>(value) {}
251 
252  template <typename OtherNumericType>
253  friend class PlanarPosition;
254 };
255 
256 template <typename NumericType>
257 inline constexpr bool operator==(const PlanarDisplacement<NumericType>& left,
258  const PlanarDisplacement<NumericType>& right) noexcept {
259  return left.Value() == right.Value();
260 }
261 
262 template <typename NumericType>
263 inline constexpr bool operator!=(const PlanarDisplacement<NumericType>& left,
264  const PlanarDisplacement<NumericType>& right) noexcept {
265  return left.Value() != right.Value();
266 }
267 
268 template <typename NumericType>
269 inline constexpr bool operator<(const PlanarDisplacement<NumericType>& left,
270  const PlanarDisplacement<NumericType>& right) noexcept {
271  return left.Value() < right.Value();
272 }
273 
274 template <typename NumericType>
275 inline constexpr bool operator>(const PlanarDisplacement<NumericType>& left,
276  const PlanarDisplacement<NumericType>& right) noexcept {
277  return left.Value() > right.Value();
278 }
279 
280 template <typename NumericType>
281 inline constexpr bool operator<=(const PlanarDisplacement<NumericType>& left,
282  const PlanarDisplacement<NumericType>& right) noexcept {
283  return left.Value() <= right.Value();
284 }
285 
286 template <typename NumericType>
287 inline constexpr bool operator>=(const PlanarDisplacement<NumericType>& left,
288  const PlanarDisplacement<NumericType>& right) noexcept {
289  return left.Value() >= right.Value();
290 }
291 
292 template <typename NumericType>
293 inline std::ostream& operator<<(
294  std::ostream& stream, const PlanarDisplacement<NumericType>& planar_displacement) {
295  stream << planar_displacement.Print();
296  return stream;
297 }
298 
299 template <typename NumericType>
301  const NumericType number, const PlanarDisplacement<NumericType>& planar_displacement) {
302  return planar_displacement * number;
303 }
304 
305 template <typename NumericType>
307  const PlanarDisplacement<NumericType>& planar_displacement)
308  : PlanarDirection<NumericType>(planar_displacement.Value()) {}
309 
310 template <typename NumericType>
311 inline Angle<NumericType>::Angle(const PlanarDisplacement<NumericType>& planar_displacement_1,
312  const PlanarDisplacement<NumericType>& planar_displacement_2)
313  : Angle<NumericType>(planar_displacement_1.Value(), planar_displacement_2.Value()) {}
314 
315 } // namespace PhQ
316 
317 namespace std {
318 
319 template <typename NumericType>
320 struct hash<PhQ::PlanarDisplacement<NumericType>> {
321  inline size_t operator()(const PhQ::PlanarDisplacement<NumericType>& planar_displacement) const {
322  return hash<PhQ::PlanarVector<NumericType>>()(planar_displacement.Value());
323  }
324 };
325 
326 } // namespace std
327 
328 #endif // PHQ_PLANAR_DISPLACEMENT_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::Length 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...
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
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(const PlanarVector< NumericType > &value)
Constructor. Constructs a planar displacement vector with a given value expressed in the standard len...
constexpr PlanarDisplacement< NumericType > & operator=(const PlanarDisplacement< NumericType > &other)=default
Copy assignment operator. Assigns this planar displacement vector by copying another one.
static constexpr PlanarDisplacement< NumericType > Create(const NumericType x, const NumericType y)
Statically creates a planar displacement vector from the given x and y Cartesian components expressed...
static constexpr PlanarDisplacement< NumericType > Create(const PlanarVector< NumericType > &value)
Statically creates a planar displacement vector with a given value expressed in a given length unit.
~PlanarDisplacement() noexcept=default
Destructor. Destroys this planar displacement vector.
constexpr Length< NumericType > y() const noexcept
Returns the y Cartesian component of this planar displacement vector.
constexpr PlanarDisplacement< NumericType > operator-(const PlanarDisplacement< NumericType > &planar_displacement) const
constexpr PlanarDisplacement< NumericType > operator/(const NumericType number) const
constexpr PlanarDisplacement(PlanarDisplacement< NumericType > &&other) noexcept=default
Move constructor. Constructs a planar displacement vector by moving another one.
constexpr PlanarDisplacement< NumericType > & operator=(const PlanarDisplacement< OtherNumericType > &other)
Copy assignment operator. Assigns this planar displacement vector by copying another one.
PhQ::Angle< NumericType > Angle(const PlanarDisplacement< NumericType > &planar_displacement) const
Returns the angle between this planar displacement vector and another one.
constexpr Length< NumericType > x() const noexcept
Returns the x Cartesian component of this planar displacement vector.
static constexpr PlanarDisplacement< NumericType > Zero()
Statically creates a planar displacement vector of zero.
constexpr void operator*=(const NumericType number) noexcept
PlanarDisplacement()=default
Default constructor. Constructs a planar displacement vector with an uninitialized value.
constexpr PlanarPosition< NumericType > operator-(const PlanarPosition< NumericType > &planar_position) const
Length< NumericType > Magnitude() const
Returns the magnitude of this planar displacement vector.
PlanarDisplacement(const PlanarVector< NumericType > &value, const Unit::Length unit)
Constructor. Constructs a planar displacement vector with a given value expressed in a given length u...
constexpr PlanarPosition< NumericType > operator+(const PlanarPosition< NumericType > &planar_position) const
constexpr PlanarDisplacement< NumericType > & operator=(PlanarDisplacement< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this planar displacement vector by moving another one.
constexpr PlanarDisplacement< NumericType > operator+(const PlanarDisplacement< NumericType > &planar_displacement) const
static constexpr PlanarDisplacement< NumericType > Create(const std::array< NumericType, 2 > &x_y)
Statically creates a planar displacement vector from the given x and y Cartesian components expressed...
PlanarDisplacement(const Length< NumericType > &x, const Length< NumericType > &y)
Constructor. Constructs a planar displacement vector from a given set of length components.
constexpr Length< NumericType > z() const noexcept
Returns the z Cartesian component of this planar displacement vector.
constexpr PlanarDisplacement(const Length< NumericType > &length, const PlanarDirection< NumericType > &planar_direction)
Constructor. Constructs a planar displacement vector from a given length and planar direction.
constexpr void operator/=(const NumericType number) noexcept
PhQ::PlanarDirection< NumericType > PlanarDirection() const
Returns the planar direction of this planar displacement vector.
constexpr void operator-=(const PlanarDisplacement< NumericType > &planar_displacement) noexcept
constexpr void operator+=(const PlanarDisplacement< NumericType > &planar_displacement) noexcept
Two-dimensional Euclidean position vector in the XY plane. Contains two components in Cartesian coord...
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...
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
Length
Length units.
Definition: Length.hpp:53
Frequency
Frequency units.
Definition: Frequency.hpp:53
Time
Time units.
Definition: Time.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)