Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
35namespace PhQ {
36
37// Forward declaration for class PhQ::Length.
38template <typename NumericType>
39class Area;
40
41// Forward declaration for class PhQ::Length.
42template <typename NumericType>
43class Direction;
44
45// Forward declaration for class PhQ::Length.
46template <typename NumericType>
47class Displacement;
48
49// Forward declaration for class PhQ::Length.
50template <typename NumericType>
52
53// Forward declaration for class PhQ::Length.
54template <typename NumericType>
55class Energy;
56
57// Forward declaration for class PhQ::Length.
58template <typename NumericType>
59class Frequency;
60
61// Forward declaration for class PhQ::Length.
62template <typename NumericType>
63class KinematicViscosity;
64
65// Forward declaration for class PhQ::Length.
66template <typename NumericType>
67class MassDensity;
68
69// Forward declaration for class PhQ::Length.
70template <typename NumericType>
71class PlanarDirection;
72
73// Forward declaration for class PhQ::Length.
74template <typename NumericType>
75class PlanarDisplacement;
76
77// Forward declaration for class PhQ::Length.
78template <typename NumericType>
79class PlanarPosition;
80
81// Forward declaration for class PhQ::Length.
82template <typename NumericType>
83class Position;
84
85// Forward declaration for class PhQ::Length.
86template <typename NumericType>
87class ReynoldsNumber;
88
89// Forward declaration for class PhQ::Length.
90template <typename NumericType>
91class Speed;
92
93// Forward declaration for class PhQ::Length.
94template <typename NumericType>
95class Time;
96
97// Forward declaration for class PhQ::Length.
98template <typename NumericType>
100
101// Forward declaration for class PhQ::Length.
102template <typename NumericType>
103class 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.
110template <typename NumericType = double>
111class Length : public DimensionalScalar<Unit::Length, NumericType> {
112public:
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
244private:
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
263template <typename NumericType>
264inline constexpr bool operator==(
265 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
266 return left.Value() == right.Value();
267}
268
269template <typename NumericType>
270inline constexpr bool operator!=(
271 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
272 return left.Value() != right.Value();
273}
274
275template <typename NumericType>
276inline constexpr bool operator<(
277 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
278 return left.Value() < right.Value();
279}
280
281template <typename NumericType>
282inline constexpr bool operator>(
283 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
284 return left.Value() > right.Value();
285}
286
287template <typename NumericType>
288inline constexpr bool operator<=(
289 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
290 return left.Value() <= right.Value();
291}
292
293template <typename NumericType>
294inline constexpr bool operator>=(
295 const Length<NumericType>& left, const Length<NumericType>& right) noexcept {
296 return left.Value() >= right.Value();
297}
298
299template <typename NumericType>
300inline std::ostream& operator<<(std::ostream& stream, const Length<NumericType>& length) {
301 stream << length.Print();
302 return stream;
303}
304
305template <typename NumericType>
307 const NumericType number, const Length<NumericType>& length) {
308 return length * number;
309}
310
311} // namespace PhQ
312
313namespace std {
314
315template <typename NumericType>
316struct 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...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType 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...
constexpr const PhQ::Vector< NumericType > & 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,...
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 Length< NumericType > & operator=(const Length< NumericType > &other)=default
Copy assignment operator. Assigns this length by copying another one.
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< 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
~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.
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
static constexpr Length< NumericType > Zero()
Statically creates a length of zero.
Definition Length.hpp:179
constexpr void operator*=(const NumericType number) noexcept
Definition Length.hpp:236
constexpr Length< NumericType > operator-(const Length< NumericType > &length) const
Definition Length.hpp:194
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:198
constexpr Length< NumericType > operator/(const NumericType number) const
Definition Length.hpp:216
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
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.
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.
std::ostream & operator<<(std::ostream &stream, 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 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