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

Get rid of hardcoded float in the geometry algorithms #948

Merged
merged 2 commits into from
Sep 11, 2023
Merged
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
36 changes: 21 additions & 15 deletions src/geometry/ArborX_DetailsAlgorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ KOKKOS_INLINE_FUNCTION constexpr bool isValid(Geometry const &geometry)
}

template <typename Geometry1, typename Geometry2>
KOKKOS_INLINE_FUNCTION float distance(Geometry1 const &geometry1,
Geometry2 const &geometry2)
KOKKOS_INLINE_FUNCTION auto distance(Geometry1 const &geometry1,
Geometry2 const &geometry2)
{
static_assert(GeometryTraits::dimension_v<Geometry1> ==
GeometryTraits::dimension_v<Geometry2>);
Expand Down Expand Up @@ -195,24 +195,27 @@ struct isValid<SphereTag, Sphere>
template <typename Point1, typename Point2>
struct distance<PointTag, PointTag, Point1, Point2>
{
KOKKOS_FUNCTION static float apply(Point1 const &a, Point2 const &b)
KOKKOS_FUNCTION static auto apply(Point1 const &a, Point2 const &b)
{
constexpr int DIM = GeometryTraits::dimension_v<Point1>;
float distance_squared = 0.0;
// Points may have different coordinate types. Try using implicit
// conversion to get the best one.
using Coordinate = decltype(b[0] - a[0]);
Coordinate distance_squared = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably if you want to use that coordinate type for the calculation you should cast b[d] and a[d] below.

We don't seem to enforce that the two points types are the same. Just using the coordinate type of the first point does not feel right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess we can use the type of a[d] - b[d] to allow implicit conversions.

for (int d = 0; d < DIM; ++d)
{
float tmp = b[d] - a[d];
auto tmp = b[d] - a[d];
distance_squared += tmp * tmp;
}
return std::sqrt(distance_squared);
return Kokkos::sqrt(distance_squared);
}
};

// distance point-box
template <typename Point, typename Box>
struct distance<PointTag, BoxTag, Point, Box>
{
KOKKOS_FUNCTION static float apply(Point const &point, Box const &box)
KOKKOS_FUNCTION static auto apply(Point const &point, Box const &box)
{
constexpr int DIM = GeometryTraits::dimension_v<Point>;
Point projected_point;
Expand All @@ -233,7 +236,7 @@ struct distance<PointTag, BoxTag, Point, Box>
template <typename Point, typename Sphere>
struct distance<PointTag, SphereTag, Point, Sphere>
{
KOKKOS_FUNCTION static float apply(Point const &point, Sphere const &sphere)
KOKKOS_FUNCTION static auto apply(Point const &point, Sphere const &sphere)
{
using KokkosExt::max;
return max(Details::distance(point, sphere.centroid()) - sphere.radius(),
Expand All @@ -245,10 +248,13 @@ struct distance<PointTag, SphereTag, Point, Sphere>
template <typename Box1, typename Box2>
struct distance<BoxTag, BoxTag, Box1, Box2>
{
KOKKOS_FUNCTION static float apply(Box1 const &box_a, Box2 const &box_b)
KOKKOS_FUNCTION static auto apply(Box1 const &box_a, Box2 const &box_b)
{
constexpr int DIM = GeometryTraits::dimension_v<Box1>;
float distance_squared = 0.;
// Boxes may have different coordinate types. Try using implicit
// conversion to get the best one.
using Coordinate = decltype(box_b.minCorner()[0] - box_a.minCorner()[0]);
Coordinate distance_squared = 0;
for (int d = 0; d < DIM; ++d)
{
auto const a_min = box_a.minCorner()[d];
Expand All @@ -257,32 +263,32 @@ struct distance<BoxTag, BoxTag, Box1, Box2>
auto const b_max = box_b.maxCorner()[d];
if (a_min > b_max)
{
float const delta = a_min - b_max;
auto const delta = a_min - b_max;
distance_squared += delta * delta;
}
else if (b_min > a_max)
{
float const delta = b_min - a_max;
auto const delta = b_min - a_max;
distance_squared += delta * delta;
}
else
{
// The boxes overlap on this axis: distance along this axis is zero.
}
}
return std::sqrt(distance_squared);
return Kokkos::sqrt(distance_squared);
}
};

// distance sphere-box
template <typename Sphere, typename Box>
struct distance<SphereTag, BoxTag, Sphere, Box>
{
KOKKOS_FUNCTION static float apply(Sphere const &sphere, Box const &box)
KOKKOS_FUNCTION static auto apply(Sphere const &sphere, Box const &box)
{
using KokkosExt::max;

float distance_center_box = Details::distance(sphere.centroid(), box);
auto distance_center_box = Details::distance(sphere.centroid(), box);
return max(distance_center_box - sphere.radius(), 0.f);
}
};
Expand Down