Skip to content

Commit c62ab9f

Browse files
authored
Merge pull request #1329 from aprokop/symmetrize_intersects
Symmetrize calls to intersects() and distance()
2 parents 7e08857 + 8045073 commit c62ab9f

File tree

5 files changed

+390
-439
lines changed

5 files changed

+390
-439
lines changed

src/geometry/algorithms/ArborX_Distance.hpp

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <ArborX_GeometryTraits.hpp>
1616
#include <ArborX_Triangle.hpp>
1717
#include <algorithms/ArborX_ClosestPoint.hpp>
18+
#include <algorithms/ArborX_ReverseDispatch.hpp>
1819
#include <kokkos_ext/ArborX_KokkosExtArithmeticTraits.hpp>
1920
#include <misc/ArborX_Vector.hpp>
2021

@@ -37,10 +38,10 @@ KOKKOS_INLINE_FUNCTION auto distance(Geometry1 const &geometry1,
3738
{
3839
static_assert(GeometryTraits::dimension_v<Geometry1> ==
3940
GeometryTraits::dimension_v<Geometry2>);
40-
return Details::Dispatch::distance<GeometryTraits::tag_t<Geometry1>,
41-
GeometryTraits::tag_t<Geometry2>,
42-
Geometry1, Geometry2>::apply(geometry1,
43-
geometry2);
41+
return Details::Dispatch::DoApply<
42+
Details::Dispatch::distance, GeometryTraits::tag_t<Geometry1>,
43+
GeometryTraits::tag_t<Geometry2>, Geometry1, Geometry2>::apply(geometry1,
44+
geometry2);
4445
}
4546

4647
namespace Details::Dispatch
@@ -78,15 +79,6 @@ struct distance<PointTag, BoxTag, Point, Box>
7879
}
7980
};
8081

81-
template <typename Box, typename Point>
82-
struct distance<BoxTag, PointTag, Box, Point>
83-
{
84-
KOKKOS_FUNCTION static auto apply(Box const &box, Point const &point)
85-
{
86-
return ::ArborX::distance(point, box);
87-
}
88-
};
89-
9082
// distance point-sphere
9183
template <typename Point, typename Sphere>
9284
struct distance<PointTag, SphereTag, Point, Sphere>
@@ -100,16 +92,6 @@ struct distance<PointTag, SphereTag, Point, Sphere>
10092
}
10193
};
10294

103-
// distance sphere-point
104-
template <typename Sphere, typename Point>
105-
struct distance<SphereTag, PointTag, Sphere, Point>
106-
{
107-
KOKKOS_FUNCTION static auto apply(Sphere const &sphere, Point const &point)
108-
{
109-
return ::ArborX::distance(point, sphere);
110-
}
111-
};
112-
11395
// distance sphere-sphere
11496
template <typename Sphere1, typename Sphere2>
11597
struct distance<SphereTag, SphereTag, Sphere1, Sphere2>
@@ -176,19 +158,6 @@ struct distance<PointTag, TetrahedronTag, Point, Tetrahedron>
176158
}
177159
};
178160

179-
// distance tetrahedron-point
180-
template <typename Tetrahedron, typename Point>
181-
struct distance<TetrahedronTag, PointTag, Tetrahedron, Point>
182-
{
183-
static constexpr int DIM = dimension_v<Point>;
184-
using Coordinate = coordinate_type_t<Tetrahedron>;
185-
186-
KOKKOS_FUNCTION static auto apply(Tetrahedron const &tet, Point const &p)
187-
{
188-
return ::ArborX::distance(p, tet);
189-
}
190-
};
191-
192161
// distance box-box
193162
template <typename Box1, typename Box2>
194163
struct distance<BoxTag, BoxTag, Box1, Box2>
@@ -239,16 +208,6 @@ struct distance<SphereTag, BoxTag, Sphere, Box>
239208
}
240209
};
241210

