Skip to content

Commit

Permalink
Add CTAD for Triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
aprokop committed Oct 1, 2024
1 parent 6ad668e commit 80111c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/geometry/ArborX_Triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ struct Triangle
{}
};

template <int DIM, class Coordinate>
template <typename T, std::size_t N>
#if KOKKOS_VERSION >= 40400
KOKKOS_DEDUCTION_GUIDE
#else
KOKKOS_FUNCTION
#endif
Triangle(Point<DIM, Coordinate>, Point<DIM, Coordinate>,
Point<DIM, Coordinate>) -> Triangle<DIM, Coordinate>;
Triangle(T const (&)[N], T const (&)[N], T const (&)[N]) -> Triangle<N, T>;

} // namespace ArborX

Expand Down
17 changes: 17 additions & 0 deletions test/tstCompileOnlyGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ArborX_GeometryTraits.hpp>
#include <ArborX_Point.hpp>
#include <ArborX_Sphere.hpp>
#include <ArborX_Triangle.hpp>

namespace ArborX::GeometryTraits
{
Expand Down Expand Up @@ -272,4 +273,20 @@ void test_sphere_ctad()
Sphere<3, double>>);
}

void test_triangle_ctad()
{
using ArborX::Point;
using ArborX::Triangle;

static_assert(std::is_same_v<decltype(Triangle{{0, 2}, {3, 1}, {2, 5}}),
Triangle<2, int>>);
static_assert(
std::is_same_v<decltype(Triangle{{0.f, 2.f}, {3.f, 1.f}, {2.f, 5.f}}),
Triangle<2, float>>);
static_assert(
std::is_same_v<decltype(Triangle{Point{3., 4., 2.}, Point{2., 2., 2.},
Point{6., 3., 5.}}),
Triangle<3, double>>);
}

} // namespace ArborX::GeometryTraits
12 changes: 6 additions & 6 deletions test/tstDetailsAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ BOOST_AUTO_TEST_CASE(distance_point_triangle)
| |
*/
using Point2 = ArborX::Point<2>;
constexpr ArborX::Triangle<2> triangle2{Point2{-1, 0}, Point2{1, 0},
Point2{0, 1}};
constexpr ArborX::Triangle triangle2{Point2{-1, 0}, Point2{1, 0},
Point2{0, 1}};

// vertices
BOOST_TEST(distance(Point2{-1, 0}, triangle2) == 0);
Expand All @@ -108,8 +108,8 @@ BOOST_AUTO_TEST_CASE(distance_point_triangle)
BOOST_TEST(distance(Point2{1, 1}, triangle2) == std::sqrt(2.f) / 2);

using Point3 = ArborX::Point<3>;
constexpr ArborX::Triangle<3> triangle3{Point3{1, 0, 0}, Point3{0, 1, 0},
Point3{0, 0, 0}};
constexpr ArborX::Triangle triangle3{Point3{1, 0, 0}, Point3{0, 1, 0},
Point3{0, 0, 0}};

// same plane
BOOST_TEST(distance(Point3{2, 0, 0}, triangle3) == 1);
Expand All @@ -125,8 +125,8 @@ BOOST_AUTO_TEST_CASE(distance_point_triangle)
BOOST_TEST(distance(Point3{0, -1, -1}, triangle3) == std::sqrt(2.f));
BOOST_TEST(distance(Point3{2, -1, -1}, triangle3) == std::sqrt(3.f));

constexpr ArborX::Triangle<3> triangle3_2{Point3{0, 0, 0}, Point3{0, 1, 0},
Point3{0, 0, 1}};
constexpr ArborX::Triangle triangle3_2{Point3{0, 0, 0}, Point3{0, 1, 0},
Point3{0, 0, 1}};
BOOST_TEST(distance(Point3{-1, 0, 1}, triangle3_2) == 1);
BOOST_TEST(distance(Point3{0, -1, -1}, triangle3_2) == std::sqrt(2.f));
BOOST_TEST(distance(Point3{1, -1, -1}, triangle3_2) == std::sqrt(3.f));
Expand Down
18 changes: 9 additions & 9 deletions test/tstRay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ BOOST_AUTO_TEST_CASE(intersects_triangle)
using ArborX::Point;
using ArborX::Triangle;
using ArborX::Experimental::Ray;
constexpr Triangle unit_triangle{Point{0.f, 0.f, 0.f}, Point{1.f, 0.f, 0.f},
Point{0.f, 1.f, 0.f}};
constexpr Triangle unit_triangle{
{0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}};

