ROOT logo
// @(#)root/mathcore:$Id$
// Authors: W. Brown, M. Fischler, L. Moneta    2005  

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2005 , LCG ROOT FNAL MathLib Team                    *
  *                                                                    *
  *                                                                    *
  **********************************************************************/

// Header file for class RotationY representing a rotation about the Y axis
//
// Created by: Mark Fischler Mon July 18  2005
//
// Last update: $Id$
//
#ifndef ROOT_Math_GenVector_RotationY 
#define ROOT_Math_GenVector_RotationY  1


#include "Math/GenVector/Cartesian3D.h"
#include "Math/GenVector/DisplacementVector3D.h"
#include "Math/GenVector/PositionVector3D.h"
#include "Math/GenVector/LorentzVector.h"
#include "Math/GenVector/3DDistances.h"

#include "Math/GenVector/RotationYfwd.h"

#include <cmath>

namespace ROOT {
namespace Math {


//__________________________________________________________________________________________
   /**
      Rotation class representing a 3D rotation about the Y axis by the angle of rotation.  
      For efficiency reason, in addition to the the angle, the sine and cosine of the angle are held 
      
      @ingroup GenVector
   */

class RotationY {

public:

   typedef double Scalar;


   // ========== Constructors and Assignment =====================

   /**
      Default constructor (identity rotation)
   */
   RotationY() : fAngle(0), fSin(0), fCos(1) { }

   /**
      Construct from an angle
   */
   explicit RotationY( Scalar angle ) :   fAngle(angle),
                                          fSin(std::sin(angle)),
                                          fCos(std::cos(angle)) 
   {
      Rectify();
   }

   // The compiler-generated copy ctor, copy assignment, and dtor are OK.

   /**
      Rectify makes sure the angle is in (-pi,pi]
   */
   void Rectify()  {
      if ( std::fabs(fAngle) >= M_PI ) {
         double x = fAngle / (2.0 * M_PI);
         fAngle =  (2.0 * M_PI) * ( x + std::floor(.5-x) );
         fSin = std::sin(fAngle);
         fCos = std::cos(fAngle);
      }
   }

   // ======== Components ==============

   /**
      Set given the angle.
   */
   void SetAngle (Scalar angle) {
      fSin=std::sin(angle);
      fCos=std::cos(angle);
      fAngle= angle; 
      Rectify(); 
   }
   void SetComponents (Scalar angle) { SetAngle(angle); }

   /**
      Get the angle
   */
   void GetAngle ( Scalar & angle ) const { angle = atan2 (fSin,fCos); }
   void GetComponents ( Scalar & angle ) const { GetAngle(angle); }

   /**
      Angle of rotation
   */
   Scalar Angle () const { return atan2 (fSin,fCos); }

   /**
      Sine or Cosine of the rotation angle
   */
   Scalar SinAngle () const { return fSin; }
   Scalar CosAngle () const { return fCos; }

   // =========== operations ==============

//   /**
//      Rotation operation on a cartesian vector
//    */
//   typedef  DisplacementVector3D< Cartesian3D<double> > XYZVector; 
//   XYZVector operator() (const XYZVector & v) const {
//     return XYZVector
//       ( fCos*v.x()+fSin*v.z(), v.y(), fCos*v.z()-fSin*v.x() );
//   }

   /**
      Rotation operation on a displacement vector in any coordinate system
   */
   template <class CoordSystem, class U>
   DisplacementVector3D<CoordSystem,U>
   operator() (const DisplacementVector3D<CoordSystem,U> & v) const {
      DisplacementVector3D< Cartesian3D<double>,U > xyz;
      xyz.SetXYZ( fCos*v.x()+fSin*v.z(), v.y(), fCos*v.z()-fSin*v.x() );
      return DisplacementVector3D<CoordSystem,U>(xyz);
   }

   /**
      Rotation operation on a position vector in any coordinate system
   */
   template <class CoordSystem, class U>
   PositionVector3D<CoordSystem, U>
   operator() (const PositionVector3D<CoordSystem,U> & v) const {
      DisplacementVector3D< Cartesian3D<double>,U > xyz(v);
      DisplacementVector3D< Cartesian3D<double>,U > rxyz = operator()(xyz);
      return PositionVector3D<CoordSystem,U> ( rxyz );
   }

   /**
      Rotation operation on a Lorentz vector in any 4D coordinate system
   */
   template <class CoordSystem>
   LorentzVector<CoordSystem>
   operator() (const LorentzVector<CoordSystem> & v) const {
      DisplacementVector3D< Cartesian3D<double> > xyz(v.Vect());
      xyz = operator()(xyz);
      LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
      return LorentzVector<CoordSystem> ( xyzt );
   }

