Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DetectorVolume.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DetectorVolume.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022-2023 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 http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
14 #include "Acts/Geometry/Extent.hpp"
25 
26 #include <algorithm>
27 #include <cstddef>
28 #include <memory>
29 #include <stdexcept>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 namespace Acts {
35 
36 class Surface;
37 class IVolumeMaterial;
38 class VolumeBounds;
39 
40 namespace Experimental {
41 
42 class DetectorVolume;
43 class Portal;
44 class Detector;
45 
59 class DetectorVolume : public std::enable_shared_from_this<DetectorVolume> {
60  public:
61  using BoundingBox =
64 
65  friend class DetectorVolumeFactory;
66 
74  template <typename internal_type>
75  struct ObjectStore {
77  std::vector<internal_type> internal = {};
78 
80  std::vector<const typename internal_type::element_type*> external = {};
81 
85  ObjectStore(std::vector<internal_type> objects)
86  : internal(std::move(objects)) {
88  }
89 
90  ObjectStore() = default;
91  };
92 
93  protected:
110  const Transform3& transform, std::shared_ptr<VolumeBounds> bounds,
111  std::vector<std::shared_ptr<Surface>> surfaces,
112  std::vector<std::shared_ptr<DetectorVolume>> volumes,
114  SurfaceCandidatesUpdator surfaceCandidateUpdator) noexcept(false);
115 
129  const Transform3& transform, std::shared_ptr<VolumeBounds> bounds,
130  SurfaceCandidatesUpdator surfaceCandidateUpdator) noexcept(false);
131 
135  static std::shared_ptr<DetectorVolume> makeShared(
137  const Transform3& transform, std::shared_ptr<VolumeBounds> bounds,
138  std::vector<std::shared_ptr<Surface>> surfaces,
139  std::vector<std::shared_ptr<DetectorVolume>> volumes,
141  SurfaceCandidatesUpdator surfaceCandidateUpdator);
142 
146  static std::shared_ptr<DetectorVolume> makeShared(
148  const Transform3& transform, std::shared_ptr<VolumeBounds> bounds,
149  SurfaceCandidatesUpdator surfaceCandidateUpdator);
150 
151  public:
161  std::shared_ptr<DetectorVolume> getSharedPtr();
162 
172  std::shared_ptr<const DetectorVolume> getSharedPtr() const;
173 
182  const Transform3& transform(
183  const GeometryContext& gctx = GeometryContext()) const;
184 
194 
198  const VolumeBounds& volumeBounds() const;
199 
206  bool inside(const GeometryContext& gctx, const Vector3& position) const;
207 
216  const Vector3& position) const;
217 
224  Extent extent(const GeometryContext& gctx, size_t nseg = 1) const;
225 
238  NavigationState& nState) const;
239 
243  std::vector<std::shared_ptr<Portal>>& portalPtrs();
244 
248  std::vector<std::shared_ptr<Surface>>& surfacePtrs();
249 
253  std::vector<std::shared_ptr<DetectorVolume>>& volumePtrs();
254 
261  const std::vector<const Portal*>& portals() const;
262 
270  const std::vector<const Surface*>& surfaces() const;
271 
280  const std::vector<const DetectorVolume*>& volumes() const;
281 
284 
293  SurfaceCandidatesUpdator surfaceCandidateUpdator,
294  const std::vector<std::shared_ptr<Surface>>& surfaces = {},
295  const std::vector<std::shared_ptr<DetectorVolume>>& volumes = {});
296 
299 
306  void updatePortal(std::shared_ptr<Portal> portal,
307  unsigned int pIndex) noexcept(false);
308 
310  void closePortals();
311 
319  void assignVolumeMaterial(std::shared_ptr<IVolumeMaterial> material);
320 
322  std::shared_ptr<IVolumeMaterial> volumeMaterialPtr();
323 
325  const IVolumeMaterial* volumeMaterial() const;
326 
328  const std::string& name() const;
329 
331  const GeometryIdentifier& geometryId() const;
332 
337  void assignGeometryId(const GeometryIdentifier& geoID);
338 
341  void assignDetector(const Detector& detector);
342 
344  const Detector* detector() const;
345 
346  const BoundingBox& getBoundingBox() const;
347 
348  private:
355  void construct(const GeometryContext& gctx,
356  const PortalGenerator& portalGenerator) noexcept(false);
357 
358  // Check containment - only in debug mode
364  bool checkContainment(const GeometryContext& gctx, size_t nseg = 1) const;
365 
369 
371  std::string m_name = "Unnamed";
372 
374  Transform3 m_transform = Transform3::Identity();
375 
377  std::shared_ptr<VolumeBounds> m_bounds = nullptr;
378 
381 
384 
387 
389  std::shared_ptr<const BoundingBox> m_boundingBox;
390 
392 
395 
397  std::shared_ptr<IVolumeMaterial> m_volumeMaterial = nullptr;
398 
401 
403  const Detector* m_detector = nullptr;
404 };
405 
410  public:
412  static std::shared_ptr<DetectorVolume> construct(
414  const std::string& name, const Transform3& transform,
415  std::shared_ptr<VolumeBounds> bounds,
416  const std::vector<std::shared_ptr<Surface>>& surfaces,
417  const std::vector<std::shared_ptr<DetectorVolume>>& volumes,
418  DetectorVolumeUpdator detectorVolumeUpdator,
419  SurfaceCandidatesUpdator surfaceCandidateUpdator) {
420  auto dVolume = DetectorVolume::makeShared(
421  gctx, name, transform, std::move(bounds), surfaces, volumes,
422  std::move(detectorVolumeUpdator), std::move(surfaceCandidateUpdator));
423  dVolume->construct(gctx, portalGenerator);
424  return dVolume;
425  }
426 
428  static std::shared_ptr<DetectorVolume> construct(
431  std::shared_ptr<VolumeBounds> bounds,
432  SurfaceCandidatesUpdator surfaceCandidateUpdator) {
433  auto dVolume = DetectorVolume::makeShared(
434  gctx, std::move(name), transform, std::move(bounds),
435  std::move(surfaceCandidateUpdator));
436  dVolume->construct(gctx, portalGenerator);
437  return dVolume;
438  }
439 };
440 
449  inline static const std::vector<const Portal*> extract(
450  [[maybe_unused]] const GeometryContext& gctx,
451  const NavigationState& nState) {
452  if (nState.currentVolume == nullptr) {
453  throw std::runtime_error(
454  "AllPortalsExtractor: no detector volume given.");
455  }
456  return nState.currentVolume->portals();
457  }
458 };
459 
469  inline static const std::vector<const Surface*> extract(
470  [[maybe_unused]] const GeometryContext& gctx,
471  const NavigationState& nState,
472  [[maybe_unused]] const std::vector<size_t>& indices = {}) {
473  if (nState.currentVolume == nullptr) {
474  throw std::runtime_error(
475  "AllSurfacesExtractor: no detector volume given.");
476  }
477  return nState.currentVolume->surfaces();
478  }
479 };
480 
492  inline static const std::vector<const Surface*> extract(
493  [[maybe_unused]] const GeometryContext& gctx,
494  const NavigationState& nState, const std::vector<size_t>& indices) {
495  if (nState.currentVolume == nullptr) {
496  throw std::runtime_error(
497  "IndexedSurfacesExtractor: no detector volume given.");
498  }
499  // Get the surface container
500  const auto& surfaces = nState.currentVolume->surfaces();
501  // The extracted surfaces
502  std::vector<const Surface*> eSurfaces;
503  eSurfaces.reserve(indices.size());
504  std::for_each(indices.begin(), indices.end(),
505  [&](const auto& i) { eSurfaces.push_back(surfaces[i]); });
506  return eSurfaces;
507  }
508 };
509 
519  inline static const std::vector<const DetectorVolume*> extract(
520  [[maybe_unused]] const GeometryContext& gctx,
521  const NavigationState& nState,
522  [[maybe_unused]] const std::vector<size_t>& indices = {}) {
523  if (nState.currentVolume == nullptr) {
524  throw std::runtime_error(
525  "AllSubVolumesExtractor: no detector volume given.");
526  }
527  return nState.currentVolume->volumes();
528  }
529 };
530 
540  inline static const std::vector<const DetectorVolume*> extract(
541  [[maybe_unused]] const GeometryContext& gctx,
542  const NavigationState& nState, const std::vector<size_t>& indices) {
543  if (nState.currentVolume == nullptr) {
544  throw std::runtime_error(
545  "AllSubVolumesExtractor: no detector volume given.");
546  }
547  // Get the sub volumes container
548  const auto& volumes = nState.currentVolume->volumes();
549  // The extracted volumes
550  std::vector<const DetectorVolume*> eVolumes;
551  eVolumes.reserve(indices.size());
552  std::for_each(indices.begin(), indices.end(),
553  [&](const auto& i) { eVolumes.push_back(volumes[i]); });
554  return eVolumes;
555  }
556 };
557 
558 } // namespace Experimental
559 } // namespace Acts