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 | // |
---|
27 | // $Id: G4GeoNav.cc,v 1.3 2006/06/29 17:22:50 gunter Exp $ |
---|
28 | // GEANT4 tag $Name: $ |
---|
29 | // |
---|
30 | // |
---|
31 | // -------------------------------------------------------------- |
---|
32 | // G4GeoNav |
---|
33 | // |
---|
34 | // Author: Martin Liendl - Martin.Liendl@cern.ch |
---|
35 | // |
---|
36 | // -------------------------------------------------------------- |
---|
37 | // |
---|
38 | #include "G4GeoNav.hh" |
---|
39 | |
---|
40 | G4GeoNav::G4GeoNav( G4LogicalVolume * rv ) |
---|
41 | : theLVTree(new LVTree(rv)) |
---|
42 | { |
---|
43 | theCurLV = theLVTree->root(); |
---|
44 | theRootLV = theLVTree->root(); |
---|
45 | RecursiveFill(theLVTree->root()); |
---|
46 | } |
---|
47 | |
---|
48 | G4GeoNav::~G4GeoNav() |
---|
49 | {} |
---|
50 | |
---|
51 | |
---|
52 | G4LogicalVolume * G4GeoNav::NextLV() |
---|
53 | { |
---|
54 | //FIXME: G4GeoNav::NextLV() |
---|
55 | |
---|
56 | TreeNodeIterator<G4LogicalVolume*> tni(theCurLV); |
---|
57 | |
---|
58 | G4LogicalVolume * v = 0; |
---|
59 | if (tni.next()) { |
---|
60 | v = tni.current()->data(); |
---|
61 | theCurLV = tni.current(); |
---|
62 | } |
---|
63 | return v; |
---|
64 | /* |
---|
65 | G4cerr << "DON'T CALL G4GeoNav::Next()!" << endl; |
---|
66 | exit(1); |
---|
67 | return 0; |
---|
68 | */ |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | G4int G4GeoNav::FilterLV(const G4String & aRegexStr, |
---|
73 | std::vector<G4LogicalVolume*> & result, |
---|
74 | G4bool stopAtFirst) |
---|
75 | { |
---|
76 | regex_t aRegex; |
---|
77 | const char * aRegexCStr = aRegexStr.data(); |
---|
78 | if (regcomp(&aRegex,aRegexCStr,0)) { |
---|
79 | G4cerr << "failed to interpret regex-string" << G4endl; |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | LVTree::node_t * aNode = theLVTree->root(); |
---|
84 | FindLV(&aRegex,aNode,result,stopAtFirst); |
---|
85 | regfree(&aRegex); |
---|
86 | return result.size(); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void G4GeoNav::FindLV(regex_t * aRegex, LVTree::node_t * node, |
---|
91 | std::vector<G4LogicalVolume*>& result, |
---|
92 | G4bool stopAtFirst) |
---|
93 | { |
---|
94 | if( !regexec(aRegex, node->data()->GetName().data(), 0,0,0)) |
---|
95 | { |
---|
96 | result.push_back(node->data()); |
---|
97 | if (stopAtFirst) |
---|
98 | { |
---|
99 | theCurLV = node; |
---|
100 | return; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | LVTree::node_t * i = node->firstChild(); |
---|
105 | while(i) { // recursive |
---|
106 | FindLV(aRegex, i, result, stopAtFirst); |
---|
107 | i = i->nextSibling(); |
---|
108 | } |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | G4int G4GeoNav::PathLV(std::vector<G4LogicalVolume*> & result) |
---|
113 | { |
---|
114 | result.push_back(theCurLV->data()); |
---|
115 | G4int level=1; |
---|
116 | LVTree::node_t * node = theCurLV; |
---|
117 | while (node->parent()) |
---|
118 | { |
---|
119 | node = node->parent(); |
---|
120 | G4LogicalVolume * aLV = node->data(); |
---|
121 | result.push_back(aLV); |
---|
122 | level++; |
---|
123 | } |
---|
124 | return level; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | G4int G4GeoNav::Tokenize(const G4String & aStr, |
---|
129 | std::vector<G4String>& tokens) |
---|
130 | { |
---|
131 | G4String::size_type c = aStr.size(); |
---|
132 | G4String::size_type idx = 0; |
---|
133 | G4String::size_type idx2 = 0; |
---|
134 | |
---|
135 | // empty string means 'root' |
---|
136 | if (!c) |
---|
137 | { |
---|
138 | tokens.push_back("/"); |
---|
139 | return 1; |
---|
140 | } |
---|
141 | |
---|
142 | // scan for '/' |
---|
143 | std::vector<G4int> pos; |
---|
144 | pos.push_back(0); // begin of input |
---|
145 | G4String sep("/"); |
---|
146 | |
---|
147 | if (aStr[idx]==sep[idx]) |
---|
148 | tokens.push_back("/"); |
---|
149 | |
---|
150 | G4String curStr = aStr; // G4String("-") ; |
---|
151 | |
---|
152 | G4String::size_type i=0; |
---|
153 | for (i=0; i<c; i++) { |
---|
154 | idx = i; |
---|
155 | if(curStr[idx]==sep[idx2]) { |
---|
156 | #ifdef QT_DEBUG_3 |
---|
157 | G4cout << i << " " << curStr[i] << G4endl; |
---|
158 | #endif |
---|
159 | pos.push_back(i); |
---|
160 | } |
---|
161 | } |
---|
162 | pos.push_back(c-1); // end of input |
---|
163 | |
---|
164 | #ifdef QT_DEBUG_3 |
---|
165 | for (G4int i=0; i<pos.size(); i++ ) |
---|
166 | G4cout << pos[i] << " "; |
---|
167 | G4cout << G4endl; |
---|
168 | #endif |
---|
169 | |
---|
170 | for (i=1; i<pos.size(); i++) |
---|
171 | { |
---|
172 | G4String newString; |
---|
173 | |
---|
174 | for(G4int j=pos[i-1]; j<=pos[i]; j++) { |
---|
175 | idx = j; |
---|
176 | if (curStr[idx]!=sep[idx2]) |
---|
177 | newString.append(curStr[idx]); |
---|
178 | } |
---|
179 | if (newString.size()) |
---|
180 | tokens.push_back(newString); |
---|
181 | } |
---|
182 | |
---|
183 | #ifdef QT_DEBUG |
---|
184 | for (G4int i=0; i<tokens.size(); i++) |
---|
185 | G4cout << "tok " << i << " " << tokens[i] << G4endl; |
---|
186 | #endif |
---|
187 | |
---|
188 | return tokens.size(); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | G4LogicalVolume * G4GeoNav::ChangeLV(const G4String & aRegExp) |
---|
193 | { |
---|
194 | std::vector<G4String> tokens; |
---|
195 | G4int c = Tokenize(aRegExp,tokens); |
---|
196 | |
---|
197 | LVTree::node_t * anItem = theCurLV; |
---|
198 | LVTree::node_t * anItemBefore = theCurLV; |
---|
199 | if (c) { |
---|
200 | |
---|
201 | std::vector<G4String>::iterator it = tokens.begin(); |
---|
202 | if (*it==G4String("/")) |
---|
203 | { // absolute or relative |
---|
204 | anItem = theRootLV; |
---|
205 | it++; |
---|
206 | } |
---|
207 | |
---|
208 | while ( it != tokens.end() ) |
---|
209 | { |
---|
210 | //regex_t aRegex; |
---|
211 | if (*it == G4String("..")) |
---|
212 | { |
---|
213 | anItem = anItem->parent(); |
---|
214 | if (!anItem) { |
---|
215 | G4cerr << "not found!" << G4endl; |
---|
216 | return 0; |
---|
217 | } |
---|
218 | it++; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | regex_t aRegex; |
---|
223 | const char * aRegexCStr = (*it).data(); |
---|
224 | if (regcomp(&aRegex,aRegexCStr,0)) |
---|
225 | { |
---|
226 | G4cerr << "failed to interpret regex-string" << G4endl; |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | G4int children = anItem->childCount(); |
---|
231 | |
---|
232 | if (!children) |
---|
233 | { |
---|
234 | G4cerr << "not found!!" << G4endl; |
---|
235 | return 0; |
---|
236 | } |
---|
237 | |
---|
238 | //LVTree::node_t * temp = *(anItem->firstChild()); |
---|
239 | //anItem = temp; |
---|
240 | |
---|
241 | // loop over children |
---|
242 | LVTree::node_t * u= anItem->firstChild(); |
---|
243 | G4bool found=false; |
---|
244 | while (u) |
---|
245 | { |
---|
246 | if ( !regexec(&aRegex, u->data()->GetName().data(),0,0,0)) |
---|
247 | { |
---|
248 | found=true; |
---|
249 | anItem = u; |
---|
250 | break; |
---|
251 | } |
---|
252 | u = u->nextSibling(); |
---|
253 | } |
---|
254 | |
---|
255 | regfree(&aRegex); |
---|
256 | if (!found) |
---|
257 | { |
---|
258 | G4cerr << "not found!!!!" << G4endl; |
---|
259 | return 0; |
---|
260 | } |
---|
261 | it++; |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|
265 | |
---|
266 | if (anItem!=anItemBefore) |
---|
267 | { |
---|
268 | theCurLV=anItem; |
---|
269 | return anItem->data(); |
---|
270 | } |
---|
271 | return 0; |
---|
272 | } |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | G4int G4GeoNav::PwdLV(std::vector<G4LogicalVolume *>& result) |
---|
277 | { |
---|
278 | result.push_back(theCurLV->data()); |
---|
279 | LVTree::node_t * temp = theCurLV; |
---|
280 | while (temp->parent()) |
---|
281 | { |
---|
282 | temp = temp->parent(); |
---|
283 | result.push_back(temp->data()); |
---|
284 | } |
---|
285 | return result.size(); |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | void G4GeoNav::RecursiveFill(LVTree::node_t* node) |
---|
290 | { |
---|
291 | G4LogicalVolume * lv = node->data(); |
---|
292 | |
---|
293 | std::set<G4LogicalVolume*> lvset; |
---|
294 | |
---|
295 | for ( G4int i=0; i<lv->GetNoDaughters(); ++i) |
---|
296 | { |
---|
297 | lvset.insert(lv->GetDaughter(i)->GetLogicalVolume()); |
---|
298 | } |
---|
299 | |
---|
300 | std::set<G4LogicalVolume*>::iterator it = lvset.begin(); |
---|
301 | for(;it!=lvset.end();++it) |
---|
302 | { |
---|
303 | LVTree::node_t * newnode = new LVTree::node_t(node, *it); |
---|
304 | //create new node '*it' in parent 'node' |
---|
305 | RecursiveFill(newnode); |
---|
306 | } |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | G4int G4GeoNav::LsLV(std::vector<G4LogicalVolume *>& result) |
---|
311 | { |
---|
312 | G4int c=0; |
---|
313 | LVTree::node_t * n = theCurLV->firstChild(); |
---|
314 | while(n) |
---|
315 | { |
---|
316 | result.push_back(n->data()); |
---|
317 | n = n->nextSibling(); |
---|
318 | c++; |
---|
319 | } |
---|
320 | return c; |
---|
321 | } |
---|