Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Area.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_AREA_HPP
26 #define PHQ_AREA_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Length.hpp"
34 #include "Unit/Area.hpp"
35 
36 namespace PhQ {
37 
38 // Forward declaration for class PhQ::Area.
39 template <typename NumericType>
40 class Direction;
41 
42 // Forward declaration for class PhQ::Area.
43 template <typename NumericType>
44 class Force;
45 
46 // Forward declaration for class PhQ::Area.
47 template <typename NumericType>
48 class ScalarForce;
49 
50 // Forward declaration for class PhQ::Area.
51 template <typename NumericType>
52 class ScalarTraction;
53 
54 // Forward declaration for class PhQ::Area.
55 template <typename NumericType>
56 class StaticPressure;
57 
58 // Forward declaration for class PhQ::Area.
59 template <typename NumericType>
60 class Traction;
61 
62 // Forward declaration for class PhQ::Area.
63 template <typename NumericType>
64 class VectorArea;
65 
66 /// \brief Surface area or cross-sectional area. Can also represent a scalar component of a vector
67 /// area or the magnitude of a vector area. Any closed surface has a vector area: it is the surface
68 /// integral of its surface normal direction. A vector area is an oriented area; it is the
69 /// three-dimensional Euclidean vector representation of an area; see PhQ::VectorArea.
70 template <typename NumericType = double>
71 class Area : public DimensionalScalar<Unit::Area, NumericType> {
72 public:
73  /// \brief Default constructor. Constructs an area with an uninitialized value.
74  Area() = default;
75 
76  /// \brief Constructor. Constructs an area with a given value expressed in a given area unit.
77  Area(const NumericType value, const Unit::Area unit)
78  : DimensionalScalar<Unit::Area, NumericType>(value, unit) {}
79 
80  /// \brief Constructor. Constructs an area from two given lengths.
81  constexpr Area(const Length<NumericType>& length1, const Length<NumericType>& length2)
82  : Area<NumericType>(length1.Value() * length2.Value()) {}
83 
84  /// \brief Constructor. Constructs an area from a given volume and length.
85  constexpr Area(const Volume<NumericType>& volume, const Length<NumericType>& length);
86 
87  /// \brief Constructor. Constructs an area from a given scalar force magnitude and scalar traction
88  /// magnitude using the definition of traction.
89  constexpr Area(const ScalarForce<NumericType>& scalar_force,
90  const ScalarTraction<NumericType>& scalar_traction);
91 
92  /// \brief Constructor. Constructs an area from a given scalar force magnitude and static pressure
93  /// using the definition of pressure.
94  constexpr Area(const ScalarForce<NumericType>& scalar_force,
95  const StaticPressure<NumericType>& static_pressure);
96 
97  /// \brief Destructor. Destroys this area.
98  ~Area() noexcept = default;
99 
100  /// \brief Copy constructor. Constructs an area by copying another one.
101  constexpr Area(const Area<NumericType>& other) = default;
102 
103  /// \brief Copy constructor. Constructs an area by copying another one.
104  template <typename OtherNumericType>
105  explicit constexpr Area(const Area<OtherNumericType>& other)
106  : Area(static_cast<NumericType>(other.Value())) {}
107 
108  /// \brief Move constructor. Constructs an area by moving another one.
109  constexpr Area(Area<NumericType>&& other) noexcept = default;
110 
111  /// \brief Copy assignment operator. Assigns this area by copying another one.
112  constexpr Area<NumericType>& operator=(const Area<NumericType>& other) = default;
113 
114  /// \brief Copy assignment operator. Assigns this area by copying another one.
115  template <typename OtherNumericType>
117  this->value = static_cast<NumericType>(other.Value());
118  return *this;
119  }
120 
121  /// \brief Move assignment operator. Assigns this area by moving another one.
122  constexpr Area<NumericType>& operator=(Area<NumericType>&& other) noexcept = default;
123 
124  /// \brief Statically creates an area of zero.
125  [[nodiscard]] static constexpr Area<NumericType> Zero() {
126  return Area<NumericType>{static_cast<NumericType>(0)};
127  }
128 
129  /// \brief Statically creates an area with a given value expressed in a given area unit.
130  template <Unit::Area Unit>
131  [[nodiscard]] static constexpr Area<NumericType> Create(const NumericType value) {
132  return Area<NumericType>{ConvertStatically<Unit::Area, Unit, Standard<Unit::Area>>(value)};
133  }
134 
135  constexpr Area<NumericType> operator+(const Area<NumericType>& area) const {
136  return Area<NumericType>{this->value + area.value};
137  }
138 
139  constexpr Area<NumericType> operator-(const Area<NumericType>& area) const {
140  return Area<NumericType>{this->value - area.value};
141  }
142 
143  constexpr Area<NumericType> operator*(const NumericType number) const {
144  return Area<NumericType>{this->value * number};
145  }
146 
147  constexpr Volume<NumericType> operator*(const Length<NumericType>& length) const;
148 
150  const ScalarTraction<NumericType>& scalar_traction) const;
151 
153  const StaticPressure<NumericType>& static_pressure) const;
154 
155  constexpr VectorArea<NumericType> operator*(const Direction<NumericType>& direction) const;
156 
157  constexpr Area<NumericType> operator/(const NumericType number) const {
158  return Area<NumericType>{this->value / number};
159  }
160 
161  constexpr Length<NumericType> operator/(const Length<NumericType>& length) const {
162  return Length<NumericType>{*this, length};
163  }
164 
165  constexpr NumericType operator/(const Area<NumericType>& area) const noexcept {
166  return this->value / area.value;
167  }
168 
169  constexpr void operator+=(const Area<NumericType>& area) noexcept {
170  this->value += area.value;
171  }
172 
173  constexpr void operator-=(const Area<NumericType>& area) noexcept {
174  this->value -= area.value;
175  }
176 
177  constexpr void operator*=(const NumericType number) noexcept {
178  this->value *= number;
179  }
180 
181  constexpr void operator/=(const NumericType number) noexcept {
182  this->value /= number;
183  }
184 
185 private:
186  /// \brief Constructor. Constructs an area with a given value expressed in the standard area unit.
187  explicit constexpr Area(const NumericType value)
188  : DimensionalScalar<Unit::Area, NumericType>(value) {}
189 
190  template <typename OtherArea>
191  friend class VectorArea;
192 };
193 
194 template <typename NumericType>
195 inline constexpr bool operator==(
196  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
197  return left.Value() == right.Value();
198 }
199 
200 template <typename NumericType>
201 inline constexpr bool operator!=(
202  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
203  return left.Value() != right.Value();
204 }
205 
206 template <typename NumericType>
207 inline constexpr bool operator<(
208  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
209  return left.Value() < right.Value();
210 }
211 
212 template <typename NumericType>
213 inline constexpr bool operator>(
214  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
215  return left.Value() > right.Value();
216 }
217 
218 template <typename NumericType>
219 inline constexpr bool operator<=(
220  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
221  return left.Value() <= right.Value();
222 }
223 
224 template <typename NumericType>
225 inline constexpr bool operator>=(
226  const Area<NumericType>& left, const Area<NumericType>& right) noexcept {
227  return left.Value() >= right.Value();
228 }
229 
230 template <typename NumericType>
231 inline std::ostream& operator<<(std::ostream& stream, const Area<NumericType>& area) {
232  stream << area.Print();
233  return stream;
234 }
235 
236 template <typename NumericType>
237 inline constexpr Area<NumericType> operator*(
238  const NumericType number, const Area<NumericType>& area) {
239  return area * number;
240 }
241 
242 template <typename NumericType>
243 inline constexpr Length<NumericType>::Length(
244  const Area<NumericType>& area, const Length<NumericType>& length)
245  : Length<NumericType>(area.Value() / length.Value()) {}
246 
247 template <typename NumericType>
248 inline constexpr Area<NumericType> Length<NumericType>::operator*(
249  const Length<NumericType>& length) const {
250  return Area<NumericType>{*this, length};
251 }
252 
253 } // namespace PhQ
254 
255 namespace std {
256 
257 template <typename NumericType>
258 struct hash<PhQ::Area<NumericType>> {
259  inline size_t operator()(const PhQ::Area<NumericType>& area) const {
260  return hash<NumericType>()(area.Value());
261  }
262 };
263 
264 } // namespace std
265 
266 #endif // PHQ_AREA_HPP
Surface area or cross-sectional area. Can also represent a scalar component of a vector area or the m...
Definition: Area.hpp:71
constexpr Area< NumericType > operator+(const Area< NumericType > &area) const
Definition: Area.hpp:135
constexpr Area< NumericType > & operator=(const Area< OtherNumericType > &other)
Copy assignment operator. Assigns this area by copying another one.
Definition: Area.hpp:116
constexpr Area< NumericType > operator-(const Area< NumericType > &area) const
Definition: Area.hpp:139
constexpr ScalarForce< NumericType > operator*(const ScalarTraction< NumericType > &scalar_traction) const
constexpr Area< NumericType > & operator=(const Area< NumericType > &other)=default
Copy assignment operator. Assigns this area by copying another one.
constexpr ScalarForce< NumericType > operator*(const StaticPressure< NumericType > &static_pressure) const
constexpr void operator-=(const Area< NumericType > &area) noexcept
Definition: Area.hpp:173
constexpr VectorArea< NumericType > operator*(const Direction< NumericType > &direction) const
constexpr Length< NumericType > operator/(const Length< NumericType > &length) const
Definition: Area.hpp:161
constexpr Volume< NumericType > operator*(const Length< NumericType > &length) const
constexpr Area(const Volume< NumericType > &volume, const Length< NumericType > &length)
Constructor. Constructs an area from a given volume and length.
static constexpr Area< NumericType > Create(const NumericType value)
Statically creates an area with a given value expressed in a given area unit.
Definition: Area.hpp:131
constexpr void operator*=(const NumericType number) noexcept
Definition: Area.hpp:177
constexpr Area(Area< NumericType > &&other) noexcept=default
Move constructor. Constructs an area by moving another one.
~Area() noexcept=default
Destructor. Destroys this area.
constexpr Area< NumericType > & operator=(Area< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this area by moving another one.
constexpr NumericType operator/(const Area< NumericType > &area) const noexcept
Definition: Area.hpp:165
Area(const NumericType value, const Unit::Area unit)
Constructor. Constructs an area with a given value expressed in a given area unit.
Definition: Area.hpp:77
constexpr Area(const Length< NumericType > &length1, const Length< NumericType > &length2)
Constructor. Constructs an area from two given lengths.
Definition: Area.hpp:81
constexpr Area(const NumericType value)
Constructor. Constructs an area with a given value expressed in the standard area unit.
Definition: Area.hpp:187
constexpr void operator/=(const NumericType number) noexcept
Definition: Area.hpp:181
constexpr Area(const ScalarForce< NumericType > &scalar_force, const StaticPressure< NumericType > &static_pressure)
Constructor. Constructs an area from a given scalar force magnitude and static pressure using the def...
constexpr Area(const ScalarForce< NumericType > &scalar_force, const ScalarTraction< NumericType > &scalar_traction)
Constructor. Constructs an area from a given scalar force magnitude and scalar traction magnitude usi...
constexpr Area< NumericType > operator*(const NumericType number) const
Definition: Area.hpp:143
constexpr void operator+=(const Area< NumericType > &area) noexcept
Definition: Area.hpp:169
static constexpr Area< NumericType > Zero()
Statically creates an area of zero.
Definition: Area.hpp:125
constexpr Area< NumericType > operator/(const NumericType number) const
Definition: Area.hpp:157
Area()=default
Default constructor. Constructs an area with an uninitialized value.
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::Area 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
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
Length()=default
Default constructor. Constructs a length with an uninitialized value.
Scalar force component or magnitude of a force vector. For a three-dimensional Euclidean force vector...
Definition: ScalarForce.hpp:66
Scalar traction component or magnitude of a traction vector. Traction is similar to pressure; however...
Static pressure. Pressure of a fluid at rest. Not to be confused with dynamic pressure,...
Three-dimensional Euclidean vector area. Contains three components in Cartesian coordinates: x,...
Definition: VectorArea.hpp:48
Volume. For the time rate of change of volume, see PhQ::VolumeRate; see also PhQ::Time and PhQ::Frequ...
Definition: Volume.hpp:62
Force
Force units.
Definition: Force.hpp:53
Length
Length units.
Definition: Length.hpp:53
Area
Area units.
Definition: Area.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 Area< NumericType > operator*(const NumericType number, const Area< NumericType > &area)
Definition: Area.hpp:237
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)