24#ifndef VULPES_UTILS_VMATH_HPP_
25#define VULPES_UTILS_VMATH_HPP_
51 : std::integral_constant<
53 std::is_same_v<T, mpf_class> || std::is_same_v<T, mpq_class> ||
54 std::is_same_v<T, mpz_class>> {};
69 is_gnump<T>::value || std::is_arithmetic_v<T>> {
84 : std::integral_constant<
86 std::is_same_v<T, mpf_class> || std::is_floating_point_v<T>> {};
106 typename = std::enable_if_t<std::is_unsigned_v<V> && std::is_integral_v<V>>>
107inline constexpr U
upow(U base, V exp, U result = U(1)) {
140 typename = std::enable_if_t<std::is_signed_v<V> && std::is_integral_v<V>>>
141inline constexpr U
ipow(U base, V exp, U result = U(1)) {
144 base = result / base;
146 static_assert(
sizeof(V) <=
sizeof(std::size_t));
147 return upow(base,
static_cast<std::size_t
>(exp));
170 typename = std::enable_if_t<std::is_integral_v<V>>>
171inline constexpr U
pow(U base, V exp, U result = U(1)) {
172 if constexpr (std::is_signed_v<V>) {
173 return ipow(base, exp, result);
175 return upow(base, exp, result);
191template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
192inline T
abs(
const T& value) {
193 if constexpr (std::is_arithmetic_v<T>) {
194 return std::abs(value);
200template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
202 if constexpr (std::is_arithmetic_v<T>) {
203 return std::sqrt(value);
205 return ::sqrt(value);
221template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
223 if constexpr (std::is_floating_point_v<T>) {
224#ifdef __cpp_lib_math_constants
225 return std::numbers::pi_v<T>;
234 "14159265358979323846264338327950288419716939937510582097494459230781"
235 "64062862089986280348253421170679821480865132823066470938446095505822"
236 "31725359408128481117450284102701938521105559644622948954930381964428"
237 "81097566593344612847564823378678316527120190914564856692346034861045"
238 "43266482133936072602491412737245870066063155881748815209209628292540"
239 "91715364367892590360011330530548820466521384146951941511609433057270"
240 "36575959195309218611738193261179310511854807446237996274956735188575"
241 "27248912279381830119491298336733624406566430860213949463952247371907"
242 "02179860943702770539217176293176752384674818467669405132000568127145"
243 "26356082778577134275778960917363717872146844090122495343014654958537"
244 "10507922796892589235420199561121290219608640344181598136297747713099"
245 "60518707211349999998372978049951059731732816096318595024459455346908"
246 "30264252230825334468503526193118817101000313783875288658753320838142"
247 "06171776691473035982534904287554687311595628638823537875937519577818"
248 "57780532171226806613001927876611195909216420198938095257201065485863");
269template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
270constexpr T
epsilon(
const T& instance = T(0)) {
271 if constexpr (std::is_floating_point_v<T>) {
272 return std::numeric_limits<T>::epsilon();
275 return upow(mpf_class(
"1.0e-1"), instance.get_prec() / 4);
292template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
294 if constexpr (std::is_floating_point_v<T>) {
295 constexpr std::size_t BITS_IN_BYTE = 8UL;
296 return sizeof(T) * BITS_IN_BYTE;
298 return instance.get_prec();
319 typename = std::enable_if_t<
320 is_pseudoaritmetic<U>::value && is_pseudoaritmetic<V>::value>>
321inline V
to(
const U& src) {
322 if constexpr (std::is_same_v<U, V>) {
324 }
else if constexpr (std::is_arithmetic_v<U> && std::is_arithmetic_v<V>) {
325 return static_cast<V
>(src);
328 }
else if constexpr (
330 sizeof(V) <=
sizeof(double)) {
331 return static_cast<V
>(src.get_d());
334 static_assert(!
sizeof(U),
"Not implemented 'to' for this type");
347template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
357template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
359 if constexpr (std::is_integral_v<T>) {
361 }
else if constexpr (std::is_arithmetic_v<T>) {
362 return std::tgamma(value);
364 return ::gamma(value);
380template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
381inline constexpr int64_t
pow1(T exp) {
382 return 1LL - ((exp & 1ULL) << 1ULL);
385template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
392 for (T i = 0; i < k; ++i) {
410template <
typename T,
int Options>
411inline T
sarrus_det(
const Eigen::Matrix<T, 3, 3, Options>& mat) {
412 return mat(0, 0) * mat(1, 1) * mat(2, 2) + mat(1, 0) * mat(2, 1) * mat(0, 2) +
413 mat(2, 0) * mat(0, 1) * mat(1, 2) - mat(2, 0) * mat(1, 1) * mat(0, 2) -
414 mat(1, 0) * mat(0, 1) * mat(2, 2) - mat(0, 0) * mat(2, 1) * mat(1, 2);
427template <
typename T,
int Dim,
int Options>
428inline T
determinant(
const Eigen::Matrix<T, Dim, Dim, Options>& mat) {
429 if constexpr (Dim == 2) {
430 return mat(0, 0) * mat(1, 1) - mat(0, 1) * mat(1, 0);
431 }
else if constexpr (Dim == 3) {
434 static_assert(Dim * 0,
"Inline determinant only for Dim 2 or 3");
438template <
typename T,
int Dim,
int Options>
439inline T
area(
const Eigen::Matrix<T, Dim, Dim - 1, Options>& mat) {
440 if constexpr (Dim == 2) {
442 }
else if constexpr (Dim == 3) {
443 return mat.col(0).cross(mat.col(1)).norm() / 2.0;
445 static_assert(Dim * 0,
"Inline determinant only for Dim 2 or 3");
476template <
typename FloatType,
typename IndexType>
478 const FloatType* vertices_data,
479 const IndexType* face_indices_data,
480 IndexType num_face_vertices,
481 FloatType* out_normal) {
482 for (IndexType i = 0; i < num_face_vertices; ++i) {
484 const IndexType idx_current = face_indices_data[i];
485 const IndexType idx_next = face_indices_data[(i + 1) % num_face_vertices];
488 const FloatType* p_current = vertices_data + idx_current * 3;
489 const FloatType* p_next = vertices_data + idx_next * 3;
492 out_normal[0] += (p_current[1] - p_next[1]) * (p_current[2] + p_next[2]);
493 out_normal[1] += (p_current[2] - p_next[2]) * (p_current[0] + p_next[0]);
494 out_normal[2] += (p_current[0] - p_next[0]) * (p_current[1] + p_next[1]);
497 const FloatType norm = std::sqrt(
498 out_normal[0] * out_normal[0] + out_normal[1] * out_normal[1] +
499 out_normal[2] * out_normal[2]);
502 out_normal[0] /= norm;
503 out_normal[1] /= norm;
504 out_normal[2] /= norm;
constexpr int64_t pow1(T exp)
Computes (-1)^exp.
Definition vmath.hpp:381
T area(const Eigen::Matrix< T, Dim, Dim - 1, Options > &mat)
Definition vmath.hpp:439
T gamma(const T &value)
Definition vmath.hpp:358
constexpr T factorial(T n)
Computes the factorial of a pseudo-arithmetic number iteratively.
Definition vmath.hpp:348
constexpr U ipow(U base, V exp, U result=U(1))
Computes the signed power of a number.
Definition vmath.hpp:141
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
constexpr T epsilon(const T &instance=T(0))
Returns the machine epsilon for floating-point types or a rough approximation for GMP types.
Definition vmath.hpp:270
constexpr U upow(U base, V exp, U result=U(1))
Computes the unsigned power of a number.
Definition vmath.hpp:107
void newellNormal(const FloatType *vertices_data, const IndexType *face_indices_data, IndexType num_face_vertices, FloatType *out_normal)
Accumulates the unnormalized normal of a polygon using Newell's method.
Definition vmath.hpp:477
constexpr T binomial(T n, T k)
Definition vmath.hpp:386
T abs(const T &value)
Computes the absolute value of a number.
Definition vmath.hpp:192
T sqrt(const T &value)
Definition vmath.hpp:201
T sarrus_det(const Eigen::Matrix< T, 3, 3, Options > &mat)
Computes the determinant of a 3x3 matrix using Sarrus' rule.
Definition vmath.hpp:411
T determinant(const Eigen::Matrix< T, Dim, Dim, Options > &mat)
Computes the determinant of a 2x2 or 3x3 matrix.
Definition vmath.hpp:428
std::size_t precision(const T &instance)
Returns the precision of the input value in bits.
Definition vmath.hpp:293
constexpr U pow(U base, V exp, U result=U(1))
Computes the power of a number.
Definition vmath.hpp:171
constexpr T pi()
Returns the value of pi.
Definition vmath.hpp:222
Definition cell_manager.hpp:38
Type trait to check if a type is one of the GNU Multiple Precision (GMP) classes (mpf_class,...
Definition vmath.hpp:54
Type trait to check if a type is either a GMP class or a built-in arithmetic type.
Definition vmath.hpp:69
Type trait to check if a type is either the GMP floating-point class (mpf_class) or a built-in floati...
Definition vmath.hpp:86