CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Rotation.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: Rotation.cc,v 1.4 2003/08/13 20:00:14 garren Exp $
00003 // ---------------------------------------------------------------------------
00004 //
00005 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00006 //
00007 // This is the implementation of the parts of the the HepRotation class which
00008 // were present in the original CLHEP before the merge with ZOOM PhysicsVectors.
00009 //
00010 
00011 #ifdef GNUPRAGMA
00012 #pragma implementation
00013 #endif
00014 
00015 #include "CLHEP/Vector/defs.h"
00016 #include "CLHEP/Vector/Rotation.h"
00017 #include "CLHEP/Units/PhysicalConstants.h"
00018 
00019 #include <iostream>
00020 #include <cmath>
00021 
00022 using std::abs;
00023 
00024 namespace CLHEP  {
00025 
00026 static inline double safe_acos (double x) {
00027   if (abs(x) <= 1.0) return acos(x);
00028   return ( (x>0) ? 0 : CLHEP::pi );
00029 }
00030 
00031 double HepRotation::operator() (int i, int j) const {
00032   if (i == 0) {
00033     if (j == 0) { return xx(); }
00034     if (j == 1) { return xy(); }
00035     if (j == 2) { return xz(); } 
00036   } else if (i == 1) {
00037     if (j == 0) { return yx(); }
00038     if (j == 1) { return yy(); }
00039     if (j == 2) { return yz(); } 
00040   } else if (i == 2) {
00041     if (j == 0) { return zx(); }
00042     if (j == 1) { return zy(); }
00043     if (j == 2) { return zz(); } 
00044   } 
00045   std::cerr << "HepRotation subscripting: bad indices "
00046        << "(" << i << "," << j << ")" << std::endl;
00047   return 0.0;
00048 } 
00049 
00050 HepRotation & HepRotation::rotate(double a, const Hep3Vector& axis) {
00051   if (a != 0.0) {
00052     double ll = axis.mag();
00053     if (ll == 0.0) {
00054       ZMthrowC (ZMxpvZeroVector("HepRotation: zero axis"));
00055     }else{
00056       double sa = sin(a), ca = cos(a);
00057       double dx = axis.x()/ll, dy = axis.y()/ll, dz = axis.z()/ll;   
00058       HepRotation m(
00059         ca+(1-ca)*dx*dx,          (1-ca)*dx*dy-sa*dz,    (1-ca)*dx*dz+sa*dy,
00060            (1-ca)*dy*dx+sa*dz, ca+(1-ca)*dy*dy,          (1-ca)*dy*dz-sa*dx,
00061            (1-ca)*dz*dx-sa*dy,    (1-ca)*dz*dy+sa*dx, ca+(1-ca)*dz*dz );
00062       transform(m);
00063     }
00064   }
00065   return *this;
00066 }
00067 
00068 HepRotation & HepRotation::rotateX(double a) {
00069   double c = cos(a);
00070   double s = sin(a);
00071   double x = ryx, y = ryy, z = ryz; 
00072   ryx = c*x - s*rzx;
00073   ryy = c*y - s*rzy;
00074   ryz = c*z - s*rzz;
00075   rzx = s*x + c*rzx;
00076   rzy = s*y + c*rzy;
00077   rzz = s*z + c*rzz;
00078   return *this;
00079 }
00080 
00081 HepRotation & HepRotation::rotateY(double a){
00082   double c = cos(a);
00083   double s = sin(a);
00084   double x = rzx, y = rzy, z = rzz; 
00085   rzx = c*x - s*rxx;
00086   rzy = c*y - s*rxy;
00087   rzz = c*z - s*rxz;
00088   rxx = s*x + c*rxx;
00089   rxy = s*y + c*rxy;
00090   rxz = s*z + c*rxz;
00091   return *this;
00092 }
00093 
00094 HepRotation & HepRotation::rotateZ(double a) {
00095   double c = cos(a);
00096   double s = sin(a);
00097   double x = rxx, y = rxy, z = rxz; 
00098   rxx = c*x - s*ryx;
00099   rxy = c*y - s*ryy;
00100   rxz = c*z - s*ryz;
00101   ryx = s*x + c*ryx;
00102   ryy = s*y + c*ryy;
00103   ryz = s*z + c*ryz;
00104   return *this;
00105 }
00106 
00107 HepRotation & HepRotation::rotateAxes(const Hep3Vector &newX,
00108                                       const Hep3Vector &newY,
00109                                       const Hep3Vector &newZ) {
00110   double del = 0.001;
00111   Hep3Vector w = newX.cross(newY);
00112 
00113   if (abs(newZ.x()-w.x()) > del ||
00114       abs(newZ.y()-w.y()) > del ||
00115       abs(newZ.z()-w.z()) > del ||
00116       abs(newX.mag2()-1.) > del ||
00117       abs(newY.mag2()-1.) > del || 
00118       abs(newZ.mag2()-1.) > del ||
00119       abs(newX.dot(newY)) > del ||
00120       abs(newY.dot(newZ)) > del ||
00121       abs(newZ.dot(newX)) > del) {
00122     std::cerr << "HepRotation::rotateAxes: bad axis vectors" << std::endl;
00123     return *this;
00124   }else{
00125     return transform(HepRotation(newX.x(), newY.x(), newZ.x(),
00126                                  newX.y(), newY.y(), newZ.y(),
00127                                  newX.z(), newY.z(), newZ.z()));
00128   }
00129 }
00130 
00131 double HepRotation::phiX() const {
00132   return (yx() == 0.0 && xx() == 0.0) ? 0.0 : std::atan2(yx(),xx());
00133 }
00134 
00135 double HepRotation::phiY() const {
00136   return (yy() == 0.0 && xy() == 0.0) ? 0.0 : std::atan2(yy(),xy());
00137 }
00138 
00139 double HepRotation::phiZ() const {
00140   return (yz() == 0.0 && xz() == 0.0) ? 0.0 : std::atan2(yz(),xz());
00141 }
00142 
00143 double HepRotation::thetaX() const {
00144   return safe_acos(zx());
00145 }
00146 
00147 double HepRotation::thetaY() const {
00148   return safe_acos(zy());
00149 }
00150 
00151 double HepRotation::thetaZ() const {
00152   return safe_acos(zz());
00153 }
00154 
00155 void HepRotation::getAngleAxis(double &angle, Hep3Vector &axis) const {
00156   double cosa  = 0.5*(xx()+yy()+zz()-1);
00157   double cosa1 = 1-cosa;
00158   if (cosa1 <= 0) {
00159     angle = 0;
00160     axis  = Hep3Vector(0,0,1);
00161   }else{
00162     double x=0, y=0, z=0;
00163     if (xx() > cosa) x = sqrt((xx()-cosa)/cosa1);
00164     if (yy() > cosa) y = sqrt((yy()-cosa)/cosa1);
00165     if (zz() > cosa) z = sqrt((zz()-cosa)/cosa1);
00166     if (zy() < yz()) x = -x;
00167     if (xz() < zx()) y = -y;
00168     if (yx() < xy()) z = -z;
00169     angle = (cosa < -1.) ? acos(-1.) : acos(cosa);
00170     axis  = Hep3Vector(x,y,z);
00171   }
00172 }
00173 
00174 bool HepRotation::isIdentity() const {
00175   return  (rxx == 1.0 && rxy == 0.0 && rxz == 0.0 &&
00176            ryx == 0.0 && ryy == 1.0 && ryz == 0.0 &&
00177            rzx == 0.0 && rzy == 0.0 && rzz == 1.0) ? true : false;
00178 }
00179 
00180 int HepRotation::compare ( const HepRotation & r ) const {
00181        if (rzz<r.rzz) return -1; else if (rzz>r.rzz) return 1;
00182   else if (rzy<r.rzy) return -1; else if (rzy>r.rzy) return 1;
00183   else if (rzx<r.rzx) return -1; else if (rzx>r.rzx) return 1;
00184   else if (ryz<r.ryz) return -1; else if (ryz>r.ryz) return 1;
00185   else if (ryy<r.ryy) return -1; else if (ryy>r.ryy) return 1;
00186   else if (ryx<r.ryx) return -1; else if (ryx>r.ryx) return 1;
00187   else if (rxz<r.rxz) return -1; else if (rxz>r.rxz) return 1;
00188   else if (rxy<r.rxy) return -1; else if (rxy>r.rxy) return 1;
00189   else if (rxx<r.rxx) return -1; else if (rxx>r.rxx) return 1;
00190   else return 0;
00191 }
00192 
00193 
00194 const HepRotation HepRotation::IDENTITY;
00195 
00196 }  // namespace CLHEP
00197 
00198 

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7