DD4hep  01.18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Vector3D.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : F.Gaede
11 //
12 //==========================================================================
13 #ifndef DDREC_VECTOR3D_H
14 #define DDREC_VECTOR3D_H 1
15 
16 #include <cmath>
17 #include <iostream>
18 #include <cassert>
19 
20 
21 namespace dd4hep{ namespace rec {
22 
32  class Vector3D{
33 
34  public:
35 
37  Vector3D() : _x(0.0),_y(0.0),_z(0.0) {}
38 
39 
41  Vector3D(const Vector3D& v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
42 
44  Vector3D(const float* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
45 
47  Vector3D(const double* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
48 
49 
51  template <class T>
52  Vector3D( double x,double y, double z , T(&)() ) ;
53 
54 
56  Vector3D( double x_val,double y_val, double z_val ) :
57  _x(x_val),
58  _y(y_val),
59  _z(z_val) {
60  }
61 
62  // ---- this causes all sorts of template lookup errors ...
63  // /** Copy c'tor for three vectors from other packages - requires T::x(),T::y(), T::z().
64  // */
65  // template <class T>
66  // Vector3D( const T& t) :
67  // _x( t.x() ) ,
68  // _y( t.y() ) ,
69  // _z( t.z() ){
70  // }
71 
72  //assignment operator
74  _x = v[0] ;
75  _y = v[1] ;
76  _z = v[2] ;
77  return *this ;
78  }
79 
81  template <class T>
82  inline const Vector3D& fill( const T& v ) {
83 
84  _x = v[0] ; _y = v[1] ; _z = v[2] ;
85  return *this ;
86  }
87 
89  inline const Vector3D& fill( const double* v) {
90 
91  _x = v[0] ; _y = v[1] ; _z = v[2] ;
92  return *this ;
93  }
94 
96  inline const Vector3D& fill( double x_val, double y_val, double z_val) {
97  _x = x_val ; _y = y_val ; _z = z_val ;
98  return *this ;
99  }
100 
101 
103  inline double x() const { return _x ; }
104 
106  inline double y() const { return _y ; }
107 
109  inline double z() const { return _z ; }
110 
112  inline double& x() { return _x ; }
113 
115  inline double& y() { return _y ; }
116 
118  inline double& z() { return _z ; }
119 
120 
122  inline double operator[](int i) const {
123  switch(i) {
124  case 0: return _x ; break ;
125  case 1: return _y ; break ;
126  case 2: return _z ; break ;
127  }
128  return 0.0 ;
129  }
131  inline double& operator[](int i) {
132  switch(i) {
133  case 0: return _x ; break ;
134  case 1: return _y ; break ;
135  case 2: return _z ; break ;
136  }
137  static double dummy(0.0) ;
138  return dummy ;
139  }
140 
142  inline double phi() const {
143 
144  return _x == 0.0 && _y == 0.0 ? 0.0 : atan2(_y,_x);
145  }
146 
148  inline double rho() const {
149 
150  return trans() ;
151  }
152 
154  inline double trans() const {
155 
156  return sqrt( _x*_x + _y*_y ) ;
157  }
158 
160  inline double trans2() const {
161 
162  return _x*_x + _y*_y ;
163  }
164 
166  inline double r() const {
167 
168  return sqrt( _x*_x + _y*_y + _z*_z ) ;
169  }
170 
171 
173  inline double r2() const {
174 
175  return _x*_x + _y*_y + _z*_z ;
176  }
177 
179  inline double theta() const {
180 
181  return _x == 0.0 && _y == 0.0 && _z == 0.0 ? 0.0 : atan2( rho(),_z) ;
182  }
183 
185  inline double dot( const Vector3D& v) const {
186  return _x * v.x() + _y * v.y() + _z * v.z() ;
187  }
188 
189 
191  inline Vector3D cross( const Vector3D& v) const {
192 
193  return Vector3D( _y * v.z() - _z * v.y() ,
194  _z * v.x() - _x * v.z() ,
195  _x * v.y() - _y * v.x() ) ;
196  }
197 
199  inline Vector3D unit() const {
200 
201  double n = r() ;
202  return Vector3D( _x / n , _y / n , _z / n ) ;
203  }
204 
205 
207  inline operator const double*() const {
208  return &_x ;
209  }
211  inline const double* const_array() const {
212  return &_x ;
213  }
214 
216  inline double* array() {
217  return &_x ;
218  }
219 
220 
222  inline bool isEqual( const Vector3D& b , double epsilon=1e-6) {
223 
224  if( fabs( x() - b.x() ) < epsilon &&
225  fabs( y() - b.y() ) < epsilon &&
226  fabs( z() - b.z() ) < epsilon )
227  return true;
228  else
229  return false;
230  }
231 
232 
233 
234  // this causes template lookup errors on some machines :
235  // -> use explicit conversion with to<T>()
236  // /** Implicit templated conversion to anything that has a c'tor T(x,y,z)
237  // * and accessor functions x(),y(),z(). For safety the result is checked which
238  // * causes a small performance penalty.
239  // * @see to()
240  // *
241  // */
242  // template <class T>
243  // inline operator T() const {
244 
245  // T t( _x, _y , _z ) ;
246 
247  // assert( t.x()== _x && t.y()== _y && t.z()== _z ) ;
248 
249  // return t ;
250 
251  // // return T( _x, _y, _z ) ;
252  // }
253 
254 
260  template <class T>
261  inline T to() const { return T( _x, _y, _z ) ; }
262 
263 
264  protected:
265 
266  double _x,_y,_z ;
267 
268 
269  // helper classes and function to allow
270  // different c'tors selected at compile time
271  public:
272 
273  struct Cartesian { } ;
274  struct Cylindrical { } ;
275  struct Spherical { } ;
276 
277  static Cartesian cartesian() { return Cartesian() ; }
278  static Cylindrical cylindrical(){ return Cylindrical() ;}
279  static Spherical spherical() { return Spherical() ; }
280 
281  } ;
282 
284  inline Vector3D operator+( const Vector3D& a, const Vector3D& b ) {
285 
286  return Vector3D( a.x() + b.x() , a.y() + b.y(), a.z() + b.z() ) ;
287  }
289  inline Vector3D operator-( const Vector3D& a, const Vector3D& b ) {
290 
291  return Vector3D( a.x() - b.x() , a.y() - b.y(), a.z() - b.z() ) ;
292  }
294  inline bool operator==( const Vector3D& a, const Vector3D& b ) {
295 
296  if( a.x() == b.x() && a.y() == b.y() && a.z() == b.z() )
297  return true;
298  else
299  return false;
300  }
301 
303  inline Vector3D operator*( double s , const Vector3D& v ) {
304 
305  return Vector3D( s * v.x() , s * v.y() , s * v.z() ) ;
306  }
307 
309  inline Vector3D operator-( const Vector3D& v) {
310 
311  return Vector3D( -v.x(), - v.y(), - v.z() ) ;
312  }
313 
315  inline double operator*( const Vector3D& v0, const Vector3D& v1 ){
316  return v0.dot( v1 ) ;
317  }
318 
319 
320  // template specializations for constructors of different coordinate systems
321 
325  template <>
326  inline Vector3D::Vector3D( double x_val,double y_val, double z_val, Vector3D::Cartesian (&)() ) :
327  _x(x_val),
328  _y(y_val),
329  _z(z_val) {
330  }
331 
335  template <>
336  inline Vector3D::Vector3D( double rho_val,double phi_val, double z_val, Vector3D::Cylindrical (&)() ) : _z(z_val) {
337 
338  _x = rho_val * cos( phi_val ) ;
339  _y = rho_val * sin( phi_val ) ;
340  }
341 
342 
346  template <>
347  inline Vector3D::Vector3D( double r_val,double phi_val, double theta_val, Vector3D::Spherical (&)() ) {
348  double rst = r_val * sin( theta_val ) ;
349  _x = rst * cos( phi_val ) ;
350  _y = rst * sin( phi_val ) ;
351  _z = r_val * cos( theta_val ) ;
352  }
353 
354 
355 
357  inline std::ostream & operator << (std::ostream & os, const Vector3D &v) {
358 
359  // os << "( " << v[0] << ", " << v[1] << ", " << v[2] << " )" ;
360  os << " ( " << v[0]
361  << ", " << v[1]
362  << ", " << v[2]
363  << " ) - [ phi: " << v.phi()
364  << " , rho: " << v.rho() << " ] "
365  << " [ theta: " << v.theta()
366  << " , r: " << v.r() << " ] " ;
367 
368  return os ;
369  }
370 
371 
372 
373 }} // namespace
374 
375 
376 
377 
378 
379 
380 #endif
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Addition of two vectors.
Definition: Vector3D.h:284
double r() const
Spherical r/magnitude.
Definition: Vector3D.h:166
double theta() const
Polar angle - spherical.
Definition: Vector3D.h:179
static Cartesian cartesian()
Definition: Vector3D.h:277
Vector3D(const double *v)
Constructor for double array.
Definition: Vector3D.h:47
double trans2() const
Transversal component squared.
Definition: Vector3D.h:160
double dot(const Vector3D &v) const
Scalar product.
Definition: Vector3D.h:185
double rho() const
Transversal component - cylindrical &#39;r&#39;.
Definition: Vector3D.h:148
Vector3D(const Vector3D &v)
Copy constructor.
Definition: Vector3D.h:41
Vector3D operator*(double s, const Vector3D &v)
Multiplication with scalar.
Definition: Vector3D.h:303
Vector3D cross(const Vector3D &v) const
Vector product.
Definition: Vector3D.h:191
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Subtraction of two vectors.
Definition: Vector3D.h:289
double & operator[](int i)
Accessing x,y,z with bracket operator for assignment.
Definition: Vector3D.h:131
double r2() const
Spherical r/magnitude, squared.
Definition: Vector3D.h:173
std::ostream & operator<<(std::ostream &io, const FixedPadSizeTPCData &d)
double * array()
direct access to data as double* - allows modification
Definition: Vector3D.h:216
static Spherical spherical()
Definition: Vector3D.h:279
double y() const
Cartesian y coordinate.
Definition: Vector3D.h:106
Simple three dimensional vector providing the components for cartesian, cylindrical and spherical coo...
Definition: Vector3D.h:32
Vector3D(const float *v)
Constructor for float array.
Definition: Vector3D.h:44
const Vector3D & fill(const T &v)
fill vector from arbitrary class that defines operator[]
Definition: Vector3D.h:82
Vector3D(double x_val, double y_val, double z_val)
Default corrdinate system for initialization is cartesian.
Definition: Vector3D.h:56
const Vector3D & fill(double x_val, double y_val, double z_val)
fill from double values
Definition: Vector3D.h:96
double phi() const
Azimuthal angle - cylindrical and spherical.
Definition: Vector3D.h:142
const double * const_array() const
direct access to data as const double*
Definition: Vector3D.h:211
T to() const
Implicit templated conversion to anything that has a c&#39;tor T(x,y,z) and accessor functions x()...
Definition: Vector3D.h:261
static Cylindrical cylindrical()
Definition: Vector3D.h:278
double z() const
Cartesian cartesian z coordinate.
Definition: Vector3D.h:109
Vector3D unit() const
Parallel unit vector.
Definition: Vector3D.h:199
double trans() const
Transversal component.
Definition: Vector3D.h:154
bool operator==(const Vector3D &a, const Vector3D &b)
Exact comparison of two vectors.
Definition: Vector3D.h:294
double x() const
Cartesian x coordinate.
Definition: Vector3D.h:103
double & y()
Assign to cartesian y coordinate.
Definition: Vector3D.h:115
bool isEqual(const Vector3D &b, double epsilon=1e-6)
Component wise comparison of two vectors - true if all components differ less than epsilon...
Definition: Vector3D.h:222
const Vector3D & fill(const double *v)
fill vector from double array
Definition: Vector3D.h:89
double & x()
Assign to cartesian x coordinate.
Definition: Vector3D.h:112
double operator[](int i) const
Accessing x,y,z with bracket operator.
Definition: Vector3D.h:122
STL class.
Vector3D()
Default c&#39;tor - zero vector.
Definition: Vector3D.h:37
Vector3D & operator=(const Vector3D &v)
Copy c&#39;tor for three vectors from other packages - requires T::x(),T::y(), T::z().
Definition: Vector3D.h:73
double & z()
Assign to cartesian z coordinate.
Definition: Vector3D.h:118