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