CMS 3D CMS Logo

sseBasic2DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_newBasic2DVector_h
2 #define GeometryVector_newBasic2DVector_h
3 
8 
9 #include <cmath>
10 #include <iosfwd>
11 
12 template <class T>
13 class Basic2DVector {
14 public:
15  typedef T ScalarType;
19 
25 
27  Basic2DVector(const Basic2DVector& p) : v(p.v) {}
28 
30  Basic2DVector& operator=(const Basic2DVector&) = default;
31 
32  template <typename U>
34 
42  template <class Other>
43  explicit Basic2DVector(const Other& p) : v(p.x(), p.y()) {}
44 
46  Basic2DVector(const T& x, const T& y) : v(x, y) {}
47 
48  // constructor from Vec2 or vec4
49  template <typename U>
51  template <typename U>
53 
54  MathVector const& mathVector() const { return v; }
55  MathVector& mathVector() { return v; }
56 
57  T operator[](int i) const { return v[i]; }
58  T& operator[](int i) { return v[i]; }
59 
61  T x() const { return v[0]; }
62 
64  T y() const { return v[1]; }
65 
67  T mag2() const { return ::dot(v, v); }
68 
70  T mag() const { return std::sqrt(mag2()); }
71 
73  T r() const { return mag(); }
74 
79  T barePhi() const { return std::atan2(y(), x()); }
80  Geom::Phi<T> phi() const { return Geom::Phi<T>(atan2(y(), x())); }
81 
85  Basic2DVector unit() const {
86  T my_mag = mag();
87  return my_mag == 0 ? *this : *this / my_mag;
88  }
89 
92  template <class U>
94  v = v + p.v;
95  return *this;
96  }
97 
100  template <class U>
102  v = v - p.v;
103  return *this;
104  }
105 
107  Basic2DVector operator-() const { return Basic2DVector(-v); }
108 
111  v = v * t;
112  return *this;
113  }
114 
117  t = T(1) / t;
118  v = v * t;
119  return *this;
120  }
121 
123  T dot(const Basic2DVector& lh) const { return ::dot(v, lh.v); }
124 
130  template <class U>
134  }
135 
137  T cross(const Basic2DVector& lh) const { return ::cross(v, lh.v); }
138 
144  template <class U>
148  }
149 
150 public:
152 };
153 
154 namespace geometryDetails {
155  std::ostream& print2D(std::ostream& s, double x, double y);
156 
157 }
158 
160 template <class T>
161 inline std::ostream& operator<<(std::ostream& s, const Basic2DVector<T>& v) {
162  return geometryDetails::print2D(s, v.x(), v.y());
163 }
164 
166 template <class T>
168  return a.v + b.v;
169 }
170 template <class T>
172  return a.v - b.v;
173 }
174 
175 template <class T, class U>
177  const Basic2DVector<U>& b) {
179  return RT(a) + RT(b);
180 }
181 
182 template <class T, class U>
184  const Basic2DVector<U>& b) {
186  return RT(a) - RT(b);
187 }
188 
189 // scalar product of vectors of same precision
190 template <class T>
191 inline T operator*(const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
192  return v1.dot(v2);
193 }
194 
196 template <class T, class U>
198  return v1.dot(v2);
199 }
200 
204 template <class T>
206  return v.v * t;
207 }
208 
210 template <class T>
212  return v.v * t;
213 }
214 
215 template <class T, class Scalar>
217  T t = static_cast<T>(s);
218  return v * t;
219 }
220 
222 template <class T, class Scalar>
224  T t = static_cast<T>(s);
225  return v * t;
226 }
227 
231 template <class T>
233  return v.v / t;
234 }
235 
236 template <class T, class Scalar>
238  // T t = static_cast<T>(Scalar(1)/s); return v*t;
239  T t = static_cast<T>(s);
240  return v / t;
241 }
242 
245 
246 #endif // GeometryVector_Basic2DVector_h
Basic3DVector cross(const Basic3DVector &v) const
Vector product, or "cross" product, with a vector of same type.
Basic2DVector(const T &x, const T &y)
construct from cartesian coordinates
Basic2DVector(mathSSE::Vec2< U > const &iv)
double Scalar
Definition: Definitions.h:25
Basic2DVector< float > Basic2DVectorF
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
MathVector const & mathVector() const
Basic2DVector(const Basic2DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
T y() const
Cartesian y coordinate.
Basic2DVector & operator=(const Basic2DVector &)=default
Assignment operator.
bool int lh
Definition: SIMDVec.h:27
Basic2DVector< T > operator+(const Basic2DVector< T > &a, const Basic2DVector< T > &b)
vector sum and subtraction of vectors of possibly different precision
Basic2DVector(const Basic2DVector< U > &p)
Basic2DVector(const Other &p)
Basic2DVector< T > operator/(const Basic2DVector< T > &v, T t)
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
Geom::Phi< T > phi() const
Geom::Polar2Cartesian< T > Polar
T sqrt(T t)
Definition: SSEVec.h:23
T cross(const Basic2DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
Basic2DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
PreciseFloatType< T, U >::Type cross(const Basic2DVector< U > &lh) const
Basic2DVector & operator/=(T t)
Scaling by a scalar value (division)
std::ostream & print2D(std::ostream &s, double x, double y)
Definition: print.cc:8
Basic2DVector unit() const
Basic2DVector & operator+=(const Basic2DVector< U > &p)
MathVector & mathVector()
Basic2DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
Basic2DVector< double > Basic2DVectorD
Basic2DVector< T > operator-(const Basic2DVector< T > &a, const Basic2DVector< T > &b)
T r() const
Radius, same as mag()
T & operator[](int i)
Basic2DVector & operator-=(const Basic2DVector< U > &p)
T dot(const Basic2DVector &lh) const
Scalar product, or "dot" product, with a vector of same type.
double b
Definition: hdecay.h:120
T operator*(const Basic2DVector< T > &v1, const Basic2DVector< T > &v2)
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
T barePhi() const
Basic2DVector(mathSSE::Vec4< U > const &iv)
double a
Definition: hdecay.h:121
float x
mathSSE::Vec2< T > MathVector
long double T
mathSSE::Vec2< T > VectorType
Definition: Phi.h:52
T x() const
Cartesian x coordinate.
mathSSE::Vec2< T > v
PreciseFloatType< T, U >::Type dot(const Basic2DVector< U > &lh) const
T operator[](int i) const