Geant4-11
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
12
13#include <cmath>
14#include <iostream>
15
16namespace CLHEP {
17
19
20double Hep2Vector::setTolerance (double tol) {
21// Set the tolerance for Hep2Vectors to be considered near one another
22 double oldTolerance (tolerance);
23 tolerance = tol;
24 return oldTolerance;
25}
26
27double Hep2Vector::operator () (int i) const {
28 if (i == 0) {
29 return x();
30 }else if (i == 1) {
31 return y();
32 }else{
33// std::cerr << "Hep2Vector::operator () - "
34// << "Hep2Vector::operator(): bad index" << std::endl;
35 return 0.0;
36 }
37}
38
39double & Hep2Vector::operator () (int i) {
40 static double dummy;
41 switch(i) {
42 case X:
43 return dx;
44 case Y:
45 return dy;
46 default:
47// std::cerr << "Hep2Vector::operator () - "
48// << "Hep2Vector::operator() : bad index" << std::endl;
49 return dummy;
50 }
51}
52
53void Hep2Vector::rotate(double aangle) {
54 double ss = std::sin(aangle);
55 double cc = std::cos(aangle);
56 double xx = dx;
57 dx = cc*xx - ss*dy;
58 dy = ss*xx + cc*dy;
59}
60
61Hep2Vector operator/ (const Hep2Vector & p, double a) {
62// if (a==0) {
63// std::cerr << "Hep2Vector operator/ () - "
64// << "Division of Hep2Vector by zero" << std::endl;
65// }
66 return Hep2Vector(p.x()/a, p.y()/a);
67}
68
69std::ostream & operator << (std::ostream & os, const Hep2Vector & q) {
70 os << "(" << q.x() << ", " << q.y() << ")";
71 return os;
72}
73
74void ZMinput2doubles ( std::istream & is, const char * type,
75 double & x, double & y );
76
77std::istream & operator>>(std::istream & is, Hep2Vector & p) {
78 double x, y;
79 ZMinput2doubles ( is, "Hep2Vector", x, y );
80 p.set(x, y);
81 return is;
82} // operator>>()
83
84Hep2Vector::operator Hep3Vector () const {
85 return Hep3Vector ( dx, dy, 0.0 );
86}
87
88int Hep2Vector::compare (const Hep2Vector & v) const {
89 if ( dy > v.dy ) {
90 return 1;
91 } else if ( dy < v.dy ) {
92 return -1;
93 } else if ( dx > v.dx ) {
94 return 1;
95 } else if ( dx < v.dx ) {
96 return -1;
97 } else {
98 return 0;
99 }
100} /* Compare */
101
102
103bool Hep2Vector::operator > (const Hep2Vector & v) const {
104 return (compare(v) > 0);
105}
106bool Hep2Vector::operator < (const Hep2Vector & v) const {
107 return (compare(v) < 0);
108}
109bool Hep2Vector::operator>= (const Hep2Vector & v) const {
110 return (compare(v) >= 0);
111}
112bool Hep2Vector::operator<= (const Hep2Vector & v) const {
113 return (compare(v) <= 0);
114}
115
116bool Hep2Vector::isNear(const Hep2Vector & p, double epsilon) const {
117 double limit = dot(p)*epsilon*epsilon;
118 return ( (*this - p).mag2() <= limit );
119} /* isNear() */
120
121double Hep2Vector::howNear(const Hep2Vector & p ) const {
122 double d = (*this - p).mag2();
123 double pdp = dot(p);
124 if ( (pdp > 0) && (d < pdp) ) {
125 return std::sqrt (d/pdp);
126 } else if ( (pdp == 0) && (d == 0) ) {
127 return 0;
128 } else {
129 return 1;
130 }
131} /* howNear */
132
133double Hep2Vector::howParallel (const Hep2Vector & v) const {
134 // | V1 x V2 | / | V1 dot V2 |
135 // Of course, the "cross product" is fictitious but the math is valid
136 double v1v2 = std::fabs(dot(v));
137 if ( v1v2 == 0 ) {
138 // Zero is parallel to no other vector except for zero.
139 return ( (mag2() == 0) && (v.mag2() == 0) ) ? 0 : 1;
140 }
141 double abscross = std::fabs ( dx * v.y() - dy - v.x() );
142 if ( abscross >= v1v2 ) {
143 return 1;
144 } else {
145 return abscross/v1v2;
146 }
147} /* howParallel() */
148
150 double epsilon) const {
151 // | V1 x V2 | <= epsilon * | V1 dot V2 |
152 // Of course, the "cross product" is fictitious but the math is valid
153 double v1v2 = std::fabs(dot(v));
154 if ( v1v2 == 0 ) {
155 // Zero is parallel to no other vector except for zero.
156 return ( (mag2() == 0) && (v.mag2() == 0) );
157 }
158 double abscross = std::fabs ( dx * v.y() - dy - v.x() );
159 return ( abscross <= epsilon * v1v2 );
160} /* isParallel() */
161
162double Hep2Vector::howOrthogonal (const Hep2Vector & v) const {
163 // | V1 dot V2 | / | V1 x V2 |
164 // Of course, the "cross product" is fictitious but the math is valid
165 double v1v2 = std::fabs(dot(v));
166 if ( v1v2 == 0 ) {
167 return 0; // Even if one or both are 0, they are considered orthogonal
168 }
169 double abscross = std::fabs ( dx * v.y() - dy - v.x() );
170 if ( v1v2 >= abscross ) {
171 return 1;
172 } else {
173 return v1v2/abscross;
174 }
175} /* howOrthogonal() */
176
178 double epsilon) const {
179 // | V1 dot V2 | <= epsilon * | V1 x V2 |
180 // Of course, the "cross product" is fictitious but the math is valid
181 double v1v2 = std::fabs(dot(v));
182 double abscross = std::fabs ( dx * v.y() - dy - v.x() );
183 return ( v1v2 <= epsilon * abscross );
184} /* isOrthogonal() */
185
186} // namespace CLHEP
G4double epsilon(G4double density, G4double temperature)
double mag2() const
bool isOrthogonal(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:177
bool operator<=(const Hep2Vector &v) const
Definition: TwoVector.cc:112
static double tolerance
Definition: TwoVector.h:201
double dot(const Hep2Vector &p) const
double howOrthogonal(const Hep2Vector &p) const
Definition: TwoVector.cc:162
double x() const
bool isNear(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:116
double howNear(const Hep2Vector &p) const
Definition: TwoVector.cc:121
double howParallel(const Hep2Vector &p) const
Definition: TwoVector.cc:133
int compare(const Hep2Vector &v) const
Definition: TwoVector.cc:88
double y() const
bool isParallel(const Hep2Vector &p, double epsilon=tolerance) const
Definition: TwoVector.cc:149
bool operator>(const Hep2Vector &v) const
Definition: TwoVector.cc:103
void set(double x, double y)
double operator()(int i) const
Definition: TwoVector.cc:27
bool operator>=(const Hep2Vector &v) const
Definition: TwoVector.cc:109
void rotate(double)
Definition: TwoVector.cc:53
static double setTolerance(double tol)
Definition: TwoVector.cc:20
bool operator<(const Hep2Vector &v) const
Definition: TwoVector.cc:106
Definition: DoubConv.h:17
void ZMinput2doubles(std::istream &is, const char *type, double &x, double &y)
Definition: ZMinput.cc:243
std::istream & operator>>(std::istream &is, HepRandom &dist)
Definition: Random.cc:223
std::ostream & operator<<(std::ostream &os, const HepRandom &dist)
Definition: Random.cc:219
HepLorentzVector operator/(const HepLorentzVector &, double a)