ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
geometry_writer.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/core/concepts.hpp"
13#include "detray/definitions/indexing.hpp"
14#include "detray/geometry/mask.hpp"
15#include "detray/geometry/surface.hpp"
16#include "detray/io/backend/detail/basic_converter.hpp"
17#include "detray/io/backend/detail/type_info.hpp"
18#include "detray/io/frontend/payloads.hpp"
19#include "detray/utils/grid/concepts.hpp"
20
21// System include(s)
22#include <algorithm>
23#include <limits>
24#include <string>
25#include <string_view>
26#include <vector>
27
28namespace detray::io {
29
34 public:
36 static constexpr std::string_view tag = "geometry";
37
40
46 template <detray::concepts::detector detector_t>
47 static geo_header_payload header_to_payload(const detector_t& det,
48 const std::string_view det_name) {
49 geo_header_payload header_data;
50
51 header_data.common = detail::basic_converter::to_payload(det_name, tag);
52
53 header_data.sub_header.emplace();
54 auto& geo_sub_header = header_data.sub_header.value();
55 geo_sub_header.n_volumes = det.volumes().size();
56 geo_sub_header.n_surfaces = det.surfaces().size();
57
58 return header_data;
59 }
60
66 template <detray::concepts::detector detector_t>
67 static payload_type to_payload(const detector_t& det,
68 const typename detector_t::name_map& names) {
69 payload_type det_data;
70 det_data.volumes.reserve(det.volumes().size());
71
72 for (const auto& vol : det.volumes()) {
73 if (names.contains(vol.index())) {
74 det_data.volumes.push_back(to_payload(vol, det, names.at(vol.index())));
75 } else {
76 det_data.volumes.push_back(to_payload(vol, det, ""));
77 }
78 }
79
80 return det_data;
81 }
82
87 template <detray::concepts::detector detector_t>
89 const typename detector_t::transform3_type& trf) {
90 transform_payload trf_data;
91
92 const auto& t = trf.translation();
93 const auto& x = trf.x();
94 const auto& y = trf.y();
95 const auto& z = trf.z();
96
97 trf_data.tr = {t[0], t[1], t[2]};
98 trf_data.rot = {x[0], x[1], x[2], y[0], y[1], y[2], z[0], z[1], z[2]};
99
100 return trf_data;
101 }
102
107 template <typename mask_t>
108 requires(!std::is_same_v<typename mask_t::shape, void>)
109 static mask_payload to_payload(const mask_t& m) {
110 mask_payload mask_data;
111
112 mask_data.shape = io::detail::get_id<typename mask_t::shape>();
113
114 mask_data.volume_link =
115 detail::basic_converter::to_payload(m.volume_link());
116
117 mask_data.boundaries.resize(mask_t::boundaries::e_size);
118 std::ranges::copy(m.values(), std::begin(mask_data.boundaries));
119
120 return mask_data;
121 }
122
128 template <detray::concepts::detector detector_t>
130 std::size_t sf_idx) {
131 using algebra_t = typename detector_t::algebra_type;
132 surface_payload sf_data;
133
134 sf_data.index_in_coll = sf_idx;
135 sf_data.type = sf.id();
136 sf_data.identifier = sf.identifier().value();
138 sf_data.masks = sf.template visit_mask<get_mask_link_payload>();
139 if (sf.has_material()) {
140 sf_data.material =
141 sf.template visit_material<get_material_link_payload<algebra_t>>();
142 }
143 sf_data.source = sf.source();
144
145 return sf_data;
146 }
147
154 template <detray::concepts::detector detector_t>
156 const typename detector_t::volume_type& vol_desc, const detector_t& det,
157 const std::string_view name) {
158 volume_payload vol_data;
159
160 vol_data.index = detail::basic_converter::to_payload(vol_desc.index());
161 vol_data.name = name;
162 vol_data.transform =
163 to_payload<detector_t>(det.transform_store().at(vol_desc.transform()));
164 vol_data.type = vol_desc.id();
165
166 // Count the surfaces belonging to this volume
167 std::size_t sf_idx{0};
168
169 for (const auto& sf_desc : det.surfaces()) {
170 if (sf_desc.volume() == vol_desc.index()) {
171 vol_data.surfaces.push_back(
172 to_payload(geometry::surface{det, sf_desc}, sf_idx++));
173 }
174 }
175
176 // Only run the query, if object type is contained in volume
177 const auto& link = vol_desc.accel_link();
178 // Initialize the std::optional
179 if (link.size() > 1u) {
180 vol_data.acc_links.emplace();
181 }
182 // Skip the first acceleration structure which exists in every volume
183 // and is handled automatically during detector building
184 for (unsigned int i = 1u; i < link.size(); ++i) {
185 const auto& l = link[i];
186 if (!l.is_invalid()) {
187 const auto aclp =
188 det.accelerator_store().template visit<get_acc_link_payload>(l);
189 vol_data.acc_links->push_back(aclp);
190 }
191 }
192
193 return vol_data;
194 }
195
196 private:
198 struct get_mask_link_payload {
199 template <typename mask_group_t, typename idx_range_t>
200 inline auto operator()(const mask_group_t& mask_group,
201 const idx_range_t& idx_range) const {
202 std::vector<mask_payload> payloads{};
203
204 for (const auto& mask : detray::ranges::subrange(mask_group, idx_range)) {
205 payloads.push_back(geometry_writer::to_payload(mask));
206 }
207
208 return payloads;
209 }
210 };
211
213 template <detray::concepts::algebra algebra_t>
214 struct get_material_link_payload {
215 template <typename material_group_t, typename index_t>
216 constexpr auto operator()(const material_group_t& /*unused*/,
217 const index_t& index) const {
218 using material_t = typename material_group_t::value_type;
219
220 // Find the correct material type index
221 if constexpr (detray::concepts::grid<material_t>) {
222 return detail::basic_converter::to_payload(
223 io::detail::get_id<material_t>(), index);
224 } else {
225 return detail::basic_converter::to_payload(
226 io::detail::get_id<algebra_t, material_t>(), index);
227 }
228 }
229 };
230
232 struct get_acc_link_payload {
233 template <typename acc_group_t, typename index_t>
234 constexpr auto operator()(const acc_group_t& /*unused*/,
235 const index_t& index) const {
236 using accel_t = typename acc_group_t::value_type;
237
238 auto id{acc_links_payload::type_id::unknown};
239
240 // Only convert grids
241 if constexpr (detray::concepts::surface_grid<accel_t>) {
242 id = io::detail::get_id<accel_t>();
243 }
244
245 return detail::basic_converter::to_payload(id, index);
246 }
247 };
248};
249
250} // namespace detray::io
Facade for a detray detector surface.
Definition surface.hpp:36
constexpr auto source() const
Definition surface.hpp:146
constexpr auto transform(const context &ctx) const -> const transform3_type &
Definition surface.hpp:197
constexpr auto id() const -> surface_id
Definition surface.hpp:131
constexpr auto identifier() const -> geometry::identifier
Definition surface.hpp:110
constexpr bool has_material() const
Definition surface.hpp:170
Tracking geometry writer backend.
Definition geometry_writer.hpp:33
static surface_payload to_payload(const geometry::surface< detector_t > &sf, std::size_t sf_idx)
Convert a detector surface sf into its io payload.
Definition geometry_writer.hpp:129
static volume_payload to_payload(const typename detector_t::volume_type &vol_desc, const detector_t &det, const std::string_view name)
Convert a detector volume and its contents to its IO representation.
Definition geometry_writer.hpp:155
static payload_type to_payload(const detector_t &det, const typename detector_t::name_map &names)
Convert a detector det into its io payload.
Definition geometry_writer.hpp:67
static mask_payload to_payload(const mask_t &m)
Convert a surface mask m into its io payload.
Definition geometry_writer.hpp:109
static geo_header_payload header_to_payload(const detector_t &det, const std::string_view det_name)
Convert the header information into its payload.
Definition geometry_writer.hpp:47
detector_payload payload_type
Payload type that the reader processes.
Definition geometry_writer.hpp:39
static transform_payload to_payload(const typename detector_t::transform3_type &trf)
Convert a surface transform trf into its io payload.
Definition geometry_writer.hpp:88
static constexpr std::string_view tag
Tag the writer as "geometry".
Definition geometry_writer.hpp:36
Mask a region on a surface and link it to a volume.
Definition mask.hpp:46
The following enums are global for all detectors.
Definition concepts.hpp:18
header_payload< geo_sub_header_payload > geo_header_payload
a payload for the geometry file header
Definition payloads.hpp:91
A payload for a detector geometry.
Definition payloads.hpp:355
std::vector< volume_payload > volumes
Material payloads grouped by detector volume.
Definition payloads.hpp:357
std::optional< sub_header_payload_t > sub_header
Information that is specific to a tag, i.e. type of detector payload.
Definition payloads.hpp:58
common_header_payload common
Shared version, detector name, tag, and creation date.
Definition payloads.hpp:56
A payload object for surface masks.
Definition payloads.hpp:135
mask_shape shape
Shape used to interpret the boundary values.
Definition payloads.hpp:144
single_link_payload volume_link
Sensitive/passive surface: Mother volume index, Portal: Volume the portal leads into,...
Definition payloads.hpp:147
std::vector< io::scalar > boundaries
Boundary values according to boundaries enum of the shape in shape.
Definition payloads.hpp:149
A payload for surfaces.
Definition payloads.hpp:153
std::optional< std::uint64_t > identifier
Write the surface identifier as an additional information.
Definition payloads.hpp:166
detray::surface_id type
Either sensitive, passive or portal.
Definition payloads.hpp:169
std::optional< material_link_payload > material
Write the material link of the surface as additional information.
Definition payloads.hpp:162
transform_payload transform
Placement transform of the surface.
Definition payloads.hpp:158
std::optional< std::size_t > index_in_coll
Position of the surface in the volume surface container.
Definition payloads.hpp:156
std::vector< mask_payload > masks
Mask definitions attached to the surface.
Definition payloads.hpp:160
std::uint64_t source
Identifier of the source object (e.g. value of ACTS::GeometryIdentifier).
Definition payloads.hpp:164
A payload for an affine transformation in homogeneous coordinates.
Definition payloads.hpp:100
std::array< io::scalar, 9u > rot
Column-major 3 by 3 rotation matrix.
Definition payloads.hpp:106
std::array< io::scalar, 3u > tr
Translation.
Definition payloads.hpp:102
A payload for volumes.
Definition payloads.hpp:173
std::vector< surface_payload > surfaces
Surfaces belonging to this volume.
Definition payloads.hpp:181
detray::volume_id type
Geometric shape or category of the volume.
Definition payloads.hpp:177
single_link_payload index
Index of the volume in the detector volume container.
Definition payloads.hpp:183
transform_payload transform
Placement transform of the volume.
Definition payloads.hpp:179
std::string name
Detector-local volume name.
Definition payloads.hpp:175
std::optional< std::vector< acc_links_payload > > acc_links
Optional accelerator data structures.
Definition payloads.hpp:185
interface type to construct a subrange_view with CTAD
Definition subrange.hpp:90