Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGeoVoxelGrid.h
Go to the documentation of this file.
1/// \author Sandro Wenzel <sandro.wenzel@cern.ch>
2/// \date 2024-02-22
3
4/*************************************************************************
5 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TGeoVoxelGrid
13#define ROOT_TGeoVoxelGrid
14
15// a simple structure to encode voxel indices, to address
16// individual voxels in the 3D grid.
18 int ix{-1};
19 int iy{-1};
20 int iz{-1};
21 size_t idx{std::numeric_limits<size_t>::max()};
22 bool isValid() const { return idx != std::numeric_limits<size_t>::max(); }
23};
24
25/// A finite 3D grid structure, mapping/binning arbitrary 3D cartesian points
26/// onto discrete "voxels". Each such voxel can store an object of type T.
27/// The precision of the voxel binning is done with S (float or double).
28template <typename T, typename S = float>
30public:
31 TGeoVoxelGrid(S xmin, S ymin, S zmin, S xmax, S ymax, S zmax, S Lx_, S Ly_, S Lz_)
32 : fMinBound{xmin, ymin, zmin}, fMaxBound{xmax, ymax, zmax}, fLx(Lx_), fLy(Ly_), fLz(Lz_)
33 {
34
35 // Calculate the number of voxels in each dimension
36 fNx = static_cast<int>((fMaxBound[0] - fMinBound[0]) / fLx);
37 fNy = static_cast<int>((fMaxBound[1] - fMinBound[1]) / fLy);
38 fNz = static_cast<int>((fMaxBound[2] - fMinBound[2]) / fLz);
39
40 finvLx = 1. / fLx;
41 finvLy = 1. / fLy;
42 finvLz = 1. / fLz;
43
44 fHalfDiag = std::sqrt(fLx / 2. * fLx / 2. + fLy / 2. * fLy / 2. + fLz / 2. * fLz / 2.);
45
46 // Resize the grid to hold the voxels
47 fGrid.resize(fNx * fNy * fNz);
48 }
49
50 T &at(int i, int j, int k) { return fGrid[index(i, j, k)]; }
51
52 // check if point is covered by voxel structure
53 bool inside(std::array<S, 3> const &p) const
54 {
55 for (int i = 0; i < 3; ++i) {
56 if (p[i] < fMinBound[i] || p[i] > fMaxBound[i]) {
57 return false;
58 }
59 }
60 return true;
61 }
62
63 // Access a voxel given a 3D point P
64 T &at(std::array<S, 3> const &P)
65 {
66 int i, j, k;
67 pointToVoxelIndex(P, i, j, k); // Convert point to voxel index
68 return fGrid[index(i, j, k)]; // Return reference to voxel's data
69 }
70
71 T *at(TGeoVoxelGridIndex const &vi)
72 {
73 if (!vi.isValid()) {
74 return nullptr;
75 }
76 return &fGrid[vi.idx];
77 }
78
79 // Set the data of a voxel at point P
80 void set(std::array<S, 3> const &p, const T &value)
81 {
82 int i, j, k;
83 pointToVoxelIndex(p, i, j, k); // Convert point to voxel index
84 fGrid[index(i, j, k)] = value; // Set the value at the voxel
85 }
86
87 // Set the data of a voxel at point P
88 void set(int i, int j, int k, const T &value)
89 {
90 fGrid[index(i, j, k)] = value; // Set the value at the voxel
91 }
92
93 void set(TGeoVoxelGridIndex const &vi, const T &value) { fGrid[vi.idx] = value; }
94
95 // Get voxel dimensions
96 int getVoxelCountX() const { return fNx; }
97 int getVoxelCountY() const { return fNy; }
98 int getVoxelCountZ() const { return fNz; }
99
100 // returns the cartesian mid-point coordinates of a voxel given by a VoxelIndex
101 std::array<S, 3> getVoxelMidpoint(TGeoVoxelGridIndex const &vi) const
102 {
103 const S midX = fMinBound[0] + (vi.ix + 0.5) * fLx;
104 const S midY = fMinBound[1] + (vi.iy + 0.5) * fLy;
105 const S midZ = fMinBound[2] + (vi.iz + 0.5) * fLz;
106
107 return {midX, midY, midZ};
108 }
109
110 S getDiagonalLength() const { return fHalfDiag; }
111
112 // Convert a point p(x, y, z) to voxel indices (i, j, k)
113 // if point is outside set indices i,j,k to -1
114 void pointToVoxelIndex(std::array<S, 3> const &p, int &i, int &j, int &k) const
115 {
116 if (!inside(p)) {
117 i = -1;
118 j = -1;
119 k = -1;
120 }
121
122 i = static_cast<int>((p[0] - fMinBound[0]) * finvLx);
123 j = static_cast<int>((p[1] - fMinBound[1]) * finvLy);
124 k = static_cast<int>((p[2] - fMinBound[2]) * finvLz);
125
126 // Clamp the indices to valid ranges
127 i = std::min(i, fNx - 1);
128 j = std::min(j, fNy - 1);
129 k = std::min(k, fNz - 1);
130 }
131
132 // Convert a point p(x, y, z) to voxel index object
133 // if outside, an invalid index object will be returned
134 TGeoVoxelGridIndex pointToVoxelIndex(std::array<S, 3> const &p) const
135 {
136 if (!inside(p)) {
137 return TGeoVoxelGridIndex(); // invalid voxel index
138 }
139
140 int i = static_cast<int>((p[0] - fMinBound[0]) * finvLx);
141 int j = static_cast<int>((p[1] - fMinBound[1]) * finvLy);
142 int k = static_cast<int>((p[2] - fMinBound[2]) * finvLz);
143
144 // Clamp the indices to valid ranges
145 i = std::min(i, fNz - 1);
146 j = std::min(j, fNy - 1);
147 k = std::min(k, fNz - 1);
148
149 return TGeoVoxelGridIndex{i, j, k, index(i, j, k)};
150 }
151
152 TGeoVoxelGridIndex pointToVoxelIndex(S x, S y, S z) const { return pointToVoxelIndex(std::array<S, 3>{x, y, z}); }
153
154 // Convert voxel indices (i, j, k) to a linear index in the grid array
155 size_t index(int i, int j, int k) const { return i + fNx * (j + fNy * k); }
156
157 void indexToIndices(size_t idx, int &i, int &j, int &k) const
158 {
159 k = idx % fNz;
160 j = (idx / fNz) % fNy;
161 i = idx / (fNy * fNz);
162 }
163
164 // member data
165
166 std::array<S, 3> fMinBound;
167 std::array<S, 3> fMaxBound; // 3D bounds for grid structure
168 S fLx, fLy, fLz; // Voxel dimensions
169 S finvLx, finvLy, finvLz; // inverse voxel dimensions
170 S fHalfDiag; // cached value for voxel half diagonal length
171
172 int fNx, fNy, fNz; // Number of voxels in each dimension
173 std::vector<T> fGrid; // The actual voxel grid data container
174};
175
176#endif
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
float xmin
float ymin
float xmax
float ymax
A finite 3D grid structure, mapping/binning arbitrary 3D cartesian points onto discrete "voxels".
std::vector< T > fGrid
S getDiagonalLength() const
void indexToIndices(size_t idx, int &i, int &j, int &k) const
std::array< S, 3 > fMinBound
int getVoxelCountY() const
size_t index(int i, int j, int k) const
TGeoVoxelGridIndex pointToVoxelIndex(S x, S y, S z) const
int getVoxelCountX() const
std::array< S, 3 > getVoxelMidpoint(TGeoVoxelGridIndex const &vi) const
void set(TGeoVoxelGridIndex const &vi, const T &value)
int getVoxelCountZ() const
T & at(std::array< S, 3 > const &P)
T * at(TGeoVoxelGridIndex const &vi)
bool inside(std::array< S, 3 > const &p) const
T & at(int i, int j, int k)
std::array< S, 3 > fMaxBound
TGeoVoxelGrid(S xmin, S ymin, S zmin, S xmax, S ymax, S zmax, S Lx_, S Ly_, S Lz_)
TGeoVoxelGridIndex pointToVoxelIndex(std::array< S, 3 > const &p) const
void set(std::array< S, 3 > const &p, const T &value)
void set(int i, int j, int k, const T &value)
void pointToVoxelIndex(std::array< S, 3 > const &p, int &i, int &j, int &k) const
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
bool isValid() const