Logo ROOT  
Reference Guide
 
Loading...
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/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
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 <TMath.h>
16
17#include <iosfwd>
18
19namespace ROOT {
20namespace Geom {
21
22struct Vertex_t {
23 double fVec[3] = {0.};
24
25 Vertex_t(const double a, const double b, const double c)
26 {
27 fVec[0] = a;
28 fVec[1] = b;
29 fVec[2] = c;
30 }
31
32 Vertex_t(const double a = 0.)
33 {
34 fVec[0] = a;
35 fVec[1] = a;
36 fVec[2] = a;
37 }
38
39 double &operator[](const int index) { return fVec[index]; }
40 double const &operator[](const int index) const { return fVec[index]; }
41
42 // Inplace binary operators
43
44#define Vertex_t_INPLACE_BINARY_OP(OPERATOR) \
45 inline Vertex_t &operator OPERATOR(const Vertex_t &other) \
46 { \
47 fVec[0] OPERATOR other.fVec[0]; \
48 fVec[1] OPERATOR other.fVec[1]; \
49 fVec[2] OPERATOR other.fVec[2]; \
50 return *this; \
51 } \
52 inline Vertex_t &operator OPERATOR(const double &scalar) \
53 { \
54 fVec[0] OPERATOR scalar; \
55 fVec[1] OPERATOR scalar; \
56 fVec[2] OPERATOR scalar; \
57 return *this; \
58 }
61#undef Vertex_t_INPLACE_BINARY_OP
62
63 double &x()
64 {
65 return fVec[0];
66 }
67 double const &x() const { return fVec[0]; }
68
69 double &y() { return fVec[1]; }
70 double const &y() const { return fVec[1]; }
71
72 double &z() { return fVec[2]; }
73 double const &z() const { return fVec[2]; }
74
75 inline void CopyTo(double *dest) const
76 {
77 dest[0] = fVec[0];
78 dest[1] = fVec[1];
79 dest[2] = fVec[2];
80 }
81
82 void Set(double const &a, double const &b, double const &c)
83 {
84 fVec[0] = a;
85 fVec[1] = b;
86 fVec[2] = c;
87 }
88
89 void Set(const double a) { Set(a, a, a); }
90
91 /// \return the length squared perpendicular to z direction
92 double Perp2() const { return fVec[0] * fVec[0] + fVec[1] * fVec[1]; }
93
94 /// \return the length perpendicular to z direction
95 double Perp() const { return TMath::Sqrt(Perp2()); }
96
97 /// The dot product of two vector objects
98 static double Dot(Vertex_t const &left, Vertex_t const &right)
99 {
100 return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
101 }
102
103 /// The dot product of two vector
104 double Dot(Vertex_t const &right) const { return Dot(*this, right); }
105
106 /// \return Squared magnitude of the vector.
107 double Mag2() const { return Dot(*this, *this); }
108
109 /// \return Magnitude of the vector.
110 double Mag() const { return TMath::Sqrt(Mag2()); }
111
112 double Length() const { return Mag(); }
113
114 double Length2() const { return Mag2(); }
115
116 /// Normalizes the vector by dividing each entry by the length.
117 void Normalize() { *this *= (1. / Length()); }
118
119 // Vertex_t Normalized() const { return Vertex_t(*this) * (1. / Length()); }
120
121 // checks if vector is normalized
122 bool IsNormalized() const
123 {
124 double norm = Mag2();
125 constexpr double tolerance = 1.e-10;
126 return 1. - tolerance < norm && norm < 1 + tolerance;
127 }
128
129 /// \return Azimuthal angle between -pi and pi.
130 double Phi() const { return TMath::ATan2(fVec[1], fVec[0]); }
131
132 /// \return Polar angle between 0 and pi.
133 double Theta() const { return TMath::ACos(fVec[2] / Mag()); }
134
135 /// The cross (vector) product of two Vector3D<T> objects
136 static Vertex_t Cross(Vertex_t const &left, Vertex_t const &right)
137 {
138 return Vertex_t(left[1] * right[2] - left[2] * right[1], left[2] * right[0] - left[0] * right[2],
139 left[0] * right[1] - left[1] * right[0]);
140 }
141
142 Vertex_t Abs() const { return Vertex_t(TMath::Abs(fVec[0]), TMath::Abs(fVec[1]), TMath::Abs(fVec[2])); }
143
144 double Min() const { return TMath::Min(TMath::Min(fVec[0], fVec[1]), fVec[2]); }
145
146 double Max() const { return TMath::Max(TMath::Max(fVec[0], fVec[1]), fVec[2]); }
147
149 {
150 constexpr double kMinimum = std::numeric_limits<double>::min();
151 const double mag2 = Mag2();
152 Vertex_t output(*this);
153 output /= TMath::Sqrt(mag2 + kMinimum);
154 return output;
155 }
156};
157
158inline bool operator==(Vertex_t const &lhs, Vertex_t const &rhs)
159{
160 constexpr double kTolerance = 1.e-8;
161 return TMath::Abs(lhs[0] - rhs[0]) < kTolerance && TMath::Abs(lhs[1] - rhs[1]) < kTolerance &&
162 TMath::Abs(lhs[2] - rhs[2]) < kTolerance;
163}
164
165inline bool operator!=(Vertex_t const &lhs, Vertex_t const &rhs)
166{
167 return !(lhs == rhs);
168}
169
170#define Vertex_t_BINARY_OP(OPERATOR, INPLACE) \
171 inline Vertex_t operator OPERATOR(const Vertex_t &lhs, const Vertex_t &rhs) \
172 { \
173 Vertex_t result(lhs); \
174 result INPLACE rhs; \
175 return result; \
176 } \
177 inline Vertex_t operator OPERATOR(Vertex_t const &lhs, const double rhs) \
178 { \
179 Vertex_t result(lhs); \
180 result INPLACE rhs; \
181 return result; \
182 } \
183 inline Vertex_t operator OPERATOR(const double lhs, Vertex_t const &rhs) \
184 { \
185 Vertex_t result(lhs); \
186 result INPLACE rhs; \
187 return result; \
188 }
190#undef Vertex_t_BINARY_OP
191
192} // namespace Geom
193} // namespace ROOT
194
195std::ostream &operator<<(std::ostream &os, ROOT::Geom::Vertex_t const &vec);
196
197#endif
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Tessellated::Vertex_t Vertex_t
#define Vertex_t_BINARY_OP(OPERATOR, INPLACE)
std::ostream & operator<<(std::ostream &os, ROOT::Geom::Vertex_t const &vec)
#define Vertex_t_INPLACE_BINARY_OP(OPERATOR)
Definition TGeoVector3.h:44
bool operator!=(Vertex_t const &lhs, Vertex_t const &rhs)
bool operator==(Vertex_t const &lhs, Vertex_t const &rhs)
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:645
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
Double_t ATan2(Double_t y, Double_t x)
Returns the principal value of the arc tangent of y/x, expressed in radians.
Definition TMath.h:659
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122
double & operator[](const int index)
Definition TGeoVector3.h:39
double const & x() const
Definition TGeoVector3.h:67
void Set(double const &a, double const &b, double const &c)
Definition TGeoVector3.h:82
double Min() const
double const & z() const
Definition TGeoVector3.h:73
void Set(const double a)
Definition TGeoVector3.h:89
double Length2() const
double const & y() const
Definition TGeoVector3.h:70
double Perp() const
Definition TGeoVector3.h:95
double Mag() const
Vertex_t Unit() const
void CopyTo(double *dest) const
Definition TGeoVector3.h:75
Vertex_t Abs() const
double Mag2() const
double Max() const
bool IsNormalized() const
static Vertex_t Cross(Vertex_t const &left, Vertex_t const &right)
The cross (vector) product of two Vector3D<T> objects.
void Normalize()
Normalizes the vector by dividing each entry by the length.
Vertex_t(const double a=0.)
Definition TGeoVector3.h:32
double Phi() const
Vertex_t(const double a, const double b, const double c)
Definition TGeoVector3.h:25
double Length() const
double const & operator[](const int index) const
Definition TGeoVector3.h:40
double Dot(Vertex_t const &right) const
The dot product of two vector.
double Theta() const
double Perp2() const
Definition TGeoVector3.h:92
static double Dot(Vertex_t const &left, Vertex_t const &right)
The dot product of two vector objects.
Definition TGeoVector3.h:98