Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4KDTree Class Reference

#include <G4KDTree.hh>

Collaboration diagram for G4KDTree:

Classes

class  HyperRect
 

Public Member Functions

 G4KDTree (size_t dim=3)
 
 ~G4KDTree ()
 
void Clear ()
 
void Print (std::ostream &out=G4cout) const
 
void Build ()
 
void NoticeNodeDeactivation ()
 
size_t GetDim () const
 
int GetNbNodes () const
 
G4KDNode_BaseGetRoot ()
 
template<typename PointT >
G4KDNode_BaseInsertMap (PointT *pos)
 
template<typename PointT >
G4KDNode_BaseInsert (PointT *pos)
 
template<typename PointT >
G4KDNode_BaseInsert (const PointT &pos)
 
template<typename Position >
G4KDTreeResultHandle Nearest (const Position &pos)
 
G4KDTreeResultHandle Nearest (G4KDNode_Base *node)
 
template<typename Position >
G4KDTreeResultHandle NearestInRange (const Position &pos, const double &range)
 
G4KDTreeResultHandle NearestInRange (G4KDNode_Base *node, const double &range)
 
voidoperator new (size_t)
 
void operator delete (void *)
 

Protected Member Functions

void __InsertMap (G4KDNode_Base *node)
 
void __Clear_Rec (G4KDNode_Base *node)
 
template<typename Position >
int __NearestInRange (G4KDNode_Base *node, const Position &pos, const double &range_sq, const double &range, G4KDTreeResult &list, int ordered, G4KDNode_Base *source_node=0)
 
template<typename Position >
void __NearestToPosition (G4KDNode_Base *node, const Position &pos, G4KDNode_Base *&result, double *result_dist_sq, HyperRect *fRect)
 
template<typename Position >
void __NearestToNode (G4KDNode_Base *source_node, G4KDNode_Base *node, const Position &pos, std::vector< G4KDNode_Base * > &result, double *result_dist_sq, HyperRect *fRect, int &nbresult)
 

Protected Attributes

HyperRectfRect
 
G4KDNode_BasefRoot
 
size_t fDim
 
int fNbNodes
 
int fNbActiveNodes
 
G4KDMapfKDMap
 
G4ThreadLocalStatic
G4Allocator< G4KDTree > * 
fgAllocator
 

Friends

class G4KDNode_Base
 

Detailed Description

G4KDTree is used by the ITManager to locate the neareast neighbours. A kdtree sorts out node in such a way that it reduces the number of node check. The results of this search can be retrieved by G4KDTreeResultHandle.

Definition at line 72 of file G4KDTree.hh.

Constructor & Destructor Documentation

G4KDTree::G4KDTree ( size_t  dim = 3)

Definition at line 50 of file G4KDTree.cc.

50  :
51  fKDMap(new G4KDMap(k))
52 {
53  fDim = k;
54  fRoot = 0;
55  fRect = 0;
56  fNbNodes = 0;
57  fNbActiveNodes = 0;
58 }
HyperRect * fRect
Definition: G4KDTree.hh:268
size_t fDim
Definition: G4KDTree.hh:270
int fNbNodes
Definition: G4KDTree.hh:271
int fNbActiveNodes
Definition: G4KDTree.hh:272
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
G4KDMap * fKDMap
Definition: G4KDTree.hh:273
G4KDTree::~G4KDTree ( )

Definition at line 60 of file G4KDTree.cc.

61 {
62  if (fRoot) __Clear_Rec(fRoot);
63  fRoot = 0;
64 
65  if (fRect)
66  {
67  delete fRect;
68  fRect = 0;
69  }
70 
71  if (fKDMap) delete fKDMap;
72 }
HyperRect * fRect
Definition: G4KDTree.hh:268
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
G4KDMap * fKDMap
Definition: G4KDTree.hh:273
void __Clear_Rec(G4KDNode_Base *node)
Definition: G4KDTree.cc:103

Here is the call graph for this function:

Member Function Documentation

void G4KDTree::__Clear_Rec ( G4KDNode_Base node)
protected

Definition at line 103 of file G4KDTree.cc.

104 {
105  if (!node) return;
106 
107  if (node->GetLeft()) __Clear_Rec(node->GetLeft());
108  if (node->GetRight()) __Clear_Rec(node->GetRight());
109 
110  delete node;
111 }
G4KDNode_Base * GetRight()
Definition: G4KDNode.hh:86
void __Clear_Rec(G4KDNode_Base *node)
Definition: G4KDTree.cc:103
G4KDNode_Base * GetLeft()
Definition: G4KDNode.hh:85

Here is the call graph for this function:

Here is the caller graph for this function:

void G4KDTree::__InsertMap ( G4KDNode_Base node)
protected

Definition at line 113 of file G4KDTree.cc.

114 {
115  fKDMap->Insert(node);
116 }
G4KDMap * fKDMap
Definition: G4KDTree.hh:273
void Insert(G4KDNode_Base *pos)
Definition: G4KDMap.cc:90

