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#include <array>
16#include <cmath>
17#include <limits>
18
19// a simple structure to encode voxel indices, to address
20// individual voxels in the 3D grid.
22 int ix{-1};
23 int iy{-1};
24 int iz{-1};
25 size_t idx{std::numeric_limits<size_t>::max()};
26 bool isValid() const { return idx != std::numeric_limits<size_t>::max(); }
27};
28
29/// A finite 3D grid structure, mapping/binning arbitrary 3D cartesian points
30/// onto discrete "voxels". Each such voxel can store an object of type T.
31/// The precision of the voxel binning is done with S (float or double).
32template <typename T, typename S = float>
34public:
35 TGeoVoxelGrid(S xmin, S ymin, S zmin, S xmax, S ymax, S zmax, S Lx_, S Ly_, S Lz_)
36 : fMinBound{xmin, ymin, zmin}, fMaxBound{xmax, ymax, zmax}, fLx(Lx_), fLy(Ly_), fLz(Lz_)
37 {
38
39 // Calculate the number of voxels in each dimension
40 fNx = static_cast<int>((fMaxBound[0] - fMinBound[0]) / fLx);
41 fNy = static_cast<int>((fMaxBound[1] - fMinBound[1]) / fLy);
42 fNz = static_cast<int>((fMaxBound[2] - fMinBound[2]) / fLz);
43
44 finvLx = 1. / fLx;
45 finvLy = 1. / fLy;
46 finvLz = 1. / fLz;
47
48 fHalfDiag = std::sqrt(fLx / 2. * fLx / 2. + fLy / 2. * fLy / 2. + fLz / 2. * fLz / 2.);
49
50 // Resize the grid to hold the voxels
51 fGrid.resize(fNx * fNy * fNz);
52 }
53
54 T &at(int i, int j, int k) { return fGrid[index(i, j, k)]; }
55
56 // check if point is covered by voxel structure
57 bool inside(std::array<S, 3> const &p) const
58 {
59 for (int i = 0; i < 3; ++i) {
60 if (p[i] < fMinBound[i] || p[i] > fMaxBound[i]) {
61 return false;
62 }
63 }
64 return true;
65 }
66
67 // Access a voxel given a 3D point P
68 T &at(std::array<S, 3> const &P)
69 {
70 int i, j, k;
71 pointToVoxelIndex(P, i, j, k); // Convert point to voxel index
72 return fGrid[index(i, j, k)]; // Return reference to voxel's data
73 }
74
76 {
77 if (!vi.isValid()) {
78 return nullptr;
79 }
80 return &fGrid[vi.idx];
81 }
82
83 // Set the data of a voxel at point P
84 void set(std::array<S, 3> const &p, const T &value)
85 {
86 int i, j, k;
87 pointToVoxelIndex(p, i, j, k); // Convert point to voxel index
88 fGrid[index(i, j, k)] = value; // Set the value at the voxel
89 }
90
91 // Set the data of a voxel at point P
92 void set(int i, int j, int k, const T &value)
93 {
94 fGrid[index(i, j, k)] = value; // Set the value at the voxel
95 }
96
97 void set(TGeoVoxelGridIndex const &vi, const T &value) { fGrid[vi.idx] = value; }
98
99 // Get voxel dimensions
100 int getVoxelCountX() const { return fNx; }
101 int getVoxelCountY() const { return fNy; }
102 int getVoxelCountZ() const { return fNz; }
103
104 // returns the cartesian mid-point coordinates of a voxel given by a VoxelIndex
105 std::array<S, 3> getVoxelMidpoint(TGeoVoxelGridIndex const &vi) const
106 {
107 const S midX = fMinBound[0] + (vi.ix + 0.5) * fLx;
108 const S midY = fMinBound[1] + (vi.iy + 0.5) * fLy;
109 const S midZ = fMinBound[2] + (vi.iz + 0.5) * fLz;
110
111 return {midX, midY, midZ};
112 }
113
114 S getDiagonalLength() const { return fHalfDiag; }
115
116 // Convert a point p(x, y, z) to voxel indices (i, j, k)
117 // if point is outside set indices i,j,k to -1
118 void pointToVoxelIndex(std::array<S, 3> const &p, int &i, int &j, int &k) const
119 {
120 if (!inside(p)) {
121 i = -1;
122 j = -1;
123 k = -1;
124 }
125
126 i = static_cast<int>((p[0] - fMinBound[0]) * finvLx);
127 j = static_cast<int>((p[1] - fMinBound[1]) * finvLy);
128 k = static_cast<int>((p[2] - fMinBound[2]) * finvLz);
129
130 // Clamp the indices to valid ranges
131 i = std::min(i, fNx - 1);
132 j = std::min(j, fNy - 1);
133 k = std::min(k, fNz - 1);
134 }
135
136 // Convert a point p(x, y, z) to voxel index object
137 // if outside, an invalid index object will be returned
138 TGeoVoxelGridIndex pointToVoxelIndex(std::array<S, 3> const &p) const
139 {
140 if (!inside(p)) {
141 return TGeoVoxelGridIndex(); // invalid voxel index
142 }
143
144 int i = static_cast<int>((p[0] - fMinBound[0]) * finvLx);
145 int j = static_cast<int>((p[1] - fMinBound[1]) * finvLy);
146 int k = static_cast<int>((p[2] - fMinBound[2]) * finvLz);
147
148 // Clamp the indices to valid ranges
149 i = std::min(i, fNz - 1);
150 j = std::min(j, fNy - 1);
151 k = std::min(k, fNz - 1);
152
153 return TGeoVoxelGridIndex{i, j, k, index(i, j, k)};
154 }
155
156 TGeoVoxelGridIndex pointToVoxelIndex(S x, S y, S z) const { return pointToVoxelIndex(std::array<S, 3>{x, y, z}); }
157
158 // Convert voxel indices (i, j, k) to a linear index in the grid array
159 size_t index(int i, int j, int k) const { return i + fNx * (j + fNy * k); }
160
161 void indexToIndices(size_t idx, int &i, int &j, int &k) const
162 {
163 k = idx % fNz;
164 j = (idx / fNz) % fNy;
165 i = idx / (fNy * fNz);
166 }
167
168 // member data
169
170 std::array<S, 3> fMinBound;
171 std::array<S, 3> fMaxBound; // 3D bounds for grid structure
172 S fLx, fLy, fLz; // Voxel dimensions
173 S finvLx, finvLy, finvLz; // inverse voxel dimensions
174 S fHalfDiag; // cached value for voxel half diagonal length
175
176 int fNx, fNy, fNz; // Number of voxels in each dimension
177 std::vector<T> fGrid; // The actual voxel grid data container
178};
179
180#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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