16 #ifndef OPENKALMAN_ANGLE_HPP 17 #define OPENKALMAN_ANGLE_HPP 19 #include <type_traits> 21 #ifdef __cpp_lib_ranges 30 #include "values/functions/internal/update_real_part.hpp" 31 #include "values/math/sin.hpp" 32 #include "values/math/cos.hpp" 33 #include "values/math/atan2.hpp" 39 #include "linear-algebra/coordinates/interfaces/coordinate_descriptor_traits.hpp" 54 template<values::fixed Min = values::fixed_minus_pi<
long double>, values::fixed Max = values::fixed_pi<
long double>>
55 requires (values::fixed_number_of_v<Min> <= 0) and (values::
fixed_number_of_v<Max> > 0) and
60 template<
typename Min = values::fixed_minus_pi<
long double>,
typename Max = values::fixed_pi<
long double>>
64 #ifndef __cpp_concepts 65 static_assert(values::fixed<Min>);
66 static_assert(values::fixed<Max>);
67 static_assert(values::fixed_number_of_v<Min> <= 0);
68 static_assert(values::fixed_number_of_v<Max> > 0);
101 template<
typename Min,
typename Max>
110 #ifdef __cpp_concepts 113 static constexpr
bool angle =
130 template<
typename Min,
typename Max>
136 static constexpr
auto min = values::fixed_number_of_v<Min>;
137 static constexpr
auto max = values::fixed_number_of_v<Max>;
142 static constexpr
bool is_specialized =
true;
145 static constexpr
auto dimension = [](
const T&) {
return std::integral_constant<std::size_t, 1>{}; };
148 static constexpr
auto stat_dimension = [](
const T&) {
return std::integral_constant<std::size_t, 2>{}; };
151 static constexpr
auto is_euclidean = [](
const T&) {
return std::false_type{}; };
154 static constexpr
auto hash_code = [](
const T&) -> std::size_t
157 constexpr
auto min_float =
static_cast<float>(min);
158 constexpr
auto max_float =
static_cast<float>(max);
159 constexpr
float a = (min_float * 3.f + max_float * 2.f) / (max_float - min_float);
160 constexpr
auto bits = std::numeric_limits<std::size_t>::digits;
161 if constexpr (bits < 32)
return 0x62BB_uz +
static_cast<std::size_t
>(a * a * 0x1.p2f);
162 else if constexpr (bits < 64)
return 0x62BB0D37_uz +
static_cast<std::size_t
>(a * a * 0x1.p4f);
163 else return 0x62BB0D37A58D6F96_uz +
static_cast<std::size_t
>(a * a * 0x1.p8f);
168 template<
typename Scalar>
169 struct to_stat_collection
171 to_stat_collection() =
default;
172 explicit constexpr to_stat_collection(Scalar theta) : my_theta {std::move(theta)} {};
173 constexpr
auto operator()(std::size_t i)
const {
return i == 0 ?
values::cos(my_theta) :
values::sin(my_theta); };
186 static constexpr
auto 188 #ifdef __cpp_concepts 191 [](
const T&,
const auto& data_view) noexcept
196 Scalar cf {2 * numbers::pi_v<R> / (max - min)};
197 Scalar mid { R{max + min} * R{0.5}};
198 Scalar a =
collections::get(data_view, std::integral_constant<std::size_t, 0>{});
206 static constexpr
auto 208 #ifdef __cpp_concepts 211 [](
const T&,
const auto& data_view) noexcept
216 Scalar cf {2 * numbers::pi_v<R> / (max - min)};
217 Scalar mid { R{max + min} * R{0.5}};
218 Scalar x =
collections::get(data_view, std::integral_constant<std::size_t, 0>{});
219 Scalar y =
collections::get(data_view, std::integral_constant<std::size_t, 1>{});
220 #ifdef __cpp_lib_ranges 223 return ranges::views::single(
values::atan2(y, x) / cf + mid);
230 #ifdef __cpp_concepts 231 static constexpr
auto 232 wrap_impl(
auto&& a) -> std::decay_t<decltype(a)>
234 template<
typename Scalar>
235 static constexpr std::decay_t<Scalar>
236 wrap_impl(Scalar&& a)
240 if (ap >= min and ap < max)
242 return std::forward<decltype(a)>(a);
246 using R = std::decay_t<decltype(ap)>;
247 constexpr R period {max - min};
249 R ar {fmod(ap - R{min}, period)};
250 if (ar < 0)
return values::internal::update_real_part(std::forward<decltype(a)>(a), R{min} + ar + period);
251 else return values::internal::update_real_part(std::forward<decltype(a)>(a), R{min} + ar);
260 static constexpr
auto 262 #ifdef __cpp_concepts 265 [](
const T&,
auto&& data_view) noexcept
268 using D = decltype(data_view);
270 #ifdef __cpp_lib_ranges 271 if constexpr (std::ranges::output_range<D, Scalar>)
273 if constexpr (ranges::output_range<D, Scalar>)
276 Scalar& a =
collections::get(data_view, std::integral_constant<std::size_t, 0>{});
281 using V = decltype(data_view);
282 auto wrap_get = [](V& v,
auto i) {
return wrap_impl(
collections::get(v, std::move(i))); };
283 auto wrap_set = [](V& v,
auto i,
auto x) ->
auto&
286 ret = wrap_impl(std::move(x));
298 #endif //OPENKALMAN_ANGLE_HPP
Definition: basics.hpp:41
decltype(auto) constexpr get(Arg &&arg, I i)
A generalization of std::get.
Definition: get.hpp:62
A fixed version of 2*pi.
Definition: fixed-constants.hpp:73
typename common_collection_type< T >::type common_collection_type_t
Helper template for common_collection_type.
Definition: common_collection_type.hpp:54
Definition: tuple_reverse.hpp:103
Definition for collections::generate_view and collections::views::generate.
constexpr auto sin(const Arg &arg)
Constexpr alternative to the std::sin function.
Definition: sin.hpp:43
constexpr auto cos(const Arg &arg, std::enable_if_t< values::value< Arg >, int >=0)
Constexpr alternative to the std::cos function.
Definition: cos.hpp:43
Definition for collections::collection_view.
Definition: compares_with.hpp:28
constexpr auto fixed_number_of_v
Helper template for fixed_number_of.
Definition: fixed_number_of.hpp:84
Definitions relating to the availability of c++ language features.
constexpr auto atan2(const Y &y_arg, const X &x_arg)
Constexpr alternative to the std::atan2 function.
Definition: atan2.hpp:46
Definition for collections::update_view and collections::views::update.
constexpr auto real(Arg arg)
A constexpr function to obtain the real part of a (complex) number.
Definition: real.hpp:40
constexpr bool collection_view
A view to a collection which is also a std::ranges:view.
Definition: collection_view.hpp:36
Traits for coordinates::pattern objects.
Definition: coordinate_descriptor_traits.hpp:41
constexpr detail::generate_adaptor generate
a collection_view generator associated with generate_view.
Definition: generate.hpp:416
Definitions implementing features of the c++ ranges library for compatibility.
std::decay_t< decltype(values::to_number(std::declval< T >()))> number_type_of_t
Obtain the values::number type associated with avalues::value.
Definition: number_type_of_t.hpp:34
An angle or any other simple modular value.
Definition: Angle.hpp:62
Definition for collections::common_collection_type.
std::decay_t< decltype(values::real(std::declval< T >()))> real_type_of_t
Obtain the real type associated with a number (typically a values::complex number.
Definition: real_type_of_t.hpp:35
constexpr detail::update_adaptor update
a std::ranges::range_adaptor_closure associated with update_view.
Definition: update.hpp:403