Here is the call graph for this function:

template<typename Position >
int G4KDTree::__NearestInRange ( G4KDNode_Base node,
const Position &  pos,
const double &  range_sq,
const double &  range,
G4KDTreeResult list,
int  ordered,
G4KDNode_Base source_node = 0 
)
protected

Here is the caller graph for this function:

template<typename Position >
void G4KDTree::__NearestToNode ( G4KDNode_Base source_node,
G4KDNode_Base node,
const Position &  pos,
std::vector< G4KDNode_Base * > &  result,
double *  result_dist_sq,
HyperRect fRect,
int nbresult 
)
protected

Here is the caller graph for this function:

template<typename Position >
void G4KDTree::__NearestToPosition ( G4KDNode_Base node,
const Position &  pos,
G4KDNode_Base *&  result,
double *  result_dist_sq,
HyperRect fRect 
)
protected
void G4KDTree::Build ( )

Definition at line 118 of file G4KDTree.cc.

119 {
120  size_t Nnodes = fKDMap->GetSize();
121 
122  G4cout << "********************" << G4endl;
123  G4cout << "template<typename PointT> G4KDTree<PointT>::Build" << G4endl;
124  G4cout << "Map size = " << Nnodes << G4endl;
125 
126  G4KDNode_Base* root = fKDMap->PopOutMiddle(0);
127 
128  if(root == 0) return;
129 
130  fRoot = root;
131  fNbActiveNodes++;
132  fRect = new HyperRect(fDim);
133  fRect->SetMinMax(*fRoot, *fRoot);
134 
135  Nnodes--;
136 
137  G4KDNode_Base* parent = fRoot;
138 
139  for (size_t n = 0; n < Nnodes; n += fDim)
140  {
141  for (size_t dim = 0; dim < fDim; dim++)
142  {
143  G4KDNode_Base* node = fKDMap->PopOutMiddle(dim);
144  if (node)
145  {
146  parent->Insert(node);
147  fNbActiveNodes++;
148  fRect->Extend(*node);
149  parent = node;
150  }
151  }
152  }
153 }
HyperRect * fRect
Definition: G4KDTree.hh:268
void SetMinMax(const Position &min, const Position &max)
Definition: G4KDTree.hh:146
size_t fDim
Definition: G4KDTree.hh:270
G4KDNode_Base * Insert(PointT *point)
size_t GetSize()
Definition: G4KDMap.hh:110
G4KDNode_Base * PopOutMiddle(size_t dimension)
Definition: G4KDMap.cc:138
int fNbActiveNodes
Definition: G4KDTree.hh:272
G4GLOB_DLL std::ostream G4cout
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
G4KDMap * fKDMap
Definition: G4KDTree.hh:273
void Extend(const Position &pos)
Definition: G4KDTree.hh:175
#define G4endl
Definition: G4ios.hh:61

Here is the call graph for this function:

void G4KDTree::Clear ( )

Definition at line 90 of file G4KDTree.cc.

91 {
93  fRoot = 0;
94  fNbNodes = 0;
95 
96  if (fRect)
97  {
98  delete fRect;
99  fRect = 0;
100  }
101 }
HyperRect * fRect
Definition: G4KDTree.hh:268
int fNbNodes
Definition: G4KDTree.hh:271
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
void __Clear_Rec(G4KDNode_Base *node)
Definition: G4KDTree.cc:103

Here is the call graph for this function:

Here is the caller graph for this function:

size_t G4KDTree::GetDim ( ) const
inline

Definition at line 88 of file G4KDTree.hh.

89  {
90  return fDim;
91  }
size_t fDim
Definition: G4KDTree.hh:270

Here is the caller graph for this function:

int G4KDTree::GetNbNodes ( ) const
inline

Definition at line 92 of file G4KDTree.hh.

93  {
94  return fNbNodes;
95  }
int fNbNodes
Definition: G4KDTree.hh:271
G4KDNode_Base* G4KDTree::GetRoot ( )
inline

Definition at line 96 of file G4KDTree.hh.

97  {
98  return fRoot;
99  }
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
template<typename PointT >
G4KDNode_Base* G4KDTree::Insert ( PointT *  pos)
template<typename PointT >
G4KDNode_Base* G4KDTree::Insert ( const PointT &  pos)
template<typename PointT >
G4KDNode_Base* G4KDTree::InsertMap ( PointT *  pos)
template<typename Position >
G4KDTreeResultHandle G4KDTree::Nearest ( const Position &  pos)
G4KDTreeResultHandle G4KDTree::Nearest ( G4KDNode_Base node)

Definition at line 155 of file G4KDTree.cc.

