File indexing completed on 2026-08-26 08:38:40
0001
0002
0003
0004
0005
0006
0007
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
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 class TrackingGeometryJsonConverter {
0053 public:
0054
0055 struct Options {};
0056
0057
0058 template <typename object_t, const char* kContext>
0059 struct PointerToIdLookup;
0060
0061
0062
0063
0064
0065 template <typename object_t, typename pointer_t, const char* kContext>
0066 struct IdToPointerLikeLookup;
0067
0068
0069 static inline constexpr char kSurfaceLookupContext[] = "surface";
0070
0071 static inline constexpr char kPortalLookupContext[] = "portal";
0072
0073 static inline constexpr char kVolumeLookupContext[] = "volume";
0074
0075
0076 using SurfaceIdLookup = PointerToIdLookup<Surface, kSurfaceLookupContext>;
0077
0078 using SurfacePointerLookup =
0079 IdToPointerLikeLookup<RegularSurface, std::shared_ptr<RegularSurface>,
0080 kSurfaceLookupContext>;
0081
0082
0083 using PortalIdLookup = PointerToIdLookup<Portal, kPortalLookupContext>;
0084
0085 using PortalPointerLookup =
0086 IdToPointerLikeLookup<Portal, std::shared_ptr<Portal>,
0087 kPortalLookupContext>;
0088
0089
0090 using VolumeIdLookup =
0091 PointerToIdLookup<TrackingVolume, kVolumeLookupContext>;
0092
0093 using VolumePointerLookup =
0094 IdToPointerLikeLookup<TrackingVolume, TrackingVolume*,
0095 kVolumeLookupContext>;
0096
0097
0098 using PortalLinkEncoder =
0099 TypeDispatcher<PortalLinkBase,
0100 nlohmann::json(const GeometryContext&,
0101 const TrackingGeometryJsonConverter&,
0102 const SurfaceIdLookup&,
0103 const VolumeIdLookup&)>;
0104
0105 using PortalLinkDecoder = JsonKindDispatcher<
0106 std::unique_ptr<PortalLinkBase>, const TrackingGeometryJsonConverter&,
0107 const SurfacePointerLookup&, const VolumePointerLookup&>;
0108
0109
0110 using VolumeBoundsEncoder = TypeDispatcher<VolumeBounds, nlohmann::json()>;
0111
0112 using VolumeBoundsDecoder = JsonKindDispatcher<std::unique_ptr<VolumeBounds>>;
0113
0114
0115 using NavigationPolicyEncoder =
0116 TypeDispatcher<INavigationPolicy,
0117 nlohmann::json(
0118 const Acts::TrackingGeometryJsonConverter&)>;
0119
0120 using NavigationPolicyDecoder =
0121 JsonKindDispatcher<std::unique_ptr<INavigationPolicy>,
0122 const GeometryContext&,
0123 const TrackingGeometryJsonConverter&,
0124 const TrackingVolume&, const Acts::Logger&>;
0125
0126
0127 struct Config {
0128
0129 PortalLinkEncoder encodePortalLink{};
0130
0131 VolumeBoundsEncoder encodeVolumeBounds{};
0132
0133 NavigationPolicyEncoder encodeNavigationPolicy{};
0134
0135 PortalLinkDecoder decodePortalLink{"kind", "portal link"};
0136
0137 VolumeBoundsDecoder decodeVolumeBounds{"kind", "volume bounds"};
0138
0139 NavigationPolicyDecoder decodeNavigationPolicy{"kind", "navigation policy"};
0140
0141
0142
0143
0144 static Config defaultConfig();
0145 };
0146
0147
0148
0149
0150
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
0157
0158
0159
0160
0161
0162
0163 nlohmann::json toJson(const GeometryContext& gctx,
0164 const TrackingGeometry& geometry,
0165 const Options& options = Options{}) const;
0166
0167
0168
0169
0170
0171
0172
0173
0174 std::shared_ptr<TrackingGeometry> fromJson(
0175 const GeometryContext& gctx, const nlohmann::json& encoded,
0176 const Options& options = Options{}) const;
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 nlohmann::json trackingVolumeToJson(const GeometryContext& gctx,
0189 const TrackingVolume& world,
0190 const Options& options = Options{}) const;
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 std::shared_ptr<TrackingVolume> trackingVolumeFromJson(
0203 const GeometryContext& gctx, const nlohmann::json& encoded,
0204 const Options& options = Options{}) const;
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214 nlohmann::json portalLinkToJson(const GeometryContext& gctx,
0215 const PortalLinkBase& link,
0216 const SurfaceIdLookup& surfaceIds,
0217 const VolumeIdLookup& volumeIds) const;
0218
0219
0220
0221
0222
0223
0224
0225
0226 std::unique_ptr<PortalLinkBase> portalLinkFromJson(
0227 const nlohmann::json& encoded, const SurfacePointerLookup& surfaces,
0228 const VolumePointerLookup& volumes) const;
0229
0230
0231
0232
0233
0234
0235 nlohmann::json navigationPolicyToJson(const INavigationPolicy& policy) const;
0236
0237
0238
0239
0240
0241
0242
0243
0244
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 }