Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleQuaternion.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  @example ExampleQuaternion.cpp
27  @relates Quaternion
28  @brief Example for quaternion usage and relation to rotation matrix
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include "../Quaternion.hh"
34 #include <Base/Common/CompareFloatingPoint.hh>
35 
36 using namespace std;
37 using namespace BIAS;
38 
39 int main()
40 {
41  Quaternion<double> q, p;
42  q.SetValueAsAxisRad(-0.97856,0.049681,-0.14840,0.00102);
43  p.SetValueAsAxisRad(-0.97856,0.049681,-0.14840,0.00102);
44  cout << "q = " << q << ", p = " << p << endl;
45 
46  Quaternion<double> qp = q*p;
48  Matrix4x4<double> R = p.GetQuaternionMultMatrixRight();
49  Quaternion<double>Lp = L*p;
50  Quaternion<double>Rq = R*q;
51 
52  // compare quaternion multiplication results
53  cout << "q*p = " << qp << endl;
54  cout << "L(q)*p = " << Lp << endl;
55  cout << "R(p)*q = " << Rq << endl;
56  BIASASSERT(BIAS::Equal((Lp-qp).NormL2(), 0.0, 1e-12));
57  BIASASSERT(BIAS::Equal((Rq-qp).NormL2(), 0.0, 1e-12));
58 
59  // compare result with rotation matrix
60  RMatrixBase R1, R2;
61  R1.SetFromQuaternion(q);
62  R2.SetFromQuaternion(p);
63  RMatrixBase R1R2 = R1*R2;
65 #ifdef BIASASSERT_ISACTIVE
66  int res_GetQuat = R1R2.GetQuaternion(r);
67  BIASASSERT(res_GetQuat == 0);
68  BIASASSERT(BIAS::Equal(fabs(r.ScalarProduct(qp)), 1.0, 1e-12));
69 #endif
70 
71  // compare quaternion - vector multiplication with
72  // rotation matrix - vector multiplication
73  Vector3<double> v(0.5, -0.5, 0.1), qvq;
74  q.MultVec(v, qvq);
75  cout << "v = " << v << ", q*v*q' = " << qvq << endl;
76  Vector3<double> Rv = R1*v;
77  BIASASSERT(BIAS::Equal((qvq-Rv).NormL2(), 0.0, 1e-12));
78 
79  return 0;
80 }
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
Matrix4x4< QUAT_TYPE > GetQuaternionMultMatrixLeft() const
Returns matrix which expresses (left) quaternion multiplication.
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
void ScalarProduct(const Vector4< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector4.hh:475
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_...
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
void SetValueAsAxisRad(const Vector3< QUAT_TYPE > &axis, QUAT_TYPE angle)
sets the quaternion with given rotation axis and angle (in rad)
Definition: Quaternion.hh:183