Skip to content

Commit

Permalink
Add tests for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Aug 16, 2023
1 parent e90cb4c commit 1eeca28
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 86 deletions.
10 changes: 9 additions & 1 deletion test/orange/surf/ConeAligned.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ namespace test
{
//---------------------------------------------------------------------------//

TEST(ConeAlignedTest, sense)
TEST(ConeAlignedTest, construction)
{
ConeX cone{{0, 0, 1}, 0.5};
EXPECT_VEC_SOFT_EQ((Real3{0, 0, 1}), cone.origin());
EXPECT_SOFT_EQ(ipow<2>(0.5), cone.tangent_sq());

auto coney = ConeY::from_tangent_sq({1, 0, 1}, cone.tangent_sq());
EXPECT_VEC_SOFT_EQ((Real3{1, 0, 1}), coney.origin());
EXPECT_SOFT_EQ(ipow<2>(0.5), coney.tangent_sq());
}

TEST(ConeAlignedTest, sense)
{
ConeX cone{{0, 0, 1}, 0.5};
EXPECT_EQ(SignedSense::inside, cone.calc_sense({2, 0, 1}));
EXPECT_EQ(SignedSense::inside, cone.calc_sense({2, 0.99, 1}));
EXPECT_EQ(SignedSense::outside, cone.calc_sense({2, 1.01, 1}));
Expand Down
29 changes: 19 additions & 10 deletions test/orange/surf/CylAligned.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ namespace celeritas
namespace test
{
//---------------------------------------------------------------------------//
TEST(CylTest, construction)
{
CylX cyl{{1, 2, 3}, 4};
EXPECT_SOFT_EQ(2, cyl.origin_u());
EXPECT_SOFT_EQ(3, cyl.origin_v());
EXPECT_SOFT_EQ(ipow<2>(4), cyl.radius_sq());
EXPECT_VEC_EQ((Real3{0, 2, 3}), cyl.calc_origin());

auto cyly = CylY::from_radius_sq({1, 2, 3}, cyl.radius_sq());
EXPECT_SOFT_EQ(1, cyly.origin_u());
EXPECT_SOFT_EQ(3, cyly.origin_v());
EXPECT_SOFT_EQ(cyl.radius_sq(), cyly.radius_sq());

CylZ const ccyl{CCylZ{2.5}};
EXPECT_SOFT_EQ(ipow<2>(2.5), ccyl.radius_sq());
EXPECT_SOFT_EQ(0, ccyl.origin_u());
EXPECT_SOFT_EQ(0, ccyl.origin_v());
}

TEST(CylXTest, sense)
{
CylX cyl{{0, 0, 0}, 4.0};
Expand Down Expand Up @@ -404,16 +423,6 @@ TEST(CylZTest, degenerate_boundary)
}
}

//---------------------------------------------------------------------------//

TEST(CylZTest, promotion)
{
CylZ const cyl{CCylZ{2.5}};
EXPECT_SOFT_EQ(ipow<2>(2.5), cyl.radius_sq());
EXPECT_SOFT_EQ(0, cyl.origin_u());
EXPECT_SOFT_EQ(0, cyl.origin_v());
}

//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas
4 changes: 4 additions & 0 deletions test/orange/surf/CylCentered.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ TEST(TestCCylX, construction)
EXPECT_EQ(2, CCylX::Intersections{}.size());

CCylX c(4.0);
EXPECT_SOFT_EQ(ipow<2>(4), c.radius_sq());

const real_type expected_data[] = {ipow<2>(4)};

EXPECT_VEC_SOFT_EQ(expected_data, c.data());

auto cy = CCylY::from_radius_sq(c.radius_sq());
EXPECT_SOFT_EQ(c.radius_sq(), cy.radius_sq());
}

TEST(TestCCylX, sense)
Expand Down
43 changes: 22 additions & 21 deletions test/orange/surf/GeneralQuadric.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ namespace test

using Intersections = GeneralQuadric::Intersections;

//---------------------------------------------------------------------------//
TEST(GeneralQuadricTest, construction)
{
GeneralQuadric gq{SimpleQuadric{{ipow<2>(2.5) * ipow<2>(0.3),
ipow<2>(1.0) * ipow<2>(0.3),
ipow<2>(1.0) * ipow<2>(2.5)},
{0, 0, 0},
-1 * ipow<2>(2.5) * ipow<2>(0.3)}};

auto distances
= gq.calc_intersections({-2.5, 0, 0}, {1, 0, 0}, SurfaceState::off);
EXPECT_SOFT_EQ(1.5, distances[0]);
EXPECT_SOFT_EQ(1.5 + 2.0, distances[1]);
distances
= gq.calc_intersections({0, 2.5, 0}, {0, -1, 0}, SurfaceState::on);
EXPECT_SOFT_EQ(5.0, distances[0]);
EXPECT_SOFT_EQ(no_intersection(), distances[1]);
distances = gq.calc_intersections({0, 0, 0}, {0, 0, 1}, SurfaceState::off);
EXPECT_SOFT_EQ(0.3, distances[1]);
EXPECT_SOFT_EQ(no_intersection(), distances[0]);
}

//---------------------------------------------------------------------------//
/*!
* This shape is an ellipsoid:
Expand Down Expand Up @@ -82,27 +104,6 @@ TEST(GeneralQuadricTest, all)
EXPECT_SOFT_EQ(7.0, distances[1]);
}

TEST(GeneralQuadricTest, promotion)
{
GeneralQuadric gq{SimpleQuadric{{ipow<2>(2.5) * ipow<2>(0.3),
ipow<2>(1.0) * ipow<2>(0.3),
ipow<2>(1.0) * ipow<2>(2.5)},
{0, 0, 0},
-1 * ipow<2>(2.5) * ipow<2>(0.3)}};

auto distances
= gq.calc_intersections({-2.5, 0, 0}, {1, 0, 0}, SurfaceState::off);
EXPECT_SOFT_EQ(1.5, distances[0]);
EXPECT_SOFT_EQ(1.5 + 2.0, distances[1]);
distances
= gq.calc_intersections({0, 2.5, 0}, {0, -1, 0}, SurfaceState::on);
EXPECT_SOFT_EQ(5.0, distances[0]);
EXPECT_SOFT_EQ(no_intersection(), distances[1]);
distances = gq.calc_intersections({0, 0, 0}, {0, 0, 1}, SurfaceState::off);
EXPECT_SOFT_EQ(0.3, distances[1]);
EXPECT_SOFT_EQ(no_intersection(), distances[0]);
}

//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas
33 changes: 15 additions & 18 deletions test/orange/surf/Plane.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ class PlaneTest : public Test
}
};

TEST_F(PlaneTest, construction)
{
// Make a rotated plane in the xy axis
Plane p{{1 / sqrt_two, 1 / sqrt_two, 0.0}, {2 / sqrt_two, 2 / sqrt_two, 2}};
EXPECT_VEC_SOFT_EQ((Real3{1 / sqrt_two, 1 / sqrt_two, 0}), p.normal());
EXPECT_SOFT_EQ(2, p.displacement());

Plane px{PlaneX{1.25}};
EXPECT_VEC_SOFT_EQ((Real3{1, 0, 0}), px.normal());
EXPECT_SOFT_EQ(1.25, px.displacement());

Plane py{PlaneY{2.25}};
EXPECT_VEC_SOFT_EQ((Real3{0, 1, 0}), py.normal());
}

TEST_F(PlaneTest, tracking)
{
// Make a rotated plane in the xy axis
Expand Down Expand Up @@ -90,24 +105,6 @@ TEST_F(PlaneTest, tracking)
calc_intersection(p, x, dir, SurfaceState::on));
}

TEST_F(PlaneTest, promotion)
{
Plane px{PlaneX{1.25}};
EXPECT_VEC_SOFT_EQ((Real3{1, 0, 0}), px.normal());
EXPECT_SOFT_EQ(1.25, px.displacement());

Plane py{PlaneY{2.25}};
EXPECT_VEC_SOFT_EQ((Real3{0, 1, 0}), py.normal());
}

TEST_F(PlaneTest, construction)
{
// Make a rotated plane in the xy axis
Plane p{{1 / sqrt_two, 1 / sqrt_two, 0.0}, {2 / sqrt_two, 2 / sqrt_two, 2}};
EXPECT_VEC_SOFT_EQ((Real3{1 / sqrt_two, 1 / sqrt_two, 0}), p.normal());
EXPECT_SOFT_EQ(2, p.displacement());
}

//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas
67 changes: 42 additions & 25 deletions test/orange/surf/SimpleQuadric.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#include "orange/surf/SimpleQuadric.hh"

#include "corecel/math/Algorithms.hh"
#include "orange/Constants.hh"
#include "orange/surf/ConeAligned.hh"
#include "orange/surf/CylAligned.hh"
#include "orange/surf/Plane.hh"
#include "orange/surf/Sphere.hh"

#include "celeritas_test.hh"

Expand All @@ -19,7 +22,45 @@ namespace test
{
//---------------------------------------------------------------------------//

TEST(Ellipsoid, origin)
TEST(SimpleQuadricTest, construction)
{
using constants::sqrt_two;
// Plane
SimpleQuadric p{Plane{{1 / sqrt_two, 1 / sqrt_two, 0.0}, 2 * sqrt_two}};

auto distances
= p.calc_intersections({0, 0, 0}, {1, 0, 0}, SurfaceState::off);
EXPECT_SOFT_EQ(4.0, distances[0]);
EXPECT_SOFT_EQ(no_intersection(), distances[1]);

// Sphere
SimpleQuadric sph{Sphere{{1, 2, 3}, 0.5}};

distances = sph.calc_intersections({1, 2, 2}, {0, 0, 1}, SurfaceState::off);
EXPECT_SOFT_EQ(0.5, distances[0]);
EXPECT_SOFT_EQ(1.5, distances[1]);

// Cone along x axis
SimpleQuadric cx{ConeX{{1.1, 2.2, 3.3}, 2. / 3.}};

distances = cx.calc_intersections(
{1.1 + 3, 2.2 + 2 + 1.0, 3.3}, {0.0, -1.0, 0.0}, SurfaceState::off);
EXPECT_SOFT_EQ(1.0, distances[0]);
EXPECT_SOFT_EQ(5.0, distances[1]);

// Cylinder with radius 2 centered at {2, 3, 0}
SimpleQuadric cz{CylZ{{2, 3, 0}, 2}};

distances
= cz.calc_intersections({-0.5, 3, 0}, {1, 0, 0}, SurfaceState::off);
EXPECT_SOFT_EQ(0.5, distances[0]);
EXPECT_SOFT_EQ(0.5 + 4.0, distances[1]);

EXPECT_VEC_SOFT_EQ((Real3{0, 1, 0}), cz.calc_normal({2, 5, 0}));
EXPECT_VEC_SOFT_EQ((Real3{-1, 0, 0}), cz.calc_normal({0, 3, 0}));
}

TEST(SimpleQuadricTest, ellipsoid)
{
// 1 x 2.5 x .3 radii
const Real3 second{ipow<2>(2.5) * ipow<2>(0.3),
Expand Down Expand Up @@ -52,30 +93,6 @@ TEST(Ellipsoid, origin)
EXPECT_VEC_SOFT_EQ((Real3{-1, 0, 0}), sq.calc_normal({-1, 0, 0}));
}

TEST(Ellipsoid, promotion_cone)
{
SimpleQuadric sq{ConeX{{1.1, 2.2, 3.3}, 2. / 3.}};

auto distances = sq.calc_intersections(
{1.1 + 3, 2.2 + 2 + 1.0, 3.3}, {0.0, -1.0, 0.0}, SurfaceState::off);
EXPECT_SOFT_EQ(1.0, distances[0]);
EXPECT_SOFT_EQ(5.0, distances[1]);
}

TEST(Ellipsoid, promotion_cyl)
{
// Radius: 2 centered at {2, 3, 0}
SimpleQuadric sq{CylZ{{2, 3, 0}, 2}};

auto distances
= sq.calc_intersections({-0.5, 3, 0}, {1, 0, 0}, SurfaceState::off);
EXPECT_SOFT_EQ(0.5, distances[0]);
EXPECT_SOFT_EQ(0.5 + 4.0, distances[1]);

EXPECT_VEC_SOFT_EQ((Real3{0, 1, 0}), sq.calc_normal({2, 5, 0}));
EXPECT_VEC_SOFT_EQ((Real3{-1, 0, 0}), sq.calc_normal({0, 3, 0}));
}

//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas
24 changes: 16 additions & 8 deletions test/orange/surf/Sphere.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ using Intersections = Sphere::Intersections;

constexpr real_type sqrt_third = 1 / constants::sqrt_three;

//---------------------------------------------------------------------------//
TEST(SphereTest, construction)
{
Sphere s{{-1.1, 2.2, -3.3}, 4.4};
EXPECT_VEC_SOFT_EQ((Real3{-1.1, 2.2, -3.3}), s.origin());
EXPECT_SOFT_EQ(ipow<2>(4.4), s.radius_sq());

auto s2 = Sphere::from_radius_sq({1, 2, 3}, s.radius_sq());
EXPECT_VEC_SOFT_EQ((Real3{1, 2, 3}), s2.origin());
EXPECT_SOFT_EQ(s.radius_sq(), s2.radius_sq());

Sphere sc{SphereCentered{2.5}};
EXPECT_VEC_SOFT_EQ((Real3{0, 0, 0}), sc.origin());
EXPECT_SOFT_EQ(ipow<2>(2.5), sc.radius_sq());
}

//---------------------------------------------------------------------------//
TEST(SphereTest, all)
{
Expand Down Expand Up @@ -75,14 +91,6 @@ TEST(SphereTest, all)
EXPECT_SOFT_EQ(1 + 2 * radius, distances[1]);
}

//---------------------------------------------------------------------------//
TEST(SphereTest, promotion)
{
Sphere s{SphereCentered{2.5}};
EXPECT_VEC_SOFT_EQ((Real3{0, 0, 0}), s.origin());
EXPECT_SOFT_EQ(ipow<2>(2.5), s.radius_sq());
}

//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas
13 changes: 10 additions & 3 deletions test/orange/surf/SphereCentered.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ using Intersections = SphereCentered::Intersections;
constexpr real_type sqrt_third = 1 / constants::sqrt_three;

//---------------------------------------------------------------------------//
TEST(SphereCenteredTest, all)
TEST(SphereCenteredTest, construction)
{
EXPECT_EQ(SurfaceType::sc, SphereCentered::surface_type());
EXPECT_EQ(1, SphereCentered::Storage::extent);
EXPECT_EQ(2, SphereCentered::Intersections{}.size());

real_type radius = 4.4;
SphereCentered s{3};
EXPECT_SOFT_EQ(ipow<2>(3), s.radius_sq());

auto s2 = SphereCentered::from_radius_sq(s.radius_sq());
EXPECT_SOFT_EQ(s.radius_sq(), s2.radius_sq());
}

TEST(SphereCenteredTest, maths)
{
real_type radius = 4.4;
SphereCentered s{radius};
EXPECT_SOFT_EQ(radius * radius, s.radius_sq());

EXPECT_EQ(SignedSense::outside, s.calc_sense({2, 3, 5}));
EXPECT_EQ(SignedSense::inside, s.calc_sense({2, 3, 1}));
Expand Down

0 comments on commit 1eeca28

Please sign in to comment.