forked from ara-software/AraSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
161 lines (120 loc) · 5.87 KB
/
Vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
////////////////////////////////////////////////////////////////////////////////////////////////
//class Vector:
//This class represents a three-vector. Operators are overloaded to provide for the
//familiar operations of vector addition, subtraction, scalar multiplication and division,
//and the dot product. The x,y,z components can be output with the << operator onto an
//output stream.
//Methods are provided to take the vector product, find the magnitude, find a three-dimensional
//angle, and perform various rotations.
//
//The only methods that will alter an established vector are Reset and the three Set functions.
//All other methods (cross product, rotations, etc.) return a new Vector object.
////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef VECTOR_H
#define VECTOR_H
#include "TObject.h"
#include <iostream>
class Vector {
//class Vector : public TObject {
protected:
//Class variables
double x; //x component of vector
double y; //y component of vector
double z; //z component of vector
double theta; //theta component of vector in radians
double phi; //phi component of vector in radians
double r; //r component of vector
//Class private functions
void UpdateThetaPhi();
void UpdateXYZ();
//This method finds theta and phi from the x,y,z Cartesian coordinates.
//It should be called at any time that the x,y,z components are modified,
//so that the theta and phi components are current at all times.
public:
friend Vector operator +(const Vector& vector1, const Vector& vector2);
//Add two vectors component by component
friend void operator +=(Vector& vector1, const Vector& vector2);
friend Vector operator -(const Vector& vector1, const Vector& vector2);
//Subtract two vectors component by component
friend void operator -=(Vector& vector1, const Vector& vector2);
friend Vector operator -(const Vector& vec);
//Gives the negative of a vector
friend double operator *(const Vector& vector1, const Vector& vector2);
//Take the dot product of two vectors
friend Vector operator /(const Vector &v, const double& a);
//Divide a vector by a scalar
friend Vector operator *(const double& a, const Vector& v);
//Multiply a vector by a scalar
friend std::ostream& operator <<(std::ostream& outs, const Vector& vec);
//Print the three rectangular components of the vector to the screen
double operator [](int i) const;
//Returns an element of the vector. vector[0] is the x component,
//vector[1] is the y component, and vector[2] is the z component.
Vector(double x_inp,double y_inp,double z_inp);
//Constructor: Initialize a new vector with given values of x, y, and z.
Vector(double *xarray);
//Constructor: Initialize a new vector with elements of xarray giving values
// of x,y,z.
Vector(double theta, double phi);
//Constructor: Initialize a new vector with unit length and in the
//theta, phi direction.
//theta and phi must be in RADIANS!
//Accepts theta from 0 to PI, and any phi.
Vector();
//Default constructor: Initialize a unit vector in the z direction.
Vector RotateX(double angle) const;
//Returns the vector rotated counterclockwise (right handed coordinates)
//by "angle" radians about the X axis.
//N.B. : Returns a new Vector object. Does not change the vector it is called from.
Vector RotateY(double angle) const;
//Returns the vector rotated counterclockwise (right handed coordinates)
//by "angle" radians about the Y axis.
//N.B. : Returns a new Vector object. Does not change the vector it is called from.
Vector RotateZ(double angle) const;
//Returns the vector rotated counterclockwise (right handed coordinates)
//by "angle" radians about the Z axis.
//N.B. : Returns a new Vector object. Does not change the vector it is called from.
Vector Cross(const Vector &vec) const;
//Takes the cross product this x vec.
double Dot(const Vector &vec) const;
//Takes the dot product this x vec.
Vector Rotate(double angle, const Vector &axis) const;
//Returns the vector that is this vector rotated around the vector "axis" by angle (in radians) "angle".
Vector Zero();
//zero the vector
double Mag() const;
//Returns the magnitude of this vector.
double Angle(const Vector &vec) const;
//Returns the 3-dimensional angle between this vector and the argument.
Vector ChangeCoord(const Vector &new_x_axis,const Vector &new_y_axis) const;
Vector ChangeCoord(const Vector &new_z_axis) const;
//Returns this vector, rotated to a new coordinate system with the argument as the z-axis.
//The vector is rotated in such a way that a vector pointing in the z direction will be
//rotated onto the new axis.
Vector Unit() const;
//Returns a unit vector in the same direction as this vector.
void RotateUz(const Vector &NewUzVector);
// Change original z-axis to match the new vector
// Note the new vector must be normalized
//Accessor functions
double GetX() const;
double GetY() const;
double GetZ() const;
double R() const;
double Theta() const;
double Phi() const;
void Print() const;
void PrintSp() const;
//Mutator functions
void SetX(double inp);
void SetY(double inp);
void SetZ(double inp);
void SetXYZ(double inpx,double inpy,double inpz);
void SetR(double inpr);
void SetThetaPhi(double inptheta,double inpphi);
void SetRThetaPhi(double inpr,double inptheta,double inpphi);
void Reset(double x_inp, double y_inp, double z_inp);
ClassDef(Vector,1);
}; //class Vector
////////////////////////////////////////////////////////////////////////////////////////////////
#endif //VECTOR_H