Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:34

0001 #ifndef GeometryVector_Vector2DBase_h
0002 #define GeometryVector_Vector2DBase_h
0003 
0004 #include "DataFormats/GeometryVector/interface/VectorTag.h"
0005 #include "DataFormats/GeometryVector/interface/PV2DBase.h"
0006 
0007 template <class T, class FrameTag>
0008 class Vector2DBase : public PV2DBase<T, VectorTag, FrameTag> {
0009 public:
0010   typedef PV2DBase<T, VectorTag, FrameTag> BaseClass;
0011   typedef Basic2DVector<T> BasicVectorType;
0012   typedef typename BaseClass::Polar Polar;
0013 
0014   /** default constructor uses default constructor of T to initialize the 
0015    *  components. For built-in floating-point types this means initialization 
0016    * to zero
0017    */
0018   Vector2DBase() {}
0019 
0020   /** Construct from another vector in the same reference frame, possiblly
0021    *  with different precision
0022    */
0023   template <class U>
0024   Vector2DBase(const Vector2DBase<U, FrameTag>& p) : BaseClass(p.basicVector()) {}
0025 
0026   /// construct from cartesian coordinates
0027   Vector2DBase(const T& x, const T& y) : BaseClass(x, y) {}
0028 
0029   /// construct from polar coordinates
0030   explicit Vector2DBase(const Polar& set) : BaseClass(set) {}
0031 
0032   /** Explicit constructor from BasicVectorType, bypasses consistency checks
0033    *  for point/vector and for coordinate frame. To be used as carefully as
0034    *  e.g. const_cast.
0035    */
0036   template <class U>
0037   explicit Vector2DBase(const Basic2DVector<U>& v) : BaseClass(v) {}
0038 
0039   /** Unit vector parallel to this.
0040    *  If mag() is zero, a zero vector is returned.
0041    */
0042   Vector2DBase unit() const { return Vector2DBase(this->basicVector().unit()); }
0043 
0044   /** Increment by another Vector of possibly different precision,
0045    *  defined in the same reference frame 
0046    */
0047   template <class U>
0048   Vector2DBase& operator+=(const Vector2DBase<U, FrameTag>& v) {
0049     this->basicVector() += v.basicVector();
0050     return *this;
0051   }
0052 
0053   /** Decrement by another Vector of possibly different precision,
0054    *  defined in the same reference frame 
0055    */
0056   template <class U>
0057   Vector2DBase& operator-=(const Vector2DBase<U, FrameTag>& v) {
0058     this->basicVector() -= v.basicVector();
0059     return *this;
0060   }
0061 
0062   /// Unary minus, returns a vector with components (-x(),-y())
0063   Vector2DBase operator-() const { return Vector2DBase(-this->basicVector()); }
0064 
0065   /// Scaling by a scalar value (multiplication)
0066   Vector2DBase& operator*=(const T& t) {
0067     this->basicVector() *= t;
0068     return *this;
0069   }
0070 
0071   /// Scaling by a scalar value (division)
0072   Vector2DBase& operator/=(const T& t) {
0073     this->basicVector() /= t;
0074     return *this;
0075   }
0076 
0077   /** Scalar (or dot) product with a vector of possibly different precision,
0078    *  defined in the same reference frame.
0079    *  The product is computed without loss of precision. The type
0080    *  of the returned scalar is the more precise of the scalar types 
0081    *  of the two vectors.
0082    */
0083   template <class U>
0084   typename PreciseFloatType<T, U>::Type dot(const Vector2DBase<U, FrameTag>& v) const {
0085     return this->basicVector().dot(v.basicVector());
0086   }
0087 };
0088 
0089 /// vector sum and subtraction of vectors of possibly different precision
0090 template <class T, class U, class FrameTag>
0091 inline Vector2DBase<typename PreciseFloatType<T, U>::Type, FrameTag> operator+(const Vector2DBase<T, FrameTag>& v1,
0092                                                                                const Vector2DBase<U, FrameTag>& v2) {
0093   typedef Vector2DBase<typename PreciseFloatType<T, U>::Type, FrameTag> RT;
0094   return RT(v1.basicVector() + v2.basicVector());
0095 }
0096 
0097 template <class T, class U, class FrameTag>
0098 inline Vector2DBase<typename PreciseFloatType<T, U>::Type, FrameTag> operator-(const Vector2DBase<T, FrameTag>& v1,
0099                                                                                const Vector2DBase<U, FrameTag>& v2) {
0100   typedef Vector2DBase<typename PreciseFloatType<T, U>::Type, FrameTag> RT;
0101   return RT(v1.basicVector() - v2.basicVector());
0102 }
0103 
0104 /// scalar product of vectors of possibly different precision
0105 template <class T, class U, class FrameTag>
0106 inline typename PreciseFloatType<T, U>::Type operator*(const Vector2DBase<T, FrameTag>& v1,
0107                                                        const Vector2DBase<U, FrameTag>& v2) {
0108   return v1.basicVector() * v2.basicVector();
0109 }
0110 
0111 /** Multiplication by scalar, does not change the precision of the vector.
0112  *  The return type is the same as the type of the vector argument.
0113  */
0114 template <class T, class FrameTag, class Scalar>
0115 inline Vector2DBase<T, FrameTag> operator*(const Vector2DBase<T, FrameTag>& v, const Scalar& s) {
0116   return Vector2DBase<T, FrameTag>(v.basicVector() * s);
0117 }
0118 
0119 /// Same as operator*( Vector, Scalar)
0120 template <class T, class FrameTag, class Scalar>
0121 inline Vector2DBase<T, FrameTag> operator*(const Scalar& s, const Vector2DBase<T, FrameTag>& v) {
0122   return Vector2DBase<T, FrameTag>(v.basicVector() * s);
0123 }
0124 
0125 /** Division by scalar, does not change the precision of the vector.
0126  *  The return type is the same as the type of the vector argument.
0127  */
0128 template <class T, class FrameTag, class Scalar>
0129 inline Vector2DBase<T, FrameTag> operator/(const Vector2DBase<T, FrameTag>& v, const Scalar& s) {
0130   return Vector2DBase<T, FrameTag>(v.basicVector() / s);
0131 }
0132 
0133 #endif  // GeometryVector_Vector2DBase_h