Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:42

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021-2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Plugins/Json/ActsJson.hpp"
0014 #include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
0015 #include "Acts/Plugins/Json/SurfaceBoundsJsonConverter.hpp"
0016 #include "Acts/Surfaces/Surface.hpp"
0017 
0018 #include <memory>
0019 #include <string>
0020 #include <tuple>
0021 #include <utility>
0022 #include <vector>
0023 
0024 #include <nlohmann/json.hpp>
0025 
0026 // Custom Json encoder/decoders. Naming is mandated by nlohmann::json and thus
0027 // can not match our naming guidelines.
0028 namespace Acts {
0029 class ISurfaceMaterial;
0030 
0031 using SurfaceAndMaterialWithContext =
0032     std::tuple<std::shared_ptr<const Acts::Surface>,
0033                std::shared_ptr<const Acts::ISurfaceMaterial>,
0034                Acts::GeometryContext>;
0035 
0036 /// Conversion of a pair of surface and material used for the material mapping
0037 void to_json(nlohmann::json& j, const SurfaceAndMaterialWithContext& surface);
0038 
0039 /// Non-contextual conversion of a surface
0040 ///
0041 /// @note it will take the default context
0042 void to_json(nlohmann::json& j, const Surface& surface);
0043 
0044 /// Non-contextual conversion of a surface
0045 ///
0046 /// @note it will take the default context
0047 void to_json(nlohmann::json& j, const std::shared_ptr<const Surface>& surface);
0048 
0049 /// Contextual conversion of a surface
0050 ///
0051 /// @param j the json to be filled
0052 /// @param surface the surface to be converted
0053 /// @param gctx the geometry context for this
0054 void toJson(nlohmann::json& j, const std::shared_ptr<const Surface>& surface,
0055             const Acts::GeometryContext& gctx);
0056 
0057 /// Conversion to Surface from jsonn
0058 ///
0059 /// @param j the read-in json object
0060 ///
0061 /// @return a shared_ptr to a surface object for type polymorphism
0062 std::shared_ptr<Surface> surfaceFromJson(const nlohmann::json& j);
0063 
0064 /// Conversion to Surface from json in correct type
0065 ///
0066 /// The type is given as a template argument in order to be able
0067 /// to construct the correct fitting types for surfaces.
0068 ///
0069 /// @param j the read-in json object
0070 ///
0071 /// @return a shared_ptr to a typed surface object for type polymorphism
0072 template <typename surface_t, typename bounds_t>
0073 std::shared_ptr<surface_t> surfaceFromJsonT(const nlohmann::json& j) {
0074   nlohmann::json jTransform = j["transform"];
0075   Transform3 sTransform = Transform3JsonConverter::fromJson(jTransform);
0076   if constexpr (std::is_same_v<bounds_t, void>) {
0077     return Surface::makeShared<surface_t>(sTransform);
0078   } else {
0079     nlohmann::json jBounds = j["bounds"];
0080     auto sBounds = SurfaceBoundsJsonConverter::fromJson<bounds_t>(jBounds);
0081     return Surface::makeShared<surface_t>(sTransform, std::move(sBounds));
0082   }
0083 }
0084 
0085 namespace SurfaceJsonConverter {
0086 
0087 struct Options {
0088   // The way the transforms are written out
0089   Transform3JsonConverter::Options transformOptions =
0090       Transform3JsonConverter::Options{};
0091   // Write the material
0092   bool writeMaterial = true;
0093   // Write surface as a portal
0094   bool portal = false;
0095 };
0096 
0097 /// Contextual conversion of a surface
0098 ///
0099 /// @param gctx the geometry context for this
0100 /// @param surface the surface to be converted
0101 /// @param options the writing options for the surfaces
0102 ///
0103 /// @return a json object representing the surface
0104 nlohmann::json toJson(const GeometryContext& gctx, const Surface& surface,
0105                       const Options& options = Options{});
0106 
0107 /// Contextual conversion of a surface - Detray export
0108 ///
0109 /// @param gctx the geometry context for this
0110 /// @param surface the surface to be converted
0111 /// @param options the writing options for the surfaces
0112 ///
0113 /// @note reading back detray json is not supported and will fail
0114 ///
0115 /// @return a json object representing the surface
0116 nlohmann::json toJsonDetray(const GeometryContext& gctx, const Surface& surface,
0117                             const Options& options = Options{});
0118 
0119 /// @brief The Surface converter from json
0120 ///
0121 /// @param jSurface the surface json object
0122 ///
0123 /// @return a shared object created from json input
0124 std::shared_ptr<Surface> fromJson(const nlohmann::json& jSurface);
0125 
0126 }  // namespace SurfaceJsonConverter
0127 
0128 // This macro create a conversion for the surface type
0129 NLOHMANN_JSON_SERIALIZE_ENUM(
0130     Surface::SurfaceType,
0131     {{Surface::SurfaceType::Cone, "ConeSurface"},
0132      {Surface::SurfaceType::Cylinder, "CylinderSurface"},
0133      {Surface::SurfaceType::Disc, "DiscSurface"},
0134      {Surface::SurfaceType::Perigee, "PerigeeSurface"},
0135      {Surface::SurfaceType::Plane, "PlaneSurface"},
0136      {Surface::SurfaceType::Straw, "StrawSurface"},
0137      {Surface::SurfaceType::Curvilinear, "CurvilinearSurface"},
0138      {Surface::SurfaceType::Other, "Other"}})
0139 
0140 }  // namespace Acts