CMS 3D CMS Logo

sseBasic3DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_newBasic3DVector_h
2 #define GeometryVector_newBasic3DVector_h
3 
10 #include <iosfwd>
11 #include <cmath>
12 
13 namespace detailsBasic3DVector {
14  inline float __attribute__((always_inline)) __attribute__((pure)) eta(float x, float y, float z) {
15  float t(z / std::sqrt(x * x + y * y));
16  return ::asinhf(t);
17  }
18  inline double __attribute__((always_inline)) __attribute__((pure)) eta(double x, double y, double z) {
19  double t(z / std::sqrt(x * x + y * y));
20  return ::asinh(t);
21  }
22  inline long double __attribute__((always_inline)) __attribute__((pure))
23  eta(long double x, long double y, long double z) {
24  long double t(z / std::sqrt(x * x + y * y));
25  return ::asinhl(t);
26  }
27 } // namespace detailsBasic3DVector
28 
29 template <typename T>
30 class Basic3DVector {
31 public:
32  typedef T ScalarType;
37  typedef Spherical Polar; // synonym
38 
44 
46  Basic3DVector(const Basic3DVector& p) : v(p.v) {}
47 
49  Basic3DVector& operator=(const Basic3DVector&) = default;
50 
52  template <class U>
54 
56  Basic3DVector(const Basic2DVector<T>& p) : v(p.x(), p.y(), 0) {}
57 
66  template <class OtherPoint>
67  explicit Basic3DVector(const OtherPoint& p) : v(p.x(), p.y(), p.z()) {}
68 
69  // constructor from Vec4
70  template <class U>
72 
74  Basic3DVector(const T& x, const T& y, const T& z, const T& w = 0) : v(x, y, z, w) {}
75 
80  template <typename U>
81  Basic3DVector(const Geom::Theta<U>& theta, const Geom::Phi<U>& phi, const T& r) {
82  Polar p(theta.value(), phi.value(), r);
83  v.o.theX = p.x();
84  v.o.theY = p.y();
85  v.o.theZ = p.z();
86  }
87 
88  MathVector const& mathVector() const { return v; }
89  MathVector& mathVector() { return v; }
90 
91  T operator[](int i) const { return v[i]; }
92  T& operator[](int i) { return v[i]; }
93 
95  T x() const { return v.o.theX; }
96 
98  T y() const { return v.o.theY; }
99 
101  T z() const { return v.o.theZ; }
102 
103  T w() const { return v.o.theW; }
104 
105  Basic2DVector<T> xy() const { return v.xy(); }
106 
107  // equality
108  bool operator==(const Basic3DVector& rh) const { return v == rh.v; }
109 
111  T mag2() const { return ::dot(v, v); }
112 
114  T mag() const { return std::sqrt(mag2()); }
115 
117  T perp2() const { return ::dotxy(v, v); }
118 
120  T perp() const { return std::sqrt(perp2()); }
121 
123  T transverse() const { return perp(); }
124 
129  T barePhi() const { return std::atan2(y(), x()); }
130  Geom::Phi<T> phi() const { return Geom::Phi<T>(barePhi()); }
131 
136  T bareTheta() const { return std::atan2(perp(), z()); }
137  Geom::Theta<T> theta() const { return Geom::Theta<T>(std::atan2(perp(), z())); }
138 
143  // T eta() const { return -log( tan( theta()/2.));}
144  T eta() const { return detailsBasic3DVector::eta(x(), y(), z()); } // correct
145 
149  Basic3DVector unit() const {
150  T my_mag = mag2();
151  return (0 != my_mag) ? (*this) * (T(1) / std::sqrt(my_mag)) : *this;
152  }
153 
156  template <class U>
158  v = v + p.v;
159  return *this;
160  }
164  template <class U>
166  v = v - p.v;
167  return *this;
168  }
169 
171  Basic3DVector operator-() const { return Basic3DVector(-v); }
172 
175  v = t * v;
176  return *this;
177  }
181  //t = T(1)/t;
182  v = v / t;
183  return *this;
184  }
185 
187  T dot(const Basic3DVector& rh) const { return ::dot(v, rh.v); }
188 
194  template <class U>
198  }
202 
208  template <class U>
212  }
214 public:
216 } __attribute__((aligned(16)));
217 
218 namespace geometryDetails {
219  std::ostream& print3D(std::ostream& s, double x, double y, double z);
220 }
221 
223 template <class T>
224 inline std::ostream& operator<<(std::ostream& s, const Basic3DVector<T>& v) {
225  return geometryDetails::print3D(s, v.x(), v.y(), v.z());
226 }
227 
229 template <class T>
231  return a.v + b.v;
232 }
233 template <class T>
235  return a.v - b.v;
236 }
237 
238 template <class T, class U>
240  const Basic3DVector<U>& b) {
242  return RT(a).v + RT(b).v;
243 }
244 
245 template <class T, class U>
247  const Basic3DVector<U>& b) {
249  return RT(a).v - RT(b).v;
250 }
251 
253 template <class T>
254 inline T operator*(const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
255  return v1.dot(v2);
256 }
257 
259 template <class T, class U>
261  return v1.dot(v2);
262 }
263 
267 template <class T>
269  return v.v * t;
270 }
271 
273 template <class T>
275  return v.v * t;
276 }
277 
278 template <class T, typename S>
280  return static_cast<T>(t) * v;
281 }
283 template <class T, typename S>
285  return static_cast<T>(t) * v;
286 }
287 
291 template <class T>
293  return v.v / t;
294 }
295 
296 template <class T, typename S>
298  // T t = S(1)/s; return v*t;
299  T t = s;
300  return v / t;
301 }
302 
306 // add long double specialization
307 #include "Basic3DVectorLD.h"
308 
309 #endif // GeometryVector_Basic3DVector_h
T operator*(const Basic3DVector< T > &v1, const Basic3DVector< T > &v2)
scalar product of vectors of same precision
T x() const
Cartesian x coordinate.
T & operator[](int i)
T perp2() const
Squared magnitude of transverse component.
Basic3DVector(const T &x, const T &y, const T &z, const T &w=0)
construct from cartesian coordinates
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Basic3DVector(const Basic3DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
bool operator==(const Basic3DVector &rh) const
T eta() const
Vec4< align::Scalar > MathVector
mathSSE::Vec4< T > MathVector
bool int lh
Definition: SIMDVec.h:27
T y() const
Cartesian y coordinate.
Basic3DVector< T > operator+(const Basic3DVector< T > &a, const Basic3DVector< T > &b)
vector sum and subtraction of vectors of possibly different precision
T operator[](int i) const
float float float z
Basic3DVector(mathSSE::Vec4< U > const &iv)
Geom::Theta< T > theta() const
Basic3DVector< T > operator/(const Basic3DVector< T > &v, T t)
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
Basic3DVector & operator/=(T t)
Scaling by a scalar value (division)
Basic3DVector(const Geom::Theta< U > &theta, const Geom::Phi< U > &phi, const T &r)
PreciseFloatType< T, U >::Type dot(const Basic3DVector< U > &lh) const
Basic3DVector< typename PreciseFloatType< T, U >::Type > cross(const Basic3DVector< U > &lh) const
T bareTheta() const
T sqrt(T t)
Definition: SSEVec.h:23
Basic3DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
Basic3DVector cross(const Basic3DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
Basic3DVector & operator+=(const Basic3DVector< U > &p)
mathSSE::Vec4< T > v
T transverse() const
Another name for perp()
Basic3DVector< double > Basic3DVectorD
Basic3DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
Basic2DVector< T > xy() const
MathVector const & mathVector() const
Basic3DVector & operator-=(const Basic3DVector< U > &p)
T perp() const
Magnitude of transverse component.
Basic3DVector(const Basic3DVector< U > &p)
Copy constructor and implicit conversion from Basic3DVector of different precision.
Basic3DVector(const Basic2DVector< T > &p)
constructor from 2D vector (X and Y from 2D vector, z set to zero)
T z() const
Cartesian z coordinate.
Geom::Spherical2Cartesian< T > Spherical
class Basic3DVector __attribute__((aligned(16)))
Basic3DVector< float > Basic3DVectorF
double b
Definition: hdecay.h:120
MathVector & mathVector()
Basic3DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
Geom::Cylindrical2Cartesian< T > Cylindrical
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
float __attribute__((always_inline)) __attribute__((pure)) eta(float x
double a
Definition: hdecay.h:121
Basic3DVector unit() const
Geom::Phi< T > phi() const
mathSSE::Vec4< T > v
Basic3DVector cross(const Basic3DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
std::ostream & print3D(std::ostream &s, double x, double y, double z)
Definition: print.cc:5
long double T
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
Basic3DVector(const OtherPoint &p)
Definition: Phi.h:52
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
mathSSE::Vec4< T > VectorType