156 {
157  // G4cout << "Nearest(node)" << G4endl ;
158  if (!fRect)
159  {
160  G4cout << "Tree empty" << G4endl;
161  return 0;
162  }
163 
164  std::vector<G4KDNode_Base* > result;
165  double dist_sq = DBL_MAX;
166 
167  /* Duplicate the bounding hyperrectangle, we will work on the copy */
168  HyperRect *newrect = new HyperRect(*fRect);
169 
170  /* Search for the nearest neighbour recursively */
171  int nbresult = 0;
172 
173  __NearestToNode(node, fRoot, *node, result, &dist_sq, newrect, nbresult);
174 
175  /* Free the copy of the hyperrect */
176  delete newrect;
177 
178  /* Store the result */
179  if (!result.empty())
180  {
181  G4KDTreeResultHandle rset(new G4KDTreeResult(this));
182  int j = 0;
183  while (j<nbresult)
184  {
185  rset->Insert(dist_sq, result[j]);
186  j++;
187  }
188  rset->Rewind();
189 
190  return rset;
191  }
192  else
193  {
194  return 0;
195  }
196 }
G4double G4ParticleHPJENDLHEData::G4double result
HyperRect * fRect
Definition: G4KDTree.hh:268
void __NearestToNode(G4KDNode_Base *source_node, G4KDNode_Base *node, const Position &pos, std::vector< G4KDNode_Base * > &result, double *result_dist_sq, HyperRect *fRect, int &nbresult)
G4GLOB_DLL std::ostream G4cout
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
#define G4endl
Definition: G4ios.hh:61
#define DBL_MAX
Definition: templates.hh:83

Here is the call graph for this function:

template<typename Position >
G4KDTreeResultHandle G4KDTree::NearestInRange ( const Position &  pos,
const double &  range 
)
G4KDTreeResultHandle G4KDTree::NearestInRange ( G4KDNode_Base node,
const double &  range 
)

Definition at line 198 of file G4KDTree.cc.

200 {
201  if (!node) return 0;
202  int ret(-1);
203 
204  G4KDTreeResult *rset = new G4KDTreeResult(this);
205 
206  const double range_sq = sqr(range);
207 
208  if ((ret = __NearestInRange(fRoot, *node, range_sq, range, *rset, 0, node)) == -1)
209  {
210  delete rset;
211  return 0;
212  }
213  rset->Sort();
214  rset->Rewind();
215  return rset;
216 }
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269
T sqr(const T &x)
Definition: templates.hh:145
int __NearestInRange(G4KDNode_Base *node, const Position &pos, const double &range_sq, const double &range, G4KDTreeResult &list, int ordered, G4KDNode_Base *source_node=0)

Here is the call graph for this function:

void G4KDTree::NoticeNodeDeactivation ( )
inline

Definition at line 82 of file G4KDTree.hh.

83  {
85  if (fNbActiveNodes <= 0) Clear();
86  }
void Clear()
Definition: G4KDTree.cc:90
int fNbActiveNodes
Definition: G4KDTree.hh:272

Here is the call graph for this function:

Here is the caller graph for this function:

void G4KDTree::operator delete ( void aNode)

Definition at line 80 of file G4KDTree.cc.

81 {
82  fgAllocator->FreeSingle((G4KDTree*) aNode);
83 }
void FreeSingle(Type *anElement)
Definition: G4Allocator.hh:212
G4ThreadLocalStatic G4Allocator< G4KDTree > * fgAllocator
Definition: G4KDTree.hh:275
void * G4KDTree::operator new ( size_t  )

Definition at line 74 of file G4KDTree.cc.

75 {
77  return (void *) fgAllocator->MallocSingle();
78 }
Type * MallocSingle()
Definition: G4Allocator.hh:202
G4ThreadLocalStatic G4Allocator< G4KDTree > * fgAllocator
Definition: G4KDTree.hh:275

Here is the call graph for this function:

void G4KDTree::Print ( std::ostream &  out = G4cout) const

Definition at line 85 of file G4KDTree.cc.

86 {
87  if (fRoot) fRoot->Print(out);
88 }
void Print(std::ostream &out, int level=0) const
Definition: G4KDNode.cc:178
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:269

Here is the call graph for this function:

Friends And Related Function Documentation

friend class G4KDNode_Base
friend

Definition at line 74 of file G4KDTree.hh.

Member Data Documentation

size_t G4KDTree::fDim
protected

Definition at line 270 of file G4KDTree.hh.

G4ThreadLocal G4Allocator< G4KDTree > * G4KDTree::fgAllocator
protected

Definition at line 275 of file G4KDTree.hh.

G4KDMap* G4KDTree::fKDMap
protected

Definition at line 273 of file G4KDTree.hh.

int G4KDTree::fNbActiveNodes
protected

Definition at line 272 of file G4KDTree.hh.

int G4KDTree::fNbNodes
protected

Definition at line 271 of file G4KDTree.hh.

HyperRect* G4KDTree::fRect
protected

Definition at line 268 of file G4KDTree.hh.

G4KDNode_Base* G4KDTree::fRoot
protected

Definition at line 269 of file G4KDTree.hh.


The documentation for this class was generated from the following files: