Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Angle.hpp
Go to the documentation of this file.
1#pragma once
2#include "Constants.hpp"
3#include <cmath>
4#include <stdexcept>
5#include <functional>
7
8namespace fishnet::math::__impl{
14template<typename AngleImpl>
15class AbstractAngle{
16private:
17 AngleImpl& impl(){
18 return static_cast<AngleImpl &>(*this);
19 }
20
21 const AngleImpl & impl() const {
22 return static_cast<const AngleImpl &>(*this);
23 }
24
25protected:
26 double angle;
27
28 double normalizedAngle(const double newAngle)const{
29 return impl().normalizedAngle(newAngle);
30 }
31
32 void setAngle(const double newAngle){
33 [[unlikely]] if (std::isnan(newAngle))
34 throw std::invalid_argument("NaN cannot be converted to angle!");
35 this->angle = normalizedAngle(newAngle);
36 }
37
38 AbstractAngle() : angle(0.0) {};
39
40public:
41
42 static AngleImpl asin(double sine){
43 return AngleImpl::asin(sine);
44 }
45
46 static AngleImpl acos(double cosine){
47 return AngleImpl::acos(cosine);
48 }
49
50 static AngleImpl atan(double tangent){
51 return AngleImpl::atan(tangent);
52 }
53
54 static AngleImpl atan2(double y, double x){
55 return AngleImpl::atan2(y,x);
56 }
57
58 AngleImpl operator - () const {
59 return AngleImpl(-angle);
60 }
61
62 AngleImpl operator + (const AngleImpl & other) const {
63 return AngleImpl(angle + other.angle);
64 }
65
66 AngleImpl operator - (const AngleImpl & other) const {
67 return AngleImpl(angle - other.angle);
68 }
69
70 AngleImpl & operator+=(const AngleImpl & other) {
71 setAngle(this->angle + other.angle);
72 return impl();
73 }
74
75 AngleImpl & operator-=(const AngleImpl & other){
76 setAngle(this->angle + other.angle);
77 return impl();
78 }
79
80 bool operator>(const AngleImpl & other) const {
81 if(*this == other) return false;
82 return angle > other.angle;
83 }
84
85 bool operator>=(const AngleImpl & other) const {
86 return *this == other || *this > other;
87 }
88
89 bool operator==(const AngleImpl & other) const {
90 return fabs(angle-other.angle) < DOUBLE_EPSILON;
91 }
92
93 bool operator<=(const AngleImpl & other) const {
94 return *this == other || *this < other;
95 }
96
97 bool operator< (const AngleImpl & other) const {
98 if(*this == other) return false;
99 return angle < other.angle;
100 }
101
102 double sin() const {
103 return impl().sin();
104 }
105
106 double cos() const {
107 return impl().cos();
108 }
109
110 double tan() const {
111 return impl().tan();
112 }
113
114 double getAngleValue() const {
115 return angle;
116 }
117
118 size_t hash() const noexcept {
119 return std::hash<double>{}(this->getAngleValue());
120 }
121};
122}
123namespace fishnet::math{
124// Forward declaration
125class Radians;
126class Degrees;
127}
Definition NumericConcepts.hpp:4
static constexpr double DOUBLE_EPSILON
Definition Constants.hpp:10