Skip to content
Snippets Groups Projects
Commit 188bfcbd authored by Philip Trettner's avatar Philip Trettner
Browse files

Merge branch 'kschuster' into 'develop'

squared frobenius norm and angle_between fix

See merge request !33
parents 51d2b33d 43d0e724
No related branches found
No related tags found
1 merge request!33squared frobenius norm and angle_between fix
......@@ -24,14 +24,19 @@ TG_NODISCARD constexpr angle_t<fractional_result<ScalarT>> angle_between(vec<D,
template <int D, class ScalarT>
TG_NODISCARD constexpr angle_t<fractional_result<ScalarT>> angle_between(dir<D, ScalarT> const& a, dir<D, ScalarT> const& b)
{
return acos(saturate(dot(a, b)));
constexpr auto lower = decltype (dot(normal(a), normal(b)))(-1);
constexpr auto upper = decltype (dot(normal(a), normal(b)))(1);
return acos(clamp(dot(a, b), lower, upper));
}
// returns the angle between any two objects with unambiguous normals. The result is in 0..pi (0°..180°)
template <class A, class B>
TG_NODISCARD constexpr auto angle_between(A const& a, B const& b) -> decltype(acos(dot(normal(a), normal(b))))
{
return acos(saturate(dot(normal(a), normal(b))));
// TODO(ks): call to angle_between(dir, dir)?
constexpr auto lower = decltype (dot(normal(a), normal(b)))(-1);
constexpr auto upper = decltype (dot(normal(a), normal(b)))(1);
return acos(clamp(dot(normal(a), normal(b))), lower, upper);
}
// Returns the angle of a rotation of a towards b about the orthogonal_axis
......
......@@ -33,14 +33,21 @@ TG_NODISCARD constexpr T norm(vec<4, T> const& v, T p)
}
template <int C, int R, class T>
TG_NODISCARD constexpr T frobenius_norm(mat<C, R, T> const& v)
TG_NODISCARD constexpr T frobenius_norm_sqr(mat<C, R, T> const& v)
{
static_assert(is_floating_point<T>, "frobenius_norm only works on f32/f64");
static_assert(is_floating_point<T>, "frobenius_norm_sqr only works on f32/f64");
auto s = T(0);
for (auto x = 0; x < C; ++x)
for (auto y = 0; y < R; ++y)
s += tg::pow2(v[x][y]);
return tg::sqrt(s);
return s;
}
template <int C, int R, class T>
TG_NODISCARD constexpr T frobenius_norm(mat<C, R, T> const& v)
{
static_assert(is_floating_point<T>, "frobenius_norm only works on f32/f64");
return tg::sqrt(frobenius_norm_sqr(v));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment