Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
sim::PhotonVoxelDef Class Reference

Representation of a region of space diced into voxels. More...

#include <PhotonVoxels.h>

Classes

struct  NeiInfo
 

Public Member Functions

 PhotonVoxelDef ()=default
 
 PhotonVoxelDef (double xMin, double xMax, int xN, double yMin, double yMax, int yN, double zMin, double zMax, int zN)
 
template<typename Point = DefaultPoint>
decltype(auto) GetRegionLowerCorner () const
 Returns the volume vertex (type Point) with the lowest coordinates. More...
 
template<typename Point = DefaultPoint>
decltype(auto) GetRegionUpperCorner () const
 Returns the volume vertex (type Point) with the highest coordinates. More...
 
std::array< unsigned int, 3U > GetSteps () const
 Returns the number of voxels along each of the three dimensions. More...
 
template<typename Vector = DefaultVector>
Vector GetVoxelSize () const
 Returns a vector describing the span of a single voxel in x, y an z [cm]. More...
 
template<typename Vector = DefaultVector, typename Point = DefaultPoint>
Vector GetVolumeSize () const
 Returns a vector describing the full span in x, y an z [cm]. More...
 
unsigned int GetNVoxels () const
 Returns the total number of voxels in the volume. More...
 
template<typename Point >
int GetVoxelID (Point const &p) const
 Returns the ID of the voxel containing p, or -1 if none. More...
 
int GetVoxelID (double const *) const
 
bool IsLegalVoxelID (int) const
 
template<typename Point >
std::optional< std::array< NeiInfo, 8U > > GetNeighboringVoxelIDs (Point const &v) const
 Returns IDs of the eight neighboring voxels around v. More...
 
PhotonVoxel GetPhotonVoxel (int ID) const
 
std::array< int, 3U > GetVoxelCoords (int ID) const
 
bool isInside (geo::Point_t const &p) const
 Returns whether point p is inside the region (upper border excluded). More...
 
bool operator== (const PhotonVoxelDef &rhs) const
 
bool operator!= (const PhotonVoxelDef &rhs) const
 
template<typename Point >
std::optional< std::array< sim::PhotonVoxelDef::NeiInfo, 8U > > GetNeighboringVoxelIDs (Point const &v) const
 

Private Types

using DefaultPoint = geo::Point_t
 
using DefaultVector = geo::Vector_t
 

Private Member Functions

int GetVoxelIDImpl (geo::Point_t const &p) const
 
std::optional< std::array< NeiInfo, 8U > > GetNeighboringVoxelIDsImpl (geo::Point_t const &v) const
 
std::array< double, 3U > GetVoxelStepCoordsUnchecked (geo::Point_t const &p) const
 Returns the coordinates of the cvoxel containing p in step units. More...
 
bool isInsideImpl (geo::Point_t const &point) const
 Returns whether the specified point is within the volume. More...
 

Static Private Member Functions

static bool isInsideVolume (geo::Point_t const &point, geo::Point_t const &lower, geo::Point_t const &upper)
 
static bool isInsideRange (double value, double lower, double upper)
 

Private Attributes

geo::Point_t fLowerCorner
 
geo::Point_t fUpperCorner
 
unsigned int fxSteps = 1U
 
unsigned int fySteps = 1U
 
unsigned int fzSteps = 1U
 

Detailed Description

Representation of a region of space diced into voxels.

Definition at line 58 of file PhotonVoxels.h.

Member Typedef Documentation

Definition at line 59 of file PhotonVoxels.h.

Definition at line 60 of file PhotonVoxels.h.

Constructor & Destructor Documentation

sim::PhotonVoxelDef::PhotonVoxelDef ( )
default
sim::PhotonVoxelDef::PhotonVoxelDef ( double  xMin,
double  xMax,
int  xN,
double  yMin,
double  yMax,
int  yN,
double  zMin,
double  zMax,
int  zN 
)

Definition at line 22 of file PhotonVoxels.cxx.

