Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
SolidAngle.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_SOLID_ANGLE_HPP
26 #define PHQ_SOLID_ANGLE_HPP
27 
28 #include <cmath>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 
33 #include "DimensionalScalar.hpp"
34 #include "Unit/SolidAngle.hpp"
35 
36 namespace PhQ {
37 
38 /// \brief Solid angle. Measures the field of view of a portion of the surface of the unit sphere
39 /// viewed from the center of the unit sphere. Typically measured in steradians (sr), which are
40 /// square radians. The unit sphere has a total solid angle of 4π steradians.
41 template <typename NumericType = double>
42 class SolidAngle : public DimensionalScalar<Unit::SolidAngle, NumericType> {
43 public:
44  /// \brief Default constructor. Constructs a solid angle with an uninitialized value.
45  SolidAngle() = default;
46 
47  /// \brief Constructor. Constructs a solid angle with a given value expressed in a given solid
48  /// angle unit.
49  SolidAngle(const NumericType value, const Unit::SolidAngle unit)
50  : DimensionalScalar<Unit::SolidAngle, NumericType>(value, unit) {}
51 
52  /// \brief Destructor. Destroys this solid angle.
53  ~SolidAngle() noexcept = default;
54 
55  /// \brief Copy constructor. Constructs a solid angle by copying another one.
56  constexpr SolidAngle(const SolidAngle<NumericType>& other) = default;
57 
58  /// \brief Copy constructor. Constructs a solid angle by copying another one.
59  template <typename OtherNumericType>
60  explicit constexpr SolidAngle(const SolidAngle<OtherNumericType>& other)
61  : SolidAngle(static_cast<NumericType>(other.Value())) {}
62 
63  /// \brief Move constructor. Constructs a solid angle by moving another one.
64  constexpr SolidAngle(SolidAngle<NumericType>&& other) noexcept = default;
65 
66  /// \brief Copy assignment operator. Assigns this solid angle by copying another one.
67  constexpr SolidAngle<NumericType>& operator=(const SolidAngle<NumericType>& other) = default;
68 
69  /// \brief Copy assignment operator. Assigns this solid angle by copying another one.
70  template <typename OtherNumericType>
72  this->value = static_cast<NumericType>(other.Value());
73  return *this;
74  }
75 
76  /// \brief Move assignment operator. Assigns this solid angle by moving another one.
77  constexpr SolidAngle<NumericType>& operator=(SolidAngle<NumericType>&& other) noexcept = default;
78 
79  /// \brief Statically creates a solid angle of zero.
80  [[nodiscard]] static constexpr SolidAngle<NumericType> Zero() {
81  return SolidAngle<NumericType>{static_cast<NumericType>(0)};
82  }
83 
84  /// \brief Statically creates a solid angle with a given value expressed in a given solid angle
85  /// unit.
86  template <Unit::SolidAngle Unit>
87  [[nodiscard]] static constexpr SolidAngle<NumericType> Create(const NumericType value) {
89  ConvertStatically<Unit::SolidAngle, Unit, Standard<Unit::SolidAngle>>(value)};
90  }
91 
93  return SolidAngle<NumericType>{this->value + other.value};
94  }
95 
97  return SolidAngle<NumericType>{this->value - other.value};
98  }
99 
100  constexpr SolidAngle<NumericType> operator*(const NumericType number) const {
101  return SolidAngle<NumericType>{this->value * number};
102  }
103 
104  constexpr SolidAngle<NumericType> operator/(const NumericType number) const {
105  return SolidAngle<NumericType>{this->value / number};
106  }
107 
108  constexpr NumericType operator/(const SolidAngle<NumericType>& other) const noexcept {
109  return this->value / other.value;
110  }
111 
112  constexpr void operator+=(const SolidAngle<NumericType>& other) noexcept {
113  this->value += other.value;
114  }
115 
116  constexpr void operator-=(const SolidAngle<NumericType>& other) noexcept {
117  this->value -= other.value;
118  }
119 
120  constexpr void operator*=(const NumericType number) noexcept {
121  this->value *= number;
122  }
123 
124  constexpr void operator/=(const NumericType number) noexcept {
125  this->value /= number;
126  }
127 
128 private:
129  /// \brief Constructor. Constructs a solid angle with a given value expressed in the standard
130  /// solid angle unit.
131  explicit constexpr SolidAngle(const NumericType value)
132  : DimensionalScalar<Unit::SolidAngle, NumericType>(value) {}
133 };
134 
135 template <typename NumericType>
136 inline constexpr bool operator==(
137  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
138  return left.Value() == right.Value();
139 }
140 
141 template <typename NumericType>
142 inline constexpr bool operator!=(
143  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
144  return left.Value() != right.Value();
145 }
146 
147 template <typename NumericType>
148 inline constexpr bool operator<(
149  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
150  return left.Value() < right.Value();
151 }
152 
153 template <typename NumericType>
154 inline constexpr bool operator>(
155  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
156  return left.Value() > right.Value();
157 }
158 
159 template <typename NumericType>
160 inline constexpr bool operator<=(
161  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
162  return left.Value() <= right.Value();
163 }
164 
165 template <typename NumericType>
166 inline constexpr bool operator>=(
167  const SolidAngle<NumericType>& left, const SolidAngle<NumericType>& right) noexcept {
168  return left.Value() >= right.Value();
169 }
170 
171 template <typename NumericType>
172 inline std::ostream& operator<<(std::ostream& stream, const SolidAngle<NumericType>& solid_angle) {
173  stream << solid_angle.Print();
174  return stream;
175 }
176 
177 template <typename NumericType>
179  const NumericType number, const SolidAngle<NumericType>& solid_angle) {
180  return solid_angle * number;
181 }
182 
183 } // namespace PhQ
184 
185 namespace std {
186 
187 template <typename NumericType>
188 struct hash<PhQ::SolidAngle<NumericType>> {
189  inline size_t operator()(const PhQ::SolidAngle<NumericType>& solid_angle) const {
190  return hash<NumericType>()(solid_angle.Value());
191  }
192 };
193 
194 } // namespace std
195 
196 #endif // PHQ_SOLID_ANGLE_HPP
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::SolidAngle 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...
Solid angle. Measures the field of view of a portion of the surface of the unit sphere viewed from th...
Definition: SolidAngle.hpp:42
constexpr void operator/=(const NumericType number) noexcept
Definition: SolidAngle.hpp:124
constexpr SolidAngle< NumericType > operator/(const NumericType number) const
Definition: SolidAngle.hpp:104
constexpr void operator-=(const SolidAngle< NumericType > &other) noexcept
Definition: SolidAngle.hpp:116
SolidAngle(const NumericType value, const Unit::SolidAngle unit)
Constructor. Constructs a solid angle with a given value expressed in a given solid angle unit.
Definition: SolidAngle.hpp:49
constexpr SolidAngle(SolidAngle< NumericType > &&other) noexcept=default
Move constructor. Constructs a solid angle by moving another one.
constexpr void operator+=(const SolidAngle< NumericType > &other) noexcept
Definition: SolidAngle.hpp:112
~SolidAngle() noexcept=default
Destructor. Destroys this solid angle.
constexpr SolidAngle< NumericType > operator*(const NumericType number) const
Definition: SolidAngle.hpp:100
constexpr SolidAngle< NumericType > & operator=(const SolidAngle< OtherNumericType > &other)
Copy assignment operator. Assigns this solid angle by copying another one.
Definition: SolidAngle.hpp:71
SolidAngle()=default
Default constructor. Constructs a solid angle with an uninitialized value.
constexpr SolidAngle< NumericType > operator-(const SolidAngle< NumericType > &other) const
Definition: SolidAngle.hpp:96
constexpr SolidAngle< NumericType > & operator=(const SolidAngle< NumericType > &other)=default
Copy assignment operator. Assigns this solid angle by copying another one.
constexpr NumericType operator/(const SolidAngle< NumericType > &other) const noexcept
Definition: SolidAngle.hpp:108
static constexpr SolidAngle< NumericType > Zero()
Statically creates a solid angle of zero.
Definition: SolidAngle.hpp:80
constexpr SolidAngle< NumericType > operator+(const SolidAngle< NumericType > &other) const
Definition: SolidAngle.hpp:92
constexpr void operator*=(const NumericType number) noexcept
Definition: SolidAngle.hpp:120
static constexpr SolidAngle< NumericType > Create(const NumericType value)
Statically creates a solid angle with a given value expressed in a given solid angle unit.
Definition: SolidAngle.hpp:87
constexpr SolidAngle(const NumericType value)
Constructor. Constructs a solid angle with a given value expressed in the standard solid angle unit.
Definition: SolidAngle.hpp:131
constexpr SolidAngle< NumericType > & operator=(SolidAngle< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this solid angle by moving another one.
SolidAngle
Solid angle units. Measures the field of view of a portion of the surface of the unit sphere viewed f...
Definition: SolidAngle.hpp:55
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)