Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Length.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_LENGTH_HPP
26 #define PHQ_LENGTH_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Unit/Length.hpp"
34 
35 namespace PhQ {
36 
37 // Forward declaration for class PhQ::Length.
38 template <typename NumericType>
39 class Area;
40 
41 // Forward declaration for class PhQ::Length.
42 template <typename NumericType>
43 class Direction;
44 
45 // Forward declaration for class PhQ::Length.
46 template <typename NumericType>
47 class Displacement;
48 
49 // Forward declaration for class PhQ::Length.
50 template <typename NumericType>
51 class DynamicViscosity;
52 
53 // Forward declaration for class PhQ::Length.
54 template <typename NumericType>
55 class Energy;
56 
57 // Forward declaration for class PhQ::Length.
58 template <typename NumericType>
59 class Frequency;
60 
61 // Forward declaration for class PhQ::Length.
62 template <typename NumericType>
63 class KinematicViscosity;
64 
65 // Forward declaration for class PhQ::Length.
66 template <typename NumericType>
67 class MassDensity;
68 
69 // Forward declaration for class PhQ::Length.
70 template <typename NumericType>
71 class PlanarDirection;
72 
73 // Forward declaration for class PhQ::Length.
74 template <typename NumericType>
75 class PlanarDisplacement;
76 
77 // Forward declaration for class PhQ::Length.
78 template <typename NumericType>
79 class PlanarPosition;
80 
81 // Forward declaration for class PhQ::Length.
82 template <typename NumericType>
83 class Position;
84 
85 // Forward declaration for class PhQ::Length.
86 template <typename NumericType>
87 class ReynoldsNumber;
88 
89 // Forward declaration for class PhQ::Length.
90 template <typename NumericType>
91 class Speed;
92 
93 // Forward declaration for class PhQ::Length.
94 template <typename NumericType>
95 class Time;
96 
97 // Forward declaration for class PhQ::Length.
98 template <typename NumericType>
100 
101 // Forward declaration for class PhQ::Length.
102 template <typename NumericType>
103 class Volume;
104 
105 /// \brief Length, distance, or physical size. Can also represent a scalar component or magnitude of
106 /// a position or displacement vector. For a three-dimensional Euclidean position vector, see
107 /// PhQ::Position. For a three-dimensional Euclidean displacement vector, see PhQ::Displacement. For
108 /// a two-dimensional Euclidean position vector in the XY plane, see PhQ::PlanarPosition. For a
109 /// two-dimensional Euclidean displacement vector in the XY plane, see PhQ::PlanarDisplacement.
110 template <typename NumericType = double>
111 class Length : public DimensionalScalar<Unit::Length, NumericType> {
112 public:
113  /// \brief Default constructor. Constructs a length with an uninitialized value.
114  Length() = default;
115 
116  /// \brief Constructor. Constructs a length with a given value expressed in a given length unit.
117  Length(const NumericType value, const Unit::Length unit)
118  : DimensionalScalar<Unit::Length, NumericType>(value, unit) {}
119 
120  /// \brief Constructor. Constructs a length from a given area and length.
121  constexpr Length(const Area<NumericType>& area, const Length<NumericType>& length);
122 
123  /// \brief Constructor. Constructs a length from a given volume and area.
124  constexpr Length(const Volume<NumericType>& volume, const Area<NumericType>& area);
125 
126  /// \brief Constructor. Constructs a length from a given speed and time using the definition of
127  /// speed.
128  constexpr Length(const Speed<NumericType>& speed, const Time<NumericType>& time);
129 
130  /// \brief Constructor. Constructs a length from a given speed and frequency using the definition
131  /// of speed.
132  constexpr Length(const Speed<NumericType>& speed, const Frequency<NumericType>& frequency);
133 
134  /// \brief Constructor. Constructs a length from a given Reynolds number, dynamic viscosity, mass
135  /// density, and speed using the definition of the Reynolds number.
136  constexpr Length(const ReynoldsNumber<NumericType>& reynolds_number,
137  const DynamicViscosity<NumericType>& dynamic_viscosity,
138  const MassDensity<NumericType>& mass_density, const Speed<NumericType>& speed);
139 
140  /// \brief Constructor. Constructs a length from a given Reynolds number, kinematic viscosity, and
141  /// speed using the definition of the Reynolds number.
142  constexpr Length(
143  const ReynoldsNumber<NumericType>& reynolds_number,
144  const KinematicViscosity<NumericType>& kinematic_viscosity, const Speed<NumericType>& speed);
145 
146  /// \brief Constructor. Constructs a length from a given energy and transport energy consumption
147  /// using the definition of transport energy consumption.
148  constexpr Length(const Energy<NumericType>& energy,
149  const TransportEnergyConsumption<NumericType>& transport_energy_consumption);
150 
151  /// \brief Destructor. Destroys this length.
152  ~Length() noexcept = default;
153 
154  /// \brief Copy constructor. Constructs a length by copying another one.
155  constexpr Length(const Length<NumericType>& other) = default;
156 
157  /// \brief Copy constructor. Constructs a length by copying another one.
158  template <typename OtherNumericType>
159  explicit constexpr Length(const Length<OtherNumericType>& other)
160  : Length(static_cast<NumericType>(other.Value())) {}
161 
162  /// \brief Move constructor. Constructs a length by moving another one.
163  constexpr Length(Length<NumericType>&& other) noexcept = default;
164 
165  /// \brief Copy assignment operator. Assigns this length by copying another one.
166  constexpr Length<NumericType>& operator=(const Length<NumericType>& other) = default;
167 
168  /// \brief Copy assignment operator. Assigns this length by copying another one.
169  template <typename OtherNumericType>
171  this->value = static_cast<NumericType>(other.Value());
172  return *this;
173  }
174 
175  /// \brief Move assignment operator. Assigns this length by moving another one.
176  constexpr Length<NumericType>& operator=(Length<NumericType>&& other) noexcept = default;
177 
178  /// \brief Statically creates a length of zero.
179  [[nodiscard]] static constexpr Length<NumericType> Zero() {
180  return Length<NumericType>{static_cast<NumericType>(0)};
181  }
182 
183  /// \brief Statically creates a length with a given value expressed in a given length unit.
184  template <Unit::Length Unit>
185  [[nodiscard]] static constexpr Length<NumericType> Create(const NumericType value) {
186  return Length<NumericType>{
187  ConvertStatically<Unit::Length, Unit, Standard<Unit::Length>>(value)};
188  }
189 
190  constexpr Length<NumericType> operator+(const Length<NumericType>& length) const {
191  return Length<NumericType>{this->value + length.value};
192  }
193 
194  constexpr Length<NumericType> operator-(const Length<NumericType>& length) const {
195  return Length<NumericType>{this->value - length.value};
196  }
197 
198  constexpr Length<NumericType> operator*(const NumericType number) const {
199  return Length<NumericType>{this->value * number};
200  }
201 
202  constexpr Area<NumericType> operator*(const Length<NumericType>& length) const;
203 
204  constexpr Volume<NumericType> operator*(const Area<NumericType>& area) const;
205 
206  constexpr Speed<NumericType> operator*(const Frequency<NumericType>& frequency) const;
207 
208  constexpr Position<NumericType> operator*(const Direction<NumericType>& direction) const;
209 
211  const PlanarDirection<NumericType>& planar_direction) const;
212 
214  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const;
215 
216  constexpr Length<NumericType> operator/(const NumericType number) const {
217  return Length<NumericType>{this->value / number};
218  }
219 
220  constexpr Speed<NumericType> operator/(const Time<NumericType>& time) const;
221 
222  constexpr Time<NumericType> operator/(const Speed<NumericType>& speed) const;
223 
224  constexpr NumericType operator/(const Length<NumericType>& length) const noexcept {
225  return this->value / length.value;
226  }
227 
228  constexpr void operator+=(const Length<NumericType>& length) noexcept {
229  this->value += length.value;
230  }
231 
232  constexpr void operator-=(const Length<NumericType>& length) noexcept {
233  this->value -= length.value;
234  }
235 
236  constexpr void operator*=(const NumericType number) noexcept {
237  this->value *= number;
238  }
239 
240  constexpr void operator/=(const NumericType number) noexcept {
241  this->value /= number;
242  }
243 
244 private:
245  /// \brief Constructor. Constructs a length with a given value expressed in the standard length
246  /// unit.
247  explicit constexpr Length(const NumericType value)
248  : DimensionalScalar<Unit::Length, NumericType>(value) {}
249 
250  template <typename OtherNumericType>
251  friend class Displacement;
252 
253  template <typename OtherNumericType>
254  friend class PlanarDisplacement;
255 
256  template <typename OtherNumericType>
257  friend class PlanarPosition;
258 
259  template <typename OtherNumericType>
260  friend class Position;
261 };
262 
263 template <typename NumericType>
264 inline constexpr bool operator==(
265  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
266  return left.Value() == right.Value();
267 }
268 
269 template <typename NumericType>
270 inline constexpr bool operator!=(
271  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
272  return left.Value() != right.Value();
273 }
274 
275 template <typename NumericType>
276 inline constexpr bool operator<(
277  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
278  return left.Value() < right.Value();
279 }
280 
281 template <typename NumericType>
282 inline constexpr bool operator>(
283  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
284  return left.Value() > right.Value();
285 }
286 
287 template <typename NumericType>
288 inline constexpr bool operator<=(
289  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
290  return left.Value() <= right.Value();
291 }
292 
293 template <typename NumericType>
294 inline constexpr bool operator>=(
295  const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
296  return left.Value() >= right.Value();
297 }
298 
299 template <typename NumericType>
300 inline std::ostream& operator<<(std::ostream& stream, const Length<NumericType>& length) {
301  stream << length.Print();
302  return stream;
303 }
304 
305 template <typename NumericType>
307  const NumericType number, const Length<NumericType>& length) {
308  return length * number;
309 }
310 
311 } // namespace PhQ
312 
313 namespace std {
314 
315 template <typename NumericType>
316 struct hash<PhQ::Length<NumericType>> {
317  inline size_t operator()(const PhQ::Length<NumericType>& length) const {
318  return hash<NumericType>()(length.Value());
319  }
320 };
321 
322 } // namespace std
323 
324 #endif // PHQ_LENGTH_HPP
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 scalar physical quantity. Such a physical quantit...
double value
Value of this physical quantity expressed in its standard unit of measure.
constexpr 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 direction vector. Contains three components in Cartesian coordinates: x,...
Definition: Direction.hpp:115
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
Dynamic viscosity, also known as molecular dynamic viscosity. Dynamic viscosity is the relationship b...
Energy physical quantity. Can represent any kind of energy, such as kinetic energy,...
Definition: Energy.hpp:73
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
Kinematic viscosity, also known as molecular kinematic viscosity. Defined as dynamic viscosity divide...
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
constexpr void operator-=(const Length< NumericType > &length) noexcept
Definition: Length.hpp:232
constexpr Volume< NumericType > operator*(const Area< NumericType > &area) const
constexpr Speed< NumericType > operator/(const Time< NumericType > &time) const
constexpr Time< NumericType > operator/(const Speed< NumericType > &speed) const
constexpr NumericType operator/(const Length< NumericType > &length) const noexcept
Definition: Length.hpp:224
constexpr Length< NumericType > & operator=(Length< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this length by moving another one.
static constexpr Length< NumericType > Create(const NumericType value)
Statically creates a length with a given value expressed in a given length unit.
Definition: Length.hpp:185
constexpr Length< NumericType > operator-(const Length< NumericType > &length) const
Definition: Length.hpp:194
~Length() noexcept=default
Destructor. Destroys this length.
constexpr Length(Length< NumericType > &&other) noexcept=default
Move constructor. Constructs a length by moving another one.
constexpr void operator+=(const Length< NumericType > &length) noexcept
Definition: Length.hpp:228
constexpr void operator/=(const NumericType number) noexcept
Definition: Length.hpp:240
Length()=default
Default constructor. Constructs a length with an uninitialized value.
constexpr Position< NumericType > operator*(const Direction< NumericType > &direction) const
constexpr Length(const Area< NumericType > &area, const Length< NumericType > &length)
Constructor. Constructs a length from a given area and length.
constexpr Length(const Energy< NumericType > &energy, const TransportEnergyConsumption< NumericType > &transport_energy_consumption)
Constructor. Constructs a length from a given energy and transport energy consumption using the defin...
Length(const NumericType value, const Unit::Length unit)
Constructor. Constructs a length with a given value expressed in a given length unit.
Definition: Length.hpp:117
constexpr Energy< NumericType > operator*(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) const
constexpr Length(const Speed< NumericType > &speed, const Frequency< NumericType > &frequency)
Constructor. Constructs a length from a given speed and frequency using the definition of speed.
constexpr void operator*=(const NumericType number) noexcept
Definition: Length.hpp:236
constexpr Length< NumericType > operator*(const NumericType number) const
Definition: Length.hpp:198
constexpr Length(const Volume< NumericType > &volume, const Area< NumericType > &area)
Constructor. Constructs a length from a given volume and area.
static constexpr Length< NumericType > Zero()
Statically creates a length of zero.
Definition: Length.hpp:179
constexpr Speed< NumericType > operator*(const Frequency< NumericType > &frequency) const
constexpr Length(const Speed< NumericType > &speed, const Time< NumericType > &time)
Constructor. Constructs a length from a given speed and time using the definition of speed.
constexpr PlanarPosition< NumericType > operator*(const PlanarDirection< NumericType > &planar_direction) const
constexpr Length< NumericType > & operator=(const Length< OtherNumericType > &other)
Copy assignment operator. Assigns this length by copying another one.
Definition: Length.hpp:170
constexpr Length< NumericType > operator+(const Length< NumericType > &length) const
Definition: Length.hpp:190
constexpr Area< NumericType > operator*(const Length< NumericType > &length) const
constexpr Length< NumericType > & operator=(const Length< NumericType > &other)=default
Copy assignment operator. Assigns this length by copying another one.
constexpr Length(const NumericType value)
Constructor. Constructs a length with a given value expressed in the standard length unit.
Definition: Length.hpp:247
constexpr Length< NumericType > operator/(const NumericType number) const
Definition: Length.hpp:216
constexpr Length(const ReynoldsNumber< NumericType > &reynolds_number, const DynamicViscosity< NumericType > &dynamic_viscosity, const MassDensity< NumericType > &mass_density, const Speed< NumericType > &speed)
Constructor. Constructs a length from a given Reynolds number, dynamic viscosity, mass density,...
constexpr Length(const ReynoldsNumber< NumericType > &reynolds_number, const KinematicViscosity< NumericType > &kinematic_viscosity, const Speed< NumericType > &speed)
Constructor. Constructs a length from a given Reynolds number, kinematic viscosity,...
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Definition: MassDensity.hpp:83
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
Two-dimensional Euclidean displacement vector in the XY plane. Contains two components in Cartesian c...
Two-dimensional Euclidean position vector in the XY plane. Contains two components in Cartesian coord...
Three-dimensional Euclidean position vector. Contains three components in Cartesian coordinates: x,...
Definition: Position.hpp:50
Reynolds number of a fluid flow. Measures the local turbulence of a fluid flow. Represents the ratio ...
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
Transport energy consumption, also known as energy consumption in transport. A measure of energy use ...
Volume. For the time rate of change of volume, see PhQ::VolumeRate; see also PhQ::Time and PhQ::Frequ...
Definition: Volume.hpp:62
DynamicViscosity
Dynamic viscosity units.
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
Length
Length units.
Definition: Length.hpp:53
Frequency
Frequency units.
Definition: Frequency.hpp:53
Energy
Energy units.
Definition: Energy.hpp:53
TransportEnergyConsumption
Energy consumption in transport units.
Volume
Volume units.
Definition: Volume.hpp:53
Area
Area units.
Definition: Area.hpp:53
Time
Time units.
Definition: Time.hpp:53
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)