TwoVector.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // ---------------------------------------------------------------------------
00003 //
00004 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00005 //
00006 // This is the implementation of the Hep2Vector class.
00007 //
00008 //-------------------------------------------------------------
00009 
00010 #include "CLHEP/Vector/TwoVector.h"
00011 #include "CLHEP/Vector/ThreeVector.h"
00012 
00013 #include <cmath>
00014 #include <iostream>
00015 
00016 namespace CLHEP  {
00017 
00018 double Hep2Vector::tolerance = Hep2Vector::ZMpvToleranceTicks * 2.22045e-16;
00019 
00020 double Hep2Vector::setTolerance (double tol) {
00021 // Set the tolerance for Hep2Vectors to be considered near one another
00022   double oldTolerance (tolerance);
00023   tolerance = tol;
00024   return oldTolerance;
00025 }
00026 
00027 double Hep2Vector::operator () (int i) const {
00028   if (i == 0) {
00029     return x();
00030   }else if (i == 1) {
00031     return y();
00032   }else{
00033 //    std::cerr << "Hep2Vector::operator () - "
00034 //              << "Hep2Vector::operator(): bad index" << std::endl;
00035     return 0.0;
00036   }
00037 }
00038 
00039 double & Hep2Vector::operator () (int i) {
00040   static double dummy;
00041   switch(i) {
00042   case X:
00043     return dx;
00044   case Y:
00045     return dy;
00046   default:
00047 //    std::cerr << "Hep2Vector::operator () - "
00048 //              << "Hep2Vector::operator() : bad index" << std::endl;
00049     return dummy;
00050   }
00051 }
00052 
00053 void Hep2Vector::rotate(double aangle) {
00054   double ss = std::sin(aangle);
00055   double cc = std::cos(aangle);
00056   double xx = dx;
00057   dx = cc*xx - ss*dy;
00058   dy = ss*xx + cc*dy;
00059 }
00060 
00061 Hep2Vector operator/ (const Hep2Vector & p, double a) {
00062 //  if (a==0) {
00063 //    std::cerr << "Hep2Vector operator/ () - "
00064 //              << "Division of Hep2Vector by zero" << std::endl;
00065 //  }
00066   return Hep2Vector(p.x()/a, p.y()/a);
00067 }
00068 
00069 std::ostream & operator << (std::ostream & os, const Hep2Vector & q) {
00070   os << "(" << q.x() << ", " << q.y() << ")";
00071   return os;
00072 }
00073 
00074 void ZMinput2doubles ( std::istream & is, const char * type,
00075                        double & x, double & y );
00076 
00077 std::istream & operator>>(std::istream & is, Hep2Vector & p) {
00078   double x, y;
00079   ZMinput2doubles ( is, "Hep2Vector", x, y );
00080   p.set(x, y);
00081   return  is;
00082 }  // operator>>()
00083 
00084 Hep2Vector::operator Hep3Vector () const {
00085   return Hep3Vector ( dx, dy, 0.0 );
00086 }
00087 
00088 int Hep2Vector::compare (const Hep2Vector & v) const {
00089   if       ( dy > v.dy ) {
00090     return 1;
00091   } else if ( dy < v.dy ) {
00092     return -1;
00093   } else if ( dx > v.dx ) {
00094     return 1;
00095   } else if ( dx < v.dx ) {
00096     return -1;
00097   } else {
00098     return 0;
00099   }
00100 } /* Compare */
00101 
00102 
00103 bool Hep2Vector::operator > (const Hep2Vector & v) const {
00104         return (compare(v)  > 0);
00105 }
00106 bool Hep2Vector::operator < (const Hep2Vector & v) const {
00107         return (compare(v)  < 0);
00108 }
00109 bool Hep2Vector::operator>= (const Hep2Vector & v) const {
00110         return (compare(v) >= 0);
00111 }
00112 bool Hep2Vector::operator<= (const Hep2Vector & v) const {
00113         return (compare(v) <= 0);
00114 }
00115 
00116 bool Hep2Vector::isNear(const Hep2Vector & p, double epsilon) const {
00117   double limit = dot(p)*epsilon*epsilon;
00118   return ( (*this - p).mag2() <= limit );
00119 } /* isNear() */
00120 
00121 double Hep2Vector::howNear(const Hep2Vector & p ) const {
00122   double d   = (*this - p).mag2();
00123   double pdp = dot(p);
00124   if ( (pdp > 0) && (d < pdp)  ) {
00125     return std::sqrt (d/pdp);
00126   } else if ( (pdp == 0) && (d == 0) ) {
00127     return 0;
00128   } else {
00129     return 1;
00130   }
00131 } /* howNear */
00132 
00133 double Hep2Vector::howParallel (const Hep2Vector & v) const {
00134   // | V1 x V2 | / | V1 dot V2 |
00135   // Of course, the "cross product" is fictitious but the math is valid
00136   double v1v2 = std::fabs(dot(v));
00137   if ( v1v2 == 0 ) {
00138     // Zero is parallel to no other vector except for zero.
00139     return ( (mag2() == 0) && (v.mag2() == 0) ) ? 0 : 1;
00140   }
00141   double abscross = std::fabs ( dx * v.y() - dy - v.x() );
00142   if ( abscross >= v1v2 ) {
00143     return 1;
00144   } else {
00145     return abscross/v1v2;
00146   }
00147 } /* howParallel() */
00148 
00149 bool Hep2Vector::isParallel (const Hep2Vector & v,
00150                              double epsilon) const {
00151   // | V1 x V2 | <= epsilon * | V1 dot V2 | 
00152   // Of course, the "cross product" is fictitious but the math is valid
00153   double v1v2 = std::fabs(dot(v));
00154   if ( v1v2 == 0 ) {
00155     // Zero is parallel to no other vector except for zero.
00156     return ( (mag2() == 0) && (v.mag2() == 0) );
00157   }
00158   double abscross = std::fabs ( dx * v.y() - dy - v.x() );
00159   return ( abscross <= epsilon * v1v2 );
00160 } /* isParallel() */
00161 
00162 double Hep2Vector::howOrthogonal (const Hep2Vector & v) const {
00163   // | V1 dot V2 | / | V1 x V2 | 
00164   // Of course, the "cross product" is fictitious but the math is valid
00165   double v1v2 = std::fabs(dot(v));
00166   if ( v1v2 == 0 ) {
00167     return 0;   // Even if one or both are 0, they are considered orthogonal
00168   }
00169   double abscross = std::fabs ( dx * v.y() - dy - v.x() );
00170   if ( v1v2 >= abscross ) {
00171     return 1;
00172   } else {
00173     return v1v2/abscross;
00174   }
00175 } /* howOrthogonal() */
00176 
00177 bool Hep2Vector::isOrthogonal (const Hep2Vector & v,
00178                              double epsilon) const {
00179   // | V1 dot V2 | <= epsilon * | V1 x V2 | 
00180   // Of course, the "cross product" is fictitious but the math is valid
00181   double v1v2 = std::fabs(dot(v));
00182   double abscross = std::fabs ( dx * v.y() - dy - v.x() );
00183   return ( v1v2 <= epsilon * abscross );
00184 } /* isOrthogonal() */
00185 
00186 }  // namespace CLHEP

Generated on Mon May 27 17:50:36 2013 for Geant4 by  doxygen 1.4.7