31  : fLowerCorner(xMin, yMin, zMin)
32  , fUpperCorner(xMax, yMax, zMax)
33  , fxSteps(xN)
34  , fySteps(yN)
35  , fzSteps(zN)
36  {}
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
geo::Point_t fLowerCorner
Definition: PhotonVoxels.h:62
geo::Point_t fUpperCorner
Definition: PhotonVoxels.h:63

Member Function Documentation

template<typename Point >
std::optional<std::array<NeiInfo, 8U> > sim::PhotonVoxelDef::GetNeighboringVoxelIDs ( Point const &  v) const

Returns IDs of the eight neighboring voxels around v.

Parameters
vlocation within the mapped volume
Returns
an optional collection of eight neighboring voxels

If v is not inside the mapped volume, no list is returned (the optional return value evaluates to false). Otherwise, each of the eight voxels with the center closest to v are returned, each with a weight proportional to the distance of v from that center.

template<typename Point >
std::optional<std::array<sim::PhotonVoxelDef::NeiInfo, 8U> > sim::PhotonVoxelDef::GetNeighboringVoxelIDs ( Point const &  v) const

Definition at line 242 of file PhotonVoxels.h.

243 {
245 }
::geo::Point_t toPoint(Point const &p)
Convert the specified point into a geo::Point_t.
std::optional< std::array< NeiInfo, 8U > > GetNeighboringVoxelIDsImpl(geo::Point_t const &v) const
std::optional< std::array< sim::PhotonVoxelDef::NeiInfo, 8U > > sim::PhotonVoxelDef::GetNeighboringVoxelIDsImpl ( geo::Point_t const &  v) const
private

Definition at line 72 of file PhotonVoxels.cxx.

73  {
74  if (!isInside(v)) return {};
75 
76  std::array<sim::PhotonVoxelDef::NeiInfo, 8U> ret;
77 
78  // Position in voxel coordinates including floating point part
79  auto const rStepD = GetVoxelStepCoordsUnchecked(v);
80 
81  // The neighbours are the 8 corners of a cube around this point
82  std::size_t iNeigh = 0U;
83  for (int dx : {0, 1}) {
84  for (int dy : {0, 1}) {
85  for (int dz : {0, 1}) {
86  // The full 3D step
87  const int dr[3] = {dx, dy, dz};
88 
89  // The integer-only position of the current corner
90  int rStepI[3];
91  for (int d = 0; d < 3; ++d) {
92  // Round down to get the "lower left" corner
93  rStepI[d] = int(rStepD[d]);
94  // Ensure we'll stay in-bounds
95  rStepI[d] = std::max(0, rStepI[d]);
96  rStepI[d] = std::min(rStepI[d], int(GetSteps()[d]) - 2);
97  // Adjust to the corner we're actually considering
98  rStepI[d] += dr[d];
99  }
100 
101  double w = 1;
102  for (int d = 0; d < 3; ++d) {
103  // These expressions will interpolate when between the 8 corners,
104  // and extrapolate in the half-voxel space around the edges.
105  if (dr[d] == 0)
106  w *= 1 + rStepI[d] - rStepD[d];
107  else
108  w *= 1 - rStepI[d] + rStepD[d];
109  }
110 
111  const int id = (rStepI[0] + rStepI[1] * (fxSteps) + rStepI[2] * (fxSteps * fySteps));
112 
113  ret[iNeigh++] = {id, w};
114  }
115  }
116  }
117 
118  // Sanity check the weights sum to 1
119  double wSum = 0;
120  for (const NeiInfo& n : ret)
121  wSum += n.weight;
122  if (std::abs(wSum - 1) > 1e-3) {
123  std::string msg = "PhotonVoxelDef::GetNeighboringVoxelIDs():"
124  " Weights sum to " +
125  std::to_string(wSum) +
126  " (should be 1)."
127  " Weights are:";
128  for (const NeiInfo& n : ret) {
129  msg += ' ';
130  msg += std::to_string(n.weight);
131  }
132  throw std::runtime_error(msg);
133  }
134  return {ret};
135  }
void msg(const char *fmt,...)
Definition: message.cpp:107
unsigned int fySteps
Definition: PhotonVoxels.h:65
std::string string
Definition: nybbler.cc:12
std::array< double, 3U > GetVoxelStepCoordsUnchecked(geo::Point_t const &p) const
Returns the coordinates of the cvoxel containing p in step units.
bool isInside(geo::Point_t const &p) const
Returns whether point p is inside the region (upper border excluded).
Definition: PhotonVoxels.h:139
T abs(T value)
const double e
std::void_t< T > n
static int max(int a, int b)
unsigned int fxSteps
Definition: PhotonVoxels.h:64
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
std::array< unsigned int, 3U > GetSteps() const
Returns the number of voxels along each of the three dimensions.
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
unsigned int sim::PhotonVoxelDef::GetNVoxels ( ) const

