DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
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
dd4hep::rec::Vector3D::fill
const Vector3D & fill(const T &v)
fill vector from arbitrary class that defines operator[]
Definition: Vector3D.h:82
dd4hep::rec::operator+
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:284
dd4hep::rec::operator*
Vector3D operator*(double s, const Vector3D &v)
Definition: Vector3D.h:303
dd4hep::rec::Vector3D::rho
double rho() const
Definition: Vector3D.h:148
dd4hep::rec::Vector3D::z
double z() const
Definition: Vector3D.h:109
dd4hep::rec::Vector3D::Vector3D
Vector3D()
Definition: Vector3D.h:37
dd4hep::rec::Vector3D::Vector3D
Vector3D(const float *v)
Definition: Vector3D.h:44
v
View * v
Definition: MultiView.cpp:28
dd4hep::rec::Vector3D::array
double * array()
direct access to data as double* - allows modification
Definition: Vector3D.h:216
dd4hep::rec::Vector3D
Definition: Vector3D.h:32
dd4hep::rec::Vector3D::x
double & x()
Definition: Vector3D.h:112
dd4hep::rec::Vector3D::_z
double _z
Definition: Vector3D.h:266
dd4hep::rec::Vector3D::to
T to() const
Definition: Vector3D.h:261
dd4hep::rec::operator-
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:289
dd4hep::rec::Vector3D::cylindrical
static Cylindrical cylindrical()
Definition: Vector3D.h:278
dd4hep::rec::Vector3D::x
double x() const
Definition: Vector3D.h:103
dd4hep::rec::Vector3D::cartesian
static Cartesian cartesian()
Definition: Vector3D.h:277
dd4hep::rec::Vector3D::operator[]
double & operator[](int i)
Definition: Vector3D.h:131
dd4hep::rec::Vector3D::Cartesian
Definition: Vector3D.h:273
dd4hep::rec::Vector3D::Vector3D
Vector3D(double x, double y, double z, T(&)())
dd4hep::rec::Vector3D::phi
double phi() const
Definition: Vector3D.h:142
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:41
dd4hep::rec::Vector3D::_y
double _y
Definition: Vector3D.h:266
dd4hep::DDSegmentation::Vector3D::Vector3D
Vector3D(double x_val=0., double y_val=0., double z_val=0.)
Default constructor.
Definition: Segmentation.h:50
dd4hep::rec::Vector3D::unit
Vector3D unit() const
Definition: Vector3D.h:199
dd4hep::rec::Vector3D::trans2
double trans2() const
Definition: Vector3D.h:160
dd4hep::rec::Vector3D::y
double & y()
Definition: Vector3D.h:115
dd4hep::rec::Vector3D::fill
const Vector3D & fill(const double *v)
fill vector from double array
Definition: Vector3D.h:89
dd4hep::rec::Vector3D::operator=
Vector3D & operator=(const Vector3D &v)
Definition: Vector3D.h:73
dd4hep::rec::Vector3D::Vector3D
Vector3D(const double *v)
Definition: Vector3D.h:47
dd4hep::rec::Vector3D::Cylindrical
Definition: Vector3D.h:274
dd4hep::rec::Vector3D::trans
double trans() const
Definition: Vector3D.h:154
dd4hep::rec::Vector3D::cross
Vector3D cross(const Vector3D &v) const
Definition: Vector3D.h:191
dd4hep::rec::Vector3D::const_array
const double * const_array() const
direct access to data as const double*
Definition: Vector3D.h:211
dd4hep::rec::Vector3D::z
double & z()
Definition: Vector3D.h:118
dd4hep::rec::Vector3D::operator[]
double operator[](int i) const
Definition: Vector3D.h:122
dd4hep::rec::Vector3D::r2
double r2() const
Definition: Vector3D.h:173
dd4hep::rec::Vector3D::y
double y() const
Definition: Vector3D.h:106
dd4hep::rec::Vector3D::spherical
static Spherical spherical()
Definition: Vector3D.h:279
dd4hep::rec::Vector3D::Spherical
Definition: Vector3D.h:275
dd4hep::rec::Vector3D::fill
const Vector3D & fill(double x_val, double y_val, double z_val)
fill from double values
Definition: Vector3D.h:96
dd4hep::rec::Vector3D::dot
double dot(const Vector3D &v) const
Definition: Vector3D.h:185
dd4hep::rec::operator==
bool operator==(const Vector3D &a, const Vector3D &b)
Definition: Vector3D.h:294
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::rec::Vector3D::r
double r() const
Definition: Vector3D.h:166
dd4hep::rec::Vector3D::Vector3D
Vector3D(const Vector3D &v)
Definition: Vector3D.h:41
dd4hep::rec::Vector3D::isEqual
bool isEqual(const Vector3D &b, double epsilon=1e-6)
Definition: Vector3D.h:222
dd4hep::rec::Vector3D::_x
double _x
Definition: Vector3D.h:266
dd4hep::DDSegmentation::operator<<
std::ostream & operator<<(std::ostream &os, const BitField64 &b)
Definition: BitField64.cpp:37
dd4hep::rec::Vector3D::Vector3D
Vector3D(double x_val, double y_val, double z_val)
Definition: Vector3D.h:56
dd4hep::rec::Vector3D::theta
double theta() const
Definition: Vector3D.h:179