Line.C

00001 /*!@file FeatureMatching/Line.C Line segments algs */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: $
00035 // $Id: $
00036 //
00037 
00038 #ifndef LINE_C_DEFINED
00039 #define LINE_C_DEFINED
00040 
00041 #include "FeatureMatching/Line.H"
00042 #include "Image/DrawOps.H"
00043 #include "GUI/DebugWin.H"
00044 #include "Util/FastMathFunctions.H"
00045 
00046 // ######################################################################
00047 Line::Line(float sx, float sy, float ex, float ey) :
00048   p1(sx,sy),
00049   p2(ex,ey),
00050   itsDirectionIdx(-1)
00051 {}
00052 
00053 // ######################################################################
00054 Line::Line(Point2D<float> inP1, Point2D<float> inP2) :
00055   p1(inP1),
00056   p2(inP2),
00057   itsDirectionIdx(-1)
00058 {}
00059 
00060 // ######################################################################
00061 Line::Line(Point2D<int> inP1, Point2D<int> inP2) :
00062   p1(inP1),
00063   p2(inP2),
00064   itsDirectionIdx(-1)
00065 {}
00066 
00067 // ######################################################################
00068 double Line::getOri()
00069 {
00070   double theta = atan2(p2.j-p1.j,p2.i-p1.i);
00071   if (theta<0)
00072     theta += M_PI;
00073   return theta;
00074 }
00075 
00076 // ######################################################################
00077 double Line::getLength()
00078 {
00079   double dx = p2.i - p1.i;
00080   double dy = p2.j - p1.j;
00081   return sqrt(dx*dx + dy*dy);
00082 }
00083 
00084 // ######################################################################
00085 void Line::scale(double s)
00086 {
00087   p1.i *= s;
00088   p1.j *= s;
00089   p2.i *= s;
00090   p2.j *= s;
00091 }
00092 
00093 // ######################################################################
00094 void Line::scale(double sx, double sy)
00095 {
00096   p1.i *= sx;
00097   p1.j *= sy;
00098   p2.i *= sx;
00099   p2.j *= sy;
00100 }
00101 
00102 // ######################################################################
00103 Point2D<float> Line::getCenter()
00104 {
00105   Point2D<float> c = (p1+p2)/2;
00106 
00107   return c;
00108 
00109 }
00110 
00111 // ######################################################################
00112 void Line::rotate(double theta)
00113 {
00114 
00115   double sinTheta;
00116   double cosTheta;
00117   double mat[2][2];
00118 
00119   sinTheta = sin(theta);
00120   cosTheta = cos(theta);
00121   mat[0][0] = cosTheta;
00122   mat[0][1] = -sinTheta;
00123   mat[1][0] = sinTheta;
00124   mat[1][1] = cosTheta;
00125 
00126   double x, y;
00127   x = p1.i, y = p1.j;
00128   p1.i = x*mat[0][0] + y*mat[0][1];
00129   p1.j = x*mat[1][0] + y*mat[1][1];
00130 
00131   x = p2.i, y = p2.j;
00132   p2.i = x*mat[0][0] + y*mat[0][1];
00133   p2.j = x*mat[1][0] + y*mat[1][1];
00134 }
00135 
00136 // ######################################################################
00137 void Line::shear(double k1, double k2)
00138 {
00139   float tmpP1i = p1.i + p1.j*k2;
00140   float tmpP1j = p1.i*k1 + p1.j;
00141 
00142   float tmpP2i = p2.i + p2.j*k2;
00143   float tmpP2j = p2.i*k1 + p2.j;
00144 
00145   p1.i = tmpP1i;
00146   p1.j = tmpP1j;
00147   p2.i = tmpP2i;
00148   p2.j = tmpP2j;
00149 }
00150 
00151 // ######################################################################
00152 void Line::trans(Point2D<float> p)
00153 {
00154   p1 += p;
00155   p2 += p;
00156 
00157 }
00158 
00159 // ######################################################################
00160 void Line::quantize(int numDirections)
00161 {
00162   Point2D<float> center = getCenter();
00163   trans(center*-1);
00164 
00165   double ori = getOri();
00166   itsDirectionIdx =  floor ((ori  *numDirections) / (M_PI+1e-5));
00167   double dOri = (((itsDirectionIdx)*M_PI)/numDirections + M_PI/(2*numDirections)) - ori;
00168   rotate(dOri);
00169   trans(center);
00170 }
00171 
00172 void Line::setQuantizedDir(int numDirections)
00173 {
00174   double ori = getOri();
00175   itsDirectionIdx = (int) floor ((ori  *numDirections) / (M_PI+1e-5));
00176 }
00177 
00178 
00179 // ######################################################################
00180 /* So things look consistent in everyone's emacs... */
00181 /* Local Variables: */
00182 /* indent-tabs-mode: nil */
00183 /* End: */
00184 
00185 #endif
00186 
Generated on Sun May 8 08:40:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3