Fix usage of math functions in geom::vector to be ADL-friendly
This commit is contained in:
parent
655dd2778f
commit
8617fac987
1 changed files with 38 additions and 19 deletions
|
|
@ -220,9 +220,11 @@ namespace psemek::geom
|
|||
template <typename T, std::size_t N>
|
||||
T linf_norm(vector<T, N> const & v)
|
||||
{
|
||||
T r = std::abs(v[0]);
|
||||
using std::abs;
|
||||
using std::max;
|
||||
T r = abs(v[0]);
|
||||
for (std::size_t i = 1; i < N; ++i)
|
||||
r = std::max(r, std::abs(v[i]));
|
||||
r = max(r, abs(v[i]));
|
||||
return r;
|
||||
}
|
||||
|
||||
|
|
@ -230,8 +232,9 @@ namespace psemek::geom
|
|||
T length(vector<T, N> const & v)
|
||||
{
|
||||
T const m = linf_norm(v);
|
||||
if (m == 0) return m;
|
||||
return m * std::sqrt(length_sqr(v / m));
|
||||
if (m == T(0)) return m;
|
||||
using std::sqrt;
|
||||
return m * sqrt(length_sqr(v / m));
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
|
|
@ -289,9 +292,11 @@ namespace psemek::geom
|
|||
{
|
||||
std::size_t i = 0, j = 1;
|
||||
|
||||
using std::abs;
|
||||
|
||||
for (std::size_t k = 1; k < N; ++k)
|
||||
{
|
||||
if (std::abs(v[k]) > std::abs(v[i]))
|
||||
if (abs(v[k]) > abs(v[i]))
|
||||
{
|
||||
j = i;
|
||||
i = k;
|
||||
|
|
@ -314,32 +319,41 @@ namespace psemek::geom
|
|||
template <typename T>
|
||||
vector<T, 2> direction(T angle)
|
||||
{
|
||||
return {std::cos(angle), std::sin(angle)};
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
return {cos(angle), sin(angle)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T angle(vector<T, 2> const & v)
|
||||
{
|
||||
return std::atan2(v[1], v[0]);
|
||||
using std::atan2;
|
||||
return atan2(v[1], v[0]);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T angle(vector<T, 2> const & v0, vector<T, 2> const & v1)
|
||||
{
|
||||
return std::atan2(det(v0, v1), dot(v0, v1));
|
||||
using std::atan2;
|
||||
return atan2(det(v0, v1), dot(v0, v1));
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
T angle(vector<T, N> const & v0, vector<T, N> const & v1)
|
||||
{
|
||||
return std::acos(std::min(T{1}, std::max(T{-1}, dot(v0, v1))));
|
||||
using std::acos;
|
||||
using std::min;
|
||||
using std::max;
|
||||
return acos(min(T{1}, max(T{-1}, dot(v0, v1))));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
vector<T, 2> rotate(vector<T, 2> const & v, T angle)
|
||||
{
|
||||
auto const c = std::cos(angle);
|
||||
auto const s = std::sin(angle);
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
auto const c = cos(angle);
|
||||
auto const s = sin(angle);
|
||||
return {v[0] * c - v[1] * s, v[0] * s + v[1] * c};
|
||||
}
|
||||
|
||||
|
|
@ -369,19 +383,19 @@ namespace psemek::geom
|
|||
template <typename T, std::size_t N>
|
||||
vector<T, N> pointwise_log(vector<T, N> const & v)
|
||||
{
|
||||
return pointwise([](T const & a){ return std::log(a); }, v);
|
||||
return pointwise([](T const & a){ using std::log; return log(a); }, v);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
vector<T, N> pointwise_exp(vector<T, N> const & v)
|
||||
{
|
||||
return pointwise([](T const & a){ return std::exp(a); }, v);
|
||||
return pointwise([](T const & a){ using std::exp; return exp(a); }, v);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
vector<T, N> pointwise_pow(vector<T, N> const & v0, vector<T, N> const & v1)
|
||||
{
|
||||
return pointwise([](T const & a, T const & b){ return std::pow(a, b); }, v0, v1);
|
||||
return pointwise([](T const & a, T const & b){ using std::pow; return pow(a, b); }, v0, v1);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
|
|
@ -393,23 +407,27 @@ namespace psemek::geom
|
|||
template <typename T, std::size_t N>
|
||||
vector<T, N> slerp(vector<T, N> const & v0, vector<T, N> const & v1, T const & t)
|
||||
{
|
||||
using std::pow;
|
||||
using std::abs;
|
||||
using std::sin;
|
||||
|
||||
// threshold is chosen so that for abs(x) < threshold the second term in
|
||||
// sin(x) Taylor series is less than the minimum value representable by T
|
||||
static auto const threshold = std::pow(6 * std::numeric_limits<T>::min(), T{1}/T{3});
|
||||
static auto const threshold = pow(T{6} * std::numeric_limits<T>::min(), T{1}/T{3});
|
||||
|
||||
auto const omega = angle(normalized(v0), normalized(v1));
|
||||
|
||||
// NB: the case of omega ~ pi is ambiguous and isn't handled in any special way
|
||||
|
||||
if (std::abs(omega) < threshold)
|
||||
if (abs(omega) < threshold)
|
||||
{
|
||||
// prevent division by zero
|
||||
return normalized(lerp(v0, v1, t));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const s = std::sin(omega);
|
||||
return v0 * std::sin((1 - t) * omega) / s + v1 * std::sin(t * omega) / s;
|
||||
auto const s = sin(omega);
|
||||
return v0 * sin((1 - t) * omega) / s + v1 * sin(t * omega) / s;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -439,8 +457,9 @@ namespace psemek::geom
|
|||
template <typename T, std::size_t N>
|
||||
bool isfinite(vector<T, N> const & v)
|
||||
{
|
||||
using std::isfinite;
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
if (!std::isfinite(v[i]))
|
||||
if (!isfinite(v[i]))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue