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