CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
newTkRotation.h
Go to the documentation of this file.
1 #ifndef Geom_newTkRotation_H
2 #define Geom_newTkRotation_H
3 
7 
9 
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:
34 
37 
39  TkRotation( mathSSE::Rot3<T> const & irot ) : rot(irot){}
40 
41  TkRotation( T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz) :
42  rot(xx,xy,xz, yx,yy,yz, zx, zy,zz){}
43 
44  TkRotation( const T* p) :
45  rot(p[0],p[1],p[2],
46  p[3],p[4],p[5],
47  p[6],p[7],p[8]) {}
48 
49  TkRotation( const GlobalVector & aX, const GlobalVector & aY) {
50 
51  GlobalVector uX = aX.unit();
52  GlobalVector uY = aY.unit();
53  GlobalVector uZ(uX.cross(uY));
54 
55  rot.axis[0]= uX.basicVector().v;
56  rot.axis[1]= uY.basicVector().v;
57  rot.axis[2]= uZ.basicVector().v;
58 
59  }
60 
61  TkRotation( const BasicVector & aX, const BasicVector & aY) {
62 
63  BasicVector uX = aX.unit();
64  BasicVector uY = aY.unit();
65  BasicVector uZ(uX.cross(uY));
66 
67  rot.axis[0]= uX.v;
68  rot.axis[1]= uY.v;
69  rot.axis[2]= uZ.v;
70 
71  }
72 
73 
78  TkRotation( const GlobalVector & uX, const GlobalVector & uY,
79  const GlobalVector & uZ) {
80  rot.axis[0]= uX.basicVector().v;
81  rot.axis[1]= uY.basicVector().v;
82  rot.axis[2]= uZ.basicVector().v;
83  }
84 
85  TkRotation( const BasicVector & uX, const BasicVector & uY,
86  const BasicVector & uZ) {
87  rot.axis[0]= uX.v;
88  rot.axis[1]= uY.v;
89  rot.axis[2]= uZ.v;
90  }
91 
92 
101  TkRotation( const Basic3DVector<T>& axis, T phi) :
102  rot( cos(phi), sin(phi), 0,
103  -sin(phi), cos(phi), 0,
104  0, 0, 1) {
105 
106  //rotation around the z-axis by phi
107  TkRotation tmpRotz ( cos(axis.phi()), sin(axis.phi()), 0.,
108  -sin(axis.phi()), cos(axis.phi()), 0.,
109  0., 0., 1. );
110  //rotation around y-axis by theta
111  TkRotation tmpRoty ( cos(axis.theta()), 0.,-sin(axis.theta()),
112  0., 1., 0.,
113  sin(axis.theta()), 0., cos(axis.theta()) );
114  (*this)*=tmpRoty;
115  (*this)*=tmpRotz; // = this * tmpRoty * tmpRotz
116 
117  // (tmpRoty * tmpRotz)^-1 * this * tmpRoty * tmpRotz
118 
119  *this = (tmpRoty*tmpRotz).multiplyInverse(*this);
120 
121  }
122  /* this is the same thing...derived from the CLHEP ... it gives the
123  same results MODULO the sign of the rotation.... but I don't want
124  that... had
125  TkRotation (const Basic3DVector<T>& axis, float phi) {
126  T cx = axis.x();
127  T cy = axis.y();
128  T cz = axis.z();
129 
130  T ll = axis.mag();
131  if (ll == 0) {
132  geometryDetails::TkRotationErr1();
133  }else{
134 
135  float cosa = cos(phi), sina = sin(phi);
136  cx /= ll; cy /= ll; cz /= ll;
137 
138  R11 = cosa + (1-cosa)*cx*cx;
139  R12 = (1-cosa)*cx*cy - sina*cz;
140  R13 = (1-cosa)*cx*cz + sina*cy;
141 
142  R21 = (1-cosa)*cy*cx + sina*cz;
143  R22 = cosa + (1-cosa)*cy*cy;
144  R23 = (1-cosa)*cy*cz - sina*cx;
145 
146  R31 = (1-cosa)*cz*cx - sina*cy;
147  R32 = (1-cosa)*cz*cy + sina*cx;
148  R33 = cosa + (1-cosa)*cz*cz;
149 
150  }
151 
152  }
153  */
154 
155  template <typename U>
157  rot (a.xx(), a.xy(), a.xz(),
158  a.yx(), a.yy(), a.yz(),
159  a.zx(), a.zy(), a.zz()) {}
160 
162  return rot.transpose();
163  }
164 
166  return rot.rotate(v.v);
167  }
168 
170  return rot.rotateBack(v.v);
171  }
172 
173 
175  return rot.rotate(v.v);
176  }
177 
179  return rot.rotateBack(v.v);
180  }
181 
182  template <class Scalar>
184  return Basic3DVector<Scalar>( xx()*v.x() + yx()*v.y() + zx()*v.z(),
185  xy()*v.x() + yy()*v.y() + zy()*v.z(),
186  xz()*v.x() + yz()*v.y() + zz()*v.z());
187  }
188 
190  return Basic3DVector<T>( xx()*v.x() + xy()*v.y(),
191  yx()*v.x() + yy()*v.y(),
192  zx()*v.x() + zy()*v.y());
193  }
195  return Basic3DVector<T>( xx()*v.x() + yx()*v.y(),
196  xy()*v.x() + yy()*v.y(),
197  xz()*v.x() + yz()*v.y());
198  }
199 
200 
201 
202  TkRotation operator*( const TkRotation& b) const {
203  return rot*b.rot;
204  }
206  return rot.transpose()*b.rot;
207  }
208 
210  return *this = operator * (b);
211  }
212 
213  // Note a *= b; <=> a = a * b; while a.transform(b); <=> a = b * a;
215  return *this = b.operator * (*this);
216  }
217 
219  const Basic3DVector<T>& newY,
220  const Basic3DVector<T>& newZ) {
221  T del = 0.001;
222 
223  if (
224 
225  // the check for right-handedness is not needed since
226  // we want to change the handedness when it's left in cmsim
227  //
228  // fabs(newZ.x()-w.x()) > del ||
229  // fabs(newZ.y()-w.y()) > del ||
230  // fabs(newZ.z()-w.z()) > del ||
231  fabs(newX.mag2()-1.) > del ||
232  fabs(newY.mag2()-1.) > del ||
233  fabs(newZ.mag2()-1.) > del ||
234  fabs(newX.dot(newY)) > del ||
235  fabs(newY.dot(newZ)) > del ||
236  fabs(newZ.dot(newX)) > del) {
238  return *this;
239  } else {
240  return transform(TkRotation(newX.x(), newY.x(), newZ.x(),
241  newX.y(), newY.y(), newZ.y(),
242  newX.z(), newY.z(), newZ.z()));
243  }
244  }
245 
246 
247  Basic3DVector<T> x() const { return rot.axis[0];}
248  Basic3DVector<T> y() const { return rot.axis[1];}
249  Basic3DVector<T> z() const { return rot.axis[2];}
250 
251  T xx() const { return rot.axis[0].arr[0];}
252  T xy() const { return rot.axis[0].arr[1];}
253  T xz() const { return rot.axis[0].arr[2];}
254  T yx() const { return rot.axis[1].arr[0];}
255  T yy() const { return rot.axis[1].arr[1];}
256  T yz() const { return rot.axis[1].arr[2];}
257  T zx() const { return rot.axis[2].arr[0];}
258  T zy() const { return rot.axis[2].arr[1];}
259  T zz() const { return rot.axis[2].arr[2];}
260 
261 private:
262 
264 
265 };
266 
267 
268 template<>
269 std::ostream & operator<< <float>( std::ostream& s, const TkRotation<float>& r);
270 
271 template<>
272 std::ostream & operator<< <double>( std::ostream& s, const TkRotation<double>& r);
273 
274 
275 template <class T, class U>
277  return Basic3DVector<U>( r.xx()*v.x() + r.xy()*v.y() + r.xz()*v.z(),
278  r.yx()*v.x() + r.yy()*v.y() + r.yz()*v.z(),
279  r.zx()*v.x() + r.zy()*v.y() + r.zz()*v.z());
280 }
281 
282 template <class T, class U>
286  return RT( a.xx()*b.xx() + a.xy()*b.yx() + a.xz()*b.zx(),
287  a.xx()*b.xy() + a.xy()*b.yy() + a.xz()*b.zy(),
288  a.xx()*b.xz() + a.xy()*b.yz() + a.xz()*b.zz(),
289  a.yx()*b.xx() + a.yy()*b.yx() + a.yz()*b.zx(),
290  a.yx()*b.xy() + a.yy()*b.yy() + a.yz()*b.zy(),
291  a.yx()*b.xz() + a.yy()*b.yz() + a.yz()*b.zz(),
292  a.zx()*b.xx() + a.zy()*b.yx() + a.zz()*b.zx(),
293  a.zx()*b.xy() + a.zy()*b.yy() + a.zz()*b.zy(),
294  a.zx()*b.xz() + a.zy()*b.yz() + a.zz()*b.zz());
295 }
296 
297 
298 template <class T>
299 class TkRotation2D {
300 public:
301 
303 
305  TkRotation2D( mathSSE::Rot2<T> const & irot ) : rot(irot){}
306 
307  TkRotation2D( T xx, T xy, T yx, T yy) :
308  rot(xx,xy, yx,yy){}
309 
310  TkRotation2D( const T* p) :
311  rot(p[0],p[1],
312  p[2],p[3]) {}
313 
314  TkRotation2D( const BasicVector & aX) {
315 
316  BasicVector uX = aX.unit();
317  BasicVector uY(-uX.y(),uX.x());
318 
319  rot.axis[0]= uX.v;
320  rot.axis[1]= uY.v;
321 
322  }
323 
324 
325  TkRotation2D( const BasicVector & uX, const BasicVector & uY) {
326  rot.axis[0]= uX.v;
327  rot.axis[1]= uY.v;
328  }
329 
330  BasicVector x() const { return rot.axis[0];}
331  BasicVector y() const { return rot.axis[1];}
332 
333 
335  return rot.transpose();
336  }
337 
338  BasicVector rotate( const BasicVector& v) const {
339  return rot.rotate(v.v);
340  }
341 
343  return rot.rotateBack(v.v);
344  }
345 
346 
347 
348  private:
349 
351 
352 };
353 
354 
355 template<>
356 std::ostream & operator<< <float>( std::ostream& s, const TkRotation2D<float>& r);
357 
358 template<>
359 std::ostream & operator<< <double>( std::ostream& s, const TkRotation2D<double>& r);
360 
361 
362 #endif
363 
364 
365 
366 
Basic3DVector< T > BasicVector
Definition: newTkRotation.h:36
T xx() const
Basic3DVector< T > z() const
Basic3DVector< T > operator*(const Basic3DVector< T > &v) const
TkRotation(const GlobalVector &aX, const GlobalVector &aY)
Definition: newTkRotation.h:49
T y() const
Cartesian y coordinate.
TkRotation2D(mathSSE::Rot2< T > const &irot)
T x() const
Cartesian x coordinate.
TkRotation2D(const T *p)
TkRotation2D(const BasicVector &uX, const BasicVector &uY)
Basic3DVector< T > y() const
TkRotation(const Basic3DVector< T > &axis, T phi)
std::ostream & operator<< < float >(std::ostream &s, const TkRotation< float > &r)
Definition: TkRotation.cc:5
BasicVector x() const
TkRotation & transform(const TkRotation &b)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Theta< T > theta() const
Basic3DVector unit() const
TkRotation operator*(const TkRotation &b) const
Basic3DVector cross(const Basic3DVector &lh) const
Vector product, or &quot;cross&quot; product, with a vector of same type.
T yx() const
Basic3DVector< Scalar > multiplyInverse(const Basic3DVector< Scalar > &v) const
Vector3DBase< T, GlobalTag > GlobalVector
Definition: newTkRotation.h:35
TkRotation2D transposed() const
TkRotation multiplyInverse(const TkRotation &b) const
Geom::Phi< T > phi() const
mathSSE::Rot3< T > rot
BasicVector y() const
TkRotation(const TkRotation< U > &a)
TkRotation & rotateAxes(const Basic3DVector< T > &newX, const Basic3DVector< T > &newY, const Basic3DVector< T > &newZ)
Basic3DVector< T > x() const
Basic2DVector unit() const
T zx() const
T xy() const
T zz() const
mathSSE::Rot2< T > rot
Basic3DVector< T > rotate(const Basic3DVector< T > &v) const
T z() const
Cartesian z coordinate.
TkRotation(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
Definition: newTkRotation.h:41
Rot3< T > rot
Basic3DVector< T > operator*(const Basic2DVector< T > &v) const
Vector3DBase< typename PreciseFloatType< T, U >::Type, FrameTag > cross(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:119
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Basic3DVector< T > multiplyInverse(const Basic2DVector< T > &v) const
void TkRotationErr2()
Definition: TkRotation.cc:32
T zy() const
TkRotation(const BasicVector &aX, const BasicVector &aY)
Definition: newTkRotation.h:61
TkRotation(mathSSE::Rot3< T > const &irot)
Definition: newTkRotation.h:39
T yy() const
T y() const
Cartesian y coordinate.
void TkRotationErr1()
Definition: TkRotation.cc:29
Vector3DBase unit() const
Definition: Vector3DBase.h:57
TkRotation & operator*=(const TkRotation &b)
std::ostream & operator<< < double >(std::ostream &s, const TkRotation< double > &r)
Definition: TkRotation.cc:12
double b
Definition: hdecay.h:120
TkRotation(const GlobalVector &uX, const GlobalVector &uY, const GlobalVector &uZ)
Definition: newTkRotation.h:78
Basic3DVector< T > rotateBack(const Basic3DVector< T > &v) const
TkRotation2D(const BasicVector &aX)
double a
Definition: hdecay.h:121
T xz() const
TkRotation transposed() const
Rot2< T > rot
BasicVector rotateBack(const BasicVector &v) const
TkRotation2D(T xx, T xy, T yx, T yy)
MatrixMeschach operator*(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
Basic3DVector< T > multiplyInverse(const Basic3DVector< T > &v) const
TkRotation(const T *p)
Definition: newTkRotation.h:44
Basic2DVector< T > BasicVector
long double T
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
T yz() const
T x() const
Cartesian x coordinate.
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
T dot(const Basic3DVector &rh) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
TkRotation(const BasicVector &uX, const BasicVector &uY, const BasicVector &uZ)
Definition: newTkRotation.h:85
BasicVector rotate(const BasicVector &v) const