Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TGeoVector3.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/geom:$Id$
0002 // Author: Andrei Gheata   20/12/19
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TGeoVector3
0013 #define ROOT_TGeoVector3
0014 
0015 #include <Riostream.h>
0016 #include <TMath.h>
0017 
0018 namespace ROOT {
0019 namespace Geom {
0020 
0021 struct Vertex_t {
0022    double fVec[3] = {0.};
0023 
0024    Vertex_t(const double a, const double b, const double c)
0025    {
0026       fVec[0] = a;
0027       fVec[1] = b;
0028       fVec[2] = c;
0029    }
0030 
0031    Vertex_t(const double a = 0.)
0032    {
0033       fVec[0] = a;
0034       fVec[1] = a;
0035       fVec[2] = a;
0036    }
0037 
0038    double &operator[](const int index) { return fVec[index]; }
0039    double const &operator[](const int index) const { return fVec[index]; }
0040 
0041    // Inplace binary operators
0042 
0043 #define Vertex_t_INPLACE_BINARY_OP(OPERATOR)                 \
0044    inline Vertex_t &operator OPERATOR(const Vertex_t &other) \
0045    {                                                         \
0046       fVec[0] OPERATOR other.fVec[0];                        \
0047       fVec[1] OPERATOR other.fVec[1];                        \
0048       fVec[2] OPERATOR other.fVec[2];                        \
0049       return *this;                                          \
0050    }                                                         \
0051    inline Vertex_t &operator OPERATOR(const double &scalar)  \
0052    {                                                         \
0053       fVec[0] OPERATOR scalar;                               \
0054       fVec[1] OPERATOR scalar;                               \
0055       fVec[2] OPERATOR scalar;                               \
0056       return *this;                                          \
0057    }
0058    Vertex_t_INPLACE_BINARY_OP(+=) Vertex_t_INPLACE_BINARY_OP(-=) Vertex_t_INPLACE_BINARY_OP(*=)
0059       Vertex_t_INPLACE_BINARY_OP(/=)
0060 #undef Vertex_t_INPLACE_BINARY_OP
0061 
0062          double &x()
0063    {
0064       return fVec[0];
0065    }
0066    double const &x() const { return fVec[0]; }
0067 
0068    double &y() { return fVec[1]; }
0069    double const &y() const { return fVec[1]; }
0070 
0071    double &z() { return fVec[2]; }
0072    double const &z() const { return fVec[2]; }
0073 
0074    inline void CopyTo(double *dest) const
0075    {
0076       dest[0] = fVec[0];
0077       dest[1] = fVec[1];
0078       dest[2] = fVec[2];
0079    }
0080 
0081    void Set(double const &a, double const &b, double const &c)
0082    {
0083       fVec[0] = a;
0084       fVec[1] = b;
0085       fVec[2] = c;
0086    }
0087 
0088    void Set(const double a) { Set(a, a, a); }
0089 
0090    /// \return the length squared perpendicular to z direction
0091    double Perp2() const { return fVec[0] * fVec[0] + fVec[1] * fVec[1]; }
0092 
0093    /// \return the length perpendicular to z direction
0094    double Perp() const { return TMath::Sqrt(Perp2()); }
0095 
0096    /// The dot product of two vector objects
0097    static double Dot(Vertex_t const &left, Vertex_t const &right)
0098    {
0099       return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
0100    }
0101 
0102    /// The dot product of two vector
0103    double Dot(Vertex_t const &right) const { return Dot(*this, right); }
0104 
0105    /// \return Squared magnitude of the vector.
0106    double Mag2() const { return Dot(*this, *this); }
0107 
0108    /// \return Magnitude of the vector.
0109    double Mag() const { return TMath::Sqrt(Mag2()); }
0110 
0111    double Length() const { return Mag(); }
0112 
0113    double Length2() const { return Mag2(); }
0114 
0115    /// Normalizes the vector by dividing each entry by the length.
0116    void Normalize() { *this *= (1. / Length()); }
0117 
0118    // Vertex_t Normalized() const { return Vertex_t(*this) * (1. / Length()); }
0119 
0120    // checks if vector is normalized
0121    bool IsNormalized() const
0122    {
0123       double norm = Mag2();
0124       constexpr double tolerance = 1.e-10;
0125       return 1. - tolerance < norm && norm < 1 + tolerance;
0126    }
0127 
0128    /// \return Azimuthal angle between -pi and pi.
0129    double Phi() const { return TMath::ATan2(fVec[1], fVec[0]); }
0130 
0131    /// \return Polar angle between 0 and pi.
0132    double Theta() const { return TMath::ACos(fVec[2] / Mag()); }
0133 
0134    /// The cross (vector) product of two Vector3D<T> objects
0135    static Vertex_t Cross(Vertex_t const &left, Vertex_t const &right)
0136    {
0137       return Vertex_t(left[1] * right[2] - left[2] * right[1], left[2] * right[0] - left[0] * right[2],
0138                       left[0] * right[1] - left[1] * right[0]);
0139    }
0140 
0141    Vertex_t Abs() const { return Vertex_t(TMath::Abs(fVec[0]), TMath::Abs(fVec[1]), TMath::Abs(fVec[2])); }
0142 
0143    double Min() const { return TMath::Min(TMath::Min(fVec[0], fVec[1]), fVec[2]); }
0144 
0145    double Max() const { return TMath::Max(TMath::Max(fVec[0], fVec[1]), fVec[2]); }
0146 
0147    Vertex_t Unit() const
0148    {
0149       constexpr double kMinimum = std::numeric_limits<double>::min();
0150       const double mag2 = Mag2();
0151       Vertex_t output(*this);
0152       output /= TMath::Sqrt(mag2 + kMinimum);
0153       return output;
0154    }
0155 };
0156 
0157 inline bool operator==(Vertex_t const &lhs, Vertex_t const &rhs)
0158 {
0159    constexpr double kTolerance = 1.e-8;
0160    return TMath::Abs(lhs[0] - rhs[0]) < kTolerance && TMath::Abs(lhs[1] - rhs[1]) < kTolerance &&
0161           TMath::Abs(lhs[2] - rhs[2]) < kTolerance;
0162 }
0163 
0164 inline bool operator!=(Vertex_t const &lhs, Vertex_t const &rhs)
0165 {
0166    return !(lhs == rhs);
0167 }
0168 
0169 #define Vertex_t_BINARY_OP(OPERATOR, INPLACE)                                  \
0170    inline Vertex_t operator OPERATOR(const Vertex_t &lhs, const Vertex_t &rhs) \
0171    {                                                                           \
0172       Vertex_t result(lhs);                                                    \
0173       result INPLACE rhs;                                                      \
0174       return result;                                                           \
0175    }                                                                           \
0176    inline Vertex_t operator OPERATOR(Vertex_t const &lhs, const double rhs)    \
0177    {                                                                           \
0178       Vertex_t result(lhs);                                                    \
0179       result INPLACE rhs;                                                      \
0180       return result;                                                           \
0181    }                                                                           \
0182    inline Vertex_t operator OPERATOR(const double lhs, Vertex_t const &rhs)    \
0183    {                                                                           \
0184       Vertex_t result(lhs);                                                    \
0185       result INPLACE rhs;                                                      \
0186       return result;                                                           \
0187    }
0188 Vertex_t_BINARY_OP(+, +=) Vertex_t_BINARY_OP(-, -=) Vertex_t_BINARY_OP(*, *=) Vertex_t_BINARY_OP(/, /=)
0189 #undef Vertex_t_BINARY_OP
0190 
0191 } // namespace Geom
0192 } // namespace ROOT
0193 
0194 std::ostream &operator<<(std::ostream &os, ROOT::Geom::Vertex_t const &vec);
0195 
0196 #endif