grid_map_fixed.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_MAPS_GRID_MAP_FIXED_HPP_
00013 #define ECL_MAPS_GRID_MAP_FIXED_HPP_
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include "grid_map_commons.hpp"
00020 
00021 /*****************************************************************************
00022 ** Namespaces
00023 *****************************************************************************/
00024 
00025 namespace ecl {
00026 
00027 /*****************************************************************************
00028 ** Interface [GridMap]
00029 *****************************************************************************/
00049 template <typename CellType, int Rows = DynamicGridMapStorage, int Cols = Rows>
00050 class GridMap {
00051 public:
00052         /*********************
00053         ** Typedefs
00054         **********************/
00055         typedef CellType value_type; 
00056         typedef CellType* iterator; 
00057         typedef const CellType* const_iterator; 
00058         typedef CellType& reference;  
00059         typedef const CellType& const_reference;  
00060         typedef int size_type; 
00062         /*********************
00063         ** Initialisation
00064         **********************/
00065         GridMap(const double &origin_x = 0.0, const double &origin_y = 0.0, const double &scale = 1.0);
00066         void init(const CellType & initialValue= CellType() );
00067 
00068         /*********************
00069         ** 2D Accessors
00070         **********************/
00071         CellType& operator()(const int& x, const int& y) ecl_assert_throw_decl(StandardException);
00072         CellType& operator()(const double& x, const double& y) ecl_assert_throw_decl(StandardException);
00073 
00074         /*********************
00075         ** 1D Iterators
00076         **********************/
00077         size_type size() const { return Rows*Cols; } 
00078         iterator begin() { return cells; } 
00079         const_iterator begin() const { return cells; } 
00080         iterator end() { return cells+Rows*Cols; } 
00081         const_iterator end() const { return cells+Rows*Cols; } 
00082         reference front() { return cells[0]; } 
00083         const_reference front() const { return cells[0]; } 
00084         reference back() { return cells[Rows*Cols-1]; } 
00085         const_reference back() const { return cells[Rows*Cols-1]; } 
00087         /*********************
00088         ** Conversions
00089         **********************/
00090         linear_algebra::Vector2i cellCoordinates(const double &x, const double &y ) const ecl_assert_throw_decl(StandardException);
00091         linear_algebra::Vector2d worldCoordinates(const int &x, const int &y ) const ecl_assert_throw_decl(StandardException);
00092 
00093         /*********************
00094         ** Utility
00095         **********************/
00096         double cellWidth() const { return scale_multiplier; } 
00097         size_type rows() const { return Rows; } 
00098         size_type cols() const { return Cols; } 
00100 private:
00101         double scale_multiplier;
00102         linear_algebra::Vector2d origin;
00103         linear_algebra::Vector2d limits;
00104         CellType cells[Rows*Cols];
00105 };
00106 
00107 /*****************************************************************************
00108 ** Implementation [GridMap][Constructors]
00109 *****************************************************************************/
00129 template <typename CellType, int Rows, int Cols>
00130 GridMap<CellType,Rows,Cols>::GridMap(const double &origin_x, const double &origin_y,  const double &scale) :
00131         scale_multiplier(scale)
00132 {
00133         ecl_compile_time_assert( (Rows >= 0) && (Cols >= 0) );
00134 
00135         origin << origin_x, origin_y;
00136         limits << origin[0] + scale_multiplier*Cols, origin[1] + scale_multiplier*Rows;
00137 }
00138 
00144 template <typename CellType, int Rows, int Cols>
00145 void GridMap<CellType,Rows,Cols>::init(const CellType & initialValue ) {
00146         for ( int i = 0; i < Rows*Cols; ++i ) {
00147                 cells[i] = initialValue;//CellType();
00148         }
00149 }
00150 
00151 /*****************************************************************************
00152 ** Implementation [GridMap][Access]
00153 *****************************************************************************/
00154 
00155 template <typename CellType, int Rows, int Cols>
00156 CellType& GridMap<CellType,Rows,Cols>::operator() (const int& x, const int& y) ecl_assert_throw_decl(StandardException) {
00157         ecl_assert_throw( ( ( x >= 0 ) && (x <= Cols) ), StandardException(LOC,OutOfRangeError) );
00158         ecl_assert_throw( ( ( y >= 0 ) && (y <= Rows) ), StandardException(LOC,OutOfRangeError) );
00159 
00160         return cells[Cols*y+x];
00161 }
00162 
00163 template <typename CellType, int Rows, int Cols>
00164 CellType& GridMap<CellType,Rows,Cols>::operator() (const double& x, const double& y) ecl_assert_throw_decl(StandardException) {
00165 
00166         ecl_assert_throw( ( ( x >= origin[0] ) && (x <= limits[0]) ), StandardException(LOC,OutOfRangeError) );
00167         ecl_assert_throw( ( ( y >= origin[1] ) && (y <= limits[1]) ), StandardException(LOC,OutOfRangeError) );
00168 
00169         int cell_x = static_cast<int>( ( x - origin[0] )/scale_multiplier );
00170         int cell_y = static_cast<int>( ( y - origin[1] )/scale_multiplier );
00171         return cells[Cols*cell_y+cell_x];
00172 }
00173 
00174 /*****************************************************************************
00175 ** Implementation [GridMap][Conversions]
00176 *****************************************************************************/
00177 
00178 template <typename CellType, int Rows, int Cols>
00179 linear_algebra::Vector2i GridMap<CellType,Rows,Cols>::cellCoordinates (const double& x, const double& y) const ecl_assert_throw_decl(StandardException) {
00180 
00181         ecl_assert_throw( ( ( x >= origin[0] ) && (x <= limits[0]) ), StandardException(LOC,OutOfRangeError) );
00182         ecl_assert_throw( ( ( y >= origin[1] ) && (y <= limits[1]) ), StandardException(LOC,OutOfRangeError) );
00183 
00184         linear_algebra::Vector2i cell_coordinates;
00185         cell_coordinates << static_cast<int>( ( x - origin[0] )/scale_multiplier ),
00186                                                 static_cast<int>( ( y - origin[1] )/scale_multiplier );
00187         return cell_coordinates;
00188 }
00189 
00190 template <typename CellType, int Rows, int Cols>
00191 ecl::linear_algebra::Vector2d GridMap<CellType,Rows,Cols>::worldCoordinates (const int& x, const int& y) const ecl_assert_throw_decl(StandardException) {
00192 
00193         ecl_assert_throw( ( ( x >= 0 ) && (x <= Cols) ), StandardException(LOC,OutOfRangeError) );
00194         ecl_assert_throw( ( ( y >= 0 ) && (y <= Rows) ), StandardException(LOC,OutOfRangeError) );
00195 
00196         linear_algebra::Vector2d world_coordinates;
00197         world_coordinates << origin[0] + scale_multiplier*static_cast<double>(x),
00198                                                  origin[1] + scale_multiplier*static_cast<double>(y);
00199         return world_coordinates;
00200 }
00201 
00202 
00203 } // namespace ecl
00204 
00205 #endif /* ECL_MAPS_GRID_MAP_FIXED_HPP_ */
00206 
00207 


ecl_maps
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:12:25