Geant4  10.02.p01
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 93883 2015-11-03 08:25:04Z gcosmo $
27 //
28 // Author: Mathieu Karamitros, kara@cenbg.in2p3.fr
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 <G4ThreeVector.hh>
55 #include <G4Allocator.hh>
56 
57 #include "globals.hh"
58 #include <ostream>
59 
60 class G4KDTree;
61 class G4KDMap;
62 
64 {
65 public:
66  //________________________
67  // For root node :
68  // parent = 0, axis = 0, side = 0
69  G4KDNode_Base(G4KDTree*, G4KDNode_Base* /*parent*/);
70  virtual ~G4KDNode_Base();
71  virtual double operator[](size_t) const = 0;
72  virtual void InactiveNode();
73  virtual bool IsValid() const{ return true; }
74 
75  //________________________
76  inline G4KDTree* GetTree() const {return fTree;}
77  inline void SetTree(G4KDTree* tree) {fTree = tree;}
78 
79  //________________________
80  int GetDim() const;
81  inline int GetAxis() const{return fAxis;}
82  inline G4KDNode_Base* GetParent(){return fParent;}
83  inline G4KDNode_Base* GetLeft(){return fLeft;}
84  inline G4KDNode_Base* GetRight(){return fRight;}
85 
86  //________________________
87  template<typename Position>
88  G4KDNode_Base* FindParent(const Position& x0);
89 
90  template<typename PointT>
91  G4KDNode_Base* Insert(PointT* point);
92  template<typename PointT>
93  G4KDNode_Base* Insert(const PointT& point);
94  int Insert(G4KDNode_Base* newNode);
95 
96  void PullSubTree();
97  void RetrieveNodeList(std::list<G4KDNode_Base*>& node_list);
98 
99  void Print(std::ostream& out, int level = 0) const;
100 
101 // std::vector<std::deque<G4KDNode_Base* >::iterator >* GetIteratorsForSortingAlgo(G4KDMap*);
102 // std::map<G4KDMap*, std::vector<std::deque<G4KDNode_Base* >::iterator > >* fpIteratorInSortingAlgo;
103 
104 protected:
105  //°°°°°°°°°°°
106  // Members
107  //°°°°°°°°°°°
108  size_t fAxis; // axis : x, y, z ...
109  int fSide; // left/right
110  /* fSide == 0 : Is the root node
111  * fSide == -1 : It is the left of the parent node
112  * fSide == 1 : It is the right of the parent node
113  */
114 
117  /* Left : fLeft->fPosition[axis] < this->fPosition[axis]
118  * Right : fRight->fPosition[axis] > this->fPosition[axis]
119  * Root node : fParent = 0
120  */
121 private:
124 };
125 
131 template<typename PointT>
132  class G4KDNode : public G4KDNode_Base
133  {
134  public:
135  //________________________
136  // For root node :
137  // parent = 0, axis = 0, side = 0
138  G4KDNode(G4KDTree*, PointT* /*point*/, G4KDNode_Base* /*parent*/);
139  virtual ~G4KDNode();
140 
141  void *operator new(size_t);
142  void operator delete(void *);
143 
144  inline PointT* GetPoint()
145  {
146  return fPoint;
147  }
148 
149  virtual double operator[](size_t i) const
150  {
151  if(fPoint == 0) abort();
152  return (*fPoint)[i];
153  }
154 
155  virtual void InactiveNode()
156  {
157  fValid = false;
159  }
160 
161  virtual bool IsValid() const
162  {
163  return fValid;
164  }
165 
166  protected:
167  PointT* fPoint;
169 
170  private:
173 
175  };
176 
177 template<typename PointT>
179  0;
180 
181 template<typename PointT>
182  void* G4KDNode<PointT>::operator new(size_t)
183  {
184  if(!fgAllocator) fgAllocator = new G4Allocator<G4KDNode<PointT> >;
185  return (void *) fgAllocator->MallocSingle();
186  }
187 
188 template<typename PointT>
189  void G4KDNode<PointT>::operator delete(void *aNode)
190  {
191  fgAllocator->FreeSingle((G4KDNode<PointT> *) aNode);
192  }
193 
194 
200 template<typename PointCopyT>
202  {
203  public:
204  //________________________
205  // For root node :
206  // parent = 0, axis = 0, side = 0
208  const PointCopyT& point,
209  G4KDNode_Base* parent) :
210  G4KDNode_Base(tree, parent)
211  {
212  fPoint = point;
213  fValid = true;
214  }
215 
216  virtual ~G4KDNodeCopy(){}
217 
218  void *operator new(size_t)
219  {
221  return (void *) fgAllocator->MallocSingle();
222  }
223 
224  void operator delete(void* aNode)
225  {
227  }
228 
229  inline const PointCopyT& GetPoint()
230  {
231  return fPoint;
232  }
233 
234  virtual double operator[](size_t i) const
235  {
236  return fPoint[i];
237  }
238 
239  virtual void InactiveNode()
240  {
241  fValid = false;
243  }
244 
245  virtual bool IsValid() const
246  {
247  return fValid;
248  }
249 
250  protected:
251  PointCopyT fPoint;
253 
254  private:
256  G4KDNode_Base(right), fPoint(0)
257  {
258  fValid = false;
259  }
260 
263  {
264  if(this == &right) return *this;
265  fPoint = right.fPoint;
266  fTree = right.fTree;
267  fLeft = right.fLeft;
268  fRight = right.fRight;
269  fParent = right.fParent;
270  fSide = right.fSide;
271  fAxis = right.fAxis;
272  return *this;
273  }
274 
276  };
277 
278 template<typename PointT>
280  0;
281 
282 #include "G4KDNode.icc"
283 
284 #endif // G4KDNODE_HH
Type * MallocSingle()
Definition: G4Allocator.hh:202
void SetTree(G4KDTree *tree)
Definition: G4KDNode.hh:77
G4KDNodeCopy(const G4KDNodeCopy< PointCopyT > &right)
Definition: G4KDNode.hh:255
virtual bool IsValid() const
Definition: G4KDNode.hh:161
G4KDNode(G4KDTree *, PointT *, G4KDNode_Base *)
G4KDNode_Base * fLeft
Definition: G4KDNode.hh:116
PointT * GetPoint()
Definition: G4KDNode.hh:144
static G4ThreadLocal G4Allocator< G4KDNode< PointT > > * fgAllocator
Definition: G4KDNode.hh:174
G4bool fValid
Definition: G4KDNode.hh:252
virtual ~G4KDNodeCopy()
Definition: G4KDNode.hh:216
const PointCopyT & GetPoint()
Definition: G4KDNode.hh:229
G4KDNode_Base * Insert(PointT *point)
void FreeSingle(Type *anElement)
Definition: G4Allocator.hh:212
G4KDNode_Base * GetRight()
Definition: G4KDNode.hh:84
G4KDTree * GetTree() const
Definition: G4KDNode.hh:76
G4KDTree is used by the ITManager to locate the neareast neighbours.
Definition: G4KDTree.hh:72
PointT * fPoint
Definition: G4KDNode.hh:167
int GetAxis() const
Definition: G4KDNode.hh:81
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:207
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:155
virtual ~G4KDNode()
int GetDim() const
Definition: G4KDNode.cc:112
bool G4bool
Definition: G4Types.hh:79
size_t fAxis
Definition: G4KDNode.hh:108
G4KDNode stores one entity in G4KDTree This class is for internal use only.
Definition: G4KDNode.hh:201
G4KDNode stores one entity in G4KDTree This class is for internal use only.
Definition: G4KDNode.hh:132
virtual void InactiveNode()
Definition: G4KDNode.cc:107
G4KDNode_Base * FindParent(const Position &x0)
G4KDNodeCopy< PointCopyT > & operator=(const G4KDNodeCopy< PointCopyT > &right)
Definition: G4KDNode.hh:262
G4KDNode_Base & operator=(const G4KDNode_Base &right)
Definition: G4KDNode.cc:91
PointCopyT fPoint
Definition: G4KDNode.hh:251
virtual bool IsValid() const
Definition: G4KDNode.hh:73
G4KDNode_Base * fParent
Definition: G4KDNode.hh:116
virtual double operator[](size_t i) const
Definition: G4KDNode.hh:234
virtual double operator[](size_t) const =0
virtual void InactiveNode()
Definition: G4KDNode.hh:239
G4KDNode_Base * GetParent()
Definition: G4KDNode.hh:82
void PullSubTree()
Definition: G4KDNode.cc:147
virtual double operator[](size_t i) const
Definition: G4KDNode.hh:149
G4KDTree * fTree
Definition: G4KDNode.hh:115
G4bool fValid
Definition: G4KDNode.hh:168
virtual bool IsValid() const
Definition: G4KDNode.hh:245
G4KDNode_Base(G4KDTree *, G4KDNode_Base *)
Definition: G4KDNode.cc:72
G4KDNode & operator=(const G4KDNode< PointT > &right)
G4KDNode_Base * fRight
Definition: G4KDNode.hh:116
G4KDNode_Base * GetLeft()
Definition: G4KDNode.hh:83
static G4ThreadLocal G4Allocator< G4KDNodeCopy< PointCopyT > > * fgAllocator
Definition: G4KDNode.hh:275