Geant4 Cross Reference |
1 // -*- C++ -*- << 2 // 1 // 3 // ******************************************* 2 // ******************************************************************** 4 // * License and Disclaimer 3 // * License and Disclaimer * 5 // * 4 // * * 6 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 7 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 8 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 9 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 10 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 11 // * 10 // * * 12 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 13 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 14 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 15 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 16 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 17 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 18 // * 17 // * * 19 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 20 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 21 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 22 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 23 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 24 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 25 // ******************************************* 24 // ******************************************************************** 26 // 25 // 27 // History: 26 // History: 28 // 20.10.2009 G.Barrand : creation. 27 // 20.10.2009 G.Barrand : creation. 29 // 28 // 30 29 31 #include "HepPolyhedronProcessor.h" 30 #include "HepPolyhedronProcessor.h" 32 31 33 #include "globals.hh" 32 #include "globals.hh" 34 33 35 #include <list> << 34 HepPolyhedronProcessor::HepPolyhedronProcessor(){} 36 << 35 HepPolyhedronProcessor::~HepPolyhedronProcessor(){} 37 HepPolyhedronProcessor::HepPolyhedronProcessor << 38 HepPolyhedronProcessor::~HepPolyhedronProcesso << 39 //private for the moment. 36 //private for the moment. 40 HepPolyhedronProcessor::HepPolyhedronProcessor 37 HepPolyhedronProcessor::HepPolyhedronProcessor(const HepPolyhedronProcessor&){} 41 HepPolyhedronProcessor& HepPolyhedronProcessor 38 HepPolyhedronProcessor& HepPolyhedronProcessor::operator=(const HepPolyhedronProcessor&){return *this;} 42 39 43 //public 40 //public 44 void HepPolyhedronProcessor::push_back(Operati 41 void HepPolyhedronProcessor::push_back(Operation a_op,const HepPolyhedron& a_polyhedron) { 45 m_ops.emplace_back(a_op,a_polyhedron); << 42 m_ops.push_back(op_t(a_op,a_polyhedron)); 46 } << 47 << 48 void HepPolyhedronProcessor::clear() << 49 { << 50 m_ops.clear(); << 51 } 43 } >> 44 void HepPolyhedronProcessor::clear() { m_ops.clear();} 52 45 53 bool HepPolyhedronProcessor::is_same_op() cons << 46 bool HepPolyhedronProcessor::is_same_op() const { 54 { << 47 if(!m_ops.size()) return true; 55 if(m_ops.empty()) return true; << 56 Operation op = m_ops[0].first; 48 Operation op = m_ops[0].first; 57 std::vector<op_t>::const_iterator it; 49 std::vector<op_t>::const_iterator it; 58 for(it=m_ops.begin();it!=m_ops.end();++it) { 50 for(it=m_ops.begin();it!=m_ops.end();++it) { 59 if((*it).first!=op) return false; 51 if((*it).first!=op) return false; 60 } 52 } 61 return true; 53 return true; 62 } 54 } 63 55 >> 56 #include <list> >> 57 64 static bool is_in(unsigned int a_index, 58 static bool is_in(unsigned int a_index, 65 const std::list<unsigned int << 59 const std::list<unsigned int>& a_is) { 66 { << 60 std::list<unsigned int>::const_iterator it; 67 for(auto it=a_is.cbegin(); it!=a_is.cend(); << 61 for(it=a_is.begin();it!=a_is.end();++it) { 68 if(*it==a_index) return true; 62 if(*it==a_index) return true; 69 } 63 } 70 return false; 64 return false; 71 } 65 } 72 << 66 static void dump(const std::vector<unsigned int>& a_is) { 73 static void dump(const std::vector<unsigned in << 67 unsigned int number = a_is.size(); 74 { << 68 for(unsigned int index=0;index<number;index++) { 75 std::size_t number = a_is.size(); << 76 for(std::size_t index=0; index<number; ++ind << 77 G4cout << ' ' << a_is[index]; 69 G4cout << ' ' << a_is[index]; 78 } 70 } 79 G4cout << G4endl; 71 G4cout << G4endl; 80 } 72 } 81 73 82 namespace HEPVis << 74 namespace HEPVis { 83 { << 75 class bijection_visitor { 84 class bijection_visitor << 76 public: 85 { << 77 typedef std::vector<unsigned int> is_t; 86 public: << 78 virtual bool visit(const is_t&) = 0; 87 << 79 public: 88 using is_t = std::vector<unsigned int>; << 80 bijection_visitor(unsigned int a_number):m_number(a_number){} 89 virtual bool visit(const is_t&) = 0; << 81 bool visitx() { 90 << 82 m_is.resize(m_number,0); 91 bijection_visitor(unsigned int a_number) << 83 std::list<unsigned int> is; 92 bool visitx() << 84 return visit(0,is); 93 { << 85 } 94 m_is.resize(m_number,0); << 86 private: 95 std::list<unsigned int> is; << 87 bool visit(unsigned int a_level,std::list<unsigned int>& a_is) { 96 return visit(0,is); << 88 for(unsigned int index=0;index<m_number;index++) { 97 } << 89 if(is_in(index,a_is)) { 98 << 90 } else { 99 private: << 91 a_is.push_back(index); 100 << 92 m_is[a_level] = index; 101 bool visit(unsigned int a_level,std::lis << 93 if(a_level==m_number-1) { 102 { << 94 if(!visit(m_is)) return false; 103 for(unsigned int index=0; index<m_numb << 95 } else { 104 if(is_in(index,a_is)) { << 96 if(!visit(a_level+1,a_is)) return false; 105 } << 106 else { << 107 a_is.push_back(index); << 108 m_is[a_level] = index; << 109 if(a_level==m_number-1) { << 110 if(!visit(m_is)) return false; << 111 } else { << 112 if(!visit(a_level+1,a_is)) retur << 113 } << 114 a_is.pop_back(); << 115 } << 116 } 97 } 117 return true; << 98 a_is.pop_back(); 118 } 99 } >> 100 } >> 101 return true; >> 102 } >> 103 private: >> 104 unsigned int m_number; >> 105 is_t m_is; >> 106 }; 119 107 120 private: << 108 class bijection_dump : public HEPVis::bijection_visitor { 121 << 109 public: 122 unsigned int m_number; << 110 bijection_dump(unsigned int a_number) 123 is_t m_is; << 111 : HEPVis::bijection_visitor(a_number) 124 }; << 112 {} 125 << 113 virtual bool visit(const is_t& a_is) { 126 class bijection_dump : public HEPVis::biject << 114 dump(a_is); 127 { << 115 return true;//continue 128 public: << 116 } 129 << 117 }; 130 bijection_dump(unsigned int a_number) << 131 : HEPVis::bijection_visitor(a_number) << 132 {} << 133 << 134 bool visit(const is_t& a_is) override << 135 { << 136 dump(a_is); << 137 return true; //continue << 138 } << 139 }; << 140 118 141 } //namespace HEPVis 119 } //namespace HEPVis 142 120 143 class HepPolyhedron_exec : public HEPVis::bije << 121 class HepPolyhedron_exec : public HEPVis::bijection_visitor { 144 { << 122 public: 145 public: << 123 HepPolyhedron_exec(unsigned int a_number, 146 HepPolyhedron_exec(unsigned int a_number, << 124 HepPolyhedronProcessor& a_proc, 147 HepPolyhedronProcessor& a_proc, << 125 HepPolyhedron& a_poly) 148 HepPolyhedron& a_poly) << 126 : HEPVis::bijection_visitor(a_number) 149 : HEPVis::bijection_visitor(a_number) ,m << 127 ,m_proc(a_proc) 150 {} << 128 ,m_poly(a_poly) 151 << 129 {} 152 bool visit(const is_t& a_is) override << 130 virtual bool visit(const is_t& a_is) { 153 { << 131 if(m_proc.execute1(m_poly,a_is)==true) return false; //stop 154 if(m_proc.execute1(m_poly,a_is)) return << 132 return true;//continue 155 return true;//continue << 133 } 156 } << 134 private: 157 << 135 HepPolyhedronProcessor& m_proc; 158 private: << 136 HepPolyhedron& m_poly; 159 << 160 HepPolyhedronProcessor& m_proc; << 161 HepPolyhedron& m_poly; << 162 }; 137 }; 163 138 164 bool HepPolyhedronProcessor::execute(HepPolyhe << 139 bool HepPolyhedronProcessor::execute(HepPolyhedron& a_poly) { 165 { << 140 //{for(unsigned int index=0;index<5;index++) { 166 //{for(unsigned int index=0; index<5; ++inde << 167 // printf("debug : bijection : %d\n",index) 141 // printf("debug : bijection : %d\n",index); 168 // HEPVis::bijection_dump bd(index); 142 // HEPVis::bijection_dump bd(index); 169 // bd.visitx(); 143 // bd.visitx(); 170 //}} 144 //}} 171 145 172 HepPolyhedron_exec e((unsigned int)m_ops.siz << 146 HepPolyhedron_exec e(m_ops.size(),*this,a_poly); 173 if(!e.visitx()) return true; 147 if(!e.visitx()) return true; 174 //std::cerr << "HepPolyhedronProcessor::exec 148 //std::cerr << "HepPolyhedronProcessor::execute :" 175 // << " all shifts and combinatory 149 // << " all shifts and combinatory tried." 176 // << " Boolean operations failed." 150 // << " Boolean operations failed." 177 // << std::endl; 151 // << std::endl; 178 return false; 152 return false; 179 } 153 } 180 154 181 bool HepPolyhedronProcessor::execute1(HepPolyh << 155 bool HepPolyhedronProcessor::execute1( 182 const st << 156 HepPolyhedron& a_poly 183 { << 157 ,const std::vector<unsigned int>& a_is >> 158 ) { 184 HepPolyhedron result(a_poly); 159 HepPolyhedron result(a_poly); 185 std::size_t number = m_ops.size(); << 160 unsigned int number = m_ops.size(); 186 int num_shift = BooleanProcessor::get_num_sh 161 int num_shift = BooleanProcessor::get_num_shift(); 187 for(int ishift=0; ishift<num_shift; ++ishift << 162 for(int ishift=0;ishift<num_shift;ishift++) { 188 BooleanProcessor::set_shift(ishift); 163 BooleanProcessor::set_shift(ishift); >> 164 189 result = a_poly; 165 result = a_poly; 190 bool done = true; 166 bool done = true; 191 for(std::size_t index=0; index<number; ++i << 167 for(unsigned int index=0;index<number;index++) { 192 BooleanProcessor processor; //take a fre 168 BooleanProcessor processor; //take a fresh one. 193 const op_t& elem = m_ops[a_is[index]]; 169 const op_t& elem = m_ops[a_is[index]]; 194 int err; 170 int err; 195 result = processor.execute(elem.first,re 171 result = processor.execute(elem.first,result,elem.second,err); 196 if(err != 0) { << 172 if(err) { 197 done = false; 173 done = false; 198 break; 174 break; 199 } 175 } 200 } 176 } 201 if(done) { 177 if(done) { 202 a_poly = std::move(result); << 178 a_poly = result; 203 return true; 179 return true; 204 } 180 } 205 } 181 } 206 182 207 //std::cerr << "HepPolyhedronProcessor::exec 183 //std::cerr << "HepPolyhedronProcessor::execute :" 208 // << " all shifts tried. Boolean o 184 // << " all shifts tried. Boolean operations failed." 209 // << std::endl; 185 // << std::endl; 210 186 211 //a_poly = result; 187 //a_poly = result; 212 return false; 188 return false; 213 } 189 }