ROOT  6.06/09
Reference Guide
PositionVector2D.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class PositionVector2D
12 //
13 // Created by: Lorenzo Moneta at Mon Apr 16 2007
14 //
15 //
16 #ifndef ROOT_Math_GenVector_PositionVector2D
17 #define ROOT_Math_GenVector_PositionVector2D 1
18 
19 #ifndef ROOT_Math_GenVector_DisplacementVector2Dfwd
21 #endif
22 
23 #ifndef ROOT_Math_GenVector_GenVectorIO
25 #endif
26 
27 #ifndef ROOT_Math_GenVector_BitReproducible
29 #endif
30 
31 #ifndef ROOT_Math_GenVector_CoordinateSystemTags
33 #endif
34 
35 
36 namespace ROOT {
37 
38  namespace Math {
39 
40 
41 //__________________________________________________________________________________________
42  /**
43  Class describing a generic position vector (point) in 2 dimensions.
44  This class is templated on the type of Coordinate system.
45  One example is the XYPoint which is a vector based on
46  double precision x,y data members by using the
47  ROOT::Math::Cartesian2D<double> Coordinate system.
48  The class is having also an extra template parameter, the coordinate system tag,
49  to be able to identify (tag) vector described in different reference coordinate system,
50  like global or local coordinate systems.
51 
52  @ingroup GenVector
53  */
54 
55  template <class CoordSystem, class Tag = DefaultCoordinateSystemTag >
57 
58  public:
59 
60  typedef typename CoordSystem::Scalar Scalar;
61  typedef CoordSystem CoordinateType;
62  typedef Tag CoordinateSystemTag;
63 
64  // ------ ctors ------
65 
66  /**
67  Default constructor. Construct an empty object with zero values
68  */
69 
71 
72  /**
73  Construct from three values of type <em>Scalar</em>.
74  In the case of a XYPoint the values are x,y
75  In the case of a polar vector they are r,phi
76  */
77  PositionVector2D(const Scalar & a, const Scalar & b) :
78  fCoordinates ( a , b) { }
79 
80  /**
81  Construct from a position vector expressed in different
82  coordinates, or using a different Scalar type
83  */
84  template <class T>
86  fCoordinates ( v.Coordinates() ) { }
87 
88  /**
89  Construct from an arbitrary displacement vector
90  */
91  template <class T>
93  fCoordinates ( p.Coordinates() ) { }
94 
95  /**
96  Construct from a foreign 2D vector type, for example, Hep2Vector
97  Precondition: v must implement methods x() and y()
98  */
99  template <class ForeignVector>
100  explicit PositionVector2D( const ForeignVector & v) :
101  fCoordinates ( Cartesian2D<Scalar>( v.x(), v.y() ) ) { }
102 
103  // compiler-generated copy ctor and dtor are fine.
104 
105  // ------ assignment ------
106 
107  /**
108  Assignment operator from a position vector of arbitrary type
109  */
110  template <class OtherCoords>
111  PositionVector2D & operator=
113  fCoordinates = v.Coordinates();
114  return *this;
115  }
116 
117  /**
118  Assignment operator from a displacement vector of arbitrary type
119  */
120  template <class OtherCoords>
121  PositionVector2D & operator=
123  fCoordinates = v.Coordinates();
124  return *this;
125  }
126 
127  /**
128  Assignment from a foreign 2D vector type, for example, Hep2Vector
129  Precondition: v must implement methods x() and y()
130  */
131  template <class ForeignVector>
132  PositionVector2D & operator= ( const ForeignVector & v) {
133  SetXY( v.x(), v.y() );
134  return *this;
135  }
136 
137  /**
138  Retrieve a copy of the coordinates object
139  */
140  const CoordSystem & Coordinates() const {
141  return fCoordinates;
142  }
143 
144  /**
145  Set internal data based on 2 Scalar numbers.
146  These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
147  */
149  fCoordinates.SetCoordinates(a, b);
150  return *this;
151  }
152 
153 
154  /**
155  get internal data into 2 Scalar numbers.
156  These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
157  */
158  void GetCoordinates( Scalar& a, Scalar& b) const
159  { fCoordinates.GetCoordinates(a, b); }
160 
161 
162  /**
163  set the values of the vector from the cartesian components (x,y)
164  (if the vector is held in polar coordinates,
165  then (x, y) are converted to that form)
166  */
168  fCoordinates.SetXY (a,b);
169  return *this;
170  }
171 
172  // ------------------- Equality -----------------
173 
174  /**
175  Exact equality
176  */
177  bool operator==(const PositionVector2D & rhs) const {
178  return fCoordinates==rhs.fCoordinates;
179  }
180  bool operator!= (const PositionVector2D & rhs) const {
181  return !(operator==(rhs));
182  }
183 
184  // ------ Individual element access, in various coordinate systems ------
185 
186  /**
187  Cartesian X, converting if necessary from internal coordinate system.
188  */
189  Scalar X() const { return fCoordinates.X(); }
190 
191  /**
192  Cartesian Y, converting if necessary from internal coordinate system.
193  */
194  Scalar Y() const { return fCoordinates.Y(); }
195 
196  /**
197  Polar R, converting if necessary from internal coordinate system.
198  */
199  Scalar R() const { return fCoordinates.R(); }
200 
201  /**
202  Polar phi, converting if necessary from internal coordinate system.
203  */
204  Scalar Phi() const { return fCoordinates.Phi(); }
205 
206  /**
207  Magnitute squared ( r^2 in spherical coordinate)
208  */
209  Scalar Mag2() const { return fCoordinates.Mag2();}
210 
211 
212  // It is physically meaningless to speak of the unit vector corresponding
213  // to a point.
214 
215  // ------ Setting individual elements present in coordinate system ------
216 
217  /**
218  Change X - Cartesian2D coordinates only
219  */
221  fCoordinates.SetX(a);
222  return *this;
223  }
224 
225  /**
226  Change Y - Cartesian2D coordinates only
227  */
229  fCoordinates.SetY(a);
230  return *this;
231  }
232 
233 
234  /**
235  Change R - Polar2D coordinates only
236  */
238  fCoordinates.SetR(a);
239  return *this;
240  }
241 
242  /**
243  Change Phi - Polar2D coordinates
244  */
246  fCoordinates.SetPhi(ang);
247  return *this;
248  }
249 
250 
251  // ------ Operations combining two vectors ------
252  // need to specialize to exclude those with a different tags
253 
254  /**
255  Return the scalar (Dot) product of this with a displacement vector in
256  any coordinate system, but with the same tag
257  */
258  template< class OtherCoords >
259  Scalar Dot( const DisplacementVector2D<OtherCoords,Tag> & v) const {
260  return X()*v.x() + Y()*v.y();
261  }
262 
263 
264  // The Dot product of a pair of point vectors are physically
265  // meaningless concepts and thus are defined as private methods
266 
267 
268  /**
269  Self Addition with a displacement vector.
270  */
271  template <class OtherCoords>
273  {
274  SetXY( X() + v.X(), Y() + v.Y() );
275  return *this;
276  }
277 
278  /**
279  Self Difference with a displacement vector.
280  */
281  template <class OtherCoords>
283  {
284  SetXY( X() - v.X(), Y() - v.Y() );
285  return *this;
286  }
287 
288  /**
289  multiply this vector by a scalar quantity
290  */
292  fCoordinates.Scale(a);
293  return *this;
294  }
295 
296  /**
297  divide this vector by a scalar quantity
298  */
300  fCoordinates.Scale(1/a);
301  return *this;
302  }
303 
304  // The following methods (v*a and v/a) could instead be free functions.
305  // They were moved into the class to solve a problem on AIX.
306  /**
307  Multiply a vector by a real number
308  */
309  PositionVector2D operator * ( Scalar a ) const {
310  PositionVector2D tmp(*this);
311  tmp *= a;
312  return tmp;
313  }
314 
315  /**
316  Division of a vector with a real number
317  */
319  PositionVector2D tmp(*this);
320  tmp /= a;
321  return tmp;
322  }
323 
324  /**
325  Rotate by an angle
326  */
327  void Rotate( Scalar angle) {
328  return fCoordinates.Rotate(angle);
329  }
330 
331  // Limited backward name compatibility with CLHEP
332 
333  Scalar x() const { return fCoordinates.X(); }
334  Scalar y() const { return fCoordinates.Y(); }
335  Scalar r() const { return fCoordinates.R(); }
336  Scalar phi() const { return fCoordinates.Phi(); }
337  Scalar mag2() const { return fCoordinates.Mag2(); }
338 
339  private:
340 
341  CoordSystem fCoordinates;
342 
343  // Prohibited methods
344 
345  // this should not compile (if from a vector or points with different tag
346 
347  template <class OtherCoords, class OtherTag>
349 
350  template <class OtherCoords, class OtherTag>
352 
353  template <class OtherCoords, class OtherTag>
355 
356  template <class OtherCoords, class OtherTag>
358 
359  template <class OtherCoords, class OtherTag>
361 
362  template <class OtherCoords, class OtherTag>
364 
365 // /**
366 // Dot product of two position vectors is inappropriate
367 // */
368 // template <class T2, class U>
369 // PositionVector2D Dot( const PositionVector2D<T2,U> & v) const;
370 
371 
372 
373  };
374 
375 // ---------- PositionVector2D class template ends here ----------------
376 // ---------------------------------------------------------------------
377 
378  /**
379  Multiplication of a position vector by real number a*v
380  */
381  template <class CoordSystem, class U>
382  inline
386  return v *= a;
387  // Note - passing v by value and using operator *= may save one
388  // copy relative to passing v by const ref and creating a temporary.
389  }
390 
391  /**
392  Difference between two PositionVector2D vectors.
393  The result is a DisplacementVector2D.
394  The (coordinate system) type of the returned vector is defined to
395  be identical to that of the first position vector.
396  */
397 
398  template <class CoordSystem1, class CoordSystem2, class U>
399  inline
400  DisplacementVector2D<CoordSystem1,U>
404  v1.X()-v2.X(), v1.Y()-v2.Y() )
405  );
406  }
407 
408  /**
409  Addition of a PositionVector2D and a DisplacementVector2D.
410  The return type is a PositionVector2D,
411  of the same (coordinate system) type as the input PositionVector2D.
412  */
413  template <class CoordSystem1, class CoordSystem2, class U>
414  inline
415  PositionVector2D<CoordSystem2,U>
418  return p1 += v2;
419  }
420 
421  /**
422  Addition of a DisplacementVector2D and a PositionVector2D.
423  The return type is a PositionVector2D,
424  of the same (coordinate system) type as the input PositionVector2D.
425  */
426  template <class CoordSystem1, class CoordSystem2, class U>
427  inline
428  PositionVector2D<CoordSystem2,U>
431  return p2 += v1;
432  }
433 
434  /**
435  Subtraction of a DisplacementVector2D from a PositionVector2D.
436  The return type is a PositionVector2D,
437  of the same (coordinate system) type as the input PositionVector2D.
438  */
439  template <class CoordSystem1, class CoordSystem2, class U>
440  inline
441  PositionVector2D<CoordSystem2,U>
444  return p1 -= v2;
445  }
446 
447  // Scaling of a position vector with a real number is not physically meaningful
448 
449  // ------------- I/O to/from streams -------------
450 
451  template< class char_t, class traits_t, class T, class U >
452  inline
453  std::basic_ostream<char_t,traits_t> &
454  operator << ( std::basic_ostream<char_t,traits_t> & os
455  , PositionVector2D<T,U> const & v
456  )
457  {
458  if( !os ) return os;
459 
460  typename T::Scalar a, b;
461  v.GetCoordinates(a, b);
462 
463  if( detail::get_manip( os, detail::bitforbit ) ) {
464  detail::set_manip( os, detail::bitforbit, '\00' );
466  BR::Output(os, a);
467  BR::Output(os, b);
468  }
469  else {
470  os << detail::get_manip( os, detail::open ) << a
471  << detail::get_manip( os, detail::sep ) << b
473  }
474 
475  return os;
476 
477  } // op<< <>()
478 
479 
480  template< class char_t, class traits_t, class T, class U >
481  inline
482  std::basic_istream<char_t,traits_t> &
483  operator >> ( std::basic_istream<char_t,traits_t> & is
485  )
486  {
487  if( !is ) return is;
488 
489  typename T::Scalar a, b;
490 
491  if( detail::get_manip( is, detail::bitforbit ) ) {
492  detail::set_manip( is, detail::bitforbit, '\00' );
494  BR::Input(is, a);
495  BR::Input(is, b);
496  }
497  else {
498  detail::require_delim( is, detail::open ); is >> a;
499  detail::require_delim( is, detail::sep ); is >> b;
501  }
502 
503  if( is )
504  v.SetCoordinates(a, b);
505  return is;
506 
507  } // op>> <>()
508 
509 
510 
511 
512  } // namespace Math
513 
514 } // namespace ROOT
515 
516 
517 #endif /* ROOT_Math_GenVector_PositionVector2D */
PositionVector2D(const Scalar &a, const Scalar &b)
Construct from three values of type Scalar.
PositionVector2D & operator-=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Difference with a displacement vector.
const Double_t * v1
Definition: TArcBall.cxx:33
bool operator==(const PositionVector2D &rhs) const
Exact equality.
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
PositionVector2D(const PositionVector2D< T, Tag > &v)
Construct from a position vector expressed in different coordinates, or using a different Scalar type...
PositionVector2D(const DisplacementVector2D< T, Tag > &p)
Construct from an arbitrary displacement vector.
const CoordSystem & Coordinates() const
Retrieve a copy of the coordinates object.
std::basic_istream< char_t, traits_t > & operator>>(std::basic_istream< char_t, traits_t > &is, DisplacementVector2D< T, U > &v)
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
TArc * a
Definition: textangle.C:12
void GetCoordinates(Scalar &a, Scalar &b) const
get internal data into 2 Scalar numbers.
PositionVector2D< CoordSystem, Tag > & SetR(Scalar a)
Change R - Polar2D coordinates only.
PositionVector2D & operator=(const PositionVector2D< OtherCoords, Tag > &v)
Assignment operator from a position vector of arbitrary type.
DisplacementVector2D< CoordSystem1, U > operator+(DisplacementVector2D< CoordSystem1, U > v1, const DisplacementVector2D< CoordSystem2, U > &v2)
Addition of DisplacementVector2D vectors.
PositionVector2D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b)
Set internal data based on 2 Scalar numbers.
CoordSystem::Scalar Scalar
DisplacementVector2D< CoordSystem1, U > operator-(DisplacementVector2D< CoordSystem1, U > v1, DisplacementVector2D< CoordSystem2, U > const &v2)
Difference between two DisplacementVector2D vectors.
void Rotate(Scalar angle)
Rotate by an angle.
PositionVector2D< CoordSystem, Tag > & SetX(Scalar a)
Change X - Cartesian2D coordinates only.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
Scalar Dot(const DisplacementVector2D< OtherCoords, Tag > &v) const
Return the scalar (Dot) product of this with a displacement vector in any coordinate system...
static double p2(double t, double a, double b, double c)
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
PositionVector2D< CoordSystem, Tag > & SetXY(Scalar a, Scalar b)
set the values of the vector from the cartesian components (x,y) (if the vector is held in polar coor...
char_t get_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m)
Definition: GenVectorIO.h:54
SVector< double, 2 > v
Definition: Dict.h:5
PositionVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
PositionVector2D operator*(Scalar a) const
Multiply a vector by a real number.
PositionVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
#define Scalar
Definition: global.h:83
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
static double p1(double t, double a, double b)
Class describing a generic position vector (point) in 2 dimensions.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
PositionVector2D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar2D coordinates.
PositionVector2D< CoordSystem, Tag > & SetY(Scalar a)
Change Y - Cartesian2D coordinates only.
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition: Cartesian2D.h:41
PositionVector2D(const ForeignVector &v)
Construct from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement metho...
void set_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m, char_t ch)
Definition: GenVectorIO.h:74
Namespace for new Math classes and functions.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
std::basic_istream< char_t, traits_t > & require_delim(std::basic_istream< char_t, traits_t > &is, manip_t m)
Definition: GenVectorIO.h:113
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
PositionVector2D()
Default constructor.
PositionVector2D operator/(Scalar a) const
Division of a vector with a real number.
bool operator!=(const PositionVector2D &rhs) const
Class describing a generic displacement vector in 2 dimensions.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.