Skip to content

Commit

Permalink
Merge pull request #495 from Rombur/box_sphere
Browse files Browse the repository at this point in the history
Compute distance sphere-box
  • Loading branch information
dalg24 committed Mar 26, 2021
2 parents a4bcb66 + 46714d4 commit ae19f4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/details/ArborX_DetailsAlgorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ float distance(Box const &box_a, Box const &box_b)
return std::sqrt(distance_squared);
}

// distance box-sphere
KOKKOS_INLINE_FUNCTION
float distance(Sphere const &sphere, Box const &box)
{
using KokkosExt::max;

float distance_center_box = distance(sphere.centroid(), box);
return max(distance_center_box - sphere.radius(), 0.f);
}

// expand an axis-aligned bounding box to include a point
KOKKOS_INLINE_FUNCTION
void expand(Box &box, Point const &point) { box += point; }
Expand Down
20 changes: 20 additions & 0 deletions test/tstDetailsAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ BOOST_AUTO_TEST_CASE(distance_box_box)
BOOST_TEST(distance(Box{}, Box{}) == infinity);
}

BOOST_AUTO_TEST_CASE(distance_sphere_box)
{
using ArborX::Details::distance;
auto infinity = KokkosExt::ArithmeticTraits::infinity<float>::value;

// unit sphere
constexpr Sphere sphere{{{0., 0., 0.}}, 1.};
// distance between a sphere and a box no intersection
BOOST_TEST(distance(sphere, Box{{2.0, 3.0, 4.0}, {2.5, 3.5, 4.5}}) ==
std::sqrt(29.f) - 1.f);
// distance between a sphere and a box with intersection
BOOST_TEST(distance(sphere, Box{{0.5, 0.5, 0.5}, {2.5, 3.5, 4.5}}) == 0.f);
// distance between a sphere included in a box and that box
BOOST_TEST(distance(sphere, Box{{-2., -2., -2.}, {2., 2., 2.}}) == 0.f);
// distance between a sphere and a box included in that sphere
BOOST_TEST(distance(sphere, Box{{0., 0., 0.}, {0.1, 0.2, 0.3}}) == 0.f);
// distance to empty box
BOOST_TEST(distance(sphere, Box{}) == infinity);
}

BOOST_AUTO_TEST_CASE(intersects)
{
using ArborX::Details::intersects;
Expand Down

0 comments on commit ae19f4a

Please sign in to comment.