Skip to content

Commit

Permalink
Add JSON output for CSG objects (#1133)
Browse files Browse the repository at this point in the history
* Add accessors to convex region
* Add Span IO
* Add accessors to CSG objects
* Add string output to transform type
* Add JSON pimpl helper
* Implement object output
  • Loading branch information
sethrj committed Mar 6, 2024
1 parent 18f4ae6 commit 2596ea8
Show file tree
Hide file tree
Showing 19 changed files with 602 additions and 15 deletions.
31 changes: 31 additions & 0 deletions src/corecel/cont/SpanIO.json.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/cont/SpanIO.json.hh
//---------------------------------------------------------------------------//
#pragma once

#include <nlohmann/json.hpp>

#include "Span.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Write a span to a JSON file.
*/
template<class T, std::size_t N>
void to_json(nlohmann::json& j, Span<T, N> const& value)
{
j = nlohmann::json::array();
for (std::size_t i = 0; i != value.size(); ++i)
{
j.push_back(value[i]);
}
}

//---------------------------------------------------------------------------//
} // namespace celeritas
18 changes: 18 additions & 0 deletions src/corecel/io/JsonPimpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include "celeritas_config.h"
#include "corecel/Assert.hh"

#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
Expand Down Expand Up @@ -42,5 +43,22 @@ struct JsonPimpl
#endif
};

//---------------------------------------------------------------------------//
/*!
* Helper function to write an object to JSON.
*
* This hides the "not configured" boilerplate.
*/
template<class T>
void to_json_pimpl(JsonPimpl* jp, T const& self)
{
#if CELERITAS_USE_JSON
CELER_EXPECT(jp);
to_json(jp->obj, self);
#else
CELER_NOT_CONFIGURED("JSON");
#endif
}

//---------------------------------------------------------------------------//
} // namespace celeritas
1 change: 1 addition & 0 deletions src/orange/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if(CELERITAS_USE_JSON)
OrangeInputIO.json.cc
detail/OrangeInputIOImpl.json.cc
orangeinp/CsgTreeIO.json.cc
orangeinp/ObjectIO.json.cc
)
list(APPEND PRIVATE_DEPS nlohmann_json::nlohmann_json)
endif()
Expand Down
14 changes: 14 additions & 0 deletions src/orange/OrangeTypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ char const* to_cstring(SurfaceType value)
return to_cstring_impl(value);
}

//---------------------------------------------------------------------------//
/*!
* Get a string corresponding to a transform type.
*/
char const* to_cstring(TransformType value)
{
static EnumStringMapper<TransformType> const to_cstring_impl{
"no_transformation",
"translation",
"transformation",
};
return to_cstring_impl(value);
}

