DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector3D.h
Go to the documentation of this file.
1 #ifndef DDSurfaces_Vector3D_h
2 #define DDSurfaces_Vector3D_h 1
3 
4 #include <cmath>
5 #include <iostream>
6 #include <cassert>
7 
8 
9 namespace DDSurfaces {
10 
20  class Vector3D{
21 
22  public:
23 
25  Vector3D() : _x(0.0),_y(0.0),_z(0.0) {}
26 
27 
29  Vector3D(const Vector3D& v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
30 
32  Vector3D(const float* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
33 
35  Vector3D(const double* v) : _x(v[0]),_y(v[1]),_z(v[2]) {}
36 
37 
39  template <class T>
40  Vector3D( double x,double y, double z , T(&)() ) ;
41 
42 
44  Vector3D( double x_val,double y_val, double z_val ) :
45  _x(x_val),
46  _y(y_val),
47  _z(z_val) {
48  }
49 
50  // ---- this causes all sorts of template lookup errors ...
51  // /** Copy c'tor for three vectors from other packages - requires T::x(),T::y(), T::z().
52  // */
53  // template <class T>
54  // Vector3D( const T& t) :
55  // _x( t.x() ) ,
56  // _y( t.y() ) ,
57  // _z( t.z() ){
58  // }
59 
60  //assignment operator
62  _x = v[0] ;
63  _y = v[1] ;
64  _z = v[2] ;
65  return *this ;
66  }
67 
69  template <class T>
70  inline const Vector3D& fill( const T& v ) {
71 
72  _x = v[0] ; _y = v[1] ; _z = v[2] ;
73  return *this ;
74  }
75 
77  inline const Vector3D& fill( const double* v) {
78 
79  _x = v[0] ; _y = v[1] ; _z = v[2] ;
80  return *this ;
81  }
82 
84  inline const Vector3D& fill( double x_val, double y_val, double z_val) {
85  _x = x_val ; _y = y_val ; _z = z_val ;
86  return *this ;
87  }
88 
89 
91  inline double x() const { return _x ; }
92 
94  inline double y() const { return _y ; }
95 
97  inline double z() const { return _z ; }
98 
100  inline double& x() { return _x ; }
101 
103  inline double& y() { return _y ; }
104 
106  inline double& z() { return _z ; }
107 
108 
110  inline double operator[](int i) const {
111  switch(i) {
112  case 0: return _x ; break ;
113  case 1: return _y ; break ;
114  case 2: return _z ; break ;
115  }
116  return 0.0 ;
117  }
119  inline double& operator[](int i) {
120  switch(i) {
121  case 0: return _x ; break ;
122  case 1: return _y ; break ;
123  case 2: return _z ; break ;
124  }
125  static double dummy(0.0) ;
126  return dummy ;
127  }
128 
130  inline double phi() const {
131 
132  return _x == 0.0 && _y == 0.0 ? 0.0 : atan2(_y,_x);
133  }
134 
136  inline double rho() const {
137 
138  return trans() ;
139  }
140 
142  inline double trans() const {
143 
144  return sqrt( _x*_x + _y*_y ) ;
145  }
146 
148  inline double trans2() const {
149 
150  return _x*_x + _y*_y ;
151  }
152 
154  inline double r() const {
155 
156  return sqrt( _x*_x + _y*_y + _z*_z ) ;
157  }
158 
159 
161  inline double r2() const {
162 
163  return _x*_x + _y*_y + _z*_z ;
164  }
165 
167  inline double theta() const {
168 
169  return _x == 0.0 && _y == 0.0 && _z == 0.0 ? 0.0 : atan2( rho(),_z) ;
170  }
171 
173  inline double dot( const Vector3D& v) const {
174  return _x * v.x() + _y * v.y() + _z * v.z() ;
175  }
176 
177 
179  inline Vector3D cross( const Vector3D& v) const {
180 
181  return Vector3D( _y * v.z() - _z * v.y() ,
182  _z * v.x() - _x * v.z() ,
183  _x * v.y() - _y * v.x() ) ;
184  }
185 
187  inline Vector3D unit() const {
188 
189  double n = r() ;
190  return Vector3D( _x / n , _y / n , _z / n ) ;
191  }
192 
193 
195  inline operator const double*() const {
196  return &_x ;
197  }
199  inline const double* const_array() const {
200  return &_x ;
201  }
202 
204  inline double* array() {
205  return &_x ;
206  }
207 
208 
210  inline bool isEqual( const Vector3D& b , double epsilon=1e-6) {
211 
212  if( fabs( x() - b.x() ) < epsilon &&
213  fabs( y() - b.y() ) < epsilon &&
214  fabs( z() - b.z() ) < epsilon )
215  return true;
216  else
217  return false;
218  }
219 
220 
221 
222  // this causes template lookup errors on some machines :
223  // -> use explicit conversion with to<T>()
224  // /** Implicit templated conversion to anything that has a c'tor T(x,y,z)
225  // * and accessor functions x(),y(),z(). For safety the result is checked which
226  // * causes a small performance penalty.
227  // * @see to()
228  // *
229  // */
230  // template <class T>
231  // inline operator T() const {
232 
233  // T t( _x, _y , _z ) ;
234 
235  // assert( t.x()== _x && t.y()== _y && t.z()== _z ) ;
236 
237  // return t ;
238 
239  // // return T( _x, _y, _z ) ;
240  // }
241 
242 
248  template <class T>
249  inline T to() const { return T( _x, _y, _z ) ; }
250 
251 
252  protected:
253 
254  double _x,_y,_z ;
255 
256 
257  // helper classes and function to allow
258  // different c'tors selected at compile time
259  public:
260 
261  struct Cartesian { } ;
262  struct Cylindrical { } ;
263  struct Spherical { } ;
264 
265  static Cartesian cartesian() { return Cartesian() ; }
266  static Cylindrical cylindrical(){ return Cylindrical() ;}
267  static Spherical spherical() { return Spherical() ; }
268 
269  } ;
270 
272  inline Vector3D operator+( const Vector3D& a, const Vector3D& b ) {
273 
274  return Vector3D( a.x() + b.x() , a.y() + b.y(), a.z() + b.z() ) ;
275  }
277  inline Vector3D operator-( const Vector3D& a, const Vector3D& b ) {
278 
279  return Vector3D( a.x() - b.x() , a.y() - b.y(), a.z() - b.z() ) ;
280  }
282  inline bool operator==( const Vector3D& a, const Vector3D& b ) {
283 
284  if( a.x() == b.x() && a.y() == b.y() && a.z() == b.z() )
285  return true;
286  else
287  return false;
288  }
289 
291  inline Vector3D operator*( double s , const Vector3D& v ) {
292 
293  return Vector3D( s * v.x() , s * v.y() , s * v.z() ) ;
294  }
295 
297  inline Vector3D operator-( const Vector3D& v) {
298 
299  return Vector3D( -v.x(), - v.y(), - v.z() ) ;
300  }
301 
303  inline double operator*( const Vector3D& v0, const Vector3D& v1 ){
304  return v0.dot( v1 ) ;
305  }
306 
307 
308  // template specializations for constructors of different coordinate systems
309 
313  template <>
314  inline Vector3D::Vector3D( double x_val,double y_val, double z_val, Vector3D::Cartesian (&)() ) :
315  _x(x_val),
316  _y(y_val),
317  _z(z_val) {
318  }
319 
323  template <>
324  inline Vector3D::Vector3D( double rho_val,double phi_val, double z_val, Vector3D::Cylindrical (&)() ) : _z(z_val) {
325 
326  _x = rho_val * cos( phi_val ) ;
327  _y = rho_val * sin( phi_val ) ;
328  }
329 
330 
334  template <>
335  inline Vector3D::Vector3D( double r_val,double phi_val, double theta_val, Vector3D::Spherical (&)() ) {
336  double rst = r_val * sin( theta_val ) ;
337  _x = rst * cos( phi_val ) ;
338  _y = rst * sin( phi_val ) ;
339  _z = r_val * cos( theta_val ) ;
340  }
341 
342 
343 
345  inline std::ostream & operator << (std::ostream & os, const Vector3D &v) {
346 
347  // os << "( " << v[0] << ", " << v[1] << ", " << v[2] << " )" ;
348  os << " ( " << v[0]
349  << ", " << v[1]
350  << ", " << v[2]
351  << " ) - [ phi: " << v.phi()
352  << " , rho: " << v.rho() << " ] "
353  << " [ theta: " << v.theta()
354  << " , r: " << v.r() << " ] " ;
355 
356  return os ;
357  }
358 
359 
360 
361 } // namespace
362 
363 #endif
std::ostream & operator<<(std::ostream &os, const BitField64 &b)
Definition: BitField64.cpp:271
Vector3D(double x_val, double y_val, double z_val)
Definition: Vector3D.h:44
const Vector3D & fill(const T &v)
fill vector from arbitrary class that defines operator[]
Definition: Vector3D.h:70
static Cylindrical cylindrical()
Definition: Vector3D.h:266
double & operator[](int i)
Definition: Vector3D.h:119
double x() const
Definition: Vector3D.h:91
static Spherical spherical()
Definition: Vector3D.h:267
const Vector3D & fill(double x_val, double y_val, double z_val)
fill from double values
Definition: Vector3D.h:84
Vector3D(const double *v)
Definition: Vector3D.h:35
double phi() const
Definition: Vector3D.h:130
TGeoShape * s
Definition: Volumes.cpp:294
return e
Definition: Volumes.cpp:297
double r2() const
Definition: Vector3D.h:161
double dot(const Vector3D &v) const
Definition: Vector3D.h:173
double theta() const
Definition: Vector3D.h:167
double trans2() const
Definition: Vector3D.h:148
double z() const
Definition: Vector3D.h:97
Vector3D(const float *v)
Definition: Vector3D.h:32
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:277
double y() const
Definition: Vector3D.h:94
double trans() const
Definition: Vector3D.h:142
Vector3D(double x_val=0., double y_val=0., double z_val=0.)
Default constructor.
Definition: Segmentation.h:43
Vector3D unit() const
Definition: Vector3D.h:187
double operator[](int i) const
Definition: Vector3D.h:110
View * v
Definition: MultiView.cpp:30
Vector3D & operator=(const Vector3D &v)
Definition: Vector3D.h:61
Vector3D cross(const Vector3D &v) const
Definition: Vector3D.h:179
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:272
Vector3D operator*(double s, const Vector3D &v)
Definition: Vector3D.h:291
const double * const_array() const
direct access to data as const double*
Definition: Vector3D.h:199
double * array()
direct access to data as double* - allows modification
Definition: Vector3D.h:204
Vector3D(const Vector3D &v)
Definition: Vector3D.h:29
double rho() const
Definition: Vector3D.h:136
const Vector3D & fill(const double *v)
fill vector from double array
Definition: Vector3D.h:77
bool isEqual(const Vector3D &b, double epsilon=1e-6)
Definition: Vector3D.h:210
double r() const
Definition: Vector3D.h:154
static Cartesian cartesian()
Definition: Vector3D.h:265
bool operator==(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:282