projector.h
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
4  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
7  * Gianluca Massera <emmegian@yahoo.it> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22  ********************************************************************************/
23 
24 #ifndef PROJECTOR_H
25 #define PROJECTOR_H
26 
27 #include "experimentsconfig.h"
28 #include "wmatrix.h"
29 #include "mathutils.h"
30 
31 namespace farsa {
32 
36 class FARSA_EXPERIMENTS_TEMPLATE ImagePoint {
37 public:
44  x(0.0),
45  y(0.0),
46  valid(false)
47  {
48  }
49 
57  ImagePoint(double _x, double _y) :
58  x(_x),
59  y(_y),
60  valid(true)
61  {
62  }
63 
69  bool isValid() const
70  {
71  return valid;
72  }
73 
77  double x;
78 
82  double y;
83 
87  bool valid;
88 };
89 
97 class FARSA_EXPERIMENTS_TEMPLATE Projector
98 {
99 public:
104  m_horizontalAperture(112.0),
105  m_verticalAperture(94.0),
106  m_halfImageWidth(0.0),
107  m_halfImageHeight(0.0),
108  m_inverseEyeMatrix(),
109  m_imagePoint()
110  {
111  // Computing the width and height of the image we would obtain if the
112  // focal distance was 1.0
113  m_halfImageWidth = 1.0 * tan(deg2rad(m_horizontalAperture / 2.0));
114  m_halfImageHeight = 1.0 * tan(deg2rad(m_verticalAperture / 2.0));
115  };
116 
124  void setEyeMatrix(const wMatrix &eye)
125  {
126  m_inverseEyeMatrix = eye.inverse();
127  }
128 
135  void set3DPointWorld(const wVector &point)
136  {
137  set3DPointEye(m_inverseEyeMatrix.transformVector(point));
138  }
139 
146  void set3DPointEye(const wVector &point)
147  {
148  // Computing the position of the object on the image (remember
149  // that the focal distance is 1.0). Also note that the upvector
150  // for the camera is -x where x is the eye object x axis, so we
151  // have to use -point.y to get the x position on the image and
152  // -point.x to get the y position on the image. The formula
153  // below is derived using a simple proportion
154 
155  m_imagePoint.x = -point.y / point.z;
156  m_imagePoint.y = -point.x / point.z;
157 
158  if(point.z >= 0)
159  m_imagePoint.valid = true;
160  else
161  m_imagePoint.valid = false;
162  }
163 
171  const ImagePoint& getImagePoint() const
172  {
173  return m_imagePoint;
174  }
175 
185  ImagePoint getImagePoint01() const
186  {
187  ImagePoint p((m_imagePoint.x + m_halfImageWidth) / (2.0 * m_halfImageWidth), (m_imagePoint.y + m_halfImageHeight) / (2.0 * m_halfImageHeight));
188  return p;
189  }
190 
196  bool pointInsideImage() const
197  {
198  return (m_imagePoint.x <= m_halfImageWidth) && (m_imagePoint.x >= -m_halfImageWidth) &&
199  (m_imagePoint.y <= m_halfImageHeight) && (m_imagePoint.y >= -m_halfImageHeight)
200  && m_imagePoint.valid; //if not, works also for z < 0
201  }
202 
203 private:
207  double m_horizontalAperture;
208 
212  double m_verticalAperture;
213 
221  double m_halfImageWidth;
222 
230  double m_halfImageHeight;
231 
239  wMatrix m_inverseEyeMatrix;
240 
245  ImagePoint m_imagePoint;
246 };
247 
248 } //end namespace farsa
249 
250 #endif