Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Pose.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __Pose_hh__
27 #define __Pose_hh__
28 
29 #include <bias_config.h>
30 #include <Base/Geometry/Quaternion.hh>
31 #include <Base/Geometry/HomgPoint3D.hh>
32 #include <Base/Geometry/HomgPoint3DCov.hh>
33 #include <Base/Math/Matrix4x4.hh>
34 #include <Base/Math/Matrix3x4.hh>
35 #include <Base/Geometry/RMatrixBase.hh>
36 #include <Geometry/CoordinateTransform3D.hh>
37 #include <Base/Geometry/PoseParametrization.hh>
38 #ifdef BIAS_HAVE_XML2
39 # include <Base/Common/XMLBase.hh>
40 #endif
41 
42 #define POSE_TYPE double
43 
44 namespace BIAS {
45 
46  /** @class Pose
47 
48  @brief Represents 3d pose transformations, parametrized as Euclidean
49  translation and unit quaternion orientation.
50 
51  Pose transformations are special 3d coordinate transformations without
52  scaling, extended by covariance matrices to measure uncertainty.
53 
54  They specify Euclidean transformations that can also be interpreted as
55  measureable entities, i.e. a (camera) pose is measured within in the
56  coordinate system A. In A the pose will define the transformation
57  from coordinates in A to the pose (camera) coordinate system. This
58  transformation does not contain scale. Imagine A being embedded in
59  another coordinate system B. In order to find the pose in B the method
60  TransformBy() can be used. The result will not be able to transform
61  coordinates from A into the pose system. This transform can be found
62  using the method CoordinateTransform3D::ConcatenateLocalTransform()
63  on a pose object.
64 
65  @see BIAS::CoordinateTransform3D
66 
67  @ingroup g_geometry
68 
69  @author woelk/koeser/bartczak
70  @date03/2006
71  */
72 
73  class BIASGeometry_EXPORT Pose : public CoordinateTransform3D
74 #ifdef BIAS_HAVE_XML2
75  , public XMLBase
76 #endif
77  {
78  public:
79 
80  /** @brief Default constructor creates identity pose transformation. */
81  Pose();
82 
83  ~Pose();
84 
85  /** @brief Set pose from pose parametrization. */
86  int Set(const PoseParametrization& pose);
87 
88  /** @brief Set rotation (resp. orientation) from unit quaternion and
89  translation (resp. origin) from Euclidean vector.
90  @attention Resets covariance matrix to zero!
91  */
92  void Set(const Quaternion<POSE_TYPE>& Q,
93  const Vector3<POSE_TYPE>& C);
94 
95  /** @brief Set rotation (resp. orientation) from unit quaternion and
96  translation (resp. origin) from Euclidean vector, uncertainty
97  is set from 7x7 covariance matrix containing Cov([C Q]).
98  */
99  void Set(const Quaternion<POSE_TYPE>& Q,
100  const Vector3<POSE_TYPE>& C,
101  const Matrix<POSE_TYPE>& cov);
102 
103  /** @brief Set translation (resp. origin) from Euclidean vector, and
104  set its uncertainty from 3x3 covariance matrix. */
105  void SetC(const Vector3<POSE_TYPE>& C,
106  const Matrix3x3<POSE_TYPE>& cov);
107 
108  //void SetC(const HomgPoint3D& C, const HomgPoint3DCov& cov);
109 
110  /** @brief Set rotation (resp. orientation) from unit quaternion, and
111  set its uncertainty from 4x4 covariance matrix. */
112  void SetQ(const Quaternion<POSE_TYPE>& Q,
113  const Matrix4x4<POSE_TYPE>& cov);
114 
115  /** @brief Set rotation (resp. orientation) from unit quaternion.
116  @attention Resets covariance matrix to zero! */
117  void SetQ(const Quaternion<POSE_TYPE>& Q);
118 
119 
120  /** @brief Set translation (resp. origin) from Euclidean vector.
121  @attention Resets covariance matrix to zero! */
122  void SetC(const Vector3<POSE_TYPE>& C);
123 
124  /** @brief Get rotation (resp. orientation) as rotation matrix. */
125  inline void SetR(const RMatrixBase& R)
126  {
128  R.GetQuaternion(Q);
129  SetQ(Quaternion<POSE_TYPE>(Q));
130  }
131 
132  /** @brief Get pose parametrization representing this pose. */
133  PoseParametrization GetPoseParameters() const;
134 
135  /** @brief Set covariance matrix explicitely from 7x7 matrix Cov([C Q]). */
136  int SetCovariance(const Matrix<POSE_TYPE>& c);
137 
138  /** @brief Set covariance matrix explicitely from an Euclidean covariance
139  matrix, i.e. a 6x6 matrix Cov([C alpha beta gamma]) where
140  alpha, beta, gamma are rotation angles.
141  @attention This method is not implemented yet!
142  */
143  int SetCovEuclidean(Matrix<POSE_TYPE> Cov);
144 
145  /** @brief Return the 7x7 covariance matrix Cov([C Q]). */
146  inline const Matrix<POSE_TYPE>& GetCovariance() const
147  { return Cov_; }
148 
149  /** @brief Return the 3x3 covariance matrix Cov(C) of the translation
150  part C of this pose transformation. */
151  Matrix3x3<POSE_TYPE> GetCovC() const;
152 
153  /** @brief Return the 4x4 covariance matrix Cov(Q) of the rotational part
154  of this pose transformation described by unit quaternion Q. */
155  Matrix4x4<POSE_TYPE> GetCovQ() const;
156 
157  /** @brief Load pose transformation from a text file using operator >>.
158  @param[in] inputFile Name of file to read from
159  @see CoordinateTransform3D::Load()
160  @return Returns false if reading from file failed, true otherwise.
161  */
162  bool Load(const std::string& inputFile);
163 
164  /** @brief Write pose transformation to a text file using operator <<.
165  @param[in] outputFile Name of file to write to
166  @see CoordinateTransform3D::Save()
167  @return Returns false if writing to file failed, true otherwise.
168  */
169  bool Save(const std::string& outputFile);
170 
171 #ifdef BIAS_HAVE_XML2
172  /** @brief Specialization of XML block name function. */
173  virtual int XMLGetClassName(std::string& TopLevelTag,
174  double& Version) const;
175 
176  /** @brief Specialization of XML write function. */
177  virtual int XMLOut(const xmlNodePtr Node, BIAS::XMLIO& XMLObject) const;
178 
179  /** @brief Specialization of XML read function. */
180  virtual int XMLIn(const xmlNodePtr Node, BIAS::XMLIO& XMLObject);
181 #endif
182 
183  friend std::ostream& operator<<(std::ostream &os, const Pose& p);
184 
185  friend std::istream& operator>>(std::istream &is, Pose& p);
186 
187  /** @brief Copy from another pose transformation instance. */
188  const Pose& operator=(const Pose& p) {
190  Cov_ = p.GetCovariance();
191  return (*this);
192  }
193 
194  /** @brief Transforms the position and orientation stored in this pose
195  into another global coordinate system, i.e.: trsf * this = new local
196  coordinate system.
197  @param[in] trsf Coordinate transformation describing the global coordinate
198  system for the current global coordinate system.
199  */
200  void TransformToNewGlobalBy(const CoordinateTransform3D& trsf);
201 
202  Vector3<double> GetZAxis() const;
203 
204  Vector3<double> GetXAxis() const;
205 
206  Vector3<double> GetYAxis() const;
207 
208  /** @brief Create pose of camera looking at given point, similar to gluLookAt().
209  @param[in] eye Specifies the eye coordinates
210  @param[in] center Specifies the 3d point to look at
211  @param[in] up Specifies the up vector of the camera
212  @return Returns true if everything went fine, otherwise false.
213  */
214  const inline bool LookAt(const Vector3<double> &eye, Vector3<double> center,
215  const Vector3<double> &up)
216  {
217  center.SubIP(eye);
218  bool retVal = CheckLookAt_(center, up);
219  RMatrixBase r;
220  r.SetFromOriUp(center, up);
221  SetR(r);
222  return retVal;
223  };
224 
225  /** @brief Create pose of camera looking at given point, similar to gluLookAt().
226  @attention The camera center is not changed here!
227  @param[in] center Specifies the 3d point to look at
228  @param[in] up Specifies the up vector of the camera
229  @return Returns true if everything went fine, otherwise false.
230  */
231  const inline bool LookAt(const Vector3<double> &center, const Vector3<double> &up)
232  {
233  return LookAt(GetC(), center, up);
234  };
235 
236  /** @brief Create pose of camera looking at given point, similar to gluLookAt().
237  @param[in] eye Specifies the eye coordinates
238  @param[in] center Specifies the 3d point to look at
239  @param[in] up Specifies the up vector of the camera
240  @return Returns true if everything went fine, otherwise false.
241  */
242  const inline bool LookAtGL(const Vector3<double> &eye, Vector3<double> center,
243  const Vector3<double> &up)
244  {
245  return LookAt(eye, center, up);
246  };
247 
248  /** @brief Looks at given point, similar to gluLookAt
249  @param center 3D point to look at
250  @param up Up vector of the camera
251  @return true, if everythings fine - false, otherwise
252  */
253  const inline bool LookAtGL(const Vector3<double> &center, const Vector3<double> &up){
254  return LookAtGL(GetC(), center, up);
255  };
256 
257  protected:
258 
259  //using CoordinateTransform3D::SetScale;
260 
261  /** @brief Scale can not be set for poses but is fixed as 1.
262  @attention Calling this method will result in a program abort!
263  */
264  virtual void SetScale(const double& scale) { BIASABORT; }
265 
266  /** @brief Checks constraints for parameter of LookAt(). */
267  inline bool CheckLookAt_(Vector3<double> &center, const Vector3<double> &up)
268  {
269  if(center.NormL2() < std::numeric_limits<double>::epsilon()){
270  BIASERR("Vectors eye and center are the same!")
271  return false;
272  }
273  if(fabs(up.NormL2() - 1.0) > std::numeric_limits<double>::epsilon()){
274  BIASERR("Vector up is not normalized!")
275  return false;
276  }
277  return true;
278  }
279 
280  /** @brief 7x7 covariance matrix for the 7-vector representation of
281  this pose transformation (first C, then Q!)*/
283  };
284 
285  /** @brief Write pose transformation to stream.
286  @see Pose::Save()
287  */
288  inline std::ostream& operator<<(std::ostream &os, const Pose& p) {
289  os<<"Q_= "<<p.Q_<<" C_= "<<p.C_<<" scale_= "<<p.scale_<<" Cov_="<<p.Cov_;
290  os<<"RAW";
291  for(int i=0; i < 4; i++){
292  for(int j=0; j < 4; j++){
293  os<<p[i][j]<< " ";
294  }
295  os<<std::endl;
296  }
297  return os;
298  }
299 
300  /** @brief Read pose transformation from stream.
301  @see Pose::Load()
302  */
303  inline std::istream& operator>>(std::istream &is, Pose& p){
304  std::string temp;
305  is>>temp>>p.Q_>>temp>>p.C_>>temp>>p.scale_>>temp>>p.Cov_;
306  if(is && p.scale_!=1.0) {
307  BIASERR("Loaded transformation is not a pose - resetting scale!\n");
308  p.scale_ = 1.0;
309  }
310  return is;
311  }
312 
313 }
314 
315 #endif
const bool LookAtGL(const Vector3< double > &center, const Vector3< double > &up)
Looks at given point, similar to gluLookAt.
Definition: Pose.hh:253
bool CheckLookAt_(Vector3< double > &center, const Vector3< double > &up)
Checks constraints for parameter of LookAt().
Definition: Pose.hh:267
Slim class bundeling pose parametrization and associated covariance matrix.
double scale_
Specifies isometric scaling from local to global coordinate frame.
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
void SetR(const RMatrixBase &R)
Get rotation (resp.
Definition: Pose.hh:125
int SetFromOriUp(BIAS::Vector3< ROTATION_MATRIX_TYPE > ori, BIAS::Vector3< ROTATION_MATRIX_TYPE > up)
Set rotation matrix from orientation and up vector.
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
const bool LookAt(const Vector3< double > &center, const Vector3< double > &up)
Create pose of camera looking at given point, similar to gluLookAt().
Definition: Pose.hh:231
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
Vector3< double > C_
Specifies the translation from local to global coordinate frame.
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
Base class with interface for xml output.
Definition: XMLBase.hh:56
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
Matrix< POSE_TYPE > Cov_
7x7 covariance matrix for the 7-vector representation of this pose transformation (first C...
Definition: Pose.hh:282
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
void SubIP(const T &scalar)
Substraction (in place) of an scalar.
Definition: Vector3.hh:318
virtual void SetScale(const double &scale)
Scale can not be set for poses but is fixed as 1.
Definition: Pose.hh:264
const bool LookAtGL(const Vector3< double > &eye, Vector3< double > center, const Vector3< double > &up)
Create pose of camera looking at given point, similar to gluLookAt().
Definition: Pose.hh:242
Transforms 3d points between two different coodinate systems, e.g.
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
const CoordinateTransform3D & operator=(const CoordinateTransform3D &c)
Copy all elements of another instance.
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
const bool LookAt(const Vector3< double > &eye, Vector3< double > center, const Vector3< double > &up)
Create pose of camera looking at given point, similar to gluLookAt().
Definition: Pose.hh:214
class for rotation with axis and angle
const Matrix< POSE_TYPE > & GetCovariance() const
Return the 7x7 covariance matrix Cov([C Q]).
Definition: Pose.hh:146
const Pose & operator=(const Pose &p)
Copy from another pose transformation instance.
Definition: Pose.hh:188
class BIASGeometry_EXPORT Pose
Quaternion< double > Q_
Specifies the rotation from local to global coordinate frame.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157