28 #ifndef CDynamicGrid_H
29 #define CDynamicGrid_H
37 using namespace mrpt::system;
65 float resolution = 0.10f) :
67 m_x_min(),m_x_max(),m_y_min(),m_y_max(),
69 m_size_x(), m_size_y()
71 setSize(x_min,x_max,y_min,y_max,resolution);
90 m_x_min = resolution*
round(x_min/resolution);
91 m_y_min = resolution*
round(y_min/resolution);
92 m_x_max = resolution*
round(x_max/resolution);
93 m_y_max = resolution*
round(y_max/resolution);
96 m_resolution = resolution;
99 m_size_x =
round((m_x_max-m_x_min)/m_resolution);
100 m_size_y =
round((m_y_max-m_y_min)/m_resolution);
103 m_map.resize(m_size_x*m_size_y);
111 m_map.resize(m_size_x*m_size_y);
116 inline void fill(
const T& value ) {
std::fill(m_map.begin(),m_map.end(),value); }
126 const T& defaultValueNewCells,
127 float additionalMarginMeters = 2.0f )
137 unsigned int extra_x_izq,extra_y_arr,new_size_x=0,new_size_y=0;
138 typename std::vector<T> new_map;
142 if (new_x_min>=m_x_min &&
143 new_y_min>=m_y_min &&
144 new_x_max<=m_x_max &&
145 new_y_max<=m_y_max)
return;
147 if (new_x_min>m_x_min) new_x_min=m_x_min;
148 if (new_x_max<m_x_max) new_x_max=m_x_max;
149 if (new_y_min>m_y_min) new_y_min=m_y_min;
150 if (new_y_max<m_y_max) new_y_max=m_y_max;
153 if (additionalMarginMeters>0)
155 if (new_x_min<m_x_min) new_x_min= floor(new_x_min-additionalMarginMeters);
156 if (new_x_max>m_x_max) new_x_max= ceil(new_x_max+additionalMarginMeters);
157 if (new_y_min<m_y_min) new_y_min= floor(new_y_min-additionalMarginMeters);
158 if (new_y_max>m_y_max) new_y_max= ceil(new_y_max+additionalMarginMeters);
162 if (fabs(new_x_min/m_resolution -
round(new_x_min/m_resolution))>0.05f )
163 new_x_min = m_resolution*
round(new_x_min/m_resolution);
164 if (fabs(new_y_min/m_resolution -
round(new_y_min/m_resolution))>0.05f )
165 new_y_min = m_resolution*
round(new_y_min/m_resolution);
166 if (fabs(new_x_max/m_resolution -
round(new_x_max/m_resolution))>0.05f )
167 new_x_max = m_resolution*
round(new_x_max/m_resolution);
168 if (fabs(new_y_max/m_resolution -
round(new_y_max/m_resolution))>0.05f )
169 new_y_max = m_resolution*
round(new_y_max/m_resolution);
172 extra_x_izq =
round((m_x_min-new_x_min) / m_resolution);
173 extra_y_arr =
round((m_y_min-new_y_min) / m_resolution);
175 new_size_x =
round((new_x_max-new_x_min) / m_resolution);
176 new_size_y =
round((new_y_max-new_y_min) / m_resolution);
179 new_map.resize(new_size_x*new_size_y,defaultValueNewCells);
182 for (y=0;y<m_size_y;y++)
184 for (x=0,itSrc=(m_map.begin()+y*m_size_x),itDst=(new_map.begin()+extra_x_izq + (y+extra_y_arr)*new_size_x);
198 m_size_x = new_size_x;
199 m_size_y = new_size_y;
210 inline T* cellByPos(
float x,
float y )
215 if( cx<0 || cx>=static_cast<int>(m_size_x) )
return NULL;
216 if( cy<0 || cy>=static_cast<int>(m_size_y) )
return NULL;
218 return &m_map[ cx + cy*m_size_x ];
223 inline const T* cellByPos(
float x,
float y )
const
228 if( cx<0 || cx>=static_cast<int>(m_size_x) )
return NULL;
229 if( cy<0 || cy>=static_cast<int>(m_size_y) )
return NULL;
231 return &m_map[ cx + cy*m_size_x ];
236 inline T* cellByIndex(
unsigned int cx,
unsigned int cy )
238 if( cx>=m_size_x || cy>=m_size_y)
240 else return &m_map[ cx + cy*m_size_x ];
245 inline const T* cellByIndex(
unsigned int cx,
unsigned int cy )
const
247 if( cx>=m_size_x || cy>=m_size_y)
249 else return &m_map[ cx + cy*m_size_x ];
254 inline size_t getSizeX()
const {
return m_size_x; }
258 inline size_t getSizeY()
const {
return m_size_y; }
262 inline float getXMin()
const {
return m_x_min; }
266 inline float getXMax()
const {
return m_x_max; }
270 inline float getYMin()
const {
return m_y_min; }
274 inline float getYMax()
const {
return m_y_max; }
281 virtual float cell2float(
const T& c)
const
288 FILE *f=
os::fopen(fileName.c_str(),
"wt");
290 for (
unsigned int cy=0;cy<m_size_y;cy++)
292 for (
unsigned int cx=0;cx<m_size_x;cx++)
293 os::fprintf(f,
"%f ",cell2float(m_map[ cx + cy*m_size_x ]));
301 inline int x2idx(
float x)
const {
return static_cast<int>( (x-m_x_min)/m_resolution ); }
302 inline int y2idx(
float y)
const {
return static_cast<int>( (y-m_y_min)/m_resolution ); }
303 inline int xy2idx(
float x,
float y)
const {
return x2idx(x) + y2idx(y)*m_size_x; }
306 inline void idx2cxcy(
const int &idx,
int &cx,
int &cy )
const
314 inline float idx2x(
int cx)
const {
return m_x_min+(cx+0.5f)*m_resolution; }
315 inline float idx2y(
int cy)
const {
return m_y_min+(cy+0.5f)*m_resolution; }
319 inline int x2idx(
float x,
float x_min)
const {
return static_cast<int>((x-m_x_min)/m_resolution ); }
320 inline int y2idx(
float y,
float y_min)
const {
return static_cast<int>((y-m_y_min)/m_resolution ); }
329 void getAsMatrix(MAT &m)
const
331 m.setSize(m_size_y, m_size_x);
332 if (m_map.empty())
return;
333 const T* c = &m_map[0];
334 for (
size_t cy=0;cy<m_size_y;cy++)
335 for (
size_t cx=0;cx<m_size_x;cx++)
336 m.set_unsafe(cy,cx, *c++);