ROOT   Reference Guide
Searching...
No Matches
TGeoVector3.h
Go to the documentation of this file.
1// @(#)root/geom:$Id$
2// Author: Andrei Gheata 20/12/19
3
4/*************************************************************************
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. * 9 * For the list of contributors see$ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TGeoVector3
13#define ROOT_TGeoVector3
14
15#include <Riostream.h>
16#include <TMath.h>
17
18namespace ROOT {
19namespace Geom {
20
21struct Vertex_t {
22 double fVec[3] = {0.};
23
24 Vertex_t(const double a, const double b, const double c)
25 {
26 fVec[0] = a;
27 fVec[1] = b;
28 fVec[2] = c;
29 }
30
31 Vertex_t(const double a = 0.)
32 {
33 fVec[0] = a;
34 fVec[1] = a;
35 fVec[2] = a;
36 }
37
38 double &operator[](const int index) { return fVec[index]; }
39 double const &operator[](const int index) const { return fVec[index]; }
40
41 // Inplace binary operators
42
43#define Vertex_t_INPLACE_BINARY_OP(OPERATOR) \
44 inline Vertex_t &operator OPERATOR(const Vertex_t &other) \
45 { \
46 fVec[0] OPERATOR other.fVec[0]; \
47 fVec[1] OPERATOR other.fVec[1]; \
48 fVec[2] OPERATOR other.fVec[2]; \
49 return *this; \
50 } \
51 inline Vertex_t &operator OPERATOR(const double &scalar) \
52 { \
53 fVec[0] OPERATOR scalar; \
54 fVec[1] OPERATOR scalar; \
55 fVec[2] OPERATOR scalar; \
56 return *this; \
57 }
60#undef Vertex_t_INPLACE_BINARY_OP
61
62 double &x()
63 {
64 return fVec[0];
65 }
66 double const &x() const { return fVec[0]; }
67
68 double &y() { return fVec[1]; }
69 double const &y() const { return fVec[1]; }
70
71 double &z() { return fVec[2]; }
72 double const &z() const { return fVec[2]; }
73
74 inline void CopyTo(double *dest) const
75 {
76 dest[0] = fVec[0];
77 dest[1] = fVec[1];
78 dest[2] = fVec[2];
79 }
80
81 void Set(double const &a, double const &b, double const &c)
82 {
83 fVec[0] = a;
84 fVec[1] = b;
85 fVec[2] = c;
86 }
87
88 void Set(const double a) { Set(a, a, a); }
89
90 /// \Return the length squared perpendicular to z direction
91 double Perp2() const { return fVec[0] * fVec[0] + fVec[1] * fVec[1]; }
92
93 /// \Return the length perpendicular to z direction
94 double Perp() const { return TMath::Sqrt(Perp2()); }
95
96 /// The dot product of two vector objects
97 static double Dot(Vertex_t const &left, Vertex_t const &right)
98 {
99 return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
100 }
101
102 /// The dot product of two vector
103 double Dot(Vertex_t const &right) const { return Dot(*this, right); }
104
105 /// \return Squared magnitude of the vector.
106 double Mag2() const { return Dot(*this, *this); }
107
108 /// \return Magnitude of the vector.
109 double Mag() const { return TMath::Sqrt(Mag2()); }
110
111 double Length() const { return Mag(); }
112
113 double Length2() const { return Mag2(); }
114
115 /// Normalizes the vector by dividing each entry by the length.
116 void Normalize() { *this *= (1. / Length()); }
117
118 // Vertex_t Normalized() const { return Vertex_t(*this) * (1. / Length()); }
119
120 // checks if vector is normalized
121 bool IsNormalized() const
122 {
123 double norm = Mag2();
124 constexpr double tolerance = 1.e-10;
125 return 1. - tolerance < norm && norm < 1 + tolerance;
126 }
127
128 /// \return Azimuthal angle between -pi and pi.
129 double Phi() const { return TMath::ATan2(fVec[1], fVec[0]); }
130
131 /// \return Polar angle between 0 and pi.
132 double Theta() const { return TMath::ACos(fVec[2] / Mag()); }
133
134 /// The cross (vector) product of two Vector3D<T> objects
135 static Vertex_t Cross(Vertex_t const &left, Vertex_t const &right)
136 {
137 return Vertex_t(left[1] * right[2] - left[2] * right[1], left[2] * right[0] - left[0] * right[2],
138 left[0] * right[1] - left[1] * right[0]);
139 }
140
141 Vertex_t Abs() const { return Vertex_t(TMath::Abs(fVec[0]), TMath::Abs(fVec[1]), TMath::Abs(fVec[2])); }
142
143 double Min() const { return TMath::Min(TMath::Min(fVec[0], fVec[1]), fVec[2]); }
144
145 double Max() const { return TMath::Max(TMath::Max(fVec[0], fVec[1]), fVec[2]); }
146
147 Vertex_t Unit() const
148 {
149 constexpr double kMinimum = std::numeric_limits<double>::min();
150 const double mag2 = Mag2();
151 Vertex_t output(*this);
152 output /= TMath::Sqrt(mag2 + kMinimum);
153 return output;
154 }
155};
156
157inline bool operator==(Vertex_t const &lhs, Vertex_t const &rhs)
158{
159 constexpr double kTolerance = 1.e-8;
160 return TMath::Abs(lhs[0] - rhs[0]) < kTolerance && TMath::Abs(lhs[1] - rhs[1]) < kTolerance &&
161 TMath::Abs(lhs[2] - rhs[2]) < kTolerance;
162}
163
164inline bool operator!=(Vertex_t const &lhs, Vertex_t const &rhs)
165{
166 return !(lhs == rhs);
167}
168
169#define Vertex_t_BINARY_OP(OPERATOR, INPLACE) \
170 inline Vertex_t operator OPERATOR(const Vertex_t &lhs, const Vertex_t &rhs) \
171 { \
172 Vertex_t result(lhs); \
173 result INPLACE rhs; \
174 return result; \
175 } \
176 inline Vertex_t operator OPERATOR(Vertex_t const &lhs, const double rhs) \
177 { \
178 Vertex_t result(lhs); \
179 result INPLACE rhs; \
180 return result; \
181 } \
182 inline Vertex_t operator OPERATOR(const double lhs, Vertex_t const &rhs) \
183 { \
184 Vertex_t result(lhs); \
185 result INPLACE rhs; \
186 return result; \
187 }
189#undef Vertex_t_BINARY_OP
190
191} // namespace Geom
192} // namespace ROOT
193
194std::ostream &operator<<(std::ostream &os, ROOT::Geom::Vertex_t const &vec);
195
196#endif
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
TGLVector3 Cross(const TGLVector3 &v1, const TGLVector3 &v2)
Definition TGLUtil.h:323
#define Vertex_t_BINARY_OP(OPERATOR, INPLACE)
#define Vertex_t_INPLACE_BINARY_OP(OPERATOR)
Definition TGeoVector3.h:43
SVector< T, D > Unit(const SVector< T, D > &rhs)
Unit.
Definition Functions.h:382
T Mag2(const SVector< T, D > &rhs)
Vector magnitude square Template to compute .
Definition Functions.h:230
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition Functions.h:253
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
bool operator!=(Vertex_t const &lhs, Vertex_t const &rhs)
bool operator==(Vertex_t const &lhs, Vertex_t const &rhs)
double Perp(const Vector1 &v, const Vector2 &u)
Find the magnitude of the vector component of v perpendicular to the given direction of u.
Definition VectorUtil.h:226
double Perp2(const Vector1 &v, const Vector2 &u)
Find the magnitude square of the vector component of v perpendicular to the given direction of u.
Definition VectorUtil.h:210
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Double_t ACos(Double_t)
Definition TMath.h:669
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:212
Float_t Normalize(Float_t v[3])
Normalize a vector v in place.
Definition TMath.cxx:495
Double_t ATan2(Double_t y, Double_t x)
Definition TMath.h:679
Double_t Sqrt(Double_t x)
Definition TMath.h:691
Short_t Min(Short_t a, Short_t b)
Definition TMathBase.h:180
Short_t Abs(Short_t d)
Definition TMathBase.h:120
#define Dot(u, v)
Definition normal.c:49
double & operator[](const int index)
Definition TGeoVector3.h:38
Vertex_t(const double a=0.)
Definition TGeoVector3.h:31
Vertex_t(const double a, const double b, const double c)
Definition TGeoVector3.h:24
double const & operator[](const int index) const
Definition TGeoVector3.h:39
#define dest(otri, vertexptr)
Definition triangle.c:1040
static void output(int code)
Definition gifencode.c:226