Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PositionVector3D.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 PositionVector3D
12//
13// Created by: Lorenzo Moneta at Mon May 30 15:25:04 2005
14//
15// Last update: $Id$
16//
17#ifndef ROOT_Math_GenVector_PositionVector3D
18#define ROOT_Math_GenVector_PositionVector3D 1
19
21
23
25
27
29
30
31#include <cassert>
32
33namespace ROOT {
34
35 namespace Math {
36
37
38//__________________________________________________________________________________________
39 /**
40 Class describing a generic position vector (point) in 3 dimensions.
41 This class is templated on the type of Coordinate system.
42 One example is the XYZPoint which is a vector based on
43 double precision x,y,z data members by using the
44 ROOT::Math::Cartesian3D<double> Coordinate system.
45 The class is having also an extra template parameter, the coordinate system tag,
46 to be able to identify (tag) vector described in different reference coordinate system,
47 like global or local coordinate systems.
48
49 @ingroup GenVector
50 */
51
52 template <class CoordSystem, class Tag = DefaultCoordinateSystemTag >
54
55 public:
56
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 XYZPoint the values are x,y,z
72 In the case of a polar vector they are r,theta,phi
73 */
74 PositionVector3D(const Scalar & a, const Scalar & b, const Scalar & c) :
75 fCoordinates ( a , b, c) { }
76
77 /**
78 Construct from a position vector expressed in different
79 coordinates, or using a different Scalar type
80 */
81 template <class T>
83 fCoordinates ( v.Coordinates() ) { }
84
85 /**
86 Construct from an arbitrary displacement vector
87 */
88 template <class T>
90 fCoordinates ( p.Coordinates() ) { }
91
92 /**
93 Construct from a foreign 3D vector type, for example, Hep3Vector
94 Precondition: v must implement methods x(), y() and z()
95 */
96 template <class ForeignVector>
97 explicit PositionVector3D( const ForeignVector & v) :
98 fCoordinates ( Cartesian3D<Scalar>( v.x(), v.y(), v.z() ) ) { }
99
100#ifdef LATER
101 /**
102 construct from a generic linear algebra vector of at least size 3
103 implementing operator []. This could be also a C array
104 \par v LAVector
105 \par index0 index where coordinates starts (typically zero)
106 It works for all Coordinates types,
107 ( x= v[index0] for Cartesian and r=v[index0] for Polar )
108 */
109 template <class LAVector>
110 PositionVector3D(const LAVector & v, size_t index0 ) {
111 fCoordinates = CoordSystem ( v[index0], v[index0+1], v[index0+2] );
112 }
113#endif
114
115 // compiler-generated copy ctor and dtor are fine.
116
117 // ------ assignment ------
118
119 /**
120 Assignment operator from a position vector of arbitrary type
121 */
122 template <class OtherCoords>
126 return *this;
127 }
128
129 /**
130 Assignment operator from a displacement vector of arbitrary type
131 */
132 template <class OtherCoords>
136 return *this;
137 }
138
139 /**
140 Assignment from a foreign 3D vector type, for example, Hep3Vector
141 Precondition: v must implement methods x(), y() and z()
142 */
143 template <class ForeignVector>
144 PositionVector3D & operator= ( const ForeignVector & v) {
145 SetXYZ( v.x(), v.y(), v.z() );
146 return *this;
147 }
148
149#ifdef LATER
150 /**
151 assign from a generic linear algebra vector of at least size 3
152 implementing operator [].
153 \par v LAVector
154 \par index0 index where coordinates starts (typically zero)
155 It works for all Coordinates types,
156 ( x= v[index0] for Cartesian and r=v[index0] for Polar )
157 */
158 template <class LAVector>
159 PositionVector3D & assignFrom(const LAVector & v, size_t index0 = 0) {
160 fCoordinates = CoordSystem ( v[index0], v[index0+1], v[index0+2] );
161 return *this;
162 }
163#endif
164
165 /**
166 Retrieve a copy of the coordinates object
167 */
168 const CoordSystem & Coordinates() const {
169 return fCoordinates;
170 }
171
172 /**
173 Set internal data based on a C-style array of 3 Scalar numbers
174 */
176 { fCoordinates.SetCoordinates(src); return *this; }
177
178 /**
179 Set internal data based on 3 Scalar numbers
180 */
182 { fCoordinates.SetCoordinates(a, b, c); return *this; }
183
184 /**
185 Set internal data based on 3 Scalars at *begin to *end
186 */
187 template <class IT>
189 { IT a = begin; IT b = ++begin; IT c = ++begin;
190 (void)end;
191 assert (++begin==end);
192 SetCoordinates (*a,*b,*c);
193 return *this;
194 }
195
196 /**
197 get internal data into 3 Scalar numbers
198 */
199 void GetCoordinates( Scalar& a, Scalar& b, Scalar& c ) const
200 { fCoordinates.GetCoordinates(a, b, c); }
201
202 /**
203 get internal data into a C-style array of 3 Scalar numbers
204 */
205 void GetCoordinates( Scalar dest[] ) const
206 { fCoordinates.GetCoordinates(dest); }
207
208 /**
209 get internal data into 3 Scalars at *begin to *end (3 past begin)
210 */
211 template <class IT>
212 void GetCoordinates( IT begin, IT end ) const
213 { IT a = begin; IT b = ++begin; IT c = ++begin;
214 (void)end;
215 assert (++begin==end);
216 GetCoordinates (*a,*b,*c);
217 }
218
219 /**
220 get internal data into 3 Scalars at *begin
221 */
222 template <class IT>
223 void GetCoordinates( IT begin ) const {
224 Scalar a = Scalar(0);
225 Scalar b = Scalar(0);
226 Scalar c = Scalar(0);
227 GetCoordinates(a, b, c);
228 *begin++ = a;
229 *begin++ = b;
230 *begin = c;
231 }
232
233 /**
234 set the values of the vector from the cartesian components (x,y,z)
235 (if the vector is held in polar or cylindrical eta coordinates,
236 then (x, y, z) are converted to that form)
237 */
239 fCoordinates.SetXYZ(a,b,c);
240 return *this;
241 }
242
243 // ------------------- Equality -----------------
244
245 /**
246 Exact equality
247 */
248 bool operator==(const PositionVector3D & rhs) const {
249 return fCoordinates==rhs.fCoordinates;
250 }
251 bool operator!= (const PositionVector3D & rhs) const {
252 return !(operator==(rhs));
253 }
254
255 // ------ Individual element access, in various coordinate systems ------
256
257 /**
258 Cartesian X, converting if necessary from internal coordinate system.
259 */
260 Scalar X() const { return fCoordinates.X(); }
261
262 /**
263 Cartesian Y, converting if necessary from internal coordinate system.
264 */
265 Scalar Y() const { return fCoordinates.Y(); }
266
267 /**
268 Cartesian Z, converting if necessary from internal coordinate system.
269 */
270 Scalar Z() const { return fCoordinates.Z(); }
271
272 /**
273 Polar R, converting if necessary from internal coordinate system.
274 */
275 Scalar R() const { return fCoordinates.R(); }
276
277 /**
278 Polar theta, converting if necessary from internal coordinate system.
279 */
280 Scalar Theta() const { return fCoordinates.Theta(); }
281
282 /**
283 Polar phi, converting if necessary from internal coordinate system.
284 */
285 Scalar Phi() const { return fCoordinates.Phi(); }
286
287 /**
288 Polar eta, converting if necessary from internal coordinate system.
289 */
290 Scalar Eta() const { return fCoordinates.Eta(); }
291
292 /**
293 Cylindrical transverse component rho
294 */
295 Scalar Rho() const { return fCoordinates.Rho(); }
296
297 // ----- Other fundamental properties -----
298
299 /**
300 Magnitute squared ( r^2 in spherical coordinate)
301 */
302 Scalar Mag2() const { return fCoordinates.Mag2();}
303
304 /**
305 Transverse component squared (rho^2 in cylindrical coordinates.
306 */
307 Scalar Perp2() const { return fCoordinates.Perp2();}
308
309 // It is physically meaningless to speak of the unit vector corresponding
310 // to a point.
311
312 // ------ Setting individual elements present in coordinate system ------
313
314 /**
315 Change X - Cartesian3D coordinates only
316 */
318
319 /**
320 Change Y - Cartesian3D coordinates only
321 */
323
324 /**
325 Change Z - Cartesian3D coordinates only
326 */
328
329 /**
330 Change R - Polar3D coordinates only
331 */
333
334 /**
335 Change Theta - Polar3D coordinates only
336 */
337 PositionVector3D<CoordSystem, Tag>& SetTheta (Scalar ang) { fCoordinates.SetTheta(ang); return *this;}
338
339 /**
340 Change Phi - Polar3D or CylindricalEta3D coordinates
341 */
343
344 /**
345 Change Rho - CylindricalEta3D coordinates only
346 */
348
349 /**
350 Change Eta - CylindricalEta3D coordinates only
351 */
352 PositionVector3D<CoordSystem, Tag>& SetEta (Scalar etaval) { fCoordinates.SetEta(etaval); return *this;}
353
354 // ------ Operations combining two vectors ------
355 // need to specialize to exclude those with a different tags
356
357 /**
358 Return the scalar (Dot) product of this with a displacement vector in
359 any coordinate system, but with the same tag
360 */
361 template< class OtherCoords >
363 return X()*v.x() + Y()*v.y() + Z()*v.z();
364 }
365
366
367 /**
368 Return vector (Cross) product of this point with a displacement, as a
369 point vector in this coordinate system of the first.
370 */
371 template< class OtherCoords >
373 PositionVector3D result;
374 result.SetXYZ ( Y()*v.z() - v.y()*Z(),
375 Z()*v.x() - v.z()*X(),
376 X()*v.y() - v.x()*Y() );
377 return result;
378 }
379
380 // The Dot and Cross products of a pair of point vectors are physically
381 // meaningless concepts and thus are defined as private methods
382
383 // It is physically meaningless to speak of the Unit vector corresponding
384 // to a point.
385
386
387 /**
388 Self Addition with a displacement vector.
389 */
390 template <class OtherCoords>
392 {
393 SetXYZ( X() + v.X(), Y() + v.Y(), Z() + v.Z() );
394 return *this;
395 }
396
397 /**
398 Self Difference with a displacement vector.
399 */
400 template <class OtherCoords>
402 {
403 SetXYZ( X() - v.X(), Y() - v.Y(), Z() - v.Z() );
404 return *this;
405 }
406
407 /**
408 multiply this vector by a scalar quantity
409 */
411 fCoordinates.Scale(a);
412 return *this;
413 }
414
415 /**
416 divide this vector by a scalar quantity
417 */
419 fCoordinates.Scale(1/a);
420 return *this;
421 }
422
423 // The following methods (v*a and v/a) could instead be free functions.
424 // They were moved into the class to solve a problem on AIX.
425 /**
426 Multiply a vector by a real number
427 */
429 PositionVector3D tmp(*this);
430 tmp *= a;
431 return tmp;
432 }
433
434 /**
435 Division of a vector with a real number
436 */
438 PositionVector3D tmp(*this);
439 tmp /= a;
440 return tmp;
441 }
442
443 // Limited backward name compatibility with CLHEP
444
445 Scalar x() const { return fCoordinates.X(); }
446 Scalar y() const { return fCoordinates.Y(); }
447 Scalar z() const { return fCoordinates.Z(); }
448 Scalar r() const { return fCoordinates.R(); }
449 Scalar theta() const { return fCoordinates.Theta(); }
450 Scalar phi() const { return fCoordinates.Phi(); }
451 Scalar eta() const { return fCoordinates.Eta(); }
452 Scalar rho() const { return fCoordinates.Rho(); }
453 Scalar mag2() const { return fCoordinates.Mag2(); }
454 Scalar perp2() const { return fCoordinates.Perp2(); }
455
456 private:
457
458 CoordSystem fCoordinates;
459
460 // Prohibited methods
461
462 // this should not compile (if from a vector or points with different tag
463
464 template <class OtherCoords, class OtherTag>
466
467 template <class OtherCoords, class OtherTag>
469
470 template <class OtherCoords, class OtherTag>
472
473 template <class OtherCoords, class OtherTag>
475
476 template <class OtherCoords, class OtherTag>
478
479 template <class OtherCoords, class OtherTag>
481
482// /**
483// Dot product of two position vectors is inappropriate
484// */
485// template <class T2, class U>
486// PositionVector3D Dot( const PositionVector3D<T2,U> & v) const;
487
488// /**
489// Cross product of two position vectors is inappropriate
490// */
491// template <class T2, class U>
492// PositionVector3D Cross( const PositionVector3D<T2,U> & v) const;
493
494
495
496 };
497
498// ---------- PositionVector3D class template ends here ----------------
499// ---------------------------------------------------------------------
500
501 /**
502 Multiplication of a position vector by real number a*v
503 */
504 template <class CoordSystem, class U>
505 inline
509 return v *= a;
510 // Note - passing v by value and using operator *= may save one
511 // copy relative to passing v by const ref and creating a temporary.
512 }
513
514 /**
515 Difference between two PositionVector3D vectors.
516 The result is a DisplacementVector3D.
517 The (coordinate system) type of the returned vector is defined to
518 be identical to that of the first position vector.
519 */
520
521 template <class CoordSystem1, class CoordSystem2, class U>
522 inline
523 DisplacementVector3D<CoordSystem1,U>
527 v1.X()-v2.X(), v1.Y()-v2.Y(),v1.Z()-v2.Z() )
528 );
529 }
530
531 /**
532 Addition of a PositionVector3D and a DisplacementVector3D.
533 The return type is a PositionVector3D,
534 of the same (coordinate system) type as the input PositionVector3D.
535 */
536 template <class CoordSystem1, class CoordSystem2, class U>
537 inline
538 PositionVector3D<CoordSystem2,U>
541 return p1 += v2;
542 }
543
544 /**
545 Addition of a DisplacementVector3D and a PositionVector3D.
546 The return type is a PositionVector3D,
547 of the same (coordinate system) type as the input PositionVector3D.
548 */
549 template <class CoordSystem1, class CoordSystem2, class U>
550 inline
551 PositionVector3D<CoordSystem2,U>
554 return p2 += v1;
555 }
556
557 /**
558 Subtraction of a DisplacementVector3D from a PositionVector3D.
559 The return type is a PositionVector3D,
560 of the same (coordinate system) type as the input PositionVector3D.
561 */
562 template <class CoordSystem1, class CoordSystem2, class U>
563 inline
564 PositionVector3D<CoordSystem2,U>
567 return p1 -= v2;
568 }
569
570 // Scaling of a position vector with a real number is not physically meaningful
571
572 // ------------- I/O to/from streams -------------
573
574 template <
575 class char_t, class traits_t, class T, class U,
576 typename std::enable_if<std::is_arithmetic<typename PositionVector3D<T, U>::Scalar>::value>::type * = nullptr>
577 std::basic_ostream<char_t, traits_t> &operator<<(std::basic_ostream<char_t, traits_t> &os,
578 PositionVector3D<T, U> const &v)
579 {
580 if (os) {
581
582 typename T::Scalar a = 0;
583 typename T::Scalar b = 0;
584 typename T::Scalar c = 0;
585 v.GetCoordinates(a, b, c);
586
589 typedef GenVector_detail::BitReproducible BR;
590 BR::Output(os, a);
591 BR::Output(os, b);
592 BR::Output(os, c);
593 } else {
596 }
597 }
598 return os;
599 } // op<< <>()
600
601 template <
602 class char_t, class traits_t, class T, class U,
603 typename std::enable_if<!std::is_arithmetic<typename PositionVector3D<T, U>::Scalar>::value>::type * = nullptr>
604 std::basic_ostream<char_t, traits_t> &operator<<(std::basic_ostream<char_t, traits_t> &os,
605 PositionVector3D<T, U> const &v)
606 {
607 if (os) {
608 os << "{ ";
609 for (std::size_t i = 0; i < PositionVector3D<T, U>::Scalar::Size; ++i) {
610 os << "(" << v.x()[i] << "," << v.y()[i] << "," << v.z()[i] << ") ";
611 }
612 os << "}";
613 }
614 return os;
615 } // op<< <>()
616
617 template< class char_t, class traits_t, class T, class U >
618 inline
619 std::basic_istream<char_t,traits_t> &
620 operator >> ( std::basic_istream<char_t,traits_t> & is
622 )
623 {
624 if( !is ) return is;
625
626 typename T::Scalar a, b, c;
627
631 BR::Input(is, a);
632 BR::Input(is, b);
633 BR::Input(is, c);
634 }
635 else {
637 detail::require_delim( is, detail::sep ); is >> b;
638 detail::require_delim( is, detail::sep ); is >> c;
640 }
641
642 if( is )
643 v.SetCoordinates(a, b, c);
644 return is;
645
646 } // op>> <>()
647
648
649
650
651 } // namespace Math
652
653} // namespace ROOT
654
655
656#endif /* ROOT_Math_GenVector_PositionVector3D */
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
int type
Definition TGX11.cxx:121
Class describing a 3D cartesian coordinate system (x, y, z coordinates)
Definition Cartesian3D.h:44
Class describing a generic displacement vector in 3 dimensions.
Class describing a generic position vector (point) in 3 dimensions.
PositionVector3D & operator+=(const DisplacementVector3D< OtherCoords, Tag > &v)
Self Addition with a displacement vector.
PositionVector3D(const PositionVector3D< OtherCoords, OtherTag > &)
PositionVector3D< CoordSystem, Tag > & SetZ(Scalar zz)
Change Z - Cartesian3D coordinates only.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
PositionVector3D(const DisplacementVector3D< OtherCoords, OtherTag > &)
PositionVector3D(const ForeignVector &v)
Construct from a foreign 3D vector type, for example, Hep3Vector Precondition: v must implement metho...
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
PositionVector3D< CoordSystem, Tag > & SetR(Scalar rr)
Change R - Polar3D coordinates only.
bool operator!=(const PositionVector3D &rhs) const
void GetCoordinates(Scalar dest[]) const
get internal data into a C-style array of 3 Scalar numbers
PositionVector3D< CoordSystem, Tag > & SetEta(Scalar etaval)
Change Eta - CylindricalEta3D coordinates only.
void GetCoordinates(IT begin, IT end) const
get internal data into 3 Scalars at *begin to *end (3 past begin)
PositionVector3D & operator-=(const DisplacementVector3D< OtherCoords, OtherTag > &)
PositionVector3D< CoordSystem, Tag > & SetPhi(Scalar ang)
Change Phi - Polar3D or CylindricalEta3D coordinates.
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
PositionVector3D & operator-=(const DisplacementVector3D< OtherCoords, Tag > &v)
Self Difference with a displacement vector.
PositionVector3D< CoordSystem, Tag > & SetRho(Scalar rr)
Change Rho - CylindricalEta3D coordinates only.
PositionVector3D operator*(Scalar a) const
Multiply a vector by a real number.
PositionVector3D & operator=(const DisplacementVector3D< OtherCoords, OtherTag > &)
PositionVector3D & operator=(const PositionVector3D< OtherCoords, Tag > &v)
Assignment operator from a position vector of arbitrary type.
PositionVector3D< CoordSystem, Tag > & SetCoordinates(const Scalar src[])
Set internal data based on a C-style array of 3 Scalar numbers.
PositionVector3D & operator+=(const DisplacementVector3D< OtherCoords, OtherTag > &)
PositionVector3D< CoordSystem, Tag > & SetY(Scalar yy)
Change Y - Cartesian3D coordinates only.
PositionVector3D & operator/=(Scalar a)
divide this vector by a scalar quantity
Scalar Theta() const
Polar theta, converting if necessary from internal coordinate system.
PositionVector3D< CoordSystem, Tag > & SetXYZ(Scalar a, Scalar b, Scalar c)
set the values of the vector from the cartesian components (x,y,z) (if the vector is held in polar or...
PositionVector3D operator/(Scalar a) const
Division of a vector with a real number.
bool operator==(const PositionVector3D &rhs) const
Exact equality.
Scalar Phi() const
Polar phi, converting if necessary from internal coordinate system.
void GetCoordinates(Scalar &a, Scalar &b, Scalar &c) const
get internal data into 3 Scalar numbers
PositionVector3D< CoordSystem, Tag > & SetCoordinates(Scalar a, Scalar b, Scalar c)
Set internal data based on 3 Scalar numbers.
PositionVector3D(const DisplacementVector3D< T, Tag > &p)
Construct from an arbitrary displacement vector.
Scalar Rho() const
Cylindrical transverse component rho.
PositionVector3D Cross(const DisplacementVector3D< OtherCoords, Tag > &v) const
Return vector (Cross) product of this point with a displacement, as a point vector in this coordinate...
PositionVector3D & operator=(const PositionVector3D< OtherCoords, OtherTag > &)
Scalar Perp2() const
Transverse component squared (rho^2 in cylindrical coordinates.
Scalar Mag2() const
Magnitute squared ( r^2 in spherical coordinate)
PositionVector3D< CoordSystem, Tag > & SetTheta(Scalar ang)
Change Theta - Polar3D coordinates only.
PositionVector3D & operator*=(Scalar a)
multiply this vector by a scalar quantity
PositionVector3D()
Default constructor.
Scalar Eta() const
Polar eta, converting if necessary from internal coordinate system.
Scalar Dot(const DisplacementVector3D< OtherCoords, Tag > &v) const
Return the scalar (Dot) product of this with a displacement vector in any coordinate system,...
PositionVector3D< CoordSystem, Tag > & SetCoordinates(IT begin, IT end)
Set internal data based on 3 Scalars at *begin to *end.
PositionVector3D(const Scalar &a, const Scalar &b, const Scalar &c)
Construct from three values of type Scalar.
PositionVector3D< CoordSystem, Tag > & SetX(Scalar xx)
Change X - Cartesian3D coordinates only.
void GetCoordinates(IT begin) const
get internal data into 3 Scalars at *begin
PositionVector3D(const PositionVector3D< T, Tag > &v)
Construct from a position vector expressed in different coordinates, or using a different Scalar type...
const CoordSystem & Coordinates() const
Retrieve a copy of the coordinates object.
Scalar R() const
Polar R, converting if necessary from internal coordinate system.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
double T(double x)
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)
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
#define dest(otri, vertexptr)
Definition triangle.c:1041