Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:17

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 #include "Acts/Plugins/Json/DetectorVolumeJsonConverter.hpp"
0010 
0011 #include "Acts/Detector/DetectorVolume.hpp"
0012 #include "Acts/Detector/Portal.hpp"
0013 #include "Acts/Detector/PortalGenerators.hpp"
0014 #include "Acts/Navigation/DetectorVolumeFinders.hpp"
0015 #include "Acts/Navigation/InternalNavigation.hpp"
0016 #include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
0017 #include "Acts/Plugins/Json/DetrayJsonHelper.hpp"
0018 #include "Acts/Plugins/Json/IndexedSurfacesJsonConverter.hpp"
0019 #include "Acts/Plugins/Json/PortalJsonConverter.hpp"
0020 #include "Acts/Plugins/Json/SurfaceJsonConverter.hpp"
0021 #include "Acts/Plugins/Json/VolumeBoundsJsonConverter.hpp"
0022 #include "Acts/Utilities/Enumerate.hpp"
0023 
0024 #include <algorithm>
0025 #include <ctime>
0026 
0027 nlohmann::json Acts::DetectorVolumeJsonConverter::toJson(
0028     const GeometryContext& gctx, const Experimental::DetectorVolume& volume,
0029     const std::vector<const Experimental::DetectorVolume*>& detectorVolumes,
0030     const std::vector<const Experimental::Portal*>& portals,
0031     const Options& options) {
0032   nlohmann::json jVolume;
0033   jVolume["name"] = volume.name();
0034   jVolume["geometryId"] = volume.geometryId().volume();
0035   jVolume["transform"] = Transform3JsonConverter::toJson(
0036       volume.transform(gctx), options.transformOptions);
0037   jVolume["bounds"] = VolumeBoundsJsonConverter::toJson(volume.volumeBounds());
0038   // Write the surfaces
0039   nlohmann::json jSurfaces;
0040   std::for_each(
0041       volume.surfaces().begin(), volume.surfaces().end(), [&](const auto& s) {
0042         jSurfaces.push_back(
0043             SurfaceJsonConverter::toJson(gctx, *s, options.surfaceOptions));
0044       });
0045   jVolume["surfaces"] = jSurfaces;
0046   // And its surface navigation delegates
0047   nlohmann::json jSurfacesDelegate =
0048       IndexedSurfacesJsonConverter::toJson(volume.internalNavigation());
0049   jVolume["surface_navigation"] = jSurfacesDelegate;
0050 
0051   // Write the sub volumes
0052   nlohmann::json jVolumes;
0053   std::for_each(
0054       volume.volumes().begin(), volume.volumes().end(), [&](const auto& v) {
0055         jVolumes.push_back(toJson(gctx, *v, detectorVolumes, portals, options));
0056       });
0057   jVolume["volumes"] = jVolumes;
0058 
0059   // Write the portals if pre-converted as link
0060   nlohmann::json jPortals;
0061   if (!portals.empty()) {
0062     for (const auto* p : volume.portals()) {
0063       auto it = std::ranges::find(portals, p);
0064       if (it != portals.end()) {
0065         jPortals.push_back(std::distance(portals.begin(), it));
0066       } else {
0067         throw std::runtime_error("Portal not found in the list of portals");
0068       }
0069     }
0070     jVolume["portal_links"] = jPortals;
0071   } else {
0072     for (const auto& p : volume.portals()) {
0073       nlohmann::json jPortal = PortalJsonConverter::toJson(
0074           gctx, *p, detectorVolumes, options.portalOptions);
0075       jPortals.push_back(jPortal);
0076     }
0077     jVolume["portals"] = jPortals;
0078   }
0079   return jVolume;
0080 }
0081 
0082 std::shared_ptr<Acts::Experimental::DetectorVolume>
0083 Acts::DetectorVolumeJsonConverter::fromJson(const GeometryContext& gctx,
0084                                             const nlohmann::json& jVolume) {
0085   std::string name = jVolume["name"];
0086   GeometryIdentifier geoId;
0087   geoId.setVolume(jVolume["geometryId"]);
0088   Transform3 transform =
0089       Transform3JsonConverter::fromJson(jVolume["transform"]);
0090   auto bounds = VolumeBoundsJsonConverter::fromJson(jVolume["bounds"]);
0091 
0092   auto jSurfaces = jVolume["surfaces"];
0093   auto jVolumes = jVolume["volumes"];
0094 
0095   // Some tooling
0096   auto portalGenerator = Experimental::defaultPortalGenerator();
0097 
0098   if (jSurfaces.empty() && jVolumes.empty()) {
0099     auto volume = Experimental::DetectorVolumeFactory::construct(
0100         portalGenerator, gctx, name, transform, std::move(bounds),
0101         Experimental::tryAllPortals());
0102     volume->assignGeometryId(geoId);
0103     return volume;
0104   }
0105   // Convert the surfaces
0106   std::vector<std::shared_ptr<Surface>> surfaces;
0107   for (const auto& js : jSurfaces) {
0108     surfaces.push_back(SurfaceJsonConverter::fromJson(js));
0109   }
0110   // Convert the volumes
0111   std::vector<std::shared_ptr<Experimental::DetectorVolume>> volumes;
0112   for (const auto& jv : jVolumes) {
0113     volumes.push_back(DetectorVolumeJsonConverter::fromJson(gctx, jv));
0114   }
0115 
0116   auto jSurfaceNavigation = jVolume["surface_navigation"];
0117 
0118   auto volume = Experimental::DetectorVolumeFactory::construct(
0119       portalGenerator, gctx, name, transform, std::move(bounds), surfaces,
0120       volumes, Experimental::tryRootVolumes(),
0121       IndexedSurfacesJsonConverter::fromJson(jSurfaceNavigation));
0122   volume->assignGeometryId(geoId);
0123   return volume;
0124 }