Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Point2D.H

Go to the documentation of this file.
00001 /*!@file Image/Point2D.H A basic 2D point class */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Point2D.H $
00035 // $Id: Point2D.H 10409 2008-10-29 21:19:10Z lior $
00036 //
00037 
00038 #ifndef POINT2D_H_DEFINED
00039 #define POINT2D_H_DEFINED
00040 
00041 #include "Image/Dims.H"
00042 #include "Util/Promotions.H"
00043 #include "Util/log.H"
00044 #include <cmath>
00045 #include <string> // for string conversions
00046 #include <vector>
00047 
00048 //! This is a basic class to encode 2D integer coordinates
00049 /*! This is a completely public class whose goal is just to provide a
00050  shorthand notation for 2D integer coordinates.
00051 */
00052 template <class T>
00053 class Point2D
00054 {
00055 public:
00056   //! The default constructor initializes the coordinates to (0,0)
00057   inline Point2D();
00058 
00059   //! Initialize the Point2D from horizontal & vertical coordinates
00060   inline Point2D(const T ii, const T jj);
00061 
00062   //! Initialize from a Dims object; i=w, j=h
00063   explicit inline Point2D(const Dims& d);
00064 
00065   //! Explicit conversion from type T to another type U
00066   /*! Note that this simply uses clamped_convert, so it will clamp
00067       coordinates to the available range of T, and may round down. */
00068   template <class U>
00069   explicit inline Point2D(const Point2D<U>& a);
00070 
00071   //! += operator
00072   inline Point2D<T>& operator+=(const Point2D<T> &p);
00073   //! -= operator
00074   inline Point2D<T>& operator-=(const Point2D<T> &p);
00075   //! *= operator
00076   inline Point2D<T>& operator*=(const Point2D<T> &p);
00077   //! /= operator
00078   inline Point2D<T>& operator/=(const Point2D<T> &p);
00079 
00080   //! + operator
00081   template <class U>
00082   inline Point2D<typename promote_trait<T,U>::TP>
00083   operator+(const Point2D<U> &p) const;
00084   //! - operator
00085   template <class U>
00086   inline Point2D<typename promote_trait<T,U>::TP>
00087   operator-(const Point2D<U> &p) const;
00088   //! * operator
00089   template <class U>
00090   inline Point2D<typename promote_trait<T,U>::TP>
00091   operator*(const Point2D<U> &p) const;
00092   //! / operator
00093   template <class U>
00094   inline Point2D<typename promote_trait<T,U>::TP>
00095   operator/(const Point2D<U> &p) const;
00096 
00097   //! += operator
00098   inline Point2D<T>& operator+=(const T val);
00099   //! -= operator
00100   inline Point2D<T>& operator-=(const T val);
00101   //! *= operator
00102   inline Point2D<T>& operator*=(const T val);
00103   //! /= operator
00104   inline Point2D<T>& operator/=(const T val);
00105 
00106   //! + operator
00107   template <class U>
00108   inline Point2D<typename promote_trait<T,U>::TP> operator+(const U val) const;
00109 
00110   //! - operator
00111   template <class U>
00112   inline Point2D<typename promote_trait<T,U>::TP> operator-(const U val) const;
00113 
00114   //! * operator
00115   template <class U>
00116   inline Point2D<typename promote_trait<T,U>::TP> operator*(const U val) const;
00117 
00118   //! / operator
00119   template <class U>
00120   inline Point2D<typename promote_trait<T,U>::TP> operator/(const U val) const;
00121 
00122   //! test whether i and j are both positive
00123   inline bool isValid() const;
00124 
00125   //! the square of the euclidean distance
00126   inline typename promote_trait<T,float>::TP
00127   squdist(const Point2D<T>& p) const;
00128 
00129   //! the euclidean distance from p
00130   inline typename promote_trait<T,float>::TP
00131   distance(const Point2D<T>& p) const;
00132 
00133   //! clamp the coords such that the point fits into image of dimensions dims
00134   inline void clampToDims(const Dims& dims);
00135 
00136   //! 2D coordinates
00137   T i, j;
00138 };
00139 
00140 
00141 //! == operator
00142 template <class T, class U>
00143 inline bool operator==(const Point2D<T>& p1, const Point2D<U>& p2);
00144 
00145 //! != operator
00146 template <class T, class U>
00147 inline bool operator!=(const Point2D<T>& p1, const Point2D<U>& p2);
00148 
00149 //! > operator
00150 template <class T, class U>
00151 inline bool operator>(const Point2D<T>& p1, const Point2D<U>& p2);
00152 
00153 //! < operator
00154 template <class T, class U>
00155 inline bool operator<(const Point2D<T>& p1, const Point2D<U>& p2);
00156 
00157 
00158 //! Point2D<T> overload: format is "<i>,<j>"
00159 template <class T>
00160 std::string convertToString(const Point2D<T>& val);
00161 
00162 //! Point2D<T> overload: format is "<i>,<j>"
00163 template <class T>
00164 void convertFromString(const std::string& str, Point2D<T>& val);
00165 
00166 //! Is a point inside a polygon?
00167 inline bool pnpoly(const std::vector<Point2D<int> > &polygon,
00168                    const Point2D<int>& p);
00169 
00170 
00171 // ######################################################################
00172 template <class T>
00173 inline Point2D<T>::Point2D()
00174 { i = 0; j = 0; }
00175 
00176 // ######################################################################
00177 template <class T>
00178 inline Point2D<T>::Point2D(const T ii, const T jj)
00179 { i = ii; j = jj; }
00180 
00181 // ######################################################################
00182 template <class T>
00183 inline Point2D<T>::Point2D(const Dims& d)
00184 { i = d.w(); j = d.h(); }
00185 
00186 // #######################################################################
00187 template <class T>
00188 template <class U>
00189 inline Point2D<T>::Point2D(const Point2D<U>& a)
00190   : i(clamped_convert<T>(a.i)), j(clamped_convert<T>(a.j))
00191 { }
00192 
00193 // ######################################################################
00194 template <class T>
00195 inline Point2D<T>& Point2D<T>::operator+=(const Point2D<T> &p)
00196 { i += p.i; j += p.j; return *this; }
00197 
00198 // ######################################################################
00199 template <class T>
00200 inline Point2D<T>& Point2D<T>::operator-=(const Point2D<T> &p)
00201 { i -= p.i; j -= p.j; return *this; }
00202 
00203 // ######################################################################
00204 template <class T>
00205 inline Point2D<T>& Point2D<T>::operator*=(const Point2D<T> &p)
00206 { i *= p.i; j *= p.j; return *this; }
00207 
00208 // ######################################################################
00209 template <class T>
00210 inline Point2D<T>& Point2D<T>::operator/=(const Point2D<T> &p)
00211 { i /= p.i; j /= p.j; return *this; }
00212 
00213 // ######################################################################
00214 template <class T>
00215 template <class U>
00216 inline Point2D<typename promote_trait<T,U>::TP>
00217 Point2D<T>::operator+(const Point2D<U> &p) const
00218 { return Point2D<typename promote_trait<T,U>::TP>(i + p.i, j + p.j); }
00219 
00220 // ######################################################################
00221 template <class T>
00222 template <class U>
00223 inline Point2D<typename promote_trait<T,U>::TP>
00224 Point2D<T>::operator-(const Point2D<U> &p) const
00225 { return Point2D<typename promote_trait<T,U>::TP>(i - p.i, j - p.j); }
00226 
00227 // ######################################################################
00228 template <class T>
00229 template <class U>
00230 inline Point2D<typename promote_trait<T,U>::TP>
00231 Point2D<T>::operator*(const Point2D<U> &p) const
00232 { return Point2D<typename promote_trait<T,U>::TP>(i * p.i, j * p.j); }
00233 
00234 // ######################################################################
00235 template <class T>
00236 template <class U>
00237 inline Point2D<typename promote_trait<T,U>::TP>
00238 Point2D<T>::operator/(const Point2D<U> &p) const
00239 { return Point2D<typename promote_trait<T,U>::TP>(i / p.i, j / p.j); }
00240 
00241 // ######################################################################
00242 template <class T>
00243 inline Point2D<T>& Point2D<T>::operator+=(const T val)
00244 { i += val; j += val; return *this; }
00245 
00246 // ######################################################################
00247 template <class T>
00248 inline Point2D<T>& Point2D<T>::operator-=(const T val)
00249 { i -= val; j -= val; return *this; }
00250 
00251 // ######################################################################
00252 template <class T>
00253 inline Point2D<T>& Point2D<T>::operator*=(const T val)
00254 { i *= val; j *= val; return *this; }
00255 
00256 // ######################################################################
00257 template <class T>
00258 inline Point2D<T>& Point2D<T>::operator/=(const T val)
00259 { i /= val; j /= val; return *this; }
00260 
00261 // ######################################################################
00262 template <class T>
00263 template <class U>
00264 inline Point2D<typename promote_trait<T,U>::TP>
00265 Point2D<T>::operator+(const U val) const
00266 { return Point2D<typename promote_trait<T,U>::TP>(this->i+val, this->j+val); }
00267 
00268 // ######################################################################
00269 template <class T>
00270 template <class U>
00271 inline Point2D<typename promote_trait<T,U>::TP>
00272 Point2D<T>::operator-(const U val) const
00273 { return Point2D<typename promote_trait<T,U>::TP>(this->i-val, this->j-val); }
00274 
00275 // ######################################################################
00276 template <class T>
00277 template <class U>
00278 inline Point2D<typename promote_trait<T,U>::TP>
00279 Point2D<T>::operator*(const U val) const
00280 { return Point2D<typename promote_trait<T,U>::TP>(this->i*val, this->j*val); }
00281 
00282 // ######################################################################
00283 template <class T>
00284 template <class U>
00285 inline Point2D<typename promote_trait<T,U>::TP>
00286 Point2D<T>::operator/(const U val) const
00287 { return Point2D<typename promote_trait<T,U>::TP>(this->i/val, this->j/val); }
00288 
00289 // ######################################################################
00290 template <class T, class U>
00291 inline bool operator==(const Point2D<T>& p1, const Point2D<U>& p2)
00292 { return p1.i == p2.i && p1.j == p2.j; }
00293 
00294 // ######################################################################
00295 template <class T, class U>
00296 inline bool operator!=(const Point2D<T>& p1, const Point2D<U>& p2)
00297 { return p1.i != p2.i || p1.j != p2.j; }
00298 
00299 // ######################################################################
00300 template <class T, class U>
00301 inline bool operator>(const Point2D<T>& p1, const Point2D<U>& p2)
00302 { return p1.i > p2.i && p1.j > p2.j; }
00303 
00304 // ######################################################################
00305 template <class T, class U>
00306 inline bool operator<(const Point2D<T>& p1, const Point2D<U>& p2)
00307 { return p1.i < p2.i && p1.j < p2.j; }
00308 
00309 // ######################################################################
00310 template <class T>
00311 inline bool Point2D<T>::isValid() const
00312 { return ((i >= 0) && (j >= 0)); }
00313 
00314 // ######################################################################
00315 template <class T>
00316 inline typename promote_trait<T,float>::TP
00317 Point2D<T>::squdist(const Point2D<T>& p) const
00318 {
00319   typedef typename promote_trait<T,float>::TP TF;
00320   TF d1 = p.i - i, d2 = p.j - j;
00321   return (d1 * d1 + d2 * d2);
00322 }
00323 
00324 // ######################################################################
00325 template <class T>
00326 inline typename promote_trait<T,float>::TP
00327 Point2D<T>::distance(const Point2D<T>& p) const
00328 { return sqrt(squdist(p)); }
00329 
00330 // ######################################################################
00331 template <class T>
00332 inline void Point2D<T>::clampToDims(const Dims& dims)
00333 {
00334   if (dims.isEmpty())
00335     LFATAL("clampToDims() requires non-empty Dims");
00336 
00337   if (i < 0) i = 0; else if (i >= dims.w()) i = dims.w()-1;
00338   if (j < 0) j = 0; else if (j >= dims.h()) j = dims.h()-1;
00339 }
00340 
00341 // ######################################################################
00342 // Original algo by Randolph Franklin,
00343 //   http://astronomy.swin.edu.au/~pbourke/geometry/insidepoly/
00344 inline bool pnpoly(const std::vector<Point2D<int> > &polygon,
00345                    const Point2D<int>& p)
00346 {
00347   bool c = false; const uint ps = polygon.size();
00348   for (uint i = 0, j = ps-1; i < ps; j = i++)
00349     if ((((polygon[i].j <= p.j) && (p.j < polygon[j].j)) ||
00350          ((polygon[j].j <= p.j) && (p.j < polygon[i].j))) &&
00351         (p.i < (polygon[j].i - polygon[i].i) *
00352          (p.j - polygon[i].j) / (polygon[j].j - polygon[i].j)
00353          + polygon[i].i))
00354       c = !c;
00355 
00356   return c;
00357 }
00358 
00359 // ######################################################################
00360 /* So things look consistent in everyone's emacs... */
00361 /* Local Variables: */
00362 /* indent-tabs-mode: nil */
00363 /* End: */
00364 
00365 #endif

Generated on Sun Nov 22 13:42:35 2009 for iLab Neuromorphic Vision Toolkit by  doxygen 1.4.4