1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2022 Blender Foundation. */
#pragma once
/** \file
* \ingroup bli
*/
#include <algorithm>
#include <cmath>
#include <type_traits>
#include "BLI_math_base_safe.h"
#include "BLI_math_vec_types.hh"
#include "BLI_utildefines.h"
#ifdef WITH_GMP
# include "BLI_math_mpq.hh"
#endif
namespace blender::math {
/* To avoid being overly specific about what a "basic" type is, for now simply allow anything that
* isn't a `vec_base` type. In the future, if another implementation of these functions is needed,
* this would have to become more specific. */
#define BLI_ENABLE_IF_BASE(T) BLI_ENABLE_IF((!is_math_vec_type<T>))
#ifdef WITH_GMP
# define BLI_ENABLE_IF_FLT(T) \
BLI_ENABLE_IF_BASE(T), \
BLI_ENABLE_IF((std::is_floating_point_v<T> || std::is_same_v<T, mpq_class>))
#else
# define BLI_ENABLE_IF_FLT(T) BLI_ENABLE_IF_BASE(T), BLI_ENABLE_IF((std::is_floating_point_v<T>))
#endif
#define BLI_ENABLE_IF_INT(T) BLI_ENABLE_IF_BASE(T), BLI_ENABLE_IF((std::is_integral_v<T>))
template<typename T, BLI_ENABLE_IF_BASE(T)> inline bool is_zero(const T &a)
{
return a == T(0);
}
template<typename T, BLI_ENABLE_IF_BASE(T)> inline bool is_any_zero(const T &a)
{
return is_zero(a);
}
template<typename T, BLI_ENABLE_IF_BASE(T)> inline T abs(const T &a)
{
return std::abs(a);
}
template<typename T, BLI_ENABLE_IF_BASE(T)> inline T min(const T &a, const T &b)
{
return std::min(a, b);
}
template<typename T, BLI_ENABLE_IF_BASE(T)> inline T max(const T &a, const T &b)
{
return std::max(a, b);
}
template<typename T, BLI_ENABLE_IF_BASE(T)> inline T clamp(const T &a, const T &min, const T &max)
{
return std::clamp(a, min, max);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T mod(const T &a, const T &b)
{
return std::fmod(a, b);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T safe_mod(const T &a, const T &b)
{
return (b != 0) ? std::fmod(a, b) : 0;
}
template<typename T, BLI_ENABLE_IF_BASE(T)>
inline void min_max(const T &vector, T &min_vec, T &max_vec)
{
min_vec = min(vector, min_vec);
max_vec = max(vector, max_vec);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T safe_divide(const T &a, const T &b)
{
return (b != 0) ? a / b : T(0.0f);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T floor(const T &a)
{
return std::floor(a);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T ceil(const T &a)
{
return std::ceil(a);
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T fract(const T &a)
{
return a - std::floor(a);
}
template<typename T, typename FactorT, BLI_ENABLE_IF_FLT(T), BLI_ENABLE_IF_FLT(FactorT)>
inline T interpolate(const T &a, const T &b, const FactorT &t)
{
return a * (1 - t) + b * t;
}
template<typename T, BLI_ENABLE_IF_FLT(T)> inline T midpoint(const T &a, const T &b)
{
return (a + b) * 0.5;
}
#undef BLI_ENABLE_IF_BASE
#undef BLI_ENABLE_IF_FLT
#undef BLI_ENABLE_IF_INT
} // namespace blender::math
|