CMS 3D CMS Logo

extBasic2DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_newBasic2DVector_h
2 #define GeometryVector_newBasic2DVector_h
3 
8 
9 
10 #include <cmath>
11 #include <iosfwd>
12 
13 
14 template < class T>
16 public:
17 
18  typedef T ScalarType;
22 
27  Basic2DVector() : v{0,0} {}
28 
30  Basic2DVector( const Basic2DVector & p) : v(p.v) {}
31 
32  template<typename U>
33  Basic2DVector( const Basic2DVector<U> & p) : v{T(p.v[0]),T(p.v[1])} {}
34 
35 
43  template <class Other>
44  explicit Basic2DVector( const Other& p) : v{T(p.x()),T(p.y())} {}
45 
47  Basic2DVector( const T& x, const T& y) : v{x,y} {}
48 
49  // constructor from Vec2 or vec4
50  Basic2DVector(MathVector const& iv) : v(iv){}
51  template<typename U>
52  Basic2DVector(Vec2<U> const& iv) : v{iv[0],iv[1]}{}
53  template<typename U>
54  Basic2DVector(Vec4<U> const& iv) : v{iv[0],iv[1]}{}
55 
56  MathVector const & mathVector() const { return v;}
57  MathVector & mathVector() { return v;}
58 
59  T operator[](int i) const { return v[i];}
60  T & operator[](int i) { return v[i];}
61 
62 
64  T x() const { return v[0];}
65 
67  T y() const { return v[1];}
68 
70  T mag2() const { return ::dot(v,v);}
71 
73  T mag() const { return std::sqrt( mag2());}
74 
76  T r() const { return mag();}
77 
82  T barePhi() const {return std::atan2(y(),x());}
83  Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(y(),x()));}
84 
88  Basic2DVector unit() const {
89  T my_mag = mag();
90  return my_mag == 0 ? *this : *this / my_mag;
91  }
92 
95  template <class U>
97  v = v + p.v;
98  return *this;
99  }
100 
103  template <class U>
105  v = v - p.v;
106  return *this;
107  }
108 
110  Basic2DVector operator-() const { return Basic2DVector(-v);}
111 
114  v = v*t;
115  return *this;
116  }
117 
120  t = T(1)/t;
121  v = v*t;
122  return *this;
123  }
124 
126  T dot( const Basic2DVector& lh) const { return ::dot(v,lh.v);}
127 
133  template <class U>
137  }
138 
140  T cross( const Basic2DVector& lh) const { return ::cross2(v,lh.v);}
141 
147  template <class U>
151  }
152 
153 
154 public:
155 
157 
158 };
159 
160 
161 namespace geometryDetails {
162  std::ostream & print2D(std::ostream& s, double x, double y);
163 
164 }
165 
167 template <class T>
168 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
169  return geometryDetails::print2D(s, v.x(),v.y());
170 }
171 
172 
174 template <class T>
175 inline Basic2DVector<T>
177  return a.v+b.v;
178 }
179 template <class T>
180 inline Basic2DVector<T>
182  return a.v-b.v;
183 }
184 
185 template <class T, class U>
189  return RT(a) + RT(b);
190 }
191 
192 template <class T, class U>
196  return RT(a)-RT(b);
197 }
198 
199 
200 
201 
202 // scalar product of vectors of same precision
203 template <class T>
204 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
205  return v1.dot(v2);
206 }
207 
209 template <class T, class U>
211  const Basic2DVector<U>& v2) {
212  return v1.dot(v2);
213 }
214 
215 
219 template <class T>
221  return v.v*t;
222 }
223 
225 template <class T>
227  return v.v*t;
228 }
229 
230 
231 
232 template <class T, class Scalar>
234  T t = static_cast<T>(s);
235  return v*t;
236 }
237 
239 template <class T, class Scalar>
241  T t = static_cast<T>(s);
242  return v*t;
243 }
244 
248 template <class T>
250  return v.v/t;
251 }
252 
253 template <class T, class Scalar>
255  // T t = static_cast<T>(Scalar(1)/s); return v*t;
256  T t = static_cast<T>(s);
257  return v/t;
258 }
259 
262 
263 
264 #endif // GeometryVector_Basic2DVector_h
T cross(const Basic2DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
Basic2DVector(const T &x, const T &y)
construct from cartesian coordinates
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:118
double Scalar
Definition: Definitions.h:27
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:74
Basic2DVector(Vec2< U > const &iv)
Basic2DVector(const Basic2DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
T dot(const Basic2DVector &lh) const
Scalar product, or "dot" product, with a vector of same type.
Vec2< T > MathVector
Basic2DVector(Vec4< U > const &iv)
MathVector const & mathVector() const
Basic2DVector< T > operator+(const Basic2DVector< T > &a, const Basic2DVector< T > &b)
vector sum and subtraction of vectors of possibly different precision
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Basic2DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
PreciseFloatType< T, U >::Type cross(const Basic2DVector< U > &lh) const
T r() const
Radius, same as mag()
T barePhi() const
bool int lh
Definition: SIMDVec.h:21
Basic2DVector(const Basic2DVector< U > &p)
Basic2DVector(const Other &p)
Basic2DVector< double > Basic2DVectorD
Basic2DVector unit() const
PreciseFloatType< T, U >::Type dot(const Basic2DVector< U > &lh) const
Basic2DVector< float > Basic2DVectorF
Geom::Polar2Cartesian< T > Polar
T sqrt(T t)
Definition: SSEVec.h:18
T operator*(const Basic2DVector< T > &v1, const Basic2DVector< T > &v2)
Basic2DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
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(MathVector const &iv)
Basic2DVector & operator+=(const Basic2DVector< U > &p)
T y() const
Cartesian y coordinate.
Basic2DVector< T > operator/(const Basic2DVector< T > &v, T t)
MathVector & mathVector()
T & operator[](int i)
Basic2DVector & operator-=(const Basic2DVector< U > &p)
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:75
double b
Definition: hdecay.h:120
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
T operator[](int i) const
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
double a
Definition: hdecay.h:121
Vec2< T > VectorType
Geom::Phi< T > phi() const
long double T
Definition: Phi.h:20
T x() const
Cartesian x coordinate.