[831] | 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 | // |
---|
[850] | 27 | // $Id: G4SurfaceList.cc,v 1.8 2008/03/13 14:18:57 gcosmo Exp $ |
---|
| 28 | // GEANT4 tag $Name: HEAD $ |
---|
[831] | 29 | // |
---|
| 30 | // ---------------------------------------------------------------------- |
---|
| 31 | // GEANT 4 class source file |
---|
| 32 | // |
---|
| 33 | // G4SurfaceList.cc |
---|
| 34 | // |
---|
| 35 | // ---------------------------------------------------------------------- |
---|
| 36 | |
---|
| 37 | #include "G4SurfaceList.hh" |
---|
| 38 | |
---|
| 39 | G4SurfaceList::G4SurfaceList() |
---|
| 40 | { |
---|
| 41 | first = index = last = next = temp = (G4Surface*)0; |
---|
| 42 | number_of_elements=0; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | G4SurfaceList::~G4SurfaceList() |
---|
| 47 | { |
---|
| 48 | EmptyList(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void G4SurfaceList::MoveToFirst(G4Surface* srf) |
---|
| 52 | { |
---|
| 53 | if(number_of_elements) |
---|
| 54 | { |
---|
| 55 | RemovePointer(); |
---|
| 56 | srf->SetNextNode(first); |
---|
| 57 | first = srf; |
---|
| 58 | index=first; |
---|
| 59 | number_of_elements++; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | void G4SurfaceList::AddSurface(G4Surface* srf) |
---|
| 65 | { |
---|
| 66 | if(first == (G4Surface*)0) |
---|
| 67 | { |
---|
| 68 | index = srf; |
---|
| 69 | first = srf; |
---|
| 70 | last = srf; |
---|
| 71 | first->SetNextNode(0); |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | srf->SetNextNode(last->GetNextNode()); |
---|
| 76 | last->SetNextNode(srf); |
---|
| 77 | last = last->GetNextNode(); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | number_of_elements++; |
---|
| 81 | index=first; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | G4Surface* G4SurfaceList::GetSurface() |
---|
| 86 | { |
---|
| 87 | return index; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | const G4Surface* G4SurfaceList::GetSurface(G4int number) |
---|
| 92 | { |
---|
| 93 | index = first; |
---|
| 94 | for(G4int a=0;a<number;a++) |
---|
| 95 | Step(); |
---|
| 96 | |
---|
| 97 | return index; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | const G4Surface* G4SurfaceList::GetLastSurface() const |
---|
| 102 | { |
---|
| 103 | return last; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | void G4SurfaceList::RemoveSurface(G4Surface* srf) |
---|
| 108 | { |
---|
| 109 | if(srf!=(G4Surface*)0) |
---|
| 110 | { |
---|
| 111 | number_of_elements--; |
---|
| 112 | temp = first; |
---|
| 113 | |
---|
| 114 | if(srf == first) |
---|
| 115 | { |
---|
| 116 | first=first->GetNextNode(); |
---|
| 117 | index = first; |
---|
| 118 | if(number_of_elements == 0)last = first; |
---|
| 119 | delete srf; |
---|
| 120 | return; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | while(temp->GetNextNode() != srf) temp = temp->GetNextNode(); |
---|
| 125 | index = srf->GetNextNode(); |
---|
| 126 | temp->SetNextNode(index); |
---|
| 127 | if(srf == last) last = temp; |
---|
| 128 | index = first; |
---|
| 129 | delete srf; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | void G4SurfaceList::RemovePointer() |
---|
| 136 | { |
---|
| 137 | // Remove the current pointer from the List |
---|
| 138 | // Do not delete the object itself |
---|
| 139 | if(number_of_elements) |
---|
| 140 | { |
---|
| 141 | if(first != index) |
---|
| 142 | { |
---|
| 143 | temp = first; |
---|
| 144 | |
---|
| 145 | // Find previous |
---|
| 146 | while(temp->GetNextNode() != index) temp = temp->GetNextNode(); |
---|
| 147 | |
---|
| 148 | // Hop over the one to be removed |
---|
| 149 | temp->SetNextNode(index->GetNextNode()); |
---|
| 150 | |
---|
| 151 | // Correct the index pointer |
---|
| 152 | index = temp->GetNextNode(); |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | // Hop over the first |
---|
| 157 | first = first->GetNextNode(); |
---|
| 158 | index = first; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | number_of_elements--; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | void G4SurfaceList::EmptyList() |
---|
| 167 | { |
---|
| 168 | //Deletes all surfaces in List |
---|
| 169 | while (first != (G4Surface*)0) |
---|
| 170 | { |
---|
| 171 | temp = first; |
---|
| 172 | first = first->GetNextNode(); |
---|
| 173 | delete temp; |
---|
| 174 | number_of_elements--; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | last = index = first; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | void G4SurfaceList::MoveToFirst() |
---|
| 182 | { |
---|
| 183 | index = first; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | void G4SurfaceList::Step() |
---|
| 188 | { |
---|
| 189 | if(index!=(G4Surface*)0) |
---|
| 190 | index = index->GetNextNode(); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | void G4SurfaceList::G4SortList() |
---|
| 195 | { |
---|
| 196 | if(number_of_elements == 1) return; |
---|
| 197 | |
---|
| 198 | // First create a vector of the surface distances |
---|
| 199 | // to the ray origin |
---|
| 200 | G4Surface** distances = new G4Surface*[number_of_elements]; |
---|
| 201 | G4int x = 0; |
---|
| 202 | MoveToFirst(); |
---|
| 203 | |
---|
| 204 | // Copy surface pointers to vector |
---|
| 205 | if(number_of_elements > 1) |
---|
| 206 | { |
---|
| 207 | while(x < number_of_elements) |
---|
| 208 | { |
---|
| 209 | distances[x] = index; |
---|
| 210 | index = index->GetNextNode(); |
---|
| 211 | x++; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | MoveToFirst(); |
---|
| 215 | |
---|
| 216 | // Sort List of pointers using quick G4Sort |
---|
| 217 | QuickG4Sort( distances, 0, number_of_elements-1 ); |
---|
| 218 | |
---|
| 219 | // Organize the linked List of surfaces according |
---|
| 220 | // to the quickG4Sorted List. |
---|
| 221 | x = 0; |
---|
| 222 | first = distances[x]; |
---|
| 223 | last = first; |
---|
| 224 | x++; |
---|
| 225 | |
---|
| 226 | while (x < number_of_elements) |
---|
| 227 | { |
---|
| 228 | last->SetNextNode(distances[x]); |
---|
| 229 | last = last->GetNextNode(); |
---|
| 230 | x++; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | last->SetNextNode(0); |
---|
| 234 | MoveToFirst(); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | delete[] distances; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | void G4SurfaceList::QuickG4Sort(G4Surface** Dist, G4int left, G4int right) |
---|
| 242 | { |
---|
| 243 | register G4int i=left; |
---|
| 244 | register G4int j=right; |
---|
| 245 | |
---|
| 246 | G4Surface* elem1; |
---|
| 247 | G4Surface* elem2 = Dist[(left+right)/2]; |
---|
| 248 | |
---|
| 249 | do |
---|
| 250 | { |
---|
| 251 | while ( (Dist[i]->GetDistance() < elem2->GetDistance()) && (i < right) ) |
---|
| 252 | i++; |
---|
| 253 | |
---|
| 254 | while ( (elem2->GetDistance() < Dist[j]->GetDistance()) && (j > left)) |
---|
| 255 | j--; |
---|
| 256 | |
---|
| 257 | if(i<=j) |
---|
| 258 | { |
---|
| 259 | elem1 = Dist[i]; |
---|
| 260 | Dist[i] = Dist[j]; |
---|
| 261 | Dist[j] = elem1; |
---|
| 262 | i++; |
---|
| 263 | j--; |
---|
| 264 | } |
---|
| 265 | } while (i<=j); |
---|
| 266 | |
---|
| 267 | if( left < j ) |
---|
| 268 | QuickG4Sort(Dist, left, j ); |
---|
| 269 | |
---|
| 270 | if( i < right ) |
---|
| 271 | QuickG4Sort(Dist, i, right); |
---|
| 272 | } |
---|