LArSoft  v10_04_05
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(double const x, double const y);
51  Vector(double const x, double const y, double const z);
52  Vector(TVector3 const& pt);
53  Vector(TLorentzVector const& 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(Vector const& obj) const;
65  double Dist(Vector const& obj) const;
66  double Dot(Vector const& obj) const;
67  Vector Cross(Vector const& obj) const;
68  double Angle(Vector const& obj) const;
69 
70  TLorentzVector ToTLorentzVector()
71  const;
72 
74  void compat(Vector const& obj) const;
75 
77  void RotateX(double const& theta);
78  void RotateY(double const& theta);
79  void RotateZ(double const& theta);
80 
81  protected:
83  double _SqDist_(Vector const& obj) const;
85  double _Dist_(Vector const& obj) const;
87  double _Dot_(Vector const& obj) const;
89  Vector _Cross_(Vector const& obj) const;
91  double _Angle_(Vector const& obj) const;
92 
93  public:
94  //
95  // binary/uniry operators
96  //
97  inline Vector& operator+=(Vector const& 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-=(Vector const& 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*=(double const rhs)
112  {
113  for (auto& v : *this)
114  v *= rhs;
115  return *this;
116  }
117 
118  inline Vector& operator/=(double const rhs)
119  {
120  for (auto& v : *this)
121  v /= rhs;
122  return *this;
123  }
124 
125  inline Vector& operator=(Vector const& 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+(Vector const& rhs) const
134  {
135  Vector res((*this));
136  res += rhs;
137  return res;
138  }
139 
140  inline Vector operator-(Vector const& rhs) const
141  {
142  Vector res((*this));
143  res -= rhs;
144  return res;
145  }
146 
147  inline double operator*(Vector const& 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*(double const& rhs) const
156  {
157  Vector res((*this));
158  res *= rhs;
159  return res;
160  }
161 
162  inline Vector operator/(double const& rhs) const
163  {
164  Vector res((*this));
165  res /= rhs;
166  return res;
167  }
168 
169  inline bool operator<(Vector const& 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<(double const& rhs) const { return Length() < rhs; }
178 
179  inline bool operator==(Vector const& 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!=(Vector const& 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<(double const &rhs) const
Definition: GeoVector.h:177
Vector operator*(double const &rhs) const
Definition: GeoVector.h:155
void RotateX(double const &theta)
rotation operations
Definition: GeoVector.cxx:170
double operator*(Vector const &rhs) const
Definition: GeoVector.h:147
void compat(Vector const &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:128
Algorithm to compute various geometrical relation among geometrical objects. In particular functions ...
Definition: GeoAlgo.h:43
Vector & operator-=(Vector const &rhs)
Definition: GeoVector.h:104
void RotateZ(double const &theta)
Definition: GeoVector.cxx:200
double Dot(Vector const &obj) const
Definition: GeoVector.cxx:73
Float_t y
Definition: compare.C:6
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.
static double const kINVALID_DOUBLE
Vector & operator=(Vector const &rhs)
Definition: GeoVector.h:125
bool operator==(Vector const &rhs) const
Definition: GeoVector.h:179
void RotateY(double const &theta)
Definition: GeoVector.cxx:185
Vector operator/(double const &rhs) const
Definition: GeoVector.h:162
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
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(size_t n)
Ctor to instantiate with invalid value.
Definition: GeoVector.h:45
double Angle(Vector const &obj) const
Compute a cross product of two vectors.
Definition: GeoVector.cxx:101
double Dist(Vector const &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:68
double Length() const
Compute the length of the vector.
Definition: GeoVector.cxx:57
bool operator!=(Vector const &rhs) const
Definition: GeoVector.h:187
double SqDist(Vector const &obj) const
Compute the squared distance to another vector.
Definition: GeoVector.cxx:62
bool IsValid() const
Check if point is valid.
Definition: GeoVector.cxx:37
TMarker * pt
Definition: egs.C:25
double _SqDist_(Vector const &obj) const
Compute the squared-distance to another vector w/o dimension check.
Definition: GeoVector.cxx:138
Vector operator+(Vector const &rhs) const
Definition: GeoVector.h:133
double _Dot_(Vector const &obj) const
Compute a dot product w/o dimention check.
Definition: GeoVector.cxx:151
Vector Point_t
Definition: GeoVector.h:204
Vector & operator+=(Vector const &rhs)
Definition: GeoVector.h:97
Vector _Cross_(Vector const &obj) const
Compute a cross product w/o dimension check.
Definition: GeoVector.cxx:156
TLorentzVector ToTLorentzVector() const
Compute an opening angle w.r.t. the given vector.
Definition: GeoVector.cxx:109
Representation of a 3D semi-infinite line. Defines a semi-infinite 3D line by having a start point (P...
Definition: GeoHalfLine.h:30
Vector Dir() const
Return a direction unit vector.
Definition: GeoVector.cxx:121
double _Angle_(Vector const &obj) const
Compute the angle in degrees between 2 vectors w/o dimension check.
Definition: GeoVector.cxx:165
Vector operator-(Vector const &rhs) const
Definition: GeoVector.h:140
Vector Vector_t
Point has same feature as Vector.
Definition: GeoVector.h:203
Char_t n[5]
double _Dist_(Vector const &obj) const
Compute the distance to another vector w/o dimension check.
Definition: GeoVector.cxx:146
double Theta() const
Compute the angle theta.
Definition: GeoVector.cxx:94
Vector & operator*=(double const rhs)
Definition: GeoVector.h:111
bool operator<(Vector const &rhs) const
Definition: GeoVector.h:169
bool operator()(const geoalgo::Vector *lhs, const geoalgo::Vector *rhs)
Definition: GeoVector.h:212
Vector Cross(Vector const &obj) const
Compute a dot product of two vectors.
Definition: GeoVector.cxx:79
Vector & operator/=(double const rhs)
Definition: GeoVector.h:118
friend std::ostream & operator<<(std::ostream &o,::geoalgo::Vector const &a)
Streamer.
Definition: GeoVector.h:191