CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Transform3D.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: Transform3D.h,v 1.5 2010/06/16 16:21:27 garren Exp $
00003 // ---------------------------------------------------------------------------
00004 //
00005 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00006 //
00007 // Hep geometrical 3D Transformation class
00008 //
00009 // Author: Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
00010 //
00011 //          ******************************************
00012 //          *                                        *
00013 //          *               Transform                *
00014 //          *               /  / \  \                *
00015 //          *       --------  /   \  --------        *
00016 //          *      /         /     \         \       *
00017 //          *   Rotate Translate  Reflect   Scale    *
00018 //          *    / | \    / | \    / | \    / | \    *
00019 //          *   X  Y  Z  X  Y  Z  X  Y  Z  X  Y  Z   *
00020 //          *                                        *
00021 //          ******************************************
00022 //
00023 // Identity transformation:
00024 //   Transform3D::Identity   - global identity transformation;
00025 //   any constructor without parameters, e.g. Transform3D();
00026 //   m.setIdentity()            - set "m" to identity;
00027 //
00028 // General transformations:
00029 //   Transform3D(m,v)         - transformation given by Rotation "m"
00030 //                              and CLHEP::Hep3Vector "v";
00031 //   Transform3D(a0,a1,a2, b0,b1,b2) - transformation given by initial
00032 //                               and transformed positions of three points;
00033 // Rotations:
00034 //   Rotate3D(m)              - rotation given by CLHEP::HepRotation "m";
00035 //   Rotate3D(ang,v)          - rotation through the angle "ang" around
00036 //                              vector "v";
00037 //   Rotate3D(ang,p1,p2)      - rotation through the angle "ang"
00038 //                              counterclockwise around the axis given by
00039 //                              two points p1->p2;
00040 //   Rotate3D(a1,a2, b1,b2)   - rotation around the origin defined by initial
00041 //                              and transformed positions of two points;
00042 //   RotateX3D(ang)           - rotation around X-axis;
00043 //   RotateY3D(ang)           - rotation around Y-axis;
00044 //   RotateZ3D(ang)           - rotation around Z-axis;
00045 //
00046 // Translations:
00047 //   Translate3D(v)           - translation given by CLHEP::Hep3Vector "v";
00048 //   Translate3D(dx,dy,dz)    - translation on vector (dx,dy,dz);
00049 //   TraslateX3D(dx)          - translation along X-axis;
00050 //   TraslateY3D(dy)          - translation along Y-axis;
00051 //   TraslateZ3D(dz)          - translation along Z-axis;
00052 //
00053 // Reflections:
00054 //   Reflect3D(a,b,c,d)       - reflection in the plane a*x+b*y+c*z+d=0;
00055 //   Reflect3D(normal,p)      - reflection in the plane going through "p"
00056 //                              and whose normal is equal to "normal";
00057 //   ReflectX3D(a)            - reflect X in the plane x=a (default a=0);
00058 //   ReflectY3D(a)            - reflect Y in the plane y=a (default a=0);
00059 //   ReflectZ3D(a)            - reflect Z in the plane z=a (default a=0);
00060 //
00061 // Scalings:
00062 //   Scale3D(sx,sy,sz)        - general scaling with factors "sx","sy","sz"
00063 //                                 along X, Y and Z;
00064 //   Scale3D(s)               - scaling with constant factor "s" along all 
00065 //                                 directions;
00066 //   ScaleX3D(sx)             - scale X;
00067 //   ScaleY3D(sy)             - scale Y;
00068 //   ScaleZ3D(sz)             - scale Z;
00069 //
00070 // Inverse transformation:
00071 //   m.inverse() or           - returns inverse transformation;
00072 //
00073 // Compound transformation:
00074 //   m3 = m2 * m1             - it is relatively slow in comparison with
00075 //                              transformation of a vector. Use parenthesis
00076 //                              to avoid this operation (see example below);
00077 // Transformation of point:
00078 //   p2 = m * p1
00079 //
00080 // Transformation of vector:
00081 //   v2 = m * v1
00082 //
00083 // Transformation of normal:
00084 //   n2 = m * n1
00085 //
00086 // The following table explains how different transformations affect
00087 // point, vector and normal. "+" means affect, "-" means do not affect,
00088 // "*" meas affect but in different way than "+" 
00089 //
00090 //                     Point  Vector  Normal
00091 //      -------------+-------+-------+-------
00092 //       Rotation    !   +   !   +   !   +
00093 //       Translation !   +   !   -   !   -
00094 //       Reflection  !   +   !   +   !   *
00095 //       Scaling     !   +   !   +   !   *
00096 //      -------------+-------+-------+-------
00097 //
00098 // Example of the usage:
00099 //
00100 //   Transform3D m1, m2, m3;
00101 //   HepVector3D    v2, v1(0,0,0);
00102 //
00103 //   m1 = Rotate3D(angle, Vector3D(1,1,1));
00104 //   m2 = Translate3D(dx,dy,dz);
00105 //   m3 = m1.inverse();
00106 //
00107 //   v2 = m3*(m2*(m1*v1));
00108 //
00109 // History:
00110 // 24.09.96 E.Chernyaev - initial version
00111 //
00112 // 26.02.97 E.Chernyaev
00113 // - added global Identity by request of John Allison 
00114 //   (to avoid problems with compilation on HP) 
00115 // - added getRotation and getTranslation 
00116 //
00117 // 29.01.01 E.Chernyaev - added subscripting
00118 // 11.06.01 E.Chernyaev - added getDecomposition
00119 
00120 #ifndef HEP_TRANSFROM3D_H
00121 #define HEP_TRANSFROM3D_H
00122 
00123 #include "CLHEP/Geometry/defs.h"
00124 #include "CLHEP/Vector/ThreeVector.h"
00125 
00126 namespace HepGeom {
00127 
00128   template<class T> class Point3D;
00129   template<class T> class Vector3D;
00130   template<class T> class Normal3D;
00131 
00132   class Translate3D;
00133   class Rotate3D;
00134   class Scale3D;
00135 
00172   class Transform3D {
00173   protected:
00174     double xx_, xy_, xz_, dx_,     // 4x3  Transformation Matrix
00175            yx_, yy_, yz_, dy_,
00176            zx_, zy_, zz_, dz_;
00177 
00178     // Protected constructor
00179     Transform3D(double XX, double XY, double XZ, double DX,
00180                 double YX, double YY, double YZ, double DY,
00181                 double ZX, double ZY, double ZZ, double DZ)
00182       : xx_(XX), xy_(XY), xz_(XZ), dx_(DX),
00183         yx_(YX), yy_(YY), yz_(YZ), dy_(DY),
00184         zx_(ZX), zy_(ZY), zz_(ZZ), dz_(DZ) {}
00185 
00186     // Set transformation matrix
00187     void setTransform(double XX, double XY, double XZ, double DX,
00188                       double YX, double YY, double YZ, double DY,
00189                       double ZX, double ZY, double ZZ, double DZ) {
00190       xx_ = XX; xy_ = XY; xz_ = XZ; dx_ = DX;
00191       yx_ = YX; yy_ = YY; yz_ = YZ; dy_ = DY;
00192       zx_ = ZX; zy_ = ZY; zz_ = ZZ; dz_ = DZ;
00193     }
00194 
00195   public:
00198     static const Transform3D Identity;
00199 
00200     // Helper class for implemention of C-style subscripting r[i][j] 
00201     class Transform3D_row {
00202     public:
00203       inline Transform3D_row(const Transform3D &, int);
00204       inline double operator [] (int) const;
00205     private:
00206       const Transform3D & rr;
00207       int ii;
00208     };
00209 
00212     Transform3D()
00213       : xx_(1), xy_(0), xz_(0), dx_(0),
00214         yx_(0), yy_(1), yz_(0), dy_(0),
00215         zx_(0), zy_(0), zz_(1), dz_(0) {}
00216   
00219     inline Transform3D(const CLHEP::HepRotation & m, const CLHEP::Hep3Vector & v);
00220 
00223     Transform3D(const Point3D<double> & fr0,
00224                 const Point3D<double> & fr1,
00225                 const Point3D<double> & fr2,
00226                 const Point3D<double> & to0,
00227                 const Point3D<double> & to1,
00228                 const Point3D<double> & to2);
00229 
00232     Transform3D(const Transform3D & m)
00233       : xx_(m.xx_), xy_(m.xy_), xz_(m.xz_), dx_(m.dx_),
00234         yx_(m.yx_), yy_(m.yy_), yz_(m.yz_), dy_(m.dy_),
00235         zx_(m.zx_), zy_(m.zy_), zz_(m.zz_), dz_(m.dz_) {}
00236 
00242     ~Transform3D() { /* nop */ }
00243 
00246     inline const Transform3D_row operator [] (int) const; 
00247 
00249     double operator () (int, int) const;
00250 
00253     double xx() const { return xx_; }
00256     double xy() const { return xy_; }
00259     double xz() const { return xz_; }
00262     double yx() const { return yx_; }
00265     double yy() const { return yy_; }
00268     double yz() const { return yz_; }
00271     double zx() const { return zx_; }
00274     double zy() const { return zy_; }
00277     double zz() const { return zz_; }
00280     double dx() const { return dx_; }
00283     double dy() const { return dy_; }
00286     double dz() const { return dz_; }
00287     
00290     Transform3D & operator=(const Transform3D &m) {
00291       xx_= m.xx_; xy_= m.xy_; xz_= m.xz_; dx_= m.dx_;
00292       yx_= m.yx_; yy_= m.yy_; yz_= m.yz_; dy_= m.dy_;
00293       zx_= m.zx_; zy_= m.zy_; zz_= m.zz_; dz_= m.dz_;
00294       return *this;
00295     }
00296 
00299     void setIdentity() { 
00300       xy_= xz_= dx_= yx_= yz_= dy_= zx_= zy_= dz_= 0; xx_= yy_= zz_= 1;
00301     }
00302     
00305     Transform3D inverse() const;
00306     
00309     Transform3D operator*(const Transform3D & b) const;
00310     
00326     void getDecomposition(Scale3D & scale,
00327                           Rotate3D & rotation,
00328                           Translate3D & translation) const;
00329 
00334     bool isNear(const Transform3D & t, double tolerance = 2.2E-14 ) const;
00335 
00340     inline CLHEP::HepRotation getRotation() const;
00341     
00346     inline CLHEP::Hep3Vector getTranslation() const;
00347     
00350     bool operator == (const Transform3D & transform) const;
00351     
00354     bool operator != (const Transform3D & transform) const {
00355       return ! operator==(transform);
00356     }
00357   };
00358 
00359   //   R O T A T I O N S
00360 
00375   class Rotate3D : public Transform3D {
00376   public:
00379     Rotate3D() : Transform3D() {}
00380     
00383     inline Rotate3D(const CLHEP::HepRotation &m);
00384 
00391     Rotate3D(double a,
00392              const Point3D<double> & p1,
00393              const Point3D<double> & p2);
00394     
00400     inline Rotate3D(double a, const Vector3D<double> & v);
00401 
00410     inline Rotate3D(const Point3D<double> & fr1,
00411                     const Point3D<double> & fr2,
00412                     const Point3D<double> & to1,
00413                     const Point3D<double> & to2);
00414   };
00415 
00430   class RotateX3D : public Rotate3D {
00431   public:
00434     RotateX3D() : Rotate3D() {}
00435     
00438     RotateX3D(double a) {
00439       double cosa = std::cos(a), sina = std::sin(a); 
00440       setTransform(1,0,0,0,  0,cosa,-sina,0,  0,sina,cosa,0);
00441     }
00442   };
00443 
00458   class RotateY3D : public Rotate3D {
00459   public:
00462     RotateY3D() : Rotate3D() {}
00463     
00466     RotateY3D(double a) {
00467       double cosa = std::cos(a), sina = std::sin(a); 
00468       setTransform(cosa,0,sina,0,  0,1,0,0,  -sina,0,cosa,0);
00469     }
00470   };
00471 
00486   class RotateZ3D : public Rotate3D {
00487   public:
00490     RotateZ3D() : Rotate3D() {}
00491     
00494     RotateZ3D(double a) {
00495       double cosa = std::cos(a), sina = std::sin(a); 
00496       setTransform(cosa,-sina,0,0,  sina,cosa,0,0,  0,0,1,0);
00497     }
00498   };
00499 
00500   //   T R A N S L A T I O N S
00501   
00516   class Translate3D : public Transform3D {
00517   public:
00520     Translate3D() : Transform3D() {}
00521     
00524     inline Translate3D(const CLHEP::Hep3Vector &v);
00525     
00528     Translate3D(double x, double y, double z)
00529       : Transform3D(1,0,0,x, 0,1,0,y, 0,0,1,z) {}
00530   };
00531 
00546   class TranslateX3D : public Translate3D {
00547   public:
00550     TranslateX3D() : Translate3D() {}
00551     
00554     TranslateX3D(double x) : Translate3D(x, 0, 0) {}
00555   };
00556 
00571   class TranslateY3D : public Translate3D {
00572   public:
00575     TranslateY3D() : Translate3D() {}
00576 
00579     TranslateY3D(double y) : Translate3D(0, y, 0) {}
00580   };
00581 
00596   class TranslateZ3D : public Translate3D {
00597   public:
00600     TranslateZ3D() : Translate3D() {}
00601 
00604     TranslateZ3D(double z) : Translate3D(0, 0, z) {}
00605   };
00606 
00607   //   R E F L E C T I O N S
00608 
00623   class Reflect3D : public Transform3D {
00624   protected:
00625     Reflect3D(double XX, double XY, double XZ, double DX,
00626                  double YX, double YY, double YZ, double DY,
00627                  double ZX, double ZY, double ZZ, double DZ)
00628       : Transform3D(XX,XY,XZ,DX, YX,YY,YZ,DY, ZX,ZY,ZZ,DZ) {}
00629 
00630   public:
00633     Reflect3D() : Transform3D() {}
00634 
00639     Reflect3D(double a, double b, double c, double d);
00640 
00643     inline Reflect3D(const Normal3D<double> & normal,
00644                         const Point3D<double> & point);
00645   };
00646 
00661   class ReflectX3D : public Reflect3D {
00662   public:
00665     ReflectX3D(double x=0) : Reflect3D(-1,0,0,x+x, 0,1,0,0, 0,0,1,0) {}
00666   };
00667  
00682   class ReflectY3D : public Reflect3D {
00683   public:
00686     ReflectY3D(double y=0) : Reflect3D(1,0,0,0, 0,-1,0,y+y, 0,0,1,0) {}
00687   };
00688  
00703   class ReflectZ3D : public Reflect3D {
00704   public:
00707     ReflectZ3D(double z=0) : Reflect3D(1,0,0,0, 0,1,0,0, 0,0,-1,z+z) {}
00708   };
00709  
00710   //   S C A L I N G S
00711 
00726   class Scale3D : public Transform3D {
00727   public:
00730     Scale3D() : Transform3D() {}
00731 
00735     Scale3D(double x, double y, double z)
00736       : Transform3D(x,0,0,0, 0,y,0,0, 0,0,z,0) {}
00737 
00740     Scale3D(double s)
00741       : Transform3D(s,0,0,0, 0,s,0,0, 0,0,s,0) {}
00742   };
00743 
00758   class ScaleX3D : public Scale3D {
00759   public:
00762     ScaleX3D() : Scale3D() {}
00763 
00766     ScaleX3D(double x) : Scale3D(x, 1, 1) {}
00767   };
00768 
00783   class ScaleY3D : public Scale3D {
00784   public:
00787     ScaleY3D() : Scale3D() {}
00788 
00791     ScaleY3D(double y) : Scale3D(1, y, 1) {}
00792   };
00793 
00808   class ScaleZ3D : public Scale3D {
00809   public:
00812     ScaleZ3D() : Scale3D() {}
00815     ScaleZ3D(double z) : Scale3D(1, 1, z) {}
00816   };
00817 } /* namespace HepGeom */
00818 
00819 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
00820 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
00821 typedef HepGeom::Transform3D  HepTransform3D; 
00822 typedef HepGeom::Rotate3D     HepRotate3D;
00823 typedef HepGeom::RotateX3D    HepRotateX3D;
00824 typedef HepGeom::RotateY3D    HepRotateY3D;
00825 typedef HepGeom::RotateZ3D    HepRotateZ3D;
00826 typedef HepGeom::Translate3D  HepTranslate3D;
00827 typedef HepGeom::TranslateX3D HepTranslateX3D;
00828 typedef HepGeom::TranslateY3D HepTranslateY3D;
00829 typedef HepGeom::TranslateZ3D HepTranslateZ3D;
00830 typedef HepGeom::Reflect3D    HepReflect3D; 
00831 typedef HepGeom::ReflectX3D   HepReflectX3D;
00832 typedef HepGeom::ReflectY3D   HepReflectY3D;
00833 typedef HepGeom::ReflectZ3D   HepReflectZ3D;
00834 typedef HepGeom::Scale3D      HepScale3D;
00835 typedef HepGeom::ScaleX3D     HepScaleX3D;
00836 typedef HepGeom::ScaleY3D     HepScaleY3D;
00837 typedef HepGeom::ScaleZ3D     HepScaleZ3D;
00838 #endif
00839 
00840 #include "CLHEP/Geometry/Transform3D.icc"
00841 
00842 #endif /* HEP_TRANSFROM3D_H */

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7