Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CoordinateTransform3D.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 __BIAS_CoordSystsemTransform3D_hh__
27 #define __BIAS_CoordSystsemTransform3D_hh__
28 
29 // never include this unless it is absolutely needed:
30 // #include "bias_config.h"
31 
32 #include <Base/Common/BIASpragmaStart.hh>
33 #include <Base/Debug/Debug.hh>
34 #include <Base/Geometry/Quaternion.hh>
35 #include <Base/Math/Matrix4x4.hh>
36 #include <Base/Geometry/HomgPoint3D.hh>
37 #include <Base/Geometry/HomgPoint3DCov.hh>
38 #include <Base/Math/Vector3.hh>
39 
40 namespace BIAS {
41 
42 
43  /** @class CoordinateTransform3D
44 
45  @brief Transforms 3d points between two different coodinate systems, e.g.
46  from camera to world coordinates using the camera pose.
47 
48  We define a local coordinate system LCS (i.e. camera coordinate system)
49  and a global coordinate system GCS (i.e. world coordinate system).
50  The transformation from local to global coordinates is defined by matrix
51 
52  [ C_[0] ] <br>
53  [ scale_*R C_[1] ] <br>
54  T = [ C_[2] ] = invTransform_ <br>
55  [ 0 0 0 1 ] <br>
56 
57  This matrix converts a point X_L in LCS to its representation in GCS like
58 
59  X_G = T * X_L
60 
61  R is the matrix equivalent to unit quaternion Q.
62 
63  Obviously, X_L = T.Invert() * X_G
64 
65  T.invert() = [ R^T/scale_ -R^T*C_/scale_ ] = (*this) <br>
66  [ 0 0 0 1 ] <br>
67 
68  C is the origin of the local coordinate system in global coordinates,
69  Q (or R) tranforms a direction in local coordinates into a direction
70  in global coordinates.
71  The terms global and local are only used here to distinguish between
72  two arbitrary coordinate systems, neither of them has to be local/global.
73 
74  @attention do not confuse T with the matrix stored in (*this) (see above)!
75 
76  Apply this for e.g. hand-eye transformation, global-local coordinates,
77  camera to world coordinates.
78 
79  @ingroup g_geometry
80 
81  @author koeser/bartczak
82  @date 03/2006
83  */
84  class BIASGeometry_EXPORT CoordinateTransform3D :
85  protected Matrix4x4<double>
86  {
87  public:
88  /** @brief Set identity transformation. */
90  : Matrix4x4<double>(MatrixIdentity), Q_(0.0, 0.0, 0.0, 1.0),
91  C_(0.0, 0.0, 0.0), scale_(1.0), invTransform_(MatrixIdentity)
92  {}
93 
94  /** @brief Copy constructor */
96  {
97  *this = c;
98  }
99 
100  /** @brief Copy all elements of another instance. */
101  const CoordinateTransform3D&
103  Q_= c.Q_;
104  C_ = c.C_;
105  scale_ = c.scale_;
106  invTransform_ = c.invTransform_;
108  return (*this);
109  }
110 
111  /** @brief Set 3x4 matrix which transforms points in global coordinate system
112  to Euclidean points in local coordinate system.
113  @attention Use only for matrices that can not be decomposed properly,
114  otherwise use Set method!
115  */
116  void SetFromMatrix3x4(const Matrix3x4<double> &M);
117 
118  ///** @brief Set rotation and translation from pose object. */
119  //void SetFromPose(const Pose &pose);
120 
121  /** @brief Get 3x4 matrix which transforms points in global coordinate system
122  to Euclidean points in local coordinate system. */
123  Matrix3x4<double> GetMatrix3x4() const;
124 
125  /** @brief Get 4x4 matrix which transforms points in global coordinate system
126  to homogeneous points in local coordinate system. */
127  Matrix4x4<double> GetMatrix4x4() const;
128 
129  /** @brief Set orientation of local coordinate system from unit quaternion.
130  @see CoordinateTransform3D::Set() */
131  void SetQ(const Quaternion<double>& Q) { Q_ = Q; UpdateMatrix_(); }
132 
133  /** @brief Set orientation of local coordinate system from rotation matrix.
134  @see CoordinateTransform3D::Set() */
135  void SetR(const BIAS::RMatrixBase &R);
136 
137  /** @brief Set origin of local coordinate system in global coordinates.
138  @see CoordinateTransform3D::Set() */
139  void SetC(const Vector3<double>& C) { C_ = C; UpdateMatrix_(); }
140 
141  /** @brief Define local coordinate frame from within the world frame.
142  @param[in] Q Specifies the orientation of the local frame in
143  the world frame base:
144  Q*(global base vecor)*Q_conj = local base vector
145  @param[in] C Specifies the position of the origin of the local frame
146  in the world coordinate frame:
147  global base origin + C_ = local base origin
148  @param[in] scale Specifies the isotropic scale of base vector length
149  to get local base vector length:
150  ||global base vector||*scale = ||local base vector||
151  */
152  virtual void Set(const Quaternion<double>& Q, const Vector3<double>& C,
153  const double& scale = 1.0) {
154  Q_ = Q; C_ = C; scale_ = scale; UpdateMatrix_();
155  }
156 
157  /** @brief Set the length of the local coordinate axes in the world
158  coordinate frame. */
159  virtual inline void SetScale(const double& scale) {
160  scale_ = scale;
161  UpdateMatrix_();}
162 
163  /** @brief Transform point from world frame to local coordinate frame.
164  @param[in] X_G Homogeneous 3d point within world coordinate system
165  @return Returns homogeneous 3d point within local coordinate system
166  */
168  Vector4<double> X_L;
169  this->Mult(X_G, X_L);
170  return X_L;
171  }
172 
173  /** @brief Transform point from local frame to world coordinate frame.
174  @param[in] X_L Homogeneous 3d point within local coordinate system
175  @return Returns homogeneous 3d point within world coordinate system
176  */
178  Vector4<double> X_G;
179  invTransform_.Mult(X_L, X_G);
180  return X_G;
181  }
182 
183  /** @brief Transform point and its covariance matrix from world frame to
184  local coordinate frame.
185  @param[inout] X_G Gets homogeneous 3d point within world coordinate system,
186  returns 3d point within local coordinate system,
187  @param[inout] Cov_G Gets homogeneous 3d covariance within world frame,
188  returns covariance matrix in local coordinate system.
189  */
191  HomgPoint3DCov& Cov_G) const {
192  Vector4<double> X(X_G);
193  this->Mult(X, X_G);
194  Cov_G = (*this) * Cov_G * (this->Transpose());
195  }
196 
197  /** @brief Transform point and its covariance matrix from local frame to
198  world coordinate frame.
199  @param[inout] X_L Gets homogeneous 3d point within local coordinate system,
200  returns 3d point within wordl coordinate system,
201  @param[inout] Cov_L Gets homogeneous 3d covariance within local frame,
202  returns covariance matrix in world coordinate system.
203  */
205  HomgPoint3DCov& Cov_L) const {
206  HomgPoint3D X(X_L);
207  invTransform_.Mult(X, X_L);
208  Cov_L = invTransform_ * Cov_L * invTransform_.Transpose();
209  }
210 
211  /** @brief Get the length of the local coordinate axes in the world
212  coordinate frame.
213  @see CoordinateTransform3D::Set()
214  */
215  const double& GetScale() const { return scale_; }
216 
217  /** @brief Get orientation of local coordinate system as unit quaternion
218  mapping from local coordinates to global coordinates.
219  @see CoordinateTransform3D::Set()
220  */
221  const Quaternion<double>& GetQ() const { return Q_; }
222 
223  /** @brief Set origin of local coordinate system in global coordinates.
224  @see CoordinateTransform3D::Set()
225  */
226  const Vector3<double>& GetC() const { return C_; }
227 
228  /** @brief Get orientation and origin of local coordinate system as a
229  single vector containing first a 3d vector [c_x, c_y, c_z],
230  then a unit quaternion [q_x, q_y, q_z, q_w].
231  @see CoordinateTransform3D::Set()
232  */
233  inline const Vector<double> GetCQ() const {
234  Vector<double> res(7);
235  for (int i=0; i<3; i++){
236  res[i] = C_[i]; res[i+3] = Q_[i];
237  }
238  res[6] = Q_[3];
239  return res;
240  }
241 
242  /** @brief Get orientation of local coordinate system as rotation matrix
243  mapping from local coordinates to global coordinates.
244  @see CoordinateTransform3D::Set()
245  */
246  inline BIAS::RMatrixBase GetR() const {
247  RMatrixBase R;
248  R.SetFromQuaternion(GetQ());
249  return R;
250  }
251 
252  /** @brief Get 4x4 matrix which transforms points in global coordinate system
253  to homogeneous points in local coordinate system.
254  */
256  { return *this; }
257 
258  /** @brief Get 4x4 matrix which transforms points in local coordinate system
259  to homogeneous points in global coordinate system.
260  */
262  { return invTransform_; }
263 
264  friend std::ostream& operator<<(std::ostream &os,
265  const CoordinateTransform3D& p);
266 
267  friend std::istream& operator>>(std::istream &is,
269 
270  /** @brief Load 3d coordinate transformation from a text file.
271  The file format is specified by:
272 
273  Q_= <q_x> <q_y> <q_z> <q_w> C_= <c_x> <c_y> <c_z> scale_= <s>
274 
275  where the 3d vector [c_x, c_y, c_z] specifies the origin and
276  the unit quaternion [q_x, q_y, q_z, q_w] the orientation of
277  the local coordinate frame within the global coordinate frame.
278  @param[in] inputFile Name of file to read from
279  @return Returns false if reading from file failed, true otherwise.
280  */
281  bool Load(const std::string& inputFile);
282 
283  /** @brief Write 3d coordinate transformation to a text file.
284  @see CoordinateTransform3D::Load()
285  @param[in] outputFile Name of file to write to
286  @return Returns false if writing to file failed, true otherwise.
287  */
288  bool Save(const std::string& outputFile);
289 
290  /** @brief Calculates transformation from global system of this instance
291  to the local coordinate system of the given instance.
292  If this instance defines [global <-> local] and parameter
293  localT defines [local <-> local'] then the result defines
294  [global <-> local'].
295  @param[in] localT Specifies transformation [local <-> local']
296  @param[out] res Returns concatenated transformation [global <-> local']
297 
298  */
299  void ConcatenateLocalTransform(const CoordinateTransform3D& localT,
300  CoordinateTransform3D& res) const;
301 
302  /** @brief Changes this coordinate transformation so that parameter
303  newLocal becomes the local coordinate frame and parameter
304  newGlobal becomes the global coordinate system.
305 
306  It is assumed that newLocal and newGlobal are related through the same
307  global frame i.e.: A world point p_world is described by the
308  coordinates p_newGlobal and p_newLocal so that:
309 
310  p_world = T_newLocal * p_newLocal = T_newGlobal * p_newGLobal
311 
312  It follows that p_newGlobal (which is will become the new global
313  coordinate frame point) is computed by:
314 
315  p_newGlobal = T_newGlobal^-1 * T_newLocal * p_newLocal
316 
317  If newLocal defines [global <-> local] and newGlobal defines
318  [global <-> local'] then this instance becomes [local' -> local].
319 
320  @param[in] newLocal Specifies transformation [global <-> local]
321  @param[in] newGlobal Specifies transformation [global <-> local']
322  */
323  void BecomeRelativeTransform(const CoordinateTransform3D& newLocal,
324  const CoordinateTransform3D& newGlobal);
325 
326  /** @brief Swaps the global and local coordinate frame. */
327  void InvertIP() {
328  Q_.Invert(); scale_ = 1.0/scale_;
329  C_ = -scale_ * Q_.MultVec(C_);
330  UpdateMatrix_(); }
331 
332  /** @brief Applies the passed rotation to the orientation of the local frame.
333  Convenience wrapper, which simulates
334  \verbatim
335  LocalCoordTrsf.Set(Q);
336  LocalCoordTrsf.Set(Vector3<double>(0,0,0));
337  this->ConcatenateLocalTransform(Q, *this);
338  \endverbatim
339  */
340  void RotateLocalFrame(const Quaternion<double>& Q);
341 
342  /** @brief Prints this transformation with more intuitive rotation representation. */
343  void Talk() const;
344 
345  protected:
346 
347  /** @brief Computes the 4x4 transformation matrix from the currently set
348  unit quaternion Q_ and translation vector C_ */
349  virtual void UpdateMatrix_();
350 
351  /** @brief Specifies the rotation from local to global coordinate frame */
353 
354  /** @brief Specifies the translation from local to global coordinate frame */
356 
357  /** @brief Specifies isometric scaling from local to global coordinate frame */
358  double scale_;
359 
360  /** @brief Specifies the homogeneous matrix representation of the transformation
361  from local to global coordinate frame
362  @attention This transformation is considered as "inverse" since the
363  transformation from global to local is considered as the
364  reference transformation direction!
365  */
367  };
368 
369  /** @brief Write 3d coordinate transformation to stream.
370  @see CoordinateTransform3D::Save()
371  */
372  inline std::ostream& operator<<(std::ostream &os,
373  const CoordinateTransform3D& p) {
374  os<<"Q_= "<<p.Q_<<" C_= "<<p.C_<<" scale_= "<<p.scale_;
375  return os;
376  }
377 
378  /** @brief Read 3d coordinate transformation from stream.
379  @see CoordinateTransform3D::Load()
380  */
381  inline std::istream& operator>>(std::istream &is,
383  std::string temp;
384  is>>temp>>p.Q_>>temp>>p.C_>>temp>>p.scale_;
385  return is;
386  }
387 
388 }
389 
390 #include <Base/Common/BIASpragmaEnd.hh>
391 
392 #endif
393 
BIAS::RMatrixBase GetR() const
Get orientation of local coordinate system as rotation matrix mapping from local coordinates to globa...
virtual void Set(const Quaternion< double > &Q, const Vector3< double > &C, const double &scale=1.0)
Define local coordinate frame from within the world frame.
void InvertIP()
Swaps the global and local coordinate frame.
void LocalToGlobal(HomgPoint3D &X_L, HomgPoint3DCov &Cov_L) const
Transform point and its covariance matrix from local frame to world coordinate frame.
Matrix4x4< double > GetLocalToGlobalTransform() const
Get 4x4 matrix which transforms points in local coordinate system to homogeneous points in global coo...
const Vector3< double > & GetC() const
Set origin of local coordinate system in global coordinates.
const Quaternion< double > & GetQ() const
Get orientation of local coordinate system as unit quaternion mapping from local coordinates to globa...
void SetQ(const Quaternion< double > &Q)
Set orientation of local coordinate system from unit quaternion.
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
const double & GetScale() const
Get the length of the local coordinate axes in the world coordinate frame.
Matrix4x4< double > invTransform_
Specifies the homogeneous matrix representation of the transformation from local to global coordinate...
double scale_
Specifies isometric scaling from local to global coordinate frame.
CoordinateTransform3D(const CoordinateTransform3D &c)
Copy constructor.
HomgPoint3D GlobalToLocal(const HomgPoint3D &X_G) const
Transform point from world frame to local coordinate frame.
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
Vector3< double > C_
Specifies the translation from local to global coordinate frame.
CoordinateTransform3D()
Set identity transformation.
Matrix4x4 Transpose() const
Definition: Matrix4x4.hh:315
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 SetC(const Vector3< double > &C)
Set origin of local coordinate system in global coordinates.
class representing the covariance matrix of a homogenous point 3D
HomgPoint3D LocalToGlobal(const HomgPoint3D &X_L) const
Transform point from local frame to world coordinate frame.
virtual void SetScale(const double &scale)
Set the length of the local coordinate axes in the world coordinate frame.
Matrix4x4< double > GetGlobalToLocalTransform() const
Get 4x4 matrix which transforms points in global coordinate system to homogeneous points in local coo...
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
Matrix4x4 & operator=(const Matrix< T > &mat)
assignment operators calling corresponding operator from base class &quot;TNT::Matrix&quot; if appropriate ...
Definition: Matrix4x4.cpp:61
void GlobalToLocal(HomgPoint3D &X_G, HomgPoint3DCov &Cov_G) const
Transform point and its covariance matrix from world frame to local coordinate frame.
Quaternion< double > Q_
Specifies the rotation from local to global coordinate frame.
const Vector< double > GetCQ() const
Get orientation and origin of local coordinate system as a single vector containing first a 3d vector...
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157