Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:56

0001 /*
0002  * ABBoxManager.h
0003  *
0004  *  Created on: 24.04.2015
0005  *      Author: swenzel
0006  */
0007 
0008 #pragma once
0009 
0010 #include "VecGeom/base/Global.h"
0011 
0012 #include "VecGeom/volumes/PlacedVolume.h"
0013 #include "VecGeom/base/Vector3D.h"
0014 #include "VecGeom/management/GeoManager.h"
0015 #include "VecGeom/navigation/NavigationState.h"
0016 #include "VecGeom/base/Transformation3D.h"
0017 #include "VecGeom/volumes/kernel/BoxImplementation.h"
0018 
0019 #include <map>
0020 #include <vector>
0021 
0022 namespace vecgeom {
0023 inline namespace VECGEOM_IMPL_NAMESPACE {
0024 
0025 // Singleton class for ABBox manager
0026 // keeps a (centralized) map of volume pointers to vectors of aligned bounding boxes
0027 // the alternative would be to include such a thing into logical volumes
0028 class ABBoxManager {
0029 public:
0030   typedef float Real_t;
0031   using Float_v = vecgeom::VectorBackend::Float_v;
0032 
0033   typedef Vector3D<Float_v> ABBox_v;
0034   // scalar
0035   typedef Vector3D<Precision> ABBox_s;
0036 
0037   // use old style arrays here as std::vector has some problems
0038   // with Vector3D<kVc::Double_t>
0039   typedef ABBox_s *ABBoxContainer_t;
0040   typedef ABBox_v *ABBoxContainer_v;
0041 
0042   typedef std::pair<unsigned int, double> BoxIdDistancePair_t;
0043 
0044   // build an abstraction of sort to sort vectors and lists portably
0045   template <typename C, typename Compare>
0046   static void sort(C &v, Compare cmp)
0047   {
0048     std::sort(v.begin(), v.end(), cmp);
0049   }
0050 
0051   struct HitBoxComparatorFunctor {
0052     bool operator()(BoxIdDistancePair_t const &left, BoxIdDistancePair_t const &right)
0053     {
0054       return left.second < right.second;
0055     }
0056   };
0057 
0058   using FP_t = HitBoxComparatorFunctor;
0059 
0060 private:
0061   std::vector<ABBoxContainer_t> fVolToABBoxesMap;
0062   std::vector<ABBoxContainer_v> fVolToABBoxesMap_v;
0063 
0064 public:
0065   // computes the aligned bounding box for a certain placed volume
0066   static void ComputeABBox(VPlacedVolume const *pvol, ABBox_s *lower, ABBox_s *upper);
0067   static void ComputeSplittedABBox(VPlacedVolume const *pvol, std::vector<ABBox_s> &lower, std::vector<ABBox_s> &upper,
0068                                    int numOfSlices);
0069 
0070   static ABBoxManager &Instance()
0071   {
0072     static ABBoxManager instance;
0073     return instance;
0074   }
0075 
0076   // initialize ABBoxes for a certain logical volume
0077   // very first version that just creates as many boxes as there are daughters
0078   // in reality we might have a lot more boxes than daughters (but not less)
0079   void InitABBoxes(LogicalVolume const *lvol);
0080 
0081   // doing the same for many logical volumes
0082   template <typename Container>
0083   void InitABBoxes(Container const &lvolumes)
0084   {
0085     for (auto lvol : lvolumes) {
0086       InitABBoxes(lvol);
0087     }
0088   }
0089 
0090   void InitABBoxesForCompleteGeometry()
0091   {
0092     auto &container = GeoManager::Instance().GetLogicalVolumesMap();
0093     fVolToABBoxesMap.resize(container.size(), nullptr);
0094     fVolToABBoxesMap_v.resize(container.size(), nullptr);
0095     std::vector<LogicalVolume const *> logicalvolumes(container.size());
0096     logicalvolumes.resize(0);
0097     for (auto p : container) {
0098       logicalvolumes.push_back(p.second);
0099     }
0100     InitABBoxes(logicalvolumes);
0101   }
0102 
0103   // remove the boxes from the list
0104   void RemoveABBoxes(LogicalVolume const *lvol);
0105 
0106   // returns the Container for a given logical volume or nullptr if
0107   // it does not exist
0108   ABBoxContainer_t GetABBoxes(LogicalVolume const *lvol, int &size)
0109   {
0110     size = lvol->GetDaughtersp()->size();
0111     return fVolToABBoxesMap[lvol->id()];
0112   }
0113 
0114   // returns the Container for a given logical volume or nullptr if
0115   // it does not exist
0116   ABBoxContainer_v GetABBoxes_v(LogicalVolume const *lvol, int &size)
0117   {
0118     int ndaughters = lvol->GetDaughtersp()->size();
0119     int extra      = (ndaughters % vecCore::VectorSize<Float_v>() > 0) ? 1 : 0;
0120     size           = ndaughters / vecCore::VectorSize<Float_v>() + extra;
0121     return fVolToABBoxesMap_v[lvol->id()];
0122   }
0123 };
0124 
0125 // output for hitboxes
0126 template <typename stream>
0127 stream &operator<<(stream &s, std::vector<ABBoxManager::BoxIdDistancePair_t> const &list)
0128 {
0129   for (auto i : list) {
0130     s << "(" << i.first << "," << i.second << ")"
0131       << " ";
0132   }
0133   return s;
0134 }
0135 }
0136 } // end namespace