Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Friends
UVector2 Class Reference

#include <UVector2.hh>

Public Types

enum  { X = 0, Y = 1, NUM_COORDINATES = 2, SIZE = NUM_COORDINATES }
 
enum  { ZMpvToleranceTicks = 100 }
 

Public Member Functions

 UVector2 (double x=0.0, double y=0.0)
 
 UVector2 (const UVector2 &p)
 
 UVector2 (const UVector3 &s)
 
 ~UVector2 ()
 
double operator() (int i) const
 
double operator[] (int i) const
 
double & operator() (int i)
 
double & operator[] (int i)
 
void setX (double x)
 
void setY (double y)
 
void set (double x, double y)
 
double phi () const
 
double mag2 () const
 
double mag () const
 
double r () const
 
void setPhi (double phi)
 
void setMag (double r)
 
void setR (double r)
 
void setPolar (double r, double phi)
 
UVector2operator= (const UVector2 &p)
 
bool operator== (const UVector2 &v) const
 
bool operator!= (const UVector2 &v) const
 
int compare (const UVector2 &v) const
 
bool operator> (const UVector2 &v) const
 
bool operator< (const UVector2 &v) const
 
bool operator>= (const UVector2 &v) const
 
bool operator<= (const UVector2 &v) const
 
double howNear (const UVector2 &p) const
 
bool isNear (const UVector2 &p, double epsilon=tolerance) const
 
double howParallel (const UVector2 &p) const
 
bool isParallel (const UVector2 &p, double epsilon=tolerance) const
 
double howOrthogonal (const UVector2 &p) const
 
bool isOrthogonal (const UVector2 &p, double epsilon=tolerance) const
 
UVector2operator+= (const UVector2 &p)
 
UVector2operator-= (const UVector2 &p)
 
UVector2 operator- () const
 
UVector2operator*= (double a)
 
UVector2 unit () const
 
UVector2 orthogonal () const
 
double dot (const UVector2 &p) const
 
double angle (const UVector2 &) const
 
void rotate (double)
 
 operator UVector3 () const
 

Static Public Member Functions

static double getTolerance ()
 
static double setTolerance (double tol)
 

Data Fields

double x
 
double y
 

Friends

std::ostream & operator<< (std::ostream &, const UVector2 &)
 
double operator* (const UVector2 &a, const UVector2 &b)
 
UVector2 operator* (const UVector2 &p, double a)
 
UVector2 operator* (double a, const UVector2 &p)
 
UVector2 operator/ (const UVector2 &p, double a)
 
UVector2 operator+ (const UVector2 &a, const UVector2 &b)
 
UVector2 operator- (const UVector2 &a, const UVector2 &b)
 

Detailed Description

Author

Definition at line 45 of file UVector2.hh.

Member Enumeration Documentation

anonymous enum
Enumerator
NUM_COORDINATES 
SIZE 

Definition at line 50 of file UVector2.hh.

anonymous enum
Enumerator
ZMpvToleranceTicks 

Definition at line 193 of file UVector2.hh.

Constructor & Destructor Documentation

UVector2::UVector2 ( double  x = 0.0,
double  y = 0.0 
)
inline

Definition at line 220 of file UVector2.hh.

Referenced by operator-(), orthogonal(), and unit().

221  : x(x1), y(y1) {}
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2::UVector2 ( const UVector2 p)
inline

Definition at line 251 of file UVector2.hh.

252  : x(p.x), y(p.y) {}
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2::UVector2 ( const UVector3 s)
inlineexplicit

Definition at line 223 of file UVector2.hh.

224  : x(s.x), y(s.y) {}
double x
Definition: UVector3.hh:136
double x
Definition: UVector2.hh:195
double y
Definition: UVector3.hh:137
double y
Definition: UVector2.hh:196
UVector2::~UVector2 ( )
inline

Definition at line 254 of file UVector2.hh.

254 {}

Member Function Documentation

