Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4INCLThreeVector.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // INCL++ intra-nuclear cascade model
27 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28 // Davide Mancusi, CEA
29 // Alain Boudard, CEA
30 // Sylvie Leray, CEA
31 // Joseph Cugnon, University of Liege
32 //
33 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 /*
38  * ThreeVector.hh
39  *
40  * \date 4 June 2009
41  * \author Pekka Kaitaniemi
42  */
43 
44 #ifndef G4INCLThreeVector_hh
45 #define G4INCLThreeVector_hh 1
46 
47 #include <string>
48 #include <sstream>
49 #include <cmath>
50 
51 namespace G4INCL {
52 
53  class ThreeVector {
54  public:
56  :x(0.0), y(0.0), z(0.0)
57  {}
58 
60  :x(ax), y(ay), z(az)
61  {}
62 
63  inline G4double getX() const { return x; }
64  inline G4double getY() const { return y; }
65  inline G4double getZ() const { return z; }
66 
67  inline G4double perp() const { return std::sqrt(x*x + y*y); }
68  inline G4double perp2() const { return x*x + y*y; }
69  /**
70  * Get the length of the vector.
71  */
72  inline G4double mag() const { return std::sqrt(x*x + y*y + z*z); }
73 
74  /**
75  * Get the square of the length.
76  */
77  inline G4double mag2() const { return (x*x + y*y + z*z); }
78 
79  /**
80  * Theta angle
81  */
82  inline G4double theta() const {
83  return x == 0.0 && y == 0.0 && z == 0.0 ? 0.0 : std::atan2(perp(),z);
84  }
85 
86  /**
87  * Phi angle
88  */
89  inline G4double phi() const {
90  return x == 0.0 && y == 0.0 ? 0.0 : std::atan2(y,x);
91  }
92 
93  /**
94  * Dot product.
95  */
96  inline G4double dot(const ThreeVector &v) const {
97  return (x*v.x + y*v.y + z*v.z);
98  }
99 
100  /**
101  * Vector product.
102  */
103  ThreeVector vector(const ThreeVector &v) const {
104  return ThreeVector(
105  y*v.z - z*v.y,
106  z*v.x - x*v.z,
107  x*v.y - y*v.x
108  );
109  }
110 
111  /// \brief Set the x coordinate
112  inline void setX(G4double ax) { x = ax; }
113 
114  /// \brief Set the y coordinate
115  inline void setY(G4double ay) { y = ay; }
116 
117  /// \brief Set the z coordinate
118  inline void setZ(G4double az) { z = az; }
119 
120  inline void operator+= (const ThreeVector &v) {
121  x += v.x;
122  y += v.y;
123  z += v.z;
124  }
125 
126  /// \brief Unary minus operator
127  inline ThreeVector operator- () const {
128  return ThreeVector(-x,-y,-z);
129  }
130 
131  inline void operator-= (const ThreeVector &v) {
132  x -= v.x;
133  y -= v.y;
134  z -= v.z;
135  }
136 
137  template<typename T>
138  inline void operator*= (const T &c) {
139  x *= c;
140  y *= c;
141  z *= c;
142  }
143 
144  template<typename T>
145  inline void operator/= (const T &c) {
146  const G4double oneOverC = 1./c;
147  this->operator*=(oneOverC);
148  }
149 
150  inline ThreeVector operator- (const ThreeVector &v) const {
151  return ThreeVector(x-v.x, y-v.y, z-v.z);
152  }
153 
154  inline ThreeVector operator+ (const ThreeVector &v) const {
155  return ThreeVector(x+v.x, y+v.y, z+v.z);
156  }
157 
158  /**
159  * Divides all components of the vector with a constant number.
160  */
161  inline ThreeVector operator/ (const G4double C) const {
162  const G4double oneOverC = 1./C;
163  return ThreeVector(x*oneOverC, y*oneOverC, z*oneOverC);
164  }
165 
166  inline ThreeVector operator* (const G4double C) const {
167  return ThreeVector(x*C, y*C, z*C);
168  }
169 
170  /** \brief Rotate the vector by a given angle around a given axis
171  *
172  * \param angle the rotation angle
173  * \param axis the rotation axis, which must be a unit vector
174  */
175  inline void rotate(const G4double angle, const ThreeVector &axis) {
176  // Use Rodrigues' formula
177  const G4double cos = std::cos(angle);
178  const G4double sin = std::sin(angle);
179  (*this) = (*this) * cos + axis.vector(*this) * sin + axis * (axis.dot(*this)*(1.-cos));
180  }
181 
182  std::string print() const {
183  std::stringstream ss;
184  ss <<"(x = " << x << " y = " << y << " z = " << z <<")";
185  return ss.str();
186  }
187 
188  std::string dump() const {
189  std::stringstream ss;
190  ss <<"(vector3 " << x << " " << y << " " << z << ")";
191  return ss.str();
192  }
193 
194  private:
195  G4double x, y, z; //> Vector components
196  };
197 
198 }
199 
200 #endif
G4double dot(const ThreeVector &v) const
void rotate(const G4double angle, const ThreeVector &axis)
Rotate the vector by a given angle around a given axis.
ThreeVector operator*(const G4double C) const
ThreeVector operator+(const ThreeVector &v) const
ThreeVector vector(const ThreeVector &v) const
G4double mag2() const
void setY(G4double ay)
Set the y coordinate.
G4double perp2() const
void operator*=(const T &c)
void operator+=(const ThreeVector &v)
ThreeVector operator/(const G4double C) const
G4double phi() const
std::string dump() const
void operator-=(const ThreeVector &v)
G4double perp() const
ThreeVector(G4double ax, G4double ay, G4double az)
ThreeVector operator-() const
Unary minus operator.
G4double getX() const
std::string print() const
void setX(G4double ax)
Set the x coordinate.
void operator/=(const T &c)
G4double theta() const
G4double mag() const
double G4double
Definition: G4Types.hh:76
void setZ(G4double az)
Set the z coordinate.
G4double getZ() const
G4double getY() const