ROOT   Reference Guide
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 @sa Overview of the @ref GenVector "physics vector library"
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>
88 fCoordinates ( v.Coordinates() ) { }
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>
97 fCoordinates ( p.Coordinates() ) { }
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>
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>
130 ( const PositionVector2D<OtherCoords,Tag> & rhs) {
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>
141 DisplacementVector2D & operator= ( const ForeignVector & v) {
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 */
171 { fCoordinates.GetCoordinates(a, b); }
172
173
174 /**
175 set the values of the vector from the cartesian components (x,y)
176 (if the vector is held in polar coordinates,
177 then (x, y) are converted to that form)
178 */
180 fCoordinates.SetXY(a,b);
181 return *this;
182 }
183
184 // ------------------- Equality -----------------
185
186 /**
187 Exact equality
188 */
189 bool operator==(const DisplacementVector2D & rhs) const {
190 return fCoordinates==rhs.fCoordinates;
191 }
192 bool operator!= (const DisplacementVector2D & rhs) const {
193 return !(operator==(rhs));
194 }
195
196 // ------ Individual element access, in various coordinate systems ------
197
198 /**
199 Dimension
200 */
201 unsigned int Dimension() const { return fDimension; };
202
203 /**
204 Cartesian X, converting if necessary from internal coordinate system.
205 */
206 Scalar X() const { return fCoordinates.X(); }
207
208 /**
209 Cartesian Y, converting if necessary from internal coordinate system.
210 */
211 Scalar Y() const { return fCoordinates.Y(); }
212
213
214 /**
215 Polar R, converting if necessary from internal coordinate system.
216 */
217 Scalar R() const { return fCoordinates.R(); }
218
219
220 /**
221 Polar phi, converting if necessary from internal coordinate system.
222 */
223 Scalar Phi() const { return fCoordinates.Phi(); }
224
225
226 // ----- Other fundamental properties -----
227
228 /**
229 Magnitute squared ( r^2 in spherical coordinate)
230 */
231 Scalar Mag2() const { return fCoordinates.Mag2();}
232
233
234 /**
235 return unit vector parallel to this
236 */
238 Scalar tot = R();
240 }
241
242 // ------ Setting individual elements present in coordinate system ------
243
244 /**
245 Change X - Cartesian2D coordinates only
246 */
248 fCoordinates.SetX(a);
249 return *this;
250 }
251
252 /**
253 Change Y - Cartesian2D coordinates only
254 */
256 fCoordinates.SetY(a);
257 return *this;
258 }
259
260
261 /**
262 Change R - Polar2D coordinates only
263 */
265 fCoordinates.SetR(a);
266 return *this;
267 }
268
269
270 /**
271 Change Phi - Polar2D coordinates
272 */
274 fCoordinates.SetPhi(ang);
275 return *this;
276 }
277
278
279
280 // ------ Operations combining two vectors ------
281 // -- need to have the specialized version in order to avoid
282
283 /**
284 Return the scalar (dot) product of two displacement vectors.
285 It is possible to perform the product for any type of vector coordinates,
286 but they must have the same coordinate system tag
287 */
288 template< class OtherCoords >
290 return X()*v.X() + Y()*v.Y();
291 }
292 /**
293 Return the scalar (dot) product of two vectors.
294 It is possible to perform the product for any classes
295 implementing x() and y() member functions
296 */
297 template< class OtherVector >
298 Scalar Dot( const OtherVector & v) const {
299 return X()*v.x() + Y()*v.y();
300 }
301
302
303
304 /**
305 Self Addition with a displacement vector.
306 */
307 template <class OtherCoords>
310 SetXY( X() + v.X(), Y() + v.Y() );
311 return *this;
312 }
313
314 /**
315 Self Difference with a displacement vector.
316 */
317 template <class OtherCoords>
320 SetXY( x() - v.x(), y() - v.y() );
321 return *this;
322 }
323
324
325 /**
326 multiply this vector by a scalar quantity
327 */
329 fCoordinates.Scale(a);
330 return *this;
331 }
332
333 /**
334 divide this vector by a scalar quantity
335 */
337 fCoordinates.Scale(1/a);
338 return *this;
339 }
340
341 // -- The following methods (v*a and v/a) could instead be free functions.
342 // -- They were moved into the class to solve a problem on AIX.
343
344 /**
345 Multiply a vector by a real number
346 */
348 DisplacementVector2D tmp(*this);
349 tmp *= a;
350 return tmp;
351 }
352
353 /**
354 Negative of the vector
355 */
357 return operator*( Scalar(-1) );
358 }
359
360 /**
361 Positive of the vector, return itself
362 */
363 DisplacementVector2D operator + ( ) const {return *this;}
364
365 /**
366 Division of a vector with a real number
367 */
369 DisplacementVector2D tmp(*this);
370 tmp /= a;
371 return tmp;
372 }
373
374 /**
375 Rotate by an angle
376 */
378 return fCoordinates.Rotate(angle);
379 }
380
381
382 // Methods providing Limited backward name compatibility with CLHEP
383
384 Scalar x() const { return fCoordinates.X(); }
385 Scalar y() const { return fCoordinates.Y(); }
386 Scalar r() const { return fCoordinates.R(); }
387 Scalar phi() const { return fCoordinates.Phi(); }
388 Scalar mag2() const { return fCoordinates.Mag2(); }
389 DisplacementVector2D unit() const {return Unit();}
390
391
392 private:
393
394 CoordSystem fCoordinates; // internal coordinate system
395 static constexpr unsigned int fDimension = CoordinateType::Dimension;
396
397 // the following methods should not compile
398
399 // this should not compile (if from a vector or points with different tag
400 template <class OtherCoords, class OtherTag>
402
403 template <class OtherCoords, class OtherTag>
405
406 template <class OtherCoords, class OtherTag>
408
409
410 template <class OtherCoords, class OtherTag>
412
413 template <class OtherCoords, class OtherTag>
415
416 template <class OtherCoords, class OtherTag>
418
419 template<class OtherCoords, class OtherTag >
421
422 template<class OtherCoords, class OtherTag >
424
425
426 };
427
428// ---------- DisplacementVector2D class template ends here ------------
429// ---------------------------------------------------------------------
430
431
432 /**
434 The (coordinate system) type of the returned vector is defined to
435 be identical to that of the first vector, which is passed by value
436 */
437 template <class CoordSystem1, class CoordSystem2, class U>
438 inline
442 return v1 += v2;
443 }
444
445 /**
446 Difference between two DisplacementVector2D vectors.
447 The (coordinate system) type of the returned vector is defined to
448 be identical to that of the first vector.
449 */
450 template <class CoordSystem1, class CoordSystem2, class U>
451 inline
452 DisplacementVector2D<CoordSystem1,U>
455 return v1 -= v2;
456 }
457
458
459
460
461
462 /**
463 Multiplication of a displacement vector by real number a*v
464 */
465 template <class CoordSystem, class U>
466 inline
467 DisplacementVector2D<CoordSystem,U>
470 return v *= a;
471 // Note - passing v by value and using operator *= may save one
472 // copy relative to passing v by const ref and creating a temporary.
473 }
474
475
476 // v1*v2 notation for Cross product of two vectors is omitted,
477 // since it is always confusing as to whether dot product is meant.
478
479
480
481 // ------------- I/O to/from streams -------------
482
483 template< class char_t, class traits_t, class T, class U >
484 inline
485 std::basic_ostream<char_t,traits_t> &
486 operator << ( std::basic_ostream<char_t,traits_t> & os
487 , DisplacementVector2D<T,U> const & v
488 )
489 {
490 if( !os ) return os;
491
492 typename T::Scalar a, b;
493 v.GetCoordinates(a, b);
494
497 typedef GenVector_detail::BitReproducible BR;
498 BR::Output(os, a);
499 BR::Output(os, b);
500 }
501 else {
502 os << detail::get_manip( os, detail::open ) << a
503 << detail::get_manip( os, detail::sep ) << b
505 }
506
507 return os;
508
509 } // op<< <>()
510
511
512 template< class char_t, class traits_t, class T, class U >
513 inline
514 std::basic_istream<char_t,traits_t> &
515 operator >> ( std::basic_istream<char_t,traits_t> & is
517 )
518 {
519 if( !is ) return is;
520
521 typename T::Scalar a, b;
522
526 BR::Input(is, a);
527 BR::Input(is, b);
528 }
529 else {
531 detail::require_delim( is, detail::sep ); is >> b;
533 }
534
535 if( is )
536 v.SetCoordinates(a, b);
537 return is;
538
539 } // op>> <>()
540
541
542
543 } // namespace Math
544
545} // namespace ROOT
546
547
548#endif /* ROOT_Math_GenVector_DisplacementVector2D */
549
#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.
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
Class describing a generic position vector (point) in 2 dimensions.
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)