CMlib
Cell mapping algorithms in C++
css.h
Go to the documentation of this file.
1 #ifndef CELL_STATE_SPACE_H
2 #define CELL_STATE_SPACE_H
3 
4 #include "cell.h"
5 #include <vector>
6 #include <cmath>
7 
8 namespace cm {
9 
22  template <class CellType, class IDType, class StateVectorType>
24  public:
28  virtual const CellType& getCellAtState(const StateVectorType& state) const = 0;
32  virtual CellType& getCellAtState(const StateVectorType& state) = 0;
36  virtual const CellType& getCell(const IDType id) const = 0;
40  virtual CellType& getCell(const IDType id) = 0;
41  virtual const StateVectorType& getCenter() const = 0;
42  virtual const StateVectorType& getWidth() const = 0;
43  };
44 
45  template <class CellType, class IDType, class StateVectorType>
46  class UniformCellStateSpace : public CellStateSpaceBase<CellType, IDType, StateVectorType> {
47  protected:
48  std::vector<CellType> cells;
49  StateVectorType center;
50  StateVectorType width;
51  std::vector<IDType> cellCounts;
52  std::vector<IDType> cellBase;
53  IDType cellSum;
54  IDType dimension;
55  StateVectorType cellWidth;
56  public:
57  UniformCellStateSpace(StateVectorType center, StateVectorType width, const std::vector<IDType>& cellCounts) :
59  dimension = cellCounts.size();
60  cellSum = 1;
61  for (const IDType& u : cellCounts) {
62  cellSum *= u;
63  }
64  cellBase.resize(dimension);
65  cellBase[0]=1;
66  for (IDType i=1; i<dimension; i++) {
67  cellBase[i] = cellCounts[i-1] * cellBase[i-1];
68  }
69  cells.resize(cellSum+1); // +1 for the Sink cell
70  for (IDType i=0; i<dimension; i++) {
71  cellWidth[i] = width[i] / cellCounts[i];
72  }
73  }
74  const StateVectorType& getWidth() const {
75  return width;
76  }
77  const StateVectorType& getCenter() const {
78  return center;
79  }
80  IDType getIDFromCellCoord(std::vector<IDType> cellCoord) {
81  IDType id = 1;
82  for (IDType i=0; i<dimension; i++) {
83  id += cellCoord[i] * cellBase[i];
84  }
85  return id;
86  }
87  IDType getID(const StateVectorType& state) const {
88  bool inside = true; // Out of state space bounds check
89  for (IDType i=0; i<dimension; i++) {
90  inside &= (center[i]-0.5*width[i] <= state[i]) && (state[i] <= center[i]+0.5*width[i]);
91  }
92  IDType id;
93  if (!inside) {
94  id = 0; // Sink cell
95  } else {
96  IDType cellCoord;
97  id = 1; // Skip the Sink cell (id = 0)
98  for (IDType i=0; i<dimension; i++) {
99  cellCoord = (IDType)floor((state[i] - (center[i]-0.5*width[i]))/cellWidth[i]);
100  id += cellCoord * cellBase[i];
101  }
102  }
103  return id;
104  }
105  CellType& getCellAtState(const StateVectorType& state) {
106  return cells[getID(state)];
107  }
108  const CellType& getCellAtState(const StateVectorType& state) const {
109  return cells[getID(state)];
110  }
111  CellType& getCell(const IDType ID) {
112  return cells[ID];
113  }
114  const CellType& getCell(const IDType ID) const {
115  return cells[ID];
116  }
117  const StateVectorType getCenter(const IDType ID) const {
118  // Calculate the center point of the given cell
119  if (ID == 0) {
120  return center;
121  } else {
122  IDType id = ID-1;
123  std::vector<IDType> cellCoord(dimension);
124  // Calculate cell-coordinate
125  for (IDType j=dimension-1; j>0; j--) {
126  cellCoord[j] = id / cellBase[j];
127  id -= cellCoord[j]*cellBase[j];
128  }
129  cellCoord[0]=id;
130  StateVectorType centerState = center - 0.5*width + 0.5*cellWidth;
131  for (IDType j=0; j<dimension; j++) {
132  centerState[j] += cellCoord[j] * cellWidth[j];
133  }
134  return centerState;
135  }
136  }
137  IDType getCellSum() const {
138  return cellSum;
139  }
140  const std::vector<IDType> &getCellCounts() const {
141  return cellCounts;
142  }
143  };
144 
151  template <class CellType, class IDType, class StateVectorType>
152  class SCMUniformCellStateSpace : public UniformCellStateSpace<CellType, IDType, StateVectorType> {
153  public:
154  SCMUniformCellStateSpace(StateVectorType center, StateVectorType width, const std::vector<IDType>& cellCounts) :
155  UniformCellStateSpace<CellType, IDType, StateVectorType>(center, width, cellCounts) {
156  }
157  const IDType getImage(const IDType ID) const {
158  return this->cells[ID].getImage();
159  }
160  const IDType getGroup(const IDType ID) const {
161  return this->cells[ID].getGroup();
162  }
163  const IDType getStep(const IDType ID) const {
164  return this->cells[ID].getStep();
165  }
166  void setImage(const IDType ID, const IDType image) {
167  this->cells[ID].setImage(image);
168  }
169  void setGroup(const IDType ID, const IDType group) {
170  this->cells[ID].setGroup(group);
171  }
172  void setStep(const IDType ID, const IDType step) {
173  this->cells[ID].setStep(step);
174  }
175  };
176 
177 }
178 
179 #endif //CELL_STATE_SPACE_H
void setImage(const IDType ID, const IDType image)
Definition: css.h:166
Definition: css.h:46
IDType getID(const StateVectorType &state) const
Definition: css.h:87
const StateVectorType & getWidth() const
Definition: css.h:74
virtual const CellType & getCellAtState(const StateVectorType &state) const =0
Returns the cell corresponding to a point in the state space (const)
StateVectorType width
The center of the state space.
Definition: css.h:50
std::vector< IDType > cellBase
Number of cells along each dimension.
Definition: css.h:52
const CellType & getCell(const IDType ID) const
Returns the cell with the given ID (const)
Definition: css.h:114
virtual const CellType & getCell(const IDType id) const =0
Returns the cell with the given ID (const)
std::vector< IDType > cellCounts
The width of the state space.
Definition: css.h:51
CellType & getCellAtState(const StateVectorType &state)
Returns the cell corresponding to a point in the state space.
Definition: css.h:105
const IDType getGroup(const IDType ID) const
Definition: css.h:160
void setGroup(const IDType ID, const IDType group)
Definition: css.h:169
Definition: css.h:152
virtual const StateVectorType & getCenter() const =0
UniformCellStateSpace(StateVectorType center, StateVectorType width, const std::vector< IDType > &cellCounts)
The width of all cells.
Definition: css.h:57
const CellType & getCellAtState(const StateVectorType &state) const
Returns the cell corresponding to a point in the state space (const)
Definition: css.h:108
StateVectorType cellWidth
The dimension of the state space.
Definition: css.h:55
const StateVectorType & getCenter() const
Definition: css.h:77
SCMUniformCellStateSpace(StateVectorType center, StateVectorType width, const std::vector< IDType > &cellCounts)
Definition: css.h:154
const IDType getStep(const IDType ID) const
Definition: css.h:163
IDType getIDFromCellCoord(std::vector< IDType > cellCoord)
Definition: css.h:80
const std::vector< IDType > & getCellCounts() const
Definition: css.h:140
Base class for classes describing state space discretization.
Definition: css.h:23
Definition: cell.h:6
CellType & getCell(const IDType ID)
Returns the cell with the given ID.
Definition: css.h:111
virtual const StateVectorType & getWidth() const =0
StateVectorType center
Directly accessible container for cell objects.
Definition: css.h:49
IDType dimension
The total number of cells within the state space.
Definition: css.h:54
const StateVectorType getCenter(const IDType ID) const
Definition: css.h:117
std::vector< CellType > cells
Definition: css.h:48
void setStep(const IDType ID, const IDType step)
Definition: css.h:172
const IDType getImage(const IDType ID) const
Definition: css.h:157
IDType getCellSum() const
Definition: css.h:137
IDType cellSum
Base for addressing the 1D array for multi dimensional cases.
Definition: css.h:53