Logo ROOT  
Reference Guide
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 */
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 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>
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 Cartesian X, converting if necessary from internal coordinate system.
200 */
201 Scalar X() const { return fCoordinates.X(); }
202
203 /**
204 Cartesian Y, converting if necessary from internal coordinate system.
205 */
206 Scalar Y() const { return fCoordinates.Y(); }
207
208
209 /**
210 Polar R, converting if necessary from internal coordinate system.
211 */
212 Scalar R() const { return fCoordinates.R(); }
213
214
215 /**
216 Polar phi, converting if necessary from internal coordinate system.
217 */
218 Scalar Phi() const { return fCoordinates.Phi(); }
219
220
221 // ----- Other fundamental properties -----
222
223 /**
224 Magnitute squared ( r^2 in spherical coordinate)
225 */
226 Scalar Mag2() const { return fCoordinates.Mag2();}
227
228
229 /**
230 return unit vector parallel to this
231 */
233 Scalar tot = R();
234 return tot == 0 ? *this : DisplacementVector2D(*this) / tot;
235 }
236
237 // ------ Setting individual elements present in coordinate system ------
238
239 /**
240 Change X - Cartesian2D coordinates only
241 */
243 fCoordinates.SetX(a);
244 return *this;
245 }
246
247 /**
248 Change Y - Cartesian2D coordinates only
249 */
251 fCoordinates.SetY(a);
252 return *this;
253 }
254
255
256 /**
257 Change R - Polar2D coordinates only
258 */
260 fCoordinates.SetR(a);
261 return *this;
262 }
263
264
265 /**
266 Change Phi - Polar2D coordinates
267 */
269 fCoordinates.SetPhi(ang);
270 return *this;
271 }
272
273
274
275 // ------ Operations combining two vectors ------
276 // -- need to have the specialized version in order to avoid
277
278 /**
279 Return the scalar (dot) product of two displacement vectors.
280 It is possible to perform the product for any type of vector coordinates,
281 but they must have the same coordinate system tag
282 */
283 template< class OtherCoords >
285 return X()*v.X() + Y()*v.Y();
286 }
287 /**
288 Return the scalar (dot) product of two vectors.
289 It is possible to perform the product for any classes
290 implementing x() and y() member functions
291 */
292 template< class OtherVector >
293 Scalar Dot( const OtherVector & v) const {
294 return X()*v.x() + Y()*v.y();
295 }
296
297
298
299 /**
300 Self Addition with a displacement vector.
301 */
302 template <class OtherCoords>
305 SetXY( X() + v.X(), Y() + v.Y() );
306 return *this;
307 }
308
309 /**
310 Self Difference with a displacement vector.
311 */
312 template <class OtherCoords>
315 SetXY( x() - v.x(), y() - v.y() );
316 return *this;
317 }
318
319
320 /**
321 multiply this vector by a scalar quantity
322 */
324 fCoordinates.Scale(a);
325 return *this;
326 }
327
328 /**
329 divide this vector by a scalar quantity
330 */
332 fCoordinates.Scale(1/a);
333 return *this;
334 }
335
336 // -- The following methods (v*a and v/a) could instead be free functions.
337 // -- They were moved into the class to solve a problem on AIX.
338
339 /**
340 Multiply a vector by a real number
341 */
343 DisplacementVector2D tmp(*this);
344 tmp *= a;
345 return tmp;
346 }
347
348 /**
349 Negative of the vector
350 */
352 return operator*( Scalar(-1) );
353 }
354
355 /**
356 Positive of the vector, return itself
357 */
358 DisplacementVector2D operator + ( ) const {return *this;}
359
360 /**
361 Division of a vector with a real number
362 */
364 DisplacementVector2D tmp(*this);
365 tmp /= a;
366 return tmp;
367 }
368
369 /**
370 Rotate by an angle
371 */
373 return fCoordinates.Rotate(angle);
374 }
375
376
377 // Methods providing Limited backward name compatibility with CLHEP
378
379 Scalar x() const { return fCoordinates.X(); }
380 Scalar y() const { return fCoordinates.Y(); }
381 Scalar r() const { return fCoordinates.R(); }
382 Scalar phi() const { return fCoordinates.Phi(); }
383 Scalar mag2() const { return fCoordinates.Mag2(); }
384 DisplacementVector2D unit() const {return Unit();}
385
386
387 private:
388
389 CoordSystem fCoordinates; // internal coordinate system
390
391
392 // the following methods should not compile
393
394 // this should not compile (if from a vector or points with different tag
395 template <class OtherCoords, class OtherTag>
397
398 template <class OtherCoords, class OtherTag>
400
401 template <class OtherCoords, class OtherTag>
403
404
405 template <class OtherCoords, class OtherTag>
407
408 template <class OtherCoords, class OtherTag>
410
411 template <class OtherCoords, class OtherTag>
413
414 template<class OtherCoords, class OtherTag >
416
417 template<class OtherCoords, class OtherTag >
419
420
421 };
422
423// ---------- DisplacementVector2D class template ends here ------------
424// ---------------------------------------------------------------------
425
426
427 /**
428 Addition of DisplacementVector2D vectors.
429 The (coordinate system) type of the returned vector is defined to
430 be identical to that of the first vector, which is passed by value
431 */
432 template <class CoordSystem1, class CoordSystem2, class U>
433 inline
437 return v1 += v2;
438 }
439
440 /**
441 Difference between two DisplacementVector2D vectors.
442 The (coordinate system) type of the returned vector is defined to
443 be identical to that of the first vector.
444 */
445 template <class CoordSystem1, class CoordSystem2, class U>
446 inline
447 DisplacementVector2D<CoordSystem1,U>
450 return v1 -= v2;
451 }
452
453
454
455
456
457 /**
458 Multiplication of a displacement vector by real number a*v
459 */
460 template <class CoordSystem, class U>
461 inline
462 DisplacementVector2D<CoordSystem,U>
465 return v *= a;
466 // Note - passing v by value and using operator *= may save one
467 // copy relative to passing v by const ref and creating a temporary.
468 }
469
470
471 // v1*v2 notation for Cross product of two vectors is omitted,
472 // since it is always confusing as to whether dot product is meant.
473
474
475
476 // ------------- I/O to/from streams -------------
477
478 template< class char_t, class traits_t, class T, class U >
479 inline
480 std::basic_ostream<char_t,traits_t> &
481 operator << ( std::basic_ostream<char_t,traits_t> & os
482 , DisplacementVector2D<T,U> const & v
483 )
484 {
485 if( !os ) return os;
486
487 typename T::Scalar a, b;
488 v.GetCoordinates(a, b);
489
492 typedef GenVector_detail::BitReproducible BR;
493 BR::Output(os, a);
494 BR::Output(os, b);
495 }
496 else {
497 os << detail::get_manip( os, detail::open ) << a
498 << detail::get_manip( os, detail::sep ) << b
500 }
501
502 return os;
503
504 } // op<< <>()
505
506
507 template< class char_t, class traits_t, class T, class U >
508 inline
509 std::basic_istream<char_t,traits_t> &
510 operator >> ( std::basic_istream<char_t,traits_t> & is
512 )
513 {
514 if( !is ) return is;
515
516 typename T::Scalar a, b;
517
521 BR::Input(is, a);
522 BR::Input(is, b);
523 }
524 else {
526 detail::require_delim( is, detail::sep ); is >> b;
528 }
529
530 if( is )
531 v.SetCoordinates(a, b);
532 return is;
533
534 } // op>> <>()
535
536
537
538 } // namespace Math
539
540} // namespace ROOT
541
542
543#endif /* ROOT_Math_GenVector_DisplacementVector2D */
544
winID h TVirtualViewer3D TVirtualGLPainter p
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 b
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.
DisplacementVector2D()
Default constructor.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
DisplacementVector2D(Scalar a, Scalar b)
Construct from three values of type Scalar.
DisplacementVector2D(const ForeignVector &v)
Construct from a foreign 2D vector type, for example, Hep2Vector Precondition: v must implement metho...
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
DisplacementVector2D Cross(const DisplacementVector2D< OtherCoords, OtherTag > &) const
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
DisplacementVector2D & operator=(const DisplacementVector2D< OtherCoords, OtherTag > &)
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.
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.
DisplacementVector2D(const DisplacementVector2D< OtherCoords, OtherTag > &)
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 > &)
DisplacementVector2D & operator=(const PositionVector2D< OtherCoords, OtherTag > &)
DisplacementVector2D & operator/=(Scalar a)
divide this vector by a scalar quantity
bool operator!=(const DisplacementVector2D &rhs) const
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.
DisplacementVector2D(const DisplacementVector2D< OtherCoords, Tag > &v)
Construct from a displacement vector expressed in different coordinates, or using a different Scalar ...
DisplacementVector2D(const PositionVector2D< OtherCoords, OtherTag > &)
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(const PositionVector2D< OtherCoords, Tag > &p)
Construct from a position vector expressed in different coordinates but with the same coordinate syst...
DisplacementVector2D operator/(Scalar a) const
Division of a vector with a real number.
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.
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)
Definition: GenVectorIO.h:113
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)
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.
Rotation3D::Scalar Scalar
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
auto * a
Definition: textangle.C:12