ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Voxelizer.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4Voxelizer.hh
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 // G4Voxelizer
27 //
28 // Class description:
29 //
30 // Voxelizer for tessellated surfaces and solids positioning in 3D space,
31 // used in G4TessellatedSolid and G4MultiUnion.
32 
33 // 19.10.12 Marek Gayer, created
34 // --------------------------------------------------------------------
35 #ifndef G4VOXELIZER_HH
36 #define G4VOXELIZER_HH
37 
38 #include <vector>
39 #include <string>
40 #include <map>
41 
42 #include "G4Transform3D.hh"
43 #include "G4RotationMatrix.hh"
44 #include "G4SurfBits.hh"
45 #include "G4Box.hh"
46 #include "G4VFacet.hh"
47 #include "G4VSolid.hh"
48 
49 struct G4VoxelBox
50 {
51  G4ThreeVector hlen; // half length of the box
52  G4ThreeVector pos; // position of the box
53 };
54 
56 {
60 };
61 
63 {
64  public:
65 
66  template <typename T>
67  static inline G4int BinarySearch(const std::vector<T>& vec, T value);
68 
69  void Voxelize(std::vector<G4VSolid*>& solids,
70  std::vector<G4Transform3D>& transforms);
71  void Voxelize(std::vector<G4VFacet*>& facets);
72 
73  void DisplayVoxelLimits() const;
74  void DisplayBoundaries();
75  void DisplayListNodes() const;
76 
77  G4Voxelizer();
78  ~G4Voxelizer();
79 
80  void GetCandidatesVoxel(std::vector<G4int>& voxels);
81  // Method displaying the nodes located in a voxel characterized
82  // by its three indexes.
83 
85  std::vector<G4int>& list,
86  G4SurfBits* crossed = nullptr) const;
87  // Method returning in a vector container the nodes located in a voxel
88  // characterized by its three indexes.
89  G4int GetCandidatesVoxelArray(const std::vector<G4int>& voxels,
90  const G4SurfBits bitmasks[],
91  std::vector<G4int>& list,
92  G4SurfBits* crossed = nullptr) const;
93  G4int GetCandidatesVoxelArray(const std::vector<G4int>& voxels,
94  std::vector<G4int>& list,
95  G4SurfBits* crossed = nullptr)const;
96 
97  inline const std::vector<G4VoxelBox>& GetBoxes() const;
98  // Method returning the pointer to the array containing the
99  // characteristics of each box.
100 
101  inline const std::vector<G4double>& GetBoundary(G4int index) const;
102 
104  const G4ThreeVector& direction,
105  std::vector<G4int>& curVoxel) const;
106 
107  inline void GetVoxel(std::vector<G4int>& curVoxel,
108  const G4ThreeVector& point) const;
109 
110  inline G4int GetBitsPerSlice () const;
111 
112  G4bool Contains(const G4ThreeVector& point) const;
113 
115  const G4ThreeVector& direction,
116  std::vector<G4int>& curVoxel) const;
117 
119  const G4ThreeVector& direction) const;
120 
121  G4double DistanceToBoundingBox(const G4ThreeVector& point) const;
122 
123  inline G4int GetVoxelsIndex(G4int x, G4int y, G4int z) const;
124  inline G4int GetVoxelsIndex(const std::vector<G4int>& voxels) const;
125  inline G4bool GetPointVoxel(const G4ThreeVector& p,
126  std::vector<G4int>& voxels) const;
127  inline G4int GetPointIndex(const G4ThreeVector& p) const;
128 
129  inline const G4SurfBits& Empty() const;
130  inline G4bool IsEmpty(G4int index) const;
131 
132  void SetMaxVoxels(G4int max);
133  void SetMaxVoxels(const G4ThreeVector& reductionRatio);
134 
135  inline G4int GetMaxVoxels(G4ThreeVector& ratioOfReduction);
136 
138 
139  inline long long GetCountOfVoxels() const;
140 
141  inline long long CountVoxels(std::vector<G4double> boundaries[]) const;
142 
143  inline const std::vector<G4int>&
144  GetCandidates(std::vector<G4int>& curVoxel) const;
145 
146  inline G4int GetVoxelBoxesSize() const;
147 
148  inline const G4VoxelBox &GetVoxelBox(G4int i) const;
149 
150  inline const std::vector<G4int>& GetVoxelBoxCandidates(G4int i) const;
151 
152  inline G4int GetTotalCandidates() const;
153 
154  static G4double MinDistanceToBox (const G4ThreeVector& aPoint,
155  const G4ThreeVector& f);
156 
157  static void SetDefaultVoxelsCount(G4int count);
158 
159  static G4int GetDefaultVoxelsCount();
160 
161  private:
162 
164  {
165  public:
166 
167  std::vector<G4VoxelInfo>& fVoxels;
168 
169  G4VoxelComparator(std::vector<G4VoxelInfo>& voxels) : fVoxels(voxels) {}
170 
171  G4bool operator()(const G4int& l, const G4int& r) const
172  {
173  G4VoxelInfo &lv = fVoxels[l], &rv = fVoxels[r];
174  G4int left = lv.count + fVoxels[lv.next].count;
175  G4int right = rv.count + fVoxels[rv.next].count;
176  return (left == right) ? l < r : left < right;
177  }
178  };
179 
180  private:
181 
182  void BuildEmpty ();
183 
184  G4String GetCandidatesAsString(const G4SurfBits& bits) const;
185 
186  void CreateSortedBoundary(std::vector<G4double>& boundaryRaw, G4int axis);
187 
188  void BuildBoundaries();
189 
190  void BuildReduceVoxels(std::vector<G4double> fBoundaries[],
191  G4ThreeVector reductionRatio);
192  void BuildReduceVoxels2(std::vector<G4double> fBoundaries[],
193  G4ThreeVector reductionRatio);
194 
195  void BuildVoxelLimits(std::vector<G4VSolid*>& solids,
196  std::vector<G4Transform3D>& transforms);
197  void BuildVoxelLimits(std::vector<G4VFacet*>& facets);
198 
199  void DisplayBoundaries(std::vector<G4double>& fBoundaries);
200 
201  void BuildBitmasks(std::vector<G4double> fBoundaries[],
202  G4SurfBits bitmasks[], G4bool countsOnly = false);
203 
204  void BuildBoundingBox();
205  void BuildBoundingBox(G4ThreeVector& amin, G4ThreeVector& amax,
206  G4double tolerance = 0.0);
207 
208  void SetReductionRatio(G4int maxVoxels, G4ThreeVector& reductionRatio);
209 
210  void CreateMiniVoxels(std::vector<G4double> fBoundaries[],
211  G4SurfBits bitmasks[]);
212  static void FindComponentsFastest(unsigned int mask,
213  std::vector<G4int>& list, G4int i);
214 
215  inline G4ThreeVector GetGlobalPoint(const G4Transform3D& trans,
216  const G4ThreeVector& lpoint) const;
218  const G4Transform3D& transformation) const;
219 
220  private:
221 
223 
224  std::vector<G4VoxelBox> fVoxelBoxes;
225  std::vector<std::vector<G4int> > fVoxelBoxesCandidates;
226  mutable std::map<G4int, std::vector<G4int> > fCandidates;
227 
228  const std::vector<G4int> fNoCandidates;
229 
230  long long fCountOfVoxels;
231 
233 
234  std::vector<G4VoxelBox> fBoxes;
235  // Array of box limits on the 3 cartesian axis
236 
237  std::vector<G4double> fBoundaries[3];
238  // Sorted and if need skimmed fBoundaries along X,Y,Z axis
239 
240  std::vector<G4int> fCandidatesCounts[3];
241 
243 
245 
247 
249 
251 
253 
255 
257 
259 };
260 
261 #include "G4Voxelizer.icc"
262 
263 #endif