Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Feb 21, 2024
1 parent 85c649e commit 7596e32
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 72 deletions.
22 changes: 18 additions & 4 deletions src/orange/orangeinp/ConvexSurfaceBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ ConvexSurfaceBuilder::ConvexSurfaceBuilder(UnitBuilder* ub, State* state)

//---------------------------------------------------------------------------//
/*!
* Add a plane with a possibly *outside* sense.
* Add a surface with a sense.
*
* The resulting surface *MUST* result in a convex region.
*/
template<class S>
void ConvexSurfaceBuilder::operator()(Sense sense, S const& surf)
Expand All @@ -77,11 +79,17 @@ void ConvexSurfaceBuilder::operator()(Sense sense, S const& surf)
apply_transform(*state_->transform, surf));
}

//---------------------------------------------------------------------------//
// HELPER FUNCTION DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Add a surface with an "inside" sense (true for negative quadric value).
* Add a surface after transforming it to an unknown type.
*
* \param extension Constructed metadata for the surface node
* \param sense Whether the convex region is inside/outside this surface
* \param surf Type-deleted surface
*/
void ConvexSurfaceBuilder::insert_transformed(std::string&& ext,
void ConvexSurfaceBuilder::insert_transformed(std::string&& extension,
Sense sense,
VariantSurface const& surf)
{
Expand All @@ -107,7 +115,7 @@ void ConvexSurfaceBuilder::insert_transformed(std::string&& ext,
CELER_ASSERT(node_id);

// Add metadata for the surface node
ub_->insert_md(node_id, Label{state_->object_name, std::move(ext)});
ub_->insert_md(node_id, Label{state_->object_name, std::move(extension)});

if (sense == Sense::inside)
{
Expand All @@ -121,6 +129,12 @@ void ConvexSurfaceBuilder::insert_transformed(std::string&& ext,
state_->nodes.push_back(node_id);
}

//---------------------------------------------------------------------------//
// FREE FUNCTION DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct a surface using a variant.
*/
void visit(ConvexSurfaceBuilder& csb, Sense sense, VariantSurface const& surf)
{
std::visit([&csb, sense](auto const& s) { csb(sense, s); }, surf);
Expand Down
4 changes: 3 additions & 1 deletion src/orange/orangeinp/ConvexSurfaceBuilder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ struct ConvexSurfaceState;
*
* \todo Should we require that the user implicitly guarantee that the result
* is convex, e.g. prohibit quadrics outside "saddle" points? What about a
* torus, which (unless degenerate) is never convex?
* torus, which (unless degenerate) is never convex? Or should we just require
* that "exiting a surface exits the region"? (Think about application to OR-ed
* combinations for safety calculation.)
*/
class ConvexSurfaceBuilder
{
Expand Down
2 changes: 1 addition & 1 deletion src/orange/orangeinp/detail/NegatedSurfaceClipper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NegatedSurfaceClipper
return this->clip_impl(T, s.position());
}

// All other operations invalidate the "interior" box
//! All other operations invalidate the "interior" box
template<class S>
CELER_FORCEINLINE void operator()(S const&)
{
Expand Down
102 changes: 61 additions & 41 deletions test/orange/orangeinp/ConvexSurfaceBuilder.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ TEST_F(ConvexSurfaceBuilderTest, no_transform)
ConvexSurfaceBuilder build{&unit_builder_, &css};
build(PlaneZ{0.0});
build(SphereCentered{1.0});

// clang-format off
static real_type const expected_local_bz[] = {-0.86602540378444,
-0.86602540378444, -0.86602540378444, 0.86602540378444,
0.86602540378444, 0, -1, -1, -1, 1, 1, 0, 1};
static int const expected_nodes[] = {3, 5};
// clang-format on
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {3, 5};
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
{
Expand All @@ -78,17 +79,18 @@ TEST_F(ConvexSurfaceBuilderTest, no_transform)
ConvexSurfaceBuilder build{&unit_builder_, &css};
build(Sense::outside, PlaneZ{1e-5});
build(Sense::inside, SphereCentered{1.0});

// clang-format off
static real_type const expected_local_bz[] = {
-0.86602540378444, -0.86602540378444, 0,
0.86602540378444, 0.86602540378444, 0.86602540378444,
-1, -1, 0,
1, 1, 1,
1};
static int const expected_nodes[] = {2, 5};
// clang-format on
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {2, 5};
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
{
Expand All @@ -98,15 +100,15 @@ TEST_F(ConvexSurfaceBuilderTest, no_transform)
ConvexSurfaceBuilder build{&unit_builder_, &css};
build(PlaneZ{1e-5});
build(SphereCentered{1.0});

// clang-format off
static real_type const expected_local_bz[] = {-0.86602540378444,
-0.86602540378444, -0.86602540378444, 0.86602540378444,
0.86602540378444, 0, -1, -1, -1, 1, 1, 0, 1};
static int const expected_nodes[] = {3, 5};
// clang-format on
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.global_bzone));

static int const expected_nodes[] = {3, 5};
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
{
Expand All @@ -115,51 +117,56 @@ TEST_F(ConvexSurfaceBuilderTest, no_transform)
css.object_name = "sl";
ConvexSurfaceBuilder build{&unit_builder_, &css};
x_slab(build);

static real_type const expected_local_bz[] = {
-0.5, -inf, -inf, 0.5, inf, inf, -0.5, -inf, -inf, 0.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
static real_type const expected_global_bz[] = {
-0.5, -inf, -inf, 0.5, inf, inf, -0.5, -inf, -inf, 0.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {6, 8};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}

auto const& u = unit_;
// clang-format off
static char const* const expected_surface_strings[]
= {"Plane: z=0", "Sphere: r=1", "Plane: x=-0.5", "Plane: x=0.5"};
EXPECT_VEC_EQ(expected_surface_strings, surface_strings(u));
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(
R"json([["t",["~",0],["S",0],["~",2],["S",1],["~",4],["S",2],["S",3],["~",7]]])json",
tree_string(u));
}
// clang-format off
static char const* const expected_md_strings[] = {"", "",
"dh@c:pz,rh@c:mz,zh@c:pz", "", "dh@c:s,rh@c:s,zh@c:s", "", "sl@c:mx",
"sl@c:px", ""};
static char const expected_tree_string[]
= R"json([["t",["~",0],["S",0],["~",2],["S",1],["~",4],["S",2],["S",3],["~",7]]])json";
// clang-format on

auto const& u = unit_;
EXPECT_VEC_EQ(expected_surface_strings, surface_strings(u));
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(expected_tree_string, tree_string(u));
}
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
}

TEST_F(ConvexSurfaceBuilderTest, translate)
{
// clang-format off
transform_ = Translation{Real3{1, 2, 3}};
{
SCOPED_TRACE("slab");
auto css = this->make_state();
css.object_name = "sl";
ConvexSurfaceBuilder build{&unit_builder_, &css};
x_slab(build);

// clang-format off
static real_type const expected_local_bz[] = {-0.5, -inf, -inf, 0.5,
inf, inf, -0.5, -inf, -inf, 0.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
static real_type const expected_global_bz[] = {0.5, -inf, -inf, 1.5,
inf, inf, 0.5, -inf, -inf, 1.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {2, 4};
// clang-format on

EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
{
Expand All @@ -168,15 +175,19 @@ TEST_F(ConvexSurfaceBuilderTest, translate)
css.object_name = "sph";
ConvexSurfaceBuilder build{&unit_builder_, &css};
build(SphereCentered{1.0});

// clang-format off
static real_type const expected_local_bz[] = {-0.86602540378444,
-0.86602540378444, -0.86602540378444, 0.86602540378444,
0.86602540378444, 0.86602540378444, -1, -1, -1, 1, 1, 1, 1};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
static real_type const expected_global_bz[] = {0.13397459621556,
1.1339745962156, 2.1339745962156, 1.8660254037844, 2.8660254037844,
3.8660254037844, 0, 1, 2, 2, 3, 4, 1};
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {6};
// clang-format on

EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
transform_ = Translation{Real3{2, 0, 0}};
Expand All @@ -186,68 +197,77 @@ TEST_F(ConvexSurfaceBuilderTest, translate)
css.object_name = "ss";
ConvexSurfaceBuilder build{&unit_builder_, &css};
x_slab(build);

// clang-format off
static real_type const expected_local_bz[] = {-0.5, -inf, -inf, 0.5,
inf, inf, -0.5, -inf, -inf, 0.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
static real_type const expected_global_bz[] = {1.5, -inf, -inf, 2.5,
inf, inf, 1.5, -inf, -inf, 2.5, inf, inf, 1};
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {3, 8};
// clang-format on

EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
auto const& u = unit_;

static char const * const expected_surface_strings[] = {"Plane: x=0.5",
"Plane: x=1.5", "Sphere: r=1 at {1,2,3}", "Plane: x=2.5"};
static char const* const expected_md_strings[] = {
"", "", "sl@c:mx", "sl@c:px,ss@c:mx", "", "sph@c:s", "", "ss@c:px", ""};
static char const expected_tree_string[]
= R"json([["t",["~",0],["S",0],["S",1],["~",3],["S",2],["~",5],["S",3],["~",7]]])json";

auto const& u = unit_;
EXPECT_VEC_EQ(expected_surface_strings, surface_strings(u));
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(
R"json([["t",["~",0],["S",0],["S",1],["~",3],["S",2],["~",5],["S",3],["~",7]]])json",
tree_string(u));
EXPECT_JSON_EQ(expected_tree_string, tree_string(u));
}
static char const * const expected_md_strings[] = {"", "", "sl@c:mx",
"sl@c:px,ss@c:mx", "", "sph@c:s", "", "ss@c:px", ""};
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
// clang-format on
}

TEST_F(ConvexSurfaceBuilderTest, transform)
{
transform_
= Transformation{make_rotation(Axis::x, Turn{0.25}), Real3{0, 0, 1}};
// clang-format off
{
SCOPED_TRACE("hemi");
auto css = this->make_state();
css.object_name = "h";
ConvexSurfaceBuilder build{&unit_builder_, &css};
build(PlaneZ{1e-5});
build(SphereCentered{1.0});

// clang-format off
static real_type const expected_local_bz[] = {-0.86602540378444,
-0.86602540378444, -0.86602540378444, 0.86602540378444,
0.86602540378444, 0, -1, -1, -1, 1, 1, 0, 1};
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
static real_type const expected_global_bz[] = {-0.86602540378444, 0,
0.13397459621556, 0.86602540378444, 0.86602540378444,
1.8660254037844, -1, 0, 0, 1, 1, 2, 1};
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
static int const expected_nodes[] = {2, 4};

// clang-format on
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
}
auto const& u = unit_;

static char const * const expected_surface_strings[] = {"Plane: y=0",
"Sphere: r=1 at {0,0,1}"};
static char const expected_tree_string[]
= R"json([["t",["~",0],["S",0],["S",1],["~",3]]])json";
static char const* const expected_md_strings[]
= {"", "", "h@c:pz", "h@c:s", ""};

auto const& u = unit_;
EXPECT_VEC_EQ(expected_surface_strings, surface_strings(u));
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(
R"json([["t",["~",0],["S",0],["S",1],["~",3]]])json",
tree_string(u));
EXPECT_JSON_EQ(expected_tree_string, tree_string(u));
}
static char const * const expected_md_strings[] = {"", "", "h@c:pz",
"h@c:s", ""};
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
// clang-format on
}

//---------------------------------------------------------------------------//
Expand Down

0 comments on commit 7596e32

Please sign in to comment.