   /**
      Rotation operation on an arbitrary vector v.
      Preconditions:  v must implement methods x(), y(), and z()
      and the arbitrary vector type must have a constructor taking (x,y,z)
   */
   template <class ForeignVector>
   ForeignVector
   operator() (const  ForeignVector & v) const {
      DisplacementVector3D< Cartesian3D<double> > xyz(v);
      DisplacementVector3D< Cartesian3D<double> > rxyz = operator()(xyz);
      return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
   }

   /**
      Overload operator * for rotation on a vector
   */
   template <class AVector>
   inline
   AVector operator* (const AVector & v) const
   {
      return operator()(v);
   }

   /**
      Invert a rotation in place
   */
   void Invert() { fAngle = -fAngle; fSin = -fSin; }

   /**
      Return inverse of  a rotation
   */
   RotationY Inverse() const { RotationY t(*this); t.Invert(); return t; }

   // ========= Multi-Rotation Operations ===============

   /**
      Multiply (combine) two rotations
   */
   RotationY operator * (const RotationY & r) const {
      RotationY ans;
      double x   = (fAngle + r.fAngle) / (2.0 * M_PI);
      ans.fAngle = (2.0 * M_PI) * ( x + std::floor(.5-x) );
      ans.fSin   = fSin*r.fCos + fCos*r.fSin;
      ans.fCos   = fCos*r.fCos - fSin*r.fSin;
      return ans;
   }

   /**
      Post-Multiply (on right) by another rotation :  T = T*R
   */
   RotationY & operator *= (const RotationY & r) { return *this = (*this)*r; }

   /**
      Equality/inequality operators
   */
   bool operator == (const RotationY & rhs) const {
      if( fAngle != rhs.fAngle )  return false;
      return true;
   }
   bool operator != (const RotationY & rhs) const {
      return ! operator==(rhs);
   }

private:

   Scalar fAngle;   // rotation angle 
   Scalar fSin;     // sine of the rotation angle 
   Scalar fCos;     // cosine of the rotaiton angle 

};  // RotationY

// ============ Class RotationY ends here ============

/**
   Distance between two rotations
 */
template <class R>
inline
typename RotationY::Scalar
Distance ( const RotationY& r1, const R & r2) {return gv_detail::dist(r1,r2);}

/**
   Stream Output and Input
 */
  // TODO - I/O should be put in the manipulator form 

inline
std::ostream & operator<< (std::ostream & os, const RotationY & r) {
  os << " RotationY(" << r.Angle() << ") ";
  return os;
} 
  

}  // namespace Math
}  // namespace ROOT