//---------------------------------------------------------------------------//
/*!
* Get a printable character corresponding to a z ordering.
Expand Down
3 changes: 3 additions & 0 deletions src/orange/OrangeTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ inline constexpr char to_char(Sense s)
// Get a string corresponding to a surface type
char const* to_cstring(SurfaceType);

// Get a string corresponding to a transform type
char const* to_cstring(TransformType);

// Get a string corresponding to a surface state
inline char const* to_cstring(SurfaceState s)
{
Expand Down
59 changes: 59 additions & 0 deletions src/orange/orangeinp/ConvexRegion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "corecel/Constants.hh"
#include "corecel/cont/Range.hh"
#include "corecel/io/JsonPimpl.hh"
#include "geocel/BoundingBox.hh"
#include "geocel/Types.hh"
#include "orange/surf/ConeAligned.hh"
Expand All @@ -21,6 +22,10 @@

#include "ConvexSurfaceBuilder.hh"

#if CELERITAS_USE_JSON
# include "ObjectIO.json.hh"
#endif

namespace celeritas
{
namespace orangeinp
Expand Down Expand Up @@ -75,6 +80,15 @@ void Box::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(Sense::inside, PlaneZ{hw_[Z]});
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Box::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// CONE
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -167,6 +181,15 @@ void Cone::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(Sense::outside, interior_bbox);
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Cone::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// CYLINDER
//---------------------------------------------------------------------------//
Expand All @@ -191,6 +214,15 @@ void Cylinder::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(CCylZ{radius_});
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Cylinder::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// ELLIPSOID
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -249,6 +281,15 @@ void Ellipsoid::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(Sense::outside, BBox{-inner_radii, inner_radii});
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Ellipsoid::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// PRISM
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -319,6 +360,15 @@ void Prism::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(Sense::outside, interior_bbox);
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Prism::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// SPHERE
//---------------------------------------------------------------------------//
Expand All @@ -339,6 +389,15 @@ void Sphere::build(ConvexSurfaceBuilder& insert_surface) const
insert_surface(SphereCentered{radius_});
}

//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void Sphere::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
} // namespace orangeinp
} // namespace celeritas
65 changes: 65 additions & 0 deletions src/orange/orangeinp/ConvexRegion.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace celeritas
{
struct JsonPimpl;

namespace orangeinp
{
class ConvexSurfaceBuilder;
Expand Down Expand Up @@ -43,6 +45,9 @@ class ConvexRegionInterface
//! Construct surfaces that are AND-ed into this region
virtual void build(ConvexSurfaceBuilder&) const = 0;

//! Write the region to a JSON object
virtual void output(JsonPimpl*) const = 0;

protected:
//!@{
//! Allow construction and assignment only through daughter classes
Expand All @@ -65,6 +70,14 @@ class Box final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Half-width for each axis
Real3 const& halfwidths() const { return hw_; }

private:
Real3 hw_;
};
Expand Down Expand Up @@ -100,6 +113,17 @@ class Cone final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Lower and upper radii
Real2 const& radii() const { return radii_; }

//! Half-height along Z
real_type halfheight() const { return hh_; }

private:
Real2 radii_;
real_type hh_;
Expand All @@ -118,6 +142,17 @@ class Cylinder final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Radius
real_type radius() const { return radius_; }

//! Half-height along Z
real_type halfheight() const { return hh_; }

private:
real_type radius_;
real_type hh_;
Expand All @@ -136,6 +171,14 @@ class Ellipsoid final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Radius along each axis
Real3 const& radii() const { return radii_; }

private:
Real3 radii_;
};
Expand Down Expand Up @@ -171,6 +214,20 @@ class Prism final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Number of sides
int num_sides() const { return num_sides_; }
//! Inner radius
real_type apothem() const { return apothem_; }
//! Half the Z height
real_type halfheight() const { return hh_; }
//! Rotation factor
real_type orientation() const { return orientation_; }

private:
// Number of sides
int num_sides_;
Expand Down Expand Up @@ -198,6 +255,14 @@ class Sphere final : public ConvexRegionInterface
// Build surfaces
void build(ConvexSurfaceBuilder&) const final;

// Output to JSON
void output(JsonPimpl*) const final;

//// ACCESSORS ////

//! Radius
real_type radius() const { return radius_; }

private:
real_type radius_;
};
Expand Down
25 changes: 25 additions & 0 deletions src/orange/orangeinp/CsgObject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

#include <utility>

#include "corecel/io/JsonPimpl.hh"

#include "detail/CsgUnitBuilder.hh"
#include "detail/VolumeBuilder.hh"

#if CELERITAS_USE_JSON
# include "ObjectIO.json.hh"
#endif

namespace celeritas
{
namespace orangeinp
Expand Down Expand Up @@ -49,6 +55,15 @@ NodeId NegatedObject::build(VolumeBuilder& vb) const
return vb.insert_region(Label{label_}, Negated{daughter_id});
}

//---------------------------------------------------------------------------//
/*!
* Output to JSON.
*/
void NegatedObject::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// JOIN_OBJECTS
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -91,6 +106,16 @@ NodeId JoinObjects<Op>::build(VolumeBuilder& vb) const
return vb.insert_region(Label{label_}, Joined{op_token, std::move(nodes)});
}

//---------------------------------------------------------------------------//
/*!
* Output to JSON.
*/
template<OperatorToken Op>
void JoinObjects<Op>::output(JsonPimpl* j) const
{
to_json_pimpl(j, *this);
}

//---------------------------------------------------------------------------//
// FREE FUNCTIONS
//---------------------------------------------------------------------------//
Expand Down

0 comments on commit 2596ea8

Please sign in to comment.