LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GeoVector.cxx
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include <sstream>
6 #include <vector>
7 
8 namespace geoalgo {
9 
10  Vector::Vector(const double x, const double y) : Vector(2)
11  {
12  (*this)[0] = x;
13  (*this)[1] = y;
14  }
15 
16  Vector::Vector(const double x, const double y, const double z) : Vector(3)
17  {
18  (*this)[0] = x;
19  (*this)[1] = y;
20  (*this)[2] = z;
21  }
22 
23  Vector::Vector(const TVector3& pt) : Vector(3)
24  {
25  (*this)[0] = pt[0];
26  (*this)[1] = pt[1];
27  (*this)[2] = pt[2];
28  }
29 
30  Vector::Vector(const TLorentzVector& pt) : Vector(3)
31  {
32  (*this)[0] = pt[0];
33  (*this)[1] = pt[1];
34  (*this)[2] = pt[2];
35  }
36 
37  bool Vector::IsValid() const
38  {
39 
40  for (auto const& v : (*this)) {
41  // if any point is different from kINVALID_DOUBLE
42  // then the point is valid
43  if (v != kINVALID_DOUBLE) return true;
44  }
45 
46  return false;
47  }
48 
49  double Vector::SqLength() const
50  {
51  double res = 0;
52  for (auto const& v : (*this))
53  res += v * v;
54  return res;
55  }
56 
57  double Vector::Length() const
58  {
59  return sqrt(SqLength());
60  }
61 
62  double Vector::SqDist(const Vector& obj) const
63  {
64  compat(obj);
65  return _SqDist_(obj);
66  }
67 
68  double Vector::Dist(const Vector& obj) const
69  {
70  return sqrt(SqDist(obj));
71  }
72 
73  double Vector::Dot(const Vector& obj) const
74  {
75  compat(obj);
76  return _Dot_(obj);
77  }
78 
79  Vector Vector::Cross(const Vector& obj) const
80  {
81 
82  if (size() != 3 || obj.size() != 3)
83 
84  throw GeoAlgoException("<<Cross>> only possible for 3-dimensional vectors!");
85 
86  return _Cross_(obj);
87  }
88 
89  double Vector::Phi() const
90  {
91  return (*this)[0] == 0.0 && (*this)[1] == 0.0 ? 0.0 : atan2((*this)[1], (*this)[0]);
92  }
93 
94  double Vector::Theta() const
95  {
96  if (size() != 3) throw GeoAlgoException("<<Theta>> Only possible for 3-dimensional vectors!");
97 
98  return (*this).Length() == 0.0 ? 0.0 : acos((*this)[2] / (*this).Length());
99  }
100 
101  double Vector::Angle(const Vector& obj) const
102  {
103  compat(obj);
104  if (size() != 2 && size() != 3)
105  throw GeoAlgoException("<<Angle>> only possible for 2 or 3-dimensional vectors!");
106  return _Angle_(obj);
107  }
108 
109  TLorentzVector Vector::ToTLorentzVector() const
110  {
111  if (size() != 3)
112  throw GeoAlgoException("<<ToTLorentsVector>> only possible for 3-dimensional vectors!");
113  return TLorentzVector((*this)[0], (*this)[1], (*this)[2], 0.);
114  }
115 
117  {
118  (*this) /= this->Length();
119  }
120 
122  {
123  Vector res(*this);
124  res /= res.Length();
125  return res;
126  }
127 
128  void Vector::compat(const Vector& obj) const
129  {
130  if (size() != obj.size()) {
131  std::ostringstream msg;
132  msg << "<<" << __FUNCTION__ << ">>"
133  << " size mismatch: " << size() << " != " << obj.size() << std::endl;
134  throw GeoAlgoException(msg.str());
135  }
136  }
137 
138  double Vector::_SqDist_(const Vector& obj) const
139  {
140  double dist = 0;
141  for (size_t i = 0; i < size(); ++i)
142  dist += ((*this)[i] - obj[i]) * ((*this)[i] - obj[i]);
143  return dist;
144  }
145 
146  double Vector::_Dist_(const Vector& obj) const
147  {
148  return sqrt(_SqDist_(obj));
149  }
150 
151  double Vector::_Dot_(const Vector& obj) const
152  {
153  return (*this) * obj;
154  }
155 
156  Vector Vector::_Cross_(const Vector& obj) const
157  {
158  Vector res(3);
159  res[0] = (*this)[1] * obj[2] - obj[1] * (*this)[2];
160  res[1] = (*this)[2] * obj[0] - obj[2] * (*this)[0];
161  res[2] = (*this)[0] * obj[1] - obj[0] * (*this)[1];
162  return res;
163  }
164 
165  double Vector::_Angle_(const Vector& obj) const
166  {
167  return acos(_Dot_(obj) / Length() / obj.Length());
168  }
169 
170  void Vector::RotateX(const double& theta)
171  {
172 
173  double c = cos(theta);
174  double s = sin(theta);
175 
176  double ynew = (*this)[1] * c - (*this)[2] * s;
177  double znew = (*this)[1] * s + (*this)[2] * c;
178 
179  (*this)[1] = ynew;
180  (*this)[2] = znew;
181 
182  return;
183  }
184 
185  void Vector::RotateY(const double& theta)
186  {
187 
188  double c = cos(theta);
189  double s = sin(theta);
190 
191  double xnew = (*this)[0] * c + (*this)[2] * s;
192  double znew = -(*this)[0] * s + (*this)[2] * c;
193 
194  (*this)[0] = xnew;
195  (*this)[2] = znew;
196 
197  return;
198  }
199 
200  void Vector::RotateZ(const double& theta)
201  {
202 
203  double c = cos(theta);
204  double s = sin(theta);
205 
206  double xnew = (*this)[0] * c - (*this)[1] * s;
207  double ynew = (*this)[0] * s + (*this)[1] * c;
208 
209  (*this)[0] = xnew;
210  (*this)[1] = ynew;
211 
212  return;
213  }
214 
215 }
Float_t x
Definition: compare.C:6
double _Angle_(const Vector &obj) const
Compute the angle in degrees between 2 vectors w/o dimension check.
Definition: GeoVector.cxx:165
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
Class def header for a class GeoAlgoException.
Vector()
Default ctor.
Definition: GeoVector.h:42
Double_t z
Definition: plot.C:276
double Phi() const
Compute the angle Phi.
Definition: GeoVector.cxx:89
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
void Normalize()
Normalize itself.
Definition: GeoVector.cxx:116
double SqLength() const
Compute the squared length of the vector.
Definition: GeoVector.cxx:49
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
double Length() const
Compute the length of the vector.
Definition: GeoVector.cxx:57
double Dot(const Vector &obj) const
Definition: GeoVector.cxx:73
Class def header for a class Point and Vector.
bool IsValid() const
Check if point is valid.
Definition: GeoVector.cxx:37
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
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
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
void RotateX(const double &theta)
rotation operations
Definition: GeoVector.cxx:170
Vector Dir() const
Return a direction unit vector.
Definition: GeoVector.cxx:121
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
double Angle(const Vector &obj) const
Compute a cross product of two vectors.
Definition: GeoVector.cxx:101