Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:56

0001 #pragma once
0002 
0003 #include <string>
0004 #include <vector>
0005 #include <glm/glm.hpp>
0006 #include "plog/Severity.h"
0007 
0008 struct SBitSet ;
0009 struct NP ;
0010 struct SSim ;
0011 struct stree ;
0012 struct SScene ;
0013 struct SMeshGroup ;
0014 
0015 #include "scuda.h"
0016 #include "squad.h"
0017 #include "sqat4.h"
0018 #include "saabb.h"
0019 #include "stran.h"
0020 #include "SGeo.hh"
0021 
0022 struct sframe ;
0023 struct CSGTarget ;
0024 
0025 struct SName ;
0026 struct CSGMaker ;
0027 struct CSGImport ;
0028 
0029 #include "CSGEnum.h"
0030 #include "CSGSolid.h"
0031 #include "CSGPrim.h"
0032 #include "CSGNode.h"
0033 
0034 
0035 #include "CSG_API_EXPORT.hh"
0036 
0037 /**
0038 CSGFoundry
0039 ============
0040 
0041 The purpose of CSGFoundry is to hold the subset of geometry info that is
0042 needed on GPU in a form suitable for giving to OptiX.
0043 
0044 
0045 ::
0046 
0047         +-------+     +-------+    +---------+     +--------+       +-------+   +-------+        +------+
0048         | inst  |     | solid |    |  prim   |     | node   |       | tran  |   | itra  |        | plan |
0049         +=======+     +=======+    +=========+     +========+       +=======+   +=======+        +======+
0050         | s=0   | ->  |       | -> |         | ->  |        |  ->   |       |   |       |        |      |
0051         | s=0   |     |       |    |         |     |        |       |       |   |       |        |      |
0052         | s=0   |     |       |    +---------+     |        |       |       |   |       |        |      |
0053         | s=0   |     +-------+    |         |     |        |       |       |   |       |        +------+
0054         | s=1   |                  |         |     |        |       |       |   |       |
0055         | s=1   |                  |         |     +--------+       |       |   |       |
0056         | s=1   |                  |         |     |        |       |       |   |       |
0057         | s=1   |                  +---------+     |        |       |       |   |       |
0058         +-------+                                  |        |       |       |   |       |
0059                                                    |        |       |       |   |       |
0060                                                    |        |       |       |   |       |
0061                                                    |        |       |       |   |       |
0062                                                    |        |       |       |   |       |
0063                                                    |        |       |       |   |       |
0064                                                    +--------+       |       |   |       |
0065                                                                     |       |   |       |
0066                                                                     |       |   |       |
0067                                                                     |       |   |       |
0068                                                                     |       |   |       |
0069                                                                     +-------+   +-------+
0070 
0071 inst
0072    array of qat4 with single solid references (gas_idx)
0073 
0074 solid
0075    references range of prim with (numPrim, primOffset)
0076 prim
0077    references range of node with (numNode, nodeOffset) (roughly corresponds to G4VSolid)
0078 node
0079    references single transform with tranOffset
0080    sometimes references range of planes with (planOffset, numPlan)
0081    (CSG constituent nodes : sphere, box, boolean operators)
0082 
0083 tran
0084     array of qat4 node transforms, same size as itra
0085 itra
0086     array of qat4 node inverse transforms, same size as tran
0087 plan
0088     array of float4 planes
0089 
0090 
0091 
0092 TODO: Remove WITH_SGEO SGeo pure virtual base struct
0093 -----------------------------------------------------
0094 
0095 The SGeo base enables holding pointer to CSGFoundry within SEvt
0096 using SEvt::setGeo. But now that all geometry info is better accessed
0097 from stree this complication can be removed ?
0098 
0099 **/
0100 
0101 
0102 #define FRAME_TRANSITION 1
0103 
0104 #define WITH_SGEO 1
0105 
0106 #ifdef WITH_SGEO
0107 struct CSG_API CSGFoundry : public SGeo
0108 #else
0109 struct CSG_API CSGFoundry
0110 #endif
0111 {
0112     static const plog::Severity LEVEL ;
0113     static const int  VERBOSE ;
0114     static const unsigned IMAX ;
0115     static const char* BASE ;
0116     static const char* RELDIR ;
0117     static const constexpr unsigned UNDEFINED = ~0u ;
0118 
0119     const char* descELV() const ;
0120 
0121 
0122     static bool Load_saveAlt ;
0123     static CSGFoundry* CreateFromSim();
0124     static CSGFoundry* Load();
0125     static CSGFoundry* CopySelect(const CSGFoundry* src, const SBitSet* elv );
0126 
0127     //static const char* ResolveCFBase_();
0128     static const char* ResolveCFBase();
0129 
0130     static constexpr const char* _Load_DUMP = "CSGFoundry__Load_DUMP" ;
0131     static CSGFoundry* Load_();
0132     static CSGFoundry* Load(const char* base, const char* rel=RELDIR );
0133 
0134     void setOverrideSim( const SSim* ssim );
0135     const SSim* getSim() const ;
0136 
0137     // TODO: farm off comparison machinery into another struct
0138 
0139     static int Compare(const CSGFoundry* a , const CSGFoundry* b );
0140     static int WIP_CompareStruct(const CSGFoundry* a , const CSGFoundry* b );
0141     static std::string DescCompare( const CSGFoundry* a, const CSGFoundry* b );
0142 
0143     template<typename T>
0144     static int CompareVec( const char* name, const std::vector<T>& a, const std::vector<T>& b );
0145 
0146 
0147     template<typename T>
0148     static int CompareStruct( const char* name, const std::vector<T>& a, const std::vector<T>& b );
0149 
0150     static int CompareFloat4( const char* name, const std::vector<float4>& aa, const std::vector<float4>& bb );
0151     static bool Float4_IsDiff( const float4& a , const float4& b );
0152 
0153 
0154     static int CompareBytes(const void* a, const void* b, unsigned num_bytes);
0155 
0156     static CSGFoundry* INSTANCE ;
0157     static CSGFoundry* Get() ;
0158 
0159     static constexpr const char* SAVE_OPT = "CSGFoundry__SAVE_OPT" ;
0160     CSGFoundry();
0161     void init();
0162 
0163     const char* getFold() const ;
0164     void setFold(const char* fold);
0165     void setGeom(const char* geom);
0166     void setOrigin(const CSGFoundry* origin);
0167     void setElv(const SBitSet* elv);
0168 
0169     std::string brief() const ;
0170     std::string desc() const ;
0171     std::string descBase() const ;
0172     std::string descComp() const ;
0173     std::string descSolid() const ;
0174     std::string descMeshName() const ;
0175     std::string descGAS() const ;
0176 
0177     void summary(const char* msg="CSGFoundry::summary") const ;
0178     std::string descSolids() const ;
0179     std::string descInst(unsigned ias_idx_, unsigned long long emm=~0ull ) const ;
0180 
0181     std::string descInstance() const ;   // IDX=0,10,100,1000,10000
0182     std::string descInstance(unsigned idx) const ;
0183 
0184     void dump() const ;
0185     void dumpSolid() const ;
0186     void dumpSolid(unsigned solidIdx ) const ;
0187     int findSolidIdx(const char* label) const  ; // -1 if not found
0188     void findSolidIdx(std::vector<unsigned>& solid_idx, const char* label) const ;
0189     std::string descSolidIdx( const std::vector<unsigned>& solid_selection ) ;
0190 
0191     void dumpPrim() const ;
0192     void dumpPrim(unsigned solidIdx ) const ;
0193 
0194 
0195     std::string descPrim() const ;
0196     std::string descPrim(unsigned solidIdx) const  ;
0197 
0198     int getPrimBoundary(unsigned primIdx) const ;
0199     int getPrimBoundary_( const CSGPrim* pr ) const ;
0200 
0201     void setPrimBoundary(unsigned primIdx, unsigned boundary) ;
0202 
0203     std::string detailPrim() const ;
0204     std::string detailPrim(unsigned primIdx) const ;
0205 
0206     std::string descPrimSpec() const ;
0207     std::string descPrimSpec(unsigned solidIdx) const ;
0208 
0209 
0210     void dumpNode() const ;
0211     void dumpNode(unsigned solidIdx ) const ;
0212     std::string descNode() const ;
0213     std::string descNode(unsigned solidIdx) const  ;
0214     std::string descTran(unsigned solidIdx) const  ;
0215 
0216 
0217     AABB iasBB(unsigned ias_idx_, unsigned long long emm=0ull ) const ;
0218     float4 iasCE(unsigned ias_idx_, unsigned long long emm=0ull ) const;
0219     void   iasCE(float4& ce, unsigned ias_idx_, unsigned long long emm=0ull ) const;
0220     void   gasCE(float4& ce, unsigned gas_idx ) const ;
0221     void   gasCE(float4& ce, const std::vector<unsigned>& gas_idxs ) const ;
0222 
0223     float  getMaxExtent(const std::vector<unsigned>& solid_selection) const ;
0224     std::string descSolids(const std::vector<unsigned>& solid_selection) const ;
0225 
0226 
0227     SCSGPrimSpec getPrimSpec(       unsigned solidIdx) const ;
0228     SCSGPrimSpec getPrimSpecHost(   unsigned solidIdx) const ;
0229     SCSGPrimSpec getPrimSpecDevice( unsigned solidIdx) const ;
0230     void        checkPrimSpec(     unsigned solidIdx) const ;
0231     void        checkPrimSpec() const ;
0232 
0233 
0234     const CSGSolid*   getSolidByName(const char* name) const ;
0235     const CSGSolid*   getSolid_(int solidIdx) const ;   // -ve counts from back
0236     unsigned          getSolidIdx(const CSGSolid* so) const ;
0237 
0238     const char* getSolidLabel_(int ridx) const ;
0239     char getSolidIntent(int ridx) const ;
0240     std::string descSolidIntent() const ;
0241 
0242 
0243     unsigned getNumSolid(int type_) const ;
0244     unsigned getNumSolid() const;        // STANDARD_SOLID count
0245     unsigned getNumSolidTotal() const;   // all solid count
0246 
0247     unsigned getNumPrim() const;
0248     unsigned getNumNode() const;
0249     unsigned getNumPlan() const;
0250     unsigned getNumTran() const;
0251     unsigned getNumItra() const;
0252     unsigned getNumInst() const;
0253 
0254     const CSGSolid*   getSolid(unsigned solidIdx) const ;
0255     const CSGPrim*    getPrim(unsigned primIdx) const ;
0256     const CSGNode*    getNode(unsigned nodeIdx) const ;
0257     const float4*     getPlan(unsigned planIdx) const ;
0258     const qat4*       getTran(unsigned tranIdx) const ;
0259     const qat4*       getItra(unsigned itraIdx) const ;
0260     const qat4*       getInst(unsigned instIdx) const ;
0261 
0262     CSGNode*          getNode_(unsigned nodeIdx);
0263 
0264 
0265     void              getNodePlanes(std::vector<float4>& planes, const CSGNode* nd) const ;
0266     const CSGPrim*    getSolidPrim(unsigned solidIdx, unsigned primIdxRel) const ;
0267     const CSGNode*    getSolidPrimNode(unsigned solidIdx, unsigned primIdxRel, unsigned nodeIdxRel) const ;
0268 
0269     void getMeshPrimCopies(  std::vector<CSGPrim>& select_prim, unsigned mesh_idx ) const ;
0270     void getMeshPrimPointers(std::vector<const CSGPrim*>& select_prim, unsigned mesh_idx ) const ;
0271     const CSGPrim* getMeshPrim( unsigned midx, unsigned mord ) const ;
0272 
0273     unsigned getNumMeshPrim(unsigned mesh_idx ) const ;
0274     std::string descMeshPrim() const ;
0275     std::string descPrimRange() const ;
0276     std::string descPrimRange(int solidIdx) const ;
0277     std::string comparePrimRange(int solidIdx, const SMeshGroup* mg) const ;
0278 
0279 
0280 
0281 
0282     unsigned getNumSelectedPrimInSolid(const CSGSolid* solid, const SBitSet* elv ) const ;
0283 
0284 
0285     CSGSolid* addSolid(unsigned num_prim, const char* label, int primOffset_ = -1 );
0286 
0287     int       addPrim_solidLocalPrimIdx() const ;
0288     CSGPrim*  addPrim(int num_node, int nodeOffset_ = -1 );
0289 
0290     int       addNode_solidLocalNodeIdx() const ;
0291     CSGNode*  addNode(CSGNode nd);
0292     CSGNode*  addNode();
0293     CSGNode*  addNode(CSGNode nd, const std::vector<float4>* pl, const Tran<double>* tr );
0294     CSGNode*  addNodes(const std::vector<CSGNode>& nds );
0295 
0296     CSGNode*  addNode(AABB& bb, CSGNode nd );
0297     CSGNode*  addNodes(AABB& bb, std::vector<CSGNode>& nds, const std::vector<const Tran<double>*>* trs  );
0298 
0299     CSGPrim*  addPrimNodes(AABB& bb, const std::vector<CSGNode>& nds, const std::vector<const Tran<double>*>* trs=nullptr );
0300 
0301 
0302     float4*   addPlan(const float4& pl );
0303 
0304 
0305     CSGSolid* addDeepCopySolid(unsigned solidIdx, const char* label=nullptr );
0306 
0307 
0308     template<typename T> unsigned addTran( const Tran<T>* tr  );
0309     template<typename T> unsigned addTran_( const Tran<T>* tr  );
0310     unsigned addTran( const qat4* tr, const qat4* it ) ;
0311     unsigned addTran() ;
0312     void     addTranPlaceholder();
0313 
0314     // adds transform and associates it with the node
0315     template<typename T> const qat4* addNodeTran(CSGNode* nd, const Tran<T>* tr, bool transform_node_aabb  );
0316     void addNodeTran(CSGNode* nd );
0317 
0318 
0319     void addInstance(const float* tr16, int gas_idx, int sensor_identifier, int sensor_index, bool firstcall );
0320     void addInstanceVector(const std::vector<glm::tmat4x4<float>>& v_inst_f4 );
0321     void addInstancePlaceholder();
0322 
0323 
0324     // via maker
0325     void makeDemoSolids();
0326     void importSim();
0327 
0328 
0329     CSGSolid* make(const char* name);
0330 
0331 
0332     static void DumpAABB(const char* msg, const float* aabb);
0333 
0334 
0335     const char* getBaseDir(bool create) const ;
0336 
0337     void save_(const char* dir) const ;
0338     bool save__(const char* elem) const ;
0339     void setSaveOpt(const char* save_opt_);
0340     const char* getSaveOpt() const ;
0341 
0342     void save(const char* base, const char* rel=nullptr ) const ;
0343     void saveAlt() const ;
0344 
0345     // these argumentless methods require CFBASE envvar or geom member to be set
0346     void save() const ;
0347     void load() ;
0348 
0349     static const char* load_FAIL_base_null_NOTES ;
0350     static const char* LoadFailNotes();
0351 
0352     void load( const char* base, const char* rel ) ;
0353     void setCFBase( const char* cfbase_ );
0354     const char* getCFBase() const ;
0355     const char* getOriginCFBase() const ;
0356 
0357     static const char* LOAD_FAIL_NOTES ;
0358     void load( const char* dir ) ;
0359     NP* loadAux(const char* auxrel="Values/values.npy" ) const ;
0360 
0361     static int MTime(const char* dir, const char* fname_);
0362 
0363 
0364     template<typename T> void loadArray( std::vector<T>& vec, const char* dir, const char* name, bool optional=false );
0365 
0366     void upload();
0367     bool isUploaded() const ;
0368 
0369     void inst_find_unique();
0370     unsigned getNumUniqueGAS() const ;
0371     unsigned getNumUniqueIAS() const ;
0372     /*
0373     unsigned getNumUniqueINS() const ;
0374     */
0375 
0376     unsigned getNumInstancesIAS(int ias_idx, unsigned long long emm) const ;
0377     void     getInstanceTransformsIAS(std::vector<qat4>& select_inst, int ias_idx, unsigned long long emm ) const ;
0378 
0379     unsigned getNumInstancesGAS(int gas_idx) const ;
0380     void     getInstanceTransformsGAS(std::vector<qat4>&  select_qv, int gas_idx ) const ;  // collecting by value : TODO eliminate, swiching to getInstancePointersGAS
0381     void     getInstancePointersGAS(  std::vector<const qat4*>& select_qi, int gas_idx ) const ;  // collecting pointers to the actual instances
0382 
0383     int       getInstanceIndex(int gas_idx_ , unsigned ordinal) const ;
0384     const qat4* getInstance_with_GAS_ordinal(int gas_idx_ , unsigned ordinal=0) const  ;
0385 
0386     // id
0387     void parseMOI(int& midx, int& mord, int& iidx, const char* moi) const ;
0388     const char* getName(unsigned midx) const ;
0389 
0390     static const char* getFrame_NOTES ;
0391     sframe getFrame() const ;
0392     sframe getFrame(const char* moi_or_iidx) const ;
0393 
0394     static constexpr const char* getFrame_VERBOSE = "CSGFoundry__getFrame_VERBOSE" ;
0395 
0396     int getFrame(sframe& fr, const char* frs ) const ;
0397     int getFrame(sframe& fr, int midx, int mord, int gord) const ;
0398     int getFrame(sframe& fr, int ins_idx ) const ;
0399 
0400     static constexpr const char* getFrameE_VERBOSE = "CSGFoundry__getFrameE_VERBOSE" ;
0401     sframe getFrameE() const ;
0402     static void AfterLoadOrCreate();
0403 
0404     // target
0405     int getCenterExtent(float4& ce, int midx, int mord, int gord=-1, qat4* m2w=nullptr, qat4* w2m=nullptr ) const ;
0406     int getTransform(   qat4& q   , int midx, int mord, int gord=-1) const ;
0407 
0408 
0409     template <typename T> void setMeta( const char* key, T value );
0410     template <typename T> T    getMeta( const char* key, T fallback);
0411     bool hasMeta() const ;
0412 
0413     void kludgeScalePrimBBox( const char* label, float dscale );
0414     void kludgeScalePrimBBox( unsigned solidIdx, float dscale );
0415 
0416     unsigned getNumMeshName() const ;
0417     unsigned getNumSolidLabel() const ;
0418     int findSolidWithLabel(const char* q_mml) const ;
0419     bool isSolidTrimesh_posthoc_kludge(int gas_idx) const ;  // see SGeoConfig::SolidTrimesh
0420     bool isSolidTrimesh(int gas_idx) const ;  // see notes/issues/flexible_forced_triangulation.rst
0421 
0422     static void CopyNames(    CSGFoundry* dst, const CSGFoundry* src );
0423     static void CopyMeshName( CSGFoundry* dst, const CSGFoundry* src );
0424 
0425     void getMeshName( std::vector<std::string>& mname ) const ;
0426     void getPrimName( std::vector<std::string>& pname ) const ;
0427 
0428     // SGeo protocol
0429     unsigned getNumMeshes() const ;
0430     const char* getMeshName(unsigned midx) const ;
0431     int getMeshIndexWithName(const char* name, bool startswith) const ;
0432     int lookup_mtline(int mtindex) const ;
0433     std::string desc_mt() const ;
0434 
0435     stree* getTree() const ;  // from contained (const SSim*)sim
0436     SScene* getScene() const ;
0437     void setOverrideScene(SScene* _scene);
0438 
0439 
0440     int findMeshIndex(const char* qname) const ;
0441 
0442     const std::string descELV2(const SBitSet* elv) const ;
0443     const std::string descELV(const SBitSet* elv) const ;
0444 
0445     const std::string& getSolidMMLabel(unsigned sidx) const ;
0446     void addSolidMMLabel(const char* label);
0447     void addMeshName(const char* name);
0448 
0449     // MEMBERS
0450 
0451 
0452     // HMM "meshname" is historical, should be "lvname" or "soname" ?
0453     std::vector<std::string> meshname ;  // GGeo::getMeshNames/GMeshLib (G4VSolid names from Geant4) should be primName in CF model ?
0454     std::vector<std::string> mmlabel ;   // from GGeo::getMergedMeshLabels eg of form "3084:sWorld" "7:HamamatsuR12860sMask_virtual"
0455 
0456     std::vector<CSGSolid>  solid ;
0457     std::vector<CSGPrim>   prim ;
0458     std::vector<CSGNode>   node ;
0459     std::vector<float4>    plan ;
0460     std::vector<qat4>      tran ;
0461     std::vector<qat4>      itra ;
0462     std::vector<qat4>      inst ;
0463 
0464 
0465     CSGPrim*    d_prim ;
0466     CSGNode*    d_node ;
0467     float4*     d_plan ;
0468     qat4*       d_itra ;
0469 
0470 
0471     std::vector<int> gas ;
0472 
0473 
0474     const SSim* sim ;
0475     CSGImport*  import ;
0476     SName*    id ;   // holds the meshname vector of G4VSolid names
0477 
0478     CSGTarget*  target ;
0479     CSGMaker*   maker ;
0480     bool        deepcopy_everynode_transform ;
0481 
0482 
0483     CSGSolid*   last_added_solid ;
0484     CSGPrim*    last_added_prim ;
0485     CSGNode*    last_added_node ;
0486 
0487 
0488     void setPrimBoundary(unsigned primIdx, const char* bname) ;
0489     int  mtime ;
0490 
0491     std::string meta ;
0492     const char* fold ;
0493     const char* cfbase ;
0494     const char* geom ;
0495     const char* loaddir ;
0496 
0497     const CSGFoundry* origin ;
0498     const SBitSet*    elv ;
0499     const char* save_opt ;
0500 };
0501