Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
PlanarForce.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_FORCE_HPP
26 #define PHQ_PLANAR_FORCE_HPP
27 
28 #include <array>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 
33 #include "Angle.hpp"
35 #include "PlanarDirection.hpp"
36 #include "PlanarVector.hpp"
37 #include "ScalarForce.hpp"
38 #include "Unit/Force.hpp"
39 
40 namespace PhQ {
41 
42 // Forward declaration for class PhQ::PlanarForce.
43 template <typename NumericType>
44 class PlanarTraction;
45 
46 /// \brief Two-dimensional Euclidean force vector in the XY plane. Contains two components in
47 /// Cartesian coordinates: x and y. For a three-dimensional Euclidean force vector, see PhQ::Force.
48 /// For scalar force components or for the magnitude of a force vector, see PhQ::ScalarForce.
49 template <typename NumericType = double>
50 class PlanarForce : public DimensionalPlanarVector<Unit::Force, NumericType> {
51 public:
52  /// \brief Default constructor. Constructs a planar force vector with an uninitialized value.
53  PlanarForce() = default;
54 
55  /// \brief Constructor. Constructs a planar force vector with a given value expressed in a given
56  /// force unit.
58  : DimensionalPlanarVector<Unit::Force, NumericType>(value, unit) {}
59 
60  /// \brief Constructor. Constructs a planar force vector from a given set of scalar force
61  /// components.
63  : PlanarForce<NumericType>({x.Value(), y.Value()}) {}
64 
65  /// \brief Constructor. Constructs a planar force vector from a given scalar force magnitude and
66  /// planar direction.
67  constexpr PlanarForce(const ScalarForce<NumericType>& scalar_force,
68  const PlanarDirection<NumericType>& planar_direction)
69  : PlanarForce<NumericType>(scalar_force.Value() * planar_direction.Value()) {}
70 
71  /// \brief Constructor. Constructs a planar force vector from a given force vector by projecting
72  /// the force vector onto the XY plane.
73  explicit constexpr PlanarForce(const Force<NumericType>& force);
74 
75  /// \brief Constructor. Constructs a planar force vector from a given planar traction and area
76  /// using the definition of traction.
77  constexpr PlanarForce(
78  const PlanarTraction<NumericType>& planar_traction, const Area<NumericType>& area);
79 
80  /// \brief Destructor. Destroys this planar force vector.
81  ~PlanarForce() noexcept = default;
82 
83  /// \brief Copy constructor. Constructs a planar force vector by copying another one.
84  constexpr PlanarForce(const PlanarForce<NumericType>& other) = default;
85 
86  /// \brief Copy constructor. Constructs a planar force vector by copying another one.
87  template <typename OtherNumericType>
88  explicit constexpr PlanarForce(const PlanarForce<OtherNumericType>& other)
89  : PlanarForce(static_cast<PlanarVector<NumericType>>(other.Value())) {}
90 
91  /// \brief Move constructor. Constructs a planar force vector by moving another one.
92  constexpr PlanarForce(PlanarForce<NumericType>&& other) noexcept = default;
93 
94  /// \brief Copy assignment operator. Assigns this planar force vector by copying another one.
95  constexpr PlanarForce<NumericType>& operator=(const PlanarForce<NumericType>& other) = default;
96 
97  /// \brief Copy assignment operator. Assigns this planar force vector by copying another one.
98  template <typename OtherNumericType>
100  this->value = static_cast<PlanarVector<NumericType>>(other.Value());
101  return *this;
102  }
103 
104  /// \brief Move assignment operator. Assigns this planar force vector by moving another one.
106  PlanarForce<NumericType>&& other) noexcept = default;
107 
108  /// \brief Statically creates a planar force vector of zero.
109  [[nodiscard]] static constexpr PlanarForce<NumericType> Zero() {
111  }
112 
113  /// \brief Statically creates a planar force vector from the given x and y Cartesian components
114  /// expressed in a given force unit.
115  template <Unit::Force Unit>
116  [[nodiscard]] static constexpr PlanarForce<NumericType> Create(
117  const NumericType x, const NumericType y) {
118  return PlanarForce<NumericType>{ConvertStatically<Unit::Force, Unit, Standard<Unit::Force>>(
120  }
121 
122  /// \brief Statically creates a planar force vector from the given x and y Cartesian components
123  /// expressed in a given force unit.
124  template <Unit::Force Unit>
125  [[nodiscard]] static constexpr PlanarForce<NumericType> Create(
126  const std::array<NumericType, 2>& x_y) {
127  return PlanarForce<NumericType>{ConvertStatically<Unit::Force, Unit, Standard<Unit::Force>>(
129  }
130 
131  /// \brief Statically creates a planar force vector with a given value expressed in a given force
132  /// unit.
133  template <Unit::Force Unit>
134  [[nodiscard]] static constexpr PlanarForce<NumericType> Create(
137  ConvertStatically<Unit::Force, Unit, Standard<Unit::Force>>(value)};
138  }
139 
140  /// \brief Returns the x Cartesian component of this planar force vector.
141  [[nodiscard]] constexpr ScalarForce<NumericType> x() const noexcept {
142  return ScalarForce<NumericType>{this->value.x()};
143  }
144 
145  /// \brief Returns the y Cartesian component of this planar force vector.
146  [[nodiscard]] constexpr ScalarForce<NumericType> y() const noexcept {
147  return ScalarForce<NumericType>{this->value.y()};
148  }
149 
150  /// \brief Returns the magnitude of this planar force vector.
151  [[nodiscard]] ScalarForce<NumericType> Magnitude() const {
152  return ScalarForce<NumericType>{this->value.Magnitude()};
153  }
154 
155  /// \brief Returns the planar direction of this planar force vector.
157  return this->value.PlanarDirection();
158  }
159 
160  /// \brief Returns the angle between this planar force vector and another one.
161  [[nodiscard]] PhQ::Angle<NumericType> Angle(const PlanarForce<NumericType>& planar_force) const {
162  return PhQ::Angle<NumericType>{*this, planar_force};
163  }
164 
165  constexpr PlanarForce<NumericType> operator+(const PlanarForce<NumericType>& planar_force) const {
166  return PlanarForce<NumericType>{this->value + planar_force.value};
167  }
168 
169  constexpr PlanarForce<NumericType> operator-(const PlanarForce<NumericType>& planar_force) const {
170  return PlanarForce<NumericType>{this->value - planar_force.value};
171  }
172 
173  constexpr PlanarForce<NumericType> operator*(const NumericType number) const {
174  return PlanarForce<NumericType>{this->value * number};
175  }
176 
177  constexpr PlanarForce<NumericType> operator/(const NumericType number) const {
178  return PlanarForce<NumericType>{this->value / number};
179  }
180 
181  constexpr PlanarTraction<NumericType> operator/(const Area<NumericType>& area) const;
182 
183  constexpr void operator+=(const PlanarForce<NumericType>& planar_force) noexcept {
184  this->value += planar_force.value;
185  }
186 
187  constexpr void operator-=(const PlanarForce<NumericType>& planar_force) noexcept {
188  this->value -= planar_force.value;
189  }
190 
191  constexpr void operator*=(const NumericType number) noexcept {
192  this->value *= number;
193  }
194 
195  constexpr void operator/=(const NumericType number) noexcept {
196  this->value /= number;
197  }
198 
199 private:
200  /// \brief Constructor. Constructs a planar force vector with a given value expressed in the
201  /// standard force unit.
202  explicit constexpr PlanarForce(const PlanarVector<NumericType>& value)
203  : DimensionalPlanarVector<Unit::Force, NumericType>(value) {}
204 };
205 
206 template <typename NumericType>
207 inline constexpr bool operator==(
208  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
209  return left.Value() == right.Value();
210 }
211 
212 template <typename NumericType>
213 inline constexpr bool operator!=(
214  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
215  return left.Value() != right.Value();
216 }
217 
218 template <typename NumericType>
219 inline constexpr bool operator<(
220  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
221  return left.Value() < right.Value();
222 }
223 
224 template <typename NumericType>
225 inline constexpr bool operator>(
226  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
227  return left.Value() > right.Value();
228 }
229 
230 template <typename NumericType>
231 inline constexpr bool operator<=(
232  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
233  return left.Value() <= right.Value();
234 }
235 
236 template <typename NumericType>
237 inline constexpr bool operator>=(
238  const PlanarForce<NumericType>& left, const PlanarForce<NumericType>& right) noexcept {
239  return left.Value() >= right.Value();
240 }
241 
242 template <typename NumericType>
243 inline std::ostream& operator<<(
244  std::ostream& stream, const PlanarForce<NumericType>& planar_force) {
245  stream << planar_force.Print();
246  return stream;
247 }
248 
249 template <typename NumericType>
251  const NumericType number, const PlanarForce<NumericType>& planar_force) {
252  return planar_force * number;
253 }
254 
255 template <typename NumericType>
257  : PlanarDirection<NumericType>(planar_force.Value()) {}
258 
259 template <typename NumericType>
261  const PlanarForce<NumericType>& planar_force_1, const PlanarForce<NumericType>& planar_force_2)
262  : Angle<NumericType>(planar_force_1.Value(), planar_force_2.Value()) {}
263 
264 template <typename NumericType>
266  const ScalarForce<NumericType>& scalar_force) const {
267  return PlanarForce<NumericType>{scalar_force, *this};
268 }
269 
270 template <typename NumericType>
272  const PlanarDirection<NumericType>& planar_direction) const {
273  return PlanarForce<NumericType>{*this, planar_direction};
274 }
275 
276 } // namespace PhQ
277 
278 namespace std {
279 
280 template <typename NumericType>
281 struct hash<PhQ::PlanarForce<NumericType>> {
282  inline size_t operator()(const PhQ::PlanarForce<NumericType>& planar_force) const {
283  return hash<PhQ::PlanarVector<NumericType>>()(planar_force.Value());
284  }
285 };
286 
287 } // namespace std
288 
289 #endif // PHQ_PLANAR_FORCE_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.
Surface area or cross-sectional area. Can also represent a scalar component of a vector area or the m...
Definition: Area.hpp:71
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::Force 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 force vector. Contains three components in Cartesian coordinates: x,...
Definition: Force.hpp:52
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 force vector in the XY plane. Contains two components in Cartesian coordina...
Definition: PlanarForce.hpp:50
constexpr void operator+=(const PlanarForce< NumericType > &planar_force) noexcept
PlanarForce(const ScalarForce< NumericType > &x, const ScalarForce< NumericType > &y)
Constructor. Constructs a planar force vector from a given set of scalar force components.
Definition: PlanarForce.hpp:62
constexpr void operator*=(const NumericType number) noexcept
static constexpr PlanarForce< NumericType > Create(const std::array< NumericType, 2 > &x_y)
Statically creates a planar force vector from the given x and y Cartesian components expressed in a g...
static constexpr PlanarForce< NumericType > Create(const NumericType x, const NumericType y)
Statically creates a planar force vector from the given x and y Cartesian components expressed in a g...
constexpr void operator-=(const PlanarForce< NumericType > &planar_force) noexcept
ScalarForce< NumericType > Magnitude() const
Returns the magnitude of this planar force vector.
PhQ::Angle< NumericType > Angle(const PlanarForce< NumericType > &planar_force) const
Returns the angle between this planar force vector and another one.
constexpr PlanarForce< NumericType > operator/(const NumericType number) const
constexpr PlanarForce(const ScalarForce< NumericType > &scalar_force, const PlanarDirection< NumericType > &planar_direction)
Constructor. Constructs a planar force vector from a given scalar force magnitude and planar directio...
Definition: PlanarForce.hpp:67
constexpr PlanarForce< NumericType > & operator=(PlanarForce< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this planar force vector by moving another one.
~PlanarForce() noexcept=default
Destructor. Destroys this planar force vector.
PlanarForce()=default
Default constructor. Constructs a planar force vector with an uninitialized value.
constexpr ScalarForce< NumericType > x() const noexcept
Returns the x Cartesian component of this planar force vector.
constexpr PlanarForce< NumericType > & operator=(const PlanarForce< NumericType > &other)=default
Copy assignment operator. Assigns this planar force vector by copying another one.
constexpr PlanarForce< NumericType > operator+(const PlanarForce< NumericType > &planar_force) const
constexpr PlanarForce(const PlanarVector< NumericType > &value)
Constructor. Constructs a planar force vector with a given value expressed in the standard force unit...
constexpr ScalarForce< NumericType > y() const noexcept
Returns the y Cartesian component of this planar force vector.
PhQ::PlanarDirection< NumericType > PlanarDirection() const
Returns the planar direction of this planar force vector.
static constexpr PlanarForce< NumericType > Zero()
Statically creates a planar force vector of zero.
constexpr PlanarForce< NumericType > operator-(const PlanarForce< NumericType > &planar_force) const
static constexpr PlanarForce< NumericType > Create(const PlanarVector< NumericType > &value)
Statically creates a planar force vector with a given value expressed in a given force unit.
PlanarForce(const PlanarVector< NumericType > &value, const Unit::Force unit)
Constructor. Constructs a planar force vector with a given value expressed in a given force unit.
Definition: PlanarForce.hpp:57
constexpr PlanarForce< NumericType > & operator=(const PlanarForce< OtherNumericType > &other)
Copy assignment operator. Assigns this planar force vector by copying another one.
Definition: PlanarForce.hpp:99
constexpr PlanarForce(PlanarForce< NumericType > &&other) noexcept=default
Move constructor. Constructs a planar force vector by moving another one.
constexpr void operator/=(const NumericType number) noexcept
constexpr PlanarForce< NumericType > operator*(const NumericType number) const
Two-dimensional Euclidean traction 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.
Scalar force component or magnitude of a force vector. For a three-dimensional Euclidean force vector...
Definition: ScalarForce.hpp:66
constexpr ScalarForce< NumericType > operator*(const NumericType number) const
Force
Force units.
Definition: Force.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)