Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:39

0001 #pragma once
0002 /**
0003 SLabel.h
0004 =========
0005 
0006 After the fact lookups of mmlable indices from labels. 
0007 This was used to provide primitive "post-hoc" trimesh control 
0008 prior to implementation of proper tri control. 
0009 
0010 **/
0011 
0012 
0013 #include <algorithm>
0014 #include <vector>
0015 #include <fstream>
0016 
0017 #include "spath.h"
0018 #include "sstr.h"
0019 
0020 struct SLabel
0021 {
0022     static bool IsIdxLabelListed(const std::vector<std::string>& label, unsigned idx, const char* ls, char delim=',' );
0023 
0024     static int FindIdxWithLabel(const std::vector<std::string>& label, const char* q_mml);
0025 
0026     static SLabel* Load(const char* path); 
0027     static constexpr const char* GEOMLoadPath = "$HOME/.opticks/GEOM/$GEOM/CSGFoundry/mmlabel.txt" ;  
0028     static SLabel* GEOMLoad(); 
0029 
0030     const std::vector<std::string>& label ; 
0031 
0032     SLabel(const std::vector<std::string>& label );  
0033     int  findIdxWithLabel(const char* q_mml) const ; 
0034     void findIndicesWithListedLabels( std::vector<unsigned>& indices, const char* ls, char delim );
0035 
0036     std::string detail() const ; 
0037 }; 
0038 
0039 
0040 
0041 /**
0042 SLabel::IsIdxLabelListed
0043 -------------------------
0044 
0045 1. forms SLabel instance with the label vector
0046 2. returns true when the 0-based idx label which must be less than the number of labels is present in the delimited string 
0047 
0048 **/
0049 
0050 inline bool SLabel::IsIdxLabelListed(const std::vector<std::string>& label, unsigned idx, const char* ls, char delim )
0051 {
0052     assert( idx < label.size() ); 
0053     if(ls == nullptr) return false ; 
0054 
0055     std::vector<unsigned> indices ; 
0056     SLabel lab(label); 
0057     lab.findIndicesWithListedLabels( indices, ls, delim); 
0058 
0059     bool found = std::find( indices.begin(), indices.end(), idx) != indices.end() ; 
0060     return found ; 
0061 }
0062 
0063 
0064 
0065 
0066 SLabel* SLabel::Load(const char* path_)
0067 {
0068     const char* path = spath::Resolve(path_); 
0069     if(path == nullptr) 
0070     {
0071         std::cerr 
0072             << "SLabel::Load FAILED to spath::Resolve [" 
0073             << ( path_ ? path_ : "-" ) 
0074             << std::endl
0075             ; 
0076         return nullptr ; 
0077     }
0078 
0079     typedef std::vector<std::string> VS ; 
0080     VS* label = new VS ; 
0081 
0082     std::ifstream ifs(path);
0083     std::string line;
0084     while(std::getline(ifs, line)) label->push_back(line) ; 
0085 
0086     SLabel* id = new SLabel(*label) ; 
0087     return id ;
0088 }
0089 
0090 inline SLabel* SLabel::GEOMLoad(){ return Load(GEOMLoadPath); }
0091 
0092 
0093 inline SLabel::SLabel( const std::vector<std::string>& label_ )
0094     :
0095     label(label_)
0096 {
0097 }
0098 
0099 
0100 inline int SLabel::FindIdxWithLabel(const std::vector<std::string>& label, const char* q_mml)
0101 {
0102     SLabel lab(label); 
0103     return lab.findIdxWithLabel(q_mml); 
0104 }
0105 
0106 
0107 
0108 /**
0109 SLabel::findIdxWithLabel
0110 --------------------------------
0111 
0112 Returns the 0-based index of CSGSolid aka the mmlabel, for example::
0113 
0114     [blyth@localhost ~]$ GEOM cf
0115     cd /home/blyth/.opticks/GEOM/J23_1_0_rc3_ok0/CSGFoundry
0116 
0117     [blyth@localhost CSGFoundry]$ cat mmlabel.txt 
0118     2923:sWorld
0119     5:PMT_3inch_pmt_solid
0120     9:NNVTMCPPMTsMask_virtual
0121     12:HamamatsuR12860sMask_virtual
0122     6:mask_PMT_20inch_vetosMask_virtual
0123     1:sStrutBallhead
0124     1:base_steel
0125     1:uni_acrylic1
0126     130:sPanel
0127     [blyth@localhost CSGFoundry]$ 
0128 
0129 **/
0130 
0131 inline int SLabel::findIdxWithLabel(const char* q_mml) const
0132 {
0133     int idx = -1 ; 
0134     for(int i=0 ; i < int(label.size()) ; i++) 
0135     {    
0136         const char* mml = label[i].c_str(); 
0137         if(strcmp(q_mml, mml) == 0 )
0138         {
0139             idx = i ;  
0140             break ;    
0141         }
0142     }    
0143     return idx ; 
0144 }
0145 
0146 /**
0147 SLabel::findIndicesWithListedLabels
0148 ------------------------------------
0149 
0150 Populates *indices* vector with 0-based idx of the delimited sub-strings from *ls*
0151 that are present in the label vector. 
0152 
0153 **/
0154 
0155 inline void SLabel::findIndicesWithListedLabels( std::vector<unsigned>& indices, const char* ls, char delim )
0156 {
0157     std::vector<std::string> elem ; 
0158     sstr::Split( ls, delim, elem ); 
0159     for(unsigned i=0 ; i < label.size() ; i++)
0160     {   
0161          const char* l = label[i].c_str() ;
0162          for(unsigned j=0 ; j < elem.size() ; j++)
0163          {
0164              const char* e = elem[j].c_str(); 
0165              if(strcmp(l,e) == 0) indices.push_back(i) ;  
0166          }
0167     }
0168 }
0169 
0170 
0171 inline std::string SLabel::detail() const 
0172 {
0173     std::stringstream ss ; 
0174     ss << "SLabel::detail num_name " << label.size() << std::endl ;
0175     for(unsigned i=0 ; i < label.size() ; i++) ss << label[i] << std::endl ;  
0176     std::string str = ss.str(); 
0177     return str ; 
0178 }
0179 
0180