CMS 3D CMS Logo

oldTkRotation.h
Go to the documentation of this file.
1 #ifndef Geom_oldTkRotation_H
2 #define Geom_oldTkRotation_H
3 
7 /*
8 #include "DataFormats/GeometrySurface/interface/GlobalError.h"
9 #include "DataFormats/GeometrySurface/interface/LocalError.h"
10 */
11 #include <iosfwd>
12 
13 template <class T> class TkRotation;
14 template <class T> class TkRotation2D;
15 
16 template <class T>
17 std::ostream & operator<<( std::ostream& s, const TkRotation<T>& r);
18 template <class T>
19 std::ostream & operator<<( std::ostream& s, const TkRotation2D<T>& r);
20 
21 namespace geometryDetails {
22  void TkRotationErr1();
23  void TkRotationErr2();
24 
25 }
26 
27 
31 template <class T>
32 class TkRotation {
33 public:
35 
37  R11( 1), R12( 0), R13( 0),
38  R21( 0), R22( 1), R23( 0),
39  R31( 0), R32( 0), R33( 1) {}
40 
41  TkRotation( T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz) :
42  R11(xx), R12(xy), R13(xz),
43  R21(yx), R22(yy), R23(yz),
44  R31(zx), R32(zy), R33(zz) {}
45 
46  TkRotation( const T* p) :
47  R11(p[0]), R12(p[1]), R13(p[2]),
48  R21(p[3]), R22(p[4]), R23(p[5]),
49  R31(p[6]), R32(p[7]), R33(p[8]) {}
50 
51  TkRotation( const GlobalVector & aX, const GlobalVector & aY) {
52 
53  GlobalVector uX = aX.unit();
54  GlobalVector uY = aY.unit();
55  GlobalVector uZ(uX.cross(uY));
56 
57  R11 = uX.x(); R12 = uX.y(); R13 = uX.z();
58  R21 = uY.x(); R22 = uY.y(); R23 = uY.z();
59  R31 = uZ.x(); R32 = uZ.y(); R33 = uZ.z();
60 
61  }
62 
67  TkRotation( const GlobalVector & aX, const GlobalVector & aY,
68  const GlobalVector & aZ) :
69  R11( aX.x()), R12( aX.y()), R13( aX.z()),
70  R21( aY.x()), R22( aY.y()), R23( aY.z()),
71  R31( aZ.x()), R32( aZ.y()), R33( aZ.z()) {}
72 
73 
82  TkRotation( const Basic3DVector<T>& axis, T phi) :
83  R11( cos(phi) ), R12( sin(phi)), R13( 0),
84  R21( -sin(phi)), R22( cos(phi)), R23( 0),
85  R31( 0), R32( 0), R33( 1) {
86 
87  //rotation around the z-axis by phi
88  TkRotation tmpRotz ( cos(axis.phi()), sin(axis.phi()), 0.,
89  -sin(axis.phi()), cos(axis.phi()), 0.,
90  0., 0., 1. );
91  //rotation around y-axis by theta
92  TkRotation tmpRoty ( cos(axis.theta()), 0.,-sin(axis.theta()),
93  0., 1., 0.,
94  sin(axis.theta()), 0., cos(axis.theta()) );
95  (*this)*=tmpRoty;
96  (*this)*=tmpRotz; // = this * tmpRoty * tmpRotz
97 
98  // (tmpRoty * tmpRotz)^-1 * this * tmpRoty * tmpRotz
99 
100  *this = (tmpRoty*tmpRotz).multiplyInverse(*this);
101 
102  }
103  /* this is the same thing...derived from the CLHEP ... it gives the
104  same results MODULO the sign of the rotation.... but I don't want
105  that... had
106  TkRotation (const Basic3DVector<T>& axis, float phi) {
107  T cx = axis.x();
108  T cy = axis.y();
109  T cz = axis.z();
110 
111  T ll = axis.mag();
112  if (ll == 0) {
113  geometryDetails::TkRotationErr1();
114  }else{
115 
116  float cosa = cos(phi), sina = sin(phi);
117  cx /= ll; cy /= ll; cz /= ll;
118 
119  R11 = cosa + (1-cosa)*cx*cx;
120  R12 = (1-cosa)*cx*cy - sina*cz;
121  R13 = (1-cosa)*cx*cz + sina*cy;
122 
123  R21 = (1-cosa)*cy*cx + sina*cz;
124  R22 = cosa + (1-cosa)*cy*cy;
125  R23 = (1-cosa)*cy*cz - sina*cx;
126 
127  R31 = (1-cosa)*cz*cx - sina*cy;
128  R32 = (1-cosa)*cz*cy + sina*cx;
129  R33 = cosa + (1-cosa)*cz*cz;
130 
131  }
132 
133  }
134  */
135 
136  template <typename U>
138  R11(a.xx()), R12(a.xy()), R13(a.xz()),
139  R21(a.yx()), R22(a.yy()), R23(a.yz()),
140  R31(a.zx()), R32(a.zy()), R33(a.zz()) {}
141 
143  return TkRotation( R11, R21, R31,
144  R12, R22, R32,
145  R13, R23, R33);
146  }
147 
149  return rotate(v);
150  }
151 
153  return Basic3DVector<T>( R11*v.x() + R12*v.y() + R13*v.z(),
154  R21*v.x() + R22*v.y() + R23*v.z(),
155  R31*v.x() + R32*v.y() + R33*v.z());
156  }
157 
159  return rotateBack(v);
160  }
161 
163  return Basic3DVector<T>( R11*v.x() + R21*v.y() + R31*v.z(),
164  R12*v.x() + R22*v.y() + R32*v.z(),
165  R13*v.x() + R23*v.y() + R33*v.z());
166  }
167 
168 
170  return Basic3DVector<T>( R11*v.x() + R12*v.y(),
171  R21*v.x() + R22*v.y(),
172  R31*v.x() + R32*v.y());
173  }
175  return Basic3DVector<T>( R11*v.x() + R21*v.y(),
176  R12*v.x() + R22*v.y(),
177  R13*v.x() + R23*v.y());
178  }
179 
180 
181 
182  TkRotation operator*( const TkRotation& b) const {
183  return TkRotation(R11*b.R11 + R12*b.R21 + R13*b.R31,
184  R11*b.R12 + R12*b.R22 + R13*b.R32,
185  R11*b.R13 + R12*b.R23 + R13*b.R33,
186  R21*b.R11 + R22*b.R21 + R23*b.R31,
187  R21*b.R12 + R22*b.R22 + R23*b.R32,
188  R21*b.R13 + R22*b.R23 + R23*b.R33,
189  R31*b.R11 + R32*b.R21 + R33*b.R31,
190  R31*b.R12 + R32*b.R22 + R33*b.R32,
191  R31*b.R13 + R32*b.R23 + R33*b.R33);
192  }
193 
195  return TkRotation(R11*b.R11 + R21*b.R21 + R31*b.R31,
196  R11*b.R12 + R21*b.R22 + R31*b.R32,
197  R11*b.R13 + R21*b.R23 + R31*b.R33,
198  R12*b.R11 + R22*b.R21 + R32*b.R31,
199  R12*b.R12 + R22*b.R22 + R32*b.R32,
200  R12*b.R13 + R22*b.R23 + R32*b.R33,
201  R13*b.R11 + R23*b.R21 + R33*b.R31,
202  R13*b.R12 + R23*b.R22 + R33*b.R32,
203  R13*b.R13 + R23*b.R23 + R33*b.R33);
204  }
205 
207  return *this = operator * (b);
208  }
209 
210  // Note a *= b; <=> a = a * b; while a.transform(b); <=> a = b * a;
212  return *this = b.operator * (*this);
213  }
214 
216  const Basic3DVector<T>& newY,
217  const Basic3DVector<T>& newZ) {
218  T del = 0.001;
219 
220  if (
221 
222  // the check for right-handedness is not needed since
223  // we want to change the handedness when it's left in cmsim
224  //
225  // fabs(newZ.x()-w.x()) > del ||
226  // fabs(newZ.y()-w.y()) > del ||
227  // fabs(newZ.z()-w.z()) > del ||
228  fabs(newX.mag2()-1.) > del ||
229  fabs(newY.mag2()-1.) > del ||
230  fabs(newZ.mag2()-1.) > del ||
231  fabs(newX.dot(newY)) > del ||
232  fabs(newY.dot(newZ)) > del ||
233  fabs(newZ.dot(newX)) > del) {
235  return *this;
236  } else {
237  return transform(TkRotation(newX.x(), newY.x(), newZ.x(),
238  newX.y(), newY.y(), newZ.y(),
239  newX.z(), newY.z(), newZ.z()));
240  }
241  }
242 
243  Basic3DVector<T> x() const { return Basic3DVector<T>(xx(),xy(),xz());}
244  Basic3DVector<T> y() const { return Basic3DVector<T>(yx(),yy(),yz());}
245  Basic3DVector<T> z() const { return Basic3DVector<T>(zx(),zy(),zz());}
246 
247 
248  T const &xx() const { return R11;}
249  T const &xy() const { return R12;}
250  T const &xz() const { return R13;}
251  T const &yx() const { return R21;}
252  T const &yy() const { return R22;}
253  T const &yz() const { return R23;}
254  T const &zx() const { return R31;}
255  T const &zy() const { return R32;}
256  T const &zz() const { return R33;}
257 
258 private:
259 
263 };
264 
265 
266 template<>
267 std::ostream & operator<< <float>( std::ostream& s, const TkRotation<float>& r);
268 
269 template<>
270 std::ostream & operator<< <double>( std::ostream& s, const TkRotation<double>& r);
271 
272 
273 template <class T, class U>
275  return Basic3DVector<U>( r.xx()*v.x() + r.xy()*v.y() + r.xz()*v.z(),
276  r.yx()*v.x() + r.yy()*v.y() + r.yz()*v.z(),
277  r.zx()*v.x() + r.zy()*v.y() + r.zz()*v.z());
278 }
279 
280 template <class T, class U>
284  return RT( a.xx()*b.xx() + a.xy()*b.yx() + a.xz()*b.zx(),
285  a.xx()*b.xy() + a.xy()*b.yy() + a.xz()*b.zy(),
286  a.xx()*b.xz() + a.xy()*b.yz() + a.xz()*b.zz(),
287  a.yx()*b.xx() + a.yy()*b.yx() + a.yz()*b.zx(),
288  a.yx()*b.xy() + a.yy()*b.yy() + a.yz()*b.zy(),
289  a.yx()*b.xz() + a.yy()*b.yz() + a.yz()*b.zz(),
290  a.zx()*b.xx() + a.zy()*b.yx() + a.zz()*b.zx(),
291  a.zx()*b.xy() + a.zy()*b.yy() + a.zz()*b.zy(),
292  a.zx()*b.xz() + a.zy()*b.yz() + a.zz()*b.zz());
293 }
294 
295 
296 template <class T>
297 class TkRotation2D {
298 public:
299 
301 
302 
303 
305 
306  TkRotation2D( T xx, T xy, T yx, T yy) {
307  axis[0] = BasicVector(xx,xy);
308  axis[1] =BasicVector(yx, yy);
309  }
310 
311  TkRotation2D( const T* p) {
312  axis[0] = BasicVector(p[0],p[1]);
313  axis[1] = BasicVector(p[2],p[3]);
314  }
315 
316  TkRotation2D( const BasicVector & aX) {
317 
318  BasicVector uX = aX.unit();
319  BasicVector uY(-uX.y(),uX.x());
320 
321  axis[0]= uX;
322  axis[1]= uY;
323 
324  }
325 
326 
327  TkRotation2D( const BasicVector & uX, const BasicVector & uY) {
328  axis[0]= uX;
329  axis[1]= uY;
330  }
331 
332  BasicVector x() const { return axis[0];}
333  BasicVector y() const { return axis[1];}
334 
335 
337  return TkRotation2D(axis[0][0], axis[1][0],
338  axis[0][1], axis[1][1]
339  );
340  }
341 
342  BasicVector rotate( const BasicVector& v) const {
343  return transposed().rotateBack(v);
344  }
345 
347  return v[0]*axis[0] + v[1]*axis[1];
348  }
349 
350 
351 
352  private:
353 
355 
356 };
357 
358 
359 template<>
360 std::ostream & operator<< <float>( std::ostream& s, const TkRotation2D<float>& r);
361 
362 template<>
363 std::ostream & operator<< <double>( std::ostream& s, const TkRotation2D<double>& r);
364 
365 
366 #endif
367 
368 
369 
370 
TkRotation(const GlobalVector &aX, const GlobalVector &aY)
Definition: oldTkRotation.h:51
BasicVector rotateBack(const BasicVector &v) const
Vector3DBase< typename PreciseFloatType< T, U >::Type, FrameTag > cross(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:110
T const & zz() const
T x() const
Cartesian x coordinate.
T xx() const
TkRotation2D(const T *p)
TkRotation2D(const BasicVector &uX, const BasicVector &uY)
TkRotation(const GlobalVector &aX, const GlobalVector &aY, const GlobalVector &aZ)
Definition: oldTkRotation.h:67
T z() const
Definition: PV3DBase.h:61
TkRotation(const Basic3DVector< T > &axis, T phi)
Definition: oldTkRotation.h:82
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 const & xx() const
Vector3DBase< T, GlobalTag > GlobalVector
Definition: oldTkRotation.h:34
T yy() const
T const & yx() const
T y() const
Cartesian y coordinate.
T yz() const
BasicVector x() const
T y() const
Cartesian y coordinate.
TkRotation(const TkRotation< U > &a)
Geom::Theta< T > theta() const
TkRotation & rotateAxes(const Basic3DVector< T > &newX, const Basic3DVector< T > &newY, const Basic3DVector< T > &newZ)
T const & xz() const
T zx() const
T x() const
Definition: PV3DBase.h:59
TkRotation transposed() const
T y() const
Definition: PV3DBase.h:60
Basic3DVector< T > x() const
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: oldTkRotation.h:41
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
TkRotation2D transposed() const
Basic3DVector< T > z() const
T const & xy() const
Basic2DVector unit() const
void TkRotationErr1()
Definition: TkRotation.cc:29
BasicVector axis[2]
Basic3DVector< T > rotateBack(const Basic3DVector< T > &v) const
T const & yz() const
TkRotation & operator*=(const TkRotation &b)
T const & zx() const
T z() const
Cartesian z coordinate.
Basic3DVector< T > multiplyInverse(const Basic3DVector< T > &v) const
double b
Definition: hdecay.h:120
std::ostream & operator<<< float >(std::ostream &s, const TkRotation< float > &r)
Definition: TkRotation.cc:5
T const & yy() const
Basic3DVector< U > operator*(const TkRotation< T > &r, const Basic3DVector< U > &v)
T const & zy() const
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:121
std::ostream & operator<<< double >(std::ostream &s, const TkRotation< double > &r)
Definition: TkRotation.cc:12
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)
Geom::Phi< T > phi() const
Vector3DBase unit() const
Definition: Vector3DBase.h:54
TkRotation(const T *p)
Definition: oldTkRotation.h:46
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