//* 2D matrix template with negative and positive indexes and scaling *

#ifndef _NAV_MAP_BASET #define _NAV_MAP_BASET #include <vector> template <typename T> class NAV_Map_Base { public: NAV_Map_Base (double resol=100, double resizeadd=1); NAV_Map_Base (double resol, double resizeadd, const T& ini); public: double getMinX () const { return(-res*minx); } double getMaxX () const { return(res*maxx); } double getMinY () const { return(-res*miny); } double getMaxY () const { return(res*maxy); } double getResolution () const { return(res); } // reduce and resize (expand) void clear (); // clears the matrix to 0 by 0 size // resize as needed to increase the range to the specified point (corner point just outside the actual range) void resize_as_needed(double xmin, double ymin, double xmax, double ymax) { resize_as_needed(xmin,ymin); resize_as_needed(xmax,ymax); } void resize_as_needed(double x, double y) { resizeRaw(static_cast<int>(x/res), static_cast<int>(y/res)); } // access point without resizing the array, watch out for correct range! const T& operator() (double x, double y) const { return(refRawValue(static_cast<int>(x/res), static_cast<int>(y/res))); } T& operator() (double x, double y) { return(refRawValue(static_cast<int>(x/res), static_cast<int>(y/res))); } protected: void resizeRaw (int ix, int iy); // resize from (0,0) to include (ix,iy) (2 out of 4 quadrants) const T& refRawValue (int ix, int iy) const; // reference access to the point in one of the four arrays T& refRawValue (int ix, int iy); // reference access to the point in one of the four arrays T getRawValue (int ix, int iy) const; // return value of the point in one of the four arrays protected: std::vector<std::vector<T> > PP, NP, NN, PN; // for quadrants of the plains double res; // one map grid corresponds to that much of an axis int resizeravval; // how much to exapnd if expansion needed at all T initialval; // initial value to place in new elements int minx, maxx, miny, maxy; // absolute values of the current map limits (sizes) }; template <typename T> NAV_Map_Base<T>::NAV_Map_Base(double resol, double resizeadd, const T& ini) : PP(), NP(), NN(), PN(), res(resol), resizeravval( max(static_cast<int>(resizeadd/resol), 1) ), initialval(ini), minx(resizeravval), maxx(resizeravval), miny(resizeravval), maxy(resizeravval) { clear(); } template <typename T> NAV_Map_Base<T>::NAV_Map_Base(double resol, double resizeadd) : PP(), NP(), NN(), PN(), res(resol), resizeravval( max(static_cast<int>(resizeadd/resol), 1) ), initialval(), minx(resizeravval), maxx(resizeravval), miny(resizeravval), maxy(resizeravval) { clear(); } template <typename T> void NAV_Map_Base<T>::clear() { minx=resizeravval; maxx=resizeravval; miny=resizeravval; maxy=resizeravval; PP.clear(); PP.resize(maxx); for (int i=0; i<maxx; i++) PP[i].resize(maxy, initialval); NP.clear(); NP.resize(minx); for (int i=0; i<minx; i++) NP[i].resize(maxy, initialval); NN.clear(); NN.resize(minx); for (int i=0; i<minx; i++) NN[i].resize(miny, initialval); PN.clear(); PN.resize(maxx); for (int i=0; i<maxx; i++) PN[i].resize(miny, initialval); } template <typename T> void NAV_Map_Base<T>::resizeRaw(int ix, int iy) { bool needtoresize=false; if (ix>=0) { if (ix>=maxx) { maxx=ix+resizeravval; needtoresize=true; } } else { if (-ix>=minx) { minx=-ix+resizeravval; needtoresize=true; } } if (iy>=0) { if (iy>=maxy) { maxy=iy+resizeravval; needtoresize=true; } } else { if (-iy>=miny) { miny=-iy+resizeravval; needtoresize=true; } } if (needtoresize) { PP.resize(maxx); for (int i=0; i<maxx; i++) PP[i].resize(maxy, initialval); NP.resize(minx); for (int i=0; i<minx; i++) NP[i].resize(maxy, initialval); NN.resize(minx); for (int i=0; i<minx; i++) NN[i].resize(miny, initialval); PN.resize(maxx); for (int i=0; i<maxx; i++) PN[i].resize(miny, initialval); } } template <typename T> const T& NAV_Map_Base<T>::refRawValue(int ix, int iy) const { if (ix>=0) { if (iy>=0) { return(PP[ix][iy]); } else { return(PN[ix][-iy]); } } else { if (iy>=0) { return(NP[-ix][iy]); } else { return(NN[-ix][-iy]); } } } template <typename T> T& NAV_Map_Base<T>::refRawValue(int ix, int iy) { if (ix>=0) { if (iy>=0) { return(PP[ix][iy]); } else { return(PN[ix][-iy]); } } else { if (iy>=0) { return(NP[-ix][iy]); } else { return(NN[-ix][-iy]); } } } template <typename T> T NAV_Map_Base<T>::getRawValue(int ix, int iy) const { if (ix>=0) { if (iy>=0) { return(PP[ix][iy]); } else { return(PN[ix][-iy]); } } else { if (iy>=0) { return(NP[-ix][iy]); } else { return(NN[-ix][-iy]); } } } #endif