2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
26// G4Voxelizer inline methods
28// 19.10.12 Marek Gayer, created
29// --------------------------------------------------------------------
31template <typename T> inline
32G4int G4Voxelizer::BinarySearch(const std::vector<T>& vec, T value)
34 typename std::vector<T>::const_iterator begin=vec.cbegin(), end=vec.cend();
35 return G4int(std::upper_bound(begin, end, value) - begin - 1);
39const std::vector<G4VoxelBox>& G4Voxelizer::GetBoxes() const
45const std::vector<G4double>& G4Voxelizer::GetBoundary(G4int index) const
47 return fBoundaries[index];
51void G4Voxelizer::GetVoxel(std::vector<G4int>& curVoxel,
52 const G4ThreeVector& point) const
54 for (auto i=0; i<=2; ++i)
56 const std::vector<double> &boundary = GetBoundary(i);
57 G4int n = BinarySearch(boundary, point[i]);
60 else if (n == G4int(boundary.size()) - 1)
67G4int G4Voxelizer::GetBitsPerSlice () const
69 return fNPerSlice*8*sizeof(unsigned int);
73G4int G4Voxelizer::GetVoxelsIndex(G4int x, G4int y, G4int z) const
75 if (x < 0 || y < 0 || z < 0) { return -1; }
76 std::size_t maxX = fBoundaries[0].size();
77 std::size_t maxY = fBoundaries[1].size();
79 return G4int(x + y*maxX + z*maxX*maxY);
83G4int G4Voxelizer::GetVoxelsIndex(const std::vector<G4int>& voxels) const
85 return GetVoxelsIndex(voxels[0], voxels[1], voxels[2]);
89G4bool G4Voxelizer::GetPointVoxel(const G4ThreeVector& p,
90 std::vector<G4int>& voxels) const
92 for (auto i = 0; i <= 2; ++i)
94 if (p[i] < *fBoundaries[i].begin() || p[i] > *fBoundaries[i].end())
99 for (auto i = 0; i <= 2; ++i)
101 voxels[i] = BinarySearch(fBoundaries[i], p[i]);
108G4int G4Voxelizer::GetPointIndex(const G4ThreeVector& p) const
110 std::size_t maxX = fBoundaries[0].size();
111 std::size_t maxY = fBoundaries[1].size();
112 G4int x = BinarySearch(fBoundaries[0], p[0]);
113 G4int y = BinarySearch(fBoundaries[1], p[1]);
114 G4int z = BinarySearch(fBoundaries[2], p[2]);
116 return G4int(x + y*maxX + z*maxX*maxY);
120const G4SurfBits& G4Voxelizer::Empty() const
126G4bool G4Voxelizer::IsEmpty(G4int index) const
128 return fEmpty[index];
132G4int G4Voxelizer::GetMaxVoxels(G4ThreeVector& ratioOfReduction)
134 ratioOfReduction = fReductionRatio;
139long long G4Voxelizer::GetCountOfVoxels() const
141 return fCountOfVoxels;
145long long G4Voxelizer::CountVoxels(std::vector<G4double> boundaries[]) const
147 long long sx = boundaries[0].size() - 1;
148 long long sy = boundaries[1].size() - 1;
149 long long sz = boundaries[2].size() - 1;
155const std::vector<G4int>&
156G4Voxelizer::GetCandidates(std::vector<G4int>& curVoxel) const
158 G4int voxelsIndex = GetVoxelsIndex(curVoxel);
160 if (voxelsIndex >= 0 && !fEmpty[voxelsIndex])
162 return fCandidates[voxelsIndex];
164 return fNoCandidates;
168G4int G4Voxelizer::GetTotalCandidates() const
170 return fTotalCandidates;
174G4int G4Voxelizer::GetVoxelBoxesSize() const
176 return G4int(fVoxelBoxes.size());
180const G4VoxelBox &G4Voxelizer::GetVoxelBox(G4int i) const
182 return fVoxelBoxes[i];
186const std::vector<G4int>&
187G4Voxelizer::GetVoxelBoxCandidates(G4int i) const
189 return fVoxelBoxesCandidates[i];
194G4Voxelizer::GetGlobalPoint(const G4Transform3D& trans,
195 const G4ThreeVector& local) const
197 // Returns global point coordinates converted from the local frame defined
198 // by the transformation. This is defined by multiplying this transformation
199 // with the local vector.
201 return trans*G4Point3D(local);