DP3
AxisMapping.h
Go to the documentation of this file.
1 // AxisMapping.h: Map the cells of one axis to another
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_AXISMAPPING_H
11 #define LOFAR_PARMDB_AXISMAPPING_H
12 
13 #include "Grid.h"
14 
15 #include <map>
16 
17 namespace dp3 {
18 namespace parmdb {
19 
22 
27 class AxisMapping {
28  public:
30  AxisMapping(const Axis& from, const Axis& to);
31 
34  typedef std::vector<int>::const_iterator const_iterator;
35  const_iterator begin() const { return itsMapping.begin(); }
36  const_iterator end() const { return itsMapping.end(); }
38 
40  size_t size() const { return itsMapping.size(); }
41 
43  int operator[](int i) const { return itsMapping[i]; }
44 
48  const double* getScaledCenters() const { return &(itsCenters[0]); }
49 
53  const std::vector<int>& getBorders() const { return itsBorders; }
54 
55  private:
56  std::vector<int> itsMapping;
57  std::vector<double> itsCenters;
58  std::vector<int> itsBorders;
59 };
60 
67 //
77 class AxisCache {
78  public:
81  Grid getGrid(const std::vector<Box>& domains);
82 
85  Axis::ShPtr getAxis(const Axis& axis);
86 
88  void clear() {
89  itsGridCache.clear();
90  itsAxisCache.clear();
91  }
92 
93  private:
94  std::map<int64_t, std::vector<std::pair<int, int>>> itsGridCache;
95  std::map<int, Axis::ShPtr> itsAxisCache;
96 };
97 
103  private:
105  struct AxisKey {
106  AxisKey(unsigned int fromId, unsigned int toId)
107  : itsFrom(fromId), itsTo(toId) {}
108  unsigned int itsFrom;
109  unsigned int itsTo;
110 
111  bool operator==(const AxisKey that) const {
112  return itsFrom == that.itsFrom && itsTo == that.itsTo;
113  }
114  bool operator!=(const AxisKey that) const {
115  return itsFrom != that.itsFrom || itsTo != that.itsTo;
116  }
117  bool operator<(const AxisKey that) const {
118  return itsFrom < that.itsFrom ||
119  (itsFrom == that.itsFrom && itsTo < that.itsTo);
120  }
121  };
122 
123  public:
125  size_t size() const { return itsCache.size(); }
126 
128  void clear() { itsCache.clear(); }
129 
132  const AxisMapping& get(const Axis& from, const Axis& to) {
133  std::map<AxisKey, AxisMapping>::const_iterator iter =
134  itsCache.find(AxisKey(from.getId(), to.getId()));
135  return (iter == itsCache.end() ? makeMapping(from, to) : iter->second);
136  }
137 
138  private:
140  const AxisMapping& makeMapping(const Axis& from, const Axis& to);
141 
142  std::map<AxisKey, AxisMapping> itsCache;
143 };
144 
145 class GridMapping {
146  public:
149  const Grid::Location& location,
150  const Grid& src, const Grid& dest);
151 
154  unsigned int cellId, const Grid& src,
155  const Grid& dest);
156 
158  static unsigned int findCellId(AxisMappingCache& cache,
159  const Grid::Location& location,
160  const Grid& src, const Grid& dest);
161 
163  static unsigned int findcellId(AxisMappingCache& cache, unsigned int cellId,
164  const Grid& src, const Grid& dest);
165 };
166 
168 
169 } // namespace parmdb
170 } // namespace dp3
171 
172 #endif
Class representing a regular or irregular 2-D grid.
This class caches Grid objects.
Definition: AxisMapping.h:77
void clear()
Clear the cache.
Definition: AxisMapping.h:88
Grid getGrid(const std::vector< Box > &domains)
Axis::ShPtr getAxis(const Axis &axis)
This class caches axis mappings. It uses the unique id of the from-axis and to-axis as the key in the...
Definition: AxisMapping.h:102
size_t size() const
Get the number of elements.
Definition: AxisMapping.h:125
void clear()
Clear the cache.
Definition: AxisMapping.h:128
const AxisMapping & get(const Axis &from, const Axis &to)
Definition: AxisMapping.h:132
Map the cells of one axis to another.
Definition: AxisMapping.h:27
size_t size() const
Get the number of elements.
Definition: AxisMapping.h:40
AxisMapping(const Axis &from, const Axis &to)
Create the mapping.
const double * getScaledCenters() const
Definition: AxisMapping.h:48
const std::vector< int > & getBorders() const
Definition: AxisMapping.h:53
std::vector< int >::const_iterator const_iterator
Definition: AxisMapping.h:34
const_iterator begin() const
Definition: AxisMapping.h:35
const_iterator end() const
Definition: AxisMapping.h:36
int operator[](int i) const
Get the to interval for the i-th from interval.
Definition: AxisMapping.h:43
Classes representing a regular or irregular axis.
Definition: Axis.h:29
std::shared_ptr< Axis > ShPtr
Define a shared_ptr for this class.
Definition: Axis.h:32
unsigned int getId() const
Get the unique axis id.
Definition: Axis.h:49
Definition: AxisMapping.h:145
static unsigned int findCellId(AxisMappingCache &cache, const Grid::Location &location, const Grid &src, const Grid &dest)
Find the cellId in grid 'dest', given the location in grid 'src'.
static unsigned int findcellId(AxisMappingCache &cache, unsigned int cellId, const Grid &src, const Grid &dest)
Find the cellId in grid 'dest', given the cellId in grid 'src'.
static Grid::Location findLocation(AxisMappingCache &cache, unsigned int cellId, const Grid &src, const Grid &dest)
Find the location in grid 'dest', given the cellId in grid 'src'.
static Grid::Location findLocation(AxisMappingCache &cache, const Grid::Location &location, const Grid &src, const Grid &dest)
Find the location in grid 'dest', given the location in grid 'src'.
The envelope class for a 2-D grid with regular or irregular axes. -.
Definition: Grid.h:83
std::pair< size_t, size_t > Location
Define Location: A location on a 2-D grid.
Definition: Grid.h:86
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53