242-
// distance sphere-box
243-
template <typename Box, typename Sphere>
244-
struct distance<BoxTag, SphereTag, Box, Sphere>
245-
{
246-
KOKKOS_FUNCTION static auto apply(Box const &box, Sphere const &sphere)
247-
{
248-
return ::ArborX::distance(sphere, box);
249-
}
250-
};
251-
252211
template <typename Point, typename Segment>
253212
struct distance<PointTag, SegmentTag, Point, Segment>
254213
{

src/geometry/algorithms/ArborX_Intersects.hpp

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <ArborX_GeometryTraits.hpp>
1717
#include <ArborX_Ray.hpp>
1818
#include <ArborX_Segment.hpp>
19+
#include <algorithms/ArborX_ReverseDispatch.hpp>
1920
#include <misc/ArborX_Vector.hpp>
2021

2122
#include <Kokkos_Array.hpp>
@@ -36,10 +37,10 @@ KOKKOS_INLINE_FUNCTION constexpr bool intersects(Geometry1 const &geometry1,
3637
{
3738
static_assert(GeometryTraits::dimension_v<Geometry1> ==
3839
GeometryTraits::dimension_v<Geometry2>);
39-
return Details::Dispatch::intersects<GeometryTraits::tag_t<Geometry1>,
40-
GeometryTraits::tag_t<Geometry2>,
41-
Geometry1, Geometry2>::apply(geometry1,
42-
geometry2);
40+
return Details::Dispatch::DoApply<
41+
Details::Dispatch::intersects, GeometryTraits::tag_t<Geometry1>,
42+
GeometryTraits::tag_t<Geometry2>, Geometry1, Geometry2>::apply(geometry1,
43+
geometry2);
4344
}
4445

4546
namespace Details::Dispatch
@@ -77,15 +78,6 @@ struct intersects<PointTag, BoxTag, Point, Box>
7778
return true;
7879
}
7980
};
80-
template <typename Box, typename Point>
81-
struct intersects<BoxTag, PointTag, Box, Point>
82-
{
83-
KOKKOS_FUNCTION static constexpr bool apply(Box const &box,
84-
Point const &point)
85-
{
86-
return ::ArborX::intersects(point, box);
87-
}
88-
};
8981

9082
// check if a sphere intersects with an axis-aligned bounding box
9183
template <typename Sphere, typename Box>
@@ -121,16 +113,6 @@ struct intersects<SphereTag, PointTag, Sphere, Point>
121113
}
122114
};
123115

124-
template <typename Point, typename Sphere>
125-
struct intersects<PointTag, SphereTag, Point, Sphere>
126-
{
127-
KOKKOS_FUNCTION static constexpr bool apply(Point const &point,
128-
Sphere const &sphere)
129-
{
130-
return ::ArborX::intersects(sphere, point);
131-
}
132-
};
133-
134116
// check if a sphere intersects with a triangle
135117
template <typename Sphere, typename Triangle>
136118
struct intersects<SphereTag, TriangleTag, Sphere, Triangle>
@@ -142,16 +124,6 @@ struct intersects<SphereTag, TriangleTag, Sphere, Triangle>
142124
}
143125
};
144126

145-
template <typename Triangle, typename Sphere>
146-
struct intersects<TriangleTag, SphereTag, Triangle, Sphere>
147-
{
148-
KOKKOS_FUNCTION static constexpr bool apply(Triangle const &triangle,
149-
Sphere const &sphere)
150-
{
151-
return ::ArborX::intersects(sphere, triangle);
152-
}
153-
};
154-
155127
template <typename Point, typename Triangle>
156128
struct intersects<PointTag, TriangleTag, Point, Triangle>
157129
{
@@ -353,14 +325,6 @@ struct intersects<KDOPTag, BoxTag, KDOP, Box>
353325
}
354326
};
355327

356-
template <typename Box, typename KDOP>
357-
struct intersects<BoxTag, KDOPTag, Box, KDOP>
358-
{
359-
KOKKOS_FUNCTION static constexpr bool apply(Box const &box, KDOP const &kdop)
360-
{
361-
return ::ArborX::intersects(kdop, box);
362-
}
363-
};
364328
template <typename Triangle, typename Box>
365329
struct intersects<TriangleTag, BoxTag, Triangle, Box>
366330
{
@@ -393,16 +357,6 @@ struct intersects<PointTag, KDOPTag, Point, KDOP>
393357
}
394358
};
395359

396-
template <typename KDOP, typename Point>
397-
struct intersects<KDOPTag, PointTag, KDOP, Point>
398-
{
399-
KOKKOS_FUNCTION static constexpr bool apply(KDOP const &kdop,
400-
Point const &point)
401-
{
402-
return ::ArborX::intersects(point, kdop);
403-
}
404-
};
405-
406360
template <typename KDOP1, typename KDOP2>
407361
struct intersects<KDOPTag, KDOPTag, KDOP1, KDOP2>
408362
{
@@ -540,16 +494,6 @@ struct intersects<SegmentTag, BoxTag, Segment, Box>
540494
}
541495
};
542496

543-
template <typename Box, typename Segment>
544-
struct intersects<BoxTag, SegmentTag, Box, Segment>
545-
{
546-
KOKKOS_FUNCTION static constexpr bool apply(Box const &box,
547-
Segment const &segment)
548-
{
549-
return ::ArborX::intersects(segment, box);
550-
}
551-
};
552-
553497
template <typename Segment, typename Triangle>
554498
struct intersects<SegmentTag, TriangleTag, Segment, Triangle>
555499
{
@@ -570,17 +514,6 @@ struct intersects<SegmentTag, TriangleTag, Segment, Triangle>
570514
}
571515
};
572516

