Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DisplacementVector2D.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 and *
7 * FNAL LCG ROOT MathLib Team *
8 * *
9 * *
10 **********************************************************************/
11
12// Header source file for class DisplacementVector2D
13//
14// Created by: Lorenzo Moneta at Mon Apr 16 2007
15//
16
17#ifndef ROOT_MathX_GenVectorX_DisplacementVector2D
18#define ROOT_MathX_GenVectorX_DisplacementVector2D 1
19
21
23
25
27
29
30// #include "MathX/GenVectorX/Expression2D.h"
31
33
35
36using namespace ROOT::ROOT_MATH_ARCH;
37
38namespace ROOT {
39
40namespace ROOT_MATH_ARCH {
41
42//__________________________________________________________________________________________
43/**
44 Class describing a generic displacement vector in 2 dimensions.
45 This class is templated on the type of Coordinate system.
46 One example is the XYVector which is a vector based on
47 double precision x,y data members by using the
48 ROOT::Math::Cartesian2D<double> Coordinate system.
49 The class is having also an extra template parameter, the coordinate system tag,
50 to be able to identify (tag) vector described in different reference coordinate system,
51 like global or local coordinate systems.
52
53 @ingroup GenVectorX
54
55 @see GenVectorX
56*/
57
58template <class CoordSystem, class Tag = DefaultCoordinateSystemTag>
60
61public:
62 typedef typename CoordSystem::Scalar Scalar;
63 typedef CoordSystem CoordinateType;
65
66 // ------ ctors ------
67
68 /**
69 Default constructor. Construct an empty object with zero values
70 */
72
73 /**
74 Construct from three values of type <em>Scalar</em>.
75 In the case of a XYVector the values are x,y
76 In the case of a polar vector they are r, phi
77 */
79
80 /**
81 Construct from a displacement vector expressed in different
82 coordinates, or using a different Scalar type, but with same coordinate system tag
83 */
84 template <class OtherCoords>
88
89 /**
90 Construct from a position vector expressed in different coordinates
91 but with the same coordinate system tag
92 */
93 template <class OtherCoords>
97
98 /**
99 Construct from a foreign 2D vector type, for example, Hep2Vector
100 Precondition: v must implement methods x() and y()
101 */
102 template <class ForeignVector>
104 {
105 }
106
107 // compiler-generated copy ctor and dtor are fine.
108
109 // ------ assignment ------
110
111 /**
112 Assignment operator from a displacement vector of arbitrary type
113 */
114 template <class OtherCoords>
116 {
117 fCoordinates = v.Coordinates();
118 return *this;
119 }
120
121 /**
122 Assignment operator from a position vector
123 (not necessarily efficient unless one or the other is Cartesian)
124 */
125 template <class OtherCoords>
127 {
128 SetXY(rhs.x(), rhs.y());
129 return *this;
130 }
131
132 /**
133 Assignment from a foreign 2D vector type, for example, Hep2Vector
134 Precondition: v must implement methods x() and y()
135 */
136 template <class ForeignVector>
138 {
139 SetXY(v.x(), v.y());
140 return *this;
141 }
142
143 // ------ Set, Get, and access coordinate data ------
144
145 /**
146 Retrieve a copy of the coordinates object
147 */
148 CoordSystem Coordinates() const { return fCoordinates; }
149
150 /**
151 Set internal data based on 2 Scalar numbers.
152 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
153 */
155 {
156 fCoordinates.SetCoordinates(a, b);
157 return *this;
158 }
159
160 /**
161 get internal data into 2 Scalar numbers.
162 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
163 */
164 void GetCoordinates(Scalar &a, Scalar &b) const { fCoordinates.GetCoordinates(a, b); }
165
166 /**
167 set the values of the vector from the cartesian components (x,y)
168 (if the vector is held in polar coordinates,
169 then (x, y) are converted to that form)
170 */
172 {
173 fCoordinates.SetXY(a, b);
174 return *this;
175 }
176
177 // ------------------- Equality -----------------
178
179 /**
180 Exact equality
181 */
182 bool operator==(const DisplacementVector2D &rhs) const { return fCoordinates == rhs.fCoordinates; }
183 bool operator!=(const DisplacementVector2D &rhs) const { return !(operator==(rhs)); }
184
185 // ------ Individual element access, in various coordinate systems ------
186
187 /**
188 Cartesian X, converting if necessary from internal coordinate system.
189 */
190 Scalar X() const { return fCoordinates.X(); }
191
192 /**
193 Cartesian Y, converting if necessary from internal coordinate system.
194 */
195 Scalar Y() const { return fCoordinates.Y(); }
196
197 /**
198 Polar R, converting if necessary from internal coordinate system.
199 */
200 Scalar R() const { return fCoordinates.R(); }
201
202 /**
203 Polar phi, converting if necessary from internal coordinate system.
204 */
205 Scalar Phi() const { return fCoordinates.Phi(); }
206
207 // ----- Other fundamental properties -----
208
209 /**
210 Magnitute squared ( r^2 in spherical coordinate)
211 */
212 Scalar Mag2() const { return fCoordinates.Mag2(); }
213
214 /**
215 return unit vector parallel to this
216 */
218 {
219 Scalar tot = R();
220 return tot == 0 ? *this : DisplacementVector2D(*this) / tot;
221 }
222
223 // ------ Setting individual elements present in coordinate system ------
224
225 /**
226 Change X - Cartesian2D coordinates only
227 */
229 {
230 fCoordinates.SetX(a);
231 return *this;
232 }
233
234 /**
235 Change Y - Cartesian2D coordinates only
236 */
238 {
239 fCoordinates.SetY(a);
240 return *this;
241 }
242
243 /**
244 Change R - Polar2D coordinates only
245 */
247 {
248 fCoordinates.SetR(a);
249 return *this;
250 }
251
252 /**
253 Change Phi - Polar2D coordinates
254 */
256 {
257 fCoordinates.SetPhi(ang);
258 return *this;
259 }
260
261 // ------ Operations combining two vectors ------
262 // -- need to have the specialized version in order to avoid
263
264 /**
265 Return the scalar (dot) product of two displacement vectors.
266 It is possible to perform the product for any type of vector coordinates,
267 but they must have the same coordinate system tag
268 */
269 template <class OtherCoords>
271 {
272 return X() * v.X() + Y() * v.Y();
273 }
274 /**
275 Return the scalar (dot) product of two vectors.
276 It is possible to perform the product for any classes
277 implementing x() and y() member functions
278 */
279 template <class OtherVector>
280 Scalar Dot(const OtherVector &v) const
281 {
282 return X() * v.x() + Y() * v.y();
283 }
284
285 /**
286 Self Addition with a displacement vector.
287 */
288 template <class OtherCoords>
290 {
291 SetXY(X() + v.X(), Y() + v.Y());
292 return *this;
293 }
294
295 /**
296 Self Difference with a displacement vector.
297 */
298 template <class OtherCoords>
300 {
301 SetXY(x() - v.x(), y() - v.y());
302 return *this;
303 }
304
305 /**
306 multiply this vector by a scalar quantity
307 */
309 {
310 fCoordinates.Scale(a);
311 return *this;
312 }
313
314 /**
315 divide this vector by a scalar quantity
316 */
318 {
319 fCoordinates.Scale(1 / a);
320 return *this;
321 }
322
323 // -- The following methods (v*a and v/a) could instead be free functions.
324 // -- They were moved into the class to solve a problem on AIX.
325
326 /**
327 Multiply a vector by a real number
328 */
330 {
331 DisplacementVector2D tmp(*this);
332 tmp *= a;
333 return tmp;
334 }
335
336 /**
337 Negative of the vector
338 */
340
341 /**
342 Positive of the vector, return itself
343 */
344 DisplacementVector2D operator+() const { return *this; }
345
346 /**
347 Division of a vector with a real number
348 */
350 {
351 DisplacementVector2D tmp(*this);
352 tmp /= a;
353 return tmp;
354 }
355
356 /**
357 Rotate by an angle
358 */
359 void Rotate(Scalar angle) { return fCoordinates.Rotate(angle); }
360
361 // Methods providing Limited backward name compatibility with CLHEP
362
363 Scalar x() const { return fCoordinates.X(); }
364 Scalar y() const { return fCoordinates.Y(); }
365 Scalar r() const { return fCoordinates.R(); }
366 Scalar phi() const { return fCoordinates.Phi(); }
367 Scalar mag2() const { return fCoordinates.Mag2(); }
368 DisplacementVector2D unit() const { return Unit(); }
369
370private:
371 CoordSystem fCoordinates; // internal coordinate system
372
373 // the following methods should not compile
374
375 // this should not compile (if from a vector or points with different tag
376 template <class OtherCoords, class OtherTag>
380
381 template <class OtherCoords, class OtherTag>
385
386 template <class OtherCoords, class OtherTag>
388
389 template <class OtherCoords, class OtherTag>
391
392 template <class OtherCoords, class OtherTag>
394
395 template <class OtherCoords, class OtherTag>
397
398 template <class OtherCoords, class OtherTag>
400
401 template <class OtherCoords, class OtherTag>
403};
404
405// ---------- DisplacementVector2D class template ends here ------------
406// ---------------------------------------------------------------------
407
408/**
409 Addition of DisplacementVector2D vectors.
410 The (coordinate system) type of the returned vector is defined to
411 be identical to that of the first vector, which is passed by value
412*/
413template <class CoordSystem1, class CoordSystem2, class U>
419
420/**
421 Difference between two DisplacementVector2D vectors.
422 The (coordinate system) type of the returned vector is defined to
423 be identical to that of the first vector.
424*/
425template <class CoordSystem1, class CoordSystem2, class U>
431
432/**
433 Multiplication of a displacement vector by real number a*v
434*/
435template <class CoordSystem, class U>
438{
439 return v *= a;
440 // Note - passing v by value and using operator *= may save one
441 // copy relative to passing v by const ref and creating a temporary.
442}
443
444// v1*v2 notation for Cross product of two vectors is omitted,
445// since it is always confusing as to whether dot product is meant.
446
447#if !defined(ROOT_MATH_SYCL) && !defined(ROOT_MATH_CUDA)
448
449// ------------- I/O to/from streams -------------
450
451template <class char_t, class traits_t, class T, class U>
452inline std::basic_ostream<char_t, traits_t> &
453operator<<(std::basic_ostream<char_t, traits_t> &os, DisplacementVector2D<T, U> const &v)
454{
455 if (!os)
456 return os;
457
458 typename T::Scalar a, b;
459 v.GetCoordinates(a, b);
460
464 BR::Output(os, a);
465 BR::Output(os, b);
466 } else {
469 }
470
471 return os;
472
473} // op<< <>()
474
475template <class char_t, class traits_t, class T, class U>
476inline std::basic_istream<char_t, traits_t> &
477operator>>(std::basic_istream<char_t, traits_t> &is, DisplacementVector2D<T, U> &v)
478{
479 if (!is)
480 return is;
481
482 typename T::Scalar a, b;
483
487 BR::Input(is, a);
488 BR::Input(is, b);
489 } else {
491 is >> a;
493 is >> b;
495 }
496
497 if (is)
498 v.SetCoordinates(a, b);
499 return is;
500
501} // op>> <>()
502
503#endif
504
505} // namespace ROOT_MATH_ARCH
506
507} // namespace ROOT
508
509#endif /* ROOT_MathX_GenVectorX_DisplacementVector2D */
#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 displacement vector in 2 dimensions.
DisplacementVector2D< 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...
DisplacementVector2D & operator-=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Difference with a displacement vector.
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
DisplacementVector2D< CoordSystem, Tag > & SetY(Scalar a)
Change Y - Cartesian2D coordinates only.
bool operator==(const DisplacementVector2D &rhs) const
Exact equality.
DisplacementVector2D< CoordSystem, Tag > & SetR(Scalar a)
Change R - Polar2D coordinates only.
DisplacementVector2D(const DisplacementVector2D< OtherCoords, Tag > &v)
Construct from a displacement vector expressed in different coordinates, or using a different Scalar ...
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
DisplacementVector2D & operator+=(const DisplacementVector2D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
Scalar Dot(const DisplacementVector2D< OtherCoords, OtherTag > &) const
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
DisplacementVector2D(Scalar a, Scalar b)
Construct from three values of type Scalar.
DisplacementVector2D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar2D coordinates.
DisplacementVector2D & operator=(const DisplacementVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D Unit() const
return unit vector parallel to this
DisplacementVector2D & operator=(const DisplacementVector2D< OtherCoords, Tag > &v)
Assignment operator from a displacement vector of arbitrary type.
DisplacementVector2D(const ForeignVector &v)
Construct from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement metho...
bool operator!=(const DisplacementVector2D &rhs) const
CoordSystem Coordinates() const
Retrieve a copy of the coordinates object.
DisplacementVector2D & operator=(const PositionVector2D< OtherCoords, Tag > &rhs)
Assignment operator from a position vector (not necessarily efficient unless one or the other is Cart...
DisplacementVector2D & operator-=(const DisplacementVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D Cross(const DisplacementVector2D< OtherCoords, OtherTag > &) const
DisplacementVector2D & operator=(const PositionVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
DisplacementVector2D< CoordSystem, Tag > & SetX(Scalar a)
Change X - Cartesian2D coordinates only.
DisplacementVector2D operator-() const
Negative of the vector.
void GetCoordinates(Scalar &a, Scalar &b) const
get internal data into 2 Scalar numbers.
Scalar Dot(const DisplacementVector2D< OtherCoords, Tag > &v) const
Return the scalar (dot) product of two displacement vectors.
void Rotate(Scalar angle)
Rotate by an angle.
DisplacementVector2D operator+() const
Positive of the vector, return itself.
DisplacementVector2D & operator=(const ForeignVector &v)
Assignment from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement meth...
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
DisplacementVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
DisplacementVector2D operator*(Scalar a) const
Multiply a vector by a real number.
DisplacementVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b)
Set internal data based on 2 Scalar numbers.
DisplacementVector2D operator/(Scalar a) const
Division of a vector with a real number.
DisplacementVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D & operator+=(const DisplacementVector2D< OtherCoords, OtherTag > &)
Scalar Dot(const OtherVector &v) const
Return the scalar (dot) product of two vectors.
DisplacementVector2D(const PositionVector2D< OtherCoords, Tag > &p)
Construct from a position vector expressed in different coordinates but with the same coordinate syst...
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.