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