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