Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
constants.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <limits>
11#include <type_traits>
12
13namespace momentum {
14
16template <typename T = float>
17[[nodiscard]] constexpr T nan() noexcept {
18 return std::numeric_limits<T>::quiet_NaN();
19}
20
27// TODO: Add a static_assert or constexpr error path for unsupported T to avoid
28// silent undefined-return-value behavior for non-float/double types.
29template <typename T>
30[[nodiscard]] constexpr T Eps(T FloatEps = T(1e-7), T DoubleEps = T(1e-16)) {
31 if constexpr (std::is_same_v<T, float>) {
32 return FloatEps;
33 } else if constexpr (std::is_same_v<T, double>) {
34 return DoubleEps;
35 }
36}
37
39template <typename T = float>
40[[nodiscard]] constexpr T ln2() noexcept {
41 return 0.69314718055994530942;
42}
43
49template <typename T = float>
50[[nodiscard]] constexpr T pi() noexcept {
51 return 3.14159265358979323846;
52}
53
59template <typename T = float>
60[[nodiscard]] constexpr T twopi() noexcept {
61 return T(2) * pi<T>();
62}
63
67template <typename T = float>
68[[nodiscard]] constexpr T toRad(T x = T(1)) noexcept {
69 static_assert(std::is_floating_point_v<T>, "toRad requires a floating point argument.");
70 return x * pi<T>() / T(180);
71}
72// TODO: Support Eigen types to provide consistent conversion function usage with a single argument.
73
77template <typename T = float>
78[[nodiscard]] constexpr T toDeg(T x = T(1)) noexcept {
79 static_assert(std::is_floating_point_v<T>, "toDeg requires a floating point argument.");
80 return x * T(180) / pi<T>();
81}
82
86template <typename T = float>
87[[nodiscard]] constexpr T toM(T x = T(1)) noexcept {
88 static_assert(std::is_floating_point_v<T>, "toM requires a floating point argument.");
89 return x * T(0.01);
90}
91
95template <typename T = float>
96[[nodiscard]] constexpr T toCm(T x = T(1)) noexcept {
97 static_assert(std::is_floating_point_v<T>, "toCm requires a floating point argument.");
98 return x * T(100);
99}
100
101} // namespace momentum
constexpr T toM(T x=T(1)) noexcept
Converts the given length x (in centimeters) to meters.
Definition constants.h:87
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
Definition constants.h:30
constexpr T toDeg(T x=T(1)) noexcept
Converts the given angle x (in radians) to degrees.
Definition constants.h:78
constexpr T toCm(T x=T(1)) noexcept
Converts the given length x (in meters) to centimeters.
Definition constants.h:96
constexpr T ln2() noexcept
Returns the natural logarithm of 2 (log_e 2).
Definition constants.h:40
constexpr T nan() noexcept
Returns a quiet NaN value of type T.
Definition constants.h:17
constexpr T toRad(T x=T(1)) noexcept
Converts the given angle x (in degrees) to radians.
Definition constants.h:68
constexpr T twopi() noexcept
Returns 2 * pi.
Definition constants.h:60
constexpr T pi() noexcept
Returns the mathematical constant pi.
Definition constants.h:50