#endif // ROOT_Math_GenVector_RotationY 
 RotationY.h:1
 RotationY.h:2
 RotationY.h:3
 RotationY.h:4
 RotationY.h:5
 RotationY.h:6
 RotationY.h:7
 RotationY.h:8
 RotationY.h:9
 RotationY.h:10
 RotationY.h:11
 RotationY.h:12
 RotationY.h:13
 RotationY.h:14
 RotationY.h:15
 RotationY.h:16
 RotationY.h:17
 RotationY.h:18
 RotationY.h:19
 RotationY.h:20
 RotationY.h:21
 RotationY.h:22
 RotationY.h:23
 RotationY.h:24
 RotationY.h:25
 RotationY.h:26
 RotationY.h:27
 RotationY.h:28
 RotationY.h:29
 RotationY.h:30
 RotationY.h:31
 RotationY.h:32
 RotationY.h:33
 RotationY.h:34
 RotationY.h:35
 RotationY.h:36
 RotationY.h:37
 RotationY.h:38
 RotationY.h:39
 RotationY.h:40
 RotationY.h:41
 RotationY.h:42
 RotationY.h:43
 RotationY.h:44
 RotationY.h:45
 RotationY.h:46
 RotationY.h:47
 RotationY.h:48
 RotationY.h:49
 RotationY.h:50
 RotationY.h:51
 RotationY.h:52
 RotationY.h:53
 RotationY.h:54
 RotationY.h:55
 RotationY.h:56
 RotationY.h:57
 RotationY.h:58
 RotationY.h:59
 RotationY.h:60
 RotationY.h:61
 RotationY.h:62
 RotationY.h:63
 RotationY.h:64
 RotationY.h:65
 RotationY.h:66
 RotationY.h:67
 RotationY.h:68
 RotationY.h:69
 RotationY.h:70
 RotationY.h:71
 RotationY.h:72
 RotationY.h:73
 RotationY.h:74
 RotationY.h:75
 RotationY.h:76
 RotationY.h:77
 RotationY.h:78
 RotationY.h:79
 RotationY.h:80
 RotationY.h:81
 RotationY.h:82
 RotationY.h:83
 RotationY.h:84
 RotationY.h:85
 RotationY.h:86
 RotationY.h:87
 RotationY.h:88
 RotationY.h:89
 RotationY.h:90
 RotationY.h:91
 RotationY.h:92
 RotationY.h:93
 RotationY.h:94
 RotationY.h:95
 RotationY.h:96
 RotationY.h:97
 RotationY.h:98
 RotationY.h:99
 RotationY.h:100
 RotationY.h:101
 RotationY.h:102
 RotationY.h:103
 RotationY.h:104
 RotationY.h:105
 RotationY.h:106
 RotationY.h:107
 RotationY.h:108
 RotationY.h:109
 RotationY.h:110
 RotationY.h:111
 RotationY.h:112
 RotationY.h:113
 RotationY.h:114
 RotationY.h:115
 RotationY.h:116
 RotationY.h:117
 RotationY.h:118
 RotationY.h:119
 RotationY.h:120
 RotationY.h:121
 RotationY.h:122
 RotationY.h:123
 RotationY.h:124
 RotationY.h:125
 RotationY.h:126
 RotationY.h:127
 RotationY.h:128
 RotationY.h:129
 RotationY.h:130
 RotationY.h:131
 RotationY.h:132
 RotationY.h:133
 RotationY.h:134
 RotationY.h:135
 RotationY.h:136
 RotationY.h:137
 RotationY.h:138
 RotationY.h:139
 RotationY.h:140
 RotationY.h:141
 RotationY.h:142
 RotationY.h:143
 RotationY.h:144
 RotationY.h:145
 RotationY.h:146
 RotationY.h:147
 RotationY.h:148
 RotationY.h:149
 RotationY.h:150
 RotationY.h:151
 RotationY.h:152
 RotationY.h:153
 RotationY.h:154
 RotationY.h:155
 RotationY.h:156
 RotationY.h:157
 RotationY.h:158
 RotationY.h:159
 RotationY.h:160
 RotationY.h:161
 RotationY.h:162
 RotationY.h:163
 RotationY.h:164
 RotationY.h:165
 RotationY.h:166
 RotationY.h:167
 RotationY.h:168
 RotationY.h:169
 RotationY.h:170
 RotationY.h:171
 RotationY.h:172
 RotationY.h:173
 RotationY.h:174
 RotationY.h:175
 RotationY.h:176
 RotationY.h:177
 RotationY.h:178
 RotationY.h:179
 RotationY.h:180
 RotationY.h:181
 RotationY.h:182
 RotationY.h:183
 RotationY.h:184
 RotationY.h:185
 RotationY.h:186
 RotationY.h:187
 RotationY.h:188
 RotationY.h:189
 RotationY.h:190
 RotationY.h:191
 RotationY.h:192
 RotationY.h:193
 RotationY.h:194
 RotationY.h:195
 RotationY.h:196
 RotationY.h:197
 RotationY.h:198
 RotationY.h:199
 RotationY.h:200
 RotationY.h:201
 RotationY.h:202
 RotationY.h:203
 RotationY.h:204
 RotationY.h:205
 RotationY.h:206
 RotationY.h:207
 RotationY.h:208
 RotationY.h:209
 RotationY.h:210
 RotationY.h:211
 RotationY.h:212
 RotationY.h:213
 RotationY.h:214
 RotationY.h:215
 RotationY.h:216
 RotationY.h:217
 RotationY.h:218
 RotationY.h:219
 RotationY.h:220
 RotationY.h:221
 RotationY.h:222
 RotationY.h:223
 RotationY.h:224
 RotationY.h:225
 RotationY.h:226
 RotationY.h:227
 RotationY.h:228
 RotationY.h:229
 RotationY.h:230
 RotationY.h:231
 RotationY.h:232
 RotationY.h:233
 RotationY.h:234
 RotationY.h:235
 RotationY.h:236
 RotationY.h:237
 RotationY.h:238
 RotationY.h:239
 RotationY.h:240
 RotationY.h:241
 RotationY.h:242
 RotationY.h:243
 RotationY.h:244
 RotationY.h:245
 RotationY.h:246
 RotationY.h:247
 RotationY.h:248
 RotationY.h:249
 RotationY.h:250
 RotationY.h:251
 RotationY.h:252