orca-robotics INTRODUCTION Overview Download and Install Documentation REPOSITORY Interfaces Drivers Libraries Utilities Software Map DEVELOPER Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
typemap.h00001 /* 00002 * Orca-Robotics Project: Components for robotics 00003 * http://orca-robotics.sf.net/ 00004 * Copyright (c) 2004-2008 Alex Brooks, Alexei Makarenko, Tobias Kaupp 00005 * 00006 * This copy of Orca is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 #ifndef ORCA2_TYPEMAP_H 00011 #define ORCA2_TYPEMAP_H 00012 00013 #include <vector> 00014 #include "cell2d.h" 00015 00016 namespace hydropathplan 00017 { 00020 template<typename Type> 00021 class TypeMap 00022 { 00023 00024 public: 00025 TypeMap( unsigned int szx=0, unsigned int sz=0 ); 00026 ~TypeMap() {}; 00027 00029 unsigned int sizeX() const { return sizeX_; }; 00030 00032 unsigned int sizeY() const { return sizeY_; }; 00033 00035 void resize( unsigned int szx, unsigned int szy ); 00036 00038 void fill( Type val ); 00039 00041 bool isInGrid( int x, int y ) const { return (x >= 0 && x < (int)sizeX_ && y >= 0 && y < (int)sizeY_ ); }; 00042 00044 bool isInGrid( Cell2D & cell ) const { return isInGrid( cell.x(), cell.y() ); }; 00045 00047 bool tryElement( int x, int y, Type & val ) const; 00048 00050 bool tryElement( Cell2D c, Type & val ) const { return tryElement(c.x(),c.y(),val); }; 00051 00053 void setElement( int x, int y, Type val ); 00054 00056 void setElement( Cell2D c, Type val ) { setElement(c.x(),c.y(),val); }; 00057 00058 00059 private: 00060 unsigned int sizeX_; 00061 unsigned int sizeY_; 00062 std::vector< std::vector< Type > > data_; 00063 00064 }; 00065 00066 // ====================================================================== 00067 00068 // Special kinds of 'TypeMap's 00069 00070 typedef TypeMap<float> FloatMap; 00071 typedef TypeMap<int> IntMap; 00072 00073 // =========== ELEMENT ACCESS (without specializing templated TypeMap) ========= 00074 00076 float element( const FloatMap &floatMap, int x, int y ); 00077 00079 inline float element( const FloatMap &floatMap, Cell2D c ) 00080 { 00081 return element( floatMap, c.x(), c.y() ); 00082 } 00083 00084 // ====================================================================== 00085 00086 template<typename Type> 00087 TypeMap<Type>::TypeMap( unsigned int szx, unsigned int szy ) 00088 : sizeX_( 0 ), sizeY_( 0 ) 00089 { 00090 resize( szx, szy ); 00091 } 00092 00093 template<typename Type> 00094 void 00095 TypeMap<Type>::resize( unsigned int szx, unsigned int szy ) 00096 { 00097 data_.resize(szx); 00098 00099 typedef typename std::vector< std::vector<Type> >::iterator dataIterator; 00100 for ( dataIterator it=data_.begin(); it!=data_.end(); it++ ) 00101 { 00102 it->resize(szy); 00103 } 00104 00105 sizeX_ = szx; 00106 sizeY_ = szy; 00107 } 00108 00109 template<typename Type> 00110 void 00111 TypeMap<Type>::fill(Type val) 00112 { 00113 typedef typename std::vector< std::vector<Type> >::iterator dataIterator; 00114 for( dataIterator it=data_.begin(); it!=data_.end(); it++ ) 00115 { 00116 std::fill( it->begin(), it->end(), val ); 00117 } 00118 } 00119 00120 template<typename Type> 00121 bool 00122 TypeMap<Type>::tryElement( int x, int y, Type & val ) const 00123 { 00124 //will return -1.0 if outside the grid 00125 if (!isInGrid( x, y )) return false; 00126 val = data_[x][y]; 00127 return true; 00128 } 00129 00130 00131 template<typename Type> 00132 void 00133 TypeMap<Type>::setElement( int x, int y, Type val ) 00134 { 00135 //will not set any value if outside the grid 00136 if( x >= (int)sizeX_ || y >= (int)sizeY_ || x < 0 || y < 0 ) return; 00137 data_[x][y] = val; 00138 } 00139 00140 00141 00142 } 00143 #endif 00144 |
Webmaster: Tobias Kaupp (tobasco at users.sourceforge.net)