|
| 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 | +#include "ArborX_EnableArrayComparison.hpp" |
| 13 | +#include <ArborX_Box.hpp> |
| 14 | +#include <ArborX_GeometryTraits.hpp> |
| 15 | +#include <ArborX_Point.hpp> |
| 16 | +#include <ArborX_Tetrahedron.hpp> |
| 17 | +#include <ArborX_Triangle.hpp> |
| 18 | +#include <algorithms/ArborX_BarycentricCoordinates.hpp> |
| 19 | + |
| 20 | +#include "BoostTest_CUDA_clang_workarounds.hpp" |
| 21 | +#include <boost/test/unit_test.hpp> |
| 22 | + |
| 23 | +namespace tt = boost::test_tools; |
| 24 | + |
| 25 | +template <int N> |
| 26 | +using Array = Kokkos::Array<float, N>; |
| 27 | + |
| 28 | +BOOST_AUTO_TEST_CASE(barycentric_triangle) |
| 29 | +{ |
| 30 | + using ArborX::Point; |
| 31 | + using ArborX::Triangle; |
| 32 | + using ArborX::Details::barycentricCoordinates; |
| 33 | + |
| 34 | + Triangle<2> tri{{-1, -1}, {1, -1}, {-1, 1}}; |
| 35 | + // clang-format off |
| 36 | + // vertices |
| 37 | + BOOST_TEST(barycentricCoordinates(tri, Point{-1.f, -1.f}) == (Array<3>{1, 0, 0}), tt::per_element()); |
| 38 | + BOOST_TEST(barycentricCoordinates(tri, Point{1.f, -1.f}) == (Array<3>{0, 1, 0}), tt::per_element()); |
| 39 | + BOOST_TEST(barycentricCoordinates(tri, Point{-1.f, 1.f}) == (Array<3>{0, 0, 1}), tt::per_element()); |
| 40 | + // mid edges |
| 41 | + BOOST_TEST(barycentricCoordinates(tri, Point{0.f, -1.f}) == (Array<3>{0.5f, 0.5f, 0}), tt::per_element()); |
| 42 | + BOOST_TEST(barycentricCoordinates(tri, Point{-1.f, 0.f}) == (Array<3>{0.5f, 0, 0.5f}), tt::per_element()); |
| 43 | + BOOST_TEST(barycentricCoordinates(tri, Point{0.f, 0.f}) == (Array<3>{0, 0.5f, 0.5f}), tt::per_element()); |
| 44 | + // center |
| 45 | + BOOST_TEST(barycentricCoordinates(tri, Point{-1.f/3, -1.f/3}) == (Array<3>{1.f/3, 1.f/3, 1.f/3}), tt::tolerance(1e-7f) << tt::per_element()); |
| 46 | + // off-center |
| 47 | + BOOST_TEST(barycentricCoordinates(tri, Point{-0.4f, 0.2f}) == (Array<3>{0.1f, 0.3f, 0.6f}), tt::tolerance(1e-6f) << tt::per_element()); |
| 48 | + // clang-format on |
| 49 | +} |
| 50 | + |
| 51 | +BOOST_AUTO_TEST_CASE(barycentric_tetrahedron) |
| 52 | +{ |
| 53 | + using ArborX::Point; |
| 54 | + using ArborX::Details::barycentricCoordinates; |
| 55 | + using ArborX::ExperimentalHyperGeometry::Tetrahedron; |
| 56 | + |
| 57 | + Tetrahedron<> tet{{0, 0, 0}, {0, 2, 0}, {2, 0, 0}, {0, 0, 2}}; |
| 58 | + // clang-format off |
| 59 | + // vertices |
| 60 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.f, 0.f, 0.f}) == (Array<4>{1, 0, 0, 0}), tt::per_element()); |
| 61 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.f, 2.f, 0.f}) == (Array<4>{0, 1, 0, 0}), tt::per_element()); |
| 62 | + BOOST_TEST(barycentricCoordinates(tet, Point{2.f, 0.f, 0.f}) == (Array<4>{0, 0, 1, 0}), tt::per_element()); |
| 63 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.f, 0.f, 2.f}) == (Array<4>{0, 0, 0, 1}), tt::per_element()); |
| 64 | + // (some) mid edges |
| 65 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.f, 1.f, 0.f}) == (Array<4>{0.5f, 0.5f, 0, 0}), tt::per_element()); |
| 66 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.f, 1.f, 1.f}) == (Array<4>{0, 0.5f, 0, 0.5f}), tt::per_element()); |
| 67 | + BOOST_TEST(barycentricCoordinates(tet, Point{1.f, 0.f, 1.f}) == (Array<4>{0, 0, 0.5f, 0.5f}), tt::per_element()); |
| 68 | + // (some) mid faces |
| 69 | + BOOST_TEST(barycentricCoordinates(tet, Point{2.f/3, 2.f/3, 0.f}) == (Array<4>{1.f/3, 1.f/3, 1.f/3, 0}), tt::tolerance(1e-6f) << tt::per_element()); |
| 70 | + BOOST_TEST(barycentricCoordinates(tet, Point{2.f/3, 2.f/3, 2.f/3}) == (Array<4>{0, 1.f/3, 1.f/3, 1.f/3}), tt::tolerance(1e-7f) << tt::per_element()); |
| 71 | + // center |
| 72 | + BOOST_TEST(barycentricCoordinates(tet, Point{0.5f, 0.5f, 0.5f}) == (Array<4>{0.25f, 0.25f, 0.25f, 0.25f}), tt::tolerance(1e-7f) << tt::per_element()); |
| 73 | + // clang-format on |
| 74 | +} |
0 commit comments