Wiselib
wiselib.testing/algorithms/localization/distance_based/neighborhood/localization_neighbor_info.h
Go to the documentation of this file.
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 __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_NEIGHBORHOOD_NEIGHBOR_INFO_H
00020 #define __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_NEIGHBORHOOD_NEIGHBOR_INFO_H
00021 
00022 #include "algorithms/localization/distance_based/util/localization_defutils.h"
00023 #include <limits.h>
00024 
00025 
00026 namespace wiselib
00027 {
00028 
00030 
00051    template<typename OsModel_P,
00052             typename node_id_t_P,
00053             typename NodeSet_P,
00054             typename DistanceMap_P,
00055             typename Arithmatic_P
00056             >
00057    class LocalizationNeighborInfo
00058    {
00059 
00060    public:
00061       typedef OsModel_P OsModel;
00062       typedef node_id_t_P node_id_t;
00063 
00064       typedef NodeSet_P NodeSet;
00065       typedef DistanceMap_P DistanceMap;
00066 
00067       typedef Arithmatic_P Arithmatic;
00068 
00069       typedef typename DistanceMap::iterator NeighborIterator;
00070 
00073 
00078       LocalizationNeighborInfo( node_id_t, bool );
00079       LocalizationNeighborInfo(  );
00081       ~LocalizationNeighborInfo();
00083 
00084 
00085       LocalizationNeighborInfo( const LocalizationNeighborInfo& rhs )
00086       {
00087          node_ = rhs.node_;
00088          pos_ = rhs.pos_;
00089          hops_ = rhs.hops_;
00090          distance_ = rhs.distance_;
00091          confidence_ = rhs.confidence_;
00092 
00093          is_anchor_ = rhs.is_anchor_;
00094          is_twin_ = rhs.is_twin_;
00095 
00096          has_pos_ = rhs.has_pos_;
00097 
00098          reference_points_ = rhs.reference_points_;
00099          DistanceMap tmp = rhs.neighbors_;
00100          neighbors_ = tmp;
00101       }
00102 
00103       LocalizationNeighborInfo& operator=(const LocalizationNeighborInfo &rhs)
00104       {
00105          node_ = rhs.node_;
00106          pos_ = rhs.pos_;
00107          hops_ = rhs.hops_;
00108          distance_ = rhs.distance_;
00109          confidence_ = rhs.confidence_;
00110 
00111          is_anchor_ = rhs.is_anchor_;
00112          is_twin_ = rhs.is_twin_;
00113 
00114          has_pos_ = rhs.has_pos_;
00115 
00116          reference_points_ = rhs.reference_points_;
00117          DistanceMap tmp = rhs.neighbors_;
00118          neighbors_ = tmp;
00119 
00120          return *this;
00121       }
00122 
00123 
00126 
00133       void update_neighbor( node_id_t, Arithmatic );
00140       void update_neighbors( DistanceMap& );
00142 
00143 
00146 
00151       void add_ref_node( node_id_t );
00156       void clear_ref_nodes( void );
00160       const NodeSet& ref_nodes( void )
00161          const;
00165       NodeSet& ref_nodes_w( void );
00167 
00168 
00171 
00175       void set_pos( const Vec<Arithmatic>& );
00178       void clear_pos();
00183       void set_hops( int );
00188       void set_distance( Arithmatic );
00193       void set_confidence( Arithmatic );
00198       void set_twin( bool );
00203       void set_anchor( bool );
00205 
00206       void set_node( node_id_t );
00207 
00208 
00211 
00216       void convert_hops( Arithmatic );
00218 
00219 
00222 
00224       node_id_t node( void );
00227       Vec<Arithmatic>& pos( void );
00233       int hops( void );
00236       Arithmatic distance( void );
00239       Arithmatic confidence( void );
00241 
00242 
00245 
00247       bool is_anchor( void );
00252       bool is_valid( void );
00255       bool is_twin( void );
00258       bool has_pos( void );
00262       bool has_distance( void );
00268       bool is_confident( void );
00270 
00271 
00274 
00276       DistanceMap& neighbors( void );
00281       bool has_neighbor( node_id_t );
00286       bool has_neighbor_distance( node_id_t );
00293       Arithmatic neighbor_distance( node_id_t );
00295 
00296 
00299 
00301       NeighborIterator begin_neighbors( void )
00302       { return neighbors_.begin(); }
00305       NeighborIterator end_neighbors( void )
00306       { return neighbors_.end(); }
00308 
00309    private:
00310 
00311       node_id_t node_;
00312       Vec<Arithmatic> pos_;
00313       int hops_;
00314       Arithmatic distance_;
00315       Arithmatic confidence_;
00316 
00317       bool is_anchor_;
00318       bool is_twin_;
00319 
00320       bool has_pos_;
00321 
00322       NodeSet reference_points_;
00323       DistanceMap neighbors_;
00324    };
00325    // ----------------------------------------------------------------------
00326    // ----------------------------------------------------------------------
00327    // ----------------------------------------------------------------------
00328    template<typename OsModel_P,
00329             typename node_id_t_P,
00330             typename NodeSet_P,
00331             typename DistanceMap_P,
00332             typename Arithmatic_P>
00333    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00334    LocalizationNeighborInfo( node_id_t node, bool is_anchor )
00335       : node_        ( node ),
00336          hops_       ( UNKNOWN_HOP_CNT ),
00337          distance_   ( UNKNOWN_DISTANCE ),
00338          confidence_ ( 0 ),
00339          is_anchor_  ( is_anchor ),
00340          is_twin_    ( false ),
00341          has_pos_    ( false )
00342    {}
00343    // ----------------------------------------------------------------------
00344    template<typename OsModel_P,
00345             typename node_id_t_P,
00346             typename NodeSet_P,
00347             typename DistanceMap_P,
00348             typename Arithmatic_P>
00349    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00350    LocalizationNeighborInfo(  )
00351       : node_        ( 0 ),
00352          hops_       ( UNKNOWN_HOP_CNT ),
00353          distance_   ( UNKNOWN_DISTANCE ),
00354          confidence_ ( 0 ),
00355          is_anchor_  ( false ),
00356          is_twin_    ( false ),
00357          has_pos_    ( false )
00358    {}
00359    // ----------------------------------------------------------------------
00360    template<typename OsModel_P,
00361             typename node_id_t_P,
00362             typename NodeSet_P,
00363             typename DistanceMap_P,
00364             typename Arithmatic_P>
00365    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00366    ~LocalizationNeighborInfo()
00367    {}
00368    // ----------------------------------------------------------------------
00369    template<typename OsModel_P,
00370             typename node_id_t_P,
00371             typename NodeSet_P,
00372             typename DistanceMap_P,
00373             typename Arithmatic_P>
00374    void
00375    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00376    update_neighbor( node_id_t node, Arithmatic distance )
00377    {
00378       neighbors_[node] = distance;
00379    }
00380    // ----------------------------------------------------------------------
00381    template<typename OsModel_P,
00382             typename node_id_t_P,
00383             typename NodeSet_P,
00384             typename DistanceMap_P,
00385             typename Arithmatic_P>
00386    void
00387    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00388    update_neighbors( DistanceMap& dm )
00389    {
00390       for ( NeighborIterator
00391                it = dm.begin();
00392                it != dm.end();
00393                ++it )
00394          neighbors_[it->first] = it->second;
00395    }
00396    // ----------------------------------------------------------------------
00397    template<typename OsModel_P,
00398             typename node_id_t_P,
00399             typename NodeSet_P,
00400             typename DistanceMap_P,
00401             typename Arithmatic_P>
00402    void
00403    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00404    add_ref_node( node_id_t node )
00405    {
00406       reference_points_.insert( node );
00407    }
00408    // ----------------------------------------------------------------------
00409    template<typename OsModel_P,
00410             typename node_id_t_P,
00411             typename NodeSet_P,
00412             typename DistanceMap_P,
00413             typename Arithmatic_P>
00414    void
00415    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00416    clear_ref_nodes( void )
00417    {
00418       reference_points_.clear();
00419    }
00420    // ----------------------------------------------------------------------
00421    template<typename OsModel_P,
00422             typename node_id_t_P,
00423             typename NodeSet_P,
00424             typename DistanceMap_P,
00425             typename Arithmatic_P>
00426    const NodeSet_P&
00427    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00428    ref_nodes( void )
00429       const
00430    {
00431       return reference_points_;
00432    }
00433    // ----------------------------------------------------------------------
00434    template<typename OsModel_P,
00435             typename node_id_t_P,
00436             typename NodeSet_P,
00437             typename DistanceMap_P,
00438             typename Arithmatic_P>
00439    NodeSet_P&
00440    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00441    ref_nodes_w( void )
00442    {
00443       return reference_points_;
00444    }
00445    // ----------------------------------------------------------------------
00446    template<typename OsModel_P,
00447             typename node_id_t_P,
00448             typename NodeSet_P,
00449             typename DistanceMap_P,
00450             typename Arithmatic_P>
00451    void
00452    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00453    set_pos( const wiselib::Vec<Arithmatic_P>& pos )
00454    {
00455       pos_ = pos;
00456       has_pos_ = true;
00457    }
00458    // ----------------------------------------------------------------------
00459    template<typename OsModel_P,
00460             typename node_id_t_P,
00461             typename NodeSet_P,
00462             typename DistanceMap_P,
00463             typename Arithmatic_P>
00464    void
00465    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00466    clear_pos()
00467    {
00468       pos_ = Vec<Arithmatic_P>(0,0);
00469       has_pos_ = false;
00470    }
00471    // ----------------------------------------------------------------------
00472    template<typename OsModel_P,
00473             typename node_id_t_P,
00474             typename NodeSet_P,
00475             typename DistanceMap_P,
00476             typename Arithmatic_P>
00477    void
00478    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00479    set_hops( int hops )
00480    {
00481       hops_ = hops;
00482    }
00483    // ----------------------------------------------------------------------
00484    template<typename OsModel_P,
00485             typename node_id_t_P,
00486             typename NodeSet_P,
00487             typename DistanceMap_P,
00488             typename Arithmatic_P>
00489    void
00490    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00491    set_distance( Arithmatic distance )
00492    {
00493       distance_ = distance;
00494    }
00495    // ----------------------------------------------------------------------
00496    template<typename OsModel_P,
00497             typename node_id_t_P,
00498             typename NodeSet_P,
00499             typename DistanceMap_P,
00500             typename Arithmatic_P>
00501    void
00502    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00503    set_confidence( Arithmatic confidence )
00504    {
00505       confidence_ = confidence;
00506    }
00507    // ----------------------------------------------------------------------
00508    template<typename OsModel_P,
00509             typename node_id_t_P,
00510             typename NodeSet_P,
00511             typename DistanceMap_P,
00512             typename Arithmatic_P>
00513    void
00514    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00515    set_twin( bool twin )
00516    {
00517       is_twin_ = twin;
00518    }
00519    // ----------------------------------------------------------------------
00520    template<typename OsModel_P,
00521             typename node_id_t_P,
00522             typename NodeSet_P,
00523             typename DistanceMap_P,
00524             typename Arithmatic_P>
00525    void
00526    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00527    set_anchor( bool anchor )
00528    {
00529       is_anchor_ = anchor;
00530    }
00531    // ----------------------------------------------------------------------
00532    template<typename OsModel_P,
00533             typename node_id_t_P,
00534             typename NodeSet_P,
00535             typename DistanceMap_P,
00536             typename Arithmatic_P>
00537    void
00538    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00539    set_node( node_id_t node )
00540    {
00541       node_ = node;
00542    }
00543    // ----------------------------------------------------------------------
00544    template<typename OsModel_P,
00545             typename node_id_t_P,
00546             typename NodeSet_P,
00547             typename DistanceMap_P,
00548             typename Arithmatic_P>
00549    void
00550    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00551    convert_hops( Arithmatic hop_dist )
00552    {
00553       if ( hops_ != UNKNOWN_HOP_CNT )
00554          distance_ = hops_ * hop_dist;
00555    }
00556    // ----------------------------------------------------------------------
00557    template<typename OsModel_P,
00558             typename node_id_t_P,
00559             typename NodeSet_P,
00560             typename DistanceMap_P,
00561             typename Arithmatic_P>
00562    node_id_t_P
00563    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00564    node( void )
00565    {
00566       return node_;
00567    }
00568    // ----------------------------------------------------------------------
00569    template<typename OsModel_P,
00570             typename node_id_t_P,
00571             typename NodeSet_P,
00572             typename DistanceMap_P,
00573             typename Arithmatic_P>
00574    Vec<Arithmatic_P>&
00575    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00576    pos( void )
00577    {
00578       return pos_;
00579    }
00580    // ----------------------------------------------------------------------
00581    template<typename OsModel_P,
00582             typename node_id_t_P,
00583             typename NodeSet_P,
00584             typename DistanceMap_P,
00585             typename Arithmatic_P>
00586    int
00587    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00588    hops( void )
00589    {
00590       return hops_;
00591    }
00592    // ----------------------------------------------------------------------
00593    template<typename OsModel_P,
00594             typename node_id_t_P,
00595             typename NodeSet_P,
00596             typename DistanceMap_P,
00597             typename Arithmatic_P>
00598    typename LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::Arithmatic
00599    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00600    distance( void )
00601    {
00602       return distance_;
00603    }
00604    // ----------------------------------------------------------------------
00605    template<typename OsModel_P,
00606             typename node_id_t_P,
00607             typename NodeSet_P,
00608             typename DistanceMap_P,
00609             typename Arithmatic_P>
00610    typename LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::Arithmatic
00611    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00612    confidence( void )
00613    {
00614       return confidence_;
00615    }
00616    // ----------------------------------------------------------------------
00617    template<typename OsModel_P,
00618             typename node_id_t_P,
00619             typename NodeSet_P,
00620             typename DistanceMap_P,
00621             typename Arithmatic_P>
00622    bool
00623    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00624    is_anchor( void )
00625    {
00626       return is_anchor_;
00627    }
00628    // ----------------------------------------------------------------------
00629    template<typename OsModel_P,
00630             typename node_id_t_P,
00631             typename NodeSet_P,
00632             typename DistanceMap_P,
00633             typename Arithmatic_P>
00634    bool
00635    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00636    is_valid( void )
00637    {
00638       return ( has_pos() && has_distance() );
00639    }
00640    // ----------------------------------------------------------------------
00641    template<typename OsModel_P,
00642             typename node_id_t_P,
00643             typename NodeSet_P,
00644             typename DistanceMap_P,
00645             typename Arithmatic_P>
00646    bool
00647    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00648    is_twin( void )
00649    {
00650       return is_twin_;
00651    }
00652    // ----------------------------------------------------------------------
00653    template<typename OsModel_P,
00654             typename node_id_t_P,
00655             typename NodeSet_P,
00656             typename DistanceMap_P,
00657             typename Arithmatic_P>
00658    bool
00659    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00660    has_pos( void )
00661    {
00662       return has_pos_;
00663    }
00664    // ----------------------------------------------------------------------
00665    template<typename OsModel_P,
00666             typename node_id_t_P,
00667             typename NodeSet_P,
00668             typename DistanceMap_P,
00669             typename Arithmatic_P>
00670    bool
00671    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00672    has_distance( void )
00673    {
00674       return distance_ != UNKNOWN_DISTANCE;
00675    }
00676    // ----------------------------------------------------------------------
00677    template<typename OsModel_P,
00678             typename node_id_t_P,
00679             typename NodeSet_P,
00680             typename DistanceMap_P,
00681             typename Arithmatic_P>
00682    bool
00683    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00684    is_confident( void )
00685    {
00686       return confidence() != 0 && !is_twin() && has_pos() && has_distance();
00687    }
00688    // ----------------------------------------------------------------------
00689    template<typename OsModel_P,
00690             typename node_id_t_P,
00691             typename NodeSet_P,
00692             typename DistanceMap_P,
00693             typename Arithmatic_P>
00694    DistanceMap_P&
00695    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P,Arithmatic_P>::
00696    neighbors( void )
00697    {
00698       return neighbors_;
00699    }
00700    // ----------------------------------------------------------------------
00701    template<typename OsModel_P,
00702             typename node_id_t_P,
00703             typename NodeSet_P,
00704             typename DistanceMap_P,
00705             typename Arithmatic_P>
00706    bool
00707    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00708    has_neighbor( node_id_t node )
00709    {
00710       return neighbors_.find( node ) != neighbors_.end();
00711    }
00712    // ----------------------------------------------------------------------
00713    template<typename OsModel_P,
00714             typename node_id_t_P,
00715             typename NodeSet_P,
00716             typename DistanceMap_P,
00717             typename Arithmatic_P>
00718    bool
00719    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00720    has_neighbor_distance( node_id_t node )
00721    {
00722       return neighbor_distance( node ) != UNKNOWN_DISTANCE;
00723    }
00724    // ----------------------------------------------------------------------
00725    template<typename OsModel_P,
00726             typename node_id_t_P,
00727             typename NodeSet_P,
00728             typename DistanceMap_P,
00729             typename Arithmatic_P>
00730    typename LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::Arithmatic
00731    LocalizationNeighborInfo<OsModel_P, node_id_t_P, NodeSet_P, DistanceMap_P, Arithmatic_P>::
00732    neighbor_distance( node_id_t node )
00733    {
00734       NeighborIterator it = neighbors_.find( node );
00735 
00736       if ( it != neighbors_.end() )
00737          return it->second;
00738       else
00739          return UNKNOWN_DISTANCE;
00740    }
00741 
00742 }// namespace wiselib
00743 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines