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 
29  template <typename U>
31 
39  template <class Other>
40  explicit Basic2DVector(const Other& p) : v(p.x(), p.y()) {}
41 
43  Basic2DVector(const T& x, const T& y) : v(x, y) {}
44 
45  // constructor from Vec2 or vec4
46  template <typename U>
48  template <typename U>
50 
51  MathVector const& mathVector() const { return v; }
52  MathVector& mathVector() { return v; }
53 
54  T operator[](int i) const { return v[i]; }
55  T& operator[](int i) { return v[i]; }
56 
58  T x() const { return v[0]; }
59 
61  T y() const { return v[1]; }
62 
64  T mag2() const { return ::dot(v, v); }
65 
67  T mag() const { return std::sqrt(mag2()); }
68 
70  T r() const { return mag(); }
71 
76  T barePhi() const { return std::atan2(y(), x()); }
77  Geom::Phi<T> phi() const { return Geom::Phi<T>(atan2(y(), x())); }
78 
82  Basic2DVector unit() const {
83  T my_mag = mag();
84  return my_mag == 0 ? *this : *this / my_mag;
85  }
86 
89  template <class U>
91  v = v + p.v;
92  return *this;
93  }
94 
97  template <class U>
99  v = v - p.v;
100  return *this;
101  }
102 
104  Basic2DVector operator-() const { return Basic2DVector(-v); }
105 
108  v = v * t;
109  return *this;
110  }
111 
114  t = T(1) / t;
115  v = v * t;
116  return *this;
117  }
118 
120  T dot(const Basic2DVector& lh) const { return ::dot(v, lh.v); }
121 
127  template <class U>
131  }
132 
134  T cross(const Basic2DVector& lh) const { return ::cross(v, lh.v); }
135 
141  template <class U>
145  }
146 
147 public:
149 };
150 
151 namespace geometryDetails {
152  std::ostream& print2D(std::ostream& s, double x, double y);
153 
154 }
155 
157 template <class T>
158 inline std::ostream& operator<<(std::ostream& s, const Basic2DVector<T>& v) {
159  return geometryDetails::print2D(s, v.x(), v.y());
160 }
161 
163 template <class T>
165  return a.v + b.v;
166 }
167 template <class T>
169  return a.v - b.v;
170 }
171 
172 template <class T, class U>
174  const Basic2DVector<U>& b) {
176  return RT(a) + RT(b);
177 }
178 
179 template <class T, class U>
181  const Basic2DVector<U>& b) {
183  return RT(a) - RT(b);
184 }
185 
186 // scalar product of vectors of same precision
187 template <class T>
188 inline T operator*(const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
189  return v1.dot(v2);
190 }
191 
193 template <class T, class U>
195  return v1.dot(v2);
196 }
197 
201 template <class T>
203  return v.v * t;
204 }
205 
207 template <class T>
209  return v.v * t;
210 }
211 
212 template <class T, class Scalar>
214  T t = static_cast<T>(s);
215  return v * t;
216 }
217 
219 template <class T, class Scalar>
221  T t = static_cast<T>(s);
222  return v * t;
223 }
224 
228 template <class T>
230  return v.v / t;
231 }
232 
233 template <class T, class Scalar>
235  // T t = static_cast<T>(Scalar(1)/s); return v*t;
236  T t = static_cast<T>(s);
237  return v / t;
238 }
239 
242 
243 #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
int32_t *__restrict__ iv
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.
bool int lh
Definition: SIMDVec.h:20
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:19
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