ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BasicVector3D.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file BasicVector3D.h
1 // -*- C++ -*-
2 // ---------------------------------------------------------------------------
3 //
4 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
5 //
6 // History:
7 // 12.06.01 E.Chernyaev - CLHEP-1.7: initial version
8 // 14.03.03 E.Chernyaev - CLHEP-1.9: template version
9 //
10 
11 #ifndef BASIC_VECTOR3D_H
12 #define BASIC_VECTOR3D_H
13 
14 #include <iosfwd>
15 #include <type_traits>
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 
55  BasicVector3D(const BasicVector3D<T> &) = default;
56 
59  template<typename U = T,
60  typename = typename std::enable_if<!std::is_same<U,float>::value >::type>
62  v_[0] = v.x(); v_[1] = v.y(); v_[2] = v.z();
63  }
64 
67  BasicVector3D(BasicVector3D<T> &&) = default;
68 
71  virtual ~BasicVector3D() = default;
72 
73  // -------------------------
74  // Interface to "good old C"
75  // -------------------------
76 
79  operator T * () { return v_; }
80 
83  operator const T * () const { return v_; }
84 
90  operator CLHEP::Hep3Vector () const { return CLHEP::Hep3Vector(x(),y(),z()); }
91 
92  // -----------------------------
93  // General arithmetic operations
94  // -----------------------------
95 
98  BasicVector3D<T> & operator= (const BasicVector3D<T> &) = default;
105  v_[0] += v.v_[0]; v_[1] += v.v_[1]; v_[2] += v.v_[2]; return *this;
106  }
110  v_[0] -= v.v_[0]; v_[1] -= v.v_[1]; v_[2] -= v.v_[2]; return *this;
111  }
115  v_[0] *= a; v_[1] *= a; v_[2] *= a; return *this;
116  }
120  v_[0] /= a; v_[1] /= a; v_[2] /= a; return *this;
121  }
122 
123  // ------------
124  // Subscripting
125  // ------------
126 
129  T operator()(int i) const { return v_[i]; }
132  T operator[](int i) const { return v_[i]; }
133 
136  T & operator()(int i) { return v_[i]; }
139  T & operator[](int i) { return v_[i]; }
140 
141  // ------------------------------------
142  // Cartesian coordinate system: x, y, z
143  // ------------------------------------
144 
147  T x() const { return v_[0]; }
150  T y() const { return v_[1]; }
153  T z() const { return v_[2]; }
154 
157  void setX(T a) { v_[0] = a; }
160  void setY(T a) { v_[1] = a; }
163  void setZ(T a) { v_[2] = a; }
164 
167  void set(T x1, T y1, T z1) { v_[0] = x1; v_[1] = y1; v_[2] = z1; }
168 
169  // ------------------------------------------
170  // Cylindrical coordinate system: rho, phi, z
171  // ------------------------------------------
172 
175  T perp2() const { return x()*x()+y()*y(); }
178  T perp() const { return std::sqrt(perp2()); }
181  T rho() const { return perp(); }
182 
185  void setPerp(T rh) {
186  T factor = perp();
187  if (factor > 0) {
188  factor = rh/factor; v_[0] *= factor; v_[1] *= factor;
189  }
190  }
191 
192  // ------------------------------------------
193  // Spherical coordinate system: r, phi, theta
194  // ------------------------------------------
195 
198  T mag2() const { return x()*x()+y()*y()+z()*z(); }
201  T mag() const { return std::sqrt(mag2()); }
204  T r() const { return mag(); }
207  T phi() const {
208  return x() == 0 && y() == 0 ? 0 : std::atan2(y(),x());
209  }
212  T theta() const {
213  return x() == 0 && y() == 0 && z() == 0 ? 0 : std::atan2(perp(),z());
214  }
217  T cosTheta() const { T ma = mag(); return ma == 0 ? 1 : z()/ma; }
218 
221  T getR() const { return r(); }
224  T getPhi() const { return phi(); }
227  T getTheta() const { return theta(); }
228 
231  void setMag(T ma) {
232  T factor = mag();
233  if (factor > 0) {
234  factor = ma/factor; v_[0] *= factor; v_[1] *= factor; v_[2] *= factor;
235  }
236  }
239  void setR(T ma) { setMag(ma); }
242  void setPhi(T ph) { T xy = perp(); setX(xy*std::cos(ph)); setY(xy*std::sin(ph)); }
245  void setTheta(T th) {
246  T ma = mag();
247  T ph = phi();
248  set(ma*std::sin(th)*std::cos(ph), ma*std::sin(th)*std::sin(ph), ma*std::cos(th));
249  }
250 
251  // ---------------
252  // Pseudo rapidity
253  // ---------------
254 
257  T pseudoRapidity() const;
260  T eta() const { return pseudoRapidity(); }
263  T getEta() const { return pseudoRapidity(); }
264 
267  void setEta(T a);
268 
269  // -------------------
270  // Combine two vectors
271  // -------------------
272 
275  T dot(const BasicVector3D<T> & v) const {
276  return x()*v.x()+y()*v.y()+z()*v.z();
277  }
278 
282  return BasicVector3D<T>(y()*v.z()-v.y()*z(),
283  z()*v.x()-v.z()*x(),
284  x()*v.y()-v.x()*y());
285  }
286 
289  T perp2(const BasicVector3D<T> & v) const {
290  T tot = v.mag2(), s = dot(v);
291  return tot > 0 ? mag2()-s*s/tot : mag2();
292  }
293 
296  T perp(const BasicVector3D<T> & v) const {
297  return std::sqrt(perp2(v));
298  }
299 
302  T angle(const BasicVector3D<T> & v) const;
303 
304  // ---------------
305  // Related vectors
306  // ---------------
307 
311  T len = mag();
312  return (len > 0) ?
314  }
315 
319  T dx = x() < 0 ? -x() : x();
320  T dy = y() < 0 ? -y() : y();
321  T dz = z() < 0 ? -z() : z();
322  if (dx < dy) {
323  return dx < dz ?
324  BasicVector3D<T>(0,z(),-y()) : BasicVector3D<T>(y(),-x(),0);
325  }else{
326  return dy < dz ?
327  BasicVector3D<T>(-z(),0,x()) : BasicVector3D<T>(y(),-x(),0);
328  }
329  }
330 
331  // ---------
332  // Rotations
333  // ---------
334 
347  };
348 
349  /*************************************************************************
350  * *
351  * Non-member functions for BasicVector3D<float> *
352  * *
353  *************************************************************************/
354 
359  std::ostream &
360  operator<<(std::ostream &, const BasicVector3D<float> &);
361 
366  std::istream &
367  operator>>(std::istream &, BasicVector3D<float> &);
368 
373  inline BasicVector3D<float>
374  operator+(const BasicVector3D<float> & v) { return v; }
375 
380  inline BasicVector3D<float>
382  return BasicVector3D<float>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
383  }
384 
389  inline BasicVector3D<float>
391  return BasicVector3D<float>(-v.x(), -v.y(), -v.z());
392  }
393 
398  inline BasicVector3D<float>
400  return BasicVector3D<float>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
401  }
402 
407  inline BasicVector3D<float>
408  operator*(const BasicVector3D<float> & v, double a) {
409  return BasicVector3D<float>(v.x()*static_cast<float>(a), v.y()*static_cast<float>(a), v.z()*static_cast<float>(a));
410  }
411 
416  inline float
418  return a.dot(b);
419  }
420 
425  inline BasicVector3D<float>
426  operator*(double a, const BasicVector3D<float> & v) {
427  return BasicVector3D<float>(static_cast<float>(a)*v.x(), static_cast<float>(a)*v.y(), static_cast<float>(a)*v.z());
428  }
429 
434  inline BasicVector3D<float>
435  operator/(const BasicVector3D<float> & v, double a) {
436  return BasicVector3D<float>(v.x()/static_cast<float>(a), v.y()/static_cast<float>(a), v.z()/static_cast<float>(a));
437  }
438 
443  inline bool
445  return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z());
446  }
447 
452  inline bool
454  return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z());
455  }
456 
457  /*************************************************************************
458  * *
459  * Non-member functions for BasicVector3D<double> *
460  * *
461  *************************************************************************/
462 
467  std::ostream &
468  operator<<(std::ostream &, const BasicVector3D<double> &);
469 
474  std::istream &
475  operator>>(std::istream &, BasicVector3D<double> &);
476 
481  inline BasicVector3D<double>
482  operator+(const BasicVector3D<double> & v) { return v; }
483 
488  inline BasicVector3D<double>
490  return BasicVector3D<double>(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
491  }
492 
497  inline BasicVector3D<double>
499  return BasicVector3D<double>(-v.x(), -v.y(), -v.z());
500  }
501 
506  inline BasicVector3D<double>
508  return BasicVector3D<double>(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
509  }
510 
515  inline BasicVector3D<double>
516  operator*(const BasicVector3D<double> & v, double a) {
517  return BasicVector3D<double>(v.x()*a, v.y()*a, v.z()*a);
518  }
519 
524  inline double
526  return a.dot(b);
527  }
528 
533  inline BasicVector3D<double>
534  operator*(double a, const BasicVector3D<double> & v) {
535  return BasicVector3D<double>(a*v.x(), a*v.y(), a*v.z());
536  }
537 
542  inline BasicVector3D<double>
543  operator/(const BasicVector3D<double> & v, double a) {
544  return BasicVector3D<double>(v.x()/a, v.y()/a, v.z()/a);
545  }
546 
551  inline bool
553  {
554  return (a.x()==b.x() && a.y()==b.y() && a.z()==b.z());
555  }
556 
561  inline bool
563  {
564  return (a.x()!=b.x() || a.y()!=b.y() || a.z()!=b.z());
565  }
566 } /* namespace HepGeom */
567 
568 #endif /* BASIC_VECTOR3D_H */