DP3
Grid.h
Go to the documentation of this file.
1 // Grid.h: Class representing a regular or irregular 2-D grid.
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
9 
10 #ifndef LOFAR_PARMDB_GRID_H
11 #define LOFAR_PARMDB_GRID_H
12 
13 #include "Box.h"
14 #include "Axis.h"
15 
16 #include <memory>
17 
18 namespace dp3 {
19 namespace parmdb {
20 
22 class Grid;
23 
26 
28 class GridRep {
29  public:
30  typedef std::shared_ptr<GridRep> ShPtr;
31 
34 
37 
44  GridRep(const std::vector<Grid>& domains, bool sort);
45 
52  GridRep(const std::vector<Box>& domains, bool sort);
53 
55  bool isDefault() const { return itsIsDefault; }
56 
59  Axis::ShPtr& getAxis(size_t n) { return itsAxes[n]; }
60  const Axis::ShPtr& getAxis(size_t n) const { return itsAxes[n]; }
62 
63  private:
65  void setup(const std::vector<Box>& domains);
66 
68  void setup(const std::vector<Grid>& domains);
69 
73  Axis::ShPtr combineAxes(const std::vector<Grid>& grids, unsigned int axis,
74  unsigned int n, unsigned int step) const;
75 
76  Axis::ShPtr itsAxes[2];
77 
79  bool itsIsDefault;
80 };
81 
83 class Grid {
84  public:
86  using Location = std::pair<size_t, size_t>;
87 
89  Grid() : itsRep(new GridRep()) {}
90 
92  Grid(Axis::ShPtr first, Axis::ShPtr second)
93  : itsRep(new GridRep(first, second)) {}
94 
101  Grid(const std::vector<Grid>& grids, bool sort = false);
102 
108  Grid(const std::vector<Box>& domains, bool sort = false)
109  : itsRep(new GridRep(domains, sort)) {}
110 
112  bool checkIntervals(const Grid& that) const;
113 
115  bool isDefault() const { return itsRep->isDefault(); }
116 
119  const Axis::ShPtr& getAxis(size_t n) const { return itsRep->getAxis(n); }
120  const Axis::ShPtr& operator[](size_t n) const { return itsRep->getAxis(n); }
122 
125  size_t nx() const { return getAxis(0)->size(); }
126  size_t ny() const { return getAxis(1)->size(); }
128 
130  std::pair<size_t, size_t> shape() const { return std::make_pair(nx(), ny()); }
131 
133  size_t size() const { return nx() * ny(); }
134 
136  unsigned int getCellId(const Location& location) const {
137  return location.second * nx() + location.first;
138  }
139 
141  Location getCellLocation(unsigned int id) const {
142  return Location(id % nx(), id / nx());
143  }
144 
146  Point getCellCenter(const Location& location) const {
147  assert(location.first < nx() && location.second < ny());
148  return Point(getAxis(0)->center(location.first),
149  getAxis(1)->center(location.second));
150  }
151 
154  Box getCell(const Location& location) const {
155  assert(location.first < nx() && location.second < ny());
156  return Box(Point(getAxis(0)->lower(location.first),
157  getAxis(1)->lower(location.second)),
158  Point(getAxis(0)->upper(location.first),
159  getAxis(1)->upper(location.second)));
160  }
161 
162  Box getCell(unsigned int id) const { return getCell(getCellLocation(id)); }
164 
166  Box getBoundingBox() const {
167  return Box(Point(getAxis(0)->start(), getAxis(1)->start()),
168  Point(getAxis(0)->end(), getAxis(1)->end()));
169  }
170 
172  Box getBoundingBox(const Location& start, const Location& end) const {
173  assert(start.first <= end.first && start.second <= end.second);
174  return unite(getCell(start), getCell(end));
175  }
176 
180  Location locate(const Point& point, bool biasRight = true) const {
181  return std::make_pair(getAxis(0)->locate(point.first, biasRight),
182  getAxis(1)->locate(point.second, biasRight));
183  }
184 
190  Grid subset(const Box&) const;
191  Grid subset(const Box&, Location& index) const;
192  Grid subset(const Location& start, const Location& end) const;
194 
196  void toDomains(std::vector<Box>& domains) const;
197 
201  bool operator<(const Grid& that) const {
202  return getBoundingBox().lowerY() < that.getBoundingBox().lowerY() ||
203  (getBoundingBox().lowerY() == that.getBoundingBox().lowerY() &&
204  getBoundingBox().lowerX() < that.getBoundingBox().lowerX());
205  }
206  bool operator>(const Grid& that) const {
207  return getBoundingBox().lowerY() > that.getBoundingBox().lowerY() ||
208  (getBoundingBox().lowerY() == that.getBoundingBox().lowerY() &&
209  getBoundingBox().lowerX() > that.getBoundingBox().lowerX());
210  }
212 
213  private:
214  GridRep::ShPtr itsRep;
215 };
216 
219  public:
220  CellIterator(const Grid::Location& start, const Grid::Location& end)
221  : itsStart(start), itsEnd(end), itsLocation(start) {}
222 
224  bool atEnd() const { return itsLocation.second > itsEnd.second; }
225 
228  void operator++() {
229  if (++itsLocation.first > itsEnd.first) {
230  itsLocation.first = itsStart.first;
231  ++itsLocation.second;
232  }
233  }
234  void operator++(int) { operator++(); }
236 
238  const Grid::Location& operator*() const { return itsLocation; }
239 
241  const Grid::Location* operator->() const { return &itsLocation; }
242 
243  private:
244  Grid::Location itsStart;
245  Grid::Location itsEnd;
246  Grid::Location itsLocation;
247 };
248 
250 
251 } // namespace parmdb
252 } // namespace dp3
253 
254 #endif
Classes representing a regular or irregular axis.
Class representing a 2-dim box.
std::shared_ptr< Axis > ShPtr
Define a shared_ptr for this class.
Definition: Axis.h:32
Class representing a 2-dim box.
Definition: Box.h:36
double lowerX() const
Definition: Box.h:66
double lowerY() const
Definition: Box.h:67
Utility class that simplifies iterating over a 2-D range of cells.
Definition: Grid.h:218
void operator++()
Definition: Grid.h:228
const Grid::Location & operator*() const
STL-like iterator dereference.
Definition: Grid.h:238
const Grid::Location * operator->() const
STL-like iterator pointer.
Definition: Grid.h:241
bool atEnd() const
Test if the iterator is at the end.
Definition: Grid.h:224
CellIterator(const Grid::Location &start, const Grid::Location &end)
Definition: Grid.h:220
void operator++(int)
Definition: Grid.h:234
The letter class for a 2-D grid with regular or irregular axes.
Definition: Grid.h:28
GridRep()
Default constructor uses two default RegularAxis objects.
GridRep(const std::vector< Grid > &domains, bool sort)
Axis::ShPtr & getAxis(size_t n)
Definition: Grid.h:59
std::shared_ptr< GridRep > ShPtr
Definition: Grid.h:30
const Axis::ShPtr & getAxis(size_t n) const
Definition: Grid.h:60
GridRep(Axis::ShPtr first, Axis::ShPtr second)
Create from given axes.
bool isDefault() const
Is it the default grid?
Definition: Grid.h:55
GridRep(const std::vector< Box > &domains, bool sort)
The envelope class for a 2-D grid with regular or irregular axes. -.
Definition: Grid.h:83
Grid()
Default constructor creates empty axes.
Definition: Grid.h:89
std::pair< size_t, size_t > Location
Define Location: A location on a 2-D grid.
Definition: Grid.h:86
size_t size() const
Get the total number of cells.
Definition: Grid.h:133
unsigned int getCellId(const Location &location) const
Get the cell id from an (x,y) location.
Definition: Grid.h:136
const Axis::ShPtr & operator[](size_t n) const
Definition: Grid.h:120
Location locate(const Point &point, bool biasRight=true) const
Definition: Grid.h:180
void toDomains(std::vector< Box > &domains) const
Convert the grid to domain boxes and append them to the vector.
bool checkIntervals(const Grid &that) const
Check if the corresponding intervals in this and that grid are the same.
Box getCell(const Location &location) const
Definition: Grid.h:154
Grid(const std::vector< Grid > &grids, bool sort=false)
Grid subset(const Box &) const
Box getBoundingBox(const Location &start, const Location &end) const
Get the bounding box of part of the grid.
Definition: Grid.h:172
Grid(Axis::ShPtr first, Axis::ShPtr second)
Create a grid using the given axes.
Definition: Grid.h:92
Grid subset(const Box &, Location &index) const
Grid(const std::vector< Box > &domains, bool sort=false)
Definition: Grid.h:108
bool operator<(const Grid &that) const
Definition: Grid.h:201
std::pair< size_t, size_t > shape() const
Get the grid shape (nx,ny).
Definition: Grid.h:130
Point getCellCenter(const Location &location) const
Get the coordinates of the center of the given cell.
Definition: Grid.h:146
size_t ny() const
Definition: Grid.h:126
bool isDefault() const
Is it the default grid?
Definition: Grid.h:115
Box getBoundingBox() const
Get the bounding box of the grid.
Definition: Grid.h:166
Grid subset(const Location &start, const Location &end) const
bool operator>(const Grid &that) const
Definition: Grid.h:206
size_t nx() const
Definition: Grid.h:125
const Axis::ShPtr & getAxis(size_t n) const
Definition: Grid.h:119
Box getCell(unsigned int id) const
Definition: Grid.h:162
Location getCellLocation(unsigned int id) const
Get the (x,y) location from a cell id.
Definition: Grid.h:141
Box unite(const Box &lhs, const Box &rhs)
std::pair< double, double > Point
Point: A point in a 2-D space.
Definition: Box.h:23
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53