Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*
0002  * EmbreeManager.h
0003  *
0004  *  Created on: May 18, 2018
0005  *      Author: swenzel
0006  */
0007 
0008 #ifndef VECGEOM_MANAGEMENT_EMBREEMANAGER_H_
0009 #define VECGEOM_MANAGEMENT_EMBREEMANAGER_H_
0010 
0011 #include "VecGeom/base/Global.h"
0012 #include "VecGeom/volumes/PlacedVolume.h"
0013 #include "VecGeom/base/Vector3D.h"
0014 #include "VecGeom/management/GeoManager.h"
0015 #include <vector>
0016 
0017 #include <embree3/rtcore.h>
0018 
0019 namespace vecgeom {
0020 inline namespace VECGEOM_IMPL_NAMESPACE {
0021 
0022 class VPlacedVolume;
0023 
0024 // enumeration used to decide how to build the structure
0025 enum class EmbreeBuildMode {
0026   kAABBox, // from axis aligned bounding boxes
0027   kBBox    // from arbitrary (rotated) bounding boxes
0028 };
0029 
0030 // A singleton class which manages Embree geometries/scenes/helper structure for voxelized navigation
0031 // The helper structure is whatever Intel Embree decides to use
0032 class EmbreeManager {
0033 
0034 public:
0035   typedef Vector3D<Precision> ABBox_s;
0036   typedef ABBox_s *ABBoxContainer_t;
0037 
0038   // first index is # daughter index, second is step
0039   typedef std::pair<int, double> BoxIdDistancePair_t;
0040   using HitContainer_t = std::vector<BoxIdDistancePair_t>;
0041 
0042   // the actual class encapsulating the Embree structures
0043   struct EmbreeAccelerationStructure {
0044     RTCDevice fDevice;
0045     RTCScene fScene;
0046     Vector3D<float> *fNormals; // normals of triangles/quads made available to user algorithms
0047     int fNumberObjects;        // number of objects
0048   };
0049 
0050 private:
0051   // keeps/registers an acceleration structure for logical volumes
0052   std::vector<EmbreeAccelerationStructure const *> fStructureHolder;
0053 
0054 public:
0055   // initialized the helper structure for a given logical volume
0056   void InitStructure(LogicalVolume const *lvol);
0057 
0058   // initialized the helper structure for the complete geometry
0059   // (this might not be appropriate and will consume memory)
0060   void InitVoxelStructureForCompleteGeometry()
0061   {
0062     std::vector<LogicalVolume const *> logicalvolumes;
0063     GeoManager::Instance().GetAllLogicalVolumes(logicalvolumes);
0064     for (auto lvol : logicalvolumes) {
0065       InitStructure(lvol);
0066     }
0067   }
0068 
0069   static EmbreeManager &Instance()
0070   {
0071     static EmbreeManager manager;
0072     return manager;
0073   }
0074 
0075   // removed/deletes the helper structure for a given logical volume
0076   void RemoveStructure(LogicalVolume const *lvol);
0077 
0078   EmbreeAccelerationStructure const *GetAccStructure(LogicalVolume const *lvol) const
0079   {
0080     return fStructureHolder[lvol->id()];
0081   }
0082 
0083   // public method allowing to build Embree acceleration structure
0084   // given a vector of aligned bounding boxes (without reference to a logical volume)
0085   EmbreeAccelerationStructure *BuildStructureFromBoundingBoxes(ABBoxContainer_t alignedboxes,
0086                                                                size_t numberofboxes) const;
0087 
0088   // build structure for a given logical volume
0089   EmbreeAccelerationStructure *BuildStructureFromBoundingBoxes(LogicalVolume const *lvol) const;
0090 
0091   // we could add more such methods starting from other structures (non-aligned bounding boxes or any crude triangular
0092   // hulls)
0093   void SetBuildMode(EmbreeBuildMode mode) { fBuildMode = mode; }
0094 
0095 private:
0096   // private methods use
0097   void BuildStructure(LogicalVolume const *lvol);
0098 
0099   // adds a particular bounding box to an Embree scene
0100   void AddBoxGeometryToScene(EmbreeAccelerationStructure &, Vector3D<Precision> const &lower,
0101                              Vector3D<Precision> const &upper,
0102                              Transformation3D const &transf = Transformation3D::kIdentity) const;
0103 
0104   // adds an arbitratry bounding box (not necessarily axis aligned) for pvol
0105   void AddArbitraryBBoxToScene(EmbreeAccelerationStructure &, VPlacedVolume const *pvol,
0106                                EmbreeBuildMode mode = EmbreeBuildMode::kBBox) const;
0107 
0108   EmbreeBuildMode fBuildMode = EmbreeBuildMode::kAABBox;
0109 
0110 }; // end class
0111 } // namespace VECGEOM_IMPL_NAMESPACE
0112 } // namespace vecgeom
0113 
0114 #endif /* MANAGEMENT_EMBREEMANAGER_H_ */