File indexing completed on 2025-07-11 07:50:12
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(GeometryContext()) << std::endl;
0162 throw std::invalid_argument("Surface has no geometry ID");
0163 }
0164
0165 checkIdentifier(surface, "surface");
0166
0167
0168 if (surface.geometryId().sensitive() != 0) {
0169 m_surfacesById.emplace(surface.geometryId(), &surface);
0170 }
0171 }
0172
0173 void visitLayer(const Layer& layer) override {
0174
0175
0176
0177 if (layer.geometryId() != layer.surfaceRepresentation().geometryId()) {
0178 ACTS_ERROR("Layer ID mismatch: "
0179 << layer.geometryId()
0180 << " != " << layer.surfaceRepresentation().geometryId());
0181 throw std::invalid_argument("Layer ID mismatch");
0182 }
0183 }
0184
0185 void visitBoundarySurface(
0186 const BoundarySurfaceT<TrackingVolume>& boundary) override {
0187 checkIdentifier(boundary.surfaceRepresentation(), "boundary surface");
0188 }
0189
0190 void visitPortal(const Portal& portal) override {
0191 checkIdentifier(portal.surface(), "portal");
0192 }
0193
0194 std::unordered_map<GeometryIdentifier, const TrackingVolume*> m_volumesById{};
0195 std::unordered_map<GeometryIdentifier, const Surface*> m_surfacesById{};
0196
0197 std::unordered_map<GeometryIdentifier, const GeometryObject*> m_objectsById{};
0198 };
0199
0200 }
0201 TrackingGeometry::TrackingGeometry(
0202 const MutableTrackingVolumePtr& highestVolume,
0203 const IMaterialDecorator* materialDecorator,
0204 const GeometryIdentifierHook& hook, const Logger& logger, bool close)
0205 : m_world(highestVolume) {
0206 if (close) {
0207 ACTS_DEBUG("Closing tracking geometry with Gen1 assignment");
0208 Gen1GeometryClosureVisitor visitor{logger, materialDecorator, hook};
0209 apply(visitor);
0210 }
0211
0212 GeometryIdMapVisitor mapVisitor{logger};
0213 apply(mapVisitor);
0214 m_volumesById = std::move(mapVisitor.m_volumesById);
0215 m_surfacesById = std::move(mapVisitor.m_surfacesById);
0216
0217 ACTS_DEBUG("TrackingGeometry created with "
0218 << m_volumesById.size() << " volumes and " << m_surfacesById.size()
0219 << " sensitive surfaces");
0220
0221 m_volumesById.rehash(0);
0222 m_surfacesById.rehash(0);
0223 }
0224
0225 TrackingGeometry::~TrackingGeometry() = default;
0226
0227 const TrackingVolume* TrackingGeometry::lowestTrackingVolume(
0228 const GeometryContext& gctx, const Vector3& gp) const {
0229 return m_world->lowestTrackingVolume(gctx, gp, s_onSurfaceTolerance);
0230 }
0231
0232 const TrackingVolume* TrackingGeometry::highestTrackingVolume() const {
0233 return m_world.get();
0234 }
0235
0236 TrackingVolume* TrackingGeometry::highestTrackingVolume() {
0237 return m_world.get();
0238 }
0239
0240 std::shared_ptr<const TrackingVolume>
0241 TrackingGeometry::highestTrackingVolumePtr() const {
0242 return m_world;
0243 }
0244
0245 const Layer* TrackingGeometry::associatedLayer(const GeometryContext& gctx,
0246 const Vector3& gp) const {
0247 const TrackingVolume* lowestVol = lowestTrackingVolume(gctx, gp);
0248 if (lowestVol == nullptr) {
0249 return nullptr;
0250 }
0251 return lowestVol->associatedLayer(gctx, gp);
0252 }
0253
0254 const TrackingVolume* TrackingGeometry::findVolume(
0255 GeometryIdentifier id) const {
0256 auto vol = m_volumesById.find(id);
0257 if (vol == m_volumesById.end()) {
0258 return nullptr;
0259 }
0260 return vol->second;
0261 }
0262
0263 const Surface* TrackingGeometry::findSurface(GeometryIdentifier id) const {
0264 auto srf = m_surfacesById.find(id);
0265 if (srf == m_surfacesById.end()) {
0266 return nullptr;
0267 }
0268 return srf->second;
0269 }
0270
0271 const std::unordered_map<GeometryIdentifier, const Surface*>&
0272 TrackingGeometry::geoIdSurfaceMap() const {
0273 return m_surfacesById;
0274 }
0275
0276 void TrackingGeometry::visualize(IVisualization3D& helper,
0277 const GeometryContext& gctx,
0278 const ViewConfig& viewConfig,
0279 const ViewConfig& portalViewConfig,
0280 const ViewConfig& sensitiveViewConfig) const {
0281 highestTrackingVolume()->visualize(helper, gctx, viewConfig, portalViewConfig,
0282 sensitiveViewConfig);
0283 }
0284
0285 void TrackingGeometry::apply(TrackingGeometryVisitor& visitor) const {
0286 highestTrackingVolume()->apply(visitor);
0287 }
0288
0289 void TrackingGeometry::apply(TrackingGeometryMutableVisitor& visitor) {
0290 highestTrackingVolume()->apply(visitor);
0291 }
0292
0293 TrackingGeometry::GeometryVersion TrackingGeometry::geometryVersion() const {
0294 if (highestTrackingVolume()->portals().empty()) {
0295 return GeometryVersion::Gen1;
0296 } else {
0297 return GeometryVersion::Gen3;
0298 }
0299 }
0300
0301 }