Geant4-11
G4Voxelizer.hh
Go to the documentation of this file.
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
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
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;
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
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();
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
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
static const G4int amax[]
static const G4int amin[]
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
Definition: G4Box.hh:56
G4bool operator()(const G4int &l, const G4int &r) const
Definition: G4Voxelizer.hh:171
G4VoxelComparator(std::vector< G4VoxelInfo > &voxels)
Definition: G4Voxelizer.hh:169
std::vector< G4VoxelInfo > & fVoxels
Definition: G4Voxelizer.hh:167
const G4SurfBits & Empty() const
G4ThreeVector fBoundingBoxSize
Definition: G4Voxelizer.hh:250
long long CountVoxels(std::vector< G4double > boundaries[]) const
G4double fTolerance
Definition: G4Voxelizer.hh:256
G4double DistanceToBoundingBox(const G4ThreeVector &point) const
G4bool GetPointVoxel(const G4ThreeVector &p, std::vector< G4int > &voxels) const
long long GetCountOfVoxels() const
G4ThreeVector fBoundingBoxCenter
Definition: G4Voxelizer.hh:246
G4int fTotalCandidates
Definition: G4Voxelizer.hh:242
static void FindComponentsFastest(unsigned int mask, std::vector< G4int > &list, G4int i)
Definition: G4Voxelizer.cc:911
std::vector< G4VoxelBox > fBoxes
Definition: G4Voxelizer.hh:234
static G4ThreadLocal G4int fDefaultVoxelsCount
Definition: G4Voxelizer.hh:222
G4SurfBits fBitmasks[3]
Definition: G4Voxelizer.hh:244
std::vector< G4double > fBoundaries[3]
Definition: G4Voxelizer.hh:237
const std::vector< G4double > & GetBoundary(G4int index) const
const std::vector< G4VoxelBox > & GetBoxes() const
std::vector< G4VoxelBox > fVoxelBoxes
Definition: G4Voxelizer.hh:224
void BuildReduceVoxels(std::vector< G4double > fBoundaries[], G4ThreeVector reductionRatio)
Definition: G4Voxelizer.cc:515
G4int fNPerSlice
Definition: G4Voxelizer.hh:232
G4bool IsEmpty(G4int index) const
G4bool UpdateCurrentVoxel(const G4ThreeVector &point, const G4ThreeVector &direction, std::vector< G4int > &curVoxel) const
G4int fMaxVoxels
Definition: G4Voxelizer.hh:254
void DisplayListNodes() const
Definition: G4Voxelizer.cc:431
void GetVoxel(std::vector< G4int > &curVoxel, const G4ThreeVector &point) const
void DisplayVoxelLimits() const
Definition: G4Voxelizer.cc:197
void SetReductionRatio(G4int maxVoxels, G4ThreeVector &reductionRatio)
Definition: G4Voxelizer.cc:499
std::vector< std::vector< G4int > > fVoxelBoxesCandidates
Definition: G4Voxelizer.hh:225
G4SurfBits fEmpty
Definition: G4Voxelizer.hh:258
G4ThreeVector fReductionRatio
Definition: G4Voxelizer.hh:252
G4int GetMaxVoxels(G4ThreeVector &ratioOfReduction)
static void SetDefaultVoxelsCount(G4int count)
void BuildVoxelLimits(std::vector< G4VSolid * > &solids, std::vector< G4Transform3D > &transforms)
Definition: G4Voxelizer.cc:116
void BuildReduceVoxels2(std::vector< G4double > fBoundaries[], G4ThreeVector reductionRatio)
Definition: G4Voxelizer.cc:670
void BuildBoundingBox()
Definition: G4Voxelizer.cc:456
G4int GetBitsPerSlice() const
G4double DistanceToFirst(const G4ThreeVector &point, const G4ThreeVector &direction) const
void TransformLimits(G4ThreeVector &min, G4ThreeVector &max, const G4Transform3D &transformation) const
Definition: G4Voxelizer.cc:930
G4Box fBoundingBox
Definition: G4Voxelizer.hh:248
std::map< G4int, std::vector< G4int > > fCandidates
Definition: G4Voxelizer.hh:226
G4int GetCandidatesVoxelArray(const G4ThreeVector &point, std::vector< G4int > &list, G4SurfBits *crossed=nullptr) const
Definition: G4Voxelizer.cc:973
void BuildBoundaries()
Definition: G4Voxelizer.cc:244
G4int GetVoxelBoxesSize() const
std::vector< G4int > fCandidatesCounts[3]
Definition: G4Voxelizer.hh:240
static G4double MinDistanceToBox(const G4ThreeVector &aPoint, const G4ThreeVector &f)
void SetMaxVoxels(G4int max)
void BuildBitmasks(std::vector< G4double > fBoundaries[], G4SurfBits bitmasks[], G4bool countsOnly=false)
Definition: G4Voxelizer.cc:351
const G4VoxelBox & GetVoxelBox(G4int i) const
void GetCandidatesVoxel(std::vector< G4int > &voxels)
Definition: G4Voxelizer.cc:896
G4int AllocatedMemory()
void BuildEmpty()
Definition: G4Voxelizer.cc:73
G4int GetPointIndex(const G4ThreeVector &p) const
G4int GetVoxelsIndex(const std::vector< G4int > &voxels) const
long long fCountOfVoxels
Definition: G4Voxelizer.hh:230
static G4int GetDefaultVoxelsCount()
void DisplayBoundaries()
Definition: G4Voxelizer.cc:324
static G4int BinarySearch(const std::vector< T > &vec, T value)
G4String GetCandidatesAsString(const G4SurfBits &bits) const
Definition: G4Voxelizer.cc:416
const std::vector< G4int > fNoCandidates
Definition: G4Voxelizer.hh:228
G4double DistanceToNext(const G4ThreeVector &point, const G4ThreeVector &direction, std::vector< G4int > &curVoxel) const
void CreateSortedBoundary(std::vector< G4double > &boundaryRaw, G4int axis)
Definition: G4Voxelizer.cc:214
void Voxelize(std::vector< G4VSolid * > &solids, std::vector< G4Transform3D > &transforms)
Definition: G4Voxelizer.cc:713
G4ThreeVector GetGlobalPoint(const G4Transform3D &trans, const G4ThreeVector &lpoint) const
const std::vector< G4int > & GetVoxelBoxCandidates(G4int i) const
G4int GetVoxelsIndex(G4int x, G4int y, G4int z) const
G4bool Contains(const G4ThreeVector &point) const
const std::vector< G4int > & GetCandidates(std::vector< G4int > &curVoxel) const
G4int GetTotalCandidates() const
void CreateMiniVoxels(std::vector< G4double > fBoundaries[], G4SurfBits bitmasks[])
Definition: G4Voxelizer.cc:731
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
G4ThreeVector hlen
Definition: G4Voxelizer.hh:51
G4ThreeVector pos
Definition: G4Voxelizer.hh:52
G4int previous
Definition: G4Voxelizer.hh:58
#define G4ThreadLocal
Definition: tls.hh:77