BOOST_TEST(intersects(Ray{{.1, .2, .3}, {0, 0, -1}}, unit_triangle));
BOOST_TEST(intersects(Ray{{1.1, 1.2, 1}, {-1, -1, -1}}, unit_triangle));
Expand Down Expand Up @@ -457,8 +457,8 @@ BOOST_AUTO_TEST_CASE(intersects_triangle)
// ray in a plane parallel to the triangle
BOOST_TEST(!intersects(Ray{{-0.1, 0, 1}, {1, 0, 0}}, unit_triangle));

constexpr Triangle tilted_triangle{Point{0.f, 0.f, 0.f}, Point{2.f, 0.f, 1.f},
Point{0.f, 2.f, 1.f}};
constexpr Triangle tilted_triangle{
{0.f, 0.f, 0.f}, {2.f, 0.f, 1.f}, {0.f, 2.f, 1.f}};

// ray in the same plane as the triangle
BOOST_TEST(!intersects(Ray{{10, 0, 0}, {1, 1, 1}}, tilted_triangle));
Expand Down Expand Up @@ -501,10 +501,10 @@ BOOST_AUTO_TEST_CASE(ray_triangle_intersection,
constexpr auto inf = KokkosExt::ArithmeticTraits::infinity<float>::value;
#endif

constexpr Triangle unit_triangle{Point{0.f, 0.f, 0.f}, Point{1.f, 0.f, 0.f},
Point{0.f, 1.f, 0.f}};
constexpr Triangle unit_triangle{
{0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}};
constexpr Triangle narrow_triangle{
Point{0.5f, 0.5f, 0.f}, Point{0.24f, 0.74f, 0.f}, Point{0.f, 1.f, 0.f}};
{0.5f, 0.5f, 0.f}, {0.24f, 0.74f, 0.f}, {0.f, 1.f, 0.f}};

auto const sqrtf_3 = std::sqrt(3.f);
auto const sqrtf_2 = std::sqrt(2.f);
Expand Down Expand Up @@ -594,12 +594,12 @@ BOOST_AUTO_TEST_CASE(ray_triangle_intersection,
// between the origin of the ray and the vertices.
// These tests will fail if there is no normalization.
float const size_s = 0.0001;
Triangle small_triangle{Point{-size_s, 0.f, 0.f}, Point{0.f, size_s, 0.f}, Point{0.f, 0.f, size_s}};
Triangle small_triangle{{-size_s, 0.f, 0.f}, {0.f, size_s, 0.f}, {0.f, 0.f, size_s}};
ARBORX_TEST_RAY_TRIANGLE_INTERSECTION((Ray{{0.0, 0.0, 0.0}, {-size_s, size_s, size_s}}), small_triangle, size_s/sqrtf_3, size_s/sqrtf_3);
ARBORX_TEST_RAY_TRIANGLE_INTERSECTION((Ray{{-2.f*size_s, -size_s, 0.0}, {size_s, size_s, 0}}), small_triangle, sqrtf_2*size_s, 2.f*sqrtf_2*size_s);

float const size_l = 10000;
Triangle large_triangle{Point{-size_l, 0.f, 0.f}, Point{0.f, 10000.f, 0.f}, Point{0.f, 0.f, 10000.f}};
Triangle large_triangle{{-size_l, 0.f, 0.f}, {0.f, 10000.f, 0.f}, {0.f, 0.f, 10000.f}};
ARBORX_TEST_RAY_TRIANGLE_INTERSECTION((Ray{{0.0, 0.0, 0.0}, {-size_l, size_l, size_l}}), large_triangle, size_l/sqrtf_3, size_l/sqrtf_3);
ARBORX_TEST_RAY_TRIANGLE_INTERSECTION((Ray{{-2.f*size_l, -size_l, 0.0}, {size_l, size_l, 0}}), large_triangle, sqrtf_2*size_l, 2.f*sqrtf_2*size_l);

Expand Down

0 comments on commit 80111c9

Please sign in to comment.