Geant4  10.00.p03
UVector2.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UVector2
12 //
13 // Class description:
14 //
15 // UVector2 is a general 2-vector class defining vectors in two
16 // dimension using double components.
17 //
18 // 19.09.12 Marek Gayer
19 // Created from original implementation in CLHEP
20 // --------------------------------------------------------------------
21 
22 #ifndef UVECTOR2_H
23 #define UVECTOR2_H
24 
25 #include <cmath>
26 #include <iostream>
27 
28 #include "UVector3.hh"
29 
30 // Declarations of classes and global methods
31 class UVector2;
32 std::ostream& operator << (std::ostream&, const UVector2&);
33 //std::istream & operator >> (std::istream &, UVector2 &);
34 inline double operator * (const UVector2& a, const UVector2& b);
35 inline UVector2 operator * (const UVector2& p, double a);
36 inline UVector2 operator * (double a, const UVector2& p);
37 UVector2 operator / (const UVector2& p, double a);
38 inline UVector2 operator + (const UVector2& a, const UVector2& b);
39 inline UVector2 operator - (const UVector2& a, const UVector2& b);
40 
45 class UVector2
46 {
47 
48  public:
49 
50  enum { X = 0, Y = 1, NUM_COORDINATES = 2, SIZE = NUM_COORDINATES };
51  // Safe indexing of the coordinates when using with matrices, arrays, etc.
52 
53  inline UVector2(double x = 0.0, double y = 0.0);
54  // The constructor.
55 
56  inline UVector2(const UVector2& p);
57  // The copy constructor.
58 
59  explicit UVector2(const UVector3& s);
60  // "demotion" constructor"
61  // WARNING -- THIS IGNORES THE Z COMPONENT OF THE UVector3.
62  // SO IN GENERAL, UVector2(v)==v WILL NOT HOLD!
63 
64  inline ~UVector2();
65  // The destructor.
66 
67 // inline double x() const;
68 // inline double y() const;
69  // The components in cartesian coordinate system.
70 
71  double operator()(int i) const;
72  inline double operator [](int i) const;
73  // Get components by index. 0-based.
74 
75  double& operator()(int i);
76  inline double& operator [](int i);
77  // Set components by index. 0-based.
78 
79  inline void setX(double x);
80  inline void setY(double y);
81  inline void set(double x, double y);
82  // Set the components in cartesian coordinate system.
83 
84  inline double phi() const;
85  // The azimuth angle.
86 
87  inline double mag2() const;
88  // The magnitude squared.
89 
90  inline double mag() const;
91  // The magnitude.
92 
93  inline double r() const;
94  // r in polar coordinates (r, phi): equal to mag().
95 
96  inline void setPhi(double phi);
97  // Set phi keeping mag constant.
98 
99  inline void setMag(double r);
100  // Set magnitude keeping phi constant.
101 
102  inline void setR(double r);
103  // Set R keeping phi constant. Same as setMag.
104 
105  inline void setPolar(double r, double phi);
106  // Set by polar coordinates.
107 
108  inline UVector2& operator = (const UVector2& p);
109  // Assignment.
110 
111  inline bool operator == (const UVector2& v) const;
112  inline bool operator != (const UVector2& v) const;
113  // Comparisons.
114 
115  int compare(const UVector2& v) const;
116  bool operator > (const UVector2& v) const;
117  bool operator < (const UVector2& v) const;
118  bool operator>= (const UVector2& v) const;
119  bool operator<= (const UVector2& v) const;
120  // dictionary ordering according to y, then x component
121 
122  static inline double getTolerance();
123  static double setTolerance(double tol);
124 
125  double howNear(const UVector2& p) const;
126  bool isNear(const UVector2& p, double epsilon = tolerance) const;
127 
128  double howParallel(const UVector2& p) const;
129  bool isParallel
130  (const UVector2& p, double epsilon = tolerance) const;
131 
132  double howOrthogonal(const UVector2& p) const;
133  bool isOrthogonal
134  (const UVector2& p, double epsilon = tolerance) const;
135 
136  inline UVector2& operator += (const UVector2& p);
137  // Addition.
138 
139  inline UVector2& operator -= (const UVector2& p);
140  // Subtraction.
141 
142  inline UVector2 operator - () const;
143  // Unary minus.
144 
145  inline UVector2& operator *= (double a);
146  // Scaling with real numbers.
147 
148  inline UVector2 unit() const;
149  // Unit vector parallel to this.
150 
151  inline UVector2 orthogonal() const;
152  // Vector orthogonal to this.
153 
154  inline double dot(const UVector2& p) const;
155  // Scalar product.
156 
157  inline double angle(const UVector2&) const;
158  // The angle w.r.t. another 2-vector.
159 
160  void rotate(double);
161  // Rotates the UVector2.
162 
163  operator UVector3() const;
164  // Cast a UVector2 as a UVector3.
165 
166  // The remaining methods are friends, thus defined at global scope:
167  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168 
169  friend std::ostream& operator<< (std::ostream&, const UVector2&);
170  // Output to a stream.
171 
172  inline friend double operator * (const UVector2& a,
173  const UVector2& b);
174  // Scalar product.
175 
176  inline friend UVector2 operator * (const UVector2& p, double a);
177  // v*c
178 
179  inline friend UVector2 operator * (double a, const UVector2& p);
180  // c*v
181 
182  friend UVector2 operator / (const UVector2& p, double a);
183  // v/c
184 
185  inline friend UVector2 operator + (const UVector2& a,
186  const UVector2& b);
187  // v1+v2
188 
189  inline friend UVector2 operator - (const UVector2& a,
190  const UVector2& b);
191  // v1-v2
192 
193  enum { ZMpvToleranceTicks = 100 };
194 
195  double x;
196  double y;
197  // The components.
198 
199  private:
200 
201  static double tolerance;
202  // default tolerance criterion for isNear() to return true.
203 
204 }; // UVector2
205 
206 static const UVector2 X_HAT2(1.0, 0.0);
207 static const UVector2 Y_HAT2(0.0, 1.0);
208 
209 
210 /*
211 inline double UVector2::x() const {
212  return x;
213 }
214 
215 inline double UVector2::y() const {
216  return y;
217 }
218 */
219 
220 inline UVector2::UVector2(double x1, double y1)
221  : x(x1), y(y1) {}
222 
223 inline UVector2::UVector2(const UVector3& s1)
224  : x(s1.x), y(s1.y) {}
225 
226 inline void UVector2::setX(double x1)
227 {
228  x = x1;
229 }
230 
231 inline void UVector2::setY(double y1)
232 {
233  y = y1;
234 }
235 
236 inline void UVector2::set(double x1, double y1)
237 {
238  x = x1;
239  y = y1;
240 }
241 
242 double& UVector2::operator[](int i)
243 {
244  return operator()(i);
245 }
246 double UVector2::operator[](int i) const
247 {
248  return operator()(i);
249 }
250 
251 inline UVector2::UVector2(const UVector2& p)
252  : x(p.x), y(p.y) {}
253 
255 
257 {
258  if (this == &p) { return *this; }
259  x = p.x;
260  y = p.y;
261  return *this;
262 }
263 
264 inline bool UVector2::operator == (const UVector2& v) const
265 {
266  return (v.x == x && v.y == y) ? true : false;
267 }
268 
269 inline bool UVector2::operator != (const UVector2& v) const
270 {
271  return (v.x != x || v.y != y) ? true : false;
272 }
273 
275 {
276  x += p.x;
277  y += p.y;
278  return *this;
279 }
280 
282 {
283  x -= p.x;
284  y -= p.y;
285  return *this;
286 }
287 
289 {
290  return UVector2(-x, -y);
291 }
292 
294 {
295  x *= a;
296  y *= a;
297  return *this;
298 }
299 
300 inline double UVector2::dot(const UVector2& p) const
301 {
302  return x * p.x + y * p.y;
303 }
304 
305 inline double UVector2::mag2() const
306 {
307  return x * x + y * y;
308 }
309 
310 inline double UVector2::mag() const
311 {
312  return std::sqrt(mag2());
313 }
314 
315 inline double UVector2::r() const
316 {
317  return std::sqrt(mag2());
318 }
319 
320 inline UVector2 UVector2::unit() const
321 {
322  double tot = mag2();
323  UVector2 p(*this);
324  return tot > 0.0 ? p *= (1.0 / std::sqrt(tot)) : UVector2(1, 0);
325 }
326 
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 }
339 
340 inline double UVector2::phi() const
341 {
342  return x == 0.0 && y == 0.0 ? 0.0 : std::atan2(y, x);
343 }
344 
345 inline double UVector2::angle(const UVector2& q) const
346 {
347  double ptot2 = mag2() * q.mag2();
348  return ptot2 <= 0.0 ? 0.0 : std::acos(dot(q) / std::sqrt(ptot2));
349 }
350 
351 inline void UVector2::setMag(double r1)
352 {
353  double ph = phi();
354  setX(r1 * std::cos(ph));
355  setY(r1 * std::sin(ph));
356 }
357 
358 inline void UVector2::setR(double r1)
359 {
360  setMag(r1);
361 }
362 
363 inline void UVector2::setPhi(double phi1)
364 {
365  double ma = mag();
366  setX(ma * std::cos(phi1));
367  setY(ma * std::sin(phi1));
368 }
369 
370 inline void UVector2::setPolar(double r1, double phi1)
371 {
372  setX(r1 * std::cos(phi1));
373  setY(r1 * std::sin(phi1));
374 }
375 
376 inline UVector2 operator + (const UVector2& a, const UVector2& b)
377 {
378  return UVector2(a.x + b.x, a.y + b.y);
379 }
380 
381 inline UVector2 operator - (const UVector2& a, const UVector2& b)
382 {
383  return UVector2(a.x - b.x, a.y - b.y);
384 }
385 
386 inline UVector2 operator * (const UVector2& p, double a)
387 {
388  return UVector2(a * p.x, a * p.y);
389 }
390 
391 inline UVector2 operator * (double a, const UVector2& p)
392 {
393  return UVector2(a * p.x, a * p.y);
394 }
395 
396 inline double operator * (const UVector2& a, const UVector2& b)
397 {
398  return a.dot(b);
399 }
400 
401 inline double UVector2::getTolerance()
402 {
403  return tolerance;
404 }
405 
406 
407 #endif /* UVECTOR2_H */
std::ostream & operator<<(std::ostream &, const UVector2 &)
Definition: UVector2.cc:82
UVector2 & operator+=(const UVector2 &p)
Definition: UVector2.hh:274
double angle(const UVector2 &) const
Definition: UVector2.hh:345
double operator*(const UVector2 &a, const UVector2 &b)
Definition: UVector2.hh:396
static double setTolerance(double tol)
Definition: UVector2.cc:23
void setPhi(double phi)
Definition: UVector2.hh:363
void set(double x, double y)
Definition: UVector2.hh:236
friend double operator*(const UVector2 &a, const UVector2 &b)
Definition: UVector2.hh:396
friend UVector2 operator/(const UVector2 &p, double a)
Definition: UVector2.cc:73
UVector2 operator+(const UVector2 &a, const UVector2 &b)
Definition: UVector2.hh:376
UVector2 & operator-=(const UVector2 &p)
Definition: UVector2.hh:281
UVector2 operator-(const UVector2 &a, const UVector2 &b)
Definition: UVector2.hh:381
static const UVector2 Y_HAT2(0.0, 1.0)
G4double a
Definition: TRTMaterials.hh:39
UVector2 & operator=(const UVector2 &p)
Definition: UVector2.hh:256
bool operator==(const UVector2 &v) const
Definition: UVector2.hh:264
void rotate(double)
Definition: UVector2.cc:64
static const UVector2 X_HAT2(1.0, 0.0)
bool isParallel(const UVector2 &p, double epsilon=tolerance) const
Definition: UVector2.cc:192
void setX(double x)
Definition: UVector2.hh:226
bool isOrthogonal(const UVector2 &p, double epsilon=tolerance) const
Definition: UVector2.cc:227
UVector2(double x=0.0, double y=0.0)
Definition: UVector2.hh:220
static const double s
Definition: G4SIunits.hh:150
UVector2 & operator*=(double a)
Definition: UVector2.hh:293
int compare(const UVector2 &v) const
Definition: UVector2.cc:105
double howParallel(const UVector2 &p) const
Definition: UVector2.cc:171
friend UVector2 operator+(const UVector2 &a, const UVector2 &b)
Definition: UVector2.hh:376
~UVector2()
Definition: UVector2.hh:254
double operator[](int i) const
Definition: UVector2.hh:246
static double getTolerance()
Definition: UVector2.hh:401
static double tolerance
Definition: UVector2.hh:201
void setPolar(double r, double phi)
Definition: UVector2.hh:370
double howOrthogonal(const UVector2 &p) const
Definition: UVector2.cc:207
bool operator<(const UVector2 &v) const
Definition: UVector2.cc:134
double mag() const
Definition: UVector2.hh:310
bool isNear(const UVector2 &p, double epsilon=tolerance) const
Definition: UVector2.cc:147
UVector2 orthogonal() const
Definition: UVector2.hh:327
void setY(double y)
Definition: UVector2.hh:231
void setMag(double r)
Definition: UVector2.hh:351
double x
Definition: UVector2.hh:195
bool operator>=(const UVector2 &v) const
Definition: UVector2.cc:138
friend std::ostream & operator<<(std::ostream &, const UVector2 &)
Definition: UVector2.cc:82
double howNear(const UVector2 &p) const
Definition: UVector2.cc:153
double mag2() const
Definition: UVector2.hh:305
double phi() const
Definition: UVector2.hh:340
double r() const
Definition: UVector2.hh:315
bool operator>(const UVector2 &v) const
Definition: UVector2.cc:130
double operator()(int i) const
Definition: UVector2.cc:31
void setR(double r)
Definition: UVector2.hh:358
UVector2 operator/(const UVector2 &p, double a)
Definition: UVector2.cc:73
UVector2 unit() const
Definition: UVector2.hh:320
bool operator!=(const UVector2 &v) const
Definition: UVector2.hh:269
bool operator<=(const UVector2 &v) const
Definition: UVector2.cc:142
double dot(const UVector2 &p) const
Definition: UVector2.hh:300
double y
Definition: UVector2.hh:196
UVector2 operator-() const
Definition: UVector2.hh:288