CMS 3D CMS Logo

sseTkRotation.h
Go to the documentation of this file.
1 #ifndef Geom_newTkRotation_H
2 #define Geom_newTkRotation_H
3 
7 
9 
10 #include <iosfwd>
11 
12 template <class T>
13 class TkRotation;
14 template <class T>
15 class TkRotation2D;
16 
17 template <class T>
18 std::ostream& operator<<(std::ostream& s, const TkRotation<T>& r);
19 template <class T>
20 std::ostream& operator<<(std::ostream& s, const TkRotation2D<T>& r);
21 
22 namespace geometryDetails {
23  void TkRotationErr1();
24  void TkRotationErr2();
25 
26 } // namespace geometryDetails
27 
31 template <class T>
32 class TkRotation {
33 public:
36 
38  TkRotation(mathSSE::Rot3<T> const& irot) : rot(irot) {}
39 
40  TkRotation(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz) : rot(xx, xy, xz, yx, yy, yz, zx, zy, zz) {}
41 
42  TkRotation(const T* p) : rot(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]) {}
43 
44  TkRotation(const GlobalVector& aX, const GlobalVector& aY) {
45  GlobalVector uX = aX.unit();
46  GlobalVector uY = aY.unit();
47  GlobalVector uZ(uX.cross(uY));
48 
49  rot.axis[0] = uX.basicVector().v;
50  rot.axis[1] = uY.basicVector().v;
51  rot.axis[2] = uZ.basicVector().v;
52  }
53 
54  TkRotation(const BasicVector& aX, const BasicVector& aY) {
55  BasicVector uX = aX.unit();
56  BasicVector uY = aY.unit();
57  BasicVector uZ(uX.cross(uY));
58 
59  rot.axis[0] = uX.v;
60  rot.axis[1] = uY.v;
61  rot.axis[2] = uZ.v;
62  }
63 
68  TkRotation(const GlobalVector& uX, const GlobalVector& uY, const GlobalVector& uZ) {
69  rot.axis[0] = uX.basicVector().v;
70  rot.axis[1] = uY.basicVector().v;
71  rot.axis[2] = uZ.basicVector().v;
72  }
73 
74  TkRotation(const BasicVector& uX, const BasicVector& uY, const BasicVector& uZ) {
75  rot.axis[0] = uX.v;
76  rot.axis[1] = uY.v;
77  rot.axis[2] = uZ.v;
78  }
79 
88  TkRotation(const Basic3DVector<T>& axis, T phi) : rot(cos(phi), sin(phi), 0, -sin(phi), cos(phi), 0, 0, 0, 1) {
89  //rotation around the z-axis by phi
90  TkRotation tmpRotz(cos(axis.phi()), sin(axis.phi()), 0., -sin(axis.phi()), cos(axis.phi()), 0., 0., 0., 1.);
91  //rotation around y-axis by theta
92  TkRotation tmpRoty(cos(axis.theta()), 0., -sin(axis.theta()), 0., 1., 0., sin(axis.theta()), 0., cos(axis.theta()));
93  (*this) *= tmpRoty;
94  (*this) *= tmpRotz; // = this * tmpRoty * tmpRotz
95 
96  // (tmpRoty * tmpRotz)^-1 * this * tmpRoty * tmpRotz
97 
98  *this = (tmpRoty * tmpRotz).multiplyInverse(*this);
99  }
100  /* this is the same thing...derived from the CLHEP ... it gives the
101  same results MODULO the sign of the rotation.... but I don't want
102  that... had
103  TkRotation (const Basic3DVector<T>& axis, float phi) {
104  T cx = axis.x();
105  T cy = axis.y();
106  T cz = axis.z();
107 
108  T ll = axis.mag();
109  if (ll == 0) {
110  geometryDetails::TkRotationErr1();
111  }else{
112 
113  float cosa = cos(phi), sina = sin(phi);
114  cx /= ll; cy /= ll; cz /= ll;
115 
116  R11 = cosa + (1-cosa)*cx*cx;
117  R12 = (1-cosa)*cx*cy - sina*cz;
118  R13 = (1-cosa)*cx*cz + sina*cy;
119 
120  R21 = (1-cosa)*cy*cx + sina*cz;
121  R22 = cosa + (1-cosa)*cy*cy;
122  R23 = (1-cosa)*cy*cz - sina*cx;
123 
124  R31 = (1-cosa)*cz*cx - sina*cy;
125  R32 = (1-cosa)*cz*cy + sina*cx;
126  R33 = cosa + (1-cosa)*cz*cz;
127 
128  }
129 
130  }
131  */
132 
133  template <typename U>
134  TkRotation(const TkRotation<U>& a) : rot(a.xx(), a.xy(), a.xz(), a.yx(), a.yy(), a.yz(), a.zx(), a.zy(), a.zz()) {}
135 
136  TkRotation transposed() const { return rot.transpose(); }
137 
138  Basic3DVector<T> rotate(const Basic3DVector<T>& v) const { return rot.rotate(v.v); }
139 
140  Basic3DVector<T> rotateBack(const Basic3DVector<T>& v) const { return rot.rotateBack(v.v); }
141 
142  Basic3DVector<T> operator*(const Basic3DVector<T>& v) const { return rot.rotate(v.v); }
143 
144  Basic3DVector<T> multiplyInverse(const Basic3DVector<T>& v) const { return rot.rotateBack(v.v); }
145 
146  template <class Scalar>
148  return Basic3DVector<Scalar>(xx() * v.x() + yx() * v.y() + zx() * v.z(),
149  xy() * v.x() + yy() * v.y() + zy() * v.z(),
150  xz() * v.x() + yz() * v.y() + zz() * v.z());
151  }
152 
154  return Basic3DVector<T>(xx() * v.x() + xy() * v.y(), yx() * v.x() + yy() * v.y(), zx() * v.x() + zy() * v.y());
155  }
157  return Basic3DVector<T>(xx() * v.x() + yx() * v.y(), xy() * v.x() + yy() * v.y(), xz() * v.x() + yz() * v.y());
158  }
159 
160  TkRotation operator*(const TkRotation& b) const { return rot * b.rot; }
161  TkRotation multiplyInverse(const TkRotation& b) const { return rot.transpose() * b.rot; }
162 
163  TkRotation& operator*=(const TkRotation& b) { return *this = operator*(b); }
164 
165  // Note a *= b; <=> a = a * b; while a.transform(b); <=> a = b * a;
166  TkRotation& transform(const TkRotation& b) { return *this = b.operator*(*this); }
167 
168  TkRotation& rotateAxes(const Basic3DVector<T>& newX, const Basic3DVector<T>& newY, const Basic3DVector<T>& newZ) {
169  T del = 0.001;
170 
171  if (
172 
173  // the check for right-handedness is not needed since
174  // we want to change the handedness when it's left in cmsim
175  //
176  // fabs(newZ.x()-w.x()) > del ||
177  // fabs(newZ.y()-w.y()) > del ||
178  // fabs(newZ.z()-w.z()) > del ||
179  fabs(newX.mag2() - 1.) > del || fabs(newY.mag2() - 1.) > del || fabs(newZ.mag2() - 1.) > del ||
180  fabs(newX.dot(newY)) > del || fabs(newY.dot(newZ)) > del || fabs(newZ.dot(newX)) > del) {
182  return *this;
183  } else {
184  return transform(
185  TkRotation(newX.x(), newY.x(), newZ.x(), newX.y(), newY.y(), newZ.y(), newX.z(), newY.z(), newZ.z()));
186  }
187  }
188 
189  Basic3DVector<T> x() const { return rot.axis[0]; }
190  Basic3DVector<T> y() const { return rot.axis[1]; }
191  Basic3DVector<T> z() const { return rot.axis[2]; }
192 
193  T xx() const { return rot.axis[0].arr[0]; }
194  T xy() const { return rot.axis[0].arr[1]; }
195  T xz() const { return rot.axis[0].arr[2]; }
196  T yx() const { return rot.axis[1].arr[0]; }
197  T yy() const { return rot.axis[1].arr[1]; }
198  T yz() const { return rot.axis[1].arr[2]; }
199  T zx() const { return rot.axis[2].arr[0]; }
200  T zy() const { return rot.axis[2].arr[1]; }
201  T zz() const { return rot.axis[2].arr[2]; }
202 
203 private:
205 };
206 
207 template <>
208 std::ostream& operator<< <float>(std::ostream& s, const TkRotation<float>& r);
209 
210 template <>
211 std::ostream& operator<< <double>(std::ostream& s, const TkRotation<double>& r);
212 
213 template <class T, class U>
215  return Basic3DVector<U>(r.xx() * v.x() + r.xy() * v.y() + r.xz() * v.z(),
216  r.yx() * v.x() + r.yy() * v.y() + r.yz() * v.z(),
217  r.zx() * v.x() + r.zy() * v.y() + r.zz() * v.z());
218 }
219 
220 template <class T, class U>
223  return RT(a.xx() * b.xx() + a.xy() * b.yx() + a.xz() * b.zx(),
224  a.xx() * b.xy() + a.xy() * b.yy() + a.xz() * b.zy(),
225  a.xx() * b.xz() + a.xy() * b.yz() + a.xz() * b.zz(),
226  a.yx() * b.xx() + a.yy() * b.yx() + a.yz() * b.zx(),
227  a.yx() * b.xy() + a.yy() * b.yy() + a.yz() * b.zy(),
228  a.yx() * b.xz() + a.yy() * b.yz() + a.yz() * b.zz(),
229  a.zx() * b.xx() + a.zy() * b.yx() + a.zz() * b.zx(),
230  a.zx() * b.xy() + a.zy() * b.yy() + a.zz() * b.zy(),
231  a.zx() * b.xz() + a.zy() * b.yz() + a.zz() * b.zz());
232 }
233 
234 template <class T>
235 class TkRotation2D {
236 public:
238 
240  TkRotation2D(mathSSE::Rot2<T> const& irot) : rot(irot) {}
241 
242  TkRotation2D(T xx, T xy, T yx, T yy) : rot(xx, xy, yx, yy) {}
243 
244  TkRotation2D(const T* p) : rot(p[0], p[1], p[2], p[3]) {}
245 
247  BasicVector uX = aX.unit();
248  BasicVector uY(-uX.y(), uX.x());
249 
250  rot.axis[0] = uX.v;
251  rot.axis[1] = uY.v;
252  }
253 
254  TkRotation2D(const BasicVector& uX, const BasicVector& uY) {
255  rot.axis[0] = uX.v;
256  rot.axis[1] = uY.v;
257  }
258 
259  BasicVector x() const { return rot.axis[0]; }
260  BasicVector y() const { return rot.axis[1]; }
261 
262  TkRotation2D transposed() const { return rot.transpose(); }
263 
264  BasicVector rotate(const BasicVector& v) const { return rot.rotate(v.v); }
265 
266  BasicVector rotateBack(const BasicVector& v) const { return rot.rotateBack(v.v); }
267 
268 private:
270 };
271 
272 template <>
273 std::ostream& operator<< <float>(std::ostream& s, const TkRotation2D<float>& r);
274 
275 template <>
276 std::ostream& operator<< <double>(std::ostream& s, const TkRotation2D<double>& r);
277 
278 #endif
Basic3DVector< T > BasicVector
Definition: sseTkRotation.h:35
TkRotation(const GlobalVector &aX, const GlobalVector &aY)
Definition: sseTkRotation.h:44
BasicVector rotateBack(const BasicVector &v) const
Vector3DBase< typename PreciseFloatType< T, U >::Type, FrameTag > cross(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:110
TkRotation2D(mathSSE::Rot2< T > const &irot)
T x() const
Cartesian x coordinate.
T xx() const
TkRotation2D(const T *p)
TkRotation2D(const BasicVector &uX, const BasicVector &uY)
TkRotation(const Basic3DVector< T > &axis, T phi)
Definition: sseTkRotation.h:88
Basic3DVector< T > y() const
T xy() const
TkRotation & transform(const TkRotation &b)
Basic3DVector< T > rotate(const Basic3DVector< T > &v) const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T zz() const
T yy() const
T y() const
Cartesian y coordinate.
T yz() const
BasicVector x() const
mathSSE::Rot3< T > rot
T y() const
Cartesian y coordinate.
TkRotation(const TkRotation< U > &a)
Vector3DBase< T, GlobalTag > GlobalVector
Definition: sseTkRotation.h:34
Geom::Theta< T > theta() const
TkRotation & rotateAxes(const Basic3DVector< T > &newX, const Basic3DVector< T > &newY, const Basic3DVector< T > &newZ)
T zx() const
TkRotation transposed() const
Basic3DVector< T > x() const
mathSSE::Rot2< T > rot
std::ostream & operator<<< double >(std::ostream &s, const TkRotation< double > &r)
Definition: TkRotation.cc:12
Basic3DVector< T > operator*(const Basic3DVector< T > &v) const
TkRotation(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
Definition: sseTkRotation.h:40
Basic3DVector< T > multiplyInverse(const Basic2DVector< T > &v) const
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
T zy() const
void TkRotationErr2()
Definition: TkRotation.cc:30
Basic3DVector cross(const Basic3DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
TkRotation2D transposed() const
Basic3DVector< T > z() const
Basic3DVector< Scalar > multiplyInverse(const Basic3DVector< Scalar > &v) const
TkRotation(const BasicVector &aX, const BasicVector &aY)
Definition: sseTkRotation.h:54
TkRotation(mathSSE::Rot3< T > const &irot)
Definition: sseTkRotation.h:38
Basic2DVector unit() const
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:53
void TkRotationErr1()
Definition: TkRotation.cc:29
Rot2< T > rot
Basic3DVector< T > rotateBack(const Basic3DVector< T > &v) const
Basic3DVector< U > operator*(const TkRotation< T > &r, const Basic3DVector< U > &v)
TkRotation & operator*=(const TkRotation &b)
T z() const
Cartesian z coordinate.
Basic3DVector< T > multiplyInverse(const Basic3DVector< T > &v) const
double b
Definition: hdecay.h:118
TkRotation(const GlobalVector &uX, const GlobalVector &uY, const GlobalVector &uZ)
Definition: sseTkRotation.h:68
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
TkRotation2D(const BasicVector &aX)
TkRotation operator*(const TkRotation &b) const
double a
Definition: hdecay.h:119
Rot3< T > rot
Basic3DVector< T > operator*(const Basic2DVector< T > &v) const
BasicVector rotate(const BasicVector &v) const
BasicVector y() const
TkRotation2D(T xx, T xy, T yx, T yy)
Basic3DVector unit() const
Geom::Phi< T > phi() const
Vector3DBase unit() const
Definition: Vector3DBase.h:54
TkRotation(const T *p)
Definition: sseTkRotation.h:42
Basic2DVector< T > BasicVector
long double T
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
T xz() const
T x() const
Cartesian x coordinate.
T yx() const
TkRotation multiplyInverse(const TkRotation &b) const
std::ostream & operator<<< float >(std::ostream &s, const TkRotation< float > &r)
Definition: TkRotation.cc:5
TkRotation(const BasicVector &uX, const BasicVector &uY, const BasicVector &uZ)
Definition: sseTkRotation.h:74