experiments/include/projector.h

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00007  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #ifndef PROJECTOR_H
00025 #define PROJECTOR_H
00026 
00027 #include "experimentsconfig.h"
00028 #include "wmatrix.h"
00029 #include "mathutils.h"
00030 
00031 namespace farsa {
00032 
00036 class FARSA_EXPERIMENTS_TEMPLATE ImagePoint {
00037 public:
00043     ImagePoint() :
00044         x(0.0),
00045         y(0.0),
00046         valid(false)
00047     {
00048     }
00049 
00057     ImagePoint(double _x, double _y) :
00058         x(_x),
00059         y(_y),
00060         valid(true)
00061     {
00062     }
00063 
00069     bool isValid() const
00070     {
00071         return valid;
00072     }
00073 
00077     double x;
00078 
00082     double y;
00083 
00087     bool valid;
00088 };
00089 
00097 class FARSA_EXPERIMENTS_TEMPLATE Projector
00098 {
00099 public:
00103     Projector() :
00104         m_horizontalAperture(112.0),
00105         m_verticalAperture(94.0),
00106         m_halfImageWidth(0.0),
00107         m_halfImageHeight(0.0),
00108         m_inverseEyeMatrix(),
00109         m_imagePoint()
00110     {
00111         // Computing the width and height of the image we would obtain if the
00112         // focal distance was 1.0
00113         m_halfImageWidth = 1.0 * tan(deg2rad(m_horizontalAperture / 2.0));
00114         m_halfImageHeight = 1.0 * tan(deg2rad(m_verticalAperture / 2.0));
00115     };
00116 
00124     void setEyeMatrix(const wMatrix &eye)
00125     {
00126         m_inverseEyeMatrix = eye.inverse();
00127     }
00128 
00135     void set3DPointWorld(const wVector &point)
00136     {
00137         set3DPointEye(m_inverseEyeMatrix.transformVector(point));
00138     }
00139 
00146     void set3DPointEye(const wVector &point)
00147     {
00148         // Computing the position of the object on the image (remember
00149         // that the focal distance is 1.0). Also note that the upvector
00150         // for the camera is -x where x is the eye object x axis, so we
00151         // have to use -point.y to get the x position on the image and
00152         // -point.x to get the y position on the image. The formula
00153         // below is derived using a simple proportion
00154 
00155         m_imagePoint.x = -point.y / point.z;
00156         m_imagePoint.y = -point.x / point.z;
00157 
00158         if(point.z >= 0)
00159             m_imagePoint.valid = true;
00160         else
00161             m_imagePoint.valid = false;
00162     }
00163 
00171     const ImagePoint& getImagePoint() const
00172     {
00173         return m_imagePoint;
00174     }
00175 
00185     ImagePoint getImagePoint01() const
00186     {
00187         ImagePoint p((m_imagePoint.x + m_halfImageWidth) / (2.0 * m_halfImageWidth), (m_imagePoint.y + m_halfImageHeight) / (2.0 * m_halfImageHeight));
00188         return p;
00189     }
00190 
00196     bool pointInsideImage() const
00197     {
00198         return (m_imagePoint.x <= m_halfImageWidth) && (m_imagePoint.x >= -m_halfImageWidth) &&
00199                (m_imagePoint.y <= m_halfImageHeight) && (m_imagePoint.y >= -m_halfImageHeight)
00200                && m_imagePoint.valid;   //if not, works also for z < 0
00201     }
00202 
00203 private:
00207     double m_horizontalAperture;
00208 
00212     double m_verticalAperture;
00213 
00221     double m_halfImageWidth;
00222 
00230     double m_halfImageHeight;
00231 
00239     wMatrix m_inverseEyeMatrix;
00240 
00245     ImagePoint m_imagePoint;
00246 };
00247 
00248 } //end namespace farsa
00249 
00250 #endif