Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SimilarityTransform.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_SimilarityTransformation3D_hh__
27 #define __BIAS_SimilarityTransformation3D_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/Math/Vector3.hh>
38 
39 namespace BIAS {
40 
41  /** @class SimilarityTransform
42  @brief Implements a 3D similarity transformation (rotation, translation,
43  and isometric scaling).
44 
45  Similarity transformations can be interpreted either as a coordinate
46  transformation or motion and scaling of points. Euclidean transformations
47  are special similiarty transformations with scale = 1.
48 
49  This transformation is identical to the following matrix:
50 
51  [ C_[0] ]
52  [ scale_*R C_[1] ]
53  T = [ C_[2] ]
54  [ 0 0 0 1 ]
55 
56  The internal representation of the rotation is a unit quaternion Q_.
57  This quaternion, a translation vector and a scale are used to parametrize
58  the similarity with 8 parameters (and one constraint Q_.NormL2() == 1).
59  Additional, a covariance matrix for all parameters can be stored.
60 
61  Can be used to represent poses, rigid motions or coordinate transforms.
62 
63  @author grest 05/2006
64  */
65 class BIASGeometry_EXPORT SimilarityTransform : public BIAS::Debug
66 {
67 public:
68 
69  /** @brief Creates the identity transformation with zero covariance. */
71  : Q_(0.0, 0.0, 0.0, 1.0),
72  C_(0.0,0.0,0.0), scale_(1.0) {}
73 
74  /** @brief Copy constructor, calls operator= */
76  *this = c;
77  }
78 
79  /** @brief Copy all elements of another SimilarityTransform instance */
80  const SimilarityTransform&
82  Q_= c.Q_;
83  C_ = c.C_;
84  scale_ = c.scale_;
85  return (*this);
86  }
87 
88  /** @brief Set rotational part from unit quaternion Q */
89  inline void SetQ(const Quaternion<double>& Q) { Q_ = Q; }
90 
91  /** @brief Set rotational part from rotation matrix R */
92  inline void SetR(const BIAS::RMatrixBase &R)
93  { R.GetQuaternion(Q_); }
94 
95  /** @brief Set translational part from vector C */
96  inline void SetC(const Vector3<double>& C) { C_ = C; }
97 
98  /** @brief Set rotation, translation, and optional scaling */
99  inline void Set(const Quaternion<double>& Q, const Vector3<double>& C,
100  const double& scale = 1.0)
101  { Q_ = Q; C_ = C; scale_ = scale; }
102 
103  /** @brief Return unit quaternion representing rotation */
104  inline const Quaternion<double>& GetQ() const { return Q_; }
105 
106  /** @brief Return vector representing translation */
107  inline const Vector3<double>& GetC() const { return C_; }
108 
109  /** @brief Returns 4x4 transformation matrix corresponding to this */
110  Matrix4x4<double> GetTransformMatrix() const;
111 
112  /** @brief Compute rotation matrix from the unit quaternion representing
113  the rotational part of this */
114  inline BIAS::RMatrixBase GetR() const {
115  RMatrixBase R;
116  R.SetFromQuaternion(GetQ());
117  return R;
118  }
119 
120  /** @brief Return isometric scaling of this transformation */
121  inline double GetScale(){ return scale_; }
122 
123  friend std::ostream& operator<<(std::ostream &os, const SimilarityTransform& p);
124  friend std::istream& operator>>(std::istream &is, SimilarityTransform& p);
125 
126  /** @brief Load similarity transform from text file inputFile
127  @return Return true when loading was successful, false otherwise
128  */
129  bool Load(const std::string& inputFile);
130 
131  /** @brief Save similarity transform to text file outputFile
132  @return Return true when saving was successful, false otherwise
133  */
134  bool Save(const std::string& outputFile);
135 
136  /** @brief Invert the similarity transform (in place) */
137  inline void InvertIP() {
138  Q_.Invert(); scale_ = 1.0 / scale_;
139  C_ = -scale_ * Q_.MultVec(C_);
140  }
141 
142  /** @brief Concatenate this similarity transform and the given one so that
143  result = this * arg holds for the corresponding 4x4 matrices.
144  */
145  inline void Mult(const SimilarityTransform &arg, SimilarityTransform &result)
146  {
147  Q_.Mult(arg.Q_,result.Q_);
148  Q_.MultVec(arg.C_, result.C_);
149  result.C_ *= scale_;
150  result.C_+= C_;
151  result.scale_= scale_ * arg.scale_;
152  }
153 
154  /** @brief Transform the given vector with this similarity transform so that
155  result = this * vec holds for the corresponding 4x4 matrix.
156  */
157  inline void MultVec(const Vector3<double> &vec, Vector3<double> &result) const
158  { Q_.MultVec(vec, result); result *= scale_; result += C_; }
159 
160 
161  /** @brief Transform the given homogeneous 3D point with this similarity
162  transform so that result = this * vec holds for the corresponding
163  4x4 matrix. Computes the 4x4 transformation matrix internally.
164  */
165  inline void Mult(const HomgPoint3D &vec, HomgPoint3D &result) const
166  { GetTransformMatrix().Mult(vec, result); }
167 
168  /** @brief Print transformation together with more intuitive rotation
169  representations for debugging purposed.
170  */
171  void Dump();
172 
173 protected:
174 
175  /// this is the rotation
177 
178  /// the translation
180 
181  double scale_;
182 };
183 
184 inline std::ostream& operator<<(std::ostream &os,
185  const SimilarityTransform& p) {
186  os<<"Q_= "<<p.Q_<<" C_= "<<p.C_<<" scale_= "<<p.scale_;
187  return os;
188 }
189 
190 inline std::istream& operator>>(std::istream &is,
192  std::string temp;
193  is>>temp>>p.Q_>>temp>>p.C_>>temp>>p.scale_;
194  return is;
195 }
196 
198  SimilarityTransform &arg2)
199 {
200  SimilarityTransform result;
201  arg1.Mult(arg2,result);
202  return result;
203 }
204 
206  const BIAS::Vector3<double> &vec)
207 {
208  BIAS::Vector3<double> result;
209  arg1.MultVec(vec,result);
210  return result;
211 }
212 
213 
214 
215 } // end namespace
216 
217 
218 #include <Base/Common/BIASpragmaEnd.hh>
219 
220 #endif
221 
void MultVec(const Vector3< double > &vec, Vector3< double > &result) const
Transform the given vector with this similarity transform so that result = this * vec holds for the c...
Quaternion< double > Q_
this is the rotation
void SetR(const BIAS::RMatrixBase &R)
Set rotational part from rotation matrix R.
void Mult(const HomgPoint3D &vec, HomgPoint3D &result) const
Transform the given homogeneous 3D point with this similarity transform so that result = this * vec h...
Implements a 3D similarity transformation (rotation, translation, and isometric scaling).
void Set(const Quaternion< double > &Q, const Vector3< double > &C, const double &scale=1.0)
Set rotation, translation, and optional scaling.
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
void Mult(const SimilarityTransform &arg, SimilarityTransform &result)
Concatenate this similarity transform and the given one so that result = this * arg holds for the cor...
double GetScale()
Return isometric scaling of this transformation.
const SimilarityTransform & operator=(const SimilarityTransform &c)
Copy all elements of another SimilarityTransform instance.
void InvertIP()
Invert the similarity transform (in place)
BIAS::RMatrixBase GetR() const
Compute rotation matrix from the unit quaternion representing the rotational part of this...
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
SimilarityTransform(const SimilarityTransform &c)
Copy constructor, calls operator=.
Vector3< double > C_
the translation
SimilarityTransform()
Creates the identity transformation with zero covariance.
void SetC(const Vector3< double > &C)
Set translational part from vector C.
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
DualQuaternion< T > operator*(const DualQuaternion< T > &l, const T &scalar)
void SetQ(const Quaternion< double > &Q)
Set rotational part from unit quaternion Q.
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
const Quaternion< double > & GetQ() const
Return unit quaternion representing rotation.
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
const Vector3< double > & GetC() const
Return vector representing translation.