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
Angle.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_ANGLE_HPP
26#define PHQ_ANGLE_HPP
27
28#include <cmath>
29#include <cstddef>
30#include <functional>
31#include <ostream>
32
33#include "DimensionalScalar.hpp"
34#include "PlanarVector.hpp"
35#include "Unit/Angle.hpp"
36#include "Vector.hpp"
37
38namespace PhQ {
39
40// Forward declaration for class PhQ::Angle.
41template <typename NumericType>
42class Acceleration;
43
44// Forward declaration for class PhQ::Angle.
45template <typename NumericType>
46class AngularSpeed;
47
48// Forward declaration for class PhQ::Angle.
49template <typename NumericType>
50class Direction;
51
52// Forward declaration for class PhQ::Angle.
53template <typename NumericType>
54class Displacement;
55
56// Forward declaration for class PhQ::Angle.
57template <typename NumericType>
58class Time;
59
60// Forward declaration for class PhQ::Angle.
61template <typename NumericType>
62class Force;
63
64// Forward declaration for class PhQ::Angle.
65template <typename NumericType>
66class Frequency;
67
68// Forward declaration for class PhQ::Angle.
69template <typename NumericType>
70class HeatFlux;
71
72// Forward declaration for class PhQ::Angle.
73template <typename NumericType>
74class PlanarAcceleration;
75
76// Forward declaration for class PhQ::Angle.
77template <typename NumericType>
78class PlanarDirection;
79
80// Forward declaration for class PhQ::Angle.
81template <typename NumericType>
82class PlanarDisplacement;
83
84// Forward declaration for class PhQ::Angle.
85template <typename NumericType>
86class PlanarForce;
87
88// Forward declaration for class PhQ::Angle.
89template <typename NumericType>
90class PlanarHeatFlux;
91
92// Forward declaration for class PhQ::Angle.
93template <typename NumericType>
94class PlanarPosition;
95
96// Forward declaration for class PhQ::Angle.
97template <typename NumericType>
98class PlanarTemperatureGradient;
99
100// Forward declaration for class PhQ::Angle.
101template <typename NumericType>
102class PlanarTraction;
103
104// Forward declaration for class PhQ::Angle.
105template <typename NumericType>
106class PlanarVelocity;
107
108// Forward declaration for class PhQ::Angle.
109template <typename NumericType>
110class Position;
111
112// Forward declaration for class PhQ::Angle.
113template <typename NumericType>
115
116// Forward declaration for class PhQ::Angle.
117template <typename NumericType>
118class Traction;
119
120// Forward declaration for class PhQ::Angle.
121template <typename NumericType>
122class VectorArea;
123
124// Forward declaration for class PhQ::Angle.
125template <typename NumericType>
126class Velocity;
127
128/// \brief Plane angle between two lines or dihedral angle between two planes.
129template <typename NumericType = double>
130class Angle : public DimensionalScalar<Unit::Angle, NumericType> {
131public:
132 /// \brief Default constructor. Constructs an angle with an uninitialized value.
133 Angle() = default;
134
135 /// \brief Constructor. Constructs an angle with a given value expressed in a given angle unit.
136 Angle(const NumericType value, const Unit::Angle unit)
137 : DimensionalScalar<Unit::Angle, NumericType>(value, unit) {}
138
139 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
140 /// vectors.
141 Angle(const PlanarVector<NumericType>& planar_vector_1,
142 const PlanarVector<NumericType>& planar_vector_2)
143 : Angle(std::acos(planar_vector_1.Dot(planar_vector_2)
144 / (planar_vector_1.Magnitude() * planar_vector_2.Magnitude()))) {}
145
146 /// \brief Constructor. Constructs an angle by computing the angle between two given vectors.
147 Angle(const Vector<NumericType>& vector1, const Vector<NumericType>& vector2)
148 : Angle(std::acos(vector1.Dot(vector2) / (vector1.Magnitude() * vector2.Magnitude()))) {}
149
150 /// \brief Constructor. Constructs an angle by computing the angle between a given planar vector
151 /// and planar direction.
152 Angle(const PlanarVector<NumericType>& planar_vector,
153 const PlanarDirection<NumericType>& planar_direction);
154
155 /// \brief Constructor. Constructs an angle by computing the angle between a given vector and
156 /// direction.
157 Angle(const Vector<NumericType>& vector, const Direction<NumericType>& direction);
158
159 /// \brief Constructor. Constructs an angle by computing the angle between a given planar
160 /// direction and planar vector.
161 Angle(const PlanarDirection<NumericType>& planar_direction,
162 const PlanarVector<NumericType>& planar_vector);
163
164 /// \brief Constructor. Constructs an angle by computing the angle between a given direction and
165 /// vector.
166 Angle(const Direction<NumericType>& direction, const Vector<NumericType>& vector);
167
168 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
169 /// directions.
170 Angle(const PlanarDirection<NumericType>& planar_direction_1,
171 const PlanarDirection<NumericType>& planar_direction_2);
172
173 /// \brief Constructor. Constructs an angle by computing the angle between two given directions.
174 Angle(const Direction<NumericType>& direction1, const Direction<NumericType>& direction2);
175
176 /// \brief Constructor. Constructs an angle from a given angular speed and time using the
177 /// definition of angular speed.
178 constexpr Angle(const AngularSpeed<NumericType>& angular_speed, const Time<NumericType>& time);
179
180 /// \brief Constructor. Constructs an angle from a given angular speed and frequency using the
181 /// definition of angular speed.
182 constexpr Angle(
183 const AngularSpeed<NumericType>& angular_speed, const Frequency<NumericType>& frequency);
184
185 /// \brief Constructor. Constructs an angle by computing the angle between two given acceleration
186 /// vectors.
187 Angle(const Acceleration<NumericType>& acceleration1,
188 const Acceleration<NumericType>& acceleration2);
189
190 /// \brief Constructor. Constructs an angle by computing the angle between two given vector areas.
191 Angle(const VectorArea<NumericType>& vector_area_1, const VectorArea<NumericType>& vector_area_2);
192
193 /// \brief Constructor. Constructs an angle by computing the angle between two given
194 /// displacements.
195 Angle(const Displacement<NumericType>& displacement1,
196 const Displacement<NumericType>& displacement2);
197
198 /// \brief Constructor. Constructs an angle by computing the angle between two given forces.
199 Angle(const Force<NumericType>& force1, const Force<NumericType>& force2);
200
201 /// \brief Constructor. Constructs an angle by computing the angle between two given heat fluxes.
202 Angle(const HeatFlux<NumericType>& heat_flux_1, const HeatFlux<NumericType>& heat_flux_2);
203
204 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
205 /// acceleration vectors.
206 Angle(const PlanarAcceleration<NumericType>& planar_acceleration_1,
207 const PlanarAcceleration<NumericType>& planar_acceleration_2);
208
209 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
210 /// displacements.
211 Angle(const PlanarDisplacement<NumericType>& planar_displacement_1,
212 const PlanarDisplacement<NumericType>& planar_displacement_2);
213
214 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
215 /// forces.
216 Angle(const PlanarForce<NumericType>& planar_force_1,
217 const PlanarForce<NumericType>& planar_force_2);
218
219 /// \brief Constructor. Constructs an angle by computing the angle between two given planar heat
220 /// fluxes.
221 Angle(const PlanarHeatFlux<NumericType>& planar_heat_flux_1,
222 const PlanarHeatFlux<NumericType>& planar_heat_flux_2);
223
224 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
225 /// positions.
226 Angle(const PlanarPosition<NumericType>& planar_position_1,
227 const PlanarPosition<NumericType>& planar_position_2);
228
229 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
230 /// temperature gradients.
231 Angle(const PlanarTemperatureGradient<NumericType>& planar_temperature_gradient_1,
232 const PlanarTemperatureGradient<NumericType>& planar_temperature_gradient_2);
233
234 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
235 /// tractions.
236 Angle(const PlanarTraction<NumericType>& planar_traction_1,
237 const PlanarTraction<NumericType>& planar_traction_2);
238
239 /// \brief Constructor. Constructs an angle by computing the angle between two given planar
240 /// velocities.
241 Angle(const PlanarVelocity<NumericType>& planar_velocity_1,
242 const PlanarVelocity<NumericType>& planar_velocity_2);
243
244 /// \brief Constructor. Constructs an angle by computing the angle between two given positions.
245 Angle(const Position<NumericType>& position1, const Position<NumericType>& position2);
246
247 /// \brief Constructor. Constructs an angle by computing the angle between two given temperature
248 /// gradients.
249 Angle(const TemperatureGradient<NumericType>& temperature_gradient_1,
250 const TemperatureGradient<NumericType>& temperature_gradient_2);
251
252 /// \brief Constructor. Constructs an angle by computing the angle between two given tractions.
253 Angle(const Traction<NumericType>& traction1, const Traction<NumericType>& traction2);
254
255 /// \brief Constructor. Constructs an angle by computing the angle between two given velocities.
256 Angle(const Velocity<NumericType>& velocity1, const Velocity<NumericType>& velocity2);
257
258 /// \brief Destructor. Destroys this angle.
259 ~Angle() noexcept = default;
260
261 /// \brief Copy constructor. Constructs an angle by copying another one.
262 constexpr Angle(const Angle<NumericType>& other) = default;
263
264 /// \brief Copy constructor. Constructs an angle by copying another one.
265 template <typename OtherNumericType>
266 explicit constexpr Angle(const Angle<OtherNumericType>& other)
267 : Angle(static_cast<NumericType>(other.Value())) {}
268
269 /// \brief Move constructor. Constructs an angle by moving another one.
270 constexpr Angle(Angle<NumericType>&& other) noexcept = default;
271
272 /// \brief Copy assignment operator. Assigns this angle by copying another one.
273 constexpr Angle<NumericType>& operator=(const Angle<NumericType>& other) = default;
274
275 /// \brief Copy assignment operator. Assigns this angle by copying another one.
276 template <typename OtherNumericType>
278 this->value = static_cast<NumericType>(other.Value());
279 return *this;
280 }
281
282 /// \brief Move assignment operator. Assigns this angle by moving another one.
283 constexpr Angle<NumericType>& operator=(Angle<NumericType>&& other) noexcept = default;
284
285 /// \brief Statically creates an angle of zero.
286 [[nodiscard]] static constexpr Angle<NumericType> Zero() {
287 return Angle<NumericType>{static_cast<NumericType>(0)};
288 }
289
290 /// \brief Statically creates an angle with a given value expressed in a given angle unit.
291 template <Unit::Angle Unit>
292 [[nodiscard]] static constexpr Angle<NumericType> Create(const NumericType value) {
293 return Angle<NumericType>{ConvertStatically<Unit::Angle, Unit, Standard<Unit::Angle>>(value)};
294 }
295
296 constexpr Angle<NumericType> operator+(const Angle<NumericType>& other) const {
297 return Angle<NumericType>{this->value + other.value};
298 }
299
300 constexpr Angle<NumericType> operator-(const Angle<NumericType>& other) const {
301 return Angle<NumericType>{this->value - other.value};
302 }
303
304 constexpr Angle<NumericType> operator*(const NumericType number) const {
305 return Angle<NumericType>{this->value * number};
306 }
307
308 constexpr AngularSpeed<NumericType> operator*(const Frequency<NumericType>& frequency) const;
309
310 constexpr Angle<NumericType> operator/(const NumericType number) const {
311 return Angle<NumericType>{this->value / number};
312 }
313
314 constexpr AngularSpeed<NumericType> operator/(const Time<NumericType>& time) const;
315
316 constexpr Time<NumericType> operator/(const AngularSpeed<NumericType>& angular_speed) const;
317
318 constexpr NumericType operator/(const Angle<NumericType>& other) const noexcept {
319 return this->value / other.value;
320 }
321
322 constexpr void operator+=(const Angle<NumericType>& other) noexcept {
323 this->value += other.value;
324 }
325
326 constexpr void operator-=(const Angle<NumericType>& other) noexcept {
327 this->value -= other.value;
328 }
329
330 constexpr void operator*=(const NumericType number) noexcept {
331 this->value *= number;
332 }
333
334 constexpr void operator/=(const NumericType number) noexcept {
335 this->value /= number;
336 }
337
338private:
339 /// \brief Constructor. Constructs an angle with a given value expressed in the standard angle
340 /// unit.
341 explicit constexpr Angle(const NumericType value)
342 : DimensionalScalar<Unit::Angle, NumericType>(value) {}
343};
344
345template <typename NumericType>
346inline constexpr bool operator==(
347 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
348 return left.Value() == right.Value();
349}
350
351template <typename NumericType>
352inline constexpr bool operator!=(
353 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
354 return left.Value() != right.Value();
355}
356
357template <typename NumericType>
358inline constexpr bool operator<(
359 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
360 return left.Value() < right.Value();
361}
362
363template <typename NumericType>
364inline constexpr bool operator>(
365 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
366 return left.Value() > right.Value();
367}
368
369template <typename NumericType>
370inline constexpr bool operator<=(
371 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
372 return left.Value() <= right.Value();
373}
374
375template <typename NumericType>
376inline constexpr bool operator>=(
377 const Angle<NumericType>& left, const Angle<NumericType>& right) noexcept {
378 return left.Value() >= right.Value();
379}
380
381template <typename NumericType>
382inline std::ostream& operator<<(std::ostream& stream, const Angle<NumericType>& angle) {
383 stream << angle.Print();
384 return stream;
385}
386
387template <typename NumericType>
389 const NumericType number, const Angle<NumericType>& angle) {
390 return angle * number;
391}
392
393template <typename NumericType>
395 const PlanarVector<NumericType>& other) const {
396 return PhQ::Angle{*this, other};
397}
398
399template <typename NumericType>
401 return PhQ::Angle{*this, other};
402}
403
404} // namespace PhQ
405
406namespace std {
407
408template <typename NumericType>
409struct hash<PhQ::Angle<NumericType>> {
410 inline size_t operator()(const PhQ::Angle<NumericType>& angle) const {
411 return hash<NumericType>()(angle.Value());
412 }
413};
414
415} // namespace std
416
417#endif // PHQ_ANGLE_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
Plane angle between two lines or dihedral angle between two planes.
Definition Angle.hpp:130
Angle(const PlanarVector< NumericType > &planar_vector_1, const PlanarVector< NumericType > &planar_vector_2)
Constructor. Constructs an angle by computing the angle between two given planar vectors.
Definition Angle.hpp:141
Angle(const Vector< NumericType > &vector1, const Vector< NumericType > &vector2)
Constructor. Constructs an angle by computing the angle between two given vectors.
Definition Angle.hpp:147
constexpr NumericType operator/(const Angle< NumericType > &other) const noexcept
Definition Angle.hpp:318
constexpr Angle< NumericType > operator*(const NumericType number) const
Definition Angle.hpp:304
constexpr Angle< NumericType > & operator=(const Angle< NumericType > &other)=default
Copy assignment operator. Assigns this angle by copying another one.
constexpr Angle< NumericType > operator/(const NumericType number) const
Definition Angle.hpp:310
constexpr Angle(const NumericType value)
Constructor. Constructs an angle with a given value expressed in the standard angle unit.
Definition Angle.hpp:341
constexpr Angle< NumericType > operator+(const Angle< NumericType > &other) const
Definition Angle.hpp:296
constexpr void operator-=(const Angle< NumericType > &other) noexcept
Definition Angle.hpp:326
constexpr Angle< NumericType > & operator=(Angle< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this angle by moving another one.
static constexpr Angle< NumericType > Create(const NumericType value)
Statically creates an angle with a given value expressed in a given angle unit.
Definition Angle.hpp:292
static constexpr Angle< NumericType > Zero()
Statically creates an angle of zero.
Definition Angle.hpp:286
constexpr void operator+=(const Angle< NumericType > &other) noexcept
Definition Angle.hpp:322
constexpr Angle< NumericType > & operator=(const Angle< OtherNumericType > &other)
Copy assignment operator. Assigns this angle by copying another one.
Definition Angle.hpp:277
~Angle() noexcept=default
Destructor. Destroys this angle.
constexpr Angle< NumericType > operator-(const Angle< NumericType > &other) const
Definition Angle.hpp:300
constexpr void operator/=(const NumericType number) noexcept
Definition Angle.hpp:334
Angle(const NumericType value, const Unit::Angle unit)
Constructor. Constructs an angle with a given value expressed in a given angle unit.
Definition Angle.hpp:136
constexpr Angle(Angle< NumericType > &&other) noexcept=default
Move constructor. Constructs an angle by moving another one.
Angle()=default
Default constructor. Constructs an angle with an uninitialized value.
constexpr void operator*=(const NumericType number) noexcept
Definition Angle.hpp:330
Angular speed, also known as angular rate or angular frequency. Represents the time rate of change of...
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...
Three-dimensional Euclidean direction vector. Contains three components in Cartesian coordinates: x,...
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
Three-dimensional Euclidean force vector. Contains three components in Cartesian coordinates: x,...
Definition Force.hpp:52
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition Frequency.hpp:40
Three-dimensional Euclidean heat flux vector. Contains three components in Cartesian coordinates: x,...
Definition HeatFlux.hpp:50
Two-dimensional Euclidean acceleration vector in the XY plane. Contains two components in Cartesian c...
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 force vector in the XY plane. Contains two components in Cartesian coordina...
Two-dimensional Euclidean heat flux vector in the XY plane. Contains two components in Cartesian coor...
Two-dimensional Euclidean position vector in the XY plane. Contains two components in Cartesian coord...
Two-dimensional Euclidean temperature gradient vector in the XY plane. Contains two components in Car...
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...
PhQ::Angle< NumericType > Angle(const PlanarVector< NumericType > &other) const
Returns the angle between this two-dimensional planar vector and another one.
Definition Angle.hpp:394
Two-dimensional Euclidean velocity 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
Three-dimensional Euclidean temperature gradient vector. Contains three components in Cartesian coord...
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
Three-dimensional Euclidean traction vector. Contains three components in Cartesian coordinates: x,...
Definition Traction.hpp:54
Three-dimensional Euclidean vector area. Contains three components in Cartesian coordinates: x,...
Three-dimensional Euclidean vector. Contains three components in Cartesian coordinates: x,...
Definition Vector.hpp:60
PhQ::Angle< NumericType > Angle(const Vector< NumericType > &other) const
Returns the angle between this three-dimensional vector and another one.
Definition Angle.hpp:400
Three-dimensional Euclidean velocity vector. Contains three components in Cartesian coordinates: x,...
Definition Velocity.hpp:55
Force
Force units.
Definition Force.hpp:53
TemperatureGradient
Temperature gradient units.
Frequency
Frequency units.
Definition Frequency.hpp:53
Angle
Angle units.
Definition Angle.hpp:53
Acceleration
Acceleration units.
Time
Time units.
Definition Time.hpp:53
AngularSpeed
Angular speed units.
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