|
| 1 | +/** Detray library, part of the ACTS project (R&D line) |
| 2 | + * |
| 3 | + * (c) 2024 CERN for the benefit of the ACTS project |
| 4 | + * |
| 5 | + * Mozilla Public License Version 2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +// Project include(s) |
| 11 | +#include "detray/builders/surface_factory_interface.hpp" |
| 12 | +#include "detray/definitions/detail/indexing.hpp" |
| 13 | +#include "detray/definitions/detail/qualifiers.hpp" |
| 14 | +#include "detray/materials/material.hpp" |
| 15 | +#include "detray/materials/predefined_materials.hpp" |
| 16 | + |
| 17 | +namespace detray { |
| 18 | + |
| 19 | +/// @brief Configuration for the homogeneous material generator |
| 20 | +template <typename scalar_t> |
| 21 | +struct hom_material_config { |
| 22 | + /// Type of material to put on the passive surfaces |
| 23 | + material<scalar_t> m_passive_material{silicon<scalar_t>{}}; |
| 24 | + /// Type of material to put on the sensitive surfaces |
| 25 | + material<scalar_t> m_sensitive_material{silicon<scalar_t>{}}; |
| 26 | + /// Type of material to put on the portal surfaces |
| 27 | + material<scalar_t> m_portal_material{vacuum<scalar_t>{}}; |
| 28 | + /// Minimal envelope for the portals (used in autofitting) |
| 29 | + scalar_t m_thickness{1.5f * unit<scalar_t>::mm}; |
| 30 | + |
| 31 | + /// Setters |
| 32 | + /// @{ |
| 33 | + constexpr hom_material_config &passive_material( |
| 34 | + const material<scalar_t> &m) { |
| 35 | + m_passive_material = m; |
| 36 | + return *this; |
| 37 | + } |
| 38 | + constexpr hom_material_config &sensitive_material( |
| 39 | + const material<scalar_t> &m) { |
| 40 | + m_sensitive_material = m; |
| 41 | + return *this; |
| 42 | + } |
| 43 | + constexpr hom_material_config &portal_material( |
| 44 | + const material<scalar_t> &m) { |
| 45 | + m_portal_material = m; |
| 46 | + return *this; |
| 47 | + } |
| 48 | + constexpr hom_material_config &thickness(const scalar_t t) { |
| 49 | + m_thickness = t; |
| 50 | + return *this; |
| 51 | + } |
| 52 | + /// @} |
| 53 | + |
| 54 | + /// Getters |
| 55 | + /// @{ |
| 56 | + constexpr const material<scalar_t> &passive_material() const { |
| 57 | + return m_passive_material; |
| 58 | + } |
| 59 | + constexpr const material<scalar_t> &sensitive_material() const { |
| 60 | + return m_sensitive_material; |
| 61 | + } |
| 62 | + constexpr const material<scalar_t> &portal_material() const { |
| 63 | + return m_portal_material; |
| 64 | + } |
| 65 | + constexpr scalar_t thickness() const { return m_thickness; } |
| 66 | + /// @} |
| 67 | +}; |
| 68 | + |
| 69 | +/// @brief Surface factory decorator that adds homogeneous material to surfaces |
| 70 | +/// |
| 71 | +/// @tparam detector_t the type of detector the volume belongs to. |
| 72 | +template <typename detector_t> |
| 73 | +class homogeneous_material_generator final |
| 74 | + : public factory_decorator<detector_t> { |
| 75 | + |
| 76 | + using scalar_t = typename detector_t::scalar_type; |
| 77 | + using point3_t = typename detector_t::point3; |
| 78 | + using vector3_t = typename detector_t::vector3; |
| 79 | + using transform3_t = typename detector_t::transform3; |
| 80 | + |
| 81 | + public: |
| 82 | + /// Construct from configuration @param cfg |
| 83 | + DETRAY_HOST |
| 84 | + homogeneous_material_generator( |
| 85 | + std::unique_ptr<surface_factory_interface<detector_t>> factory, |
| 86 | + const hom_material_config<scalar_t> cfg) |
| 87 | + : factory_decorator<detector_t>(std::move(factory)), m_cfg{cfg} {} |
| 88 | + |
| 89 | + /// Call the underlying surface factory and record the surface range that |
| 90 | + /// was produced |
| 91 | + /// |
| 92 | + /// @param volume the volume the portals need to be added to. |
| 93 | + /// @param surfaces the surface collection to wrap and to add the portals to |
| 94 | + /// @param transforms the transforms of the surfaces. |
| 95 | + /// @param masks the masks of the surfaces. |
| 96 | + /// @param ctx the geometry context (not needed for portals). |
| 97 | + DETRAY_HOST |
| 98 | + auto operator()(typename detector_t::volume_type &volume, |
| 99 | + typename detector_t::surface_lookup_container &surfaces, |
| 100 | + typename detector_t::transform_container &transforms, |
| 101 | + typename detector_t::mask_container &masks, |
| 102 | + typename detector_t::geometry_context ctx = {}) |
| 103 | + -> dindex_range override { |
| 104 | + |
| 105 | + auto [sf_offset, n_surfaces] = |
| 106 | + (*this->m_factory)(volume, surfaces, transforms, masks, ctx); |
| 107 | + |
| 108 | + m_surface_range = {sf_offset, sf_offset + n_surfaces}; |
| 109 | + |
| 110 | + return {sf_offset, n_surfaces}; |
| 111 | + } |
| 112 | + |
| 113 | + /// Create material slabs or rods for all surfaces that the undelying |
| 114 | + /// surface factory builds. |
| 115 | + /// |
| 116 | + /// @param surfaces surface container of the volume builder that should get |
| 117 | + /// decorated with material. |
| 118 | + /// @param material material store of the volume builder that the new |
| 119 | + /// materials get added to. |
| 120 | + DETRAY_HOST |
| 121 | + auto operator()(typename detector_t::surface_lookup_container &surfaces, |
| 122 | + typename detector_t::material_container &materials) { |
| 123 | + |
| 124 | + using material_id = typename detector_t::materials::id; |
| 125 | + using link_t = typename detector_t::surface_type::material_link; |
| 126 | + |
| 127 | + // Add the material to the surfaces that the data links against |
| 128 | + for (auto &sf : detray::ranges::subrange(surfaces, m_surface_range)) { |
| 129 | + |
| 130 | + const material<scalar_t> *mat_ptr{nullptr}; |
| 131 | + |
| 132 | + constexpr vacuum<scalar_t> vac{}; |
| 133 | + switch (sf.id()) { |
| 134 | + case surface_id::e_passive: { |
| 135 | + const auto &mat = m_cfg.passive_material(); |
| 136 | + mat_ptr = (mat != vac) ? &mat : nullptr; |
| 137 | + break; |
| 138 | + } |
| 139 | + case surface_id::e_sensitive: { |
| 140 | + const auto &mat = m_cfg.sensitive_material(); |
| 141 | + mat_ptr = (mat != vac) ? &mat : nullptr; |
| 142 | + break; |
| 143 | + } |
| 144 | + case surface_id::e_portal: { |
| 145 | + const auto &mat = m_cfg.portal_material(); |
| 146 | + mat_ptr = (mat != vac) ? &mat : nullptr; |
| 147 | + break; |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + // Found suitable material for this surface? |
| 152 | + if (mat_ptr == nullptr) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + // Handle line shaped surfaces differently |
| 157 | + bool is_line{false}; |
| 158 | + link_t mat_link; |
| 159 | + |
| 160 | + if constexpr (detector_t::materials::template is_defined< |
| 161 | + material_rod<scalar_t>>()) { |
| 162 | + |
| 163 | + using mask_id = typename detector_t::masks::id; |
| 164 | + |
| 165 | + const mask_id sf_mask_id = sf.mask().id(); |
| 166 | + if (sf_mask_id == mask_id::e_straw_tube || |
| 167 | + sf_mask_id == mask_id::e_drift_cell) { |
| 168 | + |
| 169 | + is_line = true; |
| 170 | + |
| 171 | + auto &mat_coll = |
| 172 | + materials.template get<material_id::e_rod>(); |
| 173 | + mat_coll.emplace_back(*mat_ptr, m_cfg.thickness()); |
| 174 | + |
| 175 | + mat_link = {material_id::e_rod, |
| 176 | + static_cast<dindex>(mat_coll.size() - 1u)}; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (!is_line) { |
| 181 | + auto &mat_coll = materials.template get<material_id::e_slab>(); |
| 182 | + mat_coll.emplace_back(*mat_ptr, m_cfg.thickness()); |
| 183 | + |
| 184 | + mat_link = {material_id::e_slab, |
| 185 | + static_cast<dindex>(mat_coll.size() - 1u)}; |
| 186 | + } |
| 187 | + |
| 188 | + // Set the initial surface material link (will be updated when |
| 189 | + // added to the detector) |
| 190 | + sf.material() = mat_link; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private: |
| 195 | + /// Portal generator configuration |
| 196 | + hom_material_config<scalar_t> m_cfg; |
| 197 | + /// Range of surface indices for which to generate material |
| 198 | + dindex_range m_surface_range{}; |
| 199 | +}; |
| 200 | + |
| 201 | +} // namespace detray |
0 commit comments