geom/plane: add distance2 for plane/point

This commit is contained in:
Danny Robson 2018-05-07 11:48:32 +10:00
parent 92ba5469c4
commit 1324ae2b4a

View File

@ -75,16 +75,31 @@ namespace util::geom {
/// calculates the distance from a plane to a point.
///
/// positive distances are in front of the plane, negative is behind;
///
/// NOTE: `distance2` is _not_ the squared distance. It is the
/// unnormalised distance. It needs to be divided by the norm of the
/// plane normal.
template <size_t S, typename T>
T
distance2 (plane<S,T> p, point<S,T> q)
{
return dot (p.coefficients, q.template redim<S+1> (1));
}
template <size_t S, typename T>
T
distance2 (point<S,T> p, plane<S,T> q)
{
return -distance2 (q, p);
}
template <size_t S, typename T>
T
distance (plane<S,T> p, point<S,T> q)
{
auto d = dot (
p.coefficients,
q.template redim<S+1> (1)
);
return d / norm (normal (p));
return distance2 (p, q) / norm (normal (p));
}