CLHEP 2.4.7.1
C++ Class Library for High Energy Physics
Transform3D.h
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: Transform3D.h,v 1.5 2010/06/16 16:21:27 garren Exp $
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
123#include "CLHEP/Geometry/defs.h"
125
126namespace HepGeom {
127
128 template<class T> class Point3D;
129 template<class T> class Vector3D;
130 template<class T> class Normal3D;
131
132 class Translate3D;
133 class Rotate3D;
134 class Scale3D;
135
173 protected:
174 double xx_, xy_, xz_, dx_, // 4x3 Transformation Matrix
177
178 // Protected constructor
179 Transform3D(double XX, double XY, double XZ, double DX,
180 double YX, double YY, double YZ, double DY,
181 double ZX, double ZY, double ZZ, double DZ)
182 : xx_(XX), xy_(XY), xz_(XZ), dx_(DX),
183 yx_(YX), yy_(YY), yz_(YZ), dy_(DY),
184 zx_(ZX), zy_(ZY), zz_(ZZ), dz_(DZ) {}
185
186 // Set transformation matrix
187 void setTransform(double XX, double XY, double XZ, double DX,
188 double YX, double YY, double YZ, double DY,
189 double ZX, double ZY, double ZZ, double DZ) {
190 xx_ = XX; xy_ = XY; xz_ = XZ; dx_ = DX;
191 yx_ = YX; yy_ = YY; yz_ = YZ; dy_ = DY;
192 zx_ = ZX; zy_ = ZY; zz_ = ZZ; dz_ = DZ;
193 }
194
195 public:
198 static const Transform3D Identity;
199
200 // Helper class for implemention of C-style subscripting r[i][j]
202 public:
203 inline Transform3D_row(const Transform3D &, int);
204 inline double operator [] (int) const;
205 private:
206 const Transform3D & rr;
207 int ii;
208 };
209
213 : xx_(1), xy_(0), xz_(0), dx_(0),
214 yx_(0), yy_(1), yz_(0), dy_(0),
215 zx_(0), zy_(0), zz_(1), dz_(0) {}
216
219 inline Transform3D(const CLHEP::HepRotation & mt, const CLHEP::Hep3Vector & v);
220
224 const Point3D<double> & fr1,
225 const Point3D<double> & fr2,
226 const Point3D<double> & to0,
227 const Point3D<double> & to1,
228 const Point3D<double> & to2);
229
232 Transform3D(const Transform3D & mt) = default;
233
236 Transform3D(Transform3D && mt) = default;
237
240 ~Transform3D() = default;
241
244 Transform3D & operator=(const Transform3D & mt) = default;
245
249
252 inline const Transform3D_row operator [] (int) const;
253
255 double operator () (int, int) const;
256
259 double xx() const { return xx_; }
262 double xy() const { return xy_; }
265 double xz() const { return xz_; }
268 double yx() const { return yx_; }
271 double yy() const { return yy_; }
274 double yz() const { return yz_; }
277 double zx() const { return zx_; }
280 double zy() const { return zy_; }
283 double zz() const { return zz_; }
286 double dx() const { return dx_; }
289 double dy() const { return dy_; }
292 double dz() const { return dz_; }
293
296 void setIdentity() {
297 xy_= xz_= dx_= yx_= yz_= dy_= zx_= zy_= dz_= 0; xx_= yy_= zz_= 1;
298 }
299
303
307
324 Rotate3D & rotation,
325 Translate3D & translation) const;
326
331 bool isNear(const Transform3D & t, double tolerance = 2.2E-14 ) const;
332
337 inline CLHEP::HepRotation getRotation() const;
338
343 inline CLHEP::Hep3Vector getTranslation() const;
344
347 bool operator == (const Transform3D & transform) const;
348
351 bool operator != (const Transform3D & transform) const {
352 return ! operator==(transform);
353 }
354 };
355
356 // R O T A T I O N S
357
372 class Rotate3D : public Transform3D {
373 public:
377
380 inline Rotate3D(const CLHEP::HepRotation &mt);
381
388 Rotate3D(double a,
389 const Point3D<double> & p1,
390 const Point3D<double> & p2);
391
397 inline Rotate3D(double a, const Vector3D<double> & v);
398
407 inline Rotate3D(const Point3D<double> & fr1,
408 const Point3D<double> & fr2,
409 const Point3D<double> & to1,
410 const Point3D<double> & to2);
411 };
412
427 class RotateX3D : public Rotate3D {
428 public:
432
435 RotateX3D(double a) {
436 double cosa = std::cos(a), sina = std::sin(a);
437 setTransform(1,0,0,0, 0,cosa,-sina,0, 0,sina,cosa,0);
438 }
439 };
440
455 class RotateY3D : public Rotate3D {
456 public:
460
463 RotateY3D(double a) {
464 double cosa = std::cos(a), sina = std::sin(a);
465 setTransform(cosa,0,sina,0, 0,1,0,0, -sina,0,cosa,0);
466 }
467 };
468
483 class RotateZ3D : public Rotate3D {
484 public:
488
491 RotateZ3D(double a) {
492 double cosa = std::cos(a), sina = std::sin(a);
493 setTransform(cosa,-sina,0,0, sina,cosa,0,0, 0,0,1,0);
494 }
495 };
496
497 // T R A N S L A T I O N S
498
513 class Translate3D : public Transform3D {
514 public:
518
521 inline Translate3D(const CLHEP::Hep3Vector &v);
522
525 Translate3D(double x, double y, double z)
526 : Transform3D(1,0,0,x, 0,1,0,y, 0,0,1,z) {}
527 };
528
543 class TranslateX3D : public Translate3D {
544 public:
548
551 TranslateX3D(double x) : Translate3D(x, 0, 0) {}
552 };
553
568 class TranslateY3D : public Translate3D {
569 public:
573
576 TranslateY3D(double y) : Translate3D(0, y, 0) {}
577 };
578
593 class TranslateZ3D : public Translate3D {
594 public:
598
601 TranslateZ3D(double z) : Translate3D(0, 0, z) {}
602 };
603
604 // R E F L E C T I O N S
605
620 class Reflect3D : public Transform3D {
621 protected:
622 Reflect3D(double XX, double XY, double XZ, double DX,
623 double YX, double YY, double YZ, double DY,
624 double ZX, double ZY, double ZZ, double DZ)
625 : Transform3D(XX,XY,XZ,DX, YX,YY,YZ,DY, ZX,ZY,ZZ,DZ) {}
626
627 public:
631
636 Reflect3D(double a, double b, double c, double d);
637
640 inline Reflect3D(const Normal3D<double> & normal,
641 const Point3D<double> & point);
642 };
643
658 class ReflectX3D : public Reflect3D {
659 public:
662 ReflectX3D(double x=0) : Reflect3D(-1,0,0,x+x, 0,1,0,0, 0,0,1,0) {}
663 };
664
679 class ReflectY3D : public Reflect3D {
680 public:
683 ReflectY3D(double y=0) : Reflect3D(1,0,0,0, 0,-1,0,y+y, 0,0,1,0) {}
684 };
685
700 class ReflectZ3D : public Reflect3D {
701 public:
704 ReflectZ3D(double z=0) : Reflect3D(1,0,0,0, 0,1,0,0, 0,0,-1,z+z) {}
705 };
706
707 // S C A L I N G S
708
723 class Scale3D : public Transform3D {
724 public:
728
732 Scale3D(double x, double y, double z)
733 : Transform3D(x,0,0,0, 0,y,0,0, 0,0,z,0) {}
734
737 Scale3D(double sc)
738 : Transform3D(sc,0,0,0, 0,sc,0,0, 0,0,sc,0) {}
739 };
740
755 class ScaleX3D : public Scale3D {
756 public:
760
763 ScaleX3D(double x) : Scale3D(x, 1, 1) {}
764 };
765
780 class ScaleY3D : public Scale3D {
781 public:
785
788 ScaleY3D(double y) : Scale3D(1, y, 1) {}
789 };
790
805 class ScaleZ3D : public Scale3D {
806 public:
812 ScaleZ3D(double z) : Scale3D(1, 1, z) {}
813 };
814} /* namespace HepGeom */
815
816#ifdef ENABLE_BACKWARDS_COMPATIBILITY
817// backwards compatibility will be enabled ONLY in CLHEP 1.9
818typedef HepGeom::Transform3D HepTransform3D;
819typedef HepGeom::Rotate3D HepRotate3D;
820typedef HepGeom::RotateX3D HepRotateX3D;
821typedef HepGeom::RotateY3D HepRotateY3D;
822typedef HepGeom::RotateZ3D HepRotateZ3D;
823typedef HepGeom::Translate3D HepTranslate3D;
824typedef HepGeom::TranslateX3D HepTranslateX3D;
825typedef HepGeom::TranslateY3D HepTranslateY3D;
826typedef HepGeom::TranslateZ3D HepTranslateZ3D;
827typedef HepGeom::Reflect3D HepReflect3D;
828typedef HepGeom::ReflectX3D HepReflectX3D;
829typedef HepGeom::ReflectY3D HepReflectY3D;
830typedef HepGeom::ReflectZ3D HepReflectZ3D;
831typedef HepGeom::Scale3D HepScale3D;
832typedef HepGeom::ScaleX3D HepScaleX3D;
833typedef HepGeom::ScaleY3D HepScaleY3D;
834typedef HepGeom::ScaleZ3D HepScaleZ3D;
835#endif
836
838
839#endif /* HEP_TRANSFROM3D_H */
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)
Reflect3D(double a, double b, double c, double d)
ReflectX3D(double x=0)
ReflectY3D(double y=0)
ReflectZ3D(double z=0)
Rotate3D(double a, const Point3D< double > &p1, const Point3D< double > &p2)
Scale3D(double sc)
Scale3D(double x, double y, double z)
Transform3D_row(const Transform3D &, int)
double operator()(int, int) const
static const Transform3D Identity
const Transform3D_row operator[](int) const
double dy() const
bool isNear(const Transform3D &t, double tolerance=2.2E-14) const
Transform3D operator*(const Transform3D &b) const
Transform3D & operator=(Transform3D &&mt)=default
double zz() const
double yz() const
bool operator==(const Transform3D &transform) const
double dz() const
bool operator!=(const Transform3D &transform) const
CLHEP::HepRotation getRotation() const
double dx() const
void getDecomposition(Scale3D &scale, Rotate3D &rotation, Translate3D &translation) const
double xy() const
CLHEP::Hep3Vector getTranslation() const
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)
double zx() const
Transform3D(const Point3D< double > &fr0, const Point3D< double > &fr1, const Point3D< double > &fr2, const Point3D< double > &to0, const Point3D< double > &to1, const Point3D< double > &to2)
Transform3D(const Transform3D &mt)=default
double yx() const
Transform3D inverse() const
double zy() const
double xx() const
Transform3D & operator=(const Transform3D &mt)=default
double yy() const
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)
double xz() const
Transform3D(Transform3D &&mt)=default
Translate3D(double x, double y, double z)