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