GeoVector.h
Go to the documentation of this file.
1 /**
2  * \file GeoVector.h
3  *
4  * \ingroup GeoAlgo
5  *
6  * \brief Class def header for a class Point and Vector
7  *
8  * @author kazuhiro
9  */
10 
11 /** \addtogroup GeoAlgo
12 
13  @{*/
14 #ifndef BASICTOOL_GEOVECTOR_H
15 #define BASICTOOL_GEOVECTOR_H
16 
18 
19 #include "TVector3.h"
20 #include "TLorentzVector.h"
21 
22 #include <stddef.h>
23 #include <functional>
24 #include <ostream>
25 #include <vector>
26 
27 namespace geoalgo {
28 
29  /**
30  \class Vector
31  This class represents an n-dimensional vector
32  */
33  class Vector : public std::vector<double> {
34  friend class Trajectory;
35  friend class HalfLine;
36  friend class LineSegment;
37  friend class Sphere;
38  friend class GeoAlgo;
39  public:
40  /// Default ctor
41  Vector() : std::vector<double>()
42  {}
43 
44  /// Ctor to instantiate with invalid value
45  Vector(size_t n) : std::vector<double>(n,kINVALID_DOUBLE)
46  {}
47 
48  /// Default ctor w/ a bare std::vector<double>
49  Vector(const std::vector<double> &obj) : std::vector<double>(obj)
50  {}
51 
52 
53  Vector(const double x, const double y); ///< ctor w/ x & y
54  Vector(const double x, const double y, const double z); ///< ctor w/ x, y & z
55  Vector(const TVector3 &pt); ///< ctor w/ TVector3
56  Vector(const TLorentzVector &pt); ///< ctor w/ TLorentzVector
57 
58  void Normalize(); ///< Normalize itself
59 
60  bool IsValid () const; ///< Check if point is valid
61  double SqLength() const; ///< Compute the squared length of the vector
62  double Length () const; ///< Compute the length of the vector
63  Vector Dir () const; ///< Return a direction unit vector
64  double Phi () const; ///< Compute the angle Phi
65  double Theta () const; ///< Compute the angle theta
66 
67  double SqDist(const Vector &obj) const; ///< Compute the squared distance to another vector
68  double Dist (const Vector& obj) const; ///< Compute the distance to another vector
69  double Dot (const Vector &obj) const; /// Compute a dot product of two vectors
70  Vector Cross (const Vector &obj) const; /// Compute a cross product of two vectors
71  double Angle (const Vector &obj) const; /// Compute an opening angle w.r.t. the given vector
72 
73  TLorentzVector ToTLorentzVector() const; ///< Convert geovector to TLorentzVector (with 4th element set equal to 0)
74 
75  /// Dimensional check for a compatibility
76  void compat(const Vector& obj) const;
77 
78  /// rotation operations
79  void RotateX(const double& theta);
80  void RotateY(const double& theta);
81  void RotateZ(const double& theta);
82 
83  protected:
84 
85  /// Compute the squared-distance to another vector w/o dimension check
86  double _SqDist_(const Vector& obj) const;
87  /// Compute the distance to another vector w/o dimension check
88  double _Dist_(const Vector& obj) const;
89  /// Compute a dot product w/o dimention check.
90  double _Dot_(const Vector& obj) const;
91  /// Compute a cross product w/o dimension check.
92  Vector _Cross_(const Vector& obj) const;
93  /// Compute the angle in degrees between 2 vectors w/o dimension check.
94  double _Angle_(const Vector& obj) const;
95 
96  public:
97  //
98  // binary/uniry operators
99  //
100  inline Vector& operator+=(const Vector& rhs) {
101  for(size_t i=0; i<size(); ++i) (*this)[i] += rhs[i];
102  return *this;
103  }
104 
105  inline Vector& operator-=(const Vector& rhs) {
106  for(size_t i=0; i<size(); ++i) (*this)[i] -= rhs[i];
107  return *this;
108  }
109 
110  inline Vector& operator*=(const double rhs) {
111  for(auto &v : *this) v *= rhs;
112  return *this;
113  }
114 
115  inline Vector& operator/=(const double rhs) {
116  for(auto &v : *this) v /= rhs;
117  return *this;
118  }
119 
120  inline Vector& operator=(const Vector& rhs) {
121  this->resize(rhs.size());
122  for(size_t i=0; i<rhs.size(); ++i) (*this)[i]=rhs[i];
123  return (*this);
124  }
125 
126  inline Vector operator+(const Vector& rhs) const
127  {
128  Vector res((*this));
129  res+=rhs;
130  return res;
131  }
132 
133  inline Vector operator-(const Vector& rhs) const
134  {
135  Vector res((*this));
136  res-=rhs;
137  return res;
138  }
139 
140  inline double operator*(const Vector& rhs) const
141  {
142  double res=0;
143  for(size_t i=0; i<size(); ++i) res += (*this)[i] * rhs[i];
144  return res;
145  }
146 
147  inline Vector operator*(const double& rhs) const
148  {
149  Vector res((*this));
150  res *= rhs;
151  return res;
152  }
153 
154  inline Vector operator/(const double& rhs) const
155  {
156  Vector res((*this));
157  res /= rhs;
158  return res;
159  }
160 
161  inline bool operator< ( const Vector& rhs ) const
162  {
163  compat(rhs);
164  for(size_t i=0; i<size(); ++i)
165  if((*this)[i] < rhs[i]) return true;
166  return false;
167  }
168 
169  inline bool operator< ( const double& rhs) const
170  { return Length() < rhs; }
171 
172  inline bool operator== ( const Vector& rhs) const
173  {
174  compat(rhs);
175  for(size_t i=0; i<size(); ++i)
176  if((*this)[i] != rhs[i]) return false;
177  return true;
178  }
179 
180  inline bool operator!= ( const Vector& rhs) const
181  { return !(*this == rhs); }
182 
183  /// Streamer
184  #ifndef __CINT__
185  friend std::ostream& operator << (std::ostream &o, ::geoalgo::Vector const& a)
186  { o << "Vector ("; for(auto const& v : a) o << v << " ";
187  o << ")";
188  return o;
189  }
190  #endif
191 
192  };
193 
194  /// Point has same feature as Vector
195  typedef Vector Vector_t;
196  typedef Vector Point_t;
197 }
198 
199 // Define a pointer comparison
200 namespace std {
201  template <>
202  class less<geoalgo::Vector*>
203  {
204  public:
205  bool operator()( const geoalgo::Vector* lhs, const geoalgo::Vector* rhs )
206  { return (*lhs) < (*rhs); }
207  };
208 }
209 
210 #endif
211 /** @} */ // end of doxygen group
bool operator<(const Vector &rhs) const
Definition: GeoVector.h:161
bool operator!=(const Vector &rhs) const
Definition: GeoVector.h:180
Algorithm to compute various geometrical relation among geometrical objects. In particular functions ...
Definition: GeoAlgo.h:43
double _Angle_(const Vector &obj) const
Compute the angle in degrees between 2 vectors w/o dimension check.
Definition: GeoVector.cxx:130
Vector & operator+=(const Vector &rhs)
Definition: GeoVector.h:100
Vector operator+(const Vector &rhs) const
Definition: GeoVector.h:126
double _Dot_(const Vector &obj) const
Compute a dot product w/o dimention check.
Definition: GeoVector.cxx:118
Vector()
Default ctor.
Definition: GeoVector.h:41
Representation of a simple 3D line segment Defines a finite 3D straight line by having the start and ...
struct vector vector
double Phi() const
Compute the angle Phi.
Definition: GeoVector.cxx:65
STL namespace.
void compat(const Vector &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:97
double _SqDist_(const Vector &obj) const
Compute the squared-distance to another vector w/o dimension check.
Definition: GeoVector.cxx:108
double Dist(const Vector &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:48
Vector & operator*=(const double rhs)
Definition: GeoVector.h:110
Vector operator*(const double &rhs) const
Definition: GeoVector.h:147
void resize(Vector< T > &vec1, Index n1, const V &val)
void Normalize()
Normalize itself.
Definition: GeoVector.cxx:89
double SqLength() const
Compute the squared length of the vector.
Definition: GeoVector.cxx:34
Vector(const std::vector< double > &obj)
Default ctor w/ a bare std::vector<double>
Definition: GeoVector.h:49
Vector _Cross_(const Vector &obj) const
Compute a cross product w/o dimension check.
Definition: GeoVector.cxx:121
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
Vector operator/(const double &rhs) const
Definition: GeoVector.h:154
Vector(size_t n)
Ctor to instantiate with invalid value.
Definition: GeoVector.h:45
double Length() const
Compute the length of the vector.
Definition: GeoVector.cxx:40
double Dot(const Vector &obj) const
Definition: GeoVector.cxx:51
bool IsValid() const
Check if point is valid.
Definition: GeoVector.cxx:22
std::void_t< T > n
Vector operator-(const Vector &rhs) const
Definition: GeoVector.h:133
const double a
Vector Cross(const Vector &obj) const
Compute a dot product of two vectors.
Definition: GeoVector.cxx:56
void RotateY(const double &theta)
Definition: GeoVector.cxx:150
Vector Point_t
Definition: GeoVector.h:196
double SqDist(const Vector &obj) const
Compute the squared distance to another vector.
Definition: GeoVector.cxx:43
TLorentzVector ToTLorentzVector() const
Compute an opening angle w.r.t. the given vector.
Definition: GeoVector.cxx:83
Vector & operator/=(const double rhs)
Definition: GeoVector.h:115
double operator*(const Vector &rhs) const
Definition: GeoVector.h:140
Representation of a 3D semi-infinite line. Defines a semi-infinite 3D line by having a start point (P...
Definition: GeoHalfLine.h:30
void RotateX(const double &theta)
rotation operations
Definition: GeoVector.cxx:134
Vector Dir() const
Return a direction unit vector.
Definition: GeoVector.cxx:91
Vector & operator=(const Vector &rhs)
Definition: GeoVector.h:120
Vector Vector_t
Point has same feature as Vector.
Definition: GeoVector.h:195
list x
Definition: train.py:276
double Theta() const
Compute the angle theta.
Definition: GeoVector.cxx:69
double _Dist_(const Vector &obj) const
Compute the distance to another vector w/o dimension check.
Definition: GeoVector.cxx:115
static const double kINVALID_DOUBLE
void RotateZ(const double &theta)
Definition: GeoVector.cxx:166
bool operator==(const Vector &rhs) const
Definition: GeoVector.h:172
bool operator()(const geoalgo::Vector *lhs, const geoalgo::Vector *rhs)
Definition: GeoVector.h:205
double Angle(const Vector &obj) const
Compute a cross product of two vectors.
Definition: GeoVector.cxx:76
Vector & operator-=(const Vector &rhs)
Definition: GeoVector.h:105
friend std::ostream & operator<<(std::ostream &o,::geoalgo::Vector const &a)
Streamer.
Definition: GeoVector.h:185