Returns the total number of voxels in the volume.

Definition at line 58 of file PhotonVoxels.cxx.

59  {
60  return fxSteps * fySteps * fzSteps;
61  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
PhotonVoxel sim::PhotonVoxelDef::GetPhotonVoxel ( int  ID) const

Definition at line 139 of file PhotonVoxels.cxx.

140  {
141  // float TempID = (float) ID;
142 
143  // Decompose ID into steps in each direction
144  int xStep = ID % fxSteps;
145  int yStep = ((ID - xStep) / fxSteps) % fySteps;
146  int zStep = ((ID - xStep - (yStep * fxSteps)) / (fySteps * fxSteps)) % fzSteps;
147 
148  auto const VoxelSize = GetVoxelSize<geo::Vector_t>();
149 
150  double const xMin = VoxelSize.X() * (xStep) + fLowerCorner.X();
151  double const xMax = VoxelSize.X() * (xStep + 1) + fLowerCorner.X();
152  double const yMin = VoxelSize.Y() * (yStep) + fLowerCorner.Y();
153  double const yMax = VoxelSize.Y() * (yStep + 1) + fLowerCorner.Y();
154  double const zMin = VoxelSize.Z() * (zStep) + fLowerCorner.Z();
155  double const zMax = VoxelSize.Z() * (zStep + 1) + fLowerCorner.Z();
156 
157  return PhotonVoxel(xMin, xMax, yMin, yMax, zMin, zMax);
158  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int ID
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
geo::Point_t fLowerCorner
Definition: PhotonVoxels.h:62
template<typename Point = DefaultPoint>
decltype(auto) sim::PhotonVoxelDef::GetRegionLowerCorner ( ) const

Returns the volume vertex (type Point) with the lowest coordinates.

template<typename Point = DefaultPoint>
decltype(auto) sim::PhotonVoxelDef::GetRegionUpperCorner ( ) const

Returns the volume vertex (type Point) with the highest coordinates.

std::array< unsigned int, 3U > sim::PhotonVoxelDef::GetSteps ( ) const

Returns the number of voxels along each of the three dimensions.

Definition at line 40 of file PhotonVoxels.cxx.

41  {
42  // BUG the double brace syntax is required to work around clang bug 21629
43  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
44  return {{fxSteps, fySteps, fzSteps}};
45  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
template<typename Vector = DefaultVector, typename Point = DefaultPoint>
Vector sim::PhotonVoxelDef::GetVolumeSize ( ) const
inline

Returns a vector describing the full span in x, y an z [cm].

Definition at line 98 of file PhotonVoxels.h.

99  {
100  return GetRegionUpperCorner<Point>() - GetRegionLowerCorner<Point>();
101  }
std::array< int, 3U > sim::PhotonVoxelDef::GetVoxelCoords ( int  ID) const

Definition at line 168 of file PhotonVoxels.cxx.

169  {
170  std::array<int, 3U> ReturnVector;
171  ReturnVector[0] = ID % fxSteps;
172  ReturnVector[1] = ((ID - ReturnVector[0]) / fxSteps) % fySteps;
173  ReturnVector[2] =
174  ((ID - ReturnVector[0] - (ReturnVector[1] * fxSteps)) / (fySteps * fxSteps)) % fzSteps;
175  return ReturnVector;
176  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int ID
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
template<typename Point >
int sim::PhotonVoxelDef::GetVoxelID ( Point const &  p) const

Returns the ID of the voxel containing p, or -1 if none.

Definition at line 234 of file PhotonVoxels.h.

235 {
237 }
int GetVoxelIDImpl(geo::Point_t const &p) const
::geo::Point_t toPoint(Point const &p)
Convert the specified point into a geo::Point_t.
p
Definition: test.py:223
int sim::PhotonVoxelDef::GetVoxelID ( double const *  Position) const

Definition at line 65 of file PhotonVoxels.cxx.

66  {
67  return GetVoxelIDImpl(geo::vect::makeFromCoords<geo::Point_t>(Position));
68  }
int GetVoxelIDImpl(geo::Point_t const &p) const
int sim::PhotonVoxelDef::GetVoxelIDImpl ( geo::Point_t const &  p) const
private

Definition at line 195 of file PhotonVoxels.cxx.

196  {
197  if (!isInside(p)) return -1;
198 
199  auto const stepCoords = GetVoxelStepCoordsUnchecked(p);
200 
201  // figure out how many steps this point is in the x,y,z directions;
202  // `p` is guaranteed to be in the mapped volume by the previous check
203  int xStep = static_cast<int>(stepCoords[0]);
204  int yStep = static_cast<int>(stepCoords[1]);
205  int zStep = static_cast<int>(stepCoords[2]);
206 
207  // if within bounds, generate the voxel ID
208  return (xStep + yStep * (fxSteps) + zStep * (fxSteps * fySteps));
209  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
std::array< double, 3U > GetVoxelStepCoordsUnchecked(geo::Point_t const &p) const
Returns the coordinates of the cvoxel containing p in step units.
bool isInside(geo::Point_t const &p) const
Returns whether point p is inside the region (upper border excluded).
Definition: PhotonVoxels.h:139
p
Definition: test.py:223
unsigned int fxSteps
Definition: PhotonVoxels.h:64
template<typename Vector >
Vector sim::PhotonVoxelDef::GetVoxelSize ( ) const

Returns a vector describing the span of a single voxel in x, y an z [cm].

Definition at line 224 of file PhotonVoxels.h.

225 {
226  return {(fUpperCorner.X() - fLowerCorner.X()) / fxSteps,
227  (fUpperCorner.Y() - fLowerCorner.Y()) / fySteps,
228  (fUpperCorner.Z() - fLowerCorner.Z()) / fzSteps};
229 } // sim::PhotonVoxelDef::GetVoxelSize()
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int fzSteps
Definition: PhotonVoxels.h:66
unsigned int fxSteps
Definition: PhotonVoxels.h:64
geo::Point_t fLowerCorner
Definition: PhotonVoxels.h:62
geo::Point_t fUpperCorner
Definition: PhotonVoxels.h:63
std::array< double, 3U > sim::PhotonVoxelDef::GetVoxelStepCoordsUnchecked ( geo::Point_t const &  p) const
private

Returns the coordinates of the cvoxel containing p in step units.

Definition at line 180 of file PhotonVoxels.cxx.

181  {
182 
183  auto const span = fUpperCorner - fLowerCorner;
184  auto const relPos = p - fLowerCorner;
185 
186  // BUG the double brace syntax is required to work around clang bug 21629
187  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
188  return {{(relPos.X() / span.X()) * fxSteps,
189  (relPos.Y() / span.Y()) * fySteps,
190  (relPos.Z() / span.Z()) * fzSteps}};
191  } // PhotonVoxelDef::GetVoxelStepCoordsUnchecked()
span(IterB &&b, IterE &&e, Adaptor &&adaptor) -> span< decltype(adaptor(std::forward< IterB >(b))), decltype(adaptor(std::forward< IterE >(e))) >
unsigned int fySteps
Definition: PhotonVoxels.h:65
unsigned int fzSteps
Definition: PhotonVoxels.h:66
p
Definition: test.py:223
unsigned int fxSteps
Definition: PhotonVoxels.h:64
geo::Point_t fLowerCorner
Definition: PhotonVoxels.h:62
geo::Point_t fUpperCorner
Definition: PhotonVoxels.h:63
bool sim::PhotonVoxelDef::isInside ( geo::Point_t const &  p) const
inline

Returns whether point p is inside the region (upper border excluded).

Definition at line 139 of file PhotonVoxels.h.

140  {
141  return isInsideImpl(p);
142  }
p
Definition: test.py:223
bool isInsideImpl(geo::Point_t const &point) const
Returns whether the specified point is within the volume.
Definition: PhotonVoxels.h:161
bool sim::PhotonVoxelDef::isInsideImpl ( geo::Point_t const &  point) const
inlineprivate

Returns whether the specified point is within the volume.

Definition at line 161 of file PhotonVoxels.h.

162  {
163  return isInsideVolume(point, fLowerCorner, fUpperCorner);
164  }
geo::Point_t fLowerCorner
Definition: PhotonVoxels.h:62
geo::Point_t fUpperCorner
Definition: PhotonVoxels.h:63
static bool isInsideVolume(geo::Point_t const &point, geo::Point_t const &lower, geo::Point_t const &upper)
bool sim::PhotonVoxelDef::isInsideRange ( double  value,
double  lower,
double  upper 
)
staticprivate

Definition at line 223 of file PhotonVoxels.cxx.

224  {
225 
226  return (value >= lower) && (value < upper);
227 
228  } // PhotonVoxelDef::isInsideRange()
bool sim::PhotonVoxelDef::isInsideVolume ( geo::Point_t const &  point,
geo::Point_t const &  lower,
geo::Point_t const &  upper 
)
staticprivate

Definition at line 213 of file PhotonVoxels.cxx.

216  {
217  return isInsideRange(point.X(), lower.X(), upper.X()) &&
218  isInsideRange(point.Y(), lower.Y(), upper.Y()) &&
219  isInsideRange(point.Z(), lower.Z(), upper.Z());
220  }
static bool isInsideRange(double value, double lower, double upper)
bool sim::PhotonVoxelDef::IsLegalVoxelID ( int  ID) const

Definition at line 162 of file PhotonVoxels.cxx.

163  {
164  return ((ID >= 0) && (static_cast<unsigned int>(ID) < GetNVoxels()));
165  }
unsigned int ID
unsigned int GetNVoxels() const
Returns the total number of voxels in the volume.
bool sim::PhotonVoxelDef::operator!= ( const PhotonVoxelDef rhs) const
inline

Definition at line 146 of file PhotonVoxels.h.

147  {
148  return !((*this) == rhs);
149  }
bool sim::PhotonVoxelDef::operator== ( const PhotonVoxelDef rhs) const

Definition at line 49 of file PhotonVoxels.cxx.

50  {
51  return ((GetRegionUpperCorner() == right.GetRegionUpperCorner()) &&
52  (GetRegionLowerCorner() == right.GetRegionLowerCorner()) &&
53  (GetSteps() == right.GetSteps()));
54  }
decltype(auto) GetRegionUpperCorner() const
Returns the volume vertex (type Point) with the highest coordinates.
std::array< unsigned int, 3U > GetSteps() const
Returns the number of voxels along each of the three dimensions.
decltype(auto) GetRegionLowerCorner() const
Returns the volume vertex (type Point) with the lowest coordinates.

Member Data Documentation

geo::Point_t sim::PhotonVoxelDef::fLowerCorner
private

Definition at line 62 of file PhotonVoxels.h.

geo::Point_t sim::PhotonVoxelDef::fUpperCorner
private

Definition at line 63 of file PhotonVoxels.h.

unsigned int sim::PhotonVoxelDef::fxSteps = 1U
private

Definition at line 64 of file PhotonVoxels.h.

unsigned int sim::PhotonVoxelDef::fySteps = 1U
private

Definition at line 65 of file PhotonVoxels.h.

unsigned int sim::PhotonVoxelDef::fzSteps = 1U
private

Definition at line 66 of file PhotonVoxels.h.


The documentation for this class was generated from the following files: