Skip to content

Commit

Permalink
Add convert() for geometries (#1147)
Browse files Browse the repository at this point in the history
* Add convert() for geometries

* Update the code to use convert()

* Be more explicit in the return type of convert
  • Loading branch information
aprokop authored Sep 25, 2024
1 parent 745f3ea commit 39d5f67
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/ArborX_DBSCAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ struct WithinRadiusGetter
{
static_assert(GeometryTraits::is_point_v<Point>);

constexpr int dim = GeometryTraits::dimension_v<Point>;
auto const &hyper_point =
reinterpret_cast<::ArborX::Point<dim> const &>(pair.value);
constexpr int DIM = GeometryTraits::dimension_v<Point>;
using Coordinate = GeometryTraits::coordinate_type_t<Point>;
using ArborX::intersects;
return intersects(Sphere{hyper_point, _r});
return intersects(
Sphere{convert<::ArborX::Point<DIM, Coordinate>>(pair.value), _r});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct approx_expand_by_radius<PointTag, Point>
{
constexpr int DIM = GeometryTraits::dimension_v<Point>;
using Coordinate = GeometryTraits::coordinate_type_t<Point>;
return Sphere{Kokkos::bit_cast<::ArborX::Point<DIM, Coordinate>>(point), r};
return Sphere{Details::convert<::ArborX::Point<DIM, Coordinate>>(point), r};
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/details/ArborX_NeighborList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ struct NeighborListPredicateGetter

constexpr int dim = GeometryTraits::dimension_v<Point>;
using Coordinate = typename GeometryTraits::coordinate_type_t<Point>;

auto const &hyper_point =
reinterpret_cast<::ArborX::Point<dim, Coordinate> const &>(pair.value);
return intersects(Sphere{hyper_point, _radius});
return intersects(
Sphere{Details::convert<::ArborX::Point<dim, Coordinate>>(pair.value),
_radius});
}
};

Expand Down
7 changes: 2 additions & 5 deletions src/details/ArborX_PredicateHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,8 @@ struct AccessTraits<Experimental::PrimitivesWithRadius<Primitives>,
using Point = std::decay_t<decltype(point)>;
constexpr int dim = GeometryTraits::dimension_v<Point>;
using Coordinate = typename GeometryTraits::coordinate_type<Point>::type;
// FIXME reinterpret_cast is dangerous here if access traits return user
// point structure (e.g., struct MyPoint { float y; float x; })
auto const &hyper_point =
reinterpret_cast<::ArborX::Point<dim, Coordinate> const &>(point);
return intersects(Sphere(hyper_point, x._r));
return intersects(Sphere(
Details::convert<::ArborX::Point<dim, Coordinate>>(point), x._r));
}
};

Expand Down
38 changes: 38 additions & 0 deletions src/geometry/ArborX_DetailsAlgorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ struct expand;
template <typename Tag1, typename Tag2, typename Geometry1, typename Geometry2>
struct intersects;

template <typename TagFrom, typename TagTo, typename GeometryFrom,
typename GeometryTo>
struct convert;

template <typename Tag, typename Geometry>
struct centroid;

Expand Down Expand Up @@ -157,6 +161,16 @@ struct GeometryReducer
bool references_scalar() const { return _references_scalar; }
};

template <typename GeometryTo, typename GeometryFrom>
KOKKOS_INLINE_FUNCTION GeometryTo convert(GeometryFrom const &geometry)
{
static_assert(GeometryTraits::dimension_v<GeometryFrom> ==
GeometryTraits::dimension_v<GeometryTo>);
return Dispatch::convert<typename GeometryTraits::tag_t<GeometryFrom>,
typename GeometryTraits::tag_t<GeometryTo>,
GeometryFrom, GeometryTo>::apply(geometry);
}

namespace Dispatch
{

Expand Down Expand Up @@ -763,6 +777,30 @@ struct intersects<TriangleTag, BoxTag, Triangle, Box>
}
};

template <typename PointFrom, typename PointTo>
struct convert<PointTag, PointTag, PointFrom, PointTo>
{
KOKKOS_FUNCTION static constexpr auto apply(PointFrom const &point)
{
PointTo converted;
constexpr int DIM = GeometryTraits::dimension_v<PointFrom>;
for (int d = 0; d < DIM; ++d)
converted[d] = point[d];
return converted;
}
};

template <int DIM, typename Coordinate>
struct convert<PointTag, PointTag, Point<DIM, Coordinate>,
Point<DIM, Coordinate>>
{
KOKKOS_FUNCTION static constexpr auto
apply(Point<DIM, Coordinate> const &point)
{
return point;
}
};

template <typename Point>
struct centroid<PointTag, Point>
{
Expand Down
12 changes: 12 additions & 0 deletions test/tstDetailsAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ BOOST_AUTO_TEST_CASE(expand)
BOOST_TEST(equals(box, Box{{-5, -5, -3}, {5, 8, 7}}));
}

BOOST_AUTO_TEST_CASE(convert)
{
using ArborX::Point;
using ArborX::Details::convert;
using ArborX::Details::equals;
BOOST_TEST(equals(convert<Point<2, double>>(Point{3.f, 2.f}), Point{3., 2.}));
BOOST_TEST(
equals(convert<Point<2, float>>(Point{3.f, 2.f}), Point{3.f, 2.f}));
BOOST_TEST(
!equals(convert<Point<2, float>>(Point{3.f, 2.f}), Point{2.f, 2.f}));
}

BOOST_AUTO_TEST_CASE(centroid)
{
using ArborX::Details::returnCentroid;
Expand Down

0 comments on commit 39d5f67

Please sign in to comment.