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 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 fParent(parent) 75 { 75 { 76 fSide = 0; 76 fSide = 0; 77 fAxis = fParent == nullptr? 0 : fParent->fAx 77 fAxis = fParent == nullptr? 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 { 82 { 83 fSide = 0; 83 fSide = 0; 84 fAxis = 0; 84 fAxis = 0; 85 } 85 } 86 86 87 // Assignement should not be used 87 // Assignement should not be used 88 G4KDNode_Base& G4KDNode_Base::operator=(const 88 G4KDNode_Base& G4KDNode_Base::operator=(const G4KDNode_Base& right) 89 { 89 { 90 if (this == &right) return *this; 90 if (this == &right) return *this; 91 fTree = right.fTree; 91 fTree = right.fTree; 92 fLeft = right.fLeft; 92 fLeft = right.fLeft; 93 fRight = right.fRight; 93 fRight = right.fRight; 94 fParent = right.fParent; 94 fParent = right.fParent; 95 fSide = right.fSide; 95 fSide = right.fSide; 96 fAxis = right.fAxis; 96 fAxis = right.fAxis; 97 return *this; 97 return *this; 98 } 98 } 99 99 100 G4KDNode_Base::~G4KDNode_Base() 100 G4KDNode_Base::~G4KDNode_Base() 101 = default; 101 = default; 102 102 103 void G4KDNode_Base::InactiveNode() 103 void G4KDNode_Base::InactiveNode() 104 { 104 { 105 fTree->NoticeNodeDeactivation(); 105 fTree->NoticeNodeDeactivation(); 106 } 106 } 107 107 108 G4int G4KDNode_Base::GetDim() const 108 G4int G4KDNode_Base::GetDim() const 109 { 109 { 110 if(fTree != nullptr) return (G4int)fTree->Ge 110 if(fTree != nullptr) return (G4int)fTree->GetDim(); 111 return -1; 111 return -1; 112 } 112 } 113 113 114 G4int G4KDNode_Base::Insert(G4KDNode_Base* new 114 G4int G4KDNode_Base::Insert(G4KDNode_Base* newNode) 115 { 115 { 116 G4KDNode_Base* aParent = FindParent(*newNode 116 G4KDNode_Base* aParent = FindParent(*newNode); 117 // TODO check p == aParent->pos 117 // TODO check p == aParent->pos 118 // Exception 118 // Exception 119 119 120 newNode->fAxis = aParent->fAxis +1 < fTree-> 120 newNode->fAxis = aParent->fAxis +1 < fTree->GetDim()? aParent->fAxis+1:0; 121 newNode->fParent = aParent ; 121 newNode->fParent = aParent ; 122 122 123 if((*newNode)[aParent->fAxis] > (*aParent)[a 123 if((*newNode)[aParent->fAxis] > (*aParent)[aParent->fAxis]) 124 { 124 { 125 aParent->fRight = newNode ; 125 aParent->fRight = newNode ; 126 newNode->fSide = 1 ; 126 newNode->fSide = 1 ; 127 } 127 } 128 else 128 else 129 { 129 { 130 aParent->fLeft = newNode ; 130 aParent->fLeft = newNode ; 131 newNode->fSide = -1 ; 131 newNode->fSide = -1 ; 132 } 132 } 133 133 134 newNode->fRight = nullptr; 134 newNode->fRight = nullptr; 135 newNode->fLeft = nullptr; 135 newNode->fLeft = nullptr; 136 136 137 return 0 ; 137 return 0 ; 138 } 138 } 139 139 140 140 141 void G4KDNode_Base::PullSubTree() 141 void G4KDNode_Base::PullSubTree() 142 { 142 { 143 if(fParent != nullptr) 143 if(fParent != nullptr) 144 { 144 { 145 if(fSide == -1) 145 if(fSide == -1) 146 { 146 { 147 fParent->fLeft = nullptr; 147 fParent->fLeft = nullptr; 148 } 148 } 149 else 149 else 150 fParent->fRight = nullptr; 150 fParent->fRight = nullptr; 151 } 151 } 152 if(fLeft != nullptr) fLeft -> PullSubTree(); 152 if(fLeft != nullptr) fLeft -> PullSubTree(); 153 if(fRight != nullptr) fRight-> PullSubTree() 153 if(fRight != nullptr) fRight-> PullSubTree(); 154 154 155 fParent = nullptr ; 155 fParent = nullptr ; 156 fRight = nullptr ; 156 fRight = nullptr ; 157 fLeft = nullptr ; 157 fLeft = nullptr ; 158 fTree = nullptr ; 158 fTree = nullptr ; 159 } 159 } 160 160 161 void G4KDNode_Base::RetrieveNodeList(std::list 161 void G4KDNode_Base::RetrieveNodeList(std::list<G4KDNode_Base *>& output) 162 { 162 { 163 output.push_back(this); 163 output.push_back(this); 164 164 165 if(fLeft != nullptr) 165 if(fLeft != nullptr) 166 fLeft->RetrieveNodeList(output); 166 fLeft->RetrieveNodeList(output); 167 167 168 if(fRight != nullptr) 168 if(fRight != nullptr) 169 fRight->RetrieveNodeList(output); 169 fRight->RetrieveNodeList(output); 170 } 170 } 171 171 172 void G4KDNode_Base::Print(std::ostream& out, i 172 void G4KDNode_Base::Print(std::ostream& out, int level) const 173 { 173 { 174 // Print node level 174 // Print node level 175 out << G4endl; 175 out << G4endl; 176 for (G4int i=0; i<level; ++i) // Ind 176 for (G4int i=0; i<level; ++i) // Indent to level 177 { 177 { 178 out << " "; 178 out << " "; 179 } 179 } 180 out << level; 180 out << level; 181 181 182 // Print children 182 // Print children 183 if(fLeft != nullptr) 183 if(fLeft != nullptr) 184 { 184 { 185 fLeft->Print(out, level + 1); 185 fLeft->Print(out, level + 1); 186 } 186 } 187 if(fRight != nullptr) 187 if(fRight != nullptr) 188 { 188 { 189 fRight->Print(out, level + 1); 189 fRight->Print(out, level + 1); 190 } 190 } 191 } 191 } 192 192