File indexing completed on 2026-04-09 07:49:39
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
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
0043
0044
0045
0046
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
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
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
0148
0149
0150
0151
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