Skip to content

Commit

Permalink
Merge pull request #1208 from aprokop/segment_box_intersection
Browse files Browse the repository at this point in the history
Add segment-box intersection
  • Loading branch information
aprokop authored Feb 1, 2025
2 parents 166aba4 + e7fb859 commit 9b7baae
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/geometry/algorithms/ArborX_Intersects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,42 @@ struct intersects<SegmentTag, SegmentTag, Segment, Segment>
}
};

template <typename Segment, typename Box>
struct intersects<SegmentTag, BoxTag, Segment, Box>
{
KOKKOS_FUNCTION static constexpr bool apply(Segment const &segment,
Box const &box)
{
static_assert(GeometryTraits::dimension_v<Segment> == 2);

if (Details::intersects(segment.a, box) ||
Details::intersects(segment.b, box))
return true;

using Point = std::decay_t<decltype(box.minCorner())>;

auto const &bottom_left = box.minCorner();
auto const &top_right = box.maxCorner();
auto const top_left = Point{bottom_left[0], top_right[1]};
auto const bottom_right = Point{top_right[0], bottom_left[1]};

return Details::intersects(segment, Segment{bottom_left, top_left}) ||
Details::intersects(segment, Segment{bottom_left, bottom_right}) ||
Details::intersects(segment, Segment{top_right, top_left}) ||
Details::intersects(segment, Segment{top_right, bottom_right});
}
};

template <typename Box, typename Segment>
struct intersects<BoxTag, SegmentTag, Box, Segment>
{
KOKKOS_FUNCTION static constexpr bool apply(Box const &box,
Segment const &segment)
{
return Details::intersects(segment, box);
}
};

} // namespace Dispatch

} // namespace ArborX::Details
Expand Down
14 changes: 14 additions & 0 deletions test/tstDetailsAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ BOOST_AUTO_TEST_CASE(intersects)
BOOST_TEST(!intersects(Segment2{{1.1, 1}, {2, 1}}, seg));
BOOST_TEST(!intersects(Segment2{{1, 0}, {2, 1}}, seg));
BOOST_TEST(!intersects(Segment2{{1, 3}, {3, 1.1}}, seg));

constexpr ArborX::Box<2> box2{{{0.0, 0.0}}, {{1.0, 1.0}}};
BOOST_TEST(intersects(Segment2{{0, 0}, {0, 0}}, box2));
BOOST_TEST(intersects(Segment2{{-1, 1}, {1, -1}}, box2));
BOOST_TEST(intersects(Segment2{{-1, 0}, {2, 0}}, box2));
BOOST_TEST(intersects(Segment2{{-1, 0.5}, {0.5, 0.5}}, box2));
BOOST_TEST(intersects(Segment2{{0.5, 0.5}, {0.5, 2}}, box2));
BOOST_TEST(intersects(Segment2{{-1, 2}, {2, -1}}, box2));
BOOST_TEST(intersects(Segment2{{0.5, 2}, {0.5, -1}}, box2));
BOOST_TEST(intersects(Segment2{{0.4, 0.4}, {0.6, 0.6}}, box2));
BOOST_TEST(!intersects(Segment2{{0, -1}, {1, -1}}, box2));
BOOST_TEST(!intersects(Segment2{{0.5, 1.6}, {2, 0}}, box2));
BOOST_TEST(intersects(box2, Segment2{{-1, 2}, {2, -1}}));
BOOST_TEST(!intersects(Segment2{{0.5, 1.6}, {2, 0}}, box2));
}

BOOST_AUTO_TEST_CASE(equals)
Expand Down

0 comments on commit 9b7baae

Please sign in to comment.