Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-26 08:38:40

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Geometry/GeometryContext.hpp"
0012 #include "Acts/Navigation/INavigationPolicy.hpp"
0013 #include "Acts/Navigation/SurfaceArrayNavigationPolicy.hpp"
0014 #include "Acts/Surfaces/RegularSurface.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "Acts/Utilities/TypeDispatcher.hpp"
0017 #include "ActsPlugins/Json/JsonKindDispatcher.hpp"
0018 
0019 #include <memory>
0020 #include <string>
0021 
0022 #include <nlohmann/json.hpp>
0023 
0024 namespace Acts {
0025 
0026 class PortalLinkBase;
0027 class Portal;
0028 class Surface;
0029 class TrackingGeometry;
0030 class TrackingVolume;
0031 class VolumeBounds;
0032 
0033 /// @addtogroup json_plugin
0034 /// @{
0035 
0036 /// @brief Converter for tracking geometry JSON payloads
0037 ///
0038 /// High-level conversion overview:
0039 /// - Serialization:
0040 ///   - traverse the `TrackingVolume::volumes()` tree in depth-first order
0041 ///   - collect unique instances of surfaces, portals, volumes and assign stable
0042 ///   in-file IDs
0043 ///   - serialize the instances into their independent top-level tables
0044 ///   - encode object-to-object relationships through the assigned IDs
0045 /// - Deserialization:
0046 ///   - validate schema header and collect all volume records
0047 ///   - instantiate volumes, portals, surfaces and build ID->pointer lookup
0048 ///   - attach child volumes to reconstruct the tree
0049 ///   - attach surfaces to portals and volumes via ID lookup
0050 ///   - portals to volumes via ID lookup
0051 ///   - return deserialized geometry
0052 class TrackingGeometryJsonConverter {
0053  public:
0054   /// JSON serialization options for tracking geometry conversion.
0055   struct Options {};
0056 
0057   /// Generic lookup from object pointer identity to serialized object ID.
0058   template <typename object_t, const char* kContext>
0059   struct PointerToIdLookup;
0060 
0061   /// Generic lookup from serialized ID to pointer-like object holder.
0062   ///
0063   /// `pointer_t` can be a raw pointer (`object_t*`) or an owning pointer-like
0064   /// type such as `std::shared_ptr<object_t>`.
0065   template <typename object_t, typename pointer_t, const char* kContext>
0066   struct IdToPointerLikeLookup;
0067 
0068   /// Exception context for surfaces
0069   static inline constexpr char kSurfaceLookupContext[] = "surface";
0070   /// Exception context for portals
0071   static inline constexpr char kPortalLookupContext[] = "portal";
0072   /// Exception context for volumes
0073   static inline constexpr char kVolumeLookupContext[] = "volume";
0074 
0075   /// Surface map to its JSON ID
0076   using SurfaceIdLookup = PointerToIdLookup<Surface, kSurfaceLookupContext>;
0077   /// JSON ID map to its surface
0078   using SurfacePointerLookup =
0079       IdToPointerLikeLookup<RegularSurface, std::shared_ptr<RegularSurface>,
0080                             kSurfaceLookupContext>;
0081 
0082   /// Portal map to its JSON ID
0083   using PortalIdLookup = PointerToIdLookup<Portal, kPortalLookupContext>;
0084   /// JSON ID map to its portal
0085   using PortalPointerLookup =
0086       IdToPointerLikeLookup<Portal, std::shared_ptr<Portal>,
0087                             kPortalLookupContext>;
0088 
0089   /// Tracking volume map to its JSON ID
0090   using VolumeIdLookup =
0091       PointerToIdLookup<TrackingVolume, kVolumeLookupContext>;
0092   /// JSON ID map to its tracking volume
0093   using VolumePointerLookup =
0094       IdToPointerLikeLookup<TrackingVolume, TrackingVolume*,
0095                             kVolumeLookupContext>;
0096 
0097   /// Portal link encoder
0098   using PortalLinkEncoder =
0099       TypeDispatcher<PortalLinkBase,
0100                      nlohmann::json(const GeometryContext&,
0101                                     const TrackingGeometryJsonConverter&,
0102                                     const SurfaceIdLookup&,
0103                                     const VolumeIdLookup&)>;
0104   /// Portal link decoder
0105   using PortalLinkDecoder = JsonKindDispatcher<
0106       std::unique_ptr<PortalLinkBase>, const TrackingGeometryJsonConverter&,
0107       const SurfacePointerLookup&, const VolumePointerLookup&>;
0108 
0109   /// Volume bounds encoder
0110   using VolumeBoundsEncoder = TypeDispatcher<VolumeBounds, nlohmann::json()>;
0111   /// Volume bounds decoder
0112   using VolumeBoundsDecoder = JsonKindDispatcher<std::unique_ptr<VolumeBounds>>;
0113 
0114   /// Navigation policy encoder
0115   using NavigationPolicyEncoder =
0116       TypeDispatcher<INavigationPolicy,
0117                      nlohmann::json(
0118                          const Acts::TrackingGeometryJsonConverter&)>;
0119   /// Navigation policy decoder
0120   using NavigationPolicyDecoder =
0121       JsonKindDispatcher<std::unique_ptr<INavigationPolicy>,
0122                          const GeometryContext&,
0123                          const TrackingGeometryJsonConverter&,
0124                          const TrackingVolume&, const Acts::Logger&>;
0125 
0126   /// Configuration for the tracking geometry JSON converter.
0127   struct Config {
0128     /// Dispatcher for portal link serialization.
0129     PortalLinkEncoder encodePortalLink{};
0130     /// Dispatcher for volume bounds serialization.
0131     VolumeBoundsEncoder encodeVolumeBounds{};
0132     /// Dispatcher for navigation policy serialization.
0133     NavigationPolicyEncoder encodeNavigationPolicy{};
0134     /// Decoder dispatcher for portal links by kind tag.
0135     PortalLinkDecoder decodePortalLink{"kind", "portal link"};
0136     /// Decoder dispatcher for volume bounds by kind tag.
0137     VolumeBoundsDecoder decodeVolumeBounds{"kind", "volume bounds"};
0138     /// Decoder dispatcher for portal links by kind tag.
0139     NavigationPolicyDecoder decodeNavigationPolicy{"kind", "navigation policy"};
0140 
0141     /// Construct default config with all supported converters registered.
0142     ///
0143     /// @return configuration instance
0144     static Config defaultConfig();
0145   };
0146 
0147   /// @brief Construct converter with custom or default dispatch configuration.
0148   ///
0149   /// @param config The conversion dispatch configuration
0150   /// @param logger The logger instance
0151   explicit TrackingGeometryJsonConverter(
0152       Config config = Config::defaultConfig(),
0153       std::unique_ptr<const Acts::Logger> logger = Acts::getDefaultLogger(
0154           "TrackingGeometryJsonConverter", Acts::Logging::INFO));
0155 
0156   /// @brief Convert a tracking geometry to JSON.
0157   ///
0158   /// @param gctx geometry context
0159   /// @param geometry tracking geometry to convert
0160   /// @param options options for the conversion
0161   ///
0162   /// @return serialized tracking geometry
0163   nlohmann::json toJson(const GeometryContext& gctx,
0164                         const TrackingGeometry& geometry,
0165                         const Options& options = Options{}) const;
0166 
0167   /// @brief Reconstruct a tracking geometry from JSON.
0168   ///
0169   /// @param gctx geometry context
0170   /// @param encoded serialized tracking geometry
0171   /// @param options options for the conversion
0172   ///
0173   /// @return pointer to deserialized geometry
0174   std::shared_ptr<TrackingGeometry> fromJson(
0175       const GeometryContext& gctx, const nlohmann::json& encoded,
0176       const Options& options = Options{}) const;
0177 
0178   /// @brief Convert a tracking volume hierarchy to JSON.
0179   ///
0180   /// @param gctx geometry context
0181   /// @param world top tracking volume in the hierarchy
0182   /// @param options options for the conversion
0183   ///
0184   /// @return serialized tracking volume hierarchy
0185   ///
0186   /// @note the geometry context is applied to the transformations
0187   /// during the serialization
0188   nlohmann::json trackingVolumeToJson(const GeometryContext& gctx,
0189                                       const TrackingVolume& world,
0190                                       const Options& options = Options{}) const;
0191 
0192   /// @brief Reconstruct a tracking volume hierarchy from JSON.
0193   ///
0194   /// @param gctx geometry context
0195   /// @param encoded serialized tracking volume hierarchy
0196   /// @param options options for the conversion
0197   ///
0198   /// @return pointer to deserialized tracking volume hierarchy
0199   ///
0200   /// @note currently the geometry context is only propagated to the
0201   /// Portal construction and the NavigationPolicy assignment
0202   std::shared_ptr<TrackingVolume> trackingVolumeFromJson(
0203       const GeometryContext& gctx, const nlohmann::json& encoded,
0204       const Options& options = Options{}) const;
0205 
0206   /// @brief Serialize one portal link using the configured dispatcher.
0207   ///
0208   /// @param gctx geometry context
0209   /// @param link portal link to serialize
0210   /// @param surfaceIds surface-to-id map for internal lookup
0211   /// @param volumeIds volume-to-id map for internal lookup
0212   ///
0213   /// @return serialized portal link
0214   nlohmann::json portalLinkToJson(const GeometryContext& gctx,
0215                                   const PortalLinkBase& link,
0216                                   const SurfaceIdLookup& surfaceIds,
0217                                   const VolumeIdLookup& volumeIds) const;
0218 
0219   /// @brief Deserialize one portal link using configured decoders.
0220   ///
0221   /// @param encoded serialized portal link
0222   /// @param surfaces id-to-surface map for internal lookup
0223   /// @param volumes id-to-volume map for internal lookup
0224   ///
0225   /// @return pointer to deserialized portal link
0226   std::unique_ptr<PortalLinkBase> portalLinkFromJson(
0227       const nlohmann::json& encoded, const SurfacePointerLookup& surfaces,
0228       const VolumePointerLookup& volumes) const;
0229 
0230   /// @brief Serialize navigation policy using the configured dispatcher.
0231   ///
0232   /// @param policy navigation policy to serialize
0233   ///
0234   /// @return serialized navigation policy
0235   nlohmann::json navigationPolicyToJson(const INavigationPolicy& policy) const;
0236 
0237   /// @brief Deserialize navigation policy using configured decoders.
0238   ///
0239   /// @param gctx geometry context
0240   /// @param encoded serialized navigation policy
0241   /// @param volume tracking volume to assign navigation policy to
0242   /// @param logger logging instance
0243   ///
0244   /// @return pointer to deserialized navigation policy
0245   std::unique_ptr<Acts::INavigationPolicy> navigationPolicyFromJson(
0246       const Acts::GeometryContext& gctx, const nlohmann::json& encoded,
0247       const Acts::TrackingVolume& volume, const Acts::Logger& logger) const;
0248 
0249  private:
0250   const Acts::Logger& logger() const { return *m_logger; }
0251 
0252   Config m_cfg;
0253   std::unique_ptr<const Acts::Logger> m_logger;
0254 };
0255 
0256 NLOHMANN_JSON_SERIALIZE_ENUM(
0257     SurfaceArrayNavigationPolicy::LayerType,
0258     {{SurfaceArrayNavigationPolicy::LayerType::Cylinder, "Cylinder"},
0259      {SurfaceArrayNavigationPolicy::LayerType::Disc, "Disc"},
0260      {SurfaceArrayNavigationPolicy::LayerType::Plane, "Plane"}})
0261 
0262 /// @}
0263 }  // namespace Acts