Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 00020 namespace wiselib 00021 { 00022 #ifndef __POSITION3D___ 00023 #define __POSITION3D___ 00024 template< typename Os_P, 00025 typename Radio_P, 00026 typename CoordinatesNumber_P, 00027 typename Debug_P> 00028 class Position3DType 00029 { 00030 public: 00031 typedef Os_P Os; 00032 typedef Radio_P Radio; 00033 typedef CoordinatesNumber_P CoordinatesNumber; 00034 typedef Debug_P Debug; 00035 typedef typename Radio::block_data_t block_data_t; 00036 typedef typename Radio::size_t size_t; 00037 typedef Position3DType<Os, Radio, CoordinatesNumber, Debug> self_type; 00038 Position3DType() 00039 {} 00040 Position3DType( CoordinatesNumber _x, CoordinatesNumber _y, CoordinatesNumber _z) 00041 { 00042 x = _x; 00043 y = _y; 00044 z = _z; 00045 } 00046 Position3DType( const self_type& _p ) 00047 { 00048 *this = _p; 00049 } 00050 inline block_data_t* set_buffer_from( block_data_t* buff, size_t offset = 0 ) 00051 { 00052 uint8_t X_POS = 0; 00053 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00054 uint8_t Z_POS = Y_POS + sizeof( CoordinatesNumber ); 00055 write<Os, block_data_t, CoordinatesNumber>( buff + X_POS + offset, x ); 00056 write<Os, block_data_t, CoordinatesNumber>( buff + Y_POS + offset, y ); 00057 write<Os, block_data_t, CoordinatesNumber>( buff + Z_POS + offset, z ); 00058 return buff; 00059 } 00060 inline void get_from_buffer( block_data_t* buff, size_t offset = 0 ) 00061 { 00062 uint8_t X_POS = 0; 00063 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00064 uint8_t Z_POS = Y_POS + sizeof( CoordinatesNumber ); 00065 x = read<Os, block_data_t, CoordinatesNumber>( buff + X_POS + offset ); 00066 y = read<Os, block_data_t, CoordinatesNumber>( buff + Y_POS + offset ); 00067 z = read<Os, block_data_t, CoordinatesNumber>( buff + Z_POS + offset ); 00068 } 00069 inline size_t get_buffer_size() 00070 { 00071 uint8_t X_POS = 0; 00072 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00073 uint8_t Z_POS = Y_POS + sizeof( CoordinatesNumber ); 00074 return Z_POS + sizeof( CoordinatesNumber ); 00075 } 00076 inline self_type& operator=( const self_type& _p ) 00077 { 00078 x = _p.x; 00079 y = _p.y; 00080 z = _p.z; 00081 return *this; 00082 } 00083 inline void set_x( const CoordinatesNumber& _x) 00084 { 00085 x = _x; 00086 } 00087 inline void set_y( const CoordinatesNumber& _y) 00088 { 00089 y = _y; 00090 } 00091 inline void set_z( const CoordinatesNumber& _z) 00092 { 00093 z = _z; 00094 } 00095 inline void set_all(const CoordinatesNumber& _x, const CoordinatesNumber& _y, const CoordinatesNumber& _z) 00096 { 00097 x = _x; 00098 y = _y; 00099 z = _z; 00100 } 00101 inline CoordinatesNumber get_x() 00102 { 00103 return x; 00104 } 00105 inline CoordinatesNumber get_y() 00106 { 00107 return y; 00108 } 00109 inline CoordinatesNumber get_z() 00110 { 00111 return z; 00112 } 00113 inline CoordinatesNumber distsq( const self_type& _p ) 00114 { 00115 const register CoordinatesNumber inc_x = x - _p.x; 00116 const register CoordinatesNumber inc_y = y - _p.y; 00117 const register CoordinatesNumber inc_z = z - _p.z; 00118 return inc_x*inc_x + inc_y*inc_y + inc_z*inc_z; 00119 } 00120 inline void print( Debug& debug ) 00121 { 00122 debug.debug("Position (size %i) : ( %i, %i, %i )", get_buffer_size(), x, y, z); 00123 } 00124 private: 00125 CoordinatesNumber x, y, z; 00126 }; 00127 #endif 00128 00129 #ifndef __POSITION2D___ 00130 #define __POSITION2D___ 00131 template< typename Os_P, 00132 typename Radio_P, 00133 typename CoordinatesNumber_P, 00134 typename Debug_P> 00135 class Position2DType 00136 { 00137 public: 00138 typedef Os_P Os; 00139 typedef Radio_P Radio; 00140 typedef CoordinatesNumber_P CoordinatesNumber; 00141 typedef Debug_P Debug; 00142 typedef typename Radio_P::block_data_t block_data_t; 00143 typedef typename Radio_P::size_t size_t; 00144 typedef Position2DType<Os, Radio, CoordinatesNumber, Debug> self_type; 00145 Position2DType() 00146 {} 00147 Position2DType( const CoordinatesNumber& _x, const CoordinatesNumber _y, const CoordinatesNumber _z = 1) 00148 { 00149 x = _x; 00150 y = _y; 00151 } 00152 Position2DType( const self_type& _p ) 00153 { 00154 *this = _p; 00155 } 00156 inline block_data_t* set_buffer_from( block_data_t* buff, size_t offset = 0 ) 00157 { 00158 uint8_t X_POS = 0; 00159 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00160 write<Os, block_data_t, CoordinatesNumber>( buff + X_POS + offset, x ); 00161 write<Os, block_data_t, CoordinatesNumber>( buff + Y_POS + offset, y ); 00162 return buff; 00163 } 00164 inline void get_from_buffer( block_data_t* buff, size_t offset = 0 ) 00165 { 00166 uint8_t X_POS = 0; 00167 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00168 x = read<Os, block_data_t, CoordinatesNumber>( buff + X_POS + offset ); 00169 y = read<Os, block_data_t, CoordinatesNumber>( buff + Y_POS + offset ); 00170 } 00171 inline size_t get_buffer_size() 00172 { 00173 uint8_t X_POS = 0; 00174 uint8_t Y_POS = X_POS + sizeof( CoordinatesNumber ); 00175 return Y_POS + sizeof( CoordinatesNumber ); 00176 } 00177 inline self_type& operator=( const self_type& _p ) 00178 { 00179 x = _p.x; 00180 y = _p.y; 00181 return *this; 00182 } 00183 inline void set_x( const CoordinatesNumber& _x) 00184 { 00185 x = _x; 00186 } 00187 inline void set_y( const CoordinatesNumber& _y) 00188 { 00189 y = _y; 00190 } 00191 inline void set_z( const CoordinatesNumber& _z = 1 ) 00192 { 00193 } 00194 inline void set_all( const CoordinatesNumber& _x, const CoordinatesNumber& _y, const CoordinatesNumber& _z = 1 ) 00195 { 00196 x = _x; 00197 y = _y; 00198 } 00199 inline CoordinatesNumber get_x() 00200 { 00201 return x; 00202 } 00203 inline CoordinatesNumber get_y() 00204 { 00205 return y; 00206 } 00207 inline CoordinatesNumber get_z() 00208 { 00209 return 1; 00210 } 00211 inline CoordinatesNumber distsq( const self_type& _p ) 00212 { 00213 const register CoordinatesNumber inc_x = x - _p.x; 00214 const register CoordinatesNumber inc_y = y - _p.y; 00215 return inc_x*inc_x + inc_y*inc_y; 00216 } 00217 inline void print( Debug& debug ) 00218 { 00219 debug.debug("Position (size %i) : ( %i, %i )", get_buffer_size(), x, y ); 00220 } 00221 private: 00222 CoordinatesNumber x, y; 00223 }; 00224 #endif 00225 }