LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GeoVector.h
Go to the documentation of this file.
1 
14 #ifndef BASICTOOL_GEOVECTOR_H
15 #define BASICTOOL_GEOVECTOR_H
16 
18 
19 #include "TLorentzVector.h"
20 #include "TVector3.h"
21 
22 #include <functional>
23 #include <ostream>
24 #include <stddef.h>
25 #include <vector>
26 
27 namespace geoalgo {
28 
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 
40  public:
42  Vector() : std::vector<double>() {}
43 
45  Vector(size_t n) : std::vector<double>(n, kINVALID_DOUBLE) {}
46 
48  Vector(const std::vector<double>& obj) : std::vector<double>(obj) {}
49 
50  Vector(const double x, const double y);
51  Vector(const double x, const double y, const double z);
52  Vector(const TVector3& pt);
53  Vector(const TLorentzVector& pt);
54 
55  void Normalize();
56 
57  bool IsValid() const;
58  double SqLength() const;
59  double Length() const;
60  Vector Dir() const;
61  double Phi() const;
62  double Theta() const;
63 
64  double SqDist(const Vector& obj) const;
65  double Dist(const Vector& obj) const;
66  double Dot(const Vector& obj) const;
67  Vector Cross(const Vector& obj) const;
68  double Angle(const Vector& obj) const;
69 
70  TLorentzVector ToTLorentzVector()
71  const;
72 
74  void compat(const Vector& obj) const;
75 
77  void RotateX(const double& theta);
78  void RotateY(const double& theta);
79  void RotateZ(const double& theta);
80 
81  protected:
83  double _SqDist_(const Vector& obj) const;
85  double _Dist_(const Vector& obj) const;
87  double _Dot_(const Vector& obj) const;
89  Vector _Cross_(const Vector& obj) const;
91  double _Angle_(const Vector& obj) const;
92 
93  public:
94  //
95  // binary/uniry operators
96  //
97  inline Vector& operator+=(const Vector& rhs)
98  {
99  for (size_t i = 0; i < size(); ++i)
100  (*this)[i] += rhs[i];
101  return *this;
102  }
103 
104  inline Vector& operator-=(const Vector& rhs)
105  {
106  for (size_t i = 0; i < size(); ++i)
107  (*this)[i] -= rhs[i];
108  return *this;
109  }
110 
111  inline Vector& operator*=(const double rhs)
112  {
113  for (auto& v : *this)
114  v *= rhs;
115  return *this;
116  }
117 
118  inline Vector& operator/=(const double rhs)
119  {
120  for (auto& v : *this)
121  v /= rhs;
122  return *this;
123  }
124 
125  inline Vector& operator=(const Vector& rhs)
126  {
127  this->resize(rhs.size());
128  for (size_t i = 0; i < rhs.size(); ++i)
129  (*this)[i] = rhs[i];
130  return (*this);
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 Vector operator-(const Vector& rhs) const
141  {
142  Vector res((*this));
143  res -= rhs;
144  return res;
145  }
146 
147  inline double operator*(const Vector& rhs) const
148  {
149  double res = 0;
150  for (size_t i = 0; i < size(); ++i)
151  res += (*this)[i] * rhs[i];
152  return res;
153  }
154 
155  inline Vector operator*(const double& rhs) const
156  {
157  Vector res((*this));
158  res *= rhs;
159  return res;
160  }
161 
162  inline Vector operator/(const double& rhs) const
163  {
164  Vector res((*this));
165  res /= rhs;
166  return res;
167  }
168 
169  inline bool operator<(const Vector& rhs) const
170  {
171  compat(rhs);
172  for (size_t i = 0; i < size(); ++i)
173  if ((*this)[i] < rhs[i]) return true;
174  return false;
175  }
176 
177  inline bool operator<(const double& rhs) const { return Length() < rhs; }
178 
179  inline bool operator==(const Vector& rhs) const
180  {
181  compat(rhs);
182  for (size_t i = 0; i < size(); ++i)
183  if ((*this)[i] != rhs[i]) return false;
184  return true;
185  }
186 
187  inline bool operator!=(const Vector& rhs) const { return !(*this == rhs); }
188 
190 #ifndef __CINT__
191  friend std::ostream& operator<<(std::ostream& o, ::geoalgo::Vector const& a)
192  {
193  o << "Vector (";
194  for (auto const& v : a)
195  o << v << " ";
196  o << ")";
197  return o;
198  }
199 #endif
200  };
201 
203  typedef Vector Vector_t;
204  typedef Vector Point_t;
205 }
206 
207 // Define a pointer comparison
208 namespace std {
209  template <>
210  class less<geoalgo::Vector*> {
211  public:
212  bool operator()(const geoalgo::Vector* lhs, const geoalgo::Vector* rhs)
213  {
214  return (*lhs) < (*rhs);
215  }
216  };
217 }
218 
219 #endif
220  // end of doxygen group
Float_t x
Definition: compare.C:6
bool operator<(const Vector &rhs) const
Definition: GeoVector.h:169
bool operator!=(const Vector &rhs) const
Definition: GeoVector.h:187
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:165
Vector & operator+=(const Vector &rhs)
Definition: GeoVector.h:97
Vector operator+(const Vector &rhs) const
Definition: GeoVector.h:133
Float_t y
Definition: compare.C:6
double _Dot_(const Vector &obj) const
Compute a dot product w/o dimention check.
Definition: GeoVector.cxx:151
Vector()
Default ctor.
Definition: GeoVector.h:42
Double_t z
Definition: plot.C:276
Representation of a simple 3D line segment Defines a finite 3D straight line by having the start and ...
double Phi() const
Compute the angle Phi.
Definition: GeoVector.cxx:89
STL namespace.
void compat(const Vector &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:128
double _SqDist_(const Vector &obj) const
Compute the squared-distance to another vector w/o dimension check.
Definition: GeoVector.cxx:138
double Dist(const Vector &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:68
Vector & operator*=(const double rhs)
Definition: GeoVector.h:111
Vector operator*(const double &rhs) const
Definition: GeoVector.h:155
void Normalize()
Normalize itself.
Definition: GeoVector.cxx:116
double SqLength() const
Compute the squared length of the vector.
Definition: GeoVector.cxx:49
Vector(const std::vector< double > &obj)
Default ctor w/ a bare std::vector<double>
Definition: GeoVector.h:48
Vector _Cross_(const Vector &obj) const
Compute a cross product w/o dimension check.
Definition: GeoVector.cxx:156
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
Vector operator/(const double &rhs) const
Definition: GeoVector.h:162
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:57
double Dot(const Vector &obj) const
Definition: GeoVector.cxx:73
bool operator<(const double &rhs) const
Definition: GeoVector.h:177
bool IsValid() const
Check if point is valid.
Definition: GeoVector.cxx:37
Vector operator-(const Vector &rhs) const
Definition: GeoVector.h:140
TMarker * pt
Definition: egs.C:25
Vector Cross(const Vector &obj) const
Compute a dot product of two vectors.
Definition: GeoVector.cxx:79
void RotateY(const double &theta)
Definition: GeoVector.cxx:185
Vector Point_t
Definition: GeoVector.h:204
double SqDist(const Vector &obj) const
Compute the squared distance to another vector.
Definition: GeoVector.cxx:62
TLorentzVector ToTLorentzVector() const
Compute an opening angle w.r.t. the given vector.
Definition: GeoVector.cxx:109
Vector & operator/=(const double rhs)
Definition: GeoVector.h:118
double operator*(const Vector &rhs) const
Definition: GeoVector.h:147
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:170
Vector Dir() const
Return a direction unit vector.
Definition: GeoVector.cxx:121
Vector & operator=(const Vector &rhs)
Definition: GeoVector.h:125
Vector Vector_t
Point has same feature as Vector.
Definition: GeoVector.h:203
Char_t n[5]
double Theta() const
Compute the angle theta.
Definition: GeoVector.cxx:94
double _Dist_(const Vector &obj) const
Compute the distance to another vector w/o dimension check.
Definition: GeoVector.cxx:146
static const double kINVALID_DOUBLE
void RotateZ(const double &theta)
Definition: GeoVector.cxx:200
bool operator==(const Vector &rhs) const
Definition: GeoVector.h:179
bool operator()(const geoalgo::Vector *lhs, const geoalgo::Vector *rhs)
Definition: GeoVector.h:212
double Angle(const Vector &obj) const
Compute a cross product of two vectors.
Definition: GeoVector.cxx:101
Vector & operator-=(const Vector &rhs)
Definition: GeoVector.h:104
friend std::ostream & operator<<(std::ostream &o,::geoalgo::Vector const &a)
Streamer.
Definition: GeoVector.h:191