CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

TwoVector.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // ---------------------------------------------------------------------------
3 //
4 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
5 //
6 // This is the implementation of the Hep2Vector class.
7 //
8 //-------------------------------------------------------------
9 
10 #include "CLHEP/Vector/defs.h"
11 #include "CLHEP/Vector/TwoVector.h"
12 #include "CLHEP/Vector/ZMxpv.h"
13 #include "CLHEP/Vector/ThreeVector.h"
14 
15 #include <cmath>
16 #include <iostream>
17 
18 namespace CLHEP {
19 
20 double Hep2Vector::tolerance = Hep2Vector::ZMpvToleranceTicks * 2.22045e-16;
21 
22 double Hep2Vector::setTolerance (double tol) {
23 // Set the tolerance for Hep2Vectors to be considered near one another
24  double oldTolerance (tolerance);
25  tolerance = tol;
26  return oldTolerance;
27 }
28 
29 double Hep2Vector::operator () (int i) const {
30  if (i == 0) {
31  return x();
32  }else if (i == 1) {
33  return y();
34  }else{
35  ZMthrowA(ZMxpvIndexRange(
36  "Hep2Vector::operator(): bad index"));
37  return 0.0;
38  }
39 }
40 
41 double & Hep2Vector::operator () (int i) {
42  static double dummy;
43  switch(i) {
44  case X:
45  return dx;
46  case Y:
47  return dy;
48  default:
49  ZMthrowA (ZMxpvIndexRange(
50  "Hep2Vector::operator() : bad index"));
51  return dummy;
52  }
53 }
54 
55 void Hep2Vector::rotate(double angler) {
56  double s1 = std::sin(angler);
57  double c = std::cos(angler);
58  double xx = dx;
59  dx = c*xx - s1*dy;
60  dy = s1*xx + c*dy;
61 }
62 
63 Hep2Vector operator/ (const Hep2Vector & p, double a) {
64  if (a==0) {
65  ZMthrowA(ZMxpvInfiniteVector( "Division of Hep2Vector by zero"));
66  }
67  return Hep2Vector(p.x()/a, p.y()/a);
68 }
69 
70 std::ostream & operator << (std::ostream & os, const Hep2Vector & q) {
71  os << "(" << q.x() << ", " << q.y() << ")";
72  return os;
73 }
74 
75 void ZMinput2doubles ( std::istream & is, const char * type,
76  double & x, double & y );
77 
78 std::istream & operator>>(std::istream & is, Hep2Vector & p) {
79  double x, y;
80  ZMinput2doubles ( is, "Hep2Vector", x, y );
81  p.set(x, y);
82  return is;
83 } // operator>>()
84 
85 Hep2Vector::operator Hep3Vector () const {
86  return Hep3Vector ( dx, dy, 0.0 );
87 }
88 
89 int Hep2Vector::compare (const Hep2Vector & v) const {
90  if ( dy > v.dy ) {
91  return 1;
92  } else if ( dy < v.dy ) {
93  return -1;
94  } else if ( dx > v.dx ) {
95  return 1;
96  } else if ( dx < v.dx ) {
97  return -1;
98  } else {
99  return 0;
100  }
101 } /* Compare */
102 
103 
104 bool Hep2Vector::operator > (const Hep2Vector & v) const {
105  return (compare(v) > 0);
106 }
107 bool Hep2Vector::operator < (const Hep2Vector & v) const {
108  return (compare(v) < 0);
109 }
110 bool Hep2Vector::operator>= (const Hep2Vector & v) const {
111  return (compare(v) >= 0);
112 }
113 bool Hep2Vector::operator<= (const Hep2Vector & v) const {
114  return (compare(v) <= 0);
115 }
116 
117 bool Hep2Vector::isNear(const Hep2Vector & p, double epsilon) const {
118  double limit = dot(p)*epsilon*epsilon;
119  return ( (*this - p).mag2() <= limit );
120 } /* isNear() */
121 
122 double Hep2Vector::howNear(const Hep2Vector & p ) const {
123  double d = (*this - p).mag2();
124  double pdp = dot(p);
125  if ( (pdp > 0) && (d < pdp) ) {
126  return std::sqrt (d/pdp);
127  } else if ( (pdp == 0) && (d == 0) ) {
128  return 0;
129  } else {
130  return 1;
131  }
132 } /* howNear */
133 
134 double Hep2Vector::howParallel (const Hep2Vector & v) const {
135  // | V1 x V2 | / | V1 dot V2 |
136  // Of course, the "cross product" is fictitious but the math is valid
137  double v1v2 = std::fabs(dot(v));
138  if ( v1v2 == 0 ) {
139  // Zero is parallel to no other vector except for zero.
140  return ( (mag2() == 0) && (v.mag2() == 0) ) ? 0 : 1;
141  }
142  double abscross = std::fabs ( dx * v.y() - dy - v.x() );
143  if ( abscross >= v1v2 ) {
144  return 1;
145  } else {
146  return abscross/v1v2;
147  }
148 } /* howParallel() */
149 
151  double epsilon) const {
152  // | V1 x V2 | <= epsilon * | V1 dot V2 |
153  // Of course, the "cross product" is fictitious but the math is valid
154  double v1v2 = std::fabs(dot(v));
155  if ( v1v2 == 0 ) {
156  // Zero is parallel to no other vector except for zero.
157  return ( (mag2() == 0) && (v.mag2() == 0) );
158  }
159  double abscross = std::fabs ( dx * v.y() - dy - v.x() );
160  return ( abscross <= epsilon * v1v2 );
161 } /* isParallel() */
162 
163 double Hep2Vector::howOrthogonal (const Hep2Vector & v) const {
164  // | V1 dot V2 | / | V1 x V2 |
165  // Of course, the "cross product" is fictitious but the math is valid
166  double v1v2 = std::fabs(dot(v));
167  if ( v1v2 == 0 ) {
168  return 0; // Even if one or both are 0, they are considered orthogonal
169  }
170  double abscross = std::fabs ( dx * v.y() - dy - v.x() );
171  if ( v1v2 >= abscross ) {
172  return 1;
173  } else {
174  return v1v2/abscross;
175  }
176 } /* howOrthogonal() */
177 
179  double epsilon) const {
180  // | V1 dot V2 | <= epsilon * | V1 x V2 |
181  // Of course, the "cross product" is fictitious but the math is valid
182  double v1v2 = std::fabs(dot(v));
183  double abscross = std::fabs ( dx * v.y() - dy - v.x() );
184  return ( v1v2 <= epsilon * abscross );
185 } /* isOrthogonal() */
186 
187 } // namespace CLHEP
CLHEP::Hep2Vector::y
double y() const
a
@ a
Definition: testCategories.cc:125
CLHEP::Hep2Vector::howNear
double howNear(const Hep2Vector &p) const
Definition: TwoVector.cc:122
CLHEP::Hep2Vector::X
@ X
Definition: Geometry/CLHEP/Vector/TwoVector.h:53
is
HepRotation and so forth isNear() norm2() rectify() static Rotation row1 row4(To avoid bloat in the code pulled in for programs which don 't use all these features, we split the implementation .cc files. Only isNear() goes into the original Rotation.cc) --------------------------------------- HepAxisAngle and HepEulerAngles classes --------------------------------------- These classes are very useful and simple structures for holding the result of a nice intuituve decomposition of a rotation there is no longer much content in the distinct ZOOM PhysicsVectors library The only content left in the library is the object files representing the various Exception objects When we build the CLHEP classes for the ZOOM we will set up so as to use ZOOM SpaceVector is(but we can disable namespace usage and most of our users do so at this point). What I do is leave Hep3Vector in the global namespace
CLHEP::Hep2Vector::operator()
double operator()(int i) const
Definition: TwoVector.cc:29
CLHEP::Hep2Vector::operator<=
bool operator<=(const Hep2Vector &v) const
Definition: TwoVector.cc:113
CLHEP::Hep2Vector
Definition: Geometry/CLHEP/Vector/TwoVector.h:49
CLHEP::Hep2Vector::compare
int compare(const Hep2Vector &v) const
Definition: TwoVector.cc:89
CLHEP::Hep2Vector::setTolerance
static double setTolerance(double tol)
Definition: TwoVector.cc:22
CLHEP::operator/
HepLorentzVector operator/(const HepLorentzVector &, double a)
Definition: LorentzVector.cc:162
CLHEP::Hep2Vector::ZMpvToleranceTicks
@ ZMpvToleranceTicks
Definition: Geometry/CLHEP/Vector/TwoVector.h:196
CLHEP::Hep2Vector::isNear
bool isNear(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:117
CLHEP::Hep2Vector::operator>
bool operator>(const Hep2Vector &v) const
Definition: TwoVector.cc:104
CLHEP
Definition: ClhepVersion.h:13
type
Signatures of Hep3Vector::rotate For equivalent ZOOM axis There is no harm in leaving this axis CLHEP has implemented a first forming an identity then rotating that by axis and I leave the CLHEP code alone people are of course free to use the ZOOM originated method with signature which I believe will be faster Return types for rotateZ CLHEP and PhysicsVectors each have these three and they are identical except that the ZOOM version returns a reference to while in CLHEP they return void Having methods that alter an object return a reference to that object is convenient for certain chained and costs nothing I don t wish to potentially break ZOOM user code for no good so I have made these CLHEP method conform to this convention There are a couple of other CLHEP rotate and which use the void return type
Definition: minorMergeIssues.doc:113
v
they are gone ZOOM Features Discontinued The following features of the ZOOM package were felt to be extreme overkill These have been after checking that no existing user code was utilizing as in SpaceVector v
Definition: keyMergeIssues.doc:324
Hep3Vector
Issues Concerning the PhysicsVectors CLHEP Vector Merge The merge of ZOOM PhysicsVdectors and the CLHEP Vector package is completed The purpose of this document is to list the major issues that affected the merge of these and where relevant describe the resolutions More detailed documents describe more minor issues General Approach As agreed at the June CLHEP the approach is to combine the features of each ZOOM class with the corresponding CLHEP class expanding the interface to create a single lingua franca of what a Hep3Vector(for example) means. We are not forming SpaceVector as an class derived from Hep3Vector and enhancing it in that way. Another rule imposed by the agreement is to avoid using the Exceptions package(even though that will later go into CLHEP for other uses). A desirable goal is to avoid cluttering the interface and enlarging the code linked in when ordinary CLHEP Vector functionallity is used. To this end
CLHEP::ZMinput2doubles
void ZMinput2doubles(std::istream &is, const char *type, double &x, double &y)
Definition: ZMinput.cc:240
CLHEP::Hep2Vector::x
double x() const
CLHEP::operator>>
std::istream & operator>>(std::istream &is, HepAxisAngle &aa)
Definition: AxisAngle.cc:96
CLHEP::Hep2Vector::howOrthogonal
double howOrthogonal(const Hep2Vector &p) const
Definition: TwoVector.cc:163
CLHEP::Hep2Vector::set
void set(double x, double y)
CLHEP::Hep2Vector::mag2
double mag2() const
CLHEP::Hep2Vector::isOrthogonal
bool isOrthogonal(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:178
CLHEP::Hep2Vector::operator<
bool operator<(const Hep2Vector &v) const
Definition: TwoVector.cc:107
i
long i
Definition: JamesRandomSeeding.txt:27
CLHEP::Hep2Vector::howParallel
double howParallel(const Hep2Vector &p) const
Definition: TwoVector.cc:134
CLHEP::operator<<
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition: AxisAngle.cc:86
CLHEP::Hep2Vector::Y
@ Y
Definition: Geometry/CLHEP/Vector/TwoVector.h:53
x
any side effects of that construction would occur twice The semantics of throw x
Definition: whyZMthrowRethrows.txt:37
ZMthrowA
it has advantages For I leave the ZMthrows but substitute I replaced ZMthrow with ZMthrowA in this package ZMthrowA
Definition: keyMergeIssues.doc:69
CLHEP::Hep2Vector::operator>=
bool operator>=(const Hep2Vector &v) const
Definition: TwoVector.cc:110
CLHEP::Hep2Vector::rotate
void rotate(double)
Definition: TwoVector.cc:55
CLHEP::Hep2Vector::isParallel
bool isParallel(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:150
CLHEP::Hep2Vector::dot
double dot(const Hep2Vector &p) const