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


openslam_gmapping
Author(s): Giorgio Grisetti, Cyrill Stachniss, Wolfram Burgard
autogenerated on Mon Jun 10 2019 14:04:22