Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CoordinateTransform3D.cpp
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 #include <Geometry/CoordinateTransform3D.hh>
27 #include <MathAlgo/SVD.hh>
28 #include <Base/Geometry/RMatrixBase.hh>
29 #include <fstream>
30 #include <iostream>
31 #include <Base/Math/Matrix3x4.hh>
32 
33 using namespace BIAS;
34 using namespace std;
35 
36 
38  // delete previous data in matrix
39  this->SetIdentity();
40  invTransform_.SetIdentity();
41 
42  // compute rotational part
43  RMatrixBase R;
44  R.SetFromQuaternion(Q_);
45 
46  Vector3<double> RTC(R.Transpose()*C_);
47 
48  // copy rotation and translation into matrix
49  for (unsigned int row=0; row<3; row++) {
50  for (unsigned int column=0; column<3; column++) {
51  // *this Global -> Local
52  (*this)[row][column] = R[column][row] / scale_;
53 
54  //invTransform Local -> Global
55  invTransform_[column][row] = R[column][row] * scale_;
56  }
57  (*this)[row][3] = RTC[row] / scale_ * (-1.0);// *this Global -> Local
58  invTransform_[row][3] = C_[row]*scale_;//invTransform Local -> Global
59  }
60 }
61 
64  // delete previous data in matrix
65  this->SetIdentity();
66  invTransform_.SetIdentity();
67 
68  for (unsigned int row=0; row<3; row++) {
69  for (unsigned int column=0; column<4; column++) {
70  (*this)[row][column] = M[row][column];
71  }
72  }
73  // set this to defined values
74  Q_.Set(0,0,0,0);
75  C_.Set(0,0,0);
76  scale_ = 0.0;
77 
78  SVD svd((*this));
79 
80  invTransform_ = svd.Invert();
81 }
82 
86  for (unsigned int row=0; row<3; row++) {
87  for (unsigned int column=0; column<4; column++) {
88  P[row][column] = (*this)[row][column];
89  }
90  }
91  return P;
92 }
93 
96  return *this;
97 }
98 
99 
100 
102 Load(const std::string& inputFile)
103 {
104  ifstream ifs(inputFile.c_str());
105  if(!ifs) {
106  return false;
107  }
108  ifs >> *this;
109  if(!ifs) {
110  ifs.close();
111  return false;
112  }
113  ifs.close();
114  UpdateMatrix_();
115  return true;
116 }
117 
119 Save(const std::string& outputFile)
120 {
121  ofstream ofs(outputFile.c_str());
122  if(!ofs) {
123  return false;
124  }
125  ofs << *this;
126  if(!ofs) {
127  ofs.close();
128  return false;
129  }
130  ofs.close();
131  return true;
132 }
133 
136  const CoordinateTransform3D& newGlobal)
137 {
138  CoordinateTransform3D newGlobalInv = newGlobal;
139  newGlobalInv.InvertIP();
140  newGlobalInv.ConcatenateLocalTransform(newLocal, (*this));
141 }
142 
145  CoordinateTransform3D& res) const
146 {
147  /* if we were using matrices:
148  * B1 is linear transform local to global (global = B1*local)
149  * B(1,i) is linear transform local(2) to local (local = B(1,i)*local(2))
150  * then BC = B1*B(1,i) should be global = BC * local(2)
151  * this stores B1 in Q_, C_, and scale_.
152  * localT store B(1,i) in its Q_, C_, and scale_.
153  */
154  res.Set(Q_ * localT.Q_,
155  C_ + Q_.MultVec(localT.C_) * scale_,
156  scale_ * localT.scale_);
157 }
158 
159 
162 {
163  //moves the local frame:
164  // e.g. camera is rotated by Q
165  Q_ = Q_*Q;
166  UpdateMatrix_();
167 }
168 
170 Talk() const
171 {
172  cout<<"Transformation RAW: "<<endl;
173 // cout<<*this<<endl;
174 
175  for(unsigned int i=0; i < 4; i++){
176  for(unsigned int j=0; j < 4; j++){
177  cout << (*this)[i][j] << " ";
178  }
179  cout << endl;
180  }
181 
182  if(Q_ != Quaternion<double>(0, 0, 0, 0)){
183  RMatrixBase R;
184  R.SetFromQuaternion(Q_);
185  cout<<"Rotation Matrix: "<<R<<endl;
186 
187  Vector3<double> axis;
188  double angle;
189 
190  R.GetRotationAxisAngle(axis, angle);
191  cout<<" axis = "<<axis<<endl;
192  cout<<"angle = "<<angle<<endl<<endl;
193  }
194 }
195 
196 void
198 {
199  R.GetQuaternion(Q_);
200  UpdateMatrix_();
201 }
void BecomeRelativeTransform(const CoordinateTransform3D &newLocal, const CoordinateTransform3D &newGlobal)
Changes this coordinate transformation so that parameter newLocal becomes the local coordinate frame ...
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
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.
virtual void UpdateMatrix_()
Computes the 4x4 transformation matrix from the currently set unit quaternion Q_ and translation vect...
void RotateLocalFrame(const Quaternion< double > &Q)
Applies the passed rotation to the orientation of the local frame.
void SetR(const BIAS::RMatrixBase &R)
Set orientation of local coordinate system from rotation matrix.
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
double scale_
Specifies isometric scaling from local to global coordinate frame.
bool Save(const std::string &outputFile)
Write 3d coordinate transformation to a text file.
int MultVec(const Vector3< QUAT_TYPE > &vec, Vector3< QUAT_TYPE > &res) const
rotates the given Vector qith the quaternion ( q v q* ) the resulting vector is given in res ...
Definition: Quaternion.hh:136
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
void Set(const int row, const int col, const Matrix< T > &data)
Copies the contents of data into this at the position indicated by row and col.
Definition: Matrix.cpp:816
Vector3< double > C_
Specifies the translation from local to global coordinate frame.
Matrix3x4< double > GetMatrix3x4() const
Set rotation and translation from pose object.
void SetFromMatrix3x4(const Matrix3x4< double > &M)
Set 3x4 matrix which transforms points in global coordinate system to Euclidean points in local coord...
void Talk() const
Prints this transformation with more intuitive rotation representation.
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
Transforms 3d points between two different coodinate systems, e.g.
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
int GetRotationAxisAngle(Vector3< ROTATION_MATRIX_TYPE > &axis, ROTATION_MATRIX_TYPE &angle) const
Calculates angle and rotation axis representation for this rotation matrix.
void ConcatenateLocalTransform(const CoordinateTransform3D &localT, CoordinateTransform3D &res) const
Calculates transformation from global system of this instance to the local coordinate system of the g...
Quaternion< double > Q_
Specifies the rotation from local to global coordinate frame.
bool Load(const std::string &inputFile)
Load 3d coordinate transformation from a text file.
Matrix4x4< double > GetMatrix4x4() const
Get 4x4 matrix which transforms points in global coordinate system to homogeneous points in local coo...