ROOT   Reference Guide
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
64 constexpr PositionVector2D() : fCoordinates() { }
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 constexpr 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>
79 explicit constexpr PositionVector2D( const PositionVector2D<T,Tag> & v) :
80 fCoordinates ( v.Coordinates() ) { }
81
82 /**
83 Construct from an arbitrary displacement vector
84 */
85 template <class T>
86 explicit constexpr PositionVector2D( const DisplacementVector2D<T,Tag> & p) :
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 constexpr 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 Dimension
182 */
183 unsigned int Dimension() const { return fDimension; };
184
185 /**
186 Cartesian X, converting if necessary from internal coordinate system.
187 */
188 Scalar X() const { return fCoordinates.X(); }
189
190 /**
191 Cartesian Y, converting if necessary from internal coordinate system.
192 */
193 Scalar Y() const { return fCoordinates.Y(); }
194
195 /**
196 Polar R, converting if necessary from internal coordinate system.
197 */
198 Scalar R() const { return fCoordinates.R(); }
199
200 /**
201 Polar phi, converting if necessary from internal coordinate system.
202 */
203 Scalar Phi() const { return fCoordinates.Phi(); }
204
205 /**
206 Magnitute squared ( r^2 in spherical coordinate)
207 */
208 Scalar Mag2() const { return fCoordinates.Mag2();}
209
210
211 // It is physically meaningless to speak of the unit vector corresponding
212 // to a point.
213
214 // ------ Setting individual elements present in coordinate system ------
215
216 /**
217 Change X - Cartesian2D coordinates only
218 */
220 fCoordinates.SetX(a);
221 return *this;
222 }
223
224 /**
225 Change Y - Cartesian2D coordinates only
226 */
228 fCoordinates.SetY(a);
229 return *this;
230 }
231
232
233 /**
234 Change R - Polar2D coordinates only
235 */
237 fCoordinates.SetR(a);
238 return *this;
239 }
240
241 /**
242 Change Phi - Polar2D coordinates
243 */
245 fCoordinates.SetPhi(ang);
246 return *this;
247 }
248
249
250 // ------ Operations combining two vectors ------
251 // need to specialize to exclude those with a different tags
252
253 /**
254 Return the scalar (Dot) product of this with a displacement vector in
255 any coordinate system, but with the same tag
256 */
257 template< class OtherCoords >
259 return X()*v.x() + Y()*v.y();
260 }
261
262
263 // The Dot product of a pair of point vectors are physically
264 // meaningless concepts and thus are defined as private methods
265
266
267 /**
268 Self Addition with a displacement vector.
269 */
270 template <class OtherCoords>
272 {
273 SetXY( X() + v.X(), Y() + v.Y() );
274 return *this;
275 }
276
277 /**
278 Self Difference with a displacement vector.
279 */
280 template <class OtherCoords>
282 {
283 SetXY( X() - v.X(), Y() - v.Y() );
284 return *this;
285 }
286
287 /**
288 multiply this vector by a scalar quantity
289 */
291 fCoordinates.Scale(a);
292 return *this;
293 }
294
295 /**
296 divide this vector by a scalar quantity
297 */
299 fCoordinates.Scale(1/a);
300 return *this;
301 }
302
303 // The following methods (v*a and v/a) could instead be free functions.
304 // They were moved into the class to solve a problem on AIX.
305 /**
306 Multiply a vector by a real number
307 */
309 PositionVector2D tmp(*this);
310 tmp *= a;
311 return tmp;
312 }
313
314 /**
315 Division of a vector with a real number
316 */
318 PositionVector2D tmp(*this);
319 tmp /= a;
320 return tmp;
321 }
322
323 /**
324 Rotate by an angle
325 */
327 return fCoordinates.Rotate(angle);
328 }
329
330 // Limited backward name compatibility with CLHEP
331
332 Scalar x() const { return fCoordinates.X(); }
333 Scalar y() const { return fCoordinates.Y(); }
334 Scalar r() const { return fCoordinates.R(); }
335 Scalar phi() const { return fCoordinates.Phi(); }
336 Scalar mag2() const { return fCoordinates.Mag2(); }
337
338 private:
339
340 CoordSystem fCoordinates;
341 static constexpr unsigned int fDimension = CoordinateType::Dimension;
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;
462
465 typedef GenVector_detail::BitReproducible BR;
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
494 BR::Input(is, a);
495 BR::Input(is, b);
496 }
497 else {
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 */
#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.
constexpr PositionVector2D(const Scalar &a, const Scalar &b)
Construct from three values of type Scalar.
constexpr PositionVector2D(const DisplacementVector2D< T, Tag > &p)
Construct from an arbitrary displacement vector.
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.
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< 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.
unsigned int Dimension() const
Dimension.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, OtherTag > &)
static constexpr unsigned int fDimension
constexpr PositionVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
constexpr 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 & operator+=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
constexpr PositionVector2D(const PositionVector2D< T, Tag > &v)
Construct from a position vector expressed in different coordinates, or using a different Scalar type...
PositionVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
constexpr PositionVector2D()
Default constructor.
constexpr PositionVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...