double UVector2::angle ( const UVector2 q) const
inline

Definition at line 345 of file UVector2.hh.

References dot(), and mag2().

346 {
347  double ptot2 = mag2() * q.mag2();
348  return ptot2 <= 0.0 ? 0.0 : std::acos(dot(q) / std::sqrt(ptot2));
349 }
double mag2() const
Definition: UVector2.hh:305
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
int UVector2::compare ( const UVector2 v) const

Definition at line 105 of file UVector2.cc.

References x, and y.

Referenced by operator<(), operator<=(), operator>(), and operator>=().

106 {
107  if (y > v.y)
108  {
109  return 1;
110  }
111  else if (y < v.y)
112  {
113  return -1;
114  }
115  else if (x > v.x)
116  {
117  return 1;
118  }
119  else if (x < v.x)
120  {
121  return -1;
122  }
123  else
124  {
125  return 0;
126  }
127 } /* Compare */
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double UVector2::dot ( const UVector2 p) const
inline

Definition at line 300 of file UVector2.hh.

References x, and y.

Referenced by angle(), howNear(), howOrthogonal(), howParallel(), isNear(), isOrthogonal(), isParallel(), and operator*().

301 {
302  return x * p.x + y * p.y;
303 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double UVector2::getTolerance ( )
inlinestatic

Definition at line 401 of file UVector2.hh.

402 {
403  return tolerance;
404 }
double UVector2::howNear ( const UVector2 p) const

Definition at line 153 of file UVector2.cc.

References dot(), and mag2().

154 {
155  double d = (*this - p).mag2();
156  double pdp = dot(p);
157  if ((pdp > 0) && (d < pdp))
158  {
159  return std::sqrt(d / pdp);
160  }
161  else if ((pdp == 0) && (d == 0))
162  {
163  return 0;
164  }
165  else
166  {
167  return 1;
168  }
169 } /* howNear */
const char * p
Definition: xmltok.h:285
double mag2() const
Definition: UVector2.hh:305
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double UVector2::howOrthogonal ( const UVector2 p) const

Definition at line 207 of file UVector2.cc.

References dot(), x, and y.

208 {
209  // | V1 dot V2 | / | V1 x V2 |
210  // Of course, the "cross product" is fictitious but the math is valid
211  double v1v2 = std::fabs(dot(v));
212  if (v1v2 == 0)
213  {
214  return 0; // Even if one or both are 0, they are considered orthogonal
215  }
216  double abscross = std::fabs(x * v.y - y - v.x);
217  if (v1v2 >= abscross)
218  {
219  return 1;
220  }
221  else
222  {
223  return v1v2 / abscross;
224  }
225 } /* howOrthogonal() */
double x
Definition: UVector2.hh:195
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double y
Definition: UVector2.hh:196
double UVector2::howParallel ( const UVector2 p) const

Definition at line 171 of file UVector2.cc.

References dot(), mag2(), x, and y.

172 {
173  // | V1 x V2 | / | V1 dot V2 |
174  // Of course, the "cross product" is fictitious but the math is valid
175  double v1v2 = std::fabs(dot(v));
176  if (v1v2 == 0)
177  {
178  // Zero is parallel to no other vector except for zero.
179  return ((mag2() == 0) && (v.mag2() == 0)) ? 0 : 1;
180  }
181  double abscross = std::fabs(x * v.y - y - v.x);
182  if (abscross >= v1v2)
183  {
184  return 1;
185  }
186  else
187  {
188  return abscross / v1v2;
189  }
190 } /* howParallel() */
double x
Definition: UVector2.hh:195
double mag2() const
Definition: UVector2.hh:305
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double y
Definition: UVector2.hh:196
bool UVector2::isNear ( const UVector2 p,
double  epsilon = tolerance 
) const

Definition at line 147 of file UVector2.cc.

References dot(), and mag2().

148 {
149  double limit = dot(p) * epsilon * epsilon;
150  return ((*this - p).mag2() <= limit);
151 } /* isNear() */
double mag2() const
Definition: UVector2.hh:305
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
bool UVector2::isOrthogonal ( const UVector2 p,
double  epsilon = tolerance 
) const

Definition at line 227 of file UVector2.cc.

References dot(), x, and y.

229 {
230  // | V1 dot V2 | <= epsilon * | V1 x V2 |
231  // Of course, the "cross product" is fictitious but the math is valid
232  double v1v2 = std::fabs(dot(v));
233  double abscross = std::fabs(x * v.y - y - v.x);
234  return (v1v2 <= epsilon * abscross);
235 } /* isOrthogonal() */
double x
Definition: UVector2.hh:195
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double y
Definition: UVector2.hh:196
bool UVector2::isParallel ( const UVector2 p,
double  epsilon = tolerance 
) const

Definition at line 192 of file UVector2.cc.

References dot(), mag2(), x, and y.

194 {
195  // | V1 x V2 | <= epsilon * | V1 dot V2 |
196  // Of course, the "cross product" is fictitious but the math is valid
197  double v1v2 = std::fabs(dot(v));
198  if (v1v2 == 0)
199  {
200  // Zero is parallel to no other vector except for zero.
201  return ((mag2() == 0) && (v.mag2() == 0));
202  }
203  double abscross = std::fabs(x * v.y - y - v.x);
204  return (abscross <= epsilon * v1v2);
205 } /* isParallel() */
double x
Definition: UVector2.hh:195
double mag2() const
Definition: UVector2.hh:305
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double y
Definition: UVector2.hh:196
double UVector2::mag ( ) const
inline

Definition at line 310 of file UVector2.hh.

References mag2().

Referenced by setPhi().

311 {
312  return std::sqrt(mag2());
313 }
double mag2() const
Definition: UVector2.hh:305
double UVector2::mag2 ( ) const
inline

Definition at line 305 of file UVector2.hh.

References x, and y.

Referenced by angle(), howNear(), howParallel(), isNear(), isParallel(), mag(), r(), and unit().

306 {
307  return x * x + y * y;
308 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2::operator UVector3 ( ) const

Definition at line 100 of file UVector2.cc.

References test::x.

101 {
102  return UVector3(x, y, 0.0);
103 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
bool UVector2::operator!= ( const UVector2 v) const
inline

Definition at line 269 of file UVector2.hh.

References x, and y.

270 {
271  return (v.x != x || v.y != y) ? true : false;
272 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double UVector2::operator() ( int  i) const

Definition at line 31 of file UVector2.cc.

References x, and y.

Referenced by operator[]().

32 {
33  if (i == 0)
34  {
35  return x;
36  }
37  else if (i == 1)
38  {
39  return y;
40  }
41  else
42  {
43  // ZMthrowA(ZMxpvIndexRange("UVector2::operator(): bad index"));
44  return 0.0;
45  }
46 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double & UVector2::operator() ( int  i)

Definition at line 48 of file UVector2.cc.

References X, x, Y, and y.

49 {
50  static double dummy;
51 
52  switch (i)
53  {
54  case X:
55  return x;
56  case Y:
57  return y;
58  default:
59  // ZMthrowA (ZMxpvIndexRange("UVector2::operator() : bad index"));
60  return dummy;
61  }
62 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 & UVector2::operator*= ( double  a)
inline

Definition at line 293 of file UVector2.hh.

References test::a, x, and y.

294 {
295  x *= a;
296  y *= a;
297  return *this;
298 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 & UVector2::operator+= ( const UVector2 p)
inline

Definition at line 274 of file UVector2.hh.

References x, and y.

275 {
276  x += p.x;
277  y += p.y;
278  return *this;
279 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 UVector2::operator- ( ) const
inline

Definition at line 288 of file UVector2.hh.

References UVector2(), x, and y.

289 {
290  return UVector2(-x, -y);
291 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 & UVector2::operator-= ( const UVector2 p)
inline

Definition at line 281 of file UVector2.hh.

References x, and y.

282 {
283  x -= p.x;
284  y -= p.y;
285  return *this;
286 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
bool UVector2::operator< ( const UVector2 v) const

Definition at line 134 of file UVector2.cc.

References compare().

135 {
136  return (compare(v) < 0);
137 }
int compare(const UVector2 &v) const
Definition: UVector2.cc:105
bool UVector2::operator<= ( const UVector2 v) const

Definition at line 142 of file UVector2.cc.

References compare().

143 {
144  return (compare(v) <= 0);
145 }
int compare(const UVector2 &v) const
Definition: UVector2.cc:105
UVector2 & UVector2::operator= ( const UVector2 p)
inline

Definition at line 256 of file UVector2.hh.

References x, and y.

257 {
258  if (this == &p) { return *this; }
259  x = p.x;
260  y = p.y;
261  return *this;
262 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
bool UVector2::operator== ( const UVector2 v) const
inline

Definition at line 264 of file UVector2.hh.

References x, and y.

265 {
266  return (v.x == x && v.y == y) ? true : false;
267 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
bool UVector2::operator> ( const UVector2 v) const

Definition at line 130 of file UVector2.cc.

References compare().

131 {
132  return (compare(v) > 0);
133 }
int compare(const UVector2 &v) const
Definition: UVector2.cc:105
bool UVector2::operator>= ( const UVector2 v) const

Definition at line 138 of file UVector2.cc.

References compare().

139 {
140  return (compare(v) >= 0);
141 }
int compare(const UVector2 &v) const
Definition: UVector2.cc:105
double UVector2::operator[] ( int  i) const
inline

Definition at line 246 of file UVector2.hh.

References operator()().

247 {
248  return operator()(i);
249 }
double operator()(int i) const
Definition: UVector2.cc:31
double & UVector2::operator[] ( int  i)
inline

Definition at line 242 of file UVector2.hh.

References operator()().

243 {
244  return operator()(i);
245 }
double operator()(int i) const
Definition: UVector2.cc:31
UVector2 UVector2::orthogonal ( ) const
inline

Definition at line 327 of file UVector2.hh.

References UVector2(), x, and y.

328 {
329  double x1 = std::fabs(x), y1 = std::fabs(y);
330  if (x1 < y1)
331  {
332  return UVector2(y, -x);
333  }
334  else
335  {
336  return UVector2(-y, x);
337  }
338 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double UVector2::phi ( ) const
inline

Definition at line 340 of file UVector2.hh.

References x, and y.

Referenced by setMag().

341 {
342  return x == 0.0 && y == 0.0 ? 0.0 : std::atan2(y, x);
343 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
double UVector2::r ( ) const
inline

Definition at line 315 of file UVector2.hh.

References mag2().

316 {
317  return std::sqrt(mag2());
318 }
double mag2() const
Definition: UVector2.hh:305
void UVector2::rotate ( double  angler)

Definition at line 64 of file UVector2.cc.

References test::c, x, and y.

65 {
66  double s = std::sin(angler);
67  double c = std::cos(angler);
68  double xx = x;
69  x = c * xx - s * y;
70  y = s * xx + c * y;
71 }
const XML_Char * s
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
void UVector2::set ( double  x,
double  y 
)
inline

Definition at line 236 of file UVector2.hh.

References x, and y.

237 {
238  x = x1;
239  y = y1;
240 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
void UVector2::setMag ( double  r)
inline

Definition at line 351 of file UVector2.hh.

References phi(), setX(), and setY().

Referenced by setR().

352 {
353  double ph = phi();
354  setX(r1 * std::cos(ph));
355  setY(r1 * std::sin(ph));
356 }
void setX(double x)
Definition: UVector2.hh:226
void setY(double y)
Definition: UVector2.hh:231
double phi() const
Definition: UVector2.hh:340
void UVector2::setPhi ( double  phi)
inline

Definition at line 363 of file UVector2.hh.

References mag(), setX(), and setY().

364 {
365  double ma = mag();
366  setX(ma * std::cos(phi1));
367  setY(ma * std::sin(phi1));
368 }
void setX(double x)
Definition: UVector2.hh:226
double mag() const
Definition: UVector2.hh:310
void setY(double y)
Definition: UVector2.hh:231
void UVector2::setPolar ( double  r,
double  phi 
)
inline

Definition at line 370 of file UVector2.hh.

References setX(), and setY().

371 {
372  setX(r1 * std::cos(phi1));
373  setY(r1 * std::sin(phi1));
374 }
void setX(double x)
Definition: UVector2.hh:226
void setY(double y)
Definition: UVector2.hh:231
void UVector2::setR ( double  r)
inline

Definition at line 358 of file UVector2.hh.

References setMag().

359 {
360  setMag(r1);
361 }
void setMag(double r)
Definition: UVector2.hh:351
double UVector2::setTolerance ( double  tol)
static

Definition at line 23 of file UVector2.cc.

24 {
25  // Set the tolerance for UVector2s to be considered near one another
26  double oldTolerance(tolerance);
27  tolerance = tol;
28  return oldTolerance;
29 }
void UVector2::setX ( double  x)
inline

Definition at line 226 of file UVector2.hh.

References x.

Referenced by setMag(), setPhi(), and setPolar().

227 {
228  x = x1;
229 }
double x
Definition: UVector2.hh:195
void UVector2::setY ( double  y)
inline

Definition at line 231 of file UVector2.hh.

References y.

Referenced by setMag(), setPhi(), and setPolar().

232 {
233  y = y1;
234 }
double y
Definition: UVector2.hh:196
UVector2 UVector2::unit ( ) const
inline

Definition at line 320 of file UVector2.hh.

References mag2(), and UVector2().

321 {
322  double tot = mag2();
323  UVector2 p(*this);
324  return tot > 0.0 ? p *= (1.0 / std::sqrt(tot)) : UVector2(1, 0);
325 }
const char * p
Definition: xmltok.h:285
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double mag2() const
Definition: UVector2.hh:305

Friends And Related Function Documentation

double operator* ( const UVector2 a,
const UVector2 b 
)
friend

Definition at line 396 of file UVector2.hh.

397 {
398  return a.dot(b);
399 }
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
UVector2 operator* ( const UVector2 p,
double  a 
)
friend

Definition at line 386 of file UVector2.hh.

387 {
388  return UVector2(a * p.x, a * p.y);
389 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 operator* ( double  a,
const UVector2 p 
)
friend

Definition at line 391 of file UVector2.hh.

392 {
393  return UVector2(a * p.x, a * p.y);
394 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 operator+ ( const UVector2 a,
const UVector2 b 
)
friend

Definition at line 376 of file UVector2.hh.

377 {
378  return UVector2(a.x + b.x, a.y + b.y);
379 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 operator- ( const UVector2 a,
const UVector2 b 
)
friend

Definition at line 381 of file UVector2.hh.

382 {
383  return UVector2(a.x - b.x, a.y - b.y);
384 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
UVector2 operator/ ( const UVector2 p,
double  a 
)
friend

Definition at line 73 of file UVector2.cc.

74 {
75  if (a == 0)
76  {
77  // ZMthrowA(ZMxpvInfiniteVector( "Division of UVector2 by zero"));
78  }
79  return UVector2(p.x / a, p.y / a);
80 }
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196
std::ostream& operator<< ( std::ostream &  os,
const UVector2 q 
)
friend

Definition at line 82 of file UVector2.cc.

83 {
84  os << "(" << q.x << ", " << q.y << ")";
85  return os;
86 }
double x
Definition: UVector2.hh:195
double y
Definition: UVector2.hh:196

Field Documentation

double UVector2::x
double UVector2::y

The documentation for this class was generated from the following files: