Project
Loading...
Searching...
No Matches
FairGeoVector.h
Go to the documentation of this file.
1/********************************************************************************
2 * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3 * *
4 * This software is distributed under the terms of the *
5 * GNU Lesser General Public Licence (LGPL) version 3, *
6 * copied verbatim in the file "LICENSE" *
7 ********************************************************************************/
8#ifndef FAIRGEOVECTOR_H
9#define FAIRGEOVECTOR_H
10
11#include <Rtypes.h> // for Double_t, Bool_t, Int_t, etc
12#include <TMath.h> // for pow, floor, sqrt
13#include <TMathBase.h> // for Abs
14#include <TObject.h> // for TObject
15#include <cmath>
16#include <iostream> // for operator<<, ostream, etc
17#include <stdio.h> // for printf
18
19class FairGeoVector : public TObject
20{
21 protected:
22 Double_t x;
23 Double_t y;
24 Double_t z;
25 inline void round(Double_t d, Int_t n);
26
27 public:
28 FairGeoVector(Double_t dx = 0, Double_t dy = 0, Double_t dz = 0)
29 : TObject()
30 , x(dx)
31 , y(dy)
32 , z(dz)
33 {}
35 : TObject(v)
36 , x(v.getX())
37 , y(v.getY())
38 , z(v.getZ())
39 {}
41 Double_t& X() { return x; }
42 Double_t& Y() { return y; }
43 Double_t& Z() { return z; }
44 Double_t getX() const { return x; }
45 Double_t getY() const { return y; }
46 Double_t getZ() const { return z; }
47
48 Double_t getValues(Int_t i)
49 {
50 if (i < 0 || i > 2) {
51 std::cout << " -E- Vector index is 0 1 2 only ! " << std::endl;
52 }
53 if (i == 0) {
54 return x;
55 }
56 if (i == 1) {
57 return y;
58 }
59 if (i == 2) {
60 return z;
61 }
62 return -1;
63 }
64
65 void setXYZ(const Double_t xx, const Double_t yy, const Double_t zz)
66 {
67 x = xx;
68 y = yy;
69 z = zz;
70 }
71 void setX(const Double_t a) { x = a; }
72 void setY(const Double_t a) { y = a; }
73 void setZ(const Double_t a) { z = a; }
74 inline void setVector(const Double_t* a);
75 inline void setVector(const Float_t* a);
76 inline Double_t operator()(const Int_t i) const;
77 inline FairGeoVector operator-() const;
78 inline FairGeoVector& operator=(const FairGeoVector& v);
79 inline Bool_t operator==(const FairGeoVector& v) const;
80 inline Bool_t operator!=(const FairGeoVector& v) const;
81 inline Bool_t operator<(const Double_t a);
82 inline Bool_t operator<=(const Double_t a);
83 inline Bool_t operator>(const Double_t a);
84 inline Bool_t operator>=(const Double_t a);
85 inline FairGeoVector& operator+=(const Double_t a);
86 inline FairGeoVector& operator-=(const Double_t a);
87 inline FairGeoVector& operator*=(const Double_t a);
88 inline FairGeoVector& operator/=(const Double_t a);
91 inline FairGeoVector operator+(const FairGeoVector& v) const;
92 inline FairGeoVector operator-(const FairGeoVector& v) const;
93 inline FairGeoVector& abs();
94 inline Double_t scalarProduct(const FairGeoVector& v) const;
95 inline FairGeoVector vectorProduct(const FairGeoVector& v) const;
96 Double_t length() const { return sqrt(x * x + y * y + z * z); }
97 void clear() { x = y = z = 0.; }
98 void print() const { printf("%10.3f%10.3f%10.3f\n", x, y, z); }
99 inline void round(Int_t n);
100 inline friend std::ostream& operator<<(std::ostream& put, const FairGeoVector& v);
101 inline friend std::istream& operator>>(std::istream& get, FairGeoVector& v);
102 ClassDef(FairGeoVector, 1); // vector with 3 components
103};
104
105// -------------------- inlines ---------------------------
106
107inline void FairGeoVector::setVector(const Double_t* a)
108{
109 x = a[0];
110 y = a[1];
111 z = a[2];
112}
113
115{
116 x = a[0];
117 y = a[1];
118 z = a[2];
119}
120
121inline Double_t FairGeoVector::operator()(const Int_t i) const
122{
123 switch (i) {
124 case 0:
125 return x;
126 case 1:
127 return y;
128 case 2:
129 return z;
130 default:
131 Error("operator()", "bad index");
132 }
133 return 0;
134}
135
137{
138 FairGeoVector p(-x, -y, -z);
139 return p;
140}
141
143{
144 x = v.getX();
145 y = v.getY();
146 z = v.getZ();
147 return *this;
148}
149
150inline Bool_t FairGeoVector::operator==(const FairGeoVector& v) const
151{
152 return ((v.getX() != x || v.getY() != y || v.getZ() != z) ? kFALSE : kTRUE);
153}
154
155inline Bool_t FairGeoVector::operator!=(const FairGeoVector& v) const
156{
157 return (v.getX() != x || v.getY() != y || v.getZ() != z) ? kTRUE : kFALSE;
158}
160inline Bool_t FairGeoVector::operator<(const Double_t a) { return (x >= a || y >= a || z >= a) ? kFALSE : kTRUE; }
161
162inline Bool_t FairGeoVector::operator<=(const Double_t a) { return (x > a || y > a || z > a) ? kFALSE : kTRUE; }
163
164inline Bool_t FairGeoVector::operator>(const Double_t a) { return (x <= a || y <= a || z <= a) ? kFALSE : kTRUE; }
165
166inline Bool_t FairGeoVector::operator>=(const Double_t a) { return (x < a || y < a || z < a) ? kFALSE : kTRUE; }
167
169{
170 x += a;
171 y += a;
172 z += a;
173 return *this;
174}
175
177{
178 x -= a;
179 y -= a;
180 z -= a;
181 return *this;
182}
183
185{
186 x *= a;
187 y *= a;
188 z *= a;
189 return *this;
190}
191
193{
194 x /= a;
195 y /= a;
196 z /= a;
197 return *this;
198}
199
201{
202 x += v.getX();
203 y += v.getY();
204 z += v.getZ();
205 return *this;
206}
207
209{
210 x -= v.getX();
211 y -= v.getY();
212 z -= v.getZ();
213 return *this;
214}
215
217{
218 FairGeoVector p(*this);
219 return p += v;
220}
221
223{
224 FairGeoVector p(*this);
225 return p -= v;
226}
227
229{
230 x = TMath::Abs(x);
231 y = TMath::Abs(y);
232 z = TMath::Abs(z);
233 return *this;
234}
235
236inline Double_t FairGeoVector::scalarProduct(const FairGeoVector& v) const
237{
238 return (x * v.getX() + y * v.getY() + z * v.getZ());
239}
240
242{
243 FairGeoVector p(y * v.getZ() - z * v.getY(), z * v.getX() - x * v.getZ(), x * v.getY() - y * v.getX());
244 return p;
245}
246
247inline void FairGeoVector::round(Double_t d, Int_t n)
248{
249 // rounds d to a precision with n digits
250 if (d > 0) {
251 d = floor(d * pow(10., n) + 0.5) / pow(10., n);
252 } else {
253 d = -floor((-d) * pow(10., n) + 0.5) / pow(10., n);
254 }
255}
256
257inline void FairGeoVector::round(Int_t n)
258{
259 // rounds every component to a precision with n digits
260 round(x, n);
261 round(y, n);
262 round(z, n);
263}
264
265inline std::ostream& operator<<(std::ostream& put, const FairGeoVector& v)
266{
267 return put << v(0) << " " << v(1) << " " << v(2) << '\n';
268}
269
270inline std::istream& operator>>(std::istream& get, FairGeoVector& v)
271{
272 Double_t x[3];
273 get >> x[0] >> x[1] >> x[2];
274 v.setVector(x);
275 return get;
276}
277
278#endif /* !FAIRGEOVECTOR_H */
std::ostream & operator<<(std::ostream &put, const FairGeoVector &v)
std::istream & operator>>(std::istream &get, FairGeoVector &v)
int32_t i
Double_t operator()(const Int_t i) const
Bool_t operator<(const Double_t a)
check with ilse
Bool_t operator>=(const Double_t a)
Bool_t operator==(const FairGeoVector &v) const
FairGeoVector & abs()
FairGeoVector & operator+=(const Double_t a)
Double_t & Y()
Bool_t operator<=(const Double_t a)
Double_t & X()
Bool_t operator>(const Double_t a)
friend std::ostream & operator<<(std::ostream &put, const FairGeoVector &v)
void round(Double_t d, Int_t n)
FairGeoVector & operator*=(const Double_t a)
FairGeoVector operator+(const FairGeoVector &v) const
Double_t getZ() const
Double_t scalarProduct(const FairGeoVector &v) const
FairGeoVector & operator-=(const Double_t a)
Double_t & Z()
ClassDef(FairGeoVector, 1)
void setY(const Double_t a)
FairGeoVector & operator=(const FairGeoVector &v)
friend std::istream & operator>>(std::istream &get, FairGeoVector &v)
void print() const
Bool_t operator!=(const FairGeoVector &v) const
Double_t getValues(Int_t i)
void setX(const Double_t a)
FairGeoVector vectorProduct(const FairGeoVector &v) const
FairGeoVector(Double_t dx=0, Double_t dy=0, Double_t dz=0)
Double_t length() const
FairGeoVector(const FairGeoVector &v)
FairGeoVector & operator/=(const Double_t a)
void setXYZ(const Double_t xx, const Double_t yy, const Double_t zz)
void setVector(const Double_t *a)
void setZ(const Double_t a)
FairGeoVector operator-() const
Double_t getX() const
Double_t getY() const
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
const GLdouble * v
Definition glcorearb.h:832
GLint y
Definition glcorearb.h:270
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843