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_Math_GenVector_DisplacementVector2D
18#define ROOT_Math_GenVector_DisplacementVector2D 1
19
21
23
25
27
29
30//#include "Math/GenVector/Expression2D.h"
31
32
33
34
35namespace ROOT {
36
37 namespace Math {
38
39
40
41//__________________________________________________________________________________________
42 /**
43 Class describing a generic displacement vector in 2 dimensions.
44 This class is templated on the type of Coordinate system.
45 One example is the XYVector which is a vector based on
46 double precision x,y data members by using the
47 ROOT::Math::Cartesian2D<double> Coordinate system.
48 The class is having also an extra template parameter, the coordinate system tag,
49 to be able to identify (tag) vector described in different reference coordinate system,
50 like global or local coordinate systems.
51
52 @ingroup GenVector
53
54 @see GenVector
55 */
56
57 template <class CoordSystem, class Tag = DefaultCoordinateSystemTag >
59
60 public:
61
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 */
71 constexpr DisplacementVector2D ( ) : fCoordinates() { }
72
73
74 /**
75 Construct from three values of type <em>Scalar</em>.
76 In the case of a XYVector the values are x,y
77 In the case of a polar vector they are r, phi
78 */
80 fCoordinates ( a , b ) { }
81
82 /**
83 Construct from a displacement vector expressed in different
84 coordinates, or using a different Scalar type, but with same coordinate system tag
85 */
86 template <class OtherCoords>
89
90
91 /**
92 Construct from a position vector expressed in different coordinates
93 but with the same coordinate system tag
94 */
95 template <class OtherCoords>
98
99
100 /**
101 Construct from a foreign 2D vector type, for example, Hep2Vector
102 Precondition: v must implement methods x() and y()
103 */
104 template <class ForeignVector>
105 explicit constexpr DisplacementVector2D( const ForeignVector & v) :
106 fCoordinates ( Cartesian2D<Scalar>( v.x(), v.y() ) ) { }
107
108
109
110 // compiler-generated copy ctor and dtor are fine.
111
112 // ------ assignment ------
113
114 /**
115 Assignment operator from a displacement vector of arbitrary type
116 */
117 template <class OtherCoords>
120 fCoordinates = v.Coordinates();
121 return *this;
122 }
123
124 /**
125 Assignment operator from a position vector
126 (not necessarily efficient unless one or the other is Cartesian)
127 */
128 template <class OtherCoords>
131 SetXY(rhs.x(), rhs.y() );
132 return *this;
133 }
134
135
136 /**
137 Assignment from a foreign 2D vector type, for example, Hep2Vector
138 Precondition: v must implement methods x() and y()
139 */
140 template <class ForeignVector>
142 SetXY( v.x(), v.y() );
143 return *this;
144 }
145
146
147 // ------ Set, Get, and access coordinate data ------
148
149 /**
150 Retrieve a copy of the coordinates object
151 */
152 CoordSystem Coordinates() const {
153 return fCoordinates;
154 }
155
156 /**
157 Set internal data based on 2 Scalar numbers.
158 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
159 */
161 fCoordinates.SetCoordinates(a, b);
162 return *this;
163 }
164
165
166 /**
167 get internal data into 2 Scalar numbers.
168 These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
169 \note Alternatively, you may use structured bindings: `auto const [a, b] = v`.
170 */
172 { fCoordinates.GetCoordinates(a, b); }
173
174
175 /**
176 set the values of the vector from the cartesian components (x,y)
177 (if the vector is held in polar coordinates,
178 then (x, y) are converted to that form)
179 */
181 fCoordinates.SetXY(a,b);
182 return *this;
183 }
184
185 // ------------------- Equality -----------------
186
187 /**
188 Exact equality
189 */
190 bool operator==(const DisplacementVector2D & rhs) const {
191 return fCoordinates==rhs.fCoordinates;
192 }
194 return !(operator==(rhs));
195 }
196
197 // ------ Individual element access, in various coordinate systems ------
198
199 /**
200 Dimension
201 */
202 unsigned int Dimension() const { return fDimension; };
203
204 /**
205 Cartesian X, converting if necessary from internal coordinate system.
206 */
207 Scalar X() const { return fCoordinates.X(); }
208
209 /**
210 Cartesian Y, converting if necessary from internal coordinate system.
211 */
212 Scalar Y() const { return fCoordinates.Y(); }
213
214
215 /**
216 Polar R, converting if necessary from internal coordinate system.
217 */
218 Scalar R() const { return fCoordinates.R(); }
219
220
221 /**
222 Polar phi, converting if necessary from internal coordinate system.
223 */
224 Scalar Phi() const { return fCoordinates.Phi(); }
225
226
227 // ----- Other fundamental properties -----
228
229 /**
230 Magnitute squared ( r^2 in spherical coordinate)
231 */
232 Scalar Mag2() const { return fCoordinates.Mag2();}
233
234
235 /**
236 return unit vector parallel to this
237 */
239 Scalar tot = R();
240 return tot == 0 ? *this : DisplacementVector2D(*this) / tot;
241 }
242
243 // ------ Setting individual elements present in coordinate system ------
244
245 /**
246 Change X - Cartesian2D coordinates only
247 */
249 fCoordinates.SetX(a);
250 return *this;
251 }
252
253 /**
254 Change Y - Cartesian2D coordinates only
255 */
257 fCoordinates.SetY(a);
258 return *this;
259 }
260
261
262 /**
263 Change R - Polar2D coordinates only
264 */
266 fCoordinates.SetR(a);
267 return *this;
268 }
269
270
271 /**
272 Change Phi - Polar2D coordinates
273 */
275 fCoordinates.SetPhi(ang);
276 return *this;
277 }
278
279
280
281 // ------ Operations combining two vectors ------
282 // -- need to have the specialized version in order to avoid
283
284 /**
285 Return the scalar (dot) product of two displacement vectors.
286 It is possible to perform the product for any type of vector coordinates,
287 but they must have the same coordinate system tag
288 */
289 template< class OtherCoords >
291 return X()*v.X() + Y()*v.Y();
292 }
293 /**
294 Return the scalar (dot) product of two vectors.
295 It is possible to perform the product for any classes
296 implementing x() and y() member functions
297 */
298 template< class OtherVector >
299 Scalar Dot( const OtherVector & v) const {
300 return X()*v.x() + Y()*v.y();
301 }
302
303
304
305 /**
306 Self Addition with a displacement vector.
307 */
308 template <class OtherCoords>
311 SetXY( X() + v.X(), Y() + v.Y() );
312 return *this;
313 }
314
315 /**
316 Self Difference with a displacement vector.
317 */
318 template <class OtherCoords>
321 SetXY( x() - v.x(), y() - v.y() );
322 return *this;
323 }
324
325
326 /**
327 multiply this vector by a scalar quantity
328 */
330 fCoordinates.Scale(a);
331 return *this;
332 }
333
334 /**
335 divide this vector by a scalar quantity
336 */
338 fCoordinates.Scale(1/a);
339 return *this;
340 }
341
342 // -- The following methods (v*a and v/a) could instead be free functions.
343 // -- They were moved into the class to solve a problem on AIX.
344
345 /**
346 Multiply a vector by a real number
347 */
349 DisplacementVector2D tmp(*this);
350 tmp *= a;
351 return tmp;
352 }
353
354 /**
355 Negative of the vector
356 */
358 return operator*( Scalar(-1) );
359 }
360
361 /**
362 Positive of the vector, return itself
363 */
364 DisplacementVector2D operator + ( ) const {return *this;}
365
366 /**
367 Division of a vector with a real number
368 */
370 DisplacementVector2D tmp(*this);
371 tmp /= a;
372 return tmp;
373 }
374
375 /**
376 Rotate by an angle
377 */
379 return fCoordinates.Rotate(angle);
380 }
381
382
383 // Methods providing Limited backward name compatibility with CLHEP
384
385 Scalar x() const { return fCoordinates.X(); }
386 Scalar y() const { return fCoordinates.Y(); }
387 Scalar r() const { return fCoordinates.R(); }
388 Scalar phi() const { return fCoordinates.Phi(); }
389 Scalar mag2() const { return fCoordinates.Mag2(); }
390 DisplacementVector2D unit() const {return Unit();}
391
392
393 private:
394
395 CoordSystem fCoordinates; // internal coordinate system
396 static constexpr unsigned int fDimension = CoordinateType::Dimension;
397
398 // the following methods should not compile
399
400 // this should not compile (if from a vector or points with different tag
401 template <class OtherCoords, class OtherTag>
403
404 template <class OtherCoords, class OtherTag>
406
407 template <class OtherCoords, class OtherTag>
409
410
411 template <class OtherCoords, class OtherTag>
413
414 template <class OtherCoords, class OtherTag>
416
417 template <class OtherCoords, class OtherTag>
419
420 template<class OtherCoords, class OtherTag >
422
423 template<class OtherCoords, class OtherTag >
425
426
427 };
428
429// ---------- DisplacementVector2D class template ends here ------------
430// ---------------------------------------------------------------------
431
432
433 /**
434 Addition of DisplacementVector2D vectors.
435 The (coordinate system) type of the returned vector is defined to
436 be identical to that of the first vector, which is passed by value
437 */
438 template <class CoordSystem1, class CoordSystem2, class U>
439 inline
445
446 /**
447 Difference between two DisplacementVector2D vectors.
448 The (coordinate system) type of the returned vector is defined to
449 be identical to that of the first vector.
450 */
451 template <class CoordSystem1, class CoordSystem2, class U>
452 inline
458
459
460
461
462
463 /**
464 Multiplication of a displacement vector by real number a*v
465 */
466 template <class CoordSystem, class U>
467 inline
471 return v *= a;
472 // Note - passing v by value and using operator *= may save one
473 // copy relative to passing v by const ref and creating a temporary.
474 }
475
476
477 // v1*v2 notation for Cross product of two vectors is omitted,
478 // since it is always confusing as to whether dot product is meant.
479
480
481
482 // ------------- I/O to/from streams -------------
483
484 template< class char_t, class traits_t, class T, class U >
485 inline
486 std::basic_ostream<char_t,traits_t> &
487 operator << ( std::basic_ostream<char_t,traits_t> & os
489 )
490 {
491 if( !os ) return os;
492
493 typename T::Scalar a, b;
494 v.GetCoordinates(a, b);
495
498 typedef GenVector_detail::BitReproducible BR;
499 BR::Output(os, a);
500 BR::Output(os, b);
501 }
502 else {
503 os << detail::get_manip( os, detail::open ) << a
504 << detail::get_manip( os, detail::sep ) << b
506 }
507
508 return os;
509
510 } // op<< <>()
511
512
513 template< class char_t, class traits_t, class T, class U >
514 inline
515 std::basic_istream<char_t,traits_t> &
516 operator >> ( std::basic_istream<char_t,traits_t> & is
518 )
519 {
520 if( !is ) return is;
521
522 typename T::Scalar a, b;
523
527 BR::Input(is, a);
528 BR::Input(is, b);
529 }
530 else {
534 }
535
536 if( is )
537 v.SetCoordinates(a, b);
538 return is;
539
540 } // op>> <>()
541
542 // Structured bindings
543 template <std::size_t I, class CoordSystem, class Tag>
544 typename CoordSystem::Scalar get(DisplacementVector2D<CoordSystem, Tag> const& p)
545 {
546 static_assert(I < 2);
547 if constexpr (I == 0) {
548 return p.x();
549 } else {
550 return p.y();
551 }
552 }
553
554 } // namespace Math
555
556} // namespace ROOT
557
558// Structured bindings
559#include <tuple>
560namespace std {
561 template <class CoordSystem, class Tag>
562 struct tuple_size<ROOT::Math::DisplacementVector2D<CoordSystem, Tag>> : integral_constant<size_t, 2> {};
563 template <size_t I, class CoordSystem, class Tag>
564 struct tuple_element<I, ROOT::Math::DisplacementVector2D<CoordSystem, Tag>> {
565 static_assert(I < 2);
566 using type = typename CoordSystem::Scalar;
567 };
568}
569
570#endif /* ROOT_Math_GenVector_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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition Cartesian2D.h:39
Class describing a generic displacement vector in 2 dimensions.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
constexpr DisplacementVector2D(const PositionVector2D< OtherCoords, Tag > &p)
Construct from a position vector expressed in different coordinates but with the same coordinate syst...
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
constexpr DisplacementVector2D()
Default constructor.
DisplacementVector2D Cross(const DisplacementVector2D< OtherCoords, OtherTag > &) const
constexpr DisplacementVector2D(Scalar a, Scalar b)
Construct from three values of type Scalar.
constexpr DisplacementVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
DisplacementVector2D & operator=(const DisplacementVector2D< OtherCoords, OtherTag > &)
constexpr DisplacementVector2D(const DisplacementVector2D< OtherCoords, Tag > &v)
Construct from a displacement vector expressed in different coordinates, or using a different Scalar ...
DisplacementVector2D< CoordSystem, Tag > & SetY(Scalar a)
Change Y - Cartesian2D coordinates only.
DisplacementVector2D & operator=(const DisplacementVector2D< OtherCoords, Tag > &v)
Assignment operator from a displacement vector of arbitrary type.
DisplacementVector2D operator+() const
Positive of the vector, return itself.
DisplacementVector2D operator-() const
Negative of the vector.
Scalar Dot(const OtherVector &v) const
Return the scalar (dot) product of two vectors.
CoordSystem Coordinates() const
Retrieve a copy of the coordinates object.
constexpr 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
Exact equality.
DisplacementVector2D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b)
Set internal data based on 2 Scalar numbers.
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
Scalar Dot(const DisplacementVector2D< OtherCoords, OtherTag > &) const
DisplacementVector2D & operator*=(Scalar a)
multiply this vector by a scalar quantity
DisplacementVector2D & operator-=(const DisplacementVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar2D coordinates.
DisplacementVector2D & operator+=(const DisplacementVector2D< OtherCoords, OtherTag > &)
constexpr DisplacementVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D & operator=(const PositionVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
bool operator!=(const DisplacementVector2D &rhs) const
static constexpr unsigned int fDimension
DisplacementVector2D unit() const
void Rotate(Scalar angle)
Rotate by an angle.
DisplacementVector2D< CoordSystem, Tag > & SetX(Scalar a)
Change X - Cartesian2D coordinates only.
DisplacementVector2D< CoordSystem, Tag > & SetR(Scalar a)
Change R - Polar2D coordinates only.
Scalar Dot(const DisplacementVector2D< OtherCoords, Tag > &v) const
Return the scalar (dot) product of two displacement vectors.
void GetCoordinates(Scalar &a, Scalar &b) const
get internal data into 2 Scalar numbers.
DisplacementVector2D operator*(Scalar a) const
Multiply a vector by a real number.
DisplacementVector2D operator/(Scalar a) const
Division of a vector with a real number.
unsigned int Dimension() const
Dimension.
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 Unit() const
return unit vector parallel to this
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
#define I(x, y, z)
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::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition AxisAngle.cxx:91
std::basic_istream< char_t, traits_t > & operator>>(std::basic_istream< char_t, traits_t > &is, DisplacementVector2D< T, U > &v)
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
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.