Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProjectionParametersSphericalSimple.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 #ifndef __BIAS_ProjectionParametersSphericalSimple_hh__
22 #define __BIAS_ProjectionParametersSphericalSimple_hh__
23 
24 #include <bias_config.h>
25 
26 //including BIASpragmaEnd at end of file implies including BIASpragmaStart at first
27 #include <Base/Common/BIASpragmaStart.hh>
28 
29 #include <Geometry/ProjectionParametersBase.hh>
30 
31 
32 namespace BIAS {
33 
34  /** @class ProjectionParametersSphericalSimple
35  * @test tested with TestProjectionParametersSphericalSimple.cpp
36  @brief projection parameters
37  camera parameters which define the mapping between rays in the
38  camera coordinate system and pixels in the image as well as external pose.
39  the parameters phiOffset and thetaOffset are considered to be intrinsic
40  parameters describing the rotation of the principal point
41  the parameters dPhi and dTheta describe the resolution of a pixel in
42  radian per pixel.
43  Distortion is ignored.
44  This class is intended to describe spherical rectification results. */
45  class BIASGeometry_EXPORT ProjectionParametersSphericalSimple
47  {
48  public:
49 
51 
52  ProjectionParametersSphericalSimple(const double phiOffs,
53  const double thetaOffs,
54  const double dPhi,
55  const double dTheta,
56  const unsigned int width,
57  const unsigned int height);
58 
61  { *this=P; }
62 
64 
65  /** @brief position of image on sphere in phi direction
66  can also considered to be rotation of the principal point. */
67  virtual double GetPhiOffset() const { return phiOffset_;};
68 
69  /** @brief position of image on sphere in theta direction */
70  virtual double GetThetaOffset() const { return thetaOffset_;};
71 
72  /** @brief set resolution in radian/pixel for phi direction*/
73  virtual double GetDPhi() const { return dPhi_;};
74 
75  /** @brief set resolution in radian/pixel for theta direction*/
76  virtual double GetDTheta() const { return dTheta_;};
77 
78  /** @brief calculates the projection of a point
79  in the local camera coordinate system
80  to a pixel in the image plane of the camera
81 
82  In the simplest case perspective pinhole projection x = K * point
83  where point is transformed of X using point = (R^T | -R^T C) X */
84  virtual HomgPoint2D ProjectLocal(const Vector3<double>& point,
85  bool IgnoreDistortion = false) const;
86 
87  /** @brief calculates the projection of a point in the local camera
88  coordinate system to a pixel in the image plane of the camera. The
89  only case when this function may not compute the 2d point is when the
90  camera center and the 3d point coincide. This case must be indicated
91  by a negative return value. In all other cases, a 2d point must be
92  computed, *particularily* when the 3d point is behind the camera
93  or when the resulting 2d point is at infinity.
94 
95  In the simplest case perspective pinhole projection x = K * point
96  where point is transformed of X using point = (R^T | -R^T C) X
97  @author woelk 08/2008 (c) www.vision-n.de */
98  virtual int ProjectLocal(const Vector3<double>& point, HomgPoint2D &p2d,
99  bool IgnoreDistortion = false) const;
100 
101  /** @brief calculates the unit size viewing ray from the camera center
102  (in the camera coordinate system) which belongs to the image position
103  pos e.g. pos=principal point results in (0,0,1)=optical axis */
104  virtual void UnProjectLocal(const HomgPoint2D& pos, Vector3<double>& pointOnRay,
105  Vector3<double>& direction,
106  bool IgnoreDistortion = false) const;
107 
108  /** @brief map points from image onto unit diameter image plane in 3D.*/
109  virtual HomgPoint3D UnProjectToImagePlane(const HomgPoint2D& pos,
110  const double& depth = 1.0,
111  bool IgnoreDistortion = false)
112  const;
113 
114  virtual bool Distort(BIAS::HomgPoint2D& point2d) const { return false; };
115 
116  virtual bool Undistort(BIAS::HomgPoint2D& point2d) const { return false; };
117 
119  { return new ProjectionParametersSphericalSimple(*this); };
120 
121  void Rescale(double ratio, const double offset);
122 
123  void Rescale(unsigned int width, unsigned int height);
124 
125  /** Checks if 3D point projects into specified image and returns
126  * belonging 2D image point.
127  * @param localX assumes homogenized point! */
128  virtual bool
129  DoesPointProjectIntoImageLocal(const Vector3<double>& localX,
130  HomgPoint2D& x,bool IgnoreDistort) const;
131 
132  ////////////////////////////////////////////////////////////////////
133  // convenience functions
134 
135  /** Transform from a semilocal reference frame to pixel coo.
136  The semilocal reference frame is defined to have its origin at
137  the camera center and to have the same orientation as the full
138  global reference frame */
139  inline void GlobalOrientation2Pixel(const Vector3<double>& p2d,
140  Vector2<double>& pixel) const;
141 
142  /** Transform from pixel coo to semilocal reference frame.
143  The semilocal reference frame is defined to have its origin at
144  the camera center and to have the same orientation as the full
145  global reference frame */
146  inline void Pixel2GlobalOrientation(const Vector2<double>& pixel,
147  Vector3<double>& p2d) const;
148 
149 
150  inline void GlobalOrientation2Angles(const Vector3<double>& p2d,
151  double &phi, double &theta) const;
152 
153 
154  inline void Angles2GlobalOrientation(const double &phi,
155  const double &theta,
156  Vector3<double>& p2d) const;
157 
158  /** Transform from local camera centric reference system to pixel coo */
159  inline void Local2Pixel(const Vector3<double>& p2d,
160  Vector2<double>& pixel) const;
161 
162  /** Transform from pixel coo to local reference frame */
163  inline void Pixel2Local(const Vector2<double>& pixel,
164  Vector3<double>& p2d) const;
165 
166  /** Transform sphere angels to local reference frame */
167  inline void Local2Angles(const Vector3<double>& local,
168  double &phi, double &theta) const;
169 
170  /** Transform local camera centric coos to sphere angles */
171  inline void Angles2Local(const double &phi, const double &theta,
172  Vector3<double>& local) const;
173 
174  /** compute the angles belonging to pixel coos */
175  inline void Pixel2Angles(const Vector2<double>& pixel, double &phi,
176  double &theta) const;
177 
178  /** compute the angles belonging to pixel coos */
179  inline void Angles2Pixel(const double &phi, const double &theta,
180  Vector2<double>& pixel) const;
181 
182 
183 #ifdef BIAS_HAVE_XML2
184  /** @brief specialization of XML block name function */
185  virtual int
186  XMLGetClassName(std::string& TopLevelTag, double& Version) const;
187 
188  /** @brief specialization of XML write function */
189  virtual int XMLOut(const xmlNodePtr Node, XMLIO& XMLObject) const;
190 
191  /** @brief specialization of XML read function */
192  virtual int XMLIn(const xmlNodePtr Node, XMLIO& XMLObject);
193 #endif
194 
195  ///////////////////
196  // ensure that R_ stays consistent with Pose_
197  /////////////////
198 
199  virtual void SetPoseParametrization(const PoseParametrization& pp);
200 
201  virtual void SetQ(const BIAS::Quaternion<double>& Q);
202 
203  virtual void SetQC(const BIAS::Quaternion<double>& Q,
204  const BIAS::Vector3<double>& C);
205 
206  virtual void SetPose(const BIAS::Pose pose);
207 
208  virtual void SetR(const BIAS::RMatrix& R);
209 
210  virtual void ValidatePose();
211 
213  { return R_; }
214 
215  /// friend functions
216  friend std::ostream& operator<<(std::ostream &os,
218  protected:
219  double phiOffset_;
220  double thetaOffset_;
221  double dPhi_;
222  double dTheta_;
223  /// a cache for the orientation of the camera
225  };
226 
227  /////////////////////////////////////////
228  // inline code
229  /////////////////////////////////////////
230 
231 
232  inline std::ostream& operator<<(std::ostream &os,
234  {
235  os << "ProjectionParametersSphericalSimple:" << std::endl;
236  os << "- phiOffset = " << p.phiOffset_ << std::endl;
237  os << "- thetaOffset = " << p.thetaOffset_ << std::endl;
238  os << "- dPhi = " << p.dPhi_ << std::endl;
239  os << "- dTheta = " << p.dTheta_ << std::endl;
240  os << "- R = " << p.R_ << std::endl;
241  return os;
242  }
243 
246  Vector2<double>& pixel) const
247  {
248  BIASASSERT(QValid_);
249  BIAS::Vector3<double> local;
250  R_.TransposedMult(p2d, local);
251  Local2Pixel(local, pixel);
252  }
253 
254 
257  Vector3<double>& p2d) const
258  {
259  BIASASSERT(QValid_);
260  Vector3<double> local;
261  Pixel2Local(pixel, local);
262  R_.Mult(local, p2d);
263  }
264 
267  double &phi, double &theta) const
268  {
269  BIASASSERT(QValid_);
270  BIAS::Vector3<double> local;
271  R_.TransposedMult(p2d, local);
272  Local2Angles(local, phi, theta);
273  }
274 
275 
277  Angles2GlobalOrientation(const double &phi, const double &theta,
278  Vector3<double>& p2d) const
279  {
280  BIASASSERT(QValid_);
281  BIAS::Vector3<double> local;
282  Angles2Local(phi, theta, local);
283  R_.Mult(local, p2d);
284  }
285 
286 
289  {
290  double phi, theta;
291  Local2Angles(p2d, phi, theta);
292  Angles2Pixel(phi, theta, pixel);
293  }
294 
295 
298  {
299  double phi, theta;
300  Pixel2Angles(pixel, phi, theta);
301  Angles2Local(phi, theta, p2d);
302  }
303 
304 
306  Local2Angles(const Vector3<double>& local, double &phi, double &theta) const
307  {
308  double r = local.NormL2();
309  if (Equal(r, 0.0)) { BEXCEPTION("cannot transform vector of zero length"); }
310  double x = local[0] / r;
311  double y = local[1] / r;
312  double z = local[2] / r;
313  theta = acos(x);
314  double sin_theta = sin(theta);
315  if (Equal(sin_theta, 0.)){
316  phi = 0.;
317  } else {
318  phi = atan2(y/sin_theta, z/sin_theta);
319  }
320  }
321 
322 
324  Angles2Local(const double &phi, const double &theta,
325  Vector3<double>& local) const
326  {
327  double x = cos(theta);
328  double y = sin(phi)*sin(theta);
329  double z = cos(phi)*sin(theta);
330  local.Set(x, y, z);
331  }
332 
333 
335  Pixel2Angles(const Vector2<double>& pixel, double &phi, double &theta) const
336  {
337  theta = pixel[0] * dTheta_ + thetaOffset_;
338  phi = pixel[1] * dPhi_ + phiOffset_;
339  }
340 
341 
343  Angles2Pixel(const double &phi, const double &theta,
344  Vector2<double>& pixel) const
345  {
346  // deal with periodicy of phi and theta
347  // phi must be in range between -pi and +pi
348  // theta must be in range [0, pi]
349  const double pi = M_PI;
350  const double two_pi = 2. * M_PI;
351  double mphi(phi), mtheta(theta);
352  while (mphi<-pi) { mphi += two_pi; }
353  while (mphi>pi) { mphi -= two_pi; }
354  while (mtheta<0.) { mtheta += two_pi; }
355  while (mtheta>two_pi) { mtheta -= two_pi; }
356  if (mtheta>pi){
357  BEXCEPTION("invalid theta: "<<mtheta<<"\n must be in [0, PI]");
358  }
359  pixel[0] = (mtheta - thetaOffset_) / dTheta_;
360  pixel[1] = (mphi - phiOffset_) / dPhi_;
361 
362  }
363 } // end namespace
364 
365 
366 #include <Base/Common/BIASpragmaEnd.hh>
367 
368 #endif
369 
virtual double GetThetaOffset() const
position of image on sphere in theta direction
void Local2Pixel(const Vector3< double > &p2d, Vector2< double > &pixel) const
Transform from local camera centric reference system to pixel coo.
virtual bool Distort(BIAS::HomgPoint2D &point2d) const
Interface defintion for lens distortion function, implemented by derived classes. ...
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
virtual double GetDPhi() const
set resolution in radian/pixel for phi direction
void GlobalOrientation2Angles(const Vector3< double > &p2d, double &phi, double &theta) const
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
projection parameters camera parameters which define the mapping between rays in the camera coordinat...
BIAS::RMatrixBase R_
a cache for the orientation of the camera
void TransposedMult(const Vector3< T > &argvec, Vector3< T > &destvec) const
multiplies matrix from left with transposed argvec, resulting in transposed destvec ...
Definition: Matrix3x3.hh:323
Slim class bundeling pose parametrization and associated covariance matrix.
Down-, Upsampling routines and Resize.
Definition: Rescale.hh:71
void Pixel2Angles(const Vector2< double > &pixel, double &phi, double &theta) const
compute the angles belonging to pixel coos
void Angles2Local(const double &phi, const double &theta, Vector3< double > &local) const
Transform local camera centric coos to sphere angles.
3D rotation matrix
Definition: RMatrix.hh:49
void Local2Angles(const Vector3< double > &local, double &phi, double &theta) const
Transform sphere angels to local reference frame.
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
void Mult(const Vector3< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector3, storing the result in destvec calculates: dest...
Definition: Matrix3x3.hh:302
void GlobalOrientation2Pixel(const Vector3< double > &p2d, Vector2< double > &pixel) const
Transform from a semilocal reference frame to pixel coo.
virtual ProjectionParametersBase * Clone() const
Covariant virtual copy constructor used in BIAS::Projection.
ProjectionParametersSphericalSimple(const ProjectionParametersSphericalSimple &P)
virtual double GetDTheta() const
set resolution in radian/pixel for theta direction
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
void Angles2GlobalOrientation(const double &phi, const double &theta, Vector3< double > &p2d) const
void Pixel2GlobalOrientation(const Vector2< double > &pixel, Vector3< double > &p2d) const
Transform from pixel coo to semilocal reference frame.
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
void Pixel2Local(const Vector2< double > &pixel, Vector3< double > &p2d) const
Transform from pixel coo to local reference frame.
virtual bool Undistort(BIAS::HomgPoint2D &point2d) const
Interface defintion for lens undistortion function, implemented by derived classes.
void Angles2Pixel(const double &phi, const double &theta, Vector2< double > &pixel) const
compute the angles belonging to pixel coos
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual double GetPhiOffset() const
position of image on sphere in phi direction can also considered to be rotation of the principal poin...
bool QValid_
validity flag for orientation and position
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633