00001
00002
00003
00004
00005
00006
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
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
00034
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
00048
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
00063
00064
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 }
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 }
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 }
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 }
00132
00133 double Hep2Vector::howParallel (const Hep2Vector & v) const {
00134
00135
00136 double v1v2 = std::fabs(dot(v));
00137 if ( v1v2 == 0 ) {
00138
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 }
00148
00149 bool Hep2Vector::isParallel (const Hep2Vector & v,
00150 double epsilon) const {
00151
00152
00153 double v1v2 = std::fabs(dot(v));
00154 if ( v1v2 == 0 ) {
00155
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 }
00161
00162 double Hep2Vector::howOrthogonal (const Hep2Vector & v) const {
00163
00164
00165 double v1v2 = std::fabs(dot(v));
00166 if ( v1v2 == 0 ) {
00167 return 0;
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 }
00176
00177 bool Hep2Vector::isOrthogonal (const Hep2Vector & v,
00178 double epsilon) const {
00179
00180
00181 double v1v2 = std::fabs(dot(v));
00182 double abscross = std::fabs ( dx * v.y() - dy - v.x() );
00183 return ( v1v2 <= epsilon * abscross );
00184 }
00185
00186 }