Geant4-11
Data Structures | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends
G4KDTree Class Reference

#include <G4KDTree.hh>

Data Structures

class  HyperRect
 

Public Member Functions

void Build ()
 
void Clear ()
 
 G4KDTree (size_t dim=3)
 
size_t GetDim () const
 
int GetNbNodes () const
 
G4KDNode_BaseGetRoot ()
 
template<typename PointT >
G4KDNode_BaseInsert (const PointT &pos)
 
template<typename PointT >
G4KDNode_BaseInsert (PointT *pos)
 
template<typename PointT >
G4KDNode_BaseInsertMap (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)
 
void NoticeNodeDeactivation ()
 
void operator delete (void *)
 
void * operator new (size_t)
 
void Print (std::ostream &out=G4cout) const
 
 ~G4KDTree ()
 

Protected Member Functions

void __Clear_Rec (G4KDNode_Base *node)
 
void __InsertMap (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 __NearestToNode (G4KDNode_Base *source_node, G4KDNode_Base *node, const Position &pos, std::vector< G4KDNode_Base * > &result, double *result_dist_sq, HyperRect *fRect, int &nbresult)
 
template<typename Position >
void __NearestToPosition (G4KDNode_Base *node, const Position &pos, G4KDNode_Base *&result, double *result_dist_sq, HyperRect *fRect)
 

Static Protected Member Functions

static G4Allocator< G4KDTree > *& fgAllocator ()
 

Protected Attributes

size_t fDim
 
G4KDMapfKDMap
 
int fNbActiveNodes
 
int fNbNodes
 
HyperRectfRect
 
G4KDNode_BasefRoot
 

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 70 of file G4KDTree.hh.

Constructor & Destructor Documentation

◆ G4KDTree()

G4KDTree::G4KDTree ( size_t  dim = 3)

Definition at line 54 of file G4KDTree.cc.

54 :
55 fKDMap(new G4KDMap(k))
56{
57 fDim = k;
58 fRoot = 0;
59 fRect = 0;
60 fNbNodes = 0;
62}
HyperRect * fRect
Definition: G4KDTree.hh:266
int fNbNodes
Definition: G4KDTree.hh:269
int fNbActiveNodes
Definition: G4KDTree.hh:270
G4KDNode_Base * fRoot
Definition: G4KDTree.hh:267
size_t fDim
Definition: G4KDTree.hh:268
G4KDMap * fKDMap
Definition: G4KDTree.hh:271

References fDim, fNbActiveNodes, fNbNodes, fRect, and fRoot.

◆ ~G4KDTree()

G4KDTree::~G4KDTree ( )

Definition at line 64 of file G4KDTree.cc.

65{
67 fRoot = 0;
68
69 if (fRect)
70 {
71 delete fRect;
72 fRect = 0;
73 }
74
75 if (fKDMap) delete fKDMap;
76}
void __Clear_Rec(G4KDNode_Base *node)
Definition: G4KDTree.cc:107

References __Clear_Rec(), fKDMap, fRect, and fRoot.

Member Function Documentation

◆ __Clear_Rec()

void G4KDTree::__Clear_Rec ( G4KDNode_Base node)
protected

Definition at line 107 of file G4KDTree.cc.

108{
109 if (!node) return;
110
111 if (node->GetLeft()) __Clear_Rec(node->GetLeft());
112 if (node->GetRight()) __Clear_Rec(node->GetRight());
113
114 delete node;
115}
G4KDNode_Base * GetRight()
Definition: G4KDNode.hh:82
G4KDNode_Base * GetLeft()
Definition: G4KDNode.hh:81

References __Clear_Rec(), G4KDNode_Base::GetLeft(), and G4KDNode_Base::GetRight().

Referenced by __Clear_Rec(), Clear(), and ~G4KDTree().

◆ __InsertMap()

void G4KDTree::__InsertMap ( G4KDNode_Base node)
protected

Definition at line 117 of file G4KDTree.cc.

118{
119 fKDMap->Insert(node);
120}
void Insert(G4KDNode_Base *pos)
Definition: G4KDMap.cc:90

References fKDMap, and G4KDMap::Insert().

◆ __NearestInRange()

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

Referenced by NearestInRange().

◆ __NearestToNode()

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

Referenced by Nearest().

◆ __NearestToPosition()

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

◆ Build()

void G4KDTree::Build ( )

Definition at line 122 of file G4KDTree.cc.

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

References G4KDTree::HyperRect::Extend(), fDim, fKDMap, fNbActiveNodes, fRect, fRoot, G4cout, G4endl, G4KDMap::GetSize(), G4KDNode_Base::Insert(), CLHEP::detail::n, G4KDMap::PopOutMiddle(), and G4KDTree::HyperRect::SetMinMax().

◆ Clear()

void G4KDTree::Clear ( )

Definition at line 94 of file G4KDTree.cc.

95{
97 fRoot = 0;
98 fNbNodes = 0;
99
100 if (fRect)
101 {
102 delete fRect;
103 fRect = 0;
104 }
105}

References __Clear_Rec(), fNbNodes, fRect, and fRoot.

Referenced by NoticeNodeDeactivation().

◆ fgAllocator()

G4Allocator< G4KDTree > *& G4KDTree::fgAllocator ( )
staticprotected

Definition at line 46 of file G4KDTree.cc.

47{
48 G4ThreadLocalStatic G4Allocator<G4KDTree>* _instance = nullptr;
49 return _instance;
50}
#define G4ThreadLocalStatic
Definition: tls.hh:76

References G4ThreadLocalStatic.

◆ GetDim()

size_t G4KDTree::GetDim ( ) const
inline

Definition at line 86 of file G4KDTree.hh.

87 {
88 return fDim;
89 }

References fDim.

Referenced by G4KDNode_Base::GetDim(), and G4KDNode_Base::Insert().

◆ GetNbNodes()

int G4KDTree::GetNbNodes ( ) const
inline

Definition at line 90 of file G4KDTree.hh.

91 {
92 return fNbNodes;
93 }

References fNbNodes.

◆ GetRoot()

G4KDNode_Base * G4KDTree::GetRoot ( )
inline

Definition at line 94 of file G4KDTree.hh.

95 {
96 return fRoot;
97 }

References fRoot.

◆ Insert() [1/2]

template<typename PointT >
G4KDNode_Base * G4KDTree::Insert ( const PointT &  pos)

◆ Insert() [2/2]

template<typename PointT >
G4KDNode_Base * G4KDTree::Insert ( PointT *  pos)

◆ InsertMap()

template<typename PointT >
G4KDNode_Base * G4KDTree::InsertMap ( PointT *  pos)

◆ Nearest() [1/2]

template<typename Position >
G4KDTreeResultHandle G4KDTree::Nearest ( const Position &  pos)

◆ Nearest() [2/2]

G4KDTreeResultHandle G4KDTree::Nearest ( G4KDNode_Base node)

Definition at line 159 of file G4KDTree.cc.

160{
161 // G4cout << "Nearest(node)" << G4endl ;
162 if (!fRect)
163 {
164 G4cout << "Tree empty" << G4endl;
165 return 0;
166 }
167
168 std::vector<G4KDNode_Base* > result;
169 double dist_sq = DBL_MAX;
170
171 /* Duplicate the bounding hyperrectangle, we will work on the copy */
172 HyperRect *newrect = new HyperRect(*fRect);
173
174 /* Search for the nearest neighbour recursively */
175 int nbresult = 0;
176
177 __NearestToNode(node, fRoot, *node, result, &dist_sq, newrect, nbresult);
178
179 /* Free the copy of the hyperrect */
180 delete newrect;
181
182 /* Store the result */
183 if (!result.empty())
184 {
185 G4KDTreeResultHandle rset(new G4KDTreeResult(this));
186 int j = 0;
187 while (j<nbresult)
188 {
189 rset->Insert(dist_sq, result[j]);
190 j++;
191 }
192 rset->Rewind();
193
194 return rset;
195 }
196 else
197 {
198 return 0;
199 }
200}
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)
#define DBL_MAX
Definition: templates.hh:62

