Geant4  10.02.p01
G4KDNode.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 //
26 // $Id$
27 //
28 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
29 //
30 // History:
31 // -----------
32 // 10 Oct 2011 M.Karamitros created
33 //
34 // -------------------------------------------------------------------
35 
36 #include "globals.hh"
37 #include "G4KDNode.hh"
38 #include "G4KDTree.hh"
39 #include <ostream>
40 
41 //*********************************************
42 
43 //______________________________________________________________________
44 // Node functions
45 
46 //void* GetData(G4KDNode* node)
47 //{
48 // return node->GetData() ;
49 //}
50 //
51 //const double* GetNodePosition(G4KDNode* node)
52 //{
53 // return node->GetPosition() ;
54 //}
55 
56 //______________________________________________________________________
57 
59 {
60  if(!node) return ;
61  node->InactiveNode() ;
62 }
63 
64 void Free(G4KDNode_Base*& node)
65 {
66  if(node)
67  delete node ;
68  node = 0;
69 }
70 
71 //______________________________________________________________________
73  G4KDNode_Base* parent):
74  fTree(tree),
75  fLeft(0), fRight(0), fParent(parent)
76 {
77  fSide = 0;
78  fAxis = fParent == 0? 0 : fParent->fAxis +1 < fTree->fDim? fParent->fAxis+1:0;
79 }
80 
81 // Copy constructor should not be used
83  fTree(0),
84  fLeft(0), fRight(0), fParent(0)
85 {
86  fSide = 0;
87  fAxis = 0;
88 }
89 
90 // Assignement should not be used
92 {
93  if (this == &right) return *this;
94  fTree = right.fTree;
95  fLeft = right.fLeft;
96  fRight = right.fRight;
97  fParent = right.fParent;
98  fSide = right.fSide;
99  fAxis = right.fAxis;
100  return *this;
101 }
102 
104 {
105 }
106 
108 {
110 }
111 
113 {
114  if(fTree)
115  return fTree->GetDim();
116  else
117  return -1;
118 }
119 
121 {
122  G4KDNode_Base* aParent = FindParent(*newNode);
123  // TODO check p == aParent->pos
124  // Exception
125 
126  newNode->fAxis = aParent->fAxis +1 < fTree->GetDim()? aParent->fAxis+1:0;
127  newNode->fParent = aParent ;
128 
129  if((*newNode)[aParent->fAxis] > (*aParent)[aParent->fAxis])
130  {
131  aParent->fRight = newNode ;
132  newNode->fSide = 1 ;
133  }
134  else
135  {
136  aParent->fLeft = newNode ;
137  newNode->fSide = -1 ;
138  }
139 
140  newNode->fRight = 0;
141  newNode->fLeft = 0;
142 
143  return 0 ;
144 }
145 
146 
148 {
149  if(fParent)
150  {
151  if(fSide == -1)
152  {
153  fParent->fLeft = 0;
154  }
155  else
156  fParent->fRight = 0;
157  }
158  if(fLeft) fLeft -> PullSubTree();
159  if(fRight) fRight-> PullSubTree();
160 
161  fParent = 0 ;
162  fRight = 0 ;
163  fLeft = 0 ;
164  fTree = 0 ;
165 }
166 
167 void G4KDNode_Base::RetrieveNodeList(std::list<G4KDNode_Base *>& output)
168 {
169  output.push_back(this);
170 
171  if(fLeft)
172  fLeft->RetrieveNodeList(output);
173 
174  if(fRight)
175  fRight->RetrieveNodeList(output);
176 }
177 
178 void G4KDNode_Base::Print(std::ostream& out, int level) const
179 {
180  // Print node level
181  out << G4endl;
182  for (int i=0; i<level; i++) // Indent to level
183  {
184  out << " ";
185  }
186  out << level;
187 
188  // Print children
189  if(fLeft)
190  {
191  fLeft->Print(out, level + 1);
192  }
193  if(fRight)
194  {
195  fRight->Print(out, level + 1);
196  }
197 }
G4KDNode_Base * fLeft
Definition: G4KDNode.hh:116
size_t fDim
Definition: G4KDTree.hh:270
G4KDNode_Base * Insert(PointT *point)
G4KDTree is used by the ITManager to locate the neareast neighbours.
Definition: G4KDTree.hh:72
virtual ~G4KDNode_Base()
Definition: G4KDNode.cc:103
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
void InactiveNode(G4KDNode_Base *node)
Definition: G4KDNode.cc:58
int GetDim() const
Definition: G4KDNode.cc:112
size_t fAxis
Definition: G4KDNode.hh:108
virtual void InactiveNode()
Definition: G4KDNode.cc:107
void Free(G4KDNode_Base *&node)
Definition: G4KDNode.cc:64
G4KDNode_Base * FindParent(const Position &x0)
G4KDNode_Base & operator=(const G4KDNode_Base &right)
Definition: G4KDNode.cc:91
G4KDNode_Base * fParent
Definition: G4KDNode.hh:116
void PullSubTree()
Definition: G4KDNode.cc:147
size_t GetDim() const
Definition: G4KDTree.hh:88
#define G4endl
Definition: G4ios.hh:61
G4KDTree * fTree
Definition: G4KDNode.hh:115
G4KDNode_Base(G4KDTree *, G4KDNode_Base *)
Definition: G4KDNode.cc:72
void NoticeNodeDeactivation()
Definition: G4KDTree.hh:82
G4KDNode_Base * fRight
Definition: G4KDNode.hh:116