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 #ifndef __INTERNAL_INTERFACE_POSITION___ 00020 #define __INTERNAL_INTERFACE_POSITION___ 00021 00022 #include <math.h> // NOTE: required for sqrt 00023 #include "util/serialization/serialization.h" 00024 namespace wiselib 00025 { 00026 00027 template<typename Float_P,typename block_data_P, typename OsModel_P> 00028 struct PositionType 00029 { 00030 typedef Float_P Float; 00031 typedef block_data_P block_data; 00032 typedef OsModel_P OsModel; 00033 00034 Float x, y, z; 00035 00036 PositionType() 00037 {} 00038 00039 PositionType( const Float& a, const Float& b, const Float& c ) 00040 : x ( a ), 00041 y ( b ), 00042 z ( c ) 00043 {} 00044 00045 PositionType( block_data* buff ) 00046 { 00047 get_Position3D_from_buffer( buff ); 00048 } 00049 00050 enum Position_Positions 00051 { 00052 X_POS = 0, 00053 Y_POS = X_POS + sizeof(Float), 00054 Z_POS = Y_POS + sizeof(Float) 00055 }; 00056 00057 inline block_data* set_buffer_from_Position2D( block_data* buff, size_t offset = 0) 00058 { 00059 write<OsModel, block_data, Float>( buff + X_POS + offset, x ); 00060 write<OsModel, block_data, Float>( buff + Y_POS + offset, y ); 00061 return buff; 00062 } 00063 00064 inline block_data* set_buffer_from_Position3D( block_data* buff, size_t offset = 0 ) 00065 { 00066 write<OsModel, block_data, Float>( buff + X_POS + offset, x ); 00067 write<OsModel, block_data, Float>( buff + Y_POS + offset, y ); 00068 write<OsModel, block_data, Float>( buff + Z_POS + offset, z ); 00069 return buff; 00070 } 00071 00072 inline void get_Position2D_from_buffer( block_data* buff, size_t offset = 0 ) 00073 { 00074 x = read<OsModel, block_data, Float>( buff + X_POS + offset ); 00075 y = read<OsModel, block_data, Float>( buff + Y_POS + offset ); 00076 } 00077 00078 inline void get_Position3D_from_buffer( block_data* buff, size_t offset = 0 ) 00079 { 00080 x = read<OsModel, block_data, Float>( buff + X_POS + offset ); 00081 y = read<OsModel, block_data, Float>( buff + Y_POS + offset ); 00082 z = read<OsModel, block_data, Float>( buff + Z_POS + offset ); 00083 } 00084 00085 inline size_t get_buffer_size_2D(){return 2*sizeof( Float );} 00086 00087 inline size_t get_buffer_size_3D(){return 3*sizeof( Float );} 00088 00089 }; 00090 00091 template<typename Float, typename block_data, typename Os> 00092 Float dist(const PositionType<Float, block_data, Os> p1, const PositionType<Float, block_data, Os> p2) 00093 { 00094 const register Float inc_x = p1.x - p2.x; 00095 const register Float inc_y = p1.y - p2.y; 00096 const register Float inc_z = p1.z - p2.z; 00097 return sqrt( inc_x*inc_x + inc_y*inc_y + inc_z*inc_z ); 00098 } 00099 00100 // NOTE: square of euclidean distance is not a distance! 00101 template<typename Float, typename block_data, typename Os> 00102 Float distsq(const PositionType<Float, block_data, Os> p1, const PositionType<Float, block_data, Os> p2) 00103 { 00104 const register Float inc_x = p1.x - p2.x; 00105 const register Float inc_y = p1.y - p2.y; 00106 const register Float inc_z = p1.z - p2.z; 00107 return inc_x*inc_x + inc_y*inc_y + inc_z*inc_z; 00108 } 00109 00110 } 00111 #endif