Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicVector3D.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id:$
3 // ---------------------------------------------------------------------------
4 //
5 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
6 //
7 // History:
8 // 12.06.01 E.Chernyaev - CLHEP-1.7: initial version
9 // 14.03.03 E.Chernyaev - CLHEP-1.9: template version
10 //
11 
12 #ifndef BASIC_VECTOR3D_H
13 #define BASIC_VECTOR3D_H
14 
15 #include <iosfwd>
17 
18 namespace HepGeom {
27  template<class T> class BasicVector3D {
28  protected:
29  T v_[3];
30 
35  BasicVector3D() { v_[0] = 0; v_[1] = 0; v_[2] = 0; }
36 
37  public:
41  enum {
42  X = 0,
43  Y = 1,
44  Z = 2,
47  };
48 
51  BasicVector3D(T x1, T y1, T z1) { v_[0] = x1; v_[1] = y1; v_[2] = z1; }
52 
61  v_[0] = v.x(); v_[1] = v.y(); v_[2] = v.z();
62  }
63 
66  virtual ~BasicVector3D() {}
67 
68  // -------------------------
69  // Interface to "good old C"
70  // -------------------------
71 
74  operator T * () { return v_; }
75 
78  operator const T * () const { return v_; }
79 
85  operator CLHEP::Hep3Vector () const { return CLHEP::Hep3Vector(x(),y(),z()); }
86 
87  // -----------------------------
88  // General arithmetic operations
89  // -----------------------------
90 
94  v_[0] = v.v_[0]; v_[1] = v.v_[1]; v_[2] = v.v_[2]; return *this;
95  }
99  v_[0] += v.v_[0]; v_[1] += v.v_[1]; v_[2] += v.v_[2]; return *this;
100  }
104  v_[0] -= v.v_[0]; v_[1] -= v.v_[1]; v_[2] -= v.v_[2]; return *this;
105  }
109  v_[0] *= a; v_[1] *= a; v_[2] *= a; return *this;
110  }
114  v_[0] /= a; v_[1] /= a; v_[2] /= a; return *this;
115  }
116 
117  // ------------
118  // Subscripting
119  // ------------
120 
123  T operator()(int i) const { return v_[i]; }
126  T operator[](int i) const { return v_[i]; }
127 
130  T & operator()(int i) { return v_[i]; }
133  T & operator[](int i) { return v_[i]; }
134 
135  // ------------------------------------
136  // Cartesian coordinate system: x, y, z
137  // ------------------------------------
138 
141  T x() const { return v_[0]; }
144  T y() const { return v_[1]; }
147  T z() const { return v_[2]; }
148 
151  void setX(T a) { v_[0] = a; }
154  void setY(T a) { v_[1] = a; }
157  void setZ(T a) { v_[2] = a; }
158 
161  void set(T x1, T y1, T z1) { v_[0] = x1; v_[1] = y1; v_[2] = z1; }
162 
163  // ------------------------------------------
164  // Cylindrical coordinate system: rho, phi, z
165  // ------------------------------------------
166 
169  T perp2() const { return x()*x()+y()*y(); }
172  T perp() const { return std::sqrt(perp2()); }
175  T rho() const { return perp(); }
176 
179  void setPerp(T rh) {
180  T factor = perp();
181  if (factor > 0) {
182  factor = rh/factor; v_[0] *= factor; v_[1] *= factor;
183  }
184  }
185 
186  // ------------------------------------------
187  // Spherical coordinate system: r, phi, theta
188  // ------------------------------------------
189 
192  T mag2() const { return x()*x()+y()*y()+z()*z(); }
195  T mag() const { return std::sqrt(mag2()); }
198  T r() const { return mag(); }
201  T phi() const {
202  return x() == 0 && y() == 0 ? 0 : std::atan2(y(),x());
203  }
206  T theta() const {
207  return x() == 0 && y() == 0 && z() == 0 ? 0 : std::atan2(perp(),z());
208  }
211  T cosTheta() const { T ma = mag(); return ma == 0 ? 1 : z()/ma; }
212 
215  T getR() const { return r(); }
218  T getPhi() const { return phi(); }
221  T getTheta() const { return theta(); }
222 
225  void setMag(T ma) {
226  T factor = mag();
227  if (factor > 0) {
228  factor = ma/factor; v_[0] *= factor; v_[1] *= factor; v_[2] *= factor;
229  }
230  }
233  void setR(T ma) { setMag(ma); }
236  void setPhi(T ph) { T xy = perp(); setX(xy*std::cos(ph)); setY(xy*std::sin(ph)); }
239  void setTheta(T th) {
240  T ma = mag();
241  T ph = phi();
242  set(ma*std::sin(th)*std::cos(ph), ma*std::sin(th)*std::sin(ph), ma*std::cos(th));
243  }
244 
245  // ---------------
246  // Pseudo rapidity
247  // ---------------
248 
251  T pseudoRapidity() const;
254  T eta() const { return pseudoRapidity(); }
257  T getEta() const { return pseudoRapidity(); }
258 
261  void setEta(T a);
262 
263  // -------------------
264  // Combine two vectors
265  // -------------------
266 
269  T dot(const BasicVector3D<T> & v) const {
270  return x()*v.x()+y()*v.y()+z()*v.z();
271  }
272 
276  return BasicVector3D<T>(y()*v.z()-v.y()*z(),
277  z()*v.x()-v.z()*x(),
278  x()*v.y()-v.x()*y());
279  }
280 
283  T perp2(const BasicVector3D<T> & v) const {
284  T tot = v.mag2(), s = dot(v);
285  return tot > 0 ? mag2()-s*s/tot : mag2();
286  }
287 
290  T perp(const BasicVector3D<T> & v) const {
291  return std::sqrt(perp2(v));
292  }
293 
296  T angle(const BasicVector3D<T> & v) const;
297 
298  // ---------------
299  // Related vectors
300  // ---------------
301 
305  T len = mag();
306  return (len > 0) ?
308  }
309 
313  T dx = x() < 0 ? -x() : x();
314  T dy = y() < 0 ? -y() : y();
315  T dz = z() < 0 ? -z() : z();
316  if (dx < dy) {
317  return dx < dz ?
318  BasicVector3D<T>(0,z(),-y()) : BasicVector3D<T>(y(),-x(),0);
319  }else{
320  return dy < dz ?
321  BasicVector3D<T>(-z(),0,x()) : BasicVector3D<T>(y(),-x(),0);
322  }
323  }
324 
325  // ---------
326  // Rotations
327  // ---------
328 
331  BasicVector3D<T> & rotateX(T a);
334  BasicVector3D<T> & rotateY(T a);
337  BasicVector3D<T> & rotateZ(T a);
340  BasicVector3D<T> & rotate(T a, const BasicVector3D<T> & v);
341  };
342 
343  /*************************************************************************
344  * *
345  * Non-member functions for BasicVector3D<float> *
346  * *
347  *************************************************************************/
348 
353  std::ostream &
354  operator<<(std::ostream &, const BasicVector3D<float> &);
355 
360  std::istream &
361  operator>>(std::istream &, BasicVector3D<float> &);
362 
367  inline BasicVector3D<float>
368  operator+(const BasicVector3D<float> & v) { return v; }
369 
374  inline BasicVector3D<float>
376  return BasicVector3D<float>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
377  }
378 
383  inline BasicVector3D<float>
385  return BasicVector3D<float>(-v.x(), -v.y(), -v.z());
386  }
387 
392  inline BasicVector3D<float>
394  return BasicVector3D<float>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
395  }
396 
401  inline BasicVector3D<float>
402  operator*(const BasicVector3D<float> & v, double a) {
403  return BasicVector3D<float>(v.x()*static_cast<float>(a), v.y()*static_cast<float>(a), v.z()*static_cast<float>(a));
404  }
405 
410  inline float
412  return a.dot(b);
413  }
414 
419  inline BasicVector3D<float>
420  operator*(double a, const BasicVector3D<float> & v) {
421  return BasicVector3D<float>(static_cast<float>(a)*v.x(), static_cast<float>(a)*v.y(), static_cast<float>(a)*v.z());
422  }
423 
428  inline BasicVector3D<float>
429  operator/(const BasicVector3D<float> & v, double a) {
430  return BasicVector3D<float>(v.x()/static_cast<float>(a), v.y()/static_cast<float>(a), v.z()/static_cast<float>(a));
431  }
432 
437  inline bool
439  return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z());
440  }
441 
446  inline bool
448  return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z());
449  }
450 
451  /*************************************************************************
452  * *
453  * Non-member functions for BasicVector3D<double> *
454  * *
455  *************************************************************************/
456 
461  std::ostream &
462  operator<<(std::ostream &, const BasicVector3D<double> &);
463 
468  std::istream &
469  operator>>(std::istream &, BasicVector3D<double> &);
470 
475  inline BasicVector3D<double>
476  operator+(const BasicVector3D<double> & v) { return v; }
477 
482  inline BasicVector3D<double>
484  return BasicVector3D<double>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
485  }
486 
491  inline BasicVector3D<double>
493  return BasicVector3D<double>(-v.x(), -v.y(), -v.z());
494  }
495 
500  inline BasicVector3D<double>
502  return BasicVector3D<double>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
503  }
504 
509  inline BasicVector3D<double>
510  operator*(const BasicVector3D<double> & v, double a) {
511  return BasicVector3D<double>(v.x()*a, v.y()*a, v.z()*a);
512  }
513 
518  inline double
520  return a.dot(b);
521  }
522 
527  inline BasicVector3D<double>
528  operator*(double a, const BasicVector3D<double> & v) {
529  return BasicVector3D<double>(a*v.x(), a*v.y(), a*v.z());
530  }
531 
536  inline BasicVector3D<double>
537  operator/(const BasicVector3D<double> & v, double a) {
538  return BasicVector3D<double>(v.x()/a, v.y()/a, v.z()/a);
539  }
540 
545  inline bool
547  {
548  return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z());
549  }
550 
555  inline bool
557  {
558  return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z());
559  }
560 } /* namespace HepGeom */
561 
562 #endif /* BASIC_VECTOR3D_H */
const XML_Char int len
Definition: expat.h:262
bool operator!=(const BasicVector3D< float > &a, const BasicVector3D< float > &b)
BasicVector3D< double > operator*(const BasicVector3D< double > &v, double a)
BasicVector3D< T > & rotate(T a, const BasicVector3D< T > &v)
BasicVector3D< float > operator*(double a, const BasicVector3D< float > &v)
BasicVector3D< double > operator/(const BasicVector3D< double > &v, double a)
void set(T x1, T y1, T z1)
BasicVector3D< double > operator+(const BasicVector3D< double > &v)
bool operator==(const BasicVector3D< double > &a, const BasicVector3D< double > &b)
BasicVector3D(const BasicVector3D< float > &v)
Definition: BasicVector3D.h:60
T operator()(int i) const
BasicVector3D< double > operator*(double a, const BasicVector3D< double > &v)
BasicVector3D< T > unit() const
BasicVector3D< T > & operator=(const BasicVector3D< T > &v)
Definition: BasicVector3D.h:93
BasicVector3D< double > operator-(const BasicVector3D< double > &a, const BasicVector3D< double > &b)
BasicVector3D< float > operator-(const BasicVector3D< float > &v)
BasicVector3D< float > operator+(const BasicVector3D< float > &v)
BasicVector3D< double > operator-(const BasicVector3D< double > &v)
BasicVector3D< T > & operator+=(const BasicVector3D< T > &v)
Definition: BasicVector3D.h:98
double operator*(const BasicVector3D< double > &a, const BasicVector3D< double > &b)
T operator[](int i) const
BasicVector3D< double > operator+(const BasicVector3D< double > &a, const BasicVector3D< double > &b)
bool operator!=(const BasicVector3D< double > &a, const BasicVector3D< double > &b)
T perp(const BasicVector3D< T > &v) const
const XML_Char * s
Definition: expat.h:262
float operator*(const BasicVector3D< float > &a, const BasicVector3D< float > &b)
BasicVector3D< T > & operator-=(const BasicVector3D< T > &v)
bool operator==(const BasicVector3D< float > &a, const BasicVector3D< float > &b)
BasicVector3D< float > operator-(const BasicVector3D< float > &a, const BasicVector3D< float > &b)
T angle(const BasicVector3D< T > &v) const
BasicVector3D< T > & rotateX(T a)
BasicVector3D< T > orthogonal() const
BasicVector3D< float > operator*(const BasicVector3D< float > &v, double a)
BasicVector3D< T > & operator*=(double a)
std::istream & operator>>(std::istream &is, BasicVector3D< float > &a)
BasicVector3D< float > operator/(const BasicVector3D< float > &v, double a)
BasicVector3D(T x1, T y1, T z1)
Definition: BasicVector3D.h:51
BasicVector3D< float > operator+(const BasicVector3D< float > &a, const BasicVector3D< float > &b)
T perp2(const BasicVector3D< T > &v) const
T dot(const BasicVector3D< T > &v) const
BasicVector3D< T > & rotateY(T a)
BasicVector3D< T > cross(const BasicVector3D< T > &v) const
BasicVector3D< T > & operator/=(double a)
BasicVector3D< T > & rotateZ(T a)