Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
28namespace 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 @sa Overview of the @ref GenVector "physics vector library"
47 */
48
49 template <class CoordSystem, class Tag = DefaultCoordinateSystemTag >
51
52 public:
53
54 typedef typename CoordSystem::Scalar Scalar;
55 typedef CoordSystem CoordinateType;
57
58 // ------ ctors ------
59
60 /**
61 Default constructor. Construct an empty object with zero values
62 */
63
65
66 /**
67 Construct from three values of type <em>Scalar</em>.
68 In the case of a XYPoint the values are x,y
69 In the case of a polar vector they are r,phi
70 */
71 PositionVector2D(const Scalar & a, const Scalar & b) :
72 fCoordinates ( a , b) { }
73
74 /**
75 Construct from a position vector expressed in different
76 coordinates, or using a different Scalar type
77 */
78 template <class T>
80 fCoordinates ( v.Coordinates() ) { }
81
82 /**
83 Construct from an arbitrary displacement vector
84 */
85 template <class T>
87 fCoordinates ( p.Coordinates() ) { }
88
89 /**
90 Construct from a foreign 2D vector type, for example, Hep2Vector
91 Precondition: v must implement methods x() and y()
92 */
93 template <class ForeignVector>
94 explicit PositionVector2D( const ForeignVector & v) :
95 fCoordinates ( Cartesian2D<Scalar>( v.x(), v.y() ) ) { }
96
97 // compiler-generated copy ctor and dtor are fine.
98
99 // ------ assignment ------
100
101 /**
102 Assignment operator from a position vector of arbitrary type
103 */
104 template <class OtherCoords>
108 return *this;
109 }
110
111 /**
112 Assignment operator from a displacement vector of arbitrary type
113 */
114 template <class OtherCoords>
118 return *this;
119 }
120
121 /**
122 Assignment from a foreign 2D vector type, for example, Hep2Vector
123 Precondition: v must implement methods x() and y()
124 */
125 template <class ForeignVector>
126 PositionVector2D & operator= ( const ForeignVector & v) {
127 SetXY( v.x(), v.y() );
128 return *this;
129 }
130
131 /**
132 Retrieve a copy of the coordinates object
133 */
134 const CoordSystem & Coordinates() const {
135 return fCoordinates;
136 }
137
138 /**
139 Set internal data based on 2 Scalar numbers.
140 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
141 */
143 fCoordinates.SetCoordinates(a, b);
144 return *this;
145 }
146
147
148 /**
149 get internal data into 2 Scalar numbers.
150 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
151 */
153 { fCoordinates.GetCoordinates(a, b); }
154
155
156 /**
157 set the values of the vector from the cartesian components (x,y)
158 (if the vector is held in polar coordinates,
159 then (x, y) are converted to that form)
160 */
162 fCoordinates.SetXY (a,b);
163 return *this;
164 }
165
166 // ------------------- Equality -----------------
167
168 /**
169 Exact equality
170 */
171 bool operator==(const PositionVector2D & rhs) const {
172 return fCoordinates==rhs.fCoordinates;
173 }
174 bool operator!= (const PositionVector2D & rhs) const {
175 return !(operator==(rhs));
176 }
177
178 // ------ Individual element access, in various coordinate systems ------
179
180 /**
181 Cartesian X, converting if necessary from internal coordinate system.
182 */
183 Scalar X() const { return fCoordinates.X(); }
184
185 /**
186 Cartesian Y, converting if necessary from internal coordinate system.
187 */
188 Scalar Y() const { return fCoordinates.Y(); }
189
190 /**
191 Polar R, converting if necessary from internal coordinate system.
192 */
193 Scalar R() const { return fCoordinates.R(); }
194
195 /**
196 Polar phi, converting if necessary from internal coordinate system.
197 */
198 Scalar Phi() const { return fCoordinates.Phi(); }
199
200 /**
201 Magnitute squared ( r^2 in spherical coordinate)
202 */
203 Scalar Mag2() const { return fCoordinates.Mag2();}
204
205
206 // It is physically meaningless to speak of the unit vector corresponding
207 // to a point.
208
209 // ------ Setting individual elements present in coordinate system ------
210
211 /**
212 Change X - Cartesian2D coordinates only
213 */
215 fCoordinates.SetX(a);
216 return *this;
217 }
218
219 /**
220 Change Y - Cartesian2D coordinates only
221 */
223 fCoordinates.SetY(a);
224 return *this;
225 }
226
227
228 /**
229 Change R - Polar2D coordinates only
230 */
232 fCoordinates.SetR(a);
233 return *this;
234 }
235
236 /**
237 Change Phi - Polar2D coordinates
238 */
240 fCoordinates.SetPhi(ang);
241 return *this;
242 }
243
244
245 // ------ Operations combining two vectors ------
246 // need to specialize to exclude those with a different tags
247
248 /**
249 Return the scalar (Dot) product of this with a displacement vector in
250 any coordinate system, but with the same tag
251 */
252 template< class OtherCoords >
254 return X()*v.x() + Y()*v.y();
255 }
256
257
258 // The Dot product of a pair of point vectors are physically
259 // meaningless concepts and thus are defined as private methods
260
261
262 /**
263 Self Addition with a displacement vector.
264 */
265 template <class OtherCoords>
267 {
268 SetXY( X() + v.X(), Y() + v.Y() );
269 return *this;
270 }
271
272 /**
273 Self Difference with a displacement vector.
274 */
275 template <class OtherCoords>
277 {
278 SetXY( X() - v.X(), Y() - v.Y() );
279 return *this;
280 }
281
282 /**
283 multiply this vector by a scalar quantity
284 */
286 fCoordinates.Scale(a);
287 return *this;
288 }
289
290 /**
291 divide this vector by a scalar quantity
292 */
294 fCoordinates.Scale(1/a);
295 return *this;
296 }
297
298 // The following methods (v*a and v/a) could instead be free functions.
299 // They were moved into the class to solve a problem on AIX.
300 /**
301 Multiply a vector by a real number
302 */
304 PositionVector2D tmp(*this);
305 tmp *= a;
306 return tmp;
307 }
308
309 /**
310 Division of a vector with a real number
311 */
313 PositionVector2D tmp(*this);
314 tmp /= a;
315 return tmp;
316 }
317
318 /**
319 Rotate by an angle
320 */
322 return fCoordinates.Rotate(angle);
323 }
324
325 // Limited backward name compatibility with CLHEP
326
327 Scalar x() const { return fCoordinates.X(); }
328 Scalar y() const { return fCoordinates.Y(); }
329 Scalar r() const { return fCoordinates.R(); }
330 Scalar phi() const { return fCoordinates.Phi(); }
331 Scalar mag2() const { return fCoordinates.Mag2(); }
332
333 private:
334
335 CoordSystem fCoordinates;
336
337 // Prohibited methods
338
339 // this should not compile (if from a vector or points with different tag
340
341 template <class OtherCoords, class OtherTag>
343
344 template <class OtherCoords, class OtherTag>
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// /**
360// Dot product of two position vectors is inappropriate
361// */
362// template <class T2, class U>
363// PositionVector2D Dot( const PositionVector2D<T2,U> & v) const;
364
365
366
367 };
368
369// ---------- PositionVector2D class template ends here ----------------
370// ---------------------------------------------------------------------
371
372 /**
373 Multiplication of a position vector by real number a*v
374 */
375 template <class CoordSystem, class U>
376 inline
380 return v *= a;
381 // Note - passing v by value and using operator *= may save one
382 // copy relative to passing v by const ref and creating a temporary.
383 }
384
385 /**
386 Difference between two PositionVector2D vectors.
387 The result is a DisplacementVector2D.
388 The (coordinate system) type of the returned vector is defined to
389 be identical to that of the first position vector.
390 */
391
392 template <class CoordSystem1, class CoordSystem2, class U>
393 inline
394 DisplacementVector2D<CoordSystem1,U>
398 v1.X()-v2.X(), v1.Y()-v2.Y() )
399 );
400 }
401
402 /**
403 Addition of a PositionVector2D and a DisplacementVector2D.
404 The return type is a PositionVector2D,
405 of the same (coordinate system) type as the input PositionVector2D.
406 */
407 template <class CoordSystem1, class CoordSystem2, class U>
408 inline
409 PositionVector2D<CoordSystem2,U>
412 return p1 += v2;
413 }
414
415 /**
416 Addition of a DisplacementVector2D and a PositionVector2D.
417 The return type is a PositionVector2D,
418 of the same (coordinate system) type as the input PositionVector2D.
419 */
420 template <class CoordSystem1, class CoordSystem2, class U>
421 inline
422 PositionVector2D<CoordSystem2,U>
425 return p2 += v1;
426 }
427
428 /**
429 Subtraction of a DisplacementVector2D from a PositionVector2D.
430 The return type is a PositionVector2D,
431 of the same (coordinate system) type as the input PositionVector2D.
432 */
433 template <class CoordSystem1, class CoordSystem2, class U>
434 inline
435 PositionVector2D<CoordSystem2,U>
438 return p1 -= v2;
439 }
440
441 // Scaling of a position vector with a real number is not physically meaningful
442
443 // ------------- I/O to/from streams -------------
444
445 template< class char_t, class traits_t, class T, class U >
446 inline
447 std::basic_ostream<char_t,traits_t> &
448 operator << ( std::basic_ostream<char_t,traits_t> & os
449 , PositionVector2D<T,U> const & v
450 )
451 {
452 if( !os ) return os;
453
454 typename T::Scalar a, b;
456
459 typedef GenVector_detail::BitReproducible BR;
460 BR::Output(os, a);
461 BR::Output(os, b);
462 }
463 else {
464 os << detail::get_manip( os, detail::open ) << a
465 << detail::get_manip( os, detail::sep ) << b
467 }
468
469 return os;
470
471 } // op<< <>()
472
473
474 template< class char_t, class traits_t, class T, class U >
475 inline
476 std::basic_istream<char_t,traits_t> &
477 operator >> ( std::basic_istream<char_t,traits_t> & is
479 )
480 {
481 if( !is ) return is;
482
483 typename T::Scalar a, b;
484
488 BR::Input(is, a);
489 BR::Input(is, b);
490 }
491 else {
493 detail::require_delim( is, detail::sep ); is >> b;
495 }
496
497 if( is )
498 v.SetCoordinates(a, b);
499 return is;
500
501 } // op>> <>()
502
503
504
505
506 } // namespace Math
507
508} // namespace ROOT
509
510
511#endif /* ROOT_Math_GenVector_PositionVector2D */
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint angle
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition Cartesian2D.h:39
Class describing a generic displacement vector in 2 dimensions.
Class describing a generic position vector (point) in 2 dimensions.
PositionVector2D< CoordSystem, Tag > & SetX(Scalar a)
Change X - Cartesian2D coordinates only.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
bool operator!=(const PositionVector2D &rhs) const
PositionVector2D & operator-=(const DisplacementVector2D< OtherCoords, OtherTag > &)
void GetCoordinates(Scalar &a, Scalar &b) const
get internal data into 2 Scalar numbers.
PositionVector2D & operator=(const PositionVector2D< OtherCoords, Tag > &v)
Assignment operator from a position vector of arbitrary type.
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
PositionVector2D & operator=(const DisplacementVector2D< OtherCoords, OtherTag > &)
void Rotate(Scalar angle)
Rotate by an angle.
Scalar Dot(const DisplacementVector2D< OtherCoords, Tag > &v) const
Return the scalar (Dot) product of this with a displacement vector in any coordinate system,...
PositionVector2D & operator=(const PositionVector2D< OtherCoords, OtherTag > &)
const CoordSystem & Coordinates() const
Retrieve a copy of the coordinates object.
PositionVector2D< CoordSystem, Tag > & SetY(Scalar a)
Change Y - Cartesian2D coordinates only.
PositionVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
PositionVector2D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar2D coordinates.
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...
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.
PositionVector2D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b)
Set internal data based on 2 Scalar numbers.
PositionVector2D operator*(Scalar a) const
Multiply a vector by a real number.
PositionVector2D operator/(Scalar a) const
Division of a vector with a real number.
PositionVector2D & operator-=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Difference with a displacement vector.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, OtherTag > &)
PositionVector2D()
Default constructor.
PositionVector2D(const ForeignVector &v)
Construct from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement metho...
PositionVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
bool operator==(const PositionVector2D &rhs) const
Exact equality.
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
PositionVector2D< CoordSystem, Tag > & SetR(Scalar a)
Change R - Polar2D coordinates only.
PositionVector2D(const Scalar &a, const Scalar &b)
Construct from three values of type Scalar.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
PositionVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
PositionVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
char_t get_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m)
Definition GenVectorIO.h:54
std::basic_istream< char_t, traits_t > & require_delim(std::basic_istream< char_t, traits_t > &is, manip_t m)
void set_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m, char_t ch)
Definition GenVectorIO.h:74
std::basic_istream< char_t, traits_t > & operator>>(std::basic_istream< char_t, traits_t > &is, DisplacementVector2D< T, U > &v)
DisplacementVector2D< CoordSystem1, U > operator+(DisplacementVector2D< CoordSystem1, U > v1, const DisplacementVector2D< CoordSystem2, U > &v2)
Addition of DisplacementVector2D vectors.
DisplacementVector2D< CoordSystem1, U > operator-(DisplacementVector2D< CoordSystem1, U > v1, DisplacementVector2D< CoordSystem2, U > const &v2)
Difference between two DisplacementVector2D vectors.
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.