Homography.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  Homography.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *******************************************************************************/
00007 
00008 #include "Homography.h"
00009 
00010 #include <math.h>
00011 #include <string.h>
00012 #include <sstream>
00013 
00014 #define THIS Homography
00015 
00016 THIS::THIS ( double homMat[9] )
00017 {
00018   memcpy( m_HomMat, homMat, 9*sizeof(double) );
00019 }
00020 
00021 THIS::THIS ( const THIS& other )
00022 {
00023   memcpy( m_HomMat, other.m_HomMat, 9*sizeof(double) );
00024 }
00025 
00026 THIS& THIS::operator=( const Homography& other )
00027 {
00028   memcpy( m_HomMat, other.m_HomMat, 9*sizeof(double) );
00029   return *this;
00030 }
00031 
00032 Point2D THIS::transform ( Point2D point2 )
00033 {
00034   if ( !point2.isValid() )
00035   {
00036     return point2;
00037   }
00038   else
00039   {
00040     double x = point2.x();
00041     double y = point2.y();
00042     double Z = 1. / ( m_HomMat[6] * x + m_HomMat[7] * y + m_HomMat[8] );
00043     double X = ( m_HomMat[0] * x + m_HomMat[1] * y + m_HomMat[2] ) * Z;
00044     double Y = ( m_HomMat[3] * x + m_HomMat[4] * y + m_HomMat[5] ) * Z;
00045     return Point2D( X, Y );
00046   }
00047 }
00048 
00049 void THIS::transform ( std::vector<Point2D>& points2, std::vector<Point2D> &projPoints )
00050 {
00051   projPoints.reserve( points2.size() );
00052 
00053   // Translate src_corners to dst_corners using homography
00054   for ( unsigned i = 0; i < points2.size(); i++ )
00055   {
00056     if ( !points2[i].isValid() )
00057     {
00058       projPoints.push_back( points2[i] );
00059     }
00060     else
00061     {
00062       double x = points2[i].x();
00063       double y = points2[i].y();
00064       double Z = 1. / ( m_HomMat[6] * x + m_HomMat[7] * y + m_HomMat[8] );
00065       double X = ( m_HomMat[0] * x + m_HomMat[1] * y + m_HomMat[2] ) * Z;
00066       double Y = ( m_HomMat[3] * x + m_HomMat[4] * y + m_HomMat[5] ) * Z;
00067       projPoints.push_back( Point2D( X, Y ) );
00068     }
00069   }
00070 }
00071 
00072 bool THIS::checkValidity ( std::vector<Point2D>& points2 )
00073 {
00074   // Translate src_corners to dst_corners using homography
00075   for ( unsigned i = 0; i < points2.size(); i++ )
00076   {
00077     if ( !points2[i].isValid() )
00078     {
00079       continue;
00080     }
00081     else
00082     {
00083       double x = points2[i].x();
00084       double y = points2[i].y();
00085       double Z = 1. / ( m_HomMat[6] * x + m_HomMat[7] * y + m_HomMat[8] );
00086       if ( Z < 0 )
00087       {
00088         return false;
00089       }
00090     }
00091   }
00092   return true;
00093 }
00094 
00095 std::string THIS::toString()
00096 {
00097   std::ostringstream s;
00098   for ( int j=0; j< 3; j++ )
00099   {
00100     for ( int i=0; i< 3; i++ )
00101     {
00102       s << m_HomMat[i+3*j] << " ";
00103     }
00104     s << std::endl;
00105   }
00106   return s.str();
00107 }
00108 
00109 #undef THIS


robbie_architecture
Author(s): Viktor Seib
autogenerated on Mon Oct 6 2014 02:53:09