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