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