00001 /** 00002 \file Robots/LoBot/misc/LoPointList.C 00003 \brief This file defines the non-inline member functions of the 00004 lobot::PointList 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/metlog/LoPointList.C $ 00039 // $Id: LoPointList.C 13936 2010-09-15 08:01:24Z mviswana $ 00040 // 00041 00042 //------------------------------ HEADERS -------------------------------- 00043 00044 // lobot headers 00045 #include "Robots/LoBot/metlog/LoPointList.H" 00046 #include "Robots/LoBot/util/LoMath.H" 00047 #include "Robots/LoBot/misc/LoExcept.H" 00048 00049 // Boost headers 00050 #include <boost/bind.hpp> 00051 00052 // Standard C++ headers 00053 #include <algorithm> 00054 #include <iterator> 00055 00056 //----------------------------- NAMESPACE ------------------------------- 00057 00058 namespace lobot { 00059 00060 //-------------------------- INITIALIZATION ----------------------------- 00061 00062 PointList::PointList(int n) 00063 { 00064 if (n > 0) 00065 m_list.reserve(n) ; 00066 } 00067 00068 // Add new points to the point list without any duplications, i.e., make 00069 // the point list behave like a point set. 00070 void PointList::add(int x, int y) 00071 { 00072 mPoint p(x, y) ; 00073 if (std::find(m_list.begin(), m_list.end(), p) == m_list.end()) 00074 m_list.push_back(p) ; 00075 } 00076 00077 // Append given point list to this one 00078 PointList& PointList::operator+=(const PointList& L) 00079 { 00080 std::copy(L.m_list.begin(), L.m_list.end(), std::back_inserter(m_list)) ; 00081 return *this ; 00082 } 00083 00084 //----------------------- POINT CORRESPONDENCES ------------------------- 00085 00086 // Return true if the Euclidean distance between point a and reference 00087 // point p is less than the Euclidean distance between point b and 00088 // reference point p, i.e., return true if a is closer to p than b. 00089 static bool dist_cmp(const mPoint& a, const mPoint& b, const mPoint& p) 00090 { 00091 return sqrt(sqr(a.first - p.first) + sqr(a.second - p.second)) 00092 < sqrt(sqr(b.first - p.first) + sqr(b.second - p.second)) ; 00093 } 00094 00095 // In the point list L, find the point closest to reference point p 00096 static mPoint nearest_point(const mPoint& p, const PointList& L) 00097 { 00098 return *std::min_element(L.begin(), L.end(), 00099 boost::bind(dist_cmp, _1, _2, p)) ; 00100 } 00101 00102 // For each point in this point list, find the nearest point in the given 00103 // point list L and return the resulting point list. 00104 PointList PointList::find_correspondences(const PointList& L) const 00105 { 00106 if (m_list.empty() || L.m_list.empty()) 00107 throw misc_error(LOGIC_ERROR) ; 00108 00109 PointList result(m_list.size()) ; 00110 std::transform(m_list.begin(), m_list.end(), 00111 std::back_inserter(result.m_list), 00112 boost::bind(nearest_point, _1, L)) ; 00113 return result ; 00114 } 00115 00116 //----------------------------------------------------------------------- 00117 00118 } // end of namespace encapsulating this file's definitions 00119 00120 /* So things look consistent in everyone's emacs... */ 00121 /* Local Variables: */ 00122 /* indent-tabs-mode: nil */ 00123 /* End: */