All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
larsim/larsim/Simulation/PhotonVoxels.h
Go to the documentation of this file.
1 /**
2  * @file larsim/Simulation/PhotonVoxels.h
3  * @brief Definitions of voxel data structures.
4  */
5 
6 #ifndef LARSIM_SIMULATION_PHOTONVOXELS_H
7 #define LARSIM_SIMULATION_PHOTONVOXELS_H
8 
9 // LArSoft libraries
12 
13 // C/C++ standard libraries
14 #include <array>
15 #include <optional>
16 
17 namespace sim {
18 
19  /// Representation of a single small volume (voxel).
20  class PhotonVoxel {
21  public:
22  PhotonVoxel() = default;
23  PhotonVoxel(geo::Point_t const& min, geo::Point_t const& max) : fVoxelMin(min), fVoxelMax(max)
24  {}
25  PhotonVoxel(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
26  : PhotonVoxel({xMin, yMin, zMin}, {xMax, yMax, zMax})
27  {}
28 
29  private:
30  geo::Point_t fVoxelMin;
32 
33  public:
35 
36  /// @{
37  // the choice of `decltype(auto)` is because in case `geo::Point_t` is the
38  // requested `Point` type, a reference to the data member is returned
39  // instead of a copy.
40 
41  /// Returns the voxel vertex (type `Point`) with the lowest coordinates.
42  template <typename Point = DefaultPoint>
43  decltype(auto) GetLowerCorner() const;
44 
45  /// Returns the voxel vertex (type `Point`) with the highest coordinates.
46  template <typename Point = DefaultPoint>
47  decltype(auto) GetUpperCorner() const;
48 
49  /// Returns the center of the voxel (type `Point`).
50  template <typename Point = DefaultPoint>
51  Point GetCenter() const;
52 
53  /// @}
54 
55  }; // class PhotonVoxel
56 
57  /// Representation of a region of space diced into voxels.
61 
64  unsigned int fxSteps = 1U;
65  unsigned int fySteps = 1U;
66  unsigned int fzSteps = 1U;
67 
68  public:
69  PhotonVoxelDef() = default;
70  PhotonVoxelDef(double xMin,
71  double xMax,
72  int xN,
73  double yMin,
74  double yMax,
75  int yN,
76  double zMin,
77  double zMax,
78  int zN);
79 
80  /// Returns the volume vertex (type `Point`) with the lowest coordinates.
81  template <typename Point = DefaultPoint>
82  decltype(auto) GetRegionLowerCorner() const;
83 
84  /// Returns the volume vertex (type `Point`) with the highest coordinates.
85  template <typename Point = DefaultPoint>
86  decltype(auto) GetRegionUpperCorner() const;
87 
88  /// Returns the number of voxels along each of the three dimensions.
89  std::array<unsigned int, 3U> GetSteps() const;
90 
91  /// Returns a vector describing the span of a single voxel in x, y an z [cm]
92  template <typename Vector = DefaultVector>
93  Vector GetVoxelSize() const;
94 
95  /// Returns a vector describing the full span in x, y an z [cm]
96  template <typename Vector = DefaultVector, typename Point = DefaultPoint>
97  Vector
98  GetVolumeSize() const
99  {
100  return GetRegionUpperCorner<Point>() - GetRegionLowerCorner<Point>();
101  }
102 
103  /// Returns the total number of voxels in the volume.
104  unsigned int GetNVoxels() const;
105 
106  /// Returns the ID of the voxel containing `p`, or `-1` if none.
107  template <typename Point>
108  int GetVoxelID(Point const& p) const;
109 
110  int GetVoxelID(double const*) const;
111  bool IsLegalVoxelID(int) const;
112 
113  struct NeiInfo {
114  NeiInfo() = default;
115  NeiInfo(int i, double w) : id(i), weight(w) {}
116  int id = -1;
117  double weight = 0.0;
118  };
119 
120  /**
121  * @brief Returns IDs of the eight neighboring voxels around `v`.
122  * @param v location within the mapped volume
123  * @return an optional collection of eight neighboring voxels
124  *
125  * If `v` is not inside the mapped volume, no list is returned (the optional
126  * return value evaluates to `false`).
127  * Otherwise, each of the eight voxels with the center closest to `v` are
128  * returned, each with a weight proportional to the distance of `v` from
129  * that center.
130  */
131  template <typename Point>
132  std::optional<std::array<NeiInfo, 8U>> GetNeighboringVoxelIDs(Point const& v) const;
133 
134  PhotonVoxel GetPhotonVoxel(int ID) const;
135  std::array<int, 3U> GetVoxelCoords(int ID) const;
136 
137  /// Returns whether point `p` is inside the region (upper border excluded).
138  bool
139  isInside(geo::Point_t const& p) const
140  {
141  return isInsideImpl(p);
142  }
143 
144  bool operator==(const PhotonVoxelDef& rhs) const;
145  bool
146  operator!=(const PhotonVoxelDef& rhs) const
147  {
148  return !((*this) == rhs);
149  }
150 
151  private:
152  int GetVoxelIDImpl(geo::Point_t const& p) const;
153 
154  std::optional<std::array<NeiInfo, 8U>> GetNeighboringVoxelIDsImpl(geo::Point_t const& v) const;
155 
156  /// Returns the coordinates of the cvoxel containing `p` in step units.
157  std::array<double, 3U> GetVoxelStepCoordsUnchecked(geo::Point_t const& p) const;
158 
159  /// Returns whether the specified point is within the volume.
160  bool
161  isInsideImpl(geo::Point_t const& point) const
162  {
163  return isInsideVolume(point, fLowerCorner, fUpperCorner);
164  }
165 
166  static bool isInsideVolume(geo::Point_t const& point,
167  geo::Point_t const& lower,
168  geo::Point_t const& upper);
169  static bool isInsideRange(double value, double lower, double upper);
170 
171  }; // class PhotonVoxelDef
172 
173  /// Prints the content of the specified voxel definition into a stream.
174  std::ostream& operator<<(std::ostream& out, sim::PhotonVoxelDef const& voxelDef);
175 
176 } // namespace sim
177 
178 //------------------------------------------------------------------------------
179 //--- template implementation
180 //------------------------------------------------------------------------------
181 //--- sim::PhotonVoxel
182 //------------------------------------------------------------------------------
183 template <typename Point /* = DefaultPoint */>
184 decltype(auto)
185 sim::PhotonVoxel::GetLowerCorner() const
186 {
187  return geo::vect::convertTo<Point>(fVoxelMin);
188 }
189 
190 template <typename Point /* = DefaultPoint */>
191 decltype(auto)
192 sim::PhotonVoxel::GetUpperCorner() const
193 {
194  return geo::vect::convertTo<Point>(fVoxelMax);
195 }
196 
197 template <typename Point /* = DefaultPoint */>
198 Point
200 {
201  return geo::vect::convertTo<Point>(geo::vect::middlePoint({fVoxelMin, fVoxelMax}));
202 }
203 
204 //------------------------------------------------------------------------------
205 //--- sim::PhotonVoxelDef
206 //------------------------------------------------------------------------------
207 template <typename Point /* = DefaultPoint */>
208 decltype(auto)
209 sim::PhotonVoxelDef::GetRegionLowerCorner() const
210 {
211  return geo::vect::convertTo<Point>(fLowerCorner);
212 }
213 
214 template <typename Point /* = DefaultPoint */>
215 decltype(auto)
216 sim::PhotonVoxelDef::GetRegionUpperCorner() const
217 {
218  return geo::vect::convertTo<Point>(fUpperCorner);
219 }
220 
221 //------------------------------------------------------------------------------
222 template <typename Vector /* = DefaultVector */>
223 Vector
225 {
226  return {(fUpperCorner.X() - fLowerCorner.X()) / fxSteps,
227  (fUpperCorner.Y() - fLowerCorner.Y()) / fySteps,
228  (fUpperCorner.Z() - fLowerCorner.Z()) / fzSteps};
229 } // sim::PhotonVoxelDef::GetVoxelSize()
230 
231 //------------------------------------------------------------------------------
232 template <typename Point>
233 int
235 {
236  return GetVoxelIDImpl(geo::vect::toPoint(p));
237 }
238 
239 //------------------------------------------------------------------------------
240 template <typename Point>
241 std::optional<std::array<sim::PhotonVoxelDef::NeiInfo, 8U>>
243 {
244  return GetNeighboringVoxelIDsImpl(geo::vect::toPoint(v));
245 }
246 
247 //------------------------------------------------------------------------------
248 
249 #endif // LARSIM_SIMULATION_PHOTONVOXELS_H
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:164
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
Vector GetVoxelSize() const
Returns a vector describing the span of a single voxel in x, y an z [cm].
PhotonVoxel(geo::Point_t const &min, geo::Point_t const &max)
pdgs p
Definition: selectors.fcl:22
Representation of a region of space diced into voxels.
::geo::Point_t toPoint(Point const &p)
Convert the specified point into a geo::Point_t.
int GetVoxelID(Point const &p) const
Returns the ID of the voxel containing p, or -1 if none.
bool isInside(geo::Point_t const &p) const
Returns whether point p is inside the region (upper border excluded).
Definitions of geometry vector data types.
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:44
Point GetCenter() const
Returns the center of the voxel (type Point).
decltype(auto) GetLowerCorner() const
Returns the voxel vertex (type Point) with the lowest coordinates.
Utilities to extend the interface of geometry vectors.
bool operator!=(const PhotonVoxelDef &rhs) const
PhotonVoxel(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
std::optional< std::array< NeiInfo, 8U > > GetNeighboringVoxelIDs(Point const &v) const
Returns IDs of the eight neighboring voxels around v.
Representation of a single small volume (voxel).
bool isInsideImpl(geo::Point_t const &point) const
Returns whether the specified point is within the volume.
temporary value
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
std::ostream & operator<<(std::ostream &output, const LArVoxelData &data)
decltype(auto) GetUpperCorner() const
Returns the voxel vertex (type Point) with the highest coordinates.
bool operator==(infinite_endcount_iterator< T > const &, count_iterator< T > const &)
Definition: counter.h:269
geo::Point_t middlePoint(BeginIter begin, EndIter end)
Returns the middle of the specified points.