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