Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4KDNode.hh
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 //
26 // $Id: G4KDNode.hh 102637 2017-02-10 11:08:17Z gcosmo $
27 //
28 // Author: Mathieu Karamitros
29 
30 // The code is developed in the framework of the ESA AO7146
31 //
32 // We would be very happy hearing from you, send us your feedback! :)
33 //
34 // In order for Geant4-DNA to be maintained and still open-source,
35 // article citations are crucial.
36 // If you use Geant4-DNA chemistry and you publish papers about your software,
37 // in addition to the general paper on Geant4-DNA:
38 //
39 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178
40 //
41 // we would be very happy if you could please also cite the following
42 // reference papers on chemistry:
43 //
44 // J. Comput. Phys. 274 (2014) 841-882
45 // Prog. Nucl. Sci. Tec. 2 (2011) 503-508
46 
47 #ifndef G4KDNODE_HH
48 #define G4KDNODE_HH
49 
50 #include <list>
51 //#include <map>
52 #include <vector>
53 #include <deque>
54 #include <ostream>
55 
56 // geant4
57 #include <G4Allocator.hh>
58 
59 #include "globals.hh"
60 #include <ostream>
61 
62 class G4KDTree;
63 class G4KDMap;
64 
66 {
67 public:
68  //----------------------------
69  // For root node :
70  // parent = 0, axis = 0, side = 0
71  G4KDNode_Base(G4KDTree*, G4KDNode_Base* /*parent*/);
72  virtual ~G4KDNode_Base();
73  virtual double operator[](size_t) const = 0;
74  virtual void InactiveNode();
75  virtual bool IsValid() const{ return true; }
76 
77  //----------------------------
78  inline G4KDTree* GetTree() const {return fTree;}
79  inline void SetTree(G4KDTree* tree) {fTree = tree;}
80 
81  //----------------------------
82  int GetDim() const;
83  inline int GetAxis() const{return fAxis;}
84  inline G4KDNode_Base* GetParent(){return fParent;}
85  inline G4KDNode_Base* GetLeft(){return fLeft;}
86  inline G4KDNode_Base* GetRight(){return fRight;}
87 
88  //----------------------------
89  template<typename Position>
90  G4KDNode_Base* FindParent(const Position& x0);
91 
92  template<typename PointT>
93  G4KDNode_Base* Insert(PointT* point);
94  template<typename PointT>
95  G4KDNode_Base* Insert(const PointT& point);
96  int Insert(G4KDNode_Base* newNode);
97 
98  void PullSubTree();
99  void RetrieveNodeList(std::list<G4KDNode_Base*>& node_list);
100 
101  void Print(std::ostream& out, int level = 0) const;
102 
103 // std::vector<std::deque<G4KDNode_Base*>::iterator>*
104 // GetIteratorsForSortingAlgo(G4KDMap*);
105 // std::map<G4KDMap*, std::vector<std::deque<G4KDNode_Base*>::iterator>>*
106 // fpIteratorInSortingAlgo;
107 
108 protected:
109  //°°°°°°°°°°°
110  // Members
111  //°°°°°°°°°°°
112  size_t fAxis; // axis : x, y, z ...
113  int fSide; // left/right
114  /* fSide == 0 : Is the root node
115  * fSide == -1 : It is the left of the parent node
116  * fSide == 1 : It is the right of the parent node
117  */
118 
121  /* Left : fLeft->fPosition[axis] < this->fPosition[axis]
122  * Right : fRight->fPosition[axis] > this->fPosition[axis]
123  * Root node : fParent = 0
124  */
125 private:
127  G4KDNode_Base& operator=(const G4KDNode_Base& right);
128 };
129 
135 template<typename PointT>
136  class G4KDNode : public G4KDNode_Base
137  {
138  public:
139  //----------------------------
140  // For root node :
141  // parent = 0, axis = 0, side = 0
142  G4KDNode(G4KDTree*, PointT* /*point*/, G4KDNode_Base* /*parent*/);
143  virtual ~G4KDNode();
144 
145  void *operator new(size_t);
146  void operator delete(void *);
147 
148  inline PointT* GetPoint()
149  {
150  return fPoint;
151  }
152 
153  virtual double operator[](size_t i) const
154  {
155  if(fPoint == 0) abort();
156  return (*fPoint)[i];
157  }
158 
159  virtual void InactiveNode()
160  {
161  fValid = false;
163  }
164 
165  virtual bool IsValid() const
166  {
167  return fValid;
168  }
169 
170  protected:
171  PointT* fPoint;
172  bool fValid;
173 
174  private:
176  G4KDNode& operator=(const G4KDNode<PointT>& right);
177 
178  static G4ThreadLocal G4Allocator<G4KDNode<PointT>>* fgAllocator;
179  };
180 
181 template<typename PointT>
184 
185 template<typename PointT>
186  void* G4KDNode<PointT>::operator new(size_t)
187  {
188  if(!fgAllocator) fgAllocator = new G4Allocator<G4KDNode<PointT> >;
189  return (void *) fgAllocator->MallocSingle();
190  }
191 
192 template<typename PointT>
193  void G4KDNode<PointT>::operator delete(void *aNode)
194  {
195  fgAllocator->FreeSingle((G4KDNode<PointT> *) aNode);
196  }
197 
203 template<typename PointCopyT>
205  {
206  public:
207  //----------------------------
208  // For root node :
209  // parent = 0, axis = 0, side = 0
211  const PointCopyT& point,
212  G4KDNode_Base* parent) :
213  G4KDNode_Base(tree, parent)
214  {
215  fPoint = point;
216  fValid = true;
217  }
218 
219  virtual ~G4KDNodeCopy(){}
220 
221  void *operator new(size_t)
222  {
223  if(!fgAllocator) fgAllocator = new G4Allocator<G4KDNodeCopy<PointCopyT>>;
224  return (void *) fgAllocator->MallocSingle();
225  }
226 
227  void operator delete(void* aNode)
228  {
229  fgAllocator->FreeSingle((G4KDNodeCopy<PointCopyT>*) aNode);
230  }
231 
232  inline const PointCopyT& GetPoint()
233  {
234  return fPoint;
235  }
236 
237  virtual double operator[](size_t i) const
238  {
239  return fPoint[i];
240  }
241 
242  virtual void InactiveNode()
243  {
244  fValid = false;
246  }
247 
248  virtual bool IsValid() const
249  {
250  return fValid;
251  }
252 
253  protected:
254  PointCopyT fPoint;
256 
257  private:
259  G4KDNode_Base(right), fPoint(0)
260  {
261  fValid = false;
262  }
263 
265  operator=(const G4KDNodeCopy<PointCopyT>& right)
266  {
267  if(this == &right) return *this;
268  fPoint = right.fPoint;
269  fTree = right.fTree;
270  fLeft = right.fLeft;
271  fRight = right.fRight;
272  fParent = right.fParent;
273  fSide = right.fSide;
274  fAxis = right.fAxis;
275  return *this;
276  }
277 
279  };
280 
281 template<typename PointT>
284 
285 #include "G4KDNode.icc"
286 
287 #endif // G4KDNODE_HH
Type * MallocSingle()
Definition: G4Allocator.hh:202
void SetTree(G4KDTree *tree)
Definition: G4KDNode.hh:79
virtual bool IsValid() const
Definition: G4KDNode.hh:165
G4KDNode(G4KDTree *, PointT *, G4KDNode_Base *)
G4KDNode_Base * fLeft
Definition: G4KDNode.hh:120
PointT * GetPoint()
Definition: G4KDNode.hh:148
G4bool fValid
Definition: G4KDNode.hh:255
virtual ~G4KDNodeCopy()
Definition: G4KDNode.hh:219
const PointCopyT & GetPoint()
Definition: G4KDNode.hh:232
G4KDNode_Base * Insert(PointT *point)
void FreeSingle(Type *anElement)
Definition: G4Allocator.hh:212
G4KDNode_Base * GetRight()
Definition: G4KDNode.hh:86
G4KDTree * GetTree() const
Definition: G4KDNode.hh:78
PointT * fPoint
Definition: G4KDNode.hh:171
int GetAxis() const
Definition: G4KDNode.hh:83
virtual ~G4KDNode_Base()
Definition: G4KDNode.cc:103
#define G4ThreadLocal
Definition: tls.hh:89
G4KDNodeCopy(G4KDTree *tree, const PointCopyT &point, G4KDNode_Base *parent)
Definition: G4KDNode.hh:210
void RetrieveNodeList(std::list< G4KDNode_Base * > &node_list)
Definition: G4KDNode.cc:167
void Print(std::ostream &out, int level=0) const
Definition: G4KDNode.cc:178
virtual void InactiveNode()
Definition: G4KDNode.hh:159
virtual ~G4KDNode()
tuple tree
Definition: gammaraytel.py:4
int GetDim() const
Definition: G4KDNode.cc:112
bool G4bool
Definition: G4Types.hh:79
size_t fAxis
Definition: G4KDNode.hh:112
virtual void InactiveNode()
Definition: G4KDNode.cc:107
G4KDNode_Base * FindParent(const Position &x0)
PointCopyT fPoint
Definition: G4KDNode.hh:254
virtual bool IsValid() const
Definition: G4KDNode.hh:75
G4KDNode_Base * fParent
Definition: G4KDNode.hh:120
virtual double operator[](size_t i) const
Definition: G4KDNode.hh:237
virtual double operator[](size_t) const =0
virtual void InactiveNode()
Definition: G4KDNode.hh:242
G4KDNode_Base * GetParent()
Definition: G4KDNode.hh:84
void PullSubTree()
Definition: G4KDNode.cc:147
bool fValid
Definition: G4KDNode.hh:172
virtual double operator[](size_t i) const
Definition: G4KDNode.hh:153
G4KDTree * fTree
Definition: G4KDNode.hh:119
virtual bool IsValid() const
Definition: G4KDNode.hh:248
G4KDNode_Base(G4KDTree *, G4KDNode_Base *)
Definition: G4KDNode.cc:72
G4KDNode_Base * fRight
Definition: G4KDNode.hh:120
G4KDNode_Base * GetLeft()
Definition: G4KDNode.hh:85