CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testTransform3D.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: testTransform3D.cc,v 1.3 2003/10/24 21:39:45 garren Exp $
3// ---------------------------------------------------------------------------
4//
5// This file is a part of the CLHEP - a Class Library for High Energy Physics.
6//
7// This is a test for the HepGeom::Transform3D class.
8//
9#include <assert.h>
10#include "CLHEP/Geometry/Transform3D.h"
11#include "CLHEP/Vector/Rotation.h"
12#include "CLHEP/Vector/ThreeVector.h"
13#include "CLHEP/Units/PhysicalConstants.h"
14
22
23#define DEL 10.e-16
24
25int main() {
26 int i,k;
27 double E[4][4] = {
28 { 1, 0, 0, 0},
29 { 0, 1, 0, 0},
30 { 0, 0, 1, 0},
31 { 0, 0, 0, 1}
32 };
33
34 // Default constructor
35
37 for (i=0; i<4; i++) {
38 for (k=0; k<4; k++) {
39 assert ( M[i][k] == E[i][k] );
40 }
41 }
42 assert ( M == Transformation::Identity );
43
44 // Rotation + Translation
45
46 HepRotation R;
47 double angA=CLHEP::pi/3, angB=CLHEP::pi/4, angC=CLHEP::pi/6;
48
49 R.rotateX(angA); R.rotateY(angB); R.rotateZ(angC);
50 const Hep3Vector D(1, 2, 3);
51 M = Transformation(R,D);
52
53 for (i=0; i<3; i++) {
54 for (k=0; k<3; k++) { assert ( M[i][k] == R[i][k] ); }
55 }
56 assert ( M(0,3) == D.x() );
57 assert ( M(1,3) == D.y() );
58 assert ( M(2,3) == D.z() );
59
60 // Transformation of point, vector, normal
61
62 const Point p0(1,1,1);
63 const Vector v0(1,1,1);
64 const Normal n0(1,1,1);
65
66 Point p1 = M * p0;
67 Point p2 = R*Hep3Vector(1,1,1) + D;
68 assert( std::abs(p1.x()-p2.x()) < DEL );
69 assert( std::abs(p1.y()-p2.y()) < DEL );
70 assert( std::abs(p1.z()-p2.z()) < DEL );
71
72 Vector v1 = M * v0;
73 Normal n1 = M * n0;
74 assert( std::abs(v1.x()-n1.x()) < DEL );
75 assert( std::abs(v1.y()-n1.y()) < DEL );
76 assert( std::abs(v1.z()-n1.z()) < DEL );
77
78 // Transformation of basis
79
80 p1 = Point(M[0][0]+D.x(), M[1][0]+D.y(), M[2][0]+D.z());
81 p2 = Point(M[0][1]+D.x(), M[1][1]+D.y(), M[2][1]+D.z());
82 Transformation T(Point(0,0,0), Point(1,0,0), Point(0,1,0), D, p1, p2);
83
84 for (i=0; i<4; i++) {
85 for (k=0; k<4; k++) { assert ( std::abs(M[i][k] - T[i][k]) < DEL ); }
86 }
87
88 // Set Identity
89
90 T.setIdentity();
91 for (i=0; i<4; i++) {
92 for (k=0; k<4; k++) { assert ( T[i][k] == E[i][k] ); }
93 }
94
95 // Assignment, fortran-style subscripting
96
97 T = M;
98 assert (T == M);
99 for (i=0; i<4; i++) {
100 for (k=0; k<4; k++) { assert ( T(i,k) == M[i][k] ); }
101 }
102
103 // Inversion
104
105 T = M.inverse();
106 assert (T != M);
107 T = M * T;
108 for (i=0; i<4; i++) {
109 for (k=0; k<4; k++) { assert ( std::abs(T[i][k] - E[i][k]) < DEL ); }
110 }
111
112 T = M.inverse();
113 T = T * M;
114 for (i=0; i<4; i++) {
115 for (k=0; k<4; k++) { assert ( std::abs(T[i][k] - E[i][k]) < DEL ); }
116 }
117
118 // Get Rotation
119
120 HepRotation Q;
121 Q = M.getRotation();
122 for (i=0; i<3; i++) {
123 for (k=0; k<3; k++) { assert ( R[i][k] == Q[i][k] ); }
124 }
125
126 // Get Translation
127
128 Hep3Vector C;
129 C = M.getTranslation();
130 assert ( C.x() == D.x() );
131 assert ( C.y() == D.y() );
132 assert ( C.z() == D.z() );
133
134 // Compound transformation
135 // Get Decomposition
136
137 Scale S(-2,3,4);
138 M = Transformation(R,D)*S;
139
140 Scale SS;
141 Rotation RR;
142 Translation TT;
143 M.getDecomposition(SS,RR,TT);
144
145 S = HepGeom::Scale3D(2,3,-4);
146 T = TT*RR*SS;
147 for (i=0; i<4; i++) {
148 for (k=0; k<4; k++) {
149 assert ( std::abs(S[i][k] - SS[i][k]) < DEL );
150 assert ( std::abs(M[i][k] - T[i][k]) < DEL );
151 }
152 }
153
154 // test for isNear()
155
156 assert ( T.isNear(M, DEL) );
157 S = HepGeom::Scale3D(2.01,3,-4);
158 T = TT*RR*S;
159 assert ( !T.isNear(M) );
160
161 // Different conversions
162
163 Hep3Vector www(1,2,3);
164 Vector vvv;
165 Point ppp(3,2,1);
166 Normal nnn;
167
168 vvv = www;
169 www = vvv;
170 nnn = ppp;
171
172 assert (vvv.x() == nnn.z());
173 assert (vvv.y() == nnn.y());
174 assert (vvv.z() == nnn.x());
175
176 nnn = Normal(ppp);
177 www = Hep3Vector(vvv);
178
179 return 0;
180}
static const Transform3D Identity
Definition: Transform3D.h:198
bool isNear(const Transform3D &t, double tolerance=2.2E-14) const
Definition: Transform3D.cc:205
CLHEP::HepRotation getRotation() const
void getDecomposition(Scale3D &scale, Rotate3D &rotation, Translate3D &translation) const
Definition: Transform3D.cc:175
CLHEP::Hep3Vector getTranslation() const
Transform3D inverse() const
Definition: Transform3D.cc:143
Definition: excDblThrow.cc:17
HepGeom::Rotate3D Rotation
HepGeom::Normal3D< double > Normal
HepGeom::Scale3D Scale
HepGeom::Translate3D Translation
#define DEL
int main()
HepGeom::Point3D< double > Point
HepGeom::Vector3D< double > Vector
HepGeom::Transform3D Transformation