ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4INCLThreeVector.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4INCLThreeVector.hh
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 // Alain Boudard, CEA-Saclay, France
28 // Joseph Cugnon, University of Liege, Belgium
29 // Jean-Christophe David, CEA-Saclay, France
30 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
31 // Sylvie Leray, CEA-Saclay, France
32 // Davide Mancusi, CEA-Saclay, France
33 //
34 #define INCLXX_IN_GEANT4_MODE 1
35 
36 #include "globals.hh"
37 
38 /*
39  * ThreeVector.hh
40  *
41  * \date 4 June 2009
42  * \author Pekka Kaitaniemi
43  */
44 
45 #ifndef G4INCLThreeVector_hh
46 #define G4INCLThreeVector_hh 1
47 
48 #include <string>
49 #include <sstream>
50 #include <cmath>
51 
52 namespace G4INCL {
53 
54  class ThreeVector {
55  public:
57  :x(0.0), y(0.0), z(0.0)
58  {}
59 
61  :x(ax), y(ay), z(az)
62  {}
63 
64  inline G4double getX() const { return x; }
65  inline G4double getY() const { return y; }
66  inline G4double getZ() const { return z; }
67 
68  inline G4double perp() const { return std::sqrt(x*x + y*y); }
69  inline G4double perp2() const { return x*x + y*y; }
73  inline G4double mag() const { return std::sqrt(x*x + y*y + z*z); }
74 
78  inline G4double mag2() const { return (x*x + y*y + z*z); }
79 
83  inline G4double theta() const {
84  return x == 0.0 && y == 0.0 && z == 0.0 ? 0.0 : std::atan2(perp(),z);
85  }
86 
90  inline G4double phi() const {
91  return x == 0.0 && y == 0.0 ? 0.0 : std::atan2(y,x);
92  }
93 
97  inline G4double dot(const ThreeVector &v) const {
98  return (x*v.x + y*v.y + z*v.z);
99  }
100 
104  ThreeVector vector(const ThreeVector &v) const {
105  return ThreeVector(
106  y*v.z - z*v.y,
107  z*v.x - x*v.z,
108  x*v.y - y*v.x
109  );
110  }
111 
113  inline void setX(G4double ax) { x = ax; }
114 
116  inline void setY(G4double ay) { y = ay; }
117 
119  inline void setZ(G4double az) { z = az; }
120 
122  inline void set(const G4double ax, const G4double ay, const G4double az) { x=ax; y=ay; z=az; }
123 
124  inline void operator+= (const ThreeVector &v) {
125  x += v.x;
126  y += v.y;
127  z += v.z;
128  }
129 
131  inline ThreeVector operator- () const {
132  return ThreeVector(-x,-y,-z);
133  }
134 
135  inline void operator-= (const ThreeVector &v) {
136  x -= v.x;
137  y -= v.y;
138  z -= v.z;
139  }
140 
141  template<typename T>
142  inline void operator*= (const T &c) {
143  x *= c;
144  y *= c;
145  z *= c;
146  }
147 
148  template<typename T>
149  inline void operator/= (const T &c) {
150  const G4double oneOverC = 1./c;
151  this->operator*=(oneOverC);
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  inline ThreeVector operator+ (const ThreeVector &v) const {
159  return ThreeVector(x+v.x, y+v.y, z+v.z);
160  }
161 
165  inline ThreeVector operator/ (const G4double C) const {
166  const G4double oneOverC = 1./C;
167  return ThreeVector(x*oneOverC, y*oneOverC, z*oneOverC);
168  }
169 
170  inline ThreeVector operator* (const G4double C) const {
171  return ThreeVector(x*C, y*C, z*C);
172  }
173 
179  inline void rotate(const G4double angle, const ThreeVector &axis) {
180  // Use Rodrigues' formula
181  const G4double cos = std::cos(angle);
182  const G4double sin = std::sin(angle);
183  (*this) = (*this) * cos + axis.vector(*this) * sin + axis * (axis.dot(*this)*(1.-cos));
184  }
185 
192  if(x<=y && x<=z)
193  return ThreeVector(0., -z, y);
194  else if(y<=x && y<=z)
195  return ThreeVector(-z, 0., x);
196  else
197  return ThreeVector(-y, x, 0.);
198  }
199 
200  std::string print() const {
201  std::stringstream ss;
202  ss <<"(x = " << x << " y = " << y << " z = " << z <<")";
203  return ss.str();
204  }
205 
206  std::string dump() const {
207  std::stringstream ss;
208  ss <<"(vector3 " << x << " " << y << " " << z << ")";
209  return ss.str();
210  }
211 
212  private:
213  G4double x, y, z; //> Vector components
214  };
215 
216 }
217 
218 #endif