LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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.

Referenced by phot::PhotonVisibilityService::doGetVisibilityOfOpLib().

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

Definition at line 225 of file PhotonVoxels.h.

References geo::vect::toPoint().

226 {
228 }
::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 68 of file PhotonVoxels.cxx.

References util::abs(), d, e, fxSteps, fySteps, GetSteps(), GetVoxelStepCoordsUnchecked(), isInside(), n, util::to_string(), and w.

69  {
70  if (!isInside(v)) return {};
71 
72  std::array<sim::PhotonVoxelDef::NeiInfo, 8U> ret;
73 
74  // Position in voxel coordinates including floating point part
75  auto const rStepD = GetVoxelStepCoordsUnchecked(v);
76 
77  // The neighbours are the 8 corners of a cube around this point
78  std::size_t iNeigh = 0U;
79  for (int dx : {0, 1}) {
80  for (int dy : {0, 1}) {
81  for (int dz : {0, 1}) {
82  // The full 3D step
83  const int dr[3] = {dx, dy, dz};
84 
85  // The integer-only position of the current corner
86  int rStepI[3];
87  for (int d = 0; d < 3; ++d) {
88  // Round down to get the "lower left" corner
89  rStepI[d] = int(rStepD[d]);
90  // Ensure we'll stay in-bounds
91  rStepI[d] = std::max(0, rStepI[d]);
92  rStepI[d] = std::min(rStepI[d], int(GetSteps()[d]) - 2);
93  // Adjust to the corner we're actually considering
94  rStepI[d] += dr[d];
95  }
96 
97  double w = 1;
98  for (int d = 0; d < 3; ++d) {
99  // These expressions will interpolate when between the 8 corners,
100  // and extrapolate in the half-voxel space around the edges.
101  if (dr[d] == 0)
102  w *= 1 + rStepI[d] - rStepD[d];
103  else
104  w *= 1 - rStepI[d] + rStepD[d];
105  }
106 
107  const int id = (rStepI[0] + rStepI[1] * (fxSteps) + rStepI[2] * (fxSteps * fySteps));
108 
109  ret[iNeigh++] = {id, w};
110  }
111  }
112  }
113 
114  // Sanity check the weights sum to 1
115  double wSum = 0;
116  for (const NeiInfo& n : ret)
117  wSum += n.weight;
118  if (std::abs(wSum - 1) > 1e-3) {
119  std::string msg = "PhotonVoxelDef::GetNeighboringVoxelIDs():"
120  " Weights sum to " +
121  std::to_string(wSum) +
122  " (should be 1)."
123  " Weights are:";
124  for (const NeiInfo& n : ret) {
125  msg += ' ';
126  msg += std::to_string(n.weight);
127  }
128  throw std::runtime_error(msg);
129  }
130  return {ret};
131  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
constexpr auto abs(T v)
Returns the absolute value of the argument.
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:137
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
Float_t d
Definition: plot.C:235
unsigned int fxSteps
Definition: PhotonVoxels.h:64
std::array< unsigned int, 3U > GetSteps() const
Returns the number of voxels along each of the three dimensions.
Char_t n[5]
Float_t e
Definition: plot.C:35
Float_t w
Definition: plot.C:20
unsigned int sim::PhotonVoxelDef::GetNVoxels ( ) const
PhotonVoxel sim::PhotonVoxelDef::GetPhotonVoxel ( int  ID) const

Definition at line 134 of file PhotonVoxels.cxx.

References fLowerCorner, fxSteps, fySteps, and fzSteps.

Referenced by phot::CreateHybridLibrary::CreateHybridLibrary(), phot::PhotonLibraryHybrid::GetCount(), evgen::LightSource::produce(), and phot::PhotonVisibilityService::reconfigure().

135  {
136  // float TempID = (float) ID;
137 
138  // Decompose ID into steps in each direction
139  int xStep = ID % fxSteps;
140  int yStep = ((ID - xStep) / fxSteps) % fySteps;
141  int zStep = ((ID - xStep - (yStep * fxSteps)) / (fySteps * fxSteps)) % fzSteps;
142 
143  auto const VoxelSize = GetVoxelSize<geo::Vector_t>();
144 
145  double const xMin = VoxelSize.X() * (xStep) + fLowerCorner.X();
146  double const xMax = VoxelSize.X() * (xStep + 1) + fLowerCorner.X();
147  double const yMin = VoxelSize.Y() * (yStep) + fLowerCorner.Y();
148  double const yMax = VoxelSize.Y() * (yStep + 1) + fLowerCorner.Y();
149  double const zMin = VoxelSize.Z() * (zStep) + fLowerCorner.Z();
150  double const zMax = VoxelSize.Z() * (zStep + 1) + fLowerCorner.Z();
151 
152  return PhotonVoxel(xMin, xMax, yMin, yMax, zMin, zMax);
153  }
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
template<typename Point = DefaultPoint>
decltype(auto) sim::PhotonVoxelDef::GetRegionLowerCorner ( ) const

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

Referenced by phot::PhotonLibraryAnalyzer::beginJob(), sim::PhotonVoxel::GetCenter(), sim::operator<<(), operator==(), and phot::PhotonLibrary::StoreMetadata().

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

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

Referenced by phot::PhotonLibraryAnalyzer::beginJob(), sim::PhotonVoxel::GetCenter(), sim::operator<<(), operator==(), and phot::PhotonLibrary::StoreMetadata().

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

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

Definition at line 39 of file PhotonVoxels.cxx.

References fxSteps, fySteps, and fzSteps.

Referenced by phot::PhotonLibraryAnalyzer::beginJob(), GetNeighboringVoxelIDsImpl(), sim::operator<<(), operator==(), and phot::PhotonLibrary::StoreMetadata().

40  {
41  // BUG the double brace syntax is required to work around clang bug 21629
42  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
43  return {{fxSteps, fySteps, fzSteps}};
44  }
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 97 of file PhotonVoxels.h.

Referenced by sim::operator<<().

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

Definition at line 161 of file PhotonVoxels.cxx.

References fxSteps, fySteps, and fzSteps.

Referenced by phot::PhotonLibraryAnalyzer::beginJob().

162  {
163  std::array<int, 3U> ReturnVector;
164  ReturnVector[0] = ID % fxSteps;
165  ReturnVector[1] = ((ID - ReturnVector[0]) / fxSteps) % fySteps;
166  ReturnVector[2] =
167  ((ID - ReturnVector[0] - (ReturnVector[1] * fxSteps)) / (fySteps * fxSteps)) % fzSteps;
168  return ReturnVector;
169  }
unsigned int fySteps
Definition: PhotonVoxels.h:65
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 217 of file PhotonVoxels.h.

References geo::vect::toPoint().

Referenced by phot::PhotonVisibilityService::VoxelAt().

218 {
220 }
int GetVoxelIDImpl(geo::Point_t const &p) const
::geo::Point_t toPoint(Point const &p)
Convert the specified point into a geo::Point_t.
int sim::PhotonVoxelDef::GetVoxelID ( double const *  Position) const

Definition at line 61 of file PhotonVoxels.cxx.

References GetVoxelIDImpl().

62  {
63  return GetVoxelIDImpl(geo::vect::makeFromCoords<geo::Point_t>(Position));
64  }
int GetVoxelIDImpl(geo::Point_t const &p) const
int sim::PhotonVoxelDef::GetVoxelIDImpl ( geo::Point_t const &  p) const
private

Definition at line 186 of file PhotonVoxels.cxx.

References fxSteps, fySteps, GetVoxelStepCoordsUnchecked(), and isInside().

Referenced by GetVoxelID().

187  {
188  if (!isInside(p)) return -1;
189 
190  auto const stepCoords = GetVoxelStepCoordsUnchecked(p);
191 
192  // figure out how many steps this point is in the x,y,z directions;
193  // `p` is guaranteed to be in the mapped volume by the previous check
194  int xStep = static_cast<int>(stepCoords[0]);
195  int yStep = static_cast<int>(stepCoords[1]);
196  int zStep = static_cast<int>(stepCoords[2]);
197 
198  // if within bounds, generate the voxel ID
199  return (xStep + yStep * (fxSteps) + zStep * (fxSteps * fySteps));
200  }
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:137
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 208 of file PhotonVoxels.h.

Referenced by evgen::LightSource::LightSource(), sim::operator<<(), and phot::PhotonLibrary::StoreMetadata().

209 {
210  return {(fUpperCorner.X() - fLowerCorner.X()) / fxSteps,
211  (fUpperCorner.Y() - fLowerCorner.Y()) / fySteps,
212  (fUpperCorner.Z() - fLowerCorner.Z()) / fzSteps};
213 } // 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 172 of file PhotonVoxels.cxx.

References fLowerCorner, fUpperCorner, fxSteps, fySteps, fzSteps, and util::span().

Referenced by GetNeighboringVoxelIDsImpl(), and GetVoxelIDImpl().

173  {
174 
175  auto const span = fUpperCorner - fLowerCorner;
176  auto const relPos = p - fLowerCorner;
177 
178  // BUG the double brace syntax is required to work around clang bug 21629
179  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
180  return {{(relPos.X() / span.X()) * fxSteps,
181  (relPos.Y() / span.Y()) * fySteps,
182  (relPos.Z() / span.Z()) * fzSteps}};
183  } // PhotonVoxelDef::GetVoxelStepCoordsUnchecked()
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
span(IterB &&b, IterE &&e, Adaptor &&adaptor) -> span< decltype(adaptor(std::forward< IterB >(b))), decltype(adaptor(std::forward< IterE >(e))) >
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 137 of file PhotonVoxels.h.

References util::details::operator==().

Referenced by GetNeighboringVoxelIDsImpl(), and GetVoxelIDImpl().

137 { return isInsideImpl(p); }
bool isInsideImpl(geo::Point_t const &point) const
Returns whether the specified point is within the volume.
Definition: PhotonVoxels.h:151
bool sim::PhotonVoxelDef::isInsideImpl ( geo::Point_t const &  point) const
inlineprivate

Returns whether the specified point is within the volume.

Definition at line 151 of file PhotonVoxels.h.

References sim::PhotonVoxel::fVoxelMax, sim::PhotonVoxel::fVoxelMin, sim::PhotonVoxel::GetLowerCorner(), sim::PhotonVoxel::GetUpperCorner(), sim::operator<<(), and value.

152  {
153  return isInsideVolume(point, fLowerCorner, fUpperCorner);
154  }
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 212 of file PhotonVoxels.cxx.

Referenced by isInsideVolume().

213  {
214 
215  return (value >= lower) && (value < upper);
216 
217  } // PhotonVoxelDef::isInsideRange()
double value
Definition: spectrum.C:18
bool sim::PhotonVoxelDef::isInsideVolume ( geo::Point_t const &  point,
geo::Point_t const &  lower,
geo::Point_t const &  upper 
)
staticprivate

Definition at line 203 of file PhotonVoxels.cxx.

References isInsideRange().

206  {
207  return isInsideRange(point.X(), lower.X(), upper.X()) &&
208  isInsideRange(point.Y(), lower.Y(), upper.Y()) &&
209  isInsideRange(point.Z(), lower.Z(), upper.Z());
210  }
static bool isInsideRange(double value, double lower, double upper)
bool sim::PhotonVoxelDef::IsLegalVoxelID ( int  ID) const

Definition at line 156 of file PhotonVoxels.cxx.

References GetNVoxels().

157  {
158  return ((ID >= 0) && (static_cast<unsigned int>(ID) < GetNVoxels()));
159  }
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 140 of file PhotonVoxels.h.

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

Definition at line 47 of file PhotonVoxels.cxx.

References GetRegionLowerCorner(), GetRegionUpperCorner(), and GetSteps().

48  {
49  return ((GetRegionUpperCorner() == right.GetRegionUpperCorner()) &&
50  (GetRegionLowerCorner() == right.GetRegionLowerCorner()) &&
51  (GetSteps() == right.GetSteps()));
52  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
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.

Referenced by GetPhotonVoxel(), and GetVoxelStepCoordsUnchecked().

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

Definition at line 63 of file PhotonVoxels.h.

Referenced by GetVoxelStepCoordsUnchecked().

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

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