diff --git a/io/include/detray/io/common/detail/definitions.hpp b/io/include/detray/io/common/detail/definitions.hpp index c30a05fe0..1718d6f32 100644 --- a/io/include/detray/io/common/detail/definitions.hpp +++ b/io/include/detray/io/common/detail/definitions.hpp @@ -19,6 +19,8 @@ #include "detray/masks/ring2D.hpp" #include "detray/masks/single3D.hpp" #include "detray/masks/trapezoid2D.hpp" +#include "detray/materials/material_rod.hpp" +#include "detray/materials/material_slab.hpp" #include "detray/utils/type_registry.hpp" namespace detray { @@ -85,6 +87,26 @@ enum class material_type : unsigned int { unknown = 11u }; +/// Infer the IO material id from the material type +template +constexpr material_type get_material_id() { + using scalar_t = typename material_t::scalar_type; + + /// Register the material types to the @c material_type enum + using mat_registry = + type_registry, cuboid3D<>, cylinder2D<>, + cylinder3D, rectangle2D<>, ring2D<>, trapezoid2D<>, + line, line, material_slab, + material_rod>; + + // Find the correct material IO id; + if constexpr (mat_registry::is_defined(material_t{})) { + return mat_registry::get_id(material_t{}); + } else { + return material_type::unknown; + } +} + /// Enumerate the different acceleration data structures enum class acc_type : unsigned int { brute_force = 0u, // try all diff --git a/io/include/detray/io/common/geometry_reader.hpp b/io/include/detray/io/common/geometry_reader.hpp index 2ec11da9d..92a05d579 100644 --- a/io/include/detray/io/common/geometry_reader.hpp +++ b/io/include/detray/io/common/geometry_reader.hpp @@ -135,11 +135,6 @@ class geometry_reader : public reader_interface { det_builder.template set_volume_finder(); } - /// @returns a link from its io payload @param link_data - static dindex deserialize(const single_link_payload& link_data) { - return static_cast(link_data.link); - } - /// @returns a surface transform from its io payload @param trf_data static typename detector_t::transform3 deserialize( const transform_payload& trf_data) { @@ -177,7 +172,8 @@ class geometry_reader : public reader_interface { std::back_inserter(mask_boundaries)); return {deserialize(sf_data.transform), - static_cast(deserialize(sf_data.mask.volume_link)), + static_cast( + base_type::deserialize(sf_data.mask.volume_link)), std::move(mask_boundaries)}; } diff --git a/io/include/detray/io/common/geometry_writer.hpp b/io/include/detray/io/common/geometry_writer.hpp index cbc6405a8..ccd9c1ba7 100644 --- a/io/include/detray/io/common/geometry_writer.hpp +++ b/io/include/detray/io/common/geometry_writer.hpp @@ -15,8 +15,6 @@ #include "detray/io/common/io_interface.hpp" #include "detray/io/common/payloads.hpp" #include "detray/masks/masks.hpp" -#include "detray/materials/material_rod.hpp" -#include "detray/materials/material_slab.hpp" // System include(s) #include @@ -76,14 +74,6 @@ class geometry_writer : public writer_interface { return det_data; } - /// Serialize a link @param idx into its io payload - static single_link_payload serialize(const std::size_t idx) { - single_link_payload link_data; - link_data.link = idx; - - return link_data; - } - /// Serialize a surface transform @param trf into its io payload static transform_payload serialize( const typename detector_t::transform3& trf) { @@ -109,7 +99,7 @@ class geometry_writer : public writer_interface { mask_data.shape = io::detail::get_shape_id(); - mask_data.volume_link = serialize(m.volume_link()); + mask_data.volume_link = base_type::serialize(m.volume_link()); mask_data.boundaries.resize(mask_t::boundaries::e_size); std::copy(std::cbegin(m.values()), std::cend(m.values()), @@ -118,29 +108,6 @@ class geometry_writer : public writer_interface { return mask_data; } - /// Serialize a surface material link @param m into its io payload - template - static material_link_payload serialize(const std::size_t idx) { - using scalar_t = typename material_t::scalar_type; - using type_id = material_link_payload::material_type; - - material_link_payload mat_data; - - // Find the correct material type index (use name for simplicity) - if constexpr (std::is_same_v>) { - mat_data.type = type_id::slab; - } else if constexpr (std::is_same_v>) { - mat_data.type = type_id::rod; - } else { - mat_data.type = type_id::unknown; - } - - mat_data.index = idx; - - return mat_data; - } - /// Serialize a detector surface @param sf into its io payload static surface_payload serialize(const surface& sf) { surface_payload sf_data; @@ -150,28 +117,18 @@ class geometry_writer : public writer_interface { sf_data.transform = serialize(sf.transform({})); sf_data.mask = sf.template visit_mask(); sf_data.material = sf.template visit_material(); - sf_data.source = serialize(sf.source()); + sf_data.source = base_type::serialize(sf.source()); return sf_data; } - /// Serialize a link @param idx into its io payload - static acc_links_payload serialize(const acc_links_payload::acc_type id, - const std::size_t idx) { - acc_links_payload link_data; - link_data.type = id; - link_data.index = idx; - - return link_data; - } - /// Serialize a detector portal @param sf into its io payload static volume_payload serialize( const typename detector_t::volume_type& vol_desc, const detector_t& det, const std::string& name) { volume_payload vol_data; - vol_data.index = serialize(vol_desc.index()); + vol_data.index = base_type::serialize(vol_desc.index()); vol_data.name = name; vol_data.transform = serialize(det.transform_store()[vol_desc.transform()]); @@ -218,8 +175,11 @@ class geometry_writer : public writer_interface { template inline auto operator()(const material_group_t&, const index_t& index) const { - return geometry_writer::template serialize< - typename material_group_t::value_type>(index); + using material_t = typename material_group_t::value_type; + + // Find the correct material type index + return base_type::serialize( + io::detail::get_material_id(), index); } }; @@ -231,16 +191,14 @@ class geometry_writer : public writer_interface { using accel_t = typename acc_group_t::value_type; - if constexpr (detail::is_grid_v) { - constexpr auto id{io::detail::get_grid_id()}; + auto id{acc_links_payload::type_id::unknown}; - return geometry_writer::serialize(id, index); - } else { - // This functor is only called for accelerator data structures - // that are not 'brute force' - return geometry_writer::serialize( - acc_links_payload::acc_type::unknown, index); + // Only serialize grids + if constexpr (detail::is_grid_v) { + id = io::detail::get_grid_id(); } + + return base_type::serialize(id, index); } }; }; diff --git a/io/include/detray/io/common/grid_writer.hpp b/io/include/detray/io/common/grid_writer.hpp index 7a34cde1c..46d5fc02e 100644 --- a/io/include/detray/io/common/grid_writer.hpp +++ b/io/include/detray/io/common/grid_writer.hpp @@ -45,12 +45,11 @@ class grid_writer : public writer_interface { const std::string_view det_name) { grid_header_payload header_data; - header_data.version = detail::get_detray_version(); - header_data.detector = det_name; - header_data.tag = tag; - header_data.date = detail::get_current_date(); + header_data.common = base_type::serialize(det_name, tag); - header_data.n_grids = get_n_grids(det.surface_store()); + header_data.sub_header.emplace(); + auto& grid_sub_header = header_data.sub_header.value(); + grid_sub_header.n_grids = get_n_grids(det.surface_store()); return header_data; } @@ -62,8 +61,22 @@ class grid_writer : public writer_interface { detector_grids_payload grids_data; - // Access the acceleration data structures recursively - get_grid_payload(det.surface_store(), grids_data); + for (const auto& vol_desc : det.volumes()) { + // Links to all acceleration data structures in the volume + const auto& multi_link = vol_desc.full_link(); + + for (dindex i = 0u; i < multi_link.size(); ++i) { + const auto& acc_link = multi_link[i]; + // Don't look at empty links + if (acc_link.is_invalid()) { + continue; + } + + // If the accelerator is a grid, insert the payload + det.surface_store().template visit( + acc_link, vol_desc.index(), grids_data); + } + } return grids_data; } @@ -71,12 +84,13 @@ class grid_writer : public writer_interface { /// Serialize a grid @param gr of type @param type and index @param idx /// into its io payload template - static grid_payload serialize(io::detail::acc_type type, + static grid_payload serialize(std::size_t volume_index, + io::detail::acc_type type, const std::size_t idx, const grid_t& gr) { grid_payload grid_data; - grid_data.type = type; - grid_data.index = idx; + grid_data.volume_link = base_type::serialize(volume_index); + grid_data.acc_link = base_type::serialize(type, idx); // Serialize the multi-axis into single axis payloads const std::array axes_data = @@ -152,30 +166,25 @@ class grid_writer : public writer_interface { } private: - /// Retrieve @c grid_payload (s) from grid collection elements - template - static void get_grid_payload( - const typename detector_t::surface_container& store, - detector_grids_payload& grids_data) { + /// Retrieve a @c grid_payload from grid collection elements + struct get_grid_payload { - using store_t = typename detector_t::surface_container; - constexpr auto coll_id{store_t::value_types::to_id(I)}; - using accel_t = typename store_t::template get_type; - - if constexpr (detail::is_grid_v) { + template + inline void operator()( + [[maybe_unused]] const grid_group_t& coll, + [[maybe_unused]] const index_t& index, + [[maybe_unused]] std::size_t volume_index, + [[maybe_unused]] detector_grids_payload& grids_data) const { + using accel_t = typename grid_group_t::value_type; - const auto& coll = store.template get(); + if constexpr (detail::is_grid_v) { - for (unsigned int i = 0u; i < coll.size(); ++i) { grids_data.grids.push_back( - serialize(io::detail::get_grid_id(), i, coll[i])); + serialize(volume_index, io::detail::get_grid_id(), + index, coll[index])); } } - - if constexpr (I < store_t::n_collections() - 1u) { - get_grid_payload(store, grids_data); - } - } + }; /// Retrieve number of overall grids in detector template diff --git a/io/include/detray/io/common/homogeneous_material_reader.hpp b/io/include/detray/io/common/homogeneous_material_reader.hpp index 342820f0d..4e0d8815a 100644 --- a/io/include/detray/io/common/homogeneous_material_reader.hpp +++ b/io/include/detray/io/common/homogeneous_material_reader.hpp @@ -24,7 +24,7 @@ namespace detray { -/// @brief Abstract base class for tracking geometry readers +/// @brief Abstract base class for a homogeneous material reader. template class homogeneous_material_reader : public reader_interface { @@ -57,7 +57,7 @@ class homogeneous_material_reader : public reader_interface { for (const auto& mv_data : det_mat_data.volumes) { // Decorate the current volume builder with material auto vm_builder = det_builder.template decorate( - static_cast(mv_data.index)); + base_type::deserialize(mv_data.volume_link)); // Add the material data to the factory auto mat_factory = std::make_shared>(); diff --git a/io/include/detray/io/common/homogeneous_material_writer.hpp b/io/include/detray/io/common/homogeneous_material_writer.hpp index 3885a354e..b16bd5ef2 100644 --- a/io/include/detray/io/common/homogeneous_material_writer.hpp +++ b/io/include/detray/io/common/homogeneous_material_writer.hpp @@ -13,7 +13,6 @@ #include "detray/io/common/payloads.hpp" #include "detray/materials/material_rod.hpp" #include "detray/materials/material_slab.hpp" -#include "detray/utils/ranges.hpp" // System include(s) #include @@ -26,6 +25,7 @@ template class homogeneous_material_writer : public writer_interface { using base_type = writer_interface; + using scalar_t = typename detector_t::scalar_type; protected: /// Tag the writer as "homogeneous_material" @@ -82,10 +82,10 @@ class homogeneous_material_writer : public writer_interface { static material_volume_payload serialize( const typename detector_t::volume_type& vol_desc, const detector_t& det) { - using material_type = material_slab_payload::material_type; + using material_type = material_slab_payload::type; material_volume_payload mv_data; - mv_data.index = vol_desc.index(); + mv_data.volume_link = base_type::serialize(vol_desc.index()); // Find all surfaces that belong to the volume for (const auto& sf_desc : det.surface_lookup()) { @@ -97,9 +97,9 @@ class homogeneous_material_writer : public writer_interface { const material_slab_payload mslp = sf.template visit_material(); - if (mslp.type == material_type::slab) { + if (mslp.mat_link.type == material_type::slab) { mv_data.mat_slabs.push_back(mslp); - } else if (mslp.type == material_type::rod) { + } else if (mslp.mat_link.type == material_type::rod) { if (not mv_data.mat_rods.has_value()) { mv_data.mat_rods.emplace(); } @@ -107,7 +107,7 @@ class homogeneous_material_writer : public writer_interface { } else { throw std::runtime_error( "Material could not be matched to payload (found type " + - std::to_string(static_cast(mslp.type)) + ")"); + std::to_string(static_cast(mslp.mat_link.type)) + ")"); } } @@ -131,26 +131,24 @@ class homogeneous_material_writer : public writer_interface { /// Serialize a surface material slab @param mat_slab into its io payload static material_slab_payload serialize( - const material_slab& mat_slab, - std::size_t idx) { + const material_slab& mat_slab, std::size_t idx) { material_slab_payload mat_data; - mat_data.type = material_slab_payload::material_type::slab; - mat_data.index = idx; + mat_data.mat_link = base_type::serialize( + io::detail::get_material_id>(), idx); mat_data.thickness = mat_slab.thickness(); mat_data.mat = serialize(mat_slab.get_material()); return mat_data; } - /// Serialize a line material rod @param mat_rod into its io payload + /// Serialize a wire material rod @param mat_rod into its io payload static material_slab_payload serialize( - const material_rod& mat_rod, - std::size_t idx) { + const material_rod& mat_rod, std::size_t idx) { material_slab_payload mat_data; - mat_data.type = material_slab_payload::material_type::rod; - mat_data.index = idx; + mat_data.mat_link = base_type::serialize( + io::detail::get_material_id>(), idx); mat_data.thickness = mat_rod.radius(); mat_data.mat = serialize(mat_rod.get_material()); diff --git a/io/include/detray/io/common/io_interface.hpp b/io/include/detray/io/common/io_interface.hpp index 74ec97c2a..218fb4da8 100644 --- a/io/include/detray/io/common/io_interface.hpp +++ b/io/include/detray/io/common/io_interface.hpp @@ -41,6 +41,11 @@ class reader_interface { typename detector_t::name_map&, const std::string&) = 0; protected: + /// @returns a link from its io payload @param link_data + static dindex deserialize(const single_link_payload& link_data) { + return static_cast(link_data.link); + } + /// Extension that matches the file format of the respective reader std::string m_file_extension; }; @@ -81,6 +86,27 @@ class writer_interface { return header_data; } + /// Serialize a link @param idx into its io payload + static single_link_payload serialize(const std::size_t idx) { + single_link_payload link_data; + link_data.link = idx; + + return link_data; + } + + /// Serialize a typed link with a type id @param id and and index + /// @param idx into its io payload + template + static typed_link_payload serialize(const type_id id, + const std::size_t idx) { + typed_link_payload link_data; + + link_data.type = id; + link_data.index = idx; + + return link_data; + } + /// Extension that matches the file format of the respective writer std::string m_file_extension; }; diff --git a/io/include/detray/io/common/payloads.hpp b/io/include/detray/io/common/payloads.hpp index ea5686868..78ac52529 100644 --- a/io/include/detray/io/common/payloads.hpp +++ b/io/include/detray/io/common/payloads.hpp @@ -26,19 +26,28 @@ namespace detray { /// @brief a payload for common information struct common_header_payload { - std::string version, detector, tag, date; + std::string version{}, detector{}, tag{}, date{}; }; /// @brief a payload for common and extra information template struct header_payload { - common_header_payload common; + common_header_payload common{}; std::optional sub_header; }; /// @brief A payload for a single object link struct single_link_payload { - std::size_t link; + std::size_t link{std::numeric_limits::max()}; +}; + +/// @brief A payload for a typed object link +template +struct typed_link_payload { + using type_id = type_id_t; + + type_id type{type_id::unknown}; + std::size_t index{std::numeric_limits::max()}; }; /// Geometry payloads @@ -46,62 +55,55 @@ struct single_link_payload { /// @brief a payload for the geometry specific part of the file header struct geo_sub_header_payload { - std::size_t n_volumes, n_surfaces; + std::size_t n_volumes{0ul}, n_surfaces{0ul}; }; /// @brief a payload for the geometry file header using geo_header_payload = header_payload; +/// @brief A payload object to link a surface to its material +using material_link_payload = typed_link_payload; + +/// @brief A payload object to link a volume to its acceleration data structures +using acc_links_payload = typed_link_payload; + /// @brief A payload for an affine transformation in homogeneous coordinates struct transform_payload { - std::array tr; + std::array tr{}; // Column major - std::array rot; + std::array rot{}; }; /// @brief A payload object for surface masks struct mask_payload { using mask_shape = io::detail::mask_shape; - mask_shape shape = mask_shape::unknown; - single_link_payload volume_link; - std::vector boundaries; -}; -/// @brief A payload object to link a surface to its material -struct material_link_payload { - using material_type = io::detail::material_type; - material_type type = material_type::unknown; - std::size_t index; + mask_shape shape{mask_shape::unknown}; + single_link_payload volume_link{}; + std::vector boundaries{}; }; /// @brief A payload for surfaces struct surface_payload { - transform_payload transform; - mask_payload mask; + transform_payload transform{}; + mask_payload mask{}; std::optional material; - single_link_payload source; + single_link_payload source{}; // Write the surface barcode as an additional information - std::uint64_t barcode; - detray::surface_id type = detray::surface_id::e_sensitive; -}; - -/// @brief A payload object to link a volume to its acceleration data structures -struct acc_links_payload { - using acc_type = io::detail::acc_type; - acc_type type; - std::size_t index; + std::uint64_t barcode{std::numeric_limits::max()}; + detray::surface_id type{detray::surface_id::e_sensitive}; }; /// @brief A payload for volumes struct volume_payload { - std::string name = ""; - detray::volume_id type = detray::volume_id::e_cylinder; - transform_payload transform; - std::vector surfaces; + std::string name{}; + detray::volume_id type{detray::volume_id::e_cylinder}; + transform_payload transform{}; + std::vector surfaces{}; // Index of the volume in the detector volume container - single_link_payload index; + single_link_payload index{}; // Optional accelerator data structures - std::optional> acc_links; + std::optional> acc_links{}; }; /// @} @@ -111,7 +113,7 @@ struct volume_payload { /// @brief a payload for the material specific part of the file header struct homogeneous_material_sub_header_payload { - std::size_t n_slabs, n_rods; + std::size_t n_slabs{0ul}, n_rods{0ul}; }; /// @brief a payload for the homogeneous material file header @@ -120,28 +122,28 @@ using homogeneous_material_header_payload = /// @brief A payload object for a material parametrization struct material_payload { - std::array params; + std::array params{}; }; /// @brief A payload object for a material slab/rod struct material_slab_payload { - using material_type = io::detail::material_type; - material_type type = material_type::unknown; - std::size_t index; - real_io thickness; - material_payload mat; + using type = io::detail::material_type; + + material_link_payload mat_link{}; + real_io thickness{std::numeric_limits::max()}; + material_payload mat{}; }; /// @brief A payload object for the material contained in a volume struct material_volume_payload { - std::size_t index; - std::vector mat_slabs = {}; + single_link_payload volume_link{}; + std::vector mat_slabs{}; std::optional> mat_rods; }; /// @brief A payload for the homogeneous material description of a detector struct detector_homogeneous_material_payload { - std::vector volumes = {}; + std::vector volumes{}; }; /// @} @@ -149,51 +151,43 @@ struct detector_homogeneous_material_payload { /// Payloads for a uniform grid /// @{ -/// @brief a payload for the simple grid file header -struct grid_header_payload { - std::string version, detector, tag, date; - std::size_t n_grids; +/// @brief a payload for the grid specific part of the file header +struct grid_sub_header_payload { + std::size_t n_grids{0u}; }; +/// @brief a payload for the grid file header +using grid_header_payload = header_payload; + /// @brief axis definition and bin edges struct axis_payload { /// axis lookup type - n_axis::binning binning = n_axis::binning::e_regular; - n_axis::bounds bounds = n_axis::bounds::e_closed; - n_axis::label label = n_axis::label::e_r; + n_axis::binning binning{n_axis::binning::e_regular}; + n_axis::bounds bounds{n_axis::bounds::e_closed}; + n_axis::label label{n_axis::label::e_r}; - std::size_t bins = 0u; - std::vector edges = {}; + std::size_t bins{0u}; + std::vector edges{}; }; /// @brief A payload for a grid bin struct grid_bin_payload { - std::vector loc_index = {}; - std::vector content = {}; + std::vector loc_index{}; + std::vector content{}; }; /// @brief A payload for a grid definition struct grid_payload { using grid_type = io::detail::acc_type; - grid_type type = grid_type::unknown; - std::size_t index; - std::vector axes = {}; - std::vector bins = {}; -}; + single_link_payload volume_link{}; + acc_links_payload acc_link{}; -/// @brief A payload for objects within a grid -struct grid_objects_payload { - grid_payload grid; + std::vector axes{}; + std::vector bins{}; std::optional transform; }; -/// @brief navigation links definition -struct links_payload { - std::vector single_links; - std::optional grid_links; -}; - /// @brief A payload for the grid collections of a detector struct detector_grids_payload { std::vector grids = {}; @@ -204,7 +198,7 @@ struct detector_grids_payload { /// @brief A payload for a detector geometry struct detector_payload { std::vector volumes = {}; - grid_objects_payload volume_grid; + grid_payload volume_grid; }; } // namespace detray diff --git a/io/include/detray/io/json/json_header_io.hpp b/io/include/detray/io/json/json_common_io.hpp similarity index 62% rename from io/include/detray/io/json/json_header_io.hpp rename to io/include/detray/io/json/json_common_io.hpp index 83c8afae6..014a4d8fe 100644 --- a/io/include/detray/io/json/json_header_io.hpp +++ b/io/include/detray/io/json/json_common_io.hpp @@ -37,4 +37,28 @@ void from_json(const nlohmann::ordered_json& j, header_payload& h) { // Do not look at the optional subheader here } +/// Data links IO +/// @{ +void to_json(nlohmann::ordered_json& j, const single_link_payload& so) { + j = so.link; +} + +void from_json(const nlohmann::ordered_json& j, single_link_payload& so) { + so.link = j; +} + +template +void to_json(nlohmann::ordered_json& j, const typed_link_payload& m) { + j["type"] = static_cast(m.type); + j["index"] = m.index; +} + +template +void from_json(const nlohmann::ordered_json& j, + typed_link_payload& m) { + m.type = static_cast(j["type"]); + m.index = j["index"]; +} +/// @} + } // namespace detray diff --git a/io/include/detray/io/json/json_geometry_io.hpp b/io/include/detray/io/json/json_geometry_io.hpp index 0168d0514..ef60cca79 100644 --- a/io/include/detray/io/json/json_geometry_io.hpp +++ b/io/include/detray/io/json/json_geometry_io.hpp @@ -11,7 +11,8 @@ #include "detray/io/common/payloads.hpp" #include "detray/io/json/json.hpp" #include "detray/io/json/json_algebra_io.hpp" -#include "detray/io/json/json_header_io.hpp" +#include "detray/io/json/json_common_io.hpp" +#include "detray/io/json/json_grids_io.hpp" // System include(s) #include @@ -45,14 +46,6 @@ void from_json(const nlohmann::ordered_json& j, geo_header_payload& h) { } } -void to_json(nlohmann::ordered_json& j, const single_link_payload& so) { - j = so.link; -} - -void from_json(const nlohmann::ordered_json& j, single_link_payload& so) { - so.link = j; -} - void to_json(nlohmann::ordered_json& j, const mask_payload& m) { j["shape"] = static_cast(m.shape); j["volume_link"] = m.volume_link; @@ -65,16 +58,6 @@ void from_json(const nlohmann::ordered_json& j, mask_payload& m) { m.boundaries = j["boundaries"].get>(); } -void to_json(nlohmann::ordered_json& j, const material_link_payload& m) { - j["type"] = static_cast(m.type); - j["index"] = m.index; -} - -void from_json(const nlohmann::ordered_json& j, material_link_payload& m) { - m.type = static_cast(j["type"]); - m.index = j["index"]; -} - void to_json(nlohmann::ordered_json& j, const surface_payload& s) { j["barcode"] = s.barcode; j["type"] = static_cast(s.type); @@ -97,16 +80,6 @@ void from_json(const nlohmann::ordered_json& j, surface_payload& s) { } } -void to_json(nlohmann::ordered_json& j, const acc_links_payload& al) { - j["type"] = static_cast(al.type); - j["index"] = al.index; -} - -void from_json(const nlohmann::ordered_json& j, acc_links_payload& al) { - al.type = static_cast(j["type"]); - al.index = j["index"]; -} - void to_json(nlohmann::ordered_json& j, const volume_payload& v) { j["name"] = v.name; j["index"] = v.index; @@ -144,4 +117,24 @@ void from_json(const nlohmann::ordered_json& j, volume_payload& v) { } } +void to_json(nlohmann::ordered_json& j, const detector_payload& d) { + if (not d.volumes.empty()) { + nlohmann::ordered_json jvolumes; + for (const auto& v : d.volumes) { + jvolumes.push_back(v); + } + j["volumes"] = jvolumes; + j["volume_grid"] = d.volume_grid; + } +} + +void from_json(const nlohmann::ordered_json& j, detector_payload& d) { + if (j.find("volumes") != j.end()) { + for (auto jvolume : j["volumes"]) { + d.volumes.push_back(jvolume); + } + d.volume_grid = j["volume_grid"]; + } +} + } // namespace detray diff --git a/io/include/detray/io/json/json_grids_io.hpp b/io/include/detray/io/json/json_grids_io.hpp index ec1d66a6e..717c5ddaf 100644 --- a/io/include/detray/io/json/json_grids_io.hpp +++ b/io/include/detray/io/json/json_grids_io.hpp @@ -12,7 +12,7 @@ #include "detray/io/common/payloads.hpp" #include "detray/io/json/json.hpp" #include "detray/io/json/json_algebra_io.hpp" -#include "detray/io/json/json_geometry_io.hpp" +#include "detray/io/json/json_common_io.hpp" // System include(s). #include @@ -22,19 +22,22 @@ namespace detray { void to_json(nlohmann::ordered_json& j, const grid_header_payload& h) { - j["version"] = h.version; - j["detector"] = h.detector; - j["date"] = h.date; - j["tag"] = h.tag; - j["no. grids"] = h.n_grids; + j["common"] = h.common; + + if (h.sub_header.has_value()) { + const auto& grid_sub_header = h.sub_header.value(); + j["grid_count"] = grid_sub_header.n_grids; + } } void from_json(const nlohmann::ordered_json& j, grid_header_payload& h) { - h.version = j["version"]; - h.detector = j["detector"]; - h.date = j["date"]; - h.tag = j["tag"]; - h.n_grids = j["no. grids"]; + h.common = j["common"]; + + if (j.find("grid_count") != j.end()) { + h.sub_header.emplace(); + auto& grid_sub_header = h.sub_header.value(); + grid_sub_header.n_grids = j["grid_count"]; + } } void to_json(nlohmann::ordered_json& j, const axis_payload& a) { @@ -64,8 +67,8 @@ void from_json(const nlohmann::ordered_json& j, grid_bin_payload& g) { } void to_json(nlohmann::ordered_json& j, const grid_payload& g) { - j["type"] = static_cast(g.type); - j["index"] = g.index; + j["volume_link"] = g.volume_link; + j["acc_link"] = g.acc_link; nlohmann::ordered_json jaxes; for (const auto& a : g.axes) { @@ -78,11 +81,15 @@ void to_json(nlohmann::ordered_json& j, const grid_payload& g) { jbins.push_back(bin); } j["bins"] = jbins; + + if (g.transform.has_value()) { + j["transform"] = g.transform.value(); + } } void from_json(const nlohmann::ordered_json& j, grid_payload& g) { - g.type = static_cast(j["type"]); - g.index = j["index"]; + g.volume_link = j["volume_link"]; + g.acc_link = j["acc_link"]; nlohmann::ordered_json jaxes = j["axes"]; for (auto jax : jaxes) { @@ -95,64 +102,13 @@ void from_json(const nlohmann::ordered_json& j, grid_payload& g) { grid_bin_payload b = jbin; g.bins.push_back(std::move(b)); } -} -void to_json(nlohmann::ordered_json& j, const grid_objects_payload& g) { - j["grid"] = g.grid; - if (g.transform.has_value()) { - j["transform"] = g.transform.value(); - } -} - -void from_json(const nlohmann::ordered_json& j, grid_objects_payload& g) { - g.grid = j["grid"]; if (j.find("transform") != j.end()) { + g.transform.emplace(); g.transform = j["transform"]; } } -void to_json(nlohmann::ordered_json& j, const links_payload& l) { - nlohmann::ordered_json js; - for (const auto& so : l.single_links) { - js.push_back(so); - } - j["single_links"] = js; - if (l.grid_links.has_value()) { - j["grid_links"] = l.grid_links.value(); - } -} - -void from_json(const nlohmann::ordered_json& j, links_payload& l) { - nlohmann::ordered_json jsl = j["single_links"]; - for (auto jl : jsl) { - single_link_payload sl = jl; - l.single_links.push_back(sl); - } - if (j.find("grid_links") != j.end()) { - l.grid_links = j["grid_links"]; - } -} - -void to_json(nlohmann::ordered_json& j, const detector_payload& d) { - if (not d.volumes.empty()) { - nlohmann::ordered_json jvolumes; - for (const auto& v : d.volumes) { - jvolumes.push_back(v); - } - j["volumes"] = jvolumes; - j["volume_grid"] = d.volume_grid; - } -} - -void from_json(const nlohmann::ordered_json& j, detector_payload& d) { - if (j.find("volumes") != j.end()) { - for (auto jvolume : j["volumes"]) { - d.volumes.push_back(jvolume); - } - d.volume_grid = j["volume_grid"]; - } -} - void to_json(nlohmann::ordered_json& j, const detector_grids_payload& d) { if (not d.grids.empty()) { nlohmann::ordered_json jgrids; diff --git a/io/include/detray/io/json/json_material_io.hpp b/io/include/detray/io/json/json_material_io.hpp index 20b341d07..accd52928 100644 --- a/io/include/detray/io/json/json_material_io.hpp +++ b/io/include/detray/io/json/json_material_io.hpp @@ -10,7 +10,7 @@ // Project include(s) #include "detray/io/common/payloads.hpp" #include "detray/io/json/json.hpp" -#include "detray/io/json/json_header_io.hpp" +#include "detray/io/json/json_common_io.hpp" // System include(s) #include @@ -52,21 +52,19 @@ void from_json(const nlohmann::ordered_json& j, material_payload& m) { } void to_json(nlohmann::ordered_json& j, const material_slab_payload& m) { - j["type"] = material_slab_payload::material_type::slab; - j["index"] = m.index; + j["mat_link"] = m.mat_link; j["thickness"] = m.thickness; j["material"] = m.mat; } void from_json(const nlohmann::ordered_json& j, material_slab_payload& m) { - m.type = material_slab_payload::material_type::slab; - m.index = j["index"]; + m.mat_link = j["mat_link"]; m.thickness = j["thickness"]; m.mat = j["material"]; } void to_json(nlohmann::ordered_json& j, const material_volume_payload& mv) { - j["index"] = mv.index; + j["volume_link"] = mv.volume_link; if (not mv.mat_slabs.empty()) { nlohmann::ordered_json jmats; @@ -85,7 +83,7 @@ void to_json(nlohmann::ordered_json& j, const material_volume_payload& mv) { } void from_json(const nlohmann::ordered_json& j, material_volume_payload& mv) { - mv.index = j["index"]; + mv.volume_link = j["volume_link"]; if (j.find("material_slabs") != j.end()) { for (auto jmats : j["material_slabs"]) { diff --git a/io/include/detray/io/json/json_serializers.hpp b/io/include/detray/io/json/json_serializers.hpp index 89a16083f..f16f865cf 100644 --- a/io/include/detray/io/json/json_serializers.hpp +++ b/io/include/detray/io/json/json_serializers.hpp @@ -10,5 +10,4 @@ #include "detray/io/json/json_algebra_io.hpp" #include "detray/io/json/json_geometry_io.hpp" #include "detray/io/json/json_grids_io.hpp" -#include "detray/io/json/json_header_io.hpp" #include "detray/io/json/json_material_io.hpp" diff --git a/tests/unit_tests/io/io_json_payload.cpp b/tests/unit_tests/io/io_json_payload.cpp index 690e86ac4..f1fc95d89 100644 --- a/tests/unit_tests/io/io_json_payload.cpp +++ b/tests/unit_tests/io/io_json_payload.cpp @@ -137,8 +137,8 @@ TEST(io, json_grid_payload) { detray::n_axis::label::e_r, 2u, std::vector{0.f, 2.f}}; detray::grid_payload g; - g.type = detray::grid_payload::grid_type::polar2_grid; - g.index = 12u; + g.acc_link = {detray::grid_payload::grid_type::polar2_grid, 12u}; + g.volume_link = {2u}; g.axes = {a0, a1}; g.bins = bins; @@ -147,106 +147,12 @@ TEST(io, json_grid_payload) { detray::grid_payload pg = j["grid"]; - EXPECT_EQ(g.type, pg.type); - EXPECT_EQ(g.index, pg.index); + EXPECT_EQ(g.acc_link.type, pg.acc_link.type); + EXPECT_EQ(g.acc_link.index, pg.acc_link.index); EXPECT_EQ(g.axes.size(), pg.axes.size()); EXPECT_EQ(g.bins.size(), pg.bins.size()); } -/// This tests the json io for a surface search grid -TEST(io, grid_objects_payload) { - detray::grid_objects_payload go; - - std::vector bins = { - {{0u, 1u}, {0u, 2u}}, {{1u, 1u}, {1u, 2u}}, {{2u, 1u}, {2u, 2u}}}; - - detray::axis_payload a0{ - detray::n_axis::binning::e_regular, detray::n_axis::bounds::e_circular, - detray::n_axis::label::e_phi, 3u, - std::vector{-detray::constant::pi, - detray::constant::pi}}; - - detray::axis_payload a1{ - detray::n_axis::binning::e_regular, detray::n_axis::bounds::e_closed, - detray::n_axis::label::e_r, 2u, std::vector{0.f, 2.f}}; - - detray::grid_payload g; - g.type = detray::grid_payload::grid_type::polar2_grid; - g.index = 12u; - g.axes = {a0, a1}; - g.bins = bins; - - go.grid = g; - - nlohmann::ordered_json j; - j["links"] = go; - detray::grid_objects_payload pgo = j["links"]; - - EXPECT_EQ(go.grid.axes.size(), pgo.grid.axes.size()); - EXPECT_EQ(go.grid.bins.size(), pgo.grid.bins.size()); - - detray::transform_payload p; - p.tr = {100.f, 200.f, 300.f}; - p.rot = {1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f}; - - detray::grid_objects_payload got; - got.transform = p; - - got.grid = g; - - j["links_t"] = got; - detray::grid_objects_payload pgot = j["links_t"]; - - EXPECT_EQ(got.grid.axes.size(), pgot.grid.axes.size()); - EXPECT_EQ(got.grid.bins.size(), pgot.grid.bins.size()); - EXPECT_EQ(got.transform.value().tr, pgot.transform.value().tr); - EXPECT_EQ(got.transform.value().rot, pgot.transform.value().rot); -} - -/// This tests the json io for a surface search grid, including links -TEST(io, json_links_payload) { - - std::vector bins = { - {{0u, 1u}, {0u, 2u}}, {{1u, 1u}, {1u, 2u}}, {{2u, 1u}, {2u, 2u}}}; - - detray::axis_payload a0{ - detray::n_axis::binning::e_regular, detray::n_axis::bounds::e_circular, - detray::n_axis::label::e_phi, 3u, - std::vector{-detray::constant::pi, - detray::constant::pi}}; - - detray::axis_payload a1{ - detray::n_axis::binning::e_regular, detray::n_axis::bounds::e_closed, - detray::n_axis::label::e_r, 2u, std::vector{0.f, 2.f}}; - - detray::grid_payload g; - g.type = detray::grid_payload::grid_type::polar2_grid; - g.index = 12u; - g.axes = {a0, a1}; - g.bins = bins; - - detray::grid_objects_payload go; - go.grid = g; - - detray::single_link_payload sl; - sl.link = 3u; - - detray::links_payload l; - l.grid_links = go; - l.single_links = {sl}; - - nlohmann::ordered_json j; - j["links"] = l; - - detray::links_payload pl = j["links"]; - - EXPECT_EQ(l.single_links.size(), pl.single_links.size()); - EXPECT_EQ(l.grid_links.value().grid.axes.size(), - pl.grid_links.value().grid.axes.size()); - EXPECT_EQ(l.grid_links.value().grid.bins.size(), - pl.grid_links.value().grid.bins.size()); -} - /// This tests the json io for a surface mask TEST(io, json_mask_payload) { @@ -272,7 +178,7 @@ TEST(io, json_mask_payload) { TEST(io, json_material_link_payload) { detray::material_link_payload m; - m.type = detray::material_link_payload::material_type::slab; + m.type = detray::material_link_payload::type_id::slab; m.index = 2u; nlohmann::ordered_json j; @@ -301,7 +207,7 @@ TEST(io, json_surface_payload) { m.boundaries = {10.f, 20.f, 34.f, 1.4f}; detray::material_link_payload mat; - mat.type = detray::material_link_payload::material_type::slab; + mat.type = detray::material_link_payload::type_id::slab; mat.index = 2u; s.transform = t; @@ -331,7 +237,7 @@ TEST(io, json_surface_payload) { TEST(io, acc_links_payload) { detray::acc_links_payload l; - l.type = detray::acc_links_payload::acc_type::cylinder2_grid; + l.type = detray::acc_links_payload::type_id::cylinder2_grid; l.index = 2u; nlohmann::ordered_json j; @@ -355,7 +261,7 @@ TEST(io, json_volume_payload) { sl.link = 1u; detray::acc_links_payload al; - al.type = detray::acc_links_payload::acc_type::cylinder2_grid; + al.type = detray::acc_links_payload::type_id::cylinder2_grid; al.index = 2u; detray::surface_payload s; @@ -366,7 +272,7 @@ TEST(io, json_volume_payload) { m.boundaries = {10.f, 20.f, 34.f, 1.4f}; detray::material_link_payload mat; - mat.type = detray::material_link_payload::material_type::slab; + mat.type = detray::material_link_payload::type_id::slab; mat.index = 2u; s.transform = t; @@ -401,8 +307,7 @@ TEST(io, json_volume_payload) { TEST(io, json_material_slab_payload) { detray::material_slab_payload m; - m.type = detray::material_slab_payload::material_type::slab; - m.index = 21u; + m.mat_link = {detray::material_slab_payload::type::slab, 21u}; m.thickness = 1.2f; m.mat.params = {1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f}; @@ -411,8 +316,8 @@ TEST(io, json_material_slab_payload) { detray::material_slab_payload pm = j["material"]; - EXPECT_EQ(m.type, pm.type); - EXPECT_EQ(m.index, pm.index); + EXPECT_EQ(m.mat_link.type, pm.mat_link.type); + EXPECT_EQ(m.mat_link.index, pm.mat_link.index); EXPECT_EQ(m.thickness, pm.thickness); EXPECT_EQ(m.mat.params, pm.mat.params); }