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> 38 #include <ostream> 39 39 40 //******************************************** 40 //********************************************* 41 41 42 //____________________________________________ 42 //______________________________________________________________________ 43 // Node functions 43 // Node functions 44 44 45 //void* GetData(G4KDNode* node) 45 //void* GetData(G4KDNode* node) 46 //{ 46 //{ 47 // return node->GetData() ; 47 // return node->GetData() ; 48 //} 48 //} 49 // 49 // 50 //const double* GetNodePosition(G4KDNode* node 50 //const double* GetNodePosition(G4KDNode* node) 51 //{ 51 //{ 52 // return node->GetPosition() ; 52 // return node->GetPosition() ; 53 //} 53 //} 54 54 55 //____________________________________________ 55 //______________________________________________________________________ 56 56 57 void InactiveNode(G4KDNode_Base* node) 57 void InactiveNode(G4KDNode_Base* node) 58 { 58 { 59 if(node == nullptr) return; 59 if(node == nullptr) return; 60 // if(node->IsValid()) 60 // if(node->IsValid()) 61 node->InactiveNode(); 61 node->InactiveNode(); 62 } 62 } 63 63 64 void Free(G4KDNode_Base*& node) 64 void Free(G4KDNode_Base*& node) 65 { 65 { 66 delete node; << 66 if(node) delete node; 67 node = nullptr; 67 node = nullptr; 68 } 68 } 69 69 70 //____________________________________________ 70 //______________________________________________________________________ 71 G4KDNode_Base::G4KDNode_Base(G4KDTree* tree, 71 G4KDNode_Base::G4KDNode_Base(G4KDTree* tree, 72 G4KDNode_Base* parent): 72 G4KDNode_Base* parent): 73 fTree(tree), 73 fTree(tree), 74 fParent(parent) << 74 fLeft(0), fRight(0), fParent(parent) 75 { 75 { 76 fSide = 0; 76 fSide = 0; 77 fAxis = fParent == nullptr? 0 : fParent->fAx << 77 fAxis = fParent == 0? 0 : fParent->fAxis +1 < fTree->fDim? fParent->fAxis+1:0; 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_Base::G4KDNode_Base(const G4KDNode_Base& ): >> 82 fTree(0), >> 83 fLeft(0), fRight(0), fParent(0) 82 { 84 { 83 fSide = 0; 85 fSide = 0; 84 fAxis = 0; 86 fAxis = 0; 85 } 87 } 86 88 87 // Assignement should not be used 89 // Assignement should not be used 88 G4KDNode_Base& G4KDNode_Base::operator=(const 90 G4KDNode_Base& G4KDNode_Base::operator=(const G4KDNode_Base& right) 89 { 91 { 90 if (this == &right) return *this; 92 if (this == &right) return *this; 91 fTree = right.fTree; 93 fTree = right.fTree; 92 fLeft = right.fLeft; 94 fLeft = right.fLeft; 93 fRight = right.fRight; 95 fRight = right.fRight; 94 fParent = right.fParent; 96 fParent = right.fParent; 95 fSide = right.fSide; 97 fSide = right.fSide; 96 fAxis = right.fAxis; 98 fAxis = right.fAxis; 97 return *this; 99 return *this; 98 } 100 } 99 101 100 G4KDNode_Base::~G4KDNode_Base() 102 G4KDNode_Base::~G4KDNode_Base() 101 = default; << 103 { >> 104 } 102 105 103 void G4KDNode_Base::InactiveNode() 106 void G4KDNode_Base::InactiveNode() 104 { 107 { 105 fTree->NoticeNodeDeactivation(); 108 fTree->NoticeNodeDeactivation(); 106 } 109 } 107 110 108 G4int G4KDNode_Base::GetDim() const << 111 int G4KDNode_Base::GetDim() const 109 { 112 { 110 if(fTree != nullptr) return (G4int)fTree->Ge << 113 if(fTree) 111 return -1; << 114 return fTree->GetDim(); >> 115 else >> 116 return -1; 112 } 117 } 113 118 114 G4int G4KDNode_Base::Insert(G4KDNode_Base* new << 119 int G4KDNode_Base::Insert(G4KDNode_Base* newNode) 115 { 120 { 116 G4KDNode_Base* aParent = FindParent(*newNode 121 G4KDNode_Base* aParent = FindParent(*newNode); 117 // TODO check p == aParent->pos 122 // TODO check p == aParent->pos 118 // Exception 123 // Exception 119 124 120 newNode->fAxis = aParent->fAxis +1 < fTree-> 125 newNode->fAxis = aParent->fAxis +1 < fTree->GetDim()? aParent->fAxis+1:0; 121 newNode->fParent = aParent ; 126 newNode->fParent = aParent ; 122 127 123 if((*newNode)[aParent->fAxis] > (*aParent)[a 128 if((*newNode)[aParent->fAxis] > (*aParent)[aParent->fAxis]) 124 { 129 { 125 aParent->fRight = newNode ; 130 aParent->fRight = newNode ; 126 newNode->fSide = 1 ; 131 newNode->fSide = 1 ; 127 } 132 } 128 else 133 else 129 { 134 { 130 aParent->fLeft = newNode ; 135 aParent->fLeft = newNode ; 131 newNode->fSide = -1 ; 136 newNode->fSide = -1 ; 132 } 137 } 133 138 134 newNode->fRight = nullptr; << 139 newNode->fRight = 0; 135 newNode->fLeft = nullptr; << 140 newNode->fLeft = 0; 136 141 137 return 0 ; 142 return 0 ; 138 } 143 } 139 144 140 145 141 void G4KDNode_Base::PullSubTree() 146 void G4KDNode_Base::PullSubTree() 142 { 147 { 143 if(fParent != nullptr) << 148 if(fParent) 144 { 149 { 145 if(fSide == -1) 150 if(fSide == -1) 146 { 151 { 147 fParent->fLeft = nullptr; << 152 fParent->fLeft = 0; 148 } 153 } 149 else 154 else 150 fParent->fRight = nullptr; << 155 fParent->fRight = 0; 151 } 156 } 152 if(fLeft != nullptr) fLeft -> PullSubTree(); << 157 if(fLeft) fLeft -> PullSubTree(); 153 if(fRight != nullptr) fRight-> PullSubTree() << 158 if(fRight) fRight-> PullSubTree(); 154 159 155 fParent = nullptr ; << 160 fParent = 0 ; 156 fRight = nullptr ; << 161 fRight = 0 ; 157 fLeft = nullptr ; << 162 fLeft = 0 ; 158 fTree = nullptr ; << 163 fTree = 0 ; 159 } 164 } 160 165 161 void G4KDNode_Base::RetrieveNodeList(std::list 166 void G4KDNode_Base::RetrieveNodeList(std::list<G4KDNode_Base *>& output) 162 { 167 { 163 output.push_back(this); 168 output.push_back(this); 164 169 165 if(fLeft != nullptr) << 170 if(fLeft) 166 fLeft->RetrieveNodeList(output); 171 fLeft->RetrieveNodeList(output); 167 172 168 if(fRight != nullptr) << 173 if(fRight) 169 fRight->RetrieveNodeList(output); 174 fRight->RetrieveNodeList(output); 170 } 175 } 171 176 172 void G4KDNode_Base::Print(std::ostream& out, i 177 void G4KDNode_Base::Print(std::ostream& out, int level) const 173 { 178 { 174 // Print node level 179 // Print node level 175 out << G4endl; 180 out << G4endl; 176 for (G4int i=0; i<level; ++i) // Ind << 181 for (int i=0; i<level; i++) // Indent to level 177 { 182 { 178 out << " "; 183 out << " "; 179 } 184 } 180 out << level; 185 out << level; 181 186 182 // Print children 187 // Print children 183 if(fLeft != nullptr) << 188 if(fLeft) 184 { 189 { 185 fLeft->Print(out, level + 1); 190 fLeft->Print(out, level + 1); 186 } 191 } 187 if(fRight != nullptr) << 192 if(fRight) 188 { 193 { 189 fRight->Print(out, level + 1); 194 fRight->Print(out, level + 1); 190 } 195 } 191 } 196 } 192 197