Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Transform3D.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id:$
3 // ---------------------------------------------------------------------------
4 //
5 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
6 //
7 // Hep geometrical 3D Transformation class
8 //
9 // Author: Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
10 //
11 // ******************************************
12 // * *
13 // * Transform *
14 // * / / \ \ *
15 // * -------- / \ -------- *
16 // * / / \ \ *
17 // * Rotate Translate Reflect Scale *
18 // * / | \ / | \ / | \ / | \ *
19 // * X Y Z X Y Z X Y Z X Y Z *
20 // * *
21 // ******************************************
22 //
23 // Identity transformation:
24 // Transform3D::Identity - global identity transformation;
25 // any constructor without parameters, e.g. Transform3D();
26 // m.setIdentity() - set "m" to identity;
27 //
28 // General transformations:
29 // Transform3D(m,v) - transformation given by Rotation "m"
30 // and CLHEP::Hep3Vector "v";
31 // Transform3D(a0,a1,a2, b0,b1,b2) - transformation given by initial
32 // and transformed positions of three points;
33 // Rotations:
34 // Rotate3D(m) - rotation given by CLHEP::HepRotation "m";
35 // Rotate3D(ang,v) - rotation through the angle "ang" around
36 // vector "v";
37 // Rotate3D(ang,p1,p2) - rotation through the angle "ang"
38 // counterclockwise around the axis given by
39 // two points p1->p2;
40 // Rotate3D(a1,a2, b1,b2) - rotation around the origin defined by initial
41 // and transformed positions of two points;
42 // RotateX3D(ang) - rotation around X-axis;
43 // RotateY3D(ang) - rotation around Y-axis;
44 // RotateZ3D(ang) - rotation around Z-axis;
45 //
46 // Translations:
47 // Translate3D(v) - translation given by CLHEP::Hep3Vector "v";
48 // Translate3D(dx,dy,dz) - translation on vector (dx,dy,dz);
49 // TraslateX3D(dx) - translation along X-axis;
50 // TraslateY3D(dy) - translation along Y-axis;
51 // TraslateZ3D(dz) - translation along Z-axis;
52 //
53 // Reflections:
54 // Reflect3D(a,b,c,d) - reflection in the plane a*x+b*y+c*z+d=0;
55 // Reflect3D(normal,p) - reflection in the plane going through "p"
56 // and whose normal is equal to "normal";
57 // ReflectX3D(a) - reflect X in the plane x=a (default a=0);
58 // ReflectY3D(a) - reflect Y in the plane y=a (default a=0);
59 // ReflectZ3D(a) - reflect Z in the plane z=a (default a=0);
60 //
61 // Scalings:
62 // Scale3D(sx,sy,sz) - general scaling with factors "sx","sy","sz"
63 // along X, Y and Z;
64 // Scale3D(s) - scaling with constant factor "s" along all
65 // directions;
66 // ScaleX3D(sx) - scale X;
67 // ScaleY3D(sy) - scale Y;
68 // ScaleZ3D(sz) - scale Z;
69 //
70 // Inverse transformation:
71 // m.inverse() or - returns inverse transformation;
72 //
73 // Compound transformation:
74 // m3 = m2 * m1 - it is relatively slow in comparison with
75 // transformation of a vector. Use parenthesis
76 // to avoid this operation (see example below);
77 // Transformation of point:
78 // p2 = m * p1
79 //
80 // Transformation of vector:
81 // v2 = m * v1
82 //
83 // Transformation of normal:
84 // n2 = m * n1
85 //
86 // The following table explains how different transformations affect
87 // point, vector and normal. "+" means affect, "-" means do not affect,
88 // "*" meas affect but in different way than "+"
89 //
90 // Point Vector Normal
91 // -------------+-------+-------+-------
92 // Rotation ! + ! + ! +
93 // Translation ! + ! - ! -
94 // Reflection ! + ! + ! *
95 // Scaling ! + ! + ! *
96 // -------------+-------+-------+-------
97 //
98 // Example of the usage:
99 //
100 // Transform3D m1, m2, m3;
101 // HepVector3D v2, v1(0,0,0);
102 //
103 // m1 = Rotate3D(angle, Vector3D(1,1,1));
104 // m2 = Translate3D(dx,dy,dz);
105 // m3 = m1.inverse();
106 //
107 // v2 = m3*(m2*(m1*v1));
108 //
109 // History:
110 // 24.09.96 E.Chernyaev - initial version
111 //
112 // 26.02.97 E.Chernyaev
113 // - added global Identity by request of John Allison
114 // (to avoid problems with compilation on HP)
115 // - added getRotation and getTranslation
116 //
117 // 29.01.01 E.Chernyaev - added subscripting
118 // 11.06.01 E.Chernyaev - added getDecomposition
119 
120 #ifndef HEP_TRANSFROM3D_H
121 #define HEP_TRANSFROM3D_H
122 
124 
125 namespace HepGeom {
126 
127  template<class T> class Point3D;
128  template<class T> class Vector3D;
129  template<class T> class Normal3D;
130 
131  class Translate3D;
132  class Rotate3D;
133  class Scale3D;
134 
171  class Transform3D {
172  protected:
173  double xx_, xy_, xz_, dx_, // 4x3 Transformation Matrix
174  yx_, yy_, yz_, dy_,
175  zx_, zy_, zz_, dz_;
176 
177  // Protected constructor
178  Transform3D(double XX, double XY, double XZ, double DX,
179  double YX, double YY, double YZ, double DY,
180  double ZX, double ZY, double ZZ, double DZ)
181  : xx_(XX), xy_(XY), xz_(XZ), dx_(DX),
182  yx_(YX), yy_(YY), yz_(YZ), dy_(DY),
183  zx_(ZX), zy_(ZY), zz_(ZZ), dz_(DZ) {}
184 
185  // Set transformation matrix
186  void setTransform(double XX, double XY, double XZ, double DX,
187  double YX, double YY, double YZ, double DY,
188  double ZX, double ZY, double ZZ, double DZ) {
189  xx_ = XX; xy_ = XY; xz_ = XZ; dx_ = DX;
190  yx_ = YX; yy_ = YY; yz_ = YZ; dy_ = DY;
191  zx_ = ZX; zy_ = ZY; zz_ = ZZ; dz_ = DZ;
192  }
193 
194  public:
197  static const Transform3D Identity;
198 
199  // Helper class for implemention of C-style subscripting r[i][j]
201  public:
202  inline Transform3D_row(const Transform3D &, int);
203  inline double operator [] (int) const;
204  private:
205  const Transform3D & rr;
206  int ii;
207  };
208 
212  : xx_(1), xy_(0), xz_(0), dx_(0),
213  yx_(0), yy_(1), yz_(0), dy_(0),
214  zx_(0), zy_(0), zz_(1), dz_(0) {}
215 
218  inline Transform3D(const CLHEP::HepRotation & m, const CLHEP::Hep3Vector & v);
219 
222  Transform3D(const Point3D<double> & fr0,
223  const Point3D<double> & fr1,
224  const Point3D<double> & fr2,
225  const Point3D<double> & to0,
226  const Point3D<double> & to1,
227  const Point3D<double> & to2);
228 
232  : xx_(m.xx_), xy_(m.xy_), xz_(m.xz_), dx_(m.dx_),
233  yx_(m.yx_), yy_(m.yy_), yz_(m.yz_), dy_(m.dy_),
234  zx_(m.zx_), zy_(m.zy_), zz_(m.zz_), dz_(m.dz_) {}
235 
241  ~Transform3D() { /* nop */ }
242 
245  inline const Transform3D_row operator [] (int) const;
246 
248  double operator () (int, int) const;
249 
252  double xx() const { return xx_; }
255  double xy() const { return xy_; }
258  double xz() const { return xz_; }
261  double yx() const { return yx_; }
264  double yy() const { return yy_; }
267  double yz() const { return yz_; }
270  double zx() const { return zx_; }
273  double zy() const { return zy_; }
276  double zz() const { return zz_; }
279  double dx() const { return dx_; }
282  double dy() const { return dy_; }
285  double dz() const { return dz_; }
286 
290  xx_= m.xx_; xy_= m.xy_; xz_= m.xz_; dx_= m.dx_;
291  yx_= m.yx_; yy_= m.yy_; yz_= m.yz_; dy_= m.dy_;
292  zx_= m.zx_; zy_= m.zy_; zz_= m.zz_; dz_= m.dz_;
293  return *this;
294  }
295 
298  void setIdentity() {
299  xy_= xz_= dx_= yx_= yz_= dy_= zx_= zy_= dz_= 0; xx_= yy_= zz_= 1;
300  }
301 
304  Transform3D inverse() const;
305 
308  Transform3D operator*(const Transform3D & b) const;
309 
325  void getDecomposition(Scale3D & scale,
326  Rotate3D & rotation,
327  Translate3D & translation) const;
328 
333  bool isNear(const Transform3D & t, double tolerance = 2.2E-14 ) const;
334 
339  inline CLHEP::HepRotation getRotation() const;
340 
345  inline CLHEP::Hep3Vector getTranslation() const;
346 
349  bool operator == (const Transform3D & transform) const;
350 
353  bool operator != (const Transform3D & transform) const {
354  return ! operator==(transform);
355  }
356  };
357 
358  // R O T A T I O N S
359 
374  class Rotate3D : public Transform3D {
375  public:
379 
382  inline Rotate3D(const CLHEP::HepRotation &m);
383 
390  Rotate3D(double a,
391  const Point3D<double> & p1,
392  const Point3D<double> & p2);
393 
399  inline Rotate3D(double a, const Vector3D<double> & v);
400 
409  inline Rotate3D(const Point3D<double> & fr1,
410  const Point3D<double> & fr2,
411  const Point3D<double> & to1,
412  const Point3D<double> & to2);
413  };
414 
429  class RotateX3D : public Rotate3D {
430  public:
434 
437  RotateX3D(double a) {
438  double cosa = std::cos(a), sina = std::sin(a);
439  setTransform(1,0,0,0, 0,cosa,-sina,0, 0,sina,cosa,0);
440  }
441  };
442 
457  class RotateY3D : public Rotate3D {
458  public:
462 
465  RotateY3D(double a) {
466  double cosa = std::cos(a), sina = std::sin(a);
467  setTransform(cosa,0,sina,0, 0,1,0,0, -sina,0,cosa,0);
468  }
469  };
470 
485  class RotateZ3D : public Rotate3D {
486  public:
490 
493  RotateZ3D(double a) {
494  double cosa = std::cos(a), sina = std::sin(a);
495  setTransform(cosa,-sina,0,0, sina,cosa,0,0, 0,0,1,0);
496  }
497  };
498 
499  // T R A N S L A T I O N S
500 
515  class Translate3D : public Transform3D {
516  public:
520 
523  inline Translate3D(const CLHEP::Hep3Vector &v);
524 
527  Translate3D(double x, double y, double z)
528  : Transform3D(1,0,0,x, 0,1,0,y, 0,0,1,z) {}
529  };
530 
545  class TranslateX3D : public Translate3D {
546  public:
550 
553  TranslateX3D(double x) : Translate3D(x, 0, 0) {}
554  };
555 
570  class TranslateY3D : public Translate3D {
571  public:
575 
578  TranslateY3D(double y) : Translate3D(0, y, 0) {}
579  };
580 
595  class TranslateZ3D : public Translate3D {
596  public:
600 
603  TranslateZ3D(double z) : Translate3D(0, 0, z) {}
604  };
605 
606  // R E F L E C T I O N S
607 
622  class Reflect3D : public Transform3D {
623  protected:
624  Reflect3D(double XX, double XY, double XZ, double DX,
625  double YX, double YY, double YZ, double DY,
626  double ZX, double ZY, double ZZ, double DZ)
627  : Transform3D(XX,XY,XZ,DX, YX,YY,YZ,DY, ZX,ZY,ZZ,DZ) {}
628 
629  public:
633 
638  Reflect3D(double a, double b, double c, double d);
639 
642  inline Reflect3D(const Normal3D<double> & normal,
643  const Point3D<double> & point);
644  };
645 
660  class ReflectX3D : public Reflect3D {
661  public:
664  ReflectX3D(double x=0) : Reflect3D(-1,0,0,x+x, 0,1,0,0, 0,0,1,0) {}
665  };
666 
681  class ReflectY3D : public Reflect3D {
682  public:
685  ReflectY3D(double y=0) : Reflect3D(1,0,0,0, 0,-1,0,y+y, 0,0,1,0) {}
686  };
687 
702  class ReflectZ3D : public Reflect3D {
703  public:
706  ReflectZ3D(double z=0) : Reflect3D(1,0,0,0, 0,1,0,0, 0,0,-1,z+z) {}
707  };
708 
709  // S C A L I N G S
710 
725  class Scale3D : public Transform3D {
726  public:
730 
734  Scale3D(double x, double y, double z)
735  : Transform3D(x,0,0,0, 0,y,0,0, 0,0,z,0) {}
736 
739  Scale3D(double s)
740  : Transform3D(s,0,0,0, 0,s,0,0, 0,0,s,0) {}
741  };
742 
757  class ScaleX3D : public Scale3D {
758  public:
761  ScaleX3D() : Scale3D() {}
762 
765  ScaleX3D(double x) : Scale3D(x, 1, 1) {}
766  };
767 
782  class ScaleY3D : public Scale3D {
783  public:
786  ScaleY3D() : Scale3D() {}
787 
790  ScaleY3D(double y) : Scale3D(1, y, 1) {}
791  };
792 
807  class ScaleZ3D : public Scale3D {
808  public:
811  ScaleZ3D() : Scale3D() {}
814  ScaleZ3D(double z) : Scale3D(1, 1, z) {}
815  };
816 } /* namespace HepGeom */
817 
818 #include "CLHEP/Geometry/Transform3D.icc"
819 
820 #endif /* HEP_TRANSFROM3D_H */
double yy() const
Definition: Transform3D.h:264
Transform3D & operator=(const Transform3D &m)
Definition: Transform3D.h:289
ScaleY3D(double y)
Definition: Transform3D.h:790
ReflectY3D(double y=0)
Definition: Transform3D.h:685
Translate3D(double x, double y, double z)
Definition: Transform3D.h:527
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
double yx() const
Definition: Transform3D.h:261
const Transform3D_row operator[](int) const
Transform3D_row(const Transform3D &, int)
void getDecomposition(Scale3D &scale, Rotate3D &rotation, Translate3D &translation) const
Definition: Transform3D.cc:174
Transform3D inverse() const
Definition: Transform3D.cc:142
double dx() const
Definition: Transform3D.h:279
RotateZ3D(double a)
Definition: Transform3D.h:493
tuple x
Definition: test.py:50
void setTransform(double XX, double XY, double XZ, double DX, double YX, double YY, double YZ, double DY, double ZX, double ZY, double ZZ, double DZ)
Definition: Transform3D.h:186
double zz() const
Definition: Transform3D.h:276
bool isNear(const Transform3D &t, double tolerance=2.2E-14) const
Definition: Transform3D.cc:204
Reflect3D(double XX, double XY, double XZ, double DX, double YX, double YY, double YZ, double DY, double ZX, double ZY, double ZZ, double DZ)
Definition: Transform3D.h:624
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77
double xz() const
Definition: Transform3D.h:258
bool operator!=(const Transform3D &transform) const
Definition: Transform3D.h:353
ScaleZ3D(double z)
Definition: Transform3D.h:814
const XML_Char * s
Definition: expat.h:262
tuple b
Definition: test.py:12
static constexpr double m
Definition: G4SIunits.hh:129
CLHEP::HepRotation getRotation() const
Transform3D operator*(const Transform3D &b) const
Definition: Transform3D.cc:52
RotateX3D(double a)
Definition: Transform3D.h:437
RotateY3D(double a)
Definition: Transform3D.h:465
Scale3D(double s)
Definition: Transform3D.h:739
bool operator==(const Transform3D &transform) const
Definition: Transform3D.cc:221
double xy() const
Definition: Transform3D.h:255
double dy() const
Definition: Transform3D.h:282
tuple v
Definition: test.py:18
ScaleX3D(double x)
Definition: Transform3D.h:765
Transform3D(const Transform3D &m)
Definition: Transform3D.h:231
double dz() const
Definition: Transform3D.h:285
double yz() const
Definition: Transform3D.h:267
Transform3D(double XX, double XY, double XZ, double DX, double YX, double YY, double YZ, double DY, double ZX, double ZY, double ZZ, double DZ)
Definition: Transform3D.h:178
tuple z
Definition: test.py:28
double xx() const
Definition: Transform3D.h:252
ReflectZ3D(double z=0)
Definition: Transform3D.h:706
double operator()(int, int) const
Definition: Transform3D.cc:24
tuple c
Definition: test.py:13
CLHEP::Hep3Vector getTranslation() const
double zy() const
Definition: Transform3D.h:273
static const Transform3D Identity
Definition: Transform3D.h:197
double zx() const
Definition: Transform3D.h:270
ReflectX3D(double x=0)
Definition: Transform3D.h:664
Scale3D(double x, double y, double z)
Definition: Transform3D.h:734