1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#ifndef GeometryVector_Geom_CoordinateSets_h
#define GeometryVector_Geom_CoordinateSets_h
#include <cmath>
namespace Geom {
/** Converts polar 2D coordinates to cartesian coordinates.
* Note: Spherical coordinates (also sometimes called Polar 3D coordinates)
* are handled by class Spherical2Cartesian
*/
template <typename T>
class Polar2Cartesian {
public:
/// Construct from radius and polar angle
Polar2Cartesian(const T& r, const T& phi) : r_(r), phi_(phi) {}
const T& r() const { return r_; }
const T& phi() const { return phi_; }
T x() const { return r_ * cos(phi_); }
T y() const { return r_ * sin(phi_); }
private:
T r_;
T phi_;
};
/** Converts cylindtical coordinates to cartesian coordinates.
*/
template <typename T>
class Cylindrical2Cartesian {
public:
/** Construct from radius, azimuthal angle, and z component.
* The radius in the cylindrical frame is the transverse component.
*/
Cylindrical2Cartesian(const T& r, const T& phi, const T& z) : r_(r), phi_(phi), z_(z) {}
const T& r() const { return r_; }
const T& phi() const { return phi_; }
const T& z() const { return z_; }
T x() const { return r_ * cos(phi_); }
T y() const { return r_ * sin(phi_); }
private:
T r_;
T phi_;
T z_;
};
/** Converts spherical (or polar 3D) coordinates to cartesian coordinates.
*/
template <typename T>
class Spherical2Cartesian {
public:
/** Construct from polar angle, azimuthal angle, and radius.
* The radius in the spherical frame is the magnitude of the vector.
*/
Spherical2Cartesian(const T& theta, const T& phi, const T& mag)
: theta_(theta), phi_(phi), r_(mag), transv_(sin(theta) * mag) {}
const T& theta() const { return theta_; }
const T& phi() const { return phi_; }
const T& r() const { return r_; }
T x() const { return transv_ * cos(phi()); }
T y() const { return transv_ * sin(phi()); }
T z() const { return cos(theta()) * r(); }
private:
T theta_;
T phi_;
T r_;
T transv_;
};
/** Cartesian coordinate set, for uniformity with other coordinate systems
*/
template <typename T>
class Cartesian2Cartesian3D {
public:
Cartesian2Cartesian3D(const T& x, const T& y, const T& z) : x_(x), y_(y), z_(z) {}
const T& x() const { return x_; }
const T& y() const { return y_; }
const T& z() const { return z_; }
private:
T x_;
T y_;
T z_;
};
} // namespace Geom
#endif
|