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