Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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