LoLRFData.C

00001 /**
00002    \file  Robots/LoBot/misc/LoLRFData.C
00003    \brief This file defines the non-inline member functions of the
00004    lobot::LRFData class.
00005 */
00006 
00007 // //////////////////////////////////////////////////////////////////// //
00008 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00009 // by the University of Southern California (USC) and the iLab at USC.  //
00010 // See http://iLab.usc.edu for information about this project.          //
00011 // //////////////////////////////////////////////////////////////////// //
00012 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00013 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00014 // in Visual Environments, and Applications'' by Christof Koch and      //
00015 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00016 // pending; application number 09/912,225 filed July 23, 2001; see      //
00017 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00018 // //////////////////////////////////////////////////////////////////// //
00019 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00022 // redistribute it and/or modify it under the terms of the GNU General  //
00023 // Public License as published by the Free Software Foundation; either  //
00024 // version 2 of the License, or (at your option) any later version.     //
00025 //                                                                      //
00026 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00027 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00028 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00029 // PURPOSE.  See the GNU General Public License for more details.       //
00030 //                                                                      //
00031 // You should have received a copy of the GNU General Public License    //
00032 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00033 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00034 // Boston, MA 02111-1307 USA.                                           //
00035 // //////////////////////////////////////////////////////////////////// //
00036 //
00037 // Primary maintainer for this file: mviswana usc edu
00038 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/io/LoLRFData.C $
00039 // $Id: LoLRFData.C 13434 2010-05-19 04:36:08Z mviswana $
00040 //
00041 
00042 //------------------------------ HEADERS --------------------------------
00043 
00044 // lobot headers
00045 #include "Robots/LoBot/io/LoLRFData.H"
00046 #include "Robots/LoBot/misc/LoExcept.H"
00047 #include "Robots/LoBot/util/LoMath.H"
00048 
00049 // INVT headers
00050 #include "Image/Image.H"
00051 
00052 // Standard C++ headers
00053 #include <algorithm>
00054 
00055 //----------------------------- NAMESPACE -------------------------------
00056 
00057 namespace lobot {
00058 
00059 //-------------------------- INITIALIZATION -----------------------------
00060 
00061 LRFData::LRFData(const LaserRangeFinder* lrf)
00062    : m_angle_range(lrf->get_angular_range()),
00063      m_distance_range(lrf->get_distance_range()),
00064      m_distances(new int[m_angle_range.size()])
00065 {
00066    Image<int> D = lrf->get_distances() ;
00067    std::copy(D.begin(), D.end(), m_distances) ;
00068 }
00069 
00070 LRFData::LRFData(const LRFData& L)
00071    : m_angle_range(L.m_angle_range),
00072      m_distance_range(L.m_distance_range),
00073      m_distances(new int[m_angle_range.size()])
00074 {
00075    std::copy(L.m_distances, L.m_distances + m_angle_range.size(), m_distances);
00076 }
00077 
00078 LRFData& LRFData::operator=(const LRFData& L)
00079 {
00080    if (&L != this) {
00081       m_angle_range    = L.m_angle_range ;
00082       m_distance_range = L.m_distance_range ;
00083       std::copy(L.m_distances, L.m_distances + m_angle_range.size(),
00084                 m_distances) ;
00085    }
00086    return *this ;
00087 }
00088 
00089 LRFData::Reading::Reading(int angle, int distance)
00090    : m_angle(angle), m_distance(distance)
00091 {}
00092 
00093 //---------------------- DISTANCE DATA RETRIEVAL ------------------------
00094 
00095 // Return measurement corresponding to given angle
00096 int LRFData::distance(int angle) const
00097 {
00098    if (angle < m_angle_range.min() || angle > m_angle_range.max())
00099       return -1 ;
00100    return m_distances[angle - m_angle_range.min()] ;
00101 }
00102 
00103 // Return all distance readings in an STL vector
00104 std::vector<int> LRFData::distances() const
00105 {
00106    return std::vector<int>(m_distances, m_distances + m_angle_range.size()) ;
00107 }
00108 
00109 // Return the average distance in some angular range
00110 float LRFData::average_distance(int min_angle, int max_angle) const
00111 {
00112    // Calculate indices corresponding to min and max angle
00113    const int a = std::max(min_angle - m_angle_range.min(), 0) ;
00114    const int b = std::min(max_angle - m_angle_range.min(),
00115                           m_angle_range.size() - 1) ;
00116 
00117    // Calculate and return average distance between above indices
00118    if (a == b) // special case for single angle
00119       return m_distances[a] ;
00120 
00121    int n = 0 ; float sum = 0 ;
00122    for (int i = a; i <= b; ++i)
00123       if (m_distances[i] > 0) {
00124          sum += m_distances[i] ;
00125          ++n ;
00126       }
00127    return (n > 0) ? sum/n : -1 ;
00128 }
00129 
00130 // Return the minimum reading in some angular range
00131 LRFData::Reading LRFData::min_distance(int min_angle, int max_angle) const
00132 {
00133    min_angle = clamp(min_angle, m_angle_range.min(), m_angle_range.max()) ;
00134    max_angle = clamp(max_angle, m_angle_range.min(), m_angle_range.max()) ;
00135    if (min_angle > max_angle)
00136       std::swap(min_angle, max_angle) ;
00137 
00138    const int m = min_angle - m_angle_range.min() ;
00139    const int M = max_angle - m_angle_range.min() ;
00140 
00141    int* min = m_distances + m ;
00142    while (*min == -1 && min < m_distances + M + 1) // skip initial bad readings
00143       ++min ;
00144    if (min >= m_distances + M) // all bad readings!?!
00145       min = m_distances + m ;
00146 
00147    for (int* p = min + 1; p < m_distances + M + 1; ++p)
00148    {
00149       if (*p == -1) // bad reading
00150          continue ;
00151       if (*p < *min)
00152          min = p ;
00153    }
00154 
00155    return Reading((min - m_distances) + m_angle_range.min(), *min) ;
00156 }
00157 
00158 //----------------------------- CLEAN-UP --------------------------------
00159 
00160 LRFData::~LRFData()
00161 {
00162    delete[] m_distances ;
00163 }
00164 
00165 //-----------------------------------------------------------------------
00166 
00167 } // end of namespace encapsulating this file's definitions
00168 
00169 /* So things look consistent in everyone's emacs... */
00170 /* Local Variables: */
00171 /* indent-tabs-mode: nil */
00172 /* End: */
Generated on Sun May 8 08:41:30 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3