Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PortalSvgConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PortalSvgConverter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include "Acts/Detector/Portal.hpp"
13 
14 namespace {
15 
24 Acts::Svg::ProtoLink makeProtoLink(
25  const Acts::Svg::PortalConverter::Options& portalOptions,
26  const Acts::Vector3& position, const Acts::Vector3& direction,
27  const Acts::Experimental::DetectorVolume* dVolume) {
29  Acts::Vector3 end3 = position + portalOptions.linkLength * direction;
30  pLink._start = {position.x(), position.y(), position.z()};
31  pLink._end = {end3.x(), end3.y(), end3.z()};
32  auto linkIndexCandidate = portalOptions.volumeIndices.find(dVolume);
33  if (linkIndexCandidate != portalOptions.volumeIndices.end()) {
34  pLink._link_index = linkIndexCandidate->second;
35  }
36  return pLink;
37 }
38 
49 std::vector<Acts::Svg::ProtoLink> convertMultiLink(
52  const Acts::Surface& surface, const Acts::Vector3& refPosition,
53  const Acts::Svg::PortalConverter::Options& portalOptions,
54  int sign) noexcept(false) {
55  // The return links
56  std::vector<Acts::Svg::ProtoLink> pLinks;
57  const auto& volumes = multiLink.indexedUpdator.extractor.dVolumes;
58  const auto& casts = multiLink.indexedUpdator.casts;
59 
60  // Generate the proto-links of the multi-link
61  for (auto [il, v] : Acts::enumerate(volumes)) {
62  Acts::Vector3 position = refPosition;
63  if constexpr (decltype(multiLink.indexedUpdator)::grid_type::DIM == 1u) {
64  // Get the binning value
65  Acts::BinningValue bValue = casts[0u];
66  // Get the boundaries
67  const auto& boundaries =
68  multiLink.indexedUpdator.grid.axes()[0u]->getBinEdges();
69 
70  Acts::ActsScalar refC = 0.5 * (boundaries[il + 1u] + boundaries[il]);
71 
72  if (bValue == Acts::binR) {
74  position = Acts::Vector3(refC * std::cos(phi), refC * std::sin(phi),
75  refPosition.z());
76  } else if (bValue == Acts::binZ) {
77  position[2] = refC;
78  } else if (bValue == Acts::binPhi) {
80  position = Acts::Vector3(r * std::cos(refC), r * std::sin(refC),
81  refPosition.z());
82  } else {
83  throw std::invalid_argument("convertMultiLink: incorrect binning.");
84  }
85  Acts::Vector3 direction = surface.normal(gctx, position);
86  pLinks.push_back(makeProtoLink(portalOptions, position,
87  Acts::Vector3(sign * direction), v));
88  }
89  }
90  return pLinks;
91 }
92 
93 } // namespace
94 
96  const GeometryContext& gctx, const Experimental::Portal& portal,
97  const PortalConverter::Options& portalOptions) {
98  ProtoPortal pPortal;
99  // First convert the surface
100  pPortal._surface = SurfaceConverter::convert(gctx, portal.surface(),
101  portalOptions.surfaceOptions);
102 
103  // Reference point and direction
104  Vector3 rPos(0., 0., 0);
105  Vector3 rDir(0., 0., 1);
106  const auto& surface = portal.surface();
107  const auto surfaceTransform = portal.surface().transform(gctx);
108  const auto surfaceTranslation = surfaceTransform.translation().eval();
109  const auto surfaceType = surface.bounds().type();
110  const auto& boundValues = surface.bounds().values();
111  switch (surfaceType) {
112  case SurfaceBounds::eCylinder: {
113  // Get phi
114  ActsScalar r = boundValues[0u];
115  ActsScalar aphi = boundValues[3u];
116  rPos = Vector3(r * std::cos(aphi), r * std::sin(aphi),
117  surfaceTranslation.z());
118  } break;
119  case SurfaceBounds::eDisc: {
120  // Get phi
121  ActsScalar r = 0.5 * (boundValues[0u] + boundValues[1u]);
122  ActsScalar aphi = boundValues[3u];
123  rPos = Vector3(r * std::cos(aphi), r * std::sin(aphi),
124  surfaceTranslation.z());
125  } break;
126  default: {
127  rPos = surfaceTranslation;
128  } break;
129  }
130  rDir = surface.normal(gctx, rPos);
131 
132  // Now convert the link objects
133  const auto& updators = portal.detectorVolumeUpdators();
134 
135  int sign = -1;
136  for (const auto& dvu : updators) {
137  // Get the instance and start the casting
138  const auto* instance = dvu.instance();
139  auto singleLink =
140  dynamic_cast<const Experimental::SingleDetectorVolumeImpl*>(instance);
141  if (singleLink != nullptr) {
142  pPortal._volume_links.push_back(makeProtoLink(
143  portalOptions, rPos, Vector3(sign * rDir), singleLink->dVolume));
144  }
145  auto multiLink =
146  dynamic_cast<const Experimental::BoundVolumesGrid1Impl*>(instance);
147  if (multiLink != nullptr) {
148  auto pLinks = convertMultiLink(gctx, *multiLink, surface, rPos,
149  portalOptions, sign);
150 
151  pPortal._volume_links.insert(pPortal._volume_links.end(), pLinks.begin(),
152  pLinks.end());
153  }
154  // Switch to the other side
155  sign += 2;
156  }
157  // Return the proto Portal
158  return pPortal;
159 }