56 : fBoundingBox(
"VoxBBox", 1, 1, 1)
58 fCountOfVoxels = fNPerSlice = fTotalCandidates = 0;
73void G4Voxelizer::BuildEmpty()
78 std::vector<G4int> xyz(3), max(3), candidates(fTotalCandidates);
79 const std::vector<G4int> empty(0);
81 for (
auto i = 0; i <= 2; ++i) max[i] = fBoundaries[i].size();
82 unsigned int size = max[0] * max[1] * max[2];
88 for (xyz[2] = 0; xyz[2] < max[2]; ++xyz[2])
90 for (xyz[1] = 0; xyz[1] < max[1]; ++xyz[1])
92 for (xyz[0] = 0; xyz[0] < max[0]; ++xyz[0])
103 std::vector<G4int> &c = (fCandidates[index] = empty);
104 c.reserve(candidates.size());
105 c.assign(candidates.begin(), candidates.end());
111 G4cout <<
"Non-empty voxels count: " << fCandidates.size() <<
G4endl;
116void G4Voxelizer::BuildVoxelLimits(std::vector<G4VSolid*>& solids,
117 std::vector<G4Transform3D>& transforms)
128 if (
G4int numNodes = solids.size())
130 fBoxes.resize(numNodes);
131 fNPerSlice = 1 + (fBoxes.size() - 1) / (8 *
sizeof(
unsigned int));
136 G4ThreeVector toleranceVector(fTolerance,fTolerance,fTolerance);
138 for (
G4int i = 0; i < numNodes; ++i)
150 orbToleranceVector.
set(tolerance,tolerance,tolerance);
151 min -= orbToleranceVector;
152 max += orbToleranceVector;
156 min -= toleranceVector;
157 max += toleranceVector;
159 TransformLimits(min, max, transform);
160 fBoxes[i].hlen = (
max -
min) / 2;
164 fTotalCandidates = fBoxes.size();
169void G4Voxelizer::BuildVoxelLimits(std::vector<G4VFacet*>& facets)
176 if (
G4int numNodes = facets.size())
178 fBoxes.resize(numNodes);
179 fNPerSlice = 1+(fBoxes.size()-1)/(8*
sizeof(
unsigned int));
181 G4ThreeVector toleranceVector(10*fTolerance, 10*fTolerance, 10*fTolerance);
183 for (
G4int i = 0; i < numNodes; ++i)
191 min -= toleranceVector;
192 max += toleranceVector;
194 fBoxes[i].hlen = hlen;
195 fBoxes[i].pos =
min + hlen;
197 fTotalCandidates = fBoxes.size();
206 G4int numNodes = fBoxes.size();
208 for(
G4int i = 0; i < numNodes; ++i)
210 G4cout << setw(10) << setiosflags(ios::fixed) <<
211 " -> Node " << i+1 <<
":\n" <<
212 "\t * [x,y,z] = " << fBoxes[i].hlen <<
213 "\t * [x,y,z] = " << fBoxes[i].pos <<
"\n";
215 G4cout.precision(oldprec);
219void G4Voxelizer::CreateSortedBoundary(std::vector<G4double>& boundary,
226 G4int numNodes = fBoxes.size();
230 for(
G4int i = 0 ; i < numNodes; ++i)
235 G4double p = fBoxes[i].pos[axis], d = fBoxes[i].hlen[axis];
240 G4cout <<
"Boundary " << p - d <<
" - " << p + d <<
G4endl;
242 boundary[2*i] = p - d;
243 boundary[2*i+1] = p + d;
245 std::sort(boundary.begin(), boundary.end());
249void G4Voxelizer::BuildBoundaries()
261 if (
G4int numNodes = fBoxes.size())
263 const G4double tolerance = fTolerance / 100.0;
266 std::vector<G4double> sortedBoundary(2*numNodes);
270 for (
auto j = 0; j <= 2; ++j)
272 CreateSortedBoundary(sortedBoundary, j);
273 std::vector<G4double> &boundary = fBoundaries[j];
278 for(
G4int i = 0 ; i < 2*numNodes; ++i)
280 G4double newBoundary = sortedBoundary[i];
282 if (j == 0)
G4cout <<
"Examining " << newBoundary <<
"..." <<
G4endl;
284 G4int size = boundary.size();
285 if(!size || std::abs(boundary[size-1] - newBoundary) > tolerance)
290 if (j == 0)
G4cout <<
"Adding boundary " << newBoundary <<
"..."
293 boundary.push_back(newBoundary);
301 G4int n = boundary.size();
307 std::vector<G4double> reduced;
308 for (
G4int i = 0; i <
n; ++i)
311 G4int size = boundary.size();
312 if (i % skip == 0 || i == 0 || i == size - 1)
319 reduced.push_back(boundary[i]);
331 char axis[3] = {
'X',
'Y',
'Z'};
332 for (
auto i = 0; i <= 2; ++i)
334 G4cout <<
" * " << axis[i] <<
" axis:" <<
G4endl <<
" | ";
344 G4int count = boundaries.size();
346 for(
G4int i = 0; i < count; ++i)
348 G4cout << setw(10) << setiosflags(ios::fixed) << boundaries[i];
349 if(i != count-1)
G4cout <<
"-> ";
352 G4cout.precision(oldprec);
356void G4Voxelizer::BuildBitmasks(std::vector<G4double> boundaries[],
362 G4int numNodes = fBoxes.size();
365 for (
auto k = 0; k < 3; ++k)
368 std::vector<G4double>& boundary = boundaries[k];
369 G4int voxelsCount = boundary.size() - 1;
378 bitmask.
SetBitNumber(voxelsCount*bitsPerSlice-1,
false);
382 std::vector<G4int>& candidatesCount = fCandidatesCounts[k];
383 candidatesCount.resize(voxelsCount);
385 for(
G4int i = 0 ; i < voxelsCount; ++i) { candidatesCount[i] = 0; }
389 for(
G4int j = 0 ; j < numNodes; ++j)
394 G4double p = fBoxes[j].pos[k], d = fBoxes[j].hlen[k];
400 if (i < 0) { i = 0; }
408 candidatesCount[i]++;
412 while (max > boundary[i] && i < voxelsCount);
426 G4int numNodes = fBoxes.size();
428 for(
G4int i=0; i<numNodes; ++i)
440 char axis[3] = {
'X',
'Y',
'Z'};
444 for (
auto j = 0; j <= 2; ++j)
447 G4int count = fBoundaries[j].size();
448 for(
G4int i=0; i < count-1; ++i)
450 G4cout <<
" Slice #" << i+1 <<
": [" << fBoundaries[j][i]
451 <<
" ; " << fBoundaries[j][i+1] <<
"] -> ";
452 bits.
set(size,(
const char *)fBitmasks[j].fAllBits+i
453 *fNPerSlice*
sizeof(
G4int));
454 G4String result = GetCandidatesAsString(bits);
461void G4Voxelizer::BuildBoundingBox()
464 fBoundaries[1].front(),
465 fBoundaries[2].front());
467 fBoundaries[1].back(),
468 fBoundaries[2].back());
469 BuildBoundingBox(min, max);
477 for (
auto i = 0; i <= 2; ++i)
481 fBoundingBoxSize[i] = (
max -
min) / 2 + tolerance * 0.5;
482 fBoundingBoxCenter[i] =
min + fBoundingBoxSize[i];
504void G4Voxelizer::SetReductionRatio(
G4int maxVoxels,
508 * fCandidatesCounts[1].size() * fCandidatesCounts[2].size();
510 if (maxVoxels > 0 && maxVoxels < maxTotal)
513 ratio = std::pow(ratio, 1./3.);
514 if (ratio > 1) { ratio = 1; }
515 reductionRatio.
set(ratio,ratio,ratio);
520void G4Voxelizer::BuildReduceVoxels(std::vector<G4double> boundaries[],
523 for (
auto k = 0; k <= 2; ++k)
525 std::vector<G4int> &candidatesCount = fCandidatesCounts[k];
527 std::vector<G4VoxelInfo> voxels(max);
528 G4VoxelComparator comp(voxels);
529 std::set<G4int, G4VoxelComparator> voxelSet(comp);
530 std::vector<G4int> mergings;
535 voxel.
count = candidatesCount[j];
541 for (
G4int j = 0; j <
max - 1; ++j) { voxelSet.insert(j); }
544 G4double reduction = reductionRatio[k];
547 G4int count = 0, currentCount;
548 while ((currentCount = voxelSet.size()) > 2)
551 if ((currentRatio <= reduction) && (currentCount <= 1000))
553 const G4int pos = *voxelSet.begin();
554 mergings.push_back(pos + 1);
559 if (voxelSet.erase(pos) != 1)
563 if (voxel.
next != max - 1)
564 if (voxelSet.erase(voxel.
next) != 1)
569 if (voxelSet.erase(voxel.
previous) != 1)
577 if (voxel.
next != max - 1)
578 voxelSet.insert(voxel.
next);
591 std::sort(mergings.begin(), mergings.end());
593 const std::vector<G4double>& boundary = boundaries[k];
594 int mergingsSize = mergings.size();
595 vector<G4double> reducedBoundary;
596 G4int skip = mergings[0], i = 0;
597 max = boundary.size();
602 reducedBoundary.push_back(boundary[j]);
604 else if (++i < mergingsSize)
609 boundaries[k] = reducedBoundary;
675void G4Voxelizer::BuildReduceVoxels2(std::vector<G4double> boundaries[],
678 for (
auto k = 0; k <= 2; ++k)
680 std::vector<G4int> &candidatesCount = fCandidatesCounts[k];
683 for (
G4int i = 0; i <
max; ++i) total += candidatesCount[i];
685 G4double reduction = reductionRatio[k];
689 G4int destination = (
G4int) (reduction * max) + 1;
690 if (destination > 1000) destination = 1000;
691 if (destination < 2) destination = 2;
694 std::vector<G4int> mergings;
696 std::vector<G4double> &boundary = boundaries[k];
697 std::vector<G4double> reducedBoundary(destination);
699 G4int sum = 0, cur = 0;
702 sum += candidatesCount[i];
703 if (sum > average * (cur + 1) || i == 0)
706 reducedBoundary[cur] = val;
708 if (cur == destination)
712 reducedBoundary[destination-1] = boundary[
max];
713 boundaries[k] = reducedBoundary;
719 std::vector<G4Transform3D>& transforms)
721 BuildVoxelLimits(solids, transforms);
723 BuildBitmasks(fBoundaries, fBitmasks);
729 for (
auto i = 0; i < 3; ++i)
731 fCandidatesCounts[i].resize(0);
736void G4Voxelizer::CreateMiniVoxels(std::vector<G4double> boundaries[],
739 std::vector<G4int> voxel(3), maxVoxels(3);
740 for (
auto i = 0; i <= 2; ++i) maxVoxels[i] = boundaries[i].size();
743 for (voxel[2] = 0; voxel[2] < maxVoxels[2] - 1; ++voxel[2])
745 for (voxel[1] = 0; voxel[1] < maxVoxels[1] - 1; ++voxel[1])
747 for (voxel[0] = 0; voxel[0] < maxVoxels[0] - 1; ++voxel[0])
749 std::vector<G4int> candidates;
754 for (
auto i = 0; i <= 2; ++i)
756 G4int index = voxel[i];
757 const std::vector<G4double> &boundary = boundaries[i];
758 G4double hlen = 0.5 * (boundary[index+1] - boundary[index]);
760 box.
pos[i] = boundary[index] + hlen;
762 fVoxelBoxes.push_back(box);
763 std::vector<G4int>(candidates).swap(candidates);
764 fVoxelBoxesCandidates.push_back(candidates);
774 G4int maxVoxels = fMaxVoxels;
777 G4int size = facets.size();
780 for (
G4int i = 0; i < (
G4int) facets.size(); ++i)
782 if (facets[i]->GetNumberOfVertices() > 3) size++;
786 if ((size >= 10 || maxVoxels > 0) && maxVoxels != 0 && maxVoxels != 1)
792 BuildVoxelLimits(facets);
804 BuildBitmasks(fBoundaries, 0,
true);
808 maxVoxels = fTotalCandidates;
809 if (fTotalCandidates > 1000000) maxVoxels = 1000000;
812 SetReductionRatio(maxVoxels, reductionRatio);
817 G4cout <<
"Total number of voxels: " << fCountOfVoxels <<
G4endl;
820 BuildReduceVoxels2(fBoundaries, reductionRatio);
825 G4cout <<
"Total number of voxels after reduction: "
826 << fCountOfVoxels <<
G4endl;
833 BuildBitmasks(fBoundaries, fBitmasks);
841 std::vector<G4double> miniBoundaries[3];
843 for (
auto i = 0; i <= 2; ++i) { miniBoundaries[i] = fBoundaries[i]; }
845 G4int voxelsCountMini = (fCountOfVoxels >= 1000)
846 ? 100 : fCountOfVoxels / 10;
848 SetReductionRatio(voxelsCountMini, reductionRatioMini);
854 BuildReduceVoxels(miniBoundaries, reductionRatioMini);
858 G4cout <<
"Total number of mini voxels: " << total <<
G4endl;
865 BuildBitmasks(miniBoundaries, bitmasksMini);
871 CreateMiniVoxels(miniBoundaries, bitmasksMini);
886 G4cout <<
"Deallocating unnecessary fields during runtime..." <<
G4endl;
891 for (
auto i = 0; i < 3; ++i)
893 fCandidatesCounts[i].resize(0);
894 fBitmasks[i].
Clear();
906 G4cout <<
" Candidates in voxel [" << voxels[0] <<
" ; " << voxels[1]
907 <<
" ; " << voxels[2] <<
"]: ";
908 std::vector<G4int> candidates;
911 for (
G4int i = 0; i < count; ++i)
G4cout << candidates[i];
916void G4Voxelizer::FindComponentsFastest(
unsigned int mask,
917 std::vector<G4int>& list,
G4int i)
919 for (
G4int byte = 0;
byte < (
G4int) (
sizeof(
unsigned int)); ++byte)
921 if (
G4int maskByte = mask & 0xFF)
923 for (
G4int bit = 0; bit < 8; ++bit)
926 { list.push_back(8*(
sizeof(
unsigned int)*i+
byte) + bit); }
927 if (!(maskByte >>= 1))
break;
954 min.set(kInfinity,kInfinity,kInfinity);
955 max.set(-kInfinity,-kInfinity,-kInfinity);
959 for (
G4int i = 0 ; i < limit; ++i)
963 G4ThreeVector current = GetGlobalPoint(transformation, vertices[i]);
966 if (current.
x() >
max.x())
max.setX(current.
x());
967 if (current.
x() <
min.x())
min.setX(current.
x());
969 if (current.
y() >
max.y())
max.setY(current.
y());
970 if (current.
y() <
min.y())
min.setY(current.
y());
972 if (current.
z() >
max.z())
max.setZ(current.
z());
973 if (current.
z() <
min.z())
min.setZ(current.
z());
979 std::vector<G4int> &list,
G4SurfBits *crossed)
const
985 for (
auto i = 0; i <= 2; ++i)
987 if(point[i] < fBoundaries[i].front() || point[i] >= fBoundaries[i].back())
991 if (fTotalCandidates == 1)
1000 unsigned int mask = 0xFFffFFff;
1002 if (fBoundaries[0].size() > 2)
1005 if (!(mask = ((
unsigned int*) fBitmasks[0].fAllBits)[slice]))
1008 if (fBoundaries[1].size() > 2)
1011 if (!(mask &= ((
unsigned int*) fBitmasks[1].fAllBits)[slice]))
1014 if (fBoundaries[2].size() > 2)
1017 if (!(mask &= ((
unsigned int*) fBitmasks[2].fAllBits)[slice]))
1020 if (crossed && (!(mask &= ~((
unsigned int*)crossed->
fAllBits)[0])))
1023 FindComponentsFastest(mask, list, 0);
1027 unsigned int* masks[3], mask;
1028 for (
auto i = 0; i <= 2; ++i)
1031 masks[i] = ((
unsigned int*) fBitmasks[i].fAllBits)
1032 + slice * fNPerSlice;
1034 unsigned int* maskCrossed = crossed
1035 ? (
unsigned int*)crossed->
fAllBits : 0;
1037 for (
G4int i = 0 ; i < fNPerSlice; ++i)
1042 if (!(mask = masks[0][i]))
continue;
1043 if (!(mask &= masks[1][i]))
continue;
1044 if (!(mask &= masks[2][i]))
continue;
1045 if (maskCrossed && !(mask &= ~maskCrossed[i]))
continue;
1047 FindComponentsFastest(mask, list, i);
1101 std::vector<G4int>& list,
1106 if (fTotalCandidates == 1)
1113 if (fNPerSlice == 1)
1116 if (!(mask = ((
unsigned int *) bitmasks[0].fAllBits)[voxels[0]]))
1118 if (!(mask &= ((
unsigned int *) bitmasks[1].fAllBits)[voxels[1]]))
1120 if (!(mask &= ((
unsigned int *) bitmasks[2].fAllBits)[voxels[2]]))
1122 if (crossed && (!(mask &= ~((
unsigned int *)crossed->
fAllBits)[0])))
1125 FindComponentsFastest(mask, list, 0);
1129 unsigned int *masks[3], mask;
1130 for (
auto i = 0; i <= 2; ++i)
1132 masks[i] = ((
unsigned int *) bitmasks[i].fAllBits)
1133 + voxels[i]*fNPerSlice;
1135 unsigned int *maskCrossed = crossed !=
nullptr
1136 ? (
unsigned int *)crossed->
fAllBits : 0;
1138 for (
G4int i = 0 ; i < fNPerSlice; ++i)
1143 if (!(mask = masks[0][i]))
continue;
1144 if (!(mask &= masks[1][i]))
continue;
1145 if (!(mask &= masks[2][i]))
continue;
1146 if (maskCrossed && !(mask &= ~maskCrossed[i]))
continue;
1148 FindComponentsFastest(mask, list, i);
1158 std::vector<G4int>& list,
G4SurfBits* crossed)
const
1168 for (
auto i = 0; i < 3; ++i)
1170 if (point[i] < fBoundaries[i].front() || point[i] > fBoundaries[i].back())
1205 safe = safx = -f.
x() + std::abs(aPoint.
x());
1206 safy = -f.
y() + std::abs(aPoint.
y());
1207 if ( safy > safe ) safe = safy;
1208 safz = -f.
z() + std::abs(aPoint.
z());
1209 if ( safz > safe ) safe = safz;
1210 if (safe < 0.0)
return 0.0;
1214 if ( safx > 0 ) { safsq += safx*safx; ++count; }
1215 if ( safy > 0 ) { safsq += safy*safy; ++count; }
1216 if ( safz > 0 ) { safsq += safz*safz; ++count; }
1217 if (count == 1)
return safe;
1218 return std::sqrt(safsq);
1225 std::vector<G4int>& curVoxel)
const
1230 for (
G4int i = 0; i <= 2; ++i)
1234 const std::vector<G4double>& boundary = fBoundaries[i];
1235 G4int index = curVoxel[i];
1236 if (direction[i] >= 1e-10)
1242 if (direction[i] > -1e-10)
1245 G4double dif = boundary[index] - point[i];
1246 G4double distance = dif / direction[i];
1248 if (shift > distance)
1255 if (shift != kInfinity)
1260 if (direction[cur] > 0)
1262 if (++curVoxel[cur] >= (
G4int) fBoundaries[cur].size() - 1)
1267 if (--curVoxel[cur] < 0)
1310 std::vector<G4int>& curVoxel)
const
1312 for (
auto i = 0; i <= 2; ++i)
1314 G4int index = curVoxel[i];
1315 const std::vector<G4double> &boundary = fBoundaries[i];
1317 if (direction[i] > 0)
1319 if (point[i] >= boundary[++index])
1320 if (++curVoxel[i] >= (
G4int) boundary.size() - 1)
1325 if (point[i] < boundary[index])
1326 if (--curVoxel[i] < 0)
1331 if (curVoxel[i] != indexOK)
1332 curVoxel[i] = indexOK;
1342 fReductionRatio.
set(0,0,0);
1349 fReductionRatio = ratioOfReduction;
1355 fDefaultVoxelsCount = count;
1361 return fDefaultVoxelsCount;
1368 size += fBoxes.capacity() *
sizeof(
G4VoxelBox);
1369 size +=
sizeof(
G4double) * (fBoundaries[0].capacity()
1370 + fBoundaries[1].capacity() + fBoundaries[2].capacity());
1371 size +=
sizeof(
G4int) * (fCandidatesCounts[0].capacity()
1372 + fCandidatesCounts[1].capacity() + fCandidatesCounts[2].capacity());
1376 G4int csize = fCandidates.size();
1377 for (
G4int i = 0; i < csize; ++i)
1379 size +=
sizeof(vector<G4int>) + fCandidates[i].capacity() *
sizeof(
G4int);
CLHEP::Hep3Vector G4ThreeVector
G4GLOB_DLL std::ostream G4cout
void set(double x, double y, double z)
void SetZHalfLength(G4double dz)
void SetYHalfLength(G4double dy)
void SetXHalfLength(G4double dx)
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
G4double GetSurfaceTolerance() const
static G4GeometryTolerance * GetInstance()
G4double GetRadialTolerance() const
static void DeRegister(G4VSolid *pSolid)
static G4SolidStore * GetInstance()
unsigned int GetNbytes() const
void ResetAllBits(G4bool value=false)
void ResetBitNumber(unsigned int bitnumber)
void SetBitNumber(unsigned int bitnumber, G4bool value=true)
void set(unsigned int nbits, const char *array)
G4bool TestBitNumber(unsigned int bitnumber) const
virtual G4double Extent(const G4ThreeVector)=0
virtual void BoundingLimits(G4ThreeVector &pMin, G4ThreeVector &pMax) const
virtual G4GeometryType GetEntityType() const =0
long long CountVoxels(std::vector< G4double > boundaries[]) const
G4double DistanceToBoundingBox(const G4ThreeVector &point) const
G4bool UpdateCurrentVoxel(const G4ThreeVector &point, const G4ThreeVector &direction, std::vector< G4int > &curVoxel) const
void DisplayListNodes() const
void DisplayVoxelLimits() const
static void SetDefaultVoxelsCount(G4int count)
G4int GetBitsPerSlice() const
G4double DistanceToFirst(const G4ThreeVector &point, const G4ThreeVector &direction) const
G4int GetCandidatesVoxelArray(const G4ThreeVector &point, std::vector< G4int > &list, G4SurfBits *crossed=nullptr) const
static G4double MinDistanceToBox(const G4ThreeVector &aPoint, const G4ThreeVector &f)
void SetMaxVoxels(G4int max)
void GetCandidatesVoxel(std::vector< G4int > &voxels)
static G4int GetDefaultVoxelsCount()
static G4int BinarySearch(const std::vector< T > &vec, T value)
G4double DistanceToNext(const G4ThreeVector &point, const G4ThreeVector &direction, std::vector< G4int > &curVoxel) const
void Voxelize(std::vector< G4VSolid * > &solids, std::vector< G4Transform3D > &transforms)
G4int GetVoxelsIndex(G4int x, G4int y, G4int z) const
G4bool Contains(const G4ThreeVector &point) const
G4double total(Particle const *const p1, Particle const *const p2)
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