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_MathX_GenVectorX_PositionVector2D
17#define ROOT_MathX_GenVectorX_PositionVector2D 1
18
20
22
24
26
28
30
31using namespace ROOT::ROOT_MATH_ARCH;
32
33namespace ROOT {
34
35namespace ROOT_MATH_ARCH {
36
37//__________________________________________________________________________________________
38/**
39 Class describing a generic position vector (point) in 2 dimensions.
40 This class is templated on the type of Coordinate system.
41 One example is the XYPoint which is a vector based on
42 double precision x,y data members by using the
43 ROOT::Math::Cartesian2D<double> Coordinate system.
44 The class is having also an extra template parameter, the coordinate system tag,
45 to be able to identify (tag) vector described in different reference coordinate system,
46 like global or local coordinate systems.
47
48 @ingroup GenVectorX
49
50 @see GenVectorX
51*/
52
53template <class CoordSystem, class Tag = DefaultCoordinateSystemTag>
55
56public:
57 typedef typename CoordSystem::Scalar Scalar;
58 typedef CoordSystem CoordinateType;
60
61 // ------ ctors ------
62
63 /**
64 Default constructor. Construct an empty object with zero values
65 */
66
68
69 /**
70 Construct from three values of type <em>Scalar</em>.
71 In the case of a XYPoint the values are x,y
72 In the case of a polar vector they are r,phi
73 */
75
76 /**
77 Construct from a position vector expressed in different
78 coordinates, or using a different Scalar type
79 */
80 template <class T>
84
85 /**
86 Construct from an arbitrary displacement vector
87 */
88 template <class T>
92
93 /**
94 Construct from a foreign 2D vector type, for example, Hep2Vector
95 Precondition: v must implement methods x() and y()
96 */
97 template <class ForeignVector>
99 {
100 }
101
102 // compiler-generated copy ctor and dtor are fine.
103
104 // ------ assignment ------
105
106 /**
107 Assignment operator from a position vector of arbitrary type
108 */
109 template <class OtherCoords>
111 {
112 fCoordinates = v.Coordinates();
113 return *this;
114 }
115
116 /**
117 Assignment operator from a displacement vector of arbitrary type
118 */
119 template <class OtherCoords>
121 {
122 fCoordinates = v.Coordinates();
123 return *this;
124 }
125
126 /**
127 Assignment from a foreign 2D vector type, for example, Hep2Vector
128 Precondition: v must implement methods x() and y()
129 */
130 template <class ForeignVector>
132 {
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 { return fCoordinates; }
141
142 /**
143 Set internal data based on 2 Scalar numbers.
144 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
145 */
147 {
148 fCoordinates.SetCoordinates(a, b);
149 return *this;
150 }
151
152 /**
153 get internal data into 2 Scalar numbers.
154 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
155 */
156 void GetCoordinates(Scalar &a, Scalar &b) const { fCoordinates.GetCoordinates(a, b); }
157
158 /**
159 set the values of the vector from the cartesian components (x,y)
160 (if the vector is held in polar coordinates,
161 then (x, y) are converted to that form)
162 */
164 {
165 fCoordinates.SetXY(a, b);
166 return *this;
167 }
168
169 // ------------------- Equality -----------------
170
171 /**
172 Exact equality
173 */
174 bool operator==(const PositionVector2D &rhs) const { return fCoordinates == rhs.fCoordinates; }
175 bool operator!=(const PositionVector2D &rhs) const { return !(operator==(rhs)); }
176
177 // ------ Individual element access, in various coordinate systems ------
178
179 /**
180 Cartesian X, converting if necessary from internal coordinate system.
181 */
182 Scalar X() const { return fCoordinates.X(); }
183
184 /**
185 Cartesian Y, converting if necessary from internal coordinate system.
186 */
187 Scalar Y() const { return fCoordinates.Y(); }
188
189 /**
190 Polar R, converting if necessary from internal coordinate system.
191 */
192 Scalar R() const { return fCoordinates.R(); }
193
194 /**
195 Polar phi, converting if necessary from internal coordinate system.
196 */
197 Scalar Phi() const { return fCoordinates.Phi(); }
198
199 /**
200 Magnitute squared ( r^2 in spherical coordinate)
201 */
202 Scalar Mag2() const { return fCoordinates.Mag2(); }
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 {
214 fCoordinates.SetX(a);
215 return *this;
216 }
217
218 /**
219 Change Y - Cartesian2D coordinates only
220 */
222 {
223 fCoordinates.SetY(a);
224 return *this;
225 }
226
227 /**
228 Change R - Polar2D coordinates only
229 */
231 {
232 fCoordinates.SetR(a);
233 return *this;
234 }
235
236 /**
237 Change Phi - Polar2D coordinates
238 */
240 {
241 fCoordinates.SetPhi(ang);
242 return *this;
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 {
255 return X() * v.x() + Y() * v.y();
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 Self Addition with a displacement vector.
263 */
264 template <class OtherCoords>
266 {
267 SetXY(X() + v.X(), Y() + v.Y());
268 return *this;
269 }
270
271 /**
272 Self Difference with a displacement vector.
273 */
274 template <class OtherCoords>
276 {
277 SetXY(X() - v.X(), Y() - v.Y());
278 return *this;
279 }
280
281 /**
282 multiply this vector by a scalar quantity
283 */
285 {
286 fCoordinates.Scale(a);
287 return *this;
288 }
289
290 /**
291 divide this vector by a scalar quantity
292 */
294 {
295 fCoordinates.Scale(1 / a);
296 return *this;
297 }
298
299 // The following methods (v*a and v/a) could instead be free functions.
300 // They were moved into the class to solve a problem on AIX.
301 /**
302 Multiply a vector by a real number
303 */
305 {
306 PositionVector2D tmp(*this);
307 tmp *= a;
308 return tmp;
309 }
310
311 /**
312 Division of a vector with a real number
313 */
315 {
316 PositionVector2D tmp(*this);
317 tmp /= a;
318 return tmp;
319 }
320
321 /**
322 Rotate by an angle
323 */
324 void Rotate(Scalar angle) { return fCoordinates.Rotate(angle); }
325
326 // Limited backward name compatibility with CLHEP
327
328 Scalar x() const { return fCoordinates.X(); }
329 Scalar y() const { return fCoordinates.Y(); }
330 Scalar r() const { return fCoordinates.R(); }
331 Scalar phi() const { return fCoordinates.Phi(); }
332 Scalar mag2() const { return fCoordinates.Mag2(); }
333
334private:
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// ---------- PositionVector2D class template ends here ----------------
367// ---------------------------------------------------------------------
368
369/**
370 Multiplication of a position vector by real number a*v
371*/
372template <class CoordSystem, class U>
375{
376 return v *= a;
377 // Note - passing v by value and using operator *= may save one
378 // copy relative to passing v by const ref and creating a temporary.
379}
380
381/**
382 Difference between two PositionVector2D vectors.
383 The result is a DisplacementVector2D.
384 The (coordinate system) type of the returned vector is defined to
385 be identical to that of the first position vector.
386*/
387
388template <class CoordSystem1, class CoordSystem2, class U>
395
396/**
397 Addition of a PositionVector2D and a DisplacementVector2D.
398 The return type is a PositionVector2D,
399 of the same (coordinate system) type as the input PositionVector2D.
400*/
401template <class CoordSystem1, class CoordSystem2, class U>
407
408/**
409 Addition of a DisplacementVector2D and a PositionVector2D.
410 The return type is a PositionVector2D,
411 of the same (coordinate system) type as the input PositionVector2D.
412*/
413template <class CoordSystem1, class CoordSystem2, class U>
419
420/**
421 Subtraction of a DisplacementVector2D from a PositionVector2D.
422 The return type is a PositionVector2D,
423 of the same (coordinate system) type as the input PositionVector2D.
424*/
425template <class CoordSystem1, class CoordSystem2, class U>
431
432// Scaling of a position vector with a real number is not physically meaningful
433
434#if !defined(ROOT_MATH_SYCL) && !defined(ROOT_MATH_CUDA)
435// ------------- I/O to/from streams -------------
436
437template <class char_t, class traits_t, class T, class U>
438inline std::basic_ostream<char_t, traits_t> &
439operator<<(std::basic_ostream<char_t, traits_t> &os, PositionVector2D<T, U> const &v)
440{
441 if (!os)
442 return os;
443
444 typename T::Scalar a, b;
445 v.GetCoordinates(a, b);
446
450 BR::Output(os, a);
451 BR::Output(os, b);
452 } else {
455 }
456
457 return os;
458
459} // op<< <>()
460
461template <class char_t, class traits_t, class T, class U>
462inline std::basic_istream<char_t, traits_t> &
463operator>>(std::basic_istream<char_t, traits_t> &is, PositionVector2D<T, U> &v)
464{
465 if (!is)
466 return is;
467
468 typename T::Scalar a, b;
469
473 BR::Input(is, a);
474 BR::Input(is, b);
475 } else {
477 is >> a;
479 is >> b;
481 }
482
483 if (is)
484 v.SetCoordinates(a, b);
485 return is;
486
487} // op>> <>()
488#endif
489
490} // namespace ROOT_MATH_ARCH
491
492} // namespace ROOT
493
494#endif /* ROOT_MathX_GenVectorX_PositionVector2D */
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:44
Class describing a generic position vector (point) in 2 dimensions.
PositionVector2D(const DisplacementVector2D< T, Tag > &p)
Construct from an arbitrary displacement vector.
PositionVector2D & operator=(const PositionVector2D< OtherCoords, Tag > &v)
Assignment operator from a position vector of arbitrary type.
PositionVector2D(const Scalar &a, const Scalar &b)
Construct from three values of type Scalar.
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
PositionVector2D & operator=(const PositionVector2D< OtherCoords, OtherTag > &)
void Rotate(Scalar angle)
Rotate by an angle.
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 & operator=(const DisplacementVector2D< OtherCoords, OtherTag > &)
PositionVector2D operator*(Scalar a) const
Multiply a vector by a real number.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
PositionVector2D(const ForeignVector &v)
Construct from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement metho...
PositionVector2D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b)
Set internal data based on 2 Scalar numbers.
PositionVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
void GetCoordinates(Scalar &a, Scalar &b) const
get internal data into 2 Scalar numbers.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, OtherTag > &)
PositionVector2D & operator=(const DisplacementVector2D< OtherCoords, Tag > &v)
Assignment operator from a displacement vector of arbitrary type.
PositionVector2D< CoordSystem, Tag > & SetY(Scalar a)
Change Y - Cartesian2D coordinates only.
PositionVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
PositionVector2D & operator-=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Difference with a displacement vector.
bool operator!=(const PositionVector2D &rhs) const
bool operator==(const PositionVector2D &rhs) const
Exact equality.
PositionVector2D & operator+=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
PositionVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
PositionVector2D & operator-=(const DisplacementVector2D< OtherCoords, OtherTag > &)
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
const CoordSystem & Coordinates() const
Retrieve a copy of the coordinates object.
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
Scalar Dot(const DisplacementVector2D< OtherCoords, Tag > &v) const
Return the scalar (Dot) product of this with a displacement vector in any coordinate system,...
PositionVector2D< CoordSystem, Tag > & SetX(Scalar a)
Change X - Cartesian2D coordinates only.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
PositionVector2D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar2D coordinates.
PositionVector2D operator/(Scalar a) const
Division of a vector with a real number.
PositionVector2D & operator=(const ForeignVector &v)
Assignment from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement meth...
PositionVector2D< CoordSystem, Tag > & SetR(Scalar a)
Change R - Polar2D coordinates only.
PositionVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
std::basic_istream< char_t, traits_t > & require_delim(std::basic_istream< char_t, traits_t > &is, manip_t m)
char_t get_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m)
Definition GenVectorIO.h:60
void set_manip(std::basic_ios< char_t, traits_t > &ios, manip_t m, char_t ch)
Definition GenVectorIO.h:77
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition AxisAngle.cxx:98
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, 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.
DisplacementVector2D< CoordSystem1, U > operator+(DisplacementVector2D< CoordSystem1, U > v1, const DisplacementVector2D< CoordSystem2, U > &v2)
Addition of DisplacementVector2D vectors.