1 #ifndef MARNAV__GEO__ANGLE__HPP
2 #define MARNAV__GEO__ANGLE__HPP
12 /// @brief A geographic angle. This is the base class for Latitudes and Longitudes.
16 constexpr static double epsilon() noexcept
{ return 1.0e-8; }
18 angle(const angle
&) = default;
19 angle(angle
&&) noexcept
= default;
21 constexpr angle() noexcept
26 /// Initializes the angle with the specified angle in degrees.
28 /// @param[in] degrees Angle in degrees.
29 constexpr angle(double degrees
) noexcept
34 uint32_t degrees() const noexcept
;
35 uint32_t minutes() const noexcept
;
36 double seconds() const noexcept
;
38 /// Converts an angle to double, units: degrees.
39 constexpr operator double() const noexcept
{ return value_
; }
41 constexpr double get() const noexcept
{ return value_
; }
43 angle
& operator=(const angle
&) = default;
44 angle
& operator=(angle
&&) noexcept
= default;
46 friend void swap(angle
& a
, angle
& b
) noexcept
;
49 /// Sets the angle in degrees.
51 /// Not part of the public interface intentionally.
52 void set(double degrees
) noexcept
{ value_
= degrees
; }
55 double value_
; // angle in degrees
58 void swap(angle
& a
, angle
& b
) noexcept
;
59 bool operator==(const angle
& a
, const angle
& b
) noexcept
;
60 bool operator!=(const angle
& a
, const angle
& b
) noexcept
;
62 /// @brief Geographic Latitude
64 /// Value ranges between +90.0 (north) to -90.0 (south).
65 class latitude
: public angle
68 enum class hemisphere
{ north
, south
};
70 constexpr static double min() noexcept
{ return -90.0; }
71 constexpr static double max() noexcept
{ return +90.0; }
73 constexpr latitude() noexcept
78 latitude(double degrees
);
79 latitude(double degrees
, hemisphere h
);
80 latitude(uint32_t d
, uint32_t m
, uint32_t s
, hemisphere h
);
82 latitude(const latitude
&) = default;
83 latitude(latitude
&&) noexcept
= default;
85 latitude
& operator=(const latitude
&) = default;
86 latitude
& operator=(latitude
&&) noexcept
= default;
88 /// Deletion of this operator prevents comparison of apples and oranges.
89 bool operator==(const angle
&) const = delete;
91 /// Deletion of this operator prevents comparison of apples and oranges.
92 bool operator!=(const angle
&) const = delete;
94 /// Returns the corresponding hemisphere.
95 constexpr hemisphere
hem() const noexcept
97 return get() >= 0.0 ? hemisphere::north
: hemisphere::south
;
101 static void check(double a
);
104 bool operator==(const latitude
& a
, const latitude
& b
) noexcept
;
105 bool operator!=(const latitude
& a
, const latitude
& b
) noexcept
;
106 std::string
to_string(latitude::hemisphere h
);
109 /// User defined literal to construct latitudes.
111 inline latitude
operator"" _lat(long double value
)
113 return latitude
{static_cast<double>(value
)};
116 inline latitude
operator"" _north(long double value
)
118 return latitude
{static_cast<double>(value
)};
121 inline latitude
operator"" _south(long double value
)
123 return latitude
{static_cast<double>(-value
)};
128 /// @brief Geographic Longitude
130 /// Value ranges between -180.0 (west) to +180.0 (east).
131 class longitude
: public angle
134 enum class hemisphere
{ east
, west
};
136 constexpr static double min() noexcept
{ return -180.0; }
137 constexpr static double max() noexcept
{ return +180.0; }
139 constexpr longitude() noexcept
144 longitude(double degrees
);
145 longitude(double degrees
, hemisphere h
);
146 longitude(uint32_t d
, uint32_t m
, uint32_t s
, hemisphere h
);
148 longitude(const longitude
&) = default;
149 longitude(longitude
&&) noexcept
= default;
151 longitude
& operator=(const longitude
&) = default;
152 longitude
& operator=(longitude
&&) noexcept
= default;
154 /// Deletion of this operator prevents comparison of apples and oranges.
155 bool operator==(const angle
&) const = delete;
157 /// Deletion of this operator prevents comparison of apples and oranges.
158 bool operator!=(const angle
&) const = delete;
160 /// Returns the corresponding hemisphere.
161 constexpr hemisphere
hem() const noexcept
163 return get() < 0.0 ? hemisphere::west
: hemisphere::east
;
167 static void check(double a
);
170 bool operator==(const longitude
& a
, const longitude
& b
) noexcept
;
171 bool operator!=(const longitude
& a
, const longitude
& b
) noexcept
;
172 std::string
to_string(longitude::hemisphere h
);
175 /// User defined literal to construct latitudes.
177 inline longitude
operator"" _lon(long double value
)
179 return longitude
{static_cast<double>(value
)};
182 inline longitude
operator"" _east(long double value
)
184 return longitude
{static_cast<double>(value
)};
187 inline longitude
operator"" _west(long double value
)
189 return longitude
{static_cast<double>(-value
)};