geom/aabb: add point-aabb distance2

This commit is contained in:
Danny Robson 2020-06-18 12:45:27 +10:00
parent 3f6964f257
commit deac73dc10
2 changed files with 34 additions and 2 deletions

View File

@ -9,6 +9,7 @@
#include "aabb.hpp"
#include "ops.hpp"
#include "iostream.hpp"
#include "../coord/iostream.hpp"
@ -35,6 +36,21 @@ aabb<3,float>::vertices (void) const noexcept
}
///////////////////////////////////////////////////////////////////////////////
/// Returns the squared minimum distance from the AABB to a point.
template <size_t S, typename T>
T cruft::geom::distance2 (aabb<S,T> a, point<S,T> b)
{
auto const clamped = cruft::max (
a.lo - b,
vector<S,T> (0),
b - a.hi
);
return sum (clamped * clamped);
}
///////////////////////////////////////////////////////////////////////////////
namespace cruft::debug {
template <size_t S, typename T>
@ -60,8 +76,9 @@ cruft::geom::operator<< (std::ostream &os, cruft::geom::aabb<S,T> b)
#define INSTANTIATE_S_T(S,T) \
namespace cruft::geom { template struct aabb<S,T>; } \
template bool cruft::debug::is_valid (const aabb<S,T>&); \
template std::ostream& cruft::geom::operator<< (std::ostream&, aabb<S,T>);
template std::ostream& cruft::geom::operator<< (std::ostream&, aabb<S,T>); \
template T cruft::geom::distance2 (point<S,T>, aabb<S,T>); \
template T cruft::geom::distance2 (aabb<S,T>, point<S,T>);
#define INSTANTIATE(T) \
INSTANTIATE_S_T(2,T) \
INSTANTIATE_S_T(3,T)

View File

@ -192,6 +192,21 @@ namespace cruft::geom {
{
return a.inclusive (b);
}
///////////////////////////////////////////////////////////////////////////
/// Returns the squared minimum distance from the AABB to a point.
template <size_t S, typename T>
T distance2 (aabb<S,T> a, point<S,T> b);
//-------------------------------------------------------------------------
template <size_t S, typename T>
T
distance2 (point<S,T> a, aabb<S,T> b)
{
return distance2 (b, a);
}
}