Geant4-11
Transform3D.h
Go to the documentation of this file.
1// -*- C++ -*-
2// ---------------------------------------------------------------------------
3//
4// This file is a part of the CLHEP - a Class Library for High Energy Physics.
5//
6// Hep geometrical 3D Transformation class
7//
8// Author: Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
9//
10// ******************************************
11// * *
12// * Transform *
13// * / / \ \ *
14// * -------- / \ -------- *
15// * / / \ \ *
16// * Rotate Translate Reflect Scale *
17// * / | \ / | \ / | \ / | \ *
18// * X Y Z X Y Z X Y Z X Y Z *
19// * *
20// ******************************************
21//
22// Identity transformation:
23// Transform3D::Identity - global identity transformation;
24// any constructor without parameters, e.g. Transform3D();
25// m.setIdentity() - set "m" to identity;
26//
27// General transformations:
28// Transform3D(m,v) - transformation given by Rotation "m"
29// and CLHEP::Hep3Vector "v";
30// Transform3D(a0,a1,a2, b0,b1,b2) - transformation given by initial
31// and transformed positions of three points;
32// Rotations:
33// Rotate3D(m) - rotation given by CLHEP::HepRotation "m";
34// Rotate3D(ang,v) - rotation through the angle "ang" around
35// vector "v";
36// Rotate3D(ang,p1,p2) - rotation through the angle "ang"
37// counterclockwise around the axis given by
38// two points p1->p2;
39// Rotate3D(a1,a2, b1,b2) - rotation around the origin defined by initial
40// and transformed positions of two points;
41// RotateX3D(ang) - rotation around X-axis;
42// RotateY3D(ang) - rotation around Y-axis;
43// RotateZ3D(ang) - rotation around Z-axis;
44//
45// Translations:
46// Translate3D(v) - translation given by CLHEP::Hep3Vector "v";
47// Translate3D(dx,dy,dz) - translation on vector (dx,dy,dz);
48// TraslateX3D(dx) - translation along X-axis;
49// TraslateY3D(dy) - translation along Y-axis;
50// TraslateZ3D(dz) - translation along Z-axis;
51//
52// Reflections:
53// Reflect3D(a,b,c,d) - reflection in the plane a*x+b*y+c*z+d=0;
54// Reflect3D(normal,p) - reflection in the plane going through "p"
55// and whose normal is equal to "normal";
56// ReflectX3D(a) - reflect X in the plane x=a (default a=0);
57// ReflectY3D(a) - reflect Y in the plane y=a (default a=0);
58// ReflectZ3D(a) - reflect Z in the plane z=a (default a=0);
59//
60// Scalings:
61// Scale3D(sx,sy,sz) - general scaling with factors "sx","sy","sz"
62// along X, Y and Z;
63// Scale3D(s) - scaling with constant factor "s" along all
64// directions;
65// ScaleX3D(sx) - scale X;
66// ScaleY3D(sy) - scale Y;
67// ScaleZ3D(sz) - scale Z;
68//
69// Inverse transformation:
70// m.inverse() or - returns inverse transformation;
71//
72// Compound transformation:
73// m3 = m2 * m1 - it is relatively slow in comparison with
74// transformation of a vector. Use parenthesis
75// to avoid this operation (see example below);
76// Transformation of point:
77// p2 = m * p1
78//
79// Transformation of vector:
80// v2 = m * v1
81//
82// Transformation of normal:
83// n2 = m * n1
84//
85// The following table explains how different transformations affect
86// point, vector and normal. "+" means affect, "-" means do not affect,
87// "*" meas affect but in different way than "+"
88//
89// Point Vector Normal
90// -------------+-------+-------+-------
91// Rotation ! + ! + ! +
92// Translation ! + ! - ! -
93// Reflection ! + ! + ! *
94// Scaling ! + ! + ! *
95// -------------+-------+-------+-------
96//
97// Example of the usage:
98//
99// Transform3D m1, m2, m3;
100// HepVector3D v2, v1(0,0,0);
101//
102// m1 = Rotate3D(angle, Vector3D(1,1,1));
103// m2 = Translate3D(dx,dy,dz);
104// m3 = m1.inverse();
105//
106// v2 = m3*(m2*(m1*v1));
107//
108// History:
109// 24.09.96 E.Chernyaev - initial version
110//
111// 26.02.97 E.Chernyaev
112// - added global Identity by request of John Allison
113// (to avoid problems with compilation on HP)
114// - added getRotation and getTranslation
115//
116// 29.01.01 E.Chernyaev - added subscripting
117// 11.06.01 E.Chernyaev - added getDecomposition
118
119#ifndef HEP_TRANSFROM3D_H
120#define HEP_TRANSFROM3D_H
121
123
124namespace HepGeom {
125
126 template<class T> class Point3D;
127 template<class T> class Vector3D;
128 template<class T> class Normal3D;
129
130 class Translate3D;
131 class Rotate3D;
132 class Scale3D;
133
171 protected:
172 double xx_, xy_, xz_, dx_, // 4x3 Transformation Matrix
175
176 // Protected constructor
177 Transform3D(double XX, double XY, double XZ, double DX,
178 double YX, double YY, double YZ, double DY,
179 double ZX, double ZY, double ZZ, double DZ)
180 : xx_(XX), xy_(XY), xz_(XZ), dx_(DX),
181 yx_(YX), yy_(YY), yz_(YZ), dy_(DY),
182 zx_(ZX), zy_(ZY), zz_(ZZ), dz_(DZ) {}
183
184 // Set transformation matrix
185 void setTransform(double XX, double XY, double XZ, double DX,
186 double YX, double YY, double YZ, double DY,
187 double ZX, double ZY, double ZZ, double DZ) {
188 xx_ = XX; xy_ = XY; xz_ = XZ; dx_ = DX;
189 yx_ = YX; yy_ = YY; yz_ = YZ; dy_ = DY;
190 zx_ = ZX; zy_ = ZY; zz_ = ZZ; dz_ = DZ;
191 }
192
193 public:
197
198 // Helper class for implemention of C-style subscripting r[i][j]
200 public:
201 inline Transform3D_row(const Transform3D &, int);
202 inline double operator [] (int) const;
203 private:
205 int ii;
206 };
207
211 : xx_(1), xy_(0), xz_(0), dx_(0),
212 yx_(0), yy_(1), yz_(0), dy_(0),
213 zx_(0), zy_(0), zz_(1), dz_(0) {}
214
217 inline Transform3D(const CLHEP::HepRotation & mt, const CLHEP::Hep3Vector & v);
218
221 Transform3D(const Point3D<double> & fr0,
222 const Point3D<double> & fr1,
223 const Point3D<double> & fr2,
224 const Point3D<double> & to0,
225 const Point3D<double> & to1,
226 const Point3D<double> & to2);
227
230 Transform3D(const Transform3D & mt) = default;
231
234 Transform3D(Transform3D && mt) = default;
235
238 ~Transform3D() = default;
239
242 Transform3D & operator=(const Transform3D & mt) = default;
243
247
250 inline const Transform3D_row operator [] (int) const;
251
253 double operator () (int, int) const;
254
257 double xx() const { return xx_; }
260 double xy() const { return xy_; }
263 double xz() const { return xz_; }
266 double yx() const { return yx_; }
269 double yy() const { return yy_; }
272 double yz() const { return yz_; }
275 double zx() const { return zx_; }
278 double zy() const { return zy_; }
281 double zz() const { return zz_; }
284 double dx() const { return dx_; }
287 double dy() const { return dy_; }
290 double dz() const { return dz_; }
291
294 void setIdentity() {
295 xy_= xz_= dx_= yx_= yz_= dy_= zx_= zy_= dz_= 0; xx_= yy_= zz_= 1;
296 }
297
300 Transform3D inverse() const;
301
304 Transform3D operator*(const Transform3D & b) const;
305
321 void getDecomposition(Scale3D & scale,
322 Rotate3D & rotation,
323 Translate3D & translation) const;
324
329 bool isNear(const Transform3D & t, double tolerance = 2.2E-14 ) const;
330
336
342
345 bool operator == (const Transform3D & transform) const;
346
349 bool operator != (const Transform3D & transform) const {
350 return ! operator==(transform);
351 }
352 };
353
354 // R O T A T I O N S
355
370 class Rotate3D : public Transform3D {
371 public:
375
378 inline Rotate3D(const CLHEP::HepRotation &mt);
379
386 Rotate3D(double a,
387 const Point3D<double> & p1,
388 const Point3D<double> & p2);
389
395 inline Rotate3D(double a, const Vector3D<double> & v);
396
405 inline Rotate3D(const Point3D<double> & fr1,
406 const Point3D<double> & fr2,
407 const Point3D<double> & to1,
408 const Point3D<double> & to2);
409 };
410
425 class RotateX3D : public Rotate3D {
426 public:
430
433 RotateX3D(double a) {
434 double cosa = std::cos(a), sina = std::sin(a);
435 setTransform(1,0,0,0, 0,cosa,-sina,0, 0,sina,cosa,0);
436 }
437 };
438
453 class RotateY3D : public Rotate3D {
454 public:
458
461 RotateY3D(double a) {
462 double cosa = std::cos(a), sina = std::sin(a);
463 setTransform(cosa,0,sina,0, 0,1,0,0, -sina,0,cosa,0);
464 }
465 };
466
481 class RotateZ3D : public Rotate3D {
482 public:
486
489 RotateZ3D(double a) {
490 double cosa = std::cos(a), sina = std::sin(a);
491 setTransform(cosa,-sina,0,0, sina,cosa,0,0, 0,0,1,0);
492 }
493 };
494
495 // T R A N S L A T I O N S
496
511 class Translate3D : public Transform3D {
512 public:
516
520
523 Translate3D(double x, double y, double z)
524 : Transform3D(1,0,0,x, 0,1,0,y, 0,0,1,z) {}
525 };
526
541 class TranslateX3D : public Translate3D {
542 public:
546
549 TranslateX3D(double x) : Translate3D(x, 0, 0) {}
550 };
551
566 class TranslateY3D : public Translate3D {
567 public:
571
574 TranslateY3D(double y) : Translate3D(0, y, 0) {}
575 };
576
591 class TranslateZ3D : public Translate3D {
592 public:
596
599 TranslateZ3D(double z) : Translate3D(0, 0, z) {}
600 };
601
602 // R E F L E C T I O N S
603
618 class Reflect3D : public Transform3D {
619 protected:
620 Reflect3D(double XX, double XY, double XZ, double DX,
621 double YX, double YY, double YZ, double DY,
622 double ZX, double ZY, double ZZ, double DZ)
623 : Transform3D(XX,XY,XZ,DX, YX,YY,YZ,DY, ZX,ZY,ZZ,DZ) {}
624
625 public:
629
634 Reflect3D(double a, double b, double c, double d);
635
639 const Point3D<double> & point);
640 };
641
656 class ReflectX3D : public Reflect3D {
657 public:
660 ReflectX3D(double x=0) : Reflect3D(-1,0,0,x+x, 0,1,0,0, 0,0,1,0) {}
661 };
662
677 class ReflectY3D : public Reflect3D {
678 public:
681 ReflectY3D(double y=0) : Reflect3D(1,0,0,0, 0,-1,0,y+y, 0,0,1,0) {}
682 };
683
698 class ReflectZ3D : public Reflect3D {
699 public:
702 ReflectZ3D(double z=0) : Reflect3D(1,0,0,0, 0,1,0,0, 0,0,-1,z+z) {}
703 };
704
705 // S C A L I N G S
706
721 class Scale3D : public Transform3D {
722 public:
726
730 Scale3D(double x, double y, double z)
731 : Transform3D(x,0,0,0, 0,y,0,0, 0,0,z,0) {}
732
735 Scale3D(double sc)
736 : Transform3D(sc,0,0,0, 0,sc,0,0, 0,0,sc,0) {}
737 };
738
753 class ScaleX3D : public Scale3D {
754 public:
758
761 ScaleX3D(double x) : Scale3D(x, 1, 1) {}
762 };
763
778 class ScaleY3D : public Scale3D {
779 public:
783
786 ScaleY3D(double y) : Scale3D(1, y, 1) {}
787 };
788
803 class ScaleZ3D : public Scale3D {
804 public:
810 ScaleZ3D(double z) : Scale3D(1, 1, z) {}
811 };
812} /* namespace HepGeom */
813
815
816#endif /* HEP_TRANSFROM3D_H */
Reflect3D(const Normal3D< double > &normal, const Point3D< double > &point)
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:620
ReflectX3D(double x=0)
Definition: Transform3D.h:660
ReflectY3D(double y=0)
Definition: Transform3D.h:681
ReflectZ3D(double z=0)
Definition: Transform3D.h:702
Rotate3D(double a, const Vector3D< double > &v)
Rotate3D(const Point3D< double > &fr1, const Point3D< double > &fr2, const Point3D< double > &to1, const Point3D< double > &to2)
Rotate3D(const CLHEP::HepRotation &mt)
RotateX3D(double a)
Definition: Transform3D.h:433
RotateY3D(double a)
Definition: Transform3D.h:461
RotateZ3D(double a)
Definition: Transform3D.h:489
Scale3D(double sc)
Definition: Transform3D.h:735
Scale3D(double x, double y, double z)
Definition: Transform3D.h:730
ScaleX3D(double x)
Definition: Transform3D.h:761
ScaleY3D(double y)
Definition: Transform3D.h:786
ScaleZ3D(double z)
Definition: Transform3D.h:810
Transform3D_row(const Transform3D &, int)
double operator()(int, int) const
Definition: Transform3D.cc:23
static DLL_API const Transform3D Identity
Definition: Transform3D.h:196
double dy() const
Definition: Transform3D.h:287
bool isNear(const Transform3D &t, double tolerance=2.2E-14) const
Definition: Transform3D.cc:203
Transform3D operator*(const Transform3D &b) const
Definition: Transform3D.cc:51
Transform3D & operator=(Transform3D &&mt)=default
double zz() const
Definition: Transform3D.h:281
double yz() const
Definition: Transform3D.h:272
bool operator==(const Transform3D &transform) const
Definition: Transform3D.cc:220
double dz() const
Definition: Transform3D.h:290
bool operator!=(const Transform3D &transform) const
Definition: Transform3D.h:349
CLHEP::HepRotation getRotation() const
double dx() const
Definition: Transform3D.h:284
void getDecomposition(Scale3D &scale, Rotate3D &rotation, Translate3D &translation) const
Definition: Transform3D.cc:173
double xy() const
Definition: Transform3D.h:260
CLHEP::Hep3Vector getTranslation() const
const Transform3D_row operator[](int) 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)
Definition: Transform3D.h:185
double zx() const
Definition: Transform3D.h:275
Transform3D(const Transform3D &mt)=default
double yx() const
Definition: Transform3D.h:266
Transform3D(const CLHEP::HepRotation &mt, const CLHEP::Hep3Vector &v)
Transform3D inverse() const
Definition: Transform3D.cc:141
double zy() const
Definition: Transform3D.h:278
double xx() const
Definition: Transform3D.h:257
Transform3D & operator=(const Transform3D &mt)=default
double yy() const
Definition: Transform3D.h:269
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:177
double xz() const
Definition: Transform3D.h:263
Transform3D(Transform3D &&mt)=default
Translate3D(double x, double y, double z)
Definition: Transform3D.h:523
Translate3D(const CLHEP::Hep3Vector &v)
#define DLL_API
Definition: defs.h:19
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:79
G4bool transform(G4String &input, const G4String &type)