File indexing completed on 2026-03-30 07:45:56
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Geometry/TrackingGeometry.hpp"
0010
0011 #include "Acts/Definitions/Tolerance.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Geometry/GeometryIdentifier.hpp"
0014 #include "Acts/Geometry/GeometryObject.hpp"
0015 #include "Acts/Geometry/TrackingGeometryVisitor.hpp"
0016 #include "Acts/Geometry/TrackingVolume.hpp"
0017 #include "Acts/Material/ProtoVolumeMaterial.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019
0020 #include <cstddef>
0021
0022 namespace Acts {
0023
0024 class Gen1GeometryClosureVisitor : public TrackingGeometryMutableVisitor {
0025 public:
0026 Gen1GeometryClosureVisitor(const Logger& logger,
0027 const IMaterialDecorator* materialDecorator,
0028 const GeometryIdentifierHook& hook)
0029 : m_logger(&logger),
0030 m_materialDecorator(materialDecorator),
0031 m_hook(&hook) {
0032 ACTS_VERBOSE("Creating Gen1GeometryClosureVisitor");
0033 }
0034
0035 const Logger& logger() const { return *m_logger; }
0036
0037 void visitVolume(TrackingVolume& volume) override {
0038 ACTS_DEBUG("Volume: " << volume.volumeName());
0039
0040
0041 m_volumeID = GeometryIdentifier().withVolume(m_volumeID.volume() + 1);
0042
0043 m_iboundary = 0;
0044
0045 m_ilayer = 0;
0046
0047
0048 ACTS_VERBOSE("~> volumeID: " << m_volumeID);
0049 volume.assignGeometryId(m_volumeID);
0050
0051
0052 if (m_materialDecorator != nullptr) {
0053 ACTS_VERBOSE("Decorating volume " << volume.volumeName()
0054 << " with material");
0055 m_materialDecorator->decorate(volume);
0056 }
0057 if (volume.volumeMaterial() == nullptr &&
0058 volume.motherVolume() != nullptr &&
0059 volume.motherVolume()->volumeMaterial() != nullptr) {
0060 auto protoMaterial = dynamic_cast<const ProtoVolumeMaterial*>(
0061 volume.motherVolume()->volumeMaterial());
0062 if (protoMaterial == nullptr) {
0063 volume.assignVolumeMaterial(volume.motherVolume()->volumeMaterialPtr());
0064 }
0065 }
0066 }
0067
0068 void visitBoundarySurface(
0069 BoundarySurfaceT<TrackingVolume>& boundary) override {
0070 ACTS_DEBUG("BoundarySurface: " << boundary.surfaceRepresentation().name());
0071
0072 auto& bSurface = boundary.surfaceRepresentation();
0073
0074 m_iboundary += 1;
0075 auto boundaryID = GeometryIdentifier(m_volumeID).withBoundary(m_iboundary);
0076 ACTS_VERBOSE("~> boundaryID: " << boundaryID);
0077
0078 auto& mutableBSurface = *(const_cast<RegularSurface*>(&bSurface));
0079
0080
0081 ACTS_VERBOSE("~> assigning boundaryID: " << boundaryID);
0082 mutableBSurface.assignGeometryId(boundaryID);
0083
0084
0085 if (m_materialDecorator != nullptr) {
0086 ACTS_VERBOSE("Decorating boundary surface " << bSurface.name()
0087 << " with material");
0088 m_materialDecorator->decorate(mutableBSurface);
0089 }
0090 }
0091
0092 void visitLayer(Layer& layer) override {
0093 ACTS_DEBUG("Close Layer");
0094
0095 m_ilayer += 1;
0096 auto layerID = GeometryIdentifier(m_volumeID).withLayer(m_ilayer);
0097 ACTS_VERBOSE("~> layerID: " << layerID);
0098
0099
0100 layer.closeGeometry(m_materialDecorator, layerID, *m_hook, *m_logger);
0101 }
0102
0103 const Logger* m_logger;
0104 GeometryIdentifier m_volumeID;
0105 GeometryIdentifier::Value m_iboundary = 0;
0106 GeometryIdentifier::Value m_ilayer = 0;
0107 const IMaterialDecorator* m_materialDecorator = nullptr;
0108 const GeometryIdentifierHook* m_hook = nullptr;
0109
0110 std::unordered_map<GeometryIdentifier, const TrackingVolume*> m_volumesById{};
0111 std::unordered_map<GeometryIdentifier, const Surface*> m_surfacesById{};
0112 };
0113
0114 namespace {
0115 class GeometryIdMapVisitor : public TrackingGeometryVisitor {
0116 private:
0117 void checkIdentifier(const GeometryObject& obj, std::string_view type) {
0118 if (obj.geometryId() == GeometryIdentifier{}) {
0119 std::stringstream ss;
0120 ss << "Encountered " << type << " with no geometry ID";
0121 throw std::invalid_argument(ss.str());
0122 }
0123
0124 ACTS_VERBOSE("Checking identifier for " << type << ": "
0125 << obj.geometryId());
0126
0127 auto [it, inserted] = m_objectsById.emplace(obj.geometryId(), &obj);
0128
0129 if (!inserted && it->second != &obj) {
0130 std::stringstream ss;
0131 ss << "Duplicate " << type << " ID: " << obj.geometryId() << ": & "
0132 << it->second << " != " << &obj;
0133 if (const auto* other = dynamic_cast<const TrackingVolume*>(it->second);
0134 other != nullptr) {
0135 ss << " (" << other->volumeName() << ")";
0136 }
0137 ACTS_ERROR(ss.str());
0138 throw std::invalid_argument(ss.str());
0139 } else {
0140 ACTS_VERBOSE("Inserted " << type << " ID: " << obj.geometryId()
0141 << " pointing at " << &obj);
0142 }
0143 }
0144
0145 const Logger& logger() const { return m_logger; }
0146 const Logger& m_logger;
0147
0148 public:
0149 explicit GeometryIdMapVisitor(const Logger& logger) : m_logger(logger) {}
0150
0151 void visitVolume(const TrackingVolume& volume) override {
0152 std::string label = "volume(" + volume.volumeName() + ")";
0153 checkIdentifier(volume, label);
0154
0155 m_volumesById.emplace(volume.geometryId(), &volume);
0156 }
0157
0158 void visitSurface(const Surface& surface) override {
0159 if (surface.geometryId() == GeometryIdentifier{}) {
0160 std::cout << "Surface has no geometry ID: "
0161 << surface.toStream(
0162 GeometryContext::dangerouslyDefaultConstruct())
0163 << std::endl;
0164 throw std::invalid_argument("Surface has no geometry ID");
0165 }
0166
0167 checkIdentifier(surface, "surface");
0168
0169 m_surfacesById.emplace(surface.geometryId(), &surface);
0170 }
0171
0172 void visitLayer(const Layer& layer) override {
0173
0174
0175
0176 if (layer.geometryId() != layer.surfaceRepresentation().geometryId()) {
0177 ACTS_ERROR("Layer ID mismatch: "
0178 << layer.geometryId()
0179 << " != " << layer.surfaceRepresentation().geometryId());
0180 throw std::invalid_argument("Layer ID mismatch");
0181 }
0182 }
0183
0184 void visitBoundarySurface(
0185 const BoundarySurfaceT<TrackingVolume>& boundary) override {
0186 const auto& surface = boundary.surfaceRepresentation();
0187 checkIdentifier(surface, "boundary surface");
0188 m_surfacesById.emplace(surface.geometryId(), &surface);
0189 }
0190
0191 void visitPortal(const Portal& portal) override {
0192 const auto& surface = portal.surface();
0193 checkIdentifier(surface, "portal");
0194 m_surfacesById.emplace(surface.geometryId(), &surface);
0195 }
0196
0197 std::unordered_map<GeometryIdentifier, const TrackingVolume*> m_volumesById{};
0198 std::unordered_map<GeometryIdentifier, const Surface*> m_surfacesById{};
0199
0200 std::unordered_map<GeometryIdentifier, const GeometryObject*> m_objectsById{};
0201 };
0202
0203 }
0204 TrackingGeometry::TrackingGeometry(
0205 const MutableTrackingVolumePtr& highestVolume,
0206 const IMaterialDecorator* materialDecorator,
0207 const GeometryIdentifierHook& hook, const Logger& logger, bool close)
0208 : m_world(highestVolume) {
0209 if (close) {
0210 ACTS_DEBUG("Closing tracking geometry with Gen1 assignment");
0211 Gen1GeometryClosureVisitor visitor{logger, materialDecorator, hook};
0212 apply(visitor);
0213 }
0214
0215 GeometryIdMapVisitor mapVisitor{logger};
0216 apply(mapVisitor);
0217 m_volumesById = std::move(mapVisitor.m_volumesById);
0218 m_surfacesById = std::move(mapVisitor.m_surfacesById);
0219
0220 ACTS_DEBUG("TrackingGeometry created with "
0221 << m_volumesById.size() << " volumes and " << m_surfacesById.size()
0222 << " surfaces");
0223
0224 m_volumesById.rehash(0);
0225 m_surfacesById.rehash(0);
0226 }
0227
0228 TrackingGeometry::~TrackingGeometry() = default;
0229
0230 const TrackingVolume* TrackingGeometry::lowestTrackingVolume(
0231 const GeometryContext& gctx, const Vector3& gp) const {
0232 return m_world->lowestTrackingVolume(gctx, gp, s_onSurfaceTolerance);
0233 }
0234
0235 const TrackingVolume* TrackingGeometry::highestTrackingVolume() const {
0236 return m_world.get();
0237 }
0238
0239 TrackingVolume* TrackingGeometry::highestTrackingVolume() {
0240 return m_world.get();
0241 }
0242
0243 std::shared_ptr<const TrackingVolume>
0244 TrackingGeometry::highestTrackingVolumePtr() const {
0245 return m_world;
0246 }
0247
0248 const Layer* TrackingGeometry::associatedLayer(const GeometryContext& gctx,
0249 const Vector3& gp) const {
0250 const TrackingVolume* lowestVol = lowestTrackingVolume(gctx, gp);
0251 if (lowestVol == nullptr) {
0252 return nullptr;
0253 }
0254 return lowestVol->associatedLayer(gctx, gp);
0255 }
0256
0257 const TrackingVolume* TrackingGeometry::findVolume(
0258 GeometryIdentifier id) const {
0259 auto vol = m_volumesById.find(id);
0260 if (vol == m_volumesById.end()) {
0261 return nullptr;
0262 }
0263 return vol->second;
0264 }
0265
0266 const Surface* TrackingGeometry::findSurface(GeometryIdentifier id) const {
0267 auto srf = m_surfacesById.find(id);
0268 if (srf == m_surfacesById.end()) {
0269 return nullptr;
0270 }
0271 return srf->second;
0272 }
0273
0274 const std::unordered_map<GeometryIdentifier, const Surface*>&
0275 TrackingGeometry::geoIdSurfaceMap() const {
0276 return m_surfacesById;
0277 }
0278
0279 void TrackingGeometry::visualize(IVisualization3D& helper,
0280 const GeometryContext& gctx,
0281 const ViewConfig& viewConfig,
0282 const ViewConfig& portalViewConfig,
0283 const ViewConfig& sensitiveViewConfig) const {
0284 highestTrackingVolume()->visualize(helper, gctx, viewConfig, portalViewConfig,
0285 sensitiveViewConfig);
0286 }
0287
0288 void TrackingGeometry::apply(TrackingGeometryVisitor& visitor) const {
0289 highestTrackingVolume()->apply(visitor);
0290 }
0291
0292 void TrackingGeometry::apply(TrackingGeometryMutableVisitor& visitor) {
0293 highestTrackingVolume()->apply(visitor);
0294 }
0295
0296 TrackingGeometry::GeometryVersion TrackingGeometry::geometryVersion() const {
0297 if (highestTrackingVolume()->portals().empty()) {
0298 return GeometryVersion::Gen1;
0299 } else {
0300 return GeometryVersion::Gen3;
0301 }
0302 }
0303
0304 }