VGM Version 5.3
Loading...
Searching...
No Matches
transform.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The ClhepVGM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// ClhepVGM utilities
14// --------------
15// Utility functions
16//
17// Author: Ivana Hrivnacova; IPN Orsay
18
19#include "ClhepVGM/transform.h"
20#include "ClhepVGM/Units.h"
21
22#include <cstdlib>
23#include <float.h>
24
25//_____________________________________________________________________________
27 const CLHEP::HepRotation& rotation, const CLHEP::Hep3Vector& translation)
28{
29
30 // Translation
31 //
32 VGM::Transform transform(VGM::kSize);
33 transform[VGM::kDx] = translation.x() * Units::Length();
34 transform[VGM::kDy] = translation.y() * Units::Length();
35 transform[VGM::kDz] = translation.z() * Units::Length();
36
37 // Get axis angles
38 // (Using E.Tchernaiev formula)
39 double angleX;
40 double angleY;
41 double angleZ;
42 double cosb =
43 sqrt(rotation.xx() * rotation.xx() + rotation.yx() * rotation.yx());
44 if (cosb > 16 * FLT_EPSILON) {
45 angleX = atan2(rotation.zy(), rotation.zz());
46 angleY = atan2(-rotation.zx(), cosb);
47 angleZ = atan2(rotation.yx(), rotation.xx());
48 }
49 else {
50 angleX = atan2(-rotation.yz(), rotation.yy());
51 angleY = atan2(-rotation.zx(), cosb);
52 angleZ = 0.;
53 }
54
55 transform[VGM::kAngleX] = angleX * Units::Angle();
56 transform[VGM::kAngleY] = angleY * Units::Angle();
57 transform[VGM::kAngleZ] = angleZ * Units::Angle();
58
59 // No reflection
60 transform[VGM::kReflZ] = 0.;
61
62 return transform;
63}
64
65//_____________________________________________________________________________
66VGM::Transform ClhepVGM::Transform(const HepGeom::Transform3D& objectTransform)
67{
68 //
69 HepGeom::Scale3D scale;
70 HepGeom::Rotate3D rotate;
71 HepGeom::Translate3D translate;
72 objectTransform.getDecomposition(scale, rotate, translate);
73
74 VGM::Transform transform =
75 Transform(rotate.getRotation(), translate.getTranslation());
76
77 if (scale(0, 0) * scale(1, 1) * scale(2, 2) < 0.)
78 transform[VGM::kReflZ] = 1.;
79 else
80 transform[VGM::kReflZ] = 0;
81 ;
82
83 return transform;
84}
85
86//_____________________________________________________________________________
87VGM::Transform ClhepVGM::TransformScale(const HepGeom::Scale3D& scale3D)
88{
89 VGM::Transform transform(VGM::kSize);
90 transform[VGM::kDx] = scale3D(0, 0);
91 transform[VGM::kDy] = scale3D(1, 1);
92 transform[VGM::kDz] = scale3D(2, 2);
93
94 return transform;
95}
96
97//_____________________________________________________________________________
99{
100 //
101 VGM::Transform transform(VGM::kSize);
102 for (int i = 0; i < 7; i++) transform[i] = 0.;
103
104 return transform;
105}
106
107//_____________________________________________________________________________
108bool ClhepVGM::HasReflection(const HepGeom::Transform3D& transform)
109{
110 //
111 HepGeom::Scale3D scale;
112 HepGeom::Rotate3D rotate;
113 HepGeom::Translate3D translate;
114 transform.getDecomposition(scale, rotate, translate);
115
116 if (scale(0, 0) * scale(1, 1) * scale(2, 2) < 0.)
117 return true;
118 else
119 return false;
120}
121
122//_____________________________________________________________________________
123CLHEP::Hep3Vector ClhepVGM::Translation(const VGM::Transform& transform)
124{
125 if (transform.size() != VGM::kSize) {
126 std::cerr << "ClhepVGM::Translation: " << std::endl;
127 std::cerr << "Wrong vector size " << transform.size() << std::endl;
128 exit(1);
129 }
130
131 return CLHEP::Hep3Vector(transform[VGM::kDx] / Units::Length(),
132 transform[VGM::kDy] / Units::Length(),
133 transform[VGM::kDz] / Units::Length());
134}
135
136//_____________________________________________________________________________
137CLHEP::HepRotation ClhepVGM::Rotation(const VGM::Transform& transform)
138{
139 if (transform.size() != VGM::kSize) {
140 std::cerr << "ClhepVGM::Rotation: " << std::endl;
141 std::cerr << "Wrong vector size. " << std::endl;
142 exit(1);
143 }
144
145 CLHEP::HepRotation hepRotation;
146 hepRotation.rotateX(transform[VGM::kAngleX] / Units::Angle());
147 hepRotation.rotateY(transform[VGM::kAngleY] / Units::Angle());
148 hepRotation.rotateZ(transform[VGM::kAngleZ] / Units::Angle());
149
150 return hepRotation;
151}
152
153//_____________________________________________________________________________
154HepGeom::Scale3D ClhepVGM::Scale(const VGM::Transform& transform)
155{
156 return HepGeom::Scale3D(
157 transform[VGM::kDx], transform[VGM::kDy], transform[VGM::kDz]);
158}
159
160//_____________________________________________________________________________
161HepGeom::Transform3D ClhepVGM::Transform(const VGM::Transform& transform)
162{
163 HepGeom::Translate3D translate(Translation(transform));
164 HepGeom::Rotate3D rotate(Rotation(transform));
165 HepGeom::ScaleZ3D scale;
166 if (HasReflection(transform)) scale = HepGeom::ScaleZ3D(-1.0);
167
168 return translate * rotate * scale;
169}
170
171//_____________________________________________________________________________
173{
174 return Round(transform[VGM::kReflZ]) == 1.;
175}
176
177//_____________________________________________________________________________
179{
180 HepGeom::Translate3D translate(Translation(transform));
181 HepGeom::Rotate3D rotate(Rotation(transform));
182 HepGeom::ScaleZ3D scale;
183 if (HasReflection(transform)) scale = HepGeom::ScaleZ3D(-1.0);
184
185 return Transform((translate * rotate * scale).inverse());
186}
187
188//_____________________________________________________________________________
189double ClhepVGM::Round(double x)
190{
193
194 double t;
195 if (x >= 0.0) {
196 t = ceil(x);
197 if (t - x > 0.5) t -= 1.0;
198 return t;
199 }
200 else {
201 t = ceil(-x);
202 if (t + x > 0.5) t -= 1.0;
203 return -t;
204 }
205}
VGM typedef for 3D transformation represented by std::vector<double>:
double Round(double x)
bool HasReflection(const HepGeom::Transform3D &transform)
VGM::Transform Identity()
Definition transform.cxx:98
VGM::Transform Inverse(const VGM::Transform &transform)
VGM::Transform Transform(const CLHEP::HepRotation &rotation, const CLHEP::Hep3Vector &translation)
Definition transform.cxx:26
CLHEP::Hep3Vector Translation(const VGM::Transform &transform)
CLHEP::HepRotation Rotation(const VGM::Transform &transform)
VGM::Transform TransformScale(const HepGeom::Scale3D &scaleTransform)
Definition transform.cxx:87
HepGeom::Scale3D Scale(const VGM::Transform &transform)
std::vector< double > Transform
Definition Transform.h:40
@ kAngleZ
Definition Transform.h:49
@ kDx
Definition Transform.h:44
@ kSize
Definition Transform.h:51
@ kReflZ
Definition Transform.h:50
@ kAngleY
Definition Transform.h:48
@ kDz
Definition Transform.h:46
@ kAngleX
Definition Transform.h:47
@ kDy
Definition Transform.h:45