Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
AxisAngle.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 // Header file for class AxisAngle, a rotation in 3 dimensions
12 // represented by its axis and angle of rotation
13 //
14 // Created by: Mark Fischler Tues July 5 2005
15 //
17 
18 #include <cmath>
19 #include <algorithm>
20 
24 
25 namespace ROOT {
26 
27 namespace Math {
28 
29 // ========== Constructors and Assignment =====================
30 
31 void AxisAngle::RectifyAngle() {
32  // Note: We could require the angle to be in [0,pi) since we
33  // can represent negative angles by flipping the axis.
34  // We choose not to do this.
35 
36  if ( fAngle <= Pi() && fAngle > -Pi() ) return;
37 
38  if ( fAngle > 0 ) {
39  int n = static_cast<int>( (fAngle+Pi())/(2*Pi()) );
40  fAngle -= 2*Pi()*n;
41  } else {
42  int n = static_cast<int>( -(fAngle-Pi())/(2*Pi()) );
43  fAngle += 2*Pi()*n;
44  }
45 } // RectifyAngle()
46 
47 void AxisAngle::Rectify()
48 {
49  // The two conditions are that the angle is in (-pi, pi] and
50  // the axis is a unit vector.
51 
52  Scalar r2 = fAxis.Mag2();
53  if ( r2 == 0 ) {
54  fAxis.SetCoordinates(0,0,1);
55  fAngle = 0;
56  return;
57  }
58  fAxis *= (1.0/r2);
59  RectifyAngle();
60 } // Rectify()
61 
62 // ======== Transformation to other Rotation Forms ==================
63 
64 enum ERotation3DMatrixIndex {
65  kXX = 0, kXY = 1, kXZ = 2
66  , kYX = 3, kYY = 4, kYZ = 5
67  , kZX = 6, kZY = 7, kZZ = 8
68 };
69 
70 
71 
72 // ========== Operations =====================
73 
74 DisplacementVector3D< Cartesian3D<double> >
75 AxisAngle::
76 operator() (const DisplacementVector3D< Cartesian3D<double> > & v) const
77 {
78  Scalar c = std::cos(fAngle);
79  Scalar s = std::sin(fAngle);
80  Scalar p = fAxis.Dot(v) * ( 1 - c );
81  return DisplacementVector3D< Cartesian3D<double> >
82  (
83  c*v.X() + p*fAxis.X() + s * (fAxis.Y()*v.Z() - fAxis.Z()*v.Y())
84  , c*v.Y() + p*fAxis.Y() + s * (fAxis.Z()*v.X() - fAxis.X()*v.Z())
85  , c*v.Z() + p*fAxis.Z() + s * (fAxis.X()*v.Y() - fAxis.Y()*v.X())
86  );
87 }
88 
89 // ========== I/O =====================
90 
91 std::ostream & operator<< (std::ostream & os, const AxisAngle & a) {
92  // TODO - this will need changing for machine-readable issues
93  // and even the human readable form may need formatiing improvements
94  os << "\n" << a.Axis() << " " << a.Angle() << "\n";
95  return os;
96 }
97 
98 
99 
100 } //namespace Math
101 } //namespace ROOT