References __NearestToNode(), DBL_MAX, fRect, fRoot, G4cout, and G4endl.

◆ NearestInRange() [1/2]

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

◆ NearestInRange() [2/2]

G4KDTreeResultHandle G4KDTree::NearestInRange ( G4KDNode_Base node,
const double &  range 
)

Definition at line 202 of file G4KDTree.cc.

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

References __NearestInRange(), fRoot, G4KDTreeResult::Rewind(), G4KDTreeResult::Sort(), and sqr().

◆ NoticeNodeDeactivation()

void G4KDTree::NoticeNodeDeactivation ( )
inline

Definition at line 80 of file G4KDTree.hh.

81 {
83 if (fNbActiveNodes <= 0) Clear();
84 }
void Clear()
Definition: G4KDTree.cc:94

References Clear(), and fNbActiveNodes.

Referenced by G4KDNode_Base::InactiveNode().

◆ operator delete()

void G4KDTree::operator delete ( void *  aNode)

Definition at line 84 of file G4KDTree.cc.

85{
86 fgAllocator()->FreeSingle((G4KDTree*) aNode);
87}
static G4Allocator< G4KDTree > *& fgAllocator()
Definition: G4KDTree.cc:46

◆ operator new()

void * G4KDTree::operator new ( size_t  )

Definition at line 78 of file G4KDTree.cc.

79{
81 return (void *) fgAllocator()->MallocSingle();
82}

References G4Allocator< Type >::MallocSingle().

◆ Print()

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

Definition at line 89 of file G4KDTree.cc.

90{
91 if (fRoot) fRoot->Print(out);
92}
void Print(std::ostream &out, int level=0) const
Definition: G4KDNode.cc:177

References fRoot, and G4KDNode_Base::Print().

Friends And Related Function Documentation

◆ G4KDNode_Base

friend class G4KDNode_Base
friend

Definition at line 72 of file G4KDTree.hh.

Field Documentation

◆ fDim

size_t G4KDTree::fDim
protected

Definition at line 268 of file G4KDTree.hh.

Referenced by Build(), G4KDNode_Base::G4KDNode_Base(), G4KDTree(), and GetDim().

◆ fKDMap

G4KDMap* G4KDTree::fKDMap
protected

Definition at line 271 of file G4KDTree.hh.

Referenced by __InsertMap(), Build(), and ~G4KDTree().

◆ fNbActiveNodes

int G4KDTree::fNbActiveNodes
protected

Definition at line 270 of file G4KDTree.hh.

Referenced by Build(), G4KDTree(), and NoticeNodeDeactivation().

◆ fNbNodes

int G4KDTree::fNbNodes
protected

Definition at line 269 of file G4KDTree.hh.

Referenced by Clear(), G4KDTree(), and GetNbNodes().

◆ fRect

HyperRect* G4KDTree::fRect
protected

Definition at line 266 of file G4KDTree.hh.

Referenced by Build(), Clear(), G4KDTree(), Nearest(), and ~G4KDTree().

◆ fRoot

G4KDNode_Base* G4KDTree::fRoot
protected

Definition at line 267 of file G4KDTree.hh.

Referenced by Build(), Clear(), G4KDTree(), GetRoot(), Nearest(), NearestInRange(), Print(), and ~G4KDTree().


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