array2d.h
Go to the documentation of this file.
1 #ifndef ARRAY2D_H
2 #define ARRAY2D_H
3 
4 #include <assert.h>
5 #include <gmapping/utils/point.h>
7 
8 #include <iostream>
9 
10 namespace GMapping {
11 
12 template<class Cell, const bool debug=false> class Array2D{
13  public:
14  Array2D(int xsize=0, int ysize=0);
15  Array2D& operator=(const Array2D &);
17  ~Array2D();
18  void clear();
19  void resize(int xmin, int ymin, int xmax, int ymax);
20 
21 
22  inline bool isInside(int x, int y) const;
23  inline const Cell& cell(int x, int y) const;
24  inline Cell& cell(int x, int y);
25  inline AccessibilityState cellState(int x, int y) const { return (AccessibilityState) (isInside(x,y)?(Inside|Allocated):Outside);}
26 
27  inline bool isInside(const IntPoint& p) const { return isInside(p.x, p.y);}
28  inline const Cell& cell(const IntPoint& p) const {return cell(p.x,p.y);}
29  inline Cell& cell(const IntPoint& p) {return cell(p.x,p.y);}
30  inline AccessibilityState cellState(const IntPoint& p) const { return cellState(p.x, p.y);}
31 
32  inline int getPatchSize() const{return 0;}
33  inline int getPatchMagnitude() const{return 0;}
34  inline int getXSize() const {return m_xsize;}
35  inline int getYSize() const {return m_ysize;}
36  inline Cell** cells() {return m_cells;}
37  Cell ** m_cells;
38  protected:
40 };
41 
42 
43 template <class Cell, const bool debug>
44 Array2D<Cell,debug>::Array2D(int xsize, int ysize){
45 // assert(xsize>0);
46 // assert(ysize>0);
47  m_xsize=xsize;
48  m_ysize=ysize;
49  if (m_xsize>0 && m_ysize>0){
50  m_cells=new Cell*[m_xsize];
51  for (int i=0; i<m_xsize; i++)
52  m_cells[i]=new Cell[m_ysize];
53  }
54  else{
55  m_xsize=m_ysize=0;
56  m_cells=0;
57  }
58  if (debug){
59  std::cerr << __func__ << std::endl;
60  std::cerr << "m_xsize= " << m_xsize<< std::endl;
61  std::cerr << "m_ysize= " << m_ysize<< std::endl;
62  }
63 }
64 
65 template <class Cell, const bool debug>
67  if (debug || m_xsize!=g.m_xsize || m_ysize!=g.m_ysize){
68  for (int i=0; i<m_xsize; i++)
69  delete [] m_cells[i];
70  delete [] m_cells;
71  m_xsize=g.m_xsize;
72  m_ysize=g.m_ysize;
73  m_cells=new Cell*[m_xsize];
74  for (int i=0; i<m_xsize; i++)
75  m_cells[i]=new Cell[m_ysize];
76  }
77  for (int x=0; x<m_xsize; x++)
78  for (int y=0; y<m_ysize; y++)
79  m_cells[x][y]=g.m_cells[x][y];
80 
81  if (debug){
82  std::cerr << __func__ << std::endl;
83  std::cerr << "m_xsize= " << m_xsize<< std::endl;
84  std::cerr << "m_ysize= " << m_ysize<< std::endl;
85  }
86  return *this;
87 }
88 
89 template <class Cell, const bool debug>
91  m_xsize=g.m_xsize;
92  m_ysize=g.m_ysize;
93  m_cells=new Cell*[m_xsize];
94  for (int x=0; x<m_xsize; x++){
95  m_cells[x]=new Cell[m_ysize];
96  for (int y=0; y<m_ysize; y++)
97  m_cells[x][y]=g.m_cells[x][y];
98  }
99  if (debug){
100  std::cerr << __func__ << std::endl;
101  std::cerr << "m_xsize= " << m_xsize<< std::endl;
102  std::cerr << "m_ysize= " << m_ysize<< std::endl;
103  }
104 }
105 
106 template <class Cell, const bool debug>
108  if (debug){
109  std::cerr << __func__ << std::endl;
110  std::cerr << "m_xsize= " << m_xsize<< std::endl;
111  std::cerr << "m_ysize= " << m_ysize<< std::endl;
112  }
113  for (int i=0; i<m_xsize; i++){
114  delete [] m_cells[i];
115  m_cells[i]=0;
116  }
117  delete [] m_cells;
118  m_cells=0;
119 }
120 
121 template <class Cell, const bool debug>
123  if (debug){
124  std::cerr << __func__ << std::endl;
125  std::cerr << "m_xsize= " << m_xsize<< std::endl;
126  std::cerr << "m_ysize= " << m_ysize<< std::endl;
127  }
128  for (int i=0; i<m_xsize; i++){
129  delete [] m_cells[i];
130  m_cells[i]=0;
131  }
132  delete [] m_cells;
133  m_cells=0;
134  m_xsize=0;
135  m_ysize=0;
136 }
137 
138 
139 template <class Cell, const bool debug>
140 void Array2D<Cell,debug>::resize(int xmin, int ymin, int xmax, int ymax){
141  int xsize=xmax-xmin;
142  int ysize=ymax-ymin;
143  Cell ** newcells=new Cell *[xsize];
144  for (int x=0; x<xsize; x++){
145  newcells[x]=new Cell[ysize];
146  }
147  int dx= xmin < 0 ? 0 : xmin;
148  int dy= ymin < 0 ? 0 : ymin;
149  int Dx=xmax<this->m_xsize?xmax:this->m_xsize;
150  int Dy=ymax<this->m_ysize?ymax:this->m_ysize;
151  for (int x=dx; x<Dx; x++){
152  for (int y=dy; y<Dy; y++){
153  newcells[x-xmin][y-ymin]=this->m_cells[x][y];
154  }
155  delete [] this->m_cells[x];
156  }
157  delete [] this->m_cells;
158  this->m_cells=newcells;
159  this->m_xsize=xsize;
160  this->m_ysize=ysize;
161 }
162 
163 template <class Cell, const bool debug>
164 inline bool Array2D<Cell,debug>::isInside(int x, int y) const{
165  return x>=0 && y>=0 && x<m_xsize && y<m_ysize;
166 }
167 
168 template <class Cell, const bool debug>
169 inline const Cell& Array2D<Cell,debug>::cell(int x, int y) const{
170  assert(isInside(x,y));
171  return m_cells[x][y];
172 }
173 
174 
175 template <class Cell, const bool debug>
176 inline Cell& Array2D<Cell,debug>::cell(int x, int y){
177  assert(isInside(x,y));
178  return m_cells[x][y];
179 }
180 
181 };
182 
183 #endif
184 
GMapping::Array2D::m_xsize
int m_xsize
Definition: array2d.h:39
GMapping::Array2D::resize
void resize(int xmin, int ymin, int xmax, int ymax)
Definition: array2d.h:140
GMapping::Array2D::cell
const Cell & cell(const IntPoint &p) const
Definition: array2d.h:28
GMapping::Array2D::cell
const Cell & cell(int x, int y) const
Definition: array2d.h:169
point.h
GMapping::Array2D::getYSize
int getYSize() const
Definition: array2d.h:35
GMapping::Array2D::~Array2D
~Array2D()
Definition: array2d.h:107
GMapping::Array2D::getPatchSize
int getPatchSize() const
Definition: array2d.h:32
GMapping
Definition: configfile.cpp:34
GMapping::Array2D::cells
Cell ** cells()
Definition: array2d.h:36
GMapping::Array2D::cellState
AccessibilityState cellState(const IntPoint &p) const
Definition: array2d.h:30
GMapping::Array2D::cellState
AccessibilityState cellState(int x, int y) const
Definition: array2d.h:25
GMapping::Array2D
Definition: array2d.h:12
GMapping::point::y
T y
Definition: point.h:16
GMapping::Array2D::isInside
bool isInside(int x, int y) const
Definition: array2d.h:164
GMapping::Array2D::Array2D
Array2D(int xsize=0, int ysize=0)
Definition: array2d.h:44
GMapping::point
Definition: point.h:13
GMapping::Array2D::operator=
Array2D & operator=(const Array2D &)
Definition: array2d.h:66
accessstate.h
GMapping::Array2D::getXSize
int getXSize() const
Definition: array2d.h:34
GMapping::Array2D::clear
void clear()
Definition: array2d.h:122
GMapping::AccessibilityState
AccessibilityState
Definition: accessstate.h:5
GMapping::Array2D::cell
Cell & cell(const IntPoint &p)
Definition: array2d.h:29
GMapping::Allocated
@ Allocated
Definition: accessstate.h:5
GMapping::Outside
@ Outside
Definition: accessstate.h:5
GMapping::Inside
@ Inside
Definition: accessstate.h:5
GMapping::Array2D::isInside
bool isInside(const IntPoint &p) const
Definition: array2d.h:27
GMapping::Array2D::getPatchMagnitude
int getPatchMagnitude() const
Definition: array2d.h:33
GMapping::Array2D::m_cells
Cell ** m_cells
Definition: array2d.h:37
GMapping::point::x
T x
Definition: point.h:16
GMapping::Array2D::m_ysize
int m_ysize
Definition: array2d.h:39
if
if(argc< 3)
Definition: gfs2stream.cpp:24


openslam_gmapping
Author(s): Cyrill Stachniss, Udo Frese, Giorgio Grisetti, Wolfram Burgard
autogenerated on Thu Oct 19 2023 02:25:51