Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Quaternion.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT FNAL MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for rotation in 3 dimensions, represented by quaternion
12 //
13 // Created by: Mark Fischler Thurs June 9 2005
14 //
15 // Last update: $Id$
16 //
18 
19 #include <cmath>
20 
24 
28 
29 namespace ROOT {
30 
31 namespace Math {
32 
33 // ========== Constructors and Assignment =====================
34 
35 void Quaternion::Rectify()
36 {
37 
38  // The vector should be a unit vector, and the first element should be
39  // non-negative (though negative fU quaternions would work just fine,
40  // being isomorphic to a quaternion with positive fU).
41 
42  if ( fU < 0 ) {
43  fU = - fU; fI = - fI; fJ = - fJ; fK = - fK;
44  }
45 
46  Scalar a = 1.0 / std::sqrt(fU*fU + fI*fI + fJ*fJ + fK*fK);
47  fU *= a;
48  fI *= a;
49  fJ *= a;
50  fK *= a;
51 
52 } // Rectify()
53 
54 
55 // ========== Operations =====================
56 
57 // DisplacementVector3D< Cartesian3D<double> >
58 // Quaternion::operator() (const DisplacementVector3D< Cartesian3D<double> > & v) const
59 // {
60 // // apply to a 3D Vector
61 // }
62 
63 // Quaternion Quaternion::operator * (const Quaternion & q) const {
64 // // combination of rotations
65 // return Quaternion (
66 // fU*q.fU - fI*q.fI - fJ*q.fJ - fK*q.fK
67 // , fU*q.fI + fI*q.fU + fJ*q.fK - fK*q.fJ
68 // , fU*q.fJ - fI*q.fK + fJ*q.fU + fK*q.fI
69 // , fU*q.fK + fI*q.fJ - fJ*q.fI + fK*q.fU );
70 //}
71 
72 Quaternion Quaternion::operator * (const Rotation3D & r) const {
73  // combination of rotations
74  return operator* ( Quaternion(r) );
75 }
76 
77 Quaternion Quaternion::operator * (const AxisAngle & a) const {
78  // combination of rotations
79  return operator* ( Quaternion(a) );
80 }
81 
82 Quaternion Quaternion::operator * (const EulerAngles & e) const {
83  // combination of rotations
84  return operator* ( Quaternion(e) );
85 }
86 
87 Quaternion Quaternion::operator * (const RotationZYX & r) const {
88  // combination of rotations
89  return operator* ( Quaternion(r) );
90 }
91 
92 Quaternion::Scalar Quaternion::Distance(const Quaternion & q) const {
93  // distance
94  Scalar chordLength = std::fabs(fU*q.fU + fI*q.fI + fJ*q.fJ + fK*q.fK);
95  if (chordLength > 1) chordLength = 1; // in case roundoff fouls us up
96  return acos(chordLength);
97 }
98 
99 // ========== I/O =====================
100 
101 std::ostream & operator<< (std::ostream & os, const Quaternion & q) {
102  // TODO - this will need changing for machine-readable issues
103  // and even the human readable form may need formatiing improvements
104  os << "\n{" << q.U() << " " << q.I()
105  << " " << q.J() << " " << q.K() << "}\n";
106  return os;
107 }
108 
109 
110 } //namespace Math
111 } //namespace ROOT