LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SpacePartition.h
Go to the documentation of this file.
1 
18 #ifndef LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_SPACEPARTITION_H
19 #define LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_SPACEPARTITION_H
20 
21 // LArSoft libraries
23 
24 // C/C++ standard libraries
25 #include <cstddef> // std::ptrdiff_t
26 #include <cmath> // std::ceil()
27 #include <vector>
28 #include <array>
29 #include <string>
30 #include <stdexcept> // std::runtime_error
31 
32 
33 namespace lar {
34  namespace example {
35 
36  // BEGIN RemoveIsolatedSpacePoints group -----------------------------------
39 
85  template <typename Point>
87 
88  namespace details {
89  template <typename Point>
90  struct PointTraits_t;
91 
93  template <typename Point>
94  using ExtractCoordType_t
96 
97  } // namespace details
98 
99 
101  template <typename Coord>
102  struct CoordRange {
104  using Coord_t = Coord;
105 
108 
110  bool contains(Coord_t c) const;
111 
113  bool empty() const;
114 
116  bool valid() const;
117 
119  Coord_t size() const;
120 
122  Coord_t offset(Coord_t c) const { return c - lower; }
123 
125  bool operator== (const Range_t& as) const;
126 
128  bool operator!= (const Range_t& than) const;
129 
130  }; // CoordRange<>
131 
132 
134  template <typename Coord>
135  struct CoordRangeCells: public CoordRange<Coord> {
137 
139  using Coord_t = typename Base_t::Coord_t;
140 
142 
149  CoordRangeCells(Coord_t low, Coord_t high, Coord_t cs);
150 
156  CoordRangeCells(Base_t const& range, Coord_t cs);
157 
158 
160  std::ptrdiff_t findCell(Coord_t c) const;
161 
162  }; // CoordRangeCells<>
163 
164 
221  template <typename PointIter>
223  using Point_t = decltype(*(PointIter()));
225 
226  public:
230 
232  using Indexer_t = typename Grid_t::Indexer_t;
233 
235  using CellIndexOffset_t = typename Indexer_t::CellIndexOffset_t;
236 
237  using CellIndex_t = typename Indexer_t::CellIndex_t;
238 
240  using CellID_t = typename Indexer_t::CellID_t;
241 
243  using Cell_t = typename Grid_t::Cell_t;
244 
247  (Range_t rangeX, Range_t rangeY, Range_t rangeZ);
248 
251  void fill(PointIter begin, PointIter end);
252 
255  CellIndexOffset_t pointIndex(Point_t const& point) const;
256 
258  Indexer_t const& indexManager() const
259  { return data.indexManager(); }
260 
262  bool has(CellIndexOffset_t ofs) const
263  { return data.has(ofs); }
264 
266  Cell_t const& operator[] (CellIndex_t index) const
267  { return data[index]; }
268 
270  typename Grid_t::const_iterator begin() const;
271 
273  typename Grid_t::const_iterator end() const;
274 
275  protected:
277 
279 
283 
285 
286  }; // SpacePartition<>
287 
288 
289 
290  //--------------------------------------------------------------------------
291 
293  namespace details {
295  template <typename Cont, typename Data>
297  static Data x(Cont const& p) { return p[0]; }
298  static Data y(Cont const& p) { return p[1]; }
299  static Data z(Cont const& p) { return p[2]; }
300  }; // PositionExtractorFromArray<T*>
301 
302  template <typename Point>
303  auto extractPositionX(Point const& point)
304  { return PositionExtractor<Point>::x(point); }
305  template <typename Point>
306  auto extractPositionY(Point const& point)
307  { return PositionExtractor<Point>::y(point); }
308  template <typename Point>
309  auto extractPositionZ(Point const& point)
310  { return PositionExtractor<Point>::z(point); }
311 
312  template <typename Point>
313  struct PointTraits_t {
315  using Coord_t = decltype(extractPositionX(std::decay_t<Point>()));
316  }; // ExtractCoordTypeHelper_t
317 
318  } // namespace details
319 
321  template <typename T>
322  struct PositionExtractor<T*>:
324  {};
325 
327  template <typename T>
328  struct PositionExtractor<std::array<T, 3U>>:
329  public details::PositionExtractorFromArray<std::array<T, 3U>, T>
330  {};
331 
334  template <typename T>
335  struct PositionExtractor<std::vector<T>>:
336  public details::PositionExtractorFromArray<std::vector<T>, T>
337  {};
338 
339 
341  // END RemoveIsolatedSpacePoints group -------------------------------------
342 
343  } // namespace example
344 } // namespace lar
345 
346 
347 //------------------------------------------------------------------------------
348 //--- lar::example::details
349 //---
350 namespace lar {
351  namespace example {
352  namespace details {
353 
355  template <typename Coord>
356  std::array<size_t, 3> diceVolume(
357  CoordRangeCells<Coord> const& rangeX,
358  CoordRangeCells<Coord> const& rangeY,
359  CoordRangeCells<Coord> const& rangeZ
360  )
361  {
362  return {{
363  size_t(std::ceil(rangeX.size() / rangeX.cellSize)),
364  size_t(std::ceil(rangeY.size() / rangeY.cellSize)),
365  size_t(std::ceil(rangeZ.size() / rangeZ.cellSize))
366  }};
367  } // diceVolume()
368 
369  } // namespace details
370  } // namespace example
371 } // namespace lar
372 
373 
374 //------------------------------------------------------------------------------
375 //--- template implementation
376 //------------------------------------------------------------------------------
377 //--- lar::example::CoordRange
378 //---
379 template <typename Coord>
381  { return (lower <= c) && (upper >= c); }
382 
383 template <typename Coord>
385  { return lower == upper; }
386 
387 
388 template <typename Coord>
390  { return lower <= upper; }
391 
392 template <typename Coord>
394  { return upper - lower; }
395 
396 template <typename Coord>
398  { return (upper == as.upper) && (lower == as.lower); }
399 
400 template <typename Coord>
402  { return (upper != than.upper) || (lower != than.lower); }
403 
404 
405 //------------------------------------------------------------------------------
406 //--- lar::example::CoordRange
407 //---
408 template <typename Coord>
410  (Coord_t low, Coord_t high, Coord_t cs)
411  : Base_t(low, high), cellSize(cs)
412  {}
413 
414 template <typename Coord>
416  (Base_t const& range, Coord_t cs)
417  : Base_t(range), cellSize(cs)
418  {}
419 
420 //------------------------------------------------------------------------------
421 template <typename Coord>
423  { return std::ptrdiff_t(Base_t::offset(c) / cellSize); }
424 
425 
426 //------------------------------------------------------------------------------
427 //--- lar::example::SpacePartition
428 //---
429 template <typename PointIter>
431  (Range_t rangeX, Range_t rangeY, Range_t rangeZ)
432  : xRange(rangeX)
433  , yRange(rangeY)
434  , zRange(rangeZ)
435  , data(details::diceVolume(xRange, yRange, zRange))
436 {
437  /*
438  std::cout << "Grid: "
439  << indexManager().sizeX() << " x "
440  << indexManager().sizeY() << " x " << indexManager().sizeZ()
441  << " (" << indexManager().size() << " cells)"
442  << "\n range X: " << xRange.lower << " -- " << xRange.upper << " [/" << xRange.cellSize << "]"
443  << "\n range Y: " << yRange.lower << " -- " << yRange.upper << " [/" << yRange.cellSize << "]"
444  << "\n range Z: " << zRange.lower << " -- " << zRange.upper << " [/" << zRange.cellSize << "]"
445  << std::endl;
446  */
447 } // lar::example::SpacePartition<>::SpacePartition
448 
449 
450 //--------------------------------------------------------------------------
451 template <typename PointIter>
453  (PointIter begin, PointIter end)
454 {
455 
456  PointIter it = begin;
457  while (it != end) {
458  // if the point is outside the volume, pointIndex will throw an exception
459  data.insert(pointIndex(*it), it);
460  ++it;
461  } // while
462 
463 } // lar::example::SpacePartition<>::fill()
464 
465 
466 //--------------------------------------------------------------------------
467 template <typename PointIter>
470 {
471  // compute the cell ID coordinates
472  Coord_t const x = details::extractPositionX(point);
473  CellDimIndex_t const xc = xRange.findCell(x);
474  if (!data.hasX(xc)) {
475  throw std::runtime_error
476  ("Point out of the volume (x = " + std::to_string(x) + ")");
477  }
478 
479  Coord_t const y = details::extractPositionY(point);
480  CellDimIndex_t const yc = yRange.findCell(y);
481  if (!data.hasY(yc)) {
482  throw std::runtime_error
483  ("Point out of the volume (y = " + std::to_string(y) + ")");
484  }
485 
486  Coord_t const z = details::extractPositionZ(point);
487  CellDimIndex_t const zc = zRange.findCell(z);
488  if (!data.hasZ(zc)) {
489  throw std::runtime_error
490  ("Point out of the volume (z = " + std::to_string(z) + ")");
491  }
492 
493  // return its index
494  return data.index(CellID_t{{ xc, yc, zc }});
495 
496 } // lar::example::SpacePartition<>::pointIndex()
497 
498 
499 //--------------------------------------------------------------------------
500 
501 #endif // LAREXAMPLES_ALGORITHMS_REMOVEISOLATEDSPACEPOINTS_SPACEPARTITION_H
Float_t x
Definition: compare.C:6
A container of points sorted in cells.
Range_t zRange
coordinates of the contained volume on z axis
Range_t yRange
coordinates of the contained volume on z axis
Coord_t size() const
Returns the size of the range (no check)
Float_t y
Definition: compare.C:6
Coord_t cellSize
length of the side of each cubic cell
std::vector< Datum_t > Cell_t
type of a single cell container
typename Cells_t::const_iterator const_iterator
type of iterator to all cells
Double_t z
Definition: plot.C:279
CellIndexOffset_t pointIndex(Point_t const &point) const
Range_t xRange
coordinates of the contained volume on x axis
typename details::PointTraits_t< Point >::Coord_t ExtractCoordType_t
type of Point coordinate
Helper extractor for point coordinates.
typename Grid_t::Indexer_t Indexer_t
type of index manager of the grid
Range of coordinates.
STL namespace.
Coord_t cellSize
size of a single cell
bool contains(Coord_t c) const
Returns whether c is contained in the range (inclusve)
enum geo::coordinates Coord_t
Enumerate the possible plane projections.
details::ExtractCoordType_t< Point_t > Coord_t
type of point coordinate
bool has(CellIndexOffset_t ofs) const
Returns whether there is a cell with the specified index (signed!)
Coord_t upper
upper boundary
std::array< size_t, 3 > diceVolume(CoordRangeCells< Coord > const &rangeX, CoordRangeCells< Coord > const &rangeY, CoordRangeCells< Coord > const &rangeZ)
Returns the dimensions of a grid diced with the specified size.
Coord_t lower
lower boundary
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
auto extractPositionY(Point const &point)
bool operator!=(const Range_t &than) const
Returns whether the specified range has limits different than this.
SpacePartition(Range_t rangeX, Range_t rangeY, Range_t rangeZ)
Constructs the partition in a given volume with the given cell size.
bool valid() const
Returns whether the range is valid (empty is also valid)
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
decltype(*(PointIter())) Point_t
type of the point
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:34
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
Coord_t offset(Coord_t c) const
Returns the distance of the specified coordinate from the lower bound.
typename Indexer_t::CellIndexOffset_t CellIndexOffset_t
type of difference between cell indices
typename Grid_t::Cell_t Cell_t
type of cell
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
decltype(extractPositionX(std::decay_t< Point >())) Coord_t
type of Point coordinate
bool operator==(const Range_t &as) const
Returns whether the specified range has the same limits as this.
bool empty() const
Returns whether the range is empty.
Base for PositionExtractor on random-access containers.
typename Indexer_t::CellDimIndex_t CellDimIndex_t
type of difference between indices
bool operator!=(geometry_element_iterator< GEOIDITER > const &iter, GEOIDITER const &id_iter)
Comparison operator: geometry ID and element point to different IDs.
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
LArSoft-specific namespace.
Grid_t data
container of points
auto extractPositionX(Point const &point)
typename Grid_t::CellDimIndex_t CellDimIndex_t
typename Indexer_t::CellID_t CellID_t
type of cell index
std::ptrdiff_t findCell(Coord_t c) const
Returns the index of the cell for coordinate c.
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
Containers with indices in 1, 2 and 3 dimensions.
Range of coordinates.
Indexer_t const & indexManager() const
Returns the index manager of the grid.
auto extractPositionZ(Point const &point)
bool operator==(geometry_element_iterator< GEOIDITER > const &iter, GEOIDITER const &id_iter)
Comparison operator: geometry ID and element point to the same ID.
CoordRangeCells(Coord_t low, Coord_t high, Coord_t cs)
Constructor: assigns range and cell size.
Coord_t Coord_t
data type for coordinate
Base class for a container of data arranged on a 3D-grid.
void fill(PointIter begin, PointIter end)
typename Indexer_t::CellIndex_t CellIndex_t