Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // Author: Mathieu Karamitros (kara (AT) cenbg 27 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr) 28 // 28 // 29 // History: 29 // History: 30 // ----------- 30 // ----------- 31 // 10 Oct 2011 M.Karamitros created 31 // 10 Oct 2011 M.Karamitros created 32 // 32 // 33 // ------------------------------------------- 33 // ------------------------------------------------------------------- 34 34 35 #include "globals.hh" 35 #include "globals.hh" 36 #include "G4KDNode.hh" 36 #include "G4KDNode.hh" 37 #include "G4KDTree.hh" 37 #include "G4KDTree.hh" 38 #include <ostream> << 39 38 40 //******************************************** 39 //********************************************* 41 40 42 //____________________________________________ 41 //______________________________________________________________________ 43 // Node functions 42 // Node functions 44 43 45 //void* GetData(G4KDNode* node) << 44 void* GetData(G4KDNode* node) 46 //{ << 45 { 47 // return node->GetData() ; << 46 return node->GetData() ; 48 //} << 47 } 49 // << 48 50 //const double* GetNodePosition(G4KDNode* node << 49 const double* GetNodePosition(G4KDNode* node) 51 //{ << 50 { 52 // return node->GetPosition() ; << 51 return node->GetPosition() ; 53 //} << 52 } 54 53 55 //____________________________________________ 54 //______________________________________________________________________ 56 55 57 void InactiveNode(G4KDNode_Base* node) << 56 void InactiveNode(G4KDNode* node) 58 { 57 { 59 if(node == nullptr) return; << 58 if(!node) return ; 60 // if(node->IsValid()) << 59 node->InactiveNode() ; 61 node->InactiveNode(); << 62 } 60 } 63 61 64 void Free(G4KDNode_Base*& node) << 62 void Free(G4KDNode*& node) 65 { 63 { 66 delete node; << 64 if(node) 67 node = nullptr; << 65 delete node ; >> 66 node = 0; 68 } 67 } >> 68 //********************************************* 69 69 70 //____________________________________________ << 70 G4KDNode::G4KDNode(G4KDTree* tree, const double* position, void* data, 71 G4KDNode_Base::G4KDNode_Base(G4KDTree* tree, << 71 G4KDNode* parent, int axis) : 72 G4KDNode_Base* parent): << 72 fPosition(0), fData(data), fTree(tree), 73 fTree(tree), << 73 fLeft(0), fRight(0), fParent(parent) 74 fParent(parent) << 74 { 75 { << 75 fSide = 0; 76 fSide = 0; << 76 fAxis = axis; 77 fAxis = fParent == nullptr? 0 : fParent->fAx << 77 SetPosition(position); 78 } 78 } 79 79 80 // Copy constructor should not be used 80 // Copy constructor should not be used 81 G4KDNode_Base::G4KDNode_Base(const G4KDNode_Ba << 81 G4KDNode::G4KDNode(const G4KDNode& ): 82 { << 82 fPosition(0), fData(0), fTree(0), 83 fSide = 0; << 83 fLeft(0), fRight(0), fParent(0) 84 fAxis = 0; << 84 { >> 85 fSide = 0; >> 86 fAxis = 0; >> 87 fPosition = 0; 85 } 88 } 86 89 87 // Assignement should not be used 90 // Assignement should not be used 88 G4KDNode_Base& G4KDNode_Base::operator=(const << 91 G4KDNode& G4KDNode::operator=(const G4KDNode& right) 89 { 92 { 90 if (this == &right) return *this; << 93 if (this == &right) return *this; 91 fTree = right.fTree; << 94 fPosition = 0; 92 fLeft = right.fLeft; << 95 fData = right.fData; 93 fRight = right.fRight; << 96 fTree = right.fTree; 94 fParent = right.fParent; << 97 fLeft = right.fLeft; 95 fSide = right.fSide; << 98 fRight = right.fRight; 96 fAxis = right.fAxis; << 99 fParent = right.fParent; 97 return *this; << 100 fSide = right.fSide; >> 101 fAxis = right.fAxis; >> 102 SetPosition(right.fPosition); >> 103 return *this; 98 } 104 } 99 105 100 G4KDNode_Base::~G4KDNode_Base() << 106 G4KDNode::~G4KDNode() 101 = default; << 107 { >> 108 delete[] fPosition; >> 109 } >> 110 >> 111 int G4KDNode::GetDim() >> 112 { >> 113 if(fTree) >> 114 return fTree->GetDim(); >> 115 else >> 116 return -1; >> 117 } 102 118 103 void G4KDNode_Base::InactiveNode() << 119 void G4KDNode::InactiveNode() 104 { 120 { 105 fTree->NoticeNodeDeactivation(); << 121 fData = 0 ; 106 } 122 } 107 123 108 G4int G4KDNode_Base::GetDim() const << 124 int G4KDNode::SetPosition(const double* newposition) 109 { 125 { 110 if(fTree != nullptr) return (G4int)fTree->Ge << 126 if(!newposition) return -1; 111 return -1; << 127 if(!fPosition) >> 128 { >> 129 fPosition = new double[fTree->fDim]; >> 130 } >> 131 >> 132 memcpy(fPosition, newposition, fTree->fDim * sizeof(double)); >> 133 >> 134 return 0; 112 } 135 } 113 136 114 G4int G4KDNode_Base::Insert(G4KDNode_Base* new << 137 G4KDNode* G4KDNode::FindParent(const double* x0) 115 { 138 { 116 G4KDNode_Base* aParent = FindParent(*newNode << 139 G4KDNode* aParent = 0 ; 117 // TODO check p == aParent->pos << 140 G4KDNode* next = this ; 118 // Exception << 141 int split = -1 ; >> 142 while(next) >> 143 { >> 144 split = next->fAxis ; >> 145 aParent = next ; 119 146 120 newNode->fAxis = aParent->fAxis +1 < fTree-> << 147 if(x0[split] > next->fPosition[split]) 121 newNode->fParent = aParent ; << 148 next = next->fRight ; >> 149 else >> 150 next = next->fLeft ; >> 151 } >> 152 return aParent ; >> 153 } 122 154 123 if((*newNode)[aParent->fAxis] > (*aParent)[a << 155 G4KDNode* G4KDNode::Insert(const double* p, void* data) 124 { << 156 { 125 aParent->fRight = newNode ; << 157 G4KDNode* aParent = FindParent(p); 126 newNode->fSide = 1 ; << 158 // TODO check p == aParent->pos 127 } << 159 // Exception 128 else << 129 { << 130 aParent->fLeft = newNode ; << 131 newNode->fSide = -1 ; << 132 } << 133 160 134 newNode->fRight = nullptr; << 161 G4KDNode* newNode = new G4KDNode(fTree, p, data, aParent, 135 newNode->fLeft = nullptr; << 162 aParent->fAxis +1 < fTree->fDim? aParent->fAxis+1:0); 136 163 137 return 0 ; << 164 if(p[aParent->fAxis] > aParent->fPosition[aParent->fAxis]) >> 165 { >> 166 aParent->fRight = newNode ; >> 167 newNode->fSide = 1 ; >> 168 } >> 169 else >> 170 { >> 171 aParent->fLeft = newNode ; >> 172 newNode->fSide = -1 ; >> 173 } >> 174 >> 175 return newNode ; 138 } 176 } 139 177 140 178 141 void G4KDNode_Base::PullSubTree() << 179 int G4KDNode::Insert(G4KDNode* newNode, double* p) 142 { 180 { 143 if(fParent != nullptr) << 181 G4KDNode* aParent = FindParent(p); 144 { << 182 // TODO check p == aParent->pos 145 if(fSide == -1) << 183 // Exception 146 { << 184 147 fParent->fLeft = nullptr; << 185 newNode->fAxis = aParent->fAxis +1 < fTree->fDim? aParent->fAxis+1:0; 148 } << 186 newNode->fParent = aParent ; 149 else << 187 150 fParent->fRight = nullptr; << 188 if(p[aParent->fAxis] > aParent->fPosition[aParent->fAxis]) 151 } << 189 { 152 if(fLeft != nullptr) fLeft -> PullSubTree(); << 190 aParent->fRight = newNode ; 153 if(fRight != nullptr) fRight-> PullSubTree() << 191 newNode->fSide = 1 ; >> 192 } >> 193 else >> 194 { >> 195 aParent->fLeft = newNode ; >> 196 newNode->fSide = -1 ; >> 197 } 154 198 155 fParent = nullptr ; << 199 newNode->fRight = 0; 156 fRight = nullptr ; << 200 newNode->fLeft = 0; 157 fLeft = nullptr ; << 201 158 fTree = nullptr ; << 202 return 0 ; 159 } 203 } 160 204 161 void G4KDNode_Base::RetrieveNodeList(std::list << 205 int G4KDNode::Insert(G4KDNode* newNode, const double& x, const double& y, const double& z) 162 { 206 { 163 output.push_back(this); << 207 double p[3] ; >> 208 p[0] = x; >> 209 p[1] = y ; >> 210 p[2] = z ; >> 211 return Insert(newNode, p); >> 212 } 164 213 165 if(fLeft != nullptr) << 214 int G4KDNode::Insert(G4KDNode* newNode) 166 fLeft->RetrieveNodeList(output); << 215 { >> 216 return Insert(newNode, newNode->fPosition); >> 217 } 167 218 168 if(fRight != nullptr) << 219 void G4KDNode::PullSubTree() 169 fRight->RetrieveNodeList(output); << 220 { >> 221 if(fParent) >> 222 { >> 223 if(fSide == -1) >> 224 { >> 225 fParent->fLeft = 0; >> 226 } >> 227 else >> 228 fParent->fRight = 0; >> 229 } >> 230 if(fLeft) fLeft -> PullSubTree(); >> 231 if(fRight) fRight-> PullSubTree(); >> 232 >> 233 fParent = 0 ; >> 234 fRight = 0 ; >> 235 fLeft = 0 ; >> 236 fTree = 0 ; 170 } 237 } 171 238 172 void G4KDNode_Base::Print(std::ostream& out, i << 239 void G4KDNode::RetrieveNodeList(std::list<G4KDNode*>& output) 173 { 240 { 174 // Print node level << 241 output.push_back(this); 175 out << G4endl; << 242 176 for (G4int i=0; i<level; ++i) // Ind << 243 if(fLeft) 177 { << 244 fLeft->RetrieveNodeList(output); 178 out << " "; << 179 } << 180 out << level; << 181 245 182 // Print children << 246 if(fRight) 183 if(fLeft != nullptr) << 247 fRight->RetrieveNodeList(output); 184 { << 185 fLeft->Print(out, level + 1); << 186 } << 187 if(fRight != nullptr) << 188 { << 189 fRight->Print(out, level + 1); << 190 } << 191 } 248 } 192 249