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

Geometry/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"
124#include "CLHEP/Vector/ThreeVector.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
172 class Transform3D {
173 protected:
174 double xx_, xy_, xz_, dx_, // 4x3 Transformation Matrix
175 yx_, yy_, yz_, dy_,
176 zx_, zy_, zz_, dz_;
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]
201 class Transform3D_row {
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
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
233 : xx_(m.xx_), xy_(m.xy_), xz_(m.xz_), dx_(m.dx_),
234 yx_(m.yx_), yy_(m.yy_), yz_(m.yz_), dy_(m.dy_),
235 zx_(m.zx_), zy_(m.zy_), zz_(m.zz_), dz_(m.dz_) {}
236
242 ~Transform3D() { /* nop */ }
243
246 inline const Transform3D_row operator [] (int) const;
247
249 double operator () (int, int) const;
250
253 double xx() const { return xx_; }
256 double xy() const { return xy_; }
259 double xz() const { return xz_; }
262 double yx() const { return yx_; }
265 double yy() const { return yy_; }
268 double yz() const { return yz_; }
271 double zx() const { return zx_; }
274 double zy() const { return zy_; }
277 double zz() const { return zz_; }
280 double dx() const { return dx_; }
283 double dy() const { return dy_; }
286 double dz() const { return dz_; }
287
291 xx_= m.xx_; xy_= m.xy_; xz_= m.xz_; dx_= m.dx_;
292 yx_= m.yx_; yy_= m.yy_; yz_= m.yz_; dy_= m.dy_;
293 zx_= m.zx_; zy_= m.zy_; zz_= m.zz_; dz_= m.dz_;
294 return *this;
295 }
296
299 void setIdentity() {
300 xy_= xz_= dx_= yx_= yz_= dy_= zx_= zy_= dz_= 0; xx_= yy_= zz_= 1;
301 }
302
306
310
327 Rotate3D & rotation,
328 Translate3D & translation) const;
329
334 bool isNear(const Transform3D & t, double tolerance = 2.2E-14 ) const;
335
341
347
350 bool operator == (const Transform3D & transform) const;
351
354 bool operator != (const Transform3D & transform) const {
355 return ! operator==(transform);
356 }
357 };
358
359 // R O T A T I O N S
360
375 class Rotate3D : public Transform3D {
376 public:
380
383 inline Rotate3D(const CLHEP::HepRotation &m);
384
391 Rotate3D(double a,
392 const Point3D<double> & p1,
393 const Point3D<double> & p2);
394
400 inline Rotate3D(double a, const Vector3D<double> & v);
401
410 inline Rotate3D(const Point3D<double> & fr1,
411 const Point3D<double> & fr2,
412 const Point3D<double> & to1,
413 const Point3D<double> & to2);
414 };
415
430 class RotateX3D : public Rotate3D {
431 public:
435
438 RotateX3D(double a) {
439 double cosa = std::cos(a), sina = std::sin(a);
440 setTransform(1,0,0,0, 0,cosa,-sina,0, 0,sina,cosa,0);
441 }
442 };
443
458 class RotateY3D : public Rotate3D {
459 public:
463
466 RotateY3D(double a) {
467 double cosa = std::cos(a), sina = std::sin(a);
468 setTransform(cosa,0,sina,0, 0,1,0,0, -sina,0,cosa,0);
469 }
470 };
471
486 class RotateZ3D : public Rotate3D {
487 public:
491
494 RotateZ3D(double a) {
495 double cosa = std::cos(a), sina = std::sin(a);
496 setTransform(cosa,-sina,0,0, sina,cosa,0,0, 0,0,1,0);
497 }
498 };
499
500 // T R A N S L A T I O N S
501
516 class Translate3D : public Transform3D {
517 public:
521
525
528 Translate3D(double x, double y, double z)
529 : Transform3D(1,0,0,x, 0,1,0,y, 0,0,1,z) {}
530 };
531
546 class TranslateX3D : public Translate3D {
547 public:
551
554 TranslateX3D(double x) : Translate3D(x, 0, 0) {}
555 };
556
571 class TranslateY3D : public Translate3D {
572 public:
576
579 TranslateY3D(double y) : Translate3D(0, y, 0) {}
580 };
581
596 class TranslateZ3D : public Translate3D {
597 public:
601
604 TranslateZ3D(double z) : Translate3D(0, 0, z) {}
605 };
606
607 // R E F L E C T I O N S
608
623 class Reflect3D : public Transform3D {
624 protected:
625 Reflect3D(double XX, double XY, double XZ, double DX,
626 double YX, double YY, double YZ, double DY,
627 double ZX, double ZY, double ZZ, double DZ)
628 : Transform3D(XX,XY,XZ,DX, YX,YY,YZ,DY, ZX,ZY,ZZ,DZ) {}
629
630 public:
634
639 Reflect3D(double a, double b, double c, double d);
640
643 inline Reflect3D(const Normal3D<double> & normal,
644 const Point3D<double> & point);
645 };
646
661 class ReflectX3D : public Reflect3D {
662 public:
665 ReflectX3D(double x=0) : Reflect3D(-1,0,0,x+x, 0,1,0,0, 0,0,1,0) {}
666 };
667
682 class ReflectY3D : public Reflect3D {
683 public:
686 ReflectY3D(double y=0) : Reflect3D(1,0,0,0, 0,-1,0,y+y, 0,0,1,0) {}
687 };
688
703 class ReflectZ3D : public Reflect3D {
704 public:
707 ReflectZ3D(double z=0) : Reflect3D(1,0,0,0, 0,1,0,0, 0,0,-1,z+z) {}
708 };
709
710 // S C A L I N G S
711
726 class Scale3D : public Transform3D {
727 public:
731
735 Scale3D(double x, double y, double z)
736 : Transform3D(x,0,0,0, 0,y,0,0, 0,0,z,0) {}
737
740 Scale3D(double s)
741 : Transform3D(s,0,0,0, 0,s,0,0, 0,0,s,0) {}
742 };
743
758 class ScaleX3D : public Scale3D {
759 public:
763
766 ScaleX3D(double x) : Scale3D(x, 1, 1) {}
767 };
768
783 class ScaleY3D : public Scale3D {
784 public:
788
791 ScaleY3D(double y) : Scale3D(1, y, 1) {}
792 };
793
808 class ScaleZ3D : public Scale3D {
809 public:
815 ScaleZ3D(double z) : Scale3D(1, 1, z) {}
816 };
817} /* namespace HepGeom */
818
819#ifdef ENABLE_BACKWARDS_COMPATIBILITY
820// backwards compatibility will be enabled ONLY in CLHEP 1.9
821typedef HepGeom::Transform3D HepTransform3D;
822typedef HepGeom::Rotate3D HepRotate3D;
823typedef HepGeom::RotateX3D HepRotateX3D;
824typedef HepGeom::RotateY3D HepRotateY3D;
825typedef HepGeom::RotateZ3D HepRotateZ3D;
826typedef HepGeom::Translate3D HepTranslate3D;
827typedef HepGeom::TranslateX3D HepTranslateX3D;
828typedef HepGeom::TranslateY3D HepTranslateY3D;
829typedef HepGeom::TranslateZ3D HepTranslateZ3D;
830typedef HepGeom::Reflect3D HepReflect3D;
831typedef HepGeom::ReflectX3D HepReflectX3D;
832typedef HepGeom::ReflectY3D HepReflectY3D;
833typedef HepGeom::ReflectZ3D HepReflectZ3D;
834typedef HepGeom::Scale3D HepScale3D;
835typedef HepGeom::ScaleX3D HepScaleX3D;
836typedef HepGeom::ScaleY3D HepScaleY3D;
837typedef HepGeom::ScaleZ3D HepScaleZ3D;
838#endif
839
840#include "CLHEP/Geometry/Transform3D.icc"
841
842#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)
Reflect3D(double a, double b, double c, double d)
Rotate3D(const CLHEP::HepRotation &m)
Rotate3D(double a, const Vector3D< double > &v)
Rotate3D(double a, const Point3D< double > &p1, const Point3D< double > &p2)
Rotate3D(const Point3D< double > &fr1, const Point3D< double > &fr2, const Point3D< double > &to1, const Point3D< double > &to2)
Scale3D(double x, double y, double z)
Transform3D_row(const Transform3D &, int)
double operator()(int, int) const
static const Transform3D Identity
bool isNear(const Transform3D &t, double tolerance=2.2E-14) const
Transform3D operator*(const Transform3D &b) const
bool operator==(const Transform3D &transform) const
bool operator!=(const Transform3D &transform) const
CLHEP::HepRotation getRotation() const
Transform3D & operator=(const Transform3D &m)
void getDecomposition(Scale3D &scale, Rotate3D &rotation, Translate3D &translation) const
CLHEP::Hep3Vector getTranslation() const
const Transform3D_row operator[](int) const
Transform3D(const CLHEP::HepRotation &m, const CLHEP::Hep3Vector &v)
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)
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 inverse() const
Transform3D(const Transform3D &m)
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)
Translate3D(double x, double y, double z)
Translate3D(const CLHEP::Hep3Vector &v)