Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute distance sphere-box #495

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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