GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
Vector3D.h
1 #ifndef Vector3D_h
2 #define Vector3D_h 1
3 
4 #include <math.h>
5 #include <iostream>
6 #include "assert.h"
7 
8 
9 namespace gear {
10 
18 class Vector3D{
19 
20 public:
21 
23  Vector3D() : _x(0.0),_y(0.0),_z(0.0) {}
24 
25 
27  Vector3D(const float* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
28 
30  Vector3D(const double* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
31 
32 
34  template <class T>
35  Vector3D( double x,double y, double z , T(&)() ) ;
36 
37 
39  Vector3D( double xx,double yy, double zz ) :
40  _x(xx),
41  _y(yy),
42  _z(zz) {
43  }
44 
45 
46  template <class T>
49  Vector3D( const T& t) :
50  _x( t.x() ) ,
51  _y( t.y() ) ,
52  _z( t.z() ){
53  }
54 
56  inline double x() const { return _x ; }
57 
59  inline double y() const { return _y ; }
60 
62  inline double z() { return _z ; }
63 
65  inline double x() { return _x ; }
66 
68  inline double y() { return _y ; }
69 
71  inline double z() const { return _z ; }
72 
73 
75  inline double operator[](int i) const {
76  switch(i) {
77  case 0: return _x ; break ;
78  case 1: return _y ; break ;
79  case 2: return _z ; break ;
80  }
81  return 0.0 ;
82  }
84  inline double& operator[](int i) {
85  switch(i) {
86  case 0: return _x ; break ;
87  case 1: return _y ; break ;
88  case 2: return _z ; break ;
89  }
90  static double dummy(0.0) ;
91  return dummy ;
92  }
93 
95  inline double phi() const {
96 
97  return _x == 0.0 && _y == 0.0 ? 0.0 : atan2(_y,_x);
98  }
99 
101  inline double rho() const {
102 
103  return trans() ;
104  }
105 
107  inline double trans() const {
108 
109  return sqrt( _x*_x + _y*_y ) ;
110  }
111 
113  inline double trans2() const {
114 
115  return _x*_x + _y*_y ;
116  }
117 
119  inline double r() const {
120 
121  return sqrt( _x*_x + _y*_y + _z*_z ) ;
122  }
123 
124 
126  inline double r2() const {
127 
128  return _x*_x + _y*_y + _z*_z ;
129  }
130 
132  inline double theta() const {
133 
134  return _x == 0.0 && _y == 0.0 && _z == 0.0 ? 0.0 : atan2( rho(),_z) ;
135  }
136 
138  inline double dot( const Vector3D& v) {
139  return _x * v.x() + _y * v.y() + _z * v.z() ;
140  }
141 
143  inline Vector3D cross( const Vector3D& v) {
144 
145  return Vector3D( _y * v.z() - _z * v.y() ,
146  _z * v.x() - _x * v.z() ,
147  _x * v.y() - _y * v.x() ) ;
148  }
149 
151  inline Vector3D unit() {
152 
153  double n = r() ;
154  return Vector3D( _x / n , _y / n , _z / n ) ;
155  }
156 
157 
164  template <class T>
165  inline operator T() {
166 
167  T t( _x, _y , _z ) ;
168 
169  assert( t.x()== _x && t.y()== _y && t.z()== _z ) ;
170 
171  return t ;
172 
173 // return T( _x, _y, _z ) ;
174  }
175 
176 
182  template <class T>
183  inline T to() { return T( _x, _y, _z ) ; }
184 
185 
186 protected:
187 
188  double _x=0,_y=0,_z=0 ;
189 
190 
191  // helper classes and function to allow
192  // different c'tors selected at compile time
193 public:
194 
195  struct Cartesian { } ;
196  struct Cylindrical { } ;
197  struct Spherical { } ;
198 
199  static Cartesian cartesian() { return Cartesian() ; }
200  static Cylindrical cylindrical(){ return Cylindrical() ;}
201  static Spherical spherical() { return Spherical() ; }
202 
203 } ;
204 
206 inline Vector3D operator+( const Vector3D& a, const Vector3D& b ) {
207 
208  return Vector3D( a.x() + b.x() , a.y() + b.y(), a.z() + b.z() ) ;
209 }
211 inline Vector3D operator-( const Vector3D& a, const Vector3D& b ) {
212 
213  return Vector3D( a.x() - b.x() , a.y() - b.y(), a.z() - b.z() ) ;
214 }
216 inline bool operator==( const Vector3D& a, const Vector3D& b ) {
217 
218  if( a.x() == b.x() && a.y() == b.y() && a.z() == b.z() )
219  return true;
220  else
221  return false;
222 }
223 
225 inline Vector3D operator*( double s , const Vector3D& v ) {
226 
227  return Vector3D( s * v.x() , s * v.y() , s * v.z() ) ;
228 }
229 
230 
231 // template specializations for constructors of different coordinate systems
232 
236 template <>
237 inline Vector3D::Vector3D( double xx,double yy, double zz, Vector3D::Cartesian (&)() ) :
238  _x(xx),
239  _y(yy),
240  _z(zz) {
241 }
242 
246 template <>
247 inline Vector3D::Vector3D( double arho,double aphi, double zz, Vector3D::Cylindrical (&)() ) :
248  _x( arho * cos( aphi ) ),
249  _y( arho * sin( aphi ) ),
250  _z(zz) {
251 
252 }
253 
254 
258 template <>
259 inline Vector3D::Vector3D( double rr,double aphi, double atheta, Vector3D::Spherical (&)() ) {
260  double rst = rr * sin( atheta ) ;
261  _x = rst * cos( aphi ) ;
262  _y = rst * sin( aphi ) ;
263  _z = rr * cos( atheta ) ;
264 }
265 
266 
267 
269  std::ostream & operator << (std::ostream & os, const Vector3D &v) ;
270 
271 
272 
273 // --- optionally one could have additional vectors with internal representations
274 // --- in other coordinates, e.g. cylindrical :
275 
276 // /** Native cylindrical Vector 3D - internal data is rho, phi, z
277 // */
278 // class Vector3DCylindrical {
279 
280 
281 // public:
282 
283 // /** Conversion c'tor */
284 // Vector3DCylindrical( const Vector3D& v ) :
285 // _rho( v.rho() ),
286 // _phi( v.phi() ),
287 // _z( v.z() ) {
288 // }
289 
290 // template <class T>
291 // Vector3DCylindrical( double rho, double phi, double z , T(&)() ) ;
292 
293 // Vector3DCylindrical( double rho, double phi, double z ) :
294 // _rho(rho),_phi(phi),_z(z){
295 // }
296 
297 
298 // inline double x() const { return _rho * cos( _phi ) ; }
299 
300 // inline double y() const { return _rho * sin( _phi ) ; }
301 
302 // inline double z() const { return _z ; }
303 
304 // inline double phi() const { return _phi ; }
305 
306 // inline double rho() const { return _rho ; }
307 
308 // inline double r() const {
309 
310 // return sqrt( _rho*_rho + _z*_z ) ;
311 // }
312 
313 // inline double theta() const {
314 
315 // return _rho == 0.0 && _z == 0.0 ? 0.0 : std::atan2( _rho ,_z);
316 // }
317 
318 
319 // /** Conversion operator */
320 // operator Vector3D() { return Vector3D( x() , y() , z() ) ; }
321 
322 // protected:
323 
324 // double _rho,_phi,_z ;
325 
326 // } ;
327 
328 // // template specializations for constructors of different coordinate systems
329 // template <>
330 // Vector3DCylindrical::Vector3DCylindrical( double x,double y, double z,
331 // Vector3D::Cartesian (&)() ) : _z(z) {
332 
333 // _rho = sqrt( x*x + y*y ) ;
334 
335 // _phi = ( x == 0.0 && y == 0.0 ? 0.0 : std::atan2(y,x) ) ;
336 
337 // }
338 
339 // template <>
340 // Vector3DCylindrical::Vector3DCylindrical( double rho,double phi, double z,
341 // Vector3D::Cylindrical (&)() ) :
342 // _rho(rho),_phi(phi),_z(z){
343 // }
344 
345 
346 // template <>
347 // Vector3DCylindrical::Vector3DCylindrical( double r,double phi, double theta,
348 // Vector3D::Spherical (&)() ) : _phi( phi) {
349 
350 // _rho = r * sin( theta ) ;
351 // _z = r * cos( theta ) ;
352 // }
353 
354 
355 } // namespace
356 #endif
double & operator[](int i)
Accessing x,y,z with bracket operator for assignment.
Definition: Vector3D.h:84
Vector3D(double xx, double yy, double zz)
Default corrdinate system for initialization is cartesian.
Definition: Vector3D.h:39
Vector3D(const double *v)
Constructor for double array.
Definition: Vector3D.h:30
Vector3D unit()
Parallel unit vector.
Definition: Vector3D.h:151
double operator[](int i) const
Accessing x,y,z with bracket operator.
Definition: Vector3D.h:75
double theta() const
Polar angle - spherical.
Definition: Vector3D.h:132
double y()
Assign to cartesian y coordinate.
Definition: Vector3D.h:68
bool operator==(const Vector3D &a, const Vector3D &b)
Comparison of two vectors.
Definition: Vector3D.h:216
Simple three dimensional vector providing the components for cartesian, cylindrical and spherical coo...
Definition: Vector3D.h:18
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Subtraction of two vectors.
Definition: Vector3D.h:211
double r2() const
Spherical r/magnitude, squared.
Definition: Vector3D.h:126
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Addition of two vectors.
Definition: Vector3D.h:206
T to()
Explicit, unchecked conversion to anything that has a c&#39;tor T(x,y,z).
Definition: Vector3D.h:183
Vector3D operator*(double s, const Vector3D &v)
Multiplication with scalar.
Definition: Vector3D.h:225
double x() const
Cartesian x coordinate.
Definition: Vector3D.h:56
double y() const
Cartesian y coordinate.
Definition: Vector3D.h:59
Vector3D(const float *v)
Constructor for float array.
Definition: Vector3D.h:27
double z()
Cartesian cartesian z coordinate.
Definition: Vector3D.h:62
Vector3D(const T &t)
Copy c&#39;tor for three vectors from other packages - requires T::x(),T::y(), T::z().
Definition: Vector3D.h:49
double rho() const
Transversal component - cylindrical &#39;r&#39;.
Definition: Vector3D.h:101
Vector3D cross(const Vector3D &v)
Vector product.
Definition: Vector3D.h:143
double trans2() const
Transversal component squared.
Definition: Vector3D.h:113
double phi() const
Azimuthal angle - cylindrical and spherical.
Definition: Vector3D.h:95
double dot(const Vector3D &v)
Scalar product.
Definition: Vector3D.h:138
double r() const
Spherical r/magnitude.
Definition: Vector3D.h:119
double x()
Assign to cartesian x coordinate.
Definition: Vector3D.h:65
Vector3D()
Default c&#39;tor - zero vector.
Definition: Vector3D.h:23
double z() const
Assign to cartesian z coordinate.
Definition: Vector3D.h:71
double trans() const
Transversal component.
Definition: Vector3D.h:107