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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#ifndef Geom_GloballyPositioned_H
#define Geom_GloballyPositioned_H
#include "DataFormats/GeometryVector/interface/Point3DBase.h"
#include "DataFormats/GeometryVector/interface/Vector3DBase.h"
#include "DataFormats/GeometryVector/interface/LocalTag.h"
#include "DataFormats/GeometryVector/interface/GlobalTag.h"
#include "DataFormats/GeometrySurface/interface/TkRotation.h"
/** Base class for surfaces and volumes positioned in global 3D space.
* This class defines a cartesian reference frame, called in the
* following the local frame.
* It provides position, orientation, and frame transformations for
* points and vectors.
*/
template <class T>
class GloballyPositioned {
public:
typedef T Scalar;
typedef Point3DBase<T, GlobalTag> PositionType;
typedef TkRotation<T> RotationType;
typedef Point3DBase<T, GlobalTag> GlobalPoint;
typedef Point3DBase<T, LocalTag> LocalPoint;
typedef Vector3DBase<T, GlobalTag> GlobalVector;
typedef Vector3DBase<T, LocalTag> LocalVector;
static T iniPhi() { return 999.9978; }
static T iniEta() { return 999.9978; }
GloballyPositioned() { setCache(); }
GloballyPositioned(const PositionType& pos, const RotationType& rot) : thePos(pos), theRot(rot) { setCache(); }
virtual ~GloballyPositioned() {}
const PositionType& position() const { return thePos; }
const RotationType& rotation() const { return theRot; }
T phi() const { return thePhi; }
T eta() const { return theEta; }
// multiply inverse is faster
class ToLocal {
public:
ToLocal(GloballyPositioned const& frame) : thePos(frame.position()), theRot(frame.rotation().transposed()) {}
LocalPoint operator()(const GlobalPoint& gp) const { return toLocal(gp); }
LocalVector operator()(const GlobalVector& gv) const { return toLocal(gv); }
LocalPoint toLocal(const GlobalPoint& gp) const {
return LocalPoint(theRot.multiplyInverse(gp.basicVector() - thePos.basicVector()));
}
LocalVector toLocal(const GlobalVector& gv) const { return LocalVector(theRot.multiplyInverse(gv.basicVector())); }
// private:
PositionType thePos;
RotationType theRot;
};
/** Transform a local point (i.e. a point with coordinates in the
* local frame) to the global frame
*/
GlobalPoint toGlobal(const LocalPoint& lp) const {
return GlobalPoint(rotation().multiplyInverse(lp.basicVector()) + position().basicVector());
}
/** Transform a local point with different float precision from the
* one of the reference frame, and return a global point with the
* same precision as the input one.
*/
template <class U>
Point3DBase<U, GlobalTag> toGlobal(const Point3DBase<U, LocalTag>& lp) const {
return Point3DBase<U, GlobalTag>(rotation().multiplyInverse(lp.basicVector()) + position().basicVector());
}
/** Transform a local vector (i.e. a vector with coordinates in the
* local frame) to the global frame
*/
GlobalVector toGlobal(const LocalVector& lv) const {
return GlobalVector(rotation().multiplyInverse(lv.basicVector()));
}
/** Transform a local vector with different float precision from the
* one of the reference frame, and return a global vector with the
* same precision as the input one.
*/
template <class U>
Vector3DBase<U, GlobalTag> toGlobal(const Vector3DBase<U, LocalTag>& lv) const {
return Vector3DBase<U, GlobalTag>(rotation().multiplyInverse(lv.basicVector()));
}
/** Transform a global point (i.e. a point with coordinates in the
* global frame) to the local frame
*/
LocalPoint toLocal(const GlobalPoint& gp) const {
return LocalPoint(rotation() * (gp.basicVector() - position().basicVector()));
}
/** Transform a global point with different float precision from the
* one of the reference frame, and return a local point with the
* same precision as the input one.
*/
template <class U>
Point3DBase<U, LocalTag> toLocal(const Point3DBase<U, GlobalTag>& gp) const {
return Point3DBase<U, LocalTag>(rotation() * (gp.basicVector() - position().basicVector()));
}
/** Transform a global vector (i.e. a vector with coordinates in the
* global frame) to the local frame
*/
LocalVector toLocal(const GlobalVector& gv) const { return LocalVector(rotation() * gv.basicVector()); }
/** Transform a global vector with different float precision from the
* one of the reference frame, and return a local vector with the
* same precision as the input one.
*/
template <class U>
Vector3DBase<U, LocalTag> toLocal(const Vector3DBase<U, GlobalTag>& gv) const {
return Vector3DBase<U, LocalTag>(rotation() * gv.basicVector());
}
/** Move the position of the frame in the global frame.
* Useful e.g. for alignment.
*/
void move(const GlobalVector& displacement) {
thePos += displacement;
setCache();
}
/** Rotate the frame in the global frame.
* Useful e.g. for alignment.
*/
void rotate(const RotationType& rotation) {
theRot *= rotation;
setCache();
}
private:
PositionType thePos;
RotationType theRot;
/*
void resetCache() {
if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
thePhi = theEta = 0.; // avoid FPE
} else {
thePhi = iniPhi();
theEta = iniEta();
}
}
*/
void setCache() {
if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
thePhi = theEta = 0.; // avoid FPE
} else {
thePhi = thePos.barePhi();
theEta = thePos.eta();
}
}
T thePhi;
T theEta;
};
#endif
|