573-
template <typename Triangle, typename Segment>
574-
struct intersects<TriangleTag, SegmentTag, Triangle, Segment>
575-
{
576-
KOKKOS_FUNCTION static constexpr bool apply(Triangle const &triangle,
577-
Segment const &segment)
578-
579-
{
580-
return ::ArborX::intersects(segment, triangle);
581-
}
582-
};
583-
584517
namespace
585518
{
586519
// Computes x^t R y
@@ -608,16 +541,6 @@ struct intersects<EllipsoidTag, PointTag, Ellipsoid, Point>
608541
}
609542
};
610543

611-
template <typename Point, typename Ellipsoid>
612-
struct intersects<PointTag, EllipsoidTag, Point, Ellipsoid>
613-
{
614-
KOKKOS_FUNCTION static constexpr bool apply(Point const &point,
615-
Ellipsoid const &ellipsoid)
616-
{
617-
return ::ArborX::intersects(ellipsoid, point);
618-
}
619-
};
620-
621544
template <typename Ellipsoid, typename Segment>
622545
struct intersects<EllipsoidTag, SegmentTag, Ellipsoid, Segment>
623546
{
@@ -658,16 +581,6 @@ struct intersects<EllipsoidTag, SegmentTag, Ellipsoid, Segment>
658581
}
659582
};
660583

661-
template <typename Segment, typename Ellipsoid>
662-
struct intersects<SegmentTag, EllipsoidTag, Segment, Ellipsoid>
663-
{
664-
KOKKOS_FUNCTION static constexpr bool apply(Segment const &segment,
665-
Ellipsoid const &ellipsoid)
666-
{
667-
return ::ArborX::intersects(ellipsoid, segment);
668-
}
669-
};
670-
671584
template <typename Ellipsoid, typename Box>
672585
struct intersects<EllipsoidTag, BoxTag, Ellipsoid, Box>
673586
{
@@ -764,16 +677,6 @@ struct intersects<EllipsoidTag, BoxTag, Ellipsoid, Box>
764677
}
765678
};
766679

767-
template <typename Box, typename Ellipsoid>
768-
struct intersects<BoxTag, EllipsoidTag, Box, Ellipsoid>
769-
{
770-
KOKKOS_FUNCTION static constexpr bool apply(Box const &box,
771-
Ellipsoid const &ellipsoid)
772-
{
773-
return ::ArborX::intersects(ellipsoid, box);
774-
}
775-
};
776-
777680
} // namespace Details::Dispatch
778681

779682
} // namespace ArborX
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/****************************************************************************
2+
* Copyright (c) 2025, ArborX authors *
3+
* All rights reserved. *
4+
* *
5+
* This file is part of the ArborX library. ArborX is *
6+
* distributed under a BSD 3-clause license. For the licensing terms see *
7+
* the LICENSE file in the top-level directory. *
8+
* *
9+
* SPDX-License-Identifier: BSD-3-Clause *
10+
****************************************************************************/
11+
12+
#ifndef ARBORX_DETAILS_GEOMETRY_REVERSE_DISPATCH_HPP
13+
#define ARBORX_DETAILS_GEOMETRY_REVERSE_DISPATCH_HPP
14+
15+
#include <Kokkos_Macros.hpp>
16+
17+
#include <concepts>
18+
19+
namespace ArborX::Details::Dispatch
20+
{
21+
22+
template <typename Algorithm, typename Geometry1, typename Geometry2>
23+
concept CanApply =
24+
requires(Geometry1 const &geometry1, Geometry2 const &geometry2) {
25+
{
26+
Algorithm::apply(geometry1, geometry2)
27+
};
28+
};
29+
30+
template <template <typename, typename, typename, typename> typename Algorithm,
31+
typename Tag1, typename Tag2, typename Geometry1, typename Geometry2>
32+
struct DoApply
33+
{
34+
using ForwardAlgorithm = Algorithm<Tag1, Tag2, Geometry1, Geometry2>;
35+
using ReverseAlgorithm = Algorithm<Tag2, Tag1, Geometry2, Geometry1>;
36+
static KOKKOS_FUNCTION constexpr auto apply(Geometry1 const &geometry1,
37+
Geometry2 const &geometry2)
38+
{
39+
if constexpr (!std::same_as<Geometry1, Geometry2> &&
40+
!CanApply<ForwardAlgorithm, Geometry1, Geometry2> &&
41+
CanApply<ReverseAlgorithm, Geometry2, Geometry1>)
42+
return ReverseAlgorithm::apply(geometry2, geometry1);
43+
else
44+
return ForwardAlgorithm::apply(geometry1, geometry2);
45+
}
46+
};
47+
48+
} // namespace ArborX::Details::Dispatch
49+
50+
#endif

0 commit comments

Comments
 (0)