CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RotationC.cc
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// This is the implementation of methods of the HepRotation class which
7// were introduced when ZOOM PhysicsVectors was merged in, which involve
8// correcting user-supplied data which is supposed to form a Rotation, or
9// rectifying a rotation matrix which may have drifted due to roundoff.
10//
11
12#include "CLHEP/Vector/defs.h"
13#include "CLHEP/Vector/Rotation.h"
14#include "CLHEP/Vector/ZMxpv.h"
15
16#include <cmath>
17#include <iostream>
18
19namespace CLHEP {
20
21// --------- Helper methods (private) for setting from 3 columns:
22
23bool HepRotation::setCols
24 ( const Hep3Vector & u1, const Hep3Vector & u2, const Hep3Vector & u3,
25 double u1u2,
26 Hep3Vector & v1, Hep3Vector & v2, Hep3Vector & v3 ) const {
27
28 if ( (1-std::fabs(u1u2)) <= Hep4RotationInterface::tolerance ) {
29 ZMthrowC (ZMxpvParallelCols(
30 "All three cols supplied for a Rotation are parallel --"
31 "\n an arbitrary rotation will be returned"));
32 setArbitrarily (u1, v1, v2, v3);
33 return true;
34 }
35
36 v1 = u1;
37 v2 = Hep3Vector(u2 - u1u2 * u1).unit();
38 v3 = v1.cross(v2);
39 if ( v3.dot(u3) >= 0 ) {
40 return true;
41 } else {
42 return false; // looks more like a reflection in this case!
43 }
44
45} // HepRotation::setCols
46
47void HepRotation::setArbitrarily (const Hep3Vector & ccolX,
48 Hep3Vector & v1, Hep3Vector & v2, Hep3Vector & v3) const {
49
50 // We have all three col's parallel. Warnings already been given;
51 // this just supplies a result which is a valid rotation.
52
53 v1 = ccolX.unit();
54 v2 = v1.cross(Hep3Vector(0,0,1));
55 if (v2.mag2() != 0) {
56 v2 = v2.unit();
57 } else {
58 v2 = Hep3Vector(1,0,0);
59 }
60 v3 = v1.cross(v2);
61
62 return;
63
64} // HepRotation::setArbitrarily
65
66
67
68// ---------- Constructors and Assignment:
69
70// 3 orthogonal columns or rows
71
73 const Hep3Vector & ccolY,
74 const Hep3Vector & ccolZ ) {
75 Hep3Vector ucolX = ccolX.unit();
76 Hep3Vector ucolY = ccolY.unit();
77 Hep3Vector ucolZ = ccolZ.unit();
78
79 double u1u2 = ucolX.dot(ucolY);
80 double f12 = std::fabs(u1u2);
82 ZMthrowC (ZMxpvNotOrthogonal(
83 "col's X and Y supplied for Rotation are not close to orthogonal"));
84 }
85 double u1u3 = ucolX.dot(ucolZ);
86 double f13 = std::fabs(u1u3);
88 ZMthrowC (ZMxpvNotOrthogonal(
89 "col's X and Z supplied for Rotation are not close to orthogonal"));
90 }
91 double u2u3 = ucolY.dot(ucolZ);
92 double f23 = std::fabs(u2u3);
94 ZMthrowC (ZMxpvNotOrthogonal(
95 "col's Y and Z supplied for Rotation are not close to orthogonal"));
96 }
97
98 Hep3Vector v1, v2, v3;
99 bool isRotation;
100 if ( (f12 <= f13) && (f12 <= f23) ) {
101 isRotation = setCols ( ucolX, ucolY, ucolZ, u1u2, v1, v2, v3 );
102 if ( !isRotation ) {
103 ZMthrowC (ZMxpvImproperRotation(
104 "col's X Y and Z supplied form closer to a reflection than a Rotation "
105 "\n col Z is set to col X cross col Y"));
106 }
107 } else if ( f13 <= f23 ) {
108 isRotation = setCols ( ucolZ, ucolX, ucolY, u1u3, v3, v1, v2 );
109 if ( !isRotation ) {
110 ZMthrowC (ZMxpvImproperRotation(
111 "col's X Y and Z supplied form closer to a reflection than a Rotation "
112 "\n col Y is set to col Z cross col X"));
113 }
114 } else {
115 isRotation = setCols ( ucolY, ucolZ, ucolX, u2u3, v2, v3, v1 );
116 if ( !isRotation ) {
117 ZMthrowC (ZMxpvImproperRotation(
118 "col's X Y and Z supplied form closer to a reflection than a Rotation "
119 "\n col X is set to col Y cross col Z"));
120 }
121 }
122
123 rxx = v1.x(); ryx = v1.y(); rzx = v1.z();
124 rxy = v2.x(); ryy = v2.y(); rzy = v2.z();
125 rxz = v3.x(); ryz = v3.y(); rzz = v3.z();
126
127 return *this;
128
129} // HepRotation::set(colX, colY, colZ)
130
132 const Hep3Vector & ccolY,
133 const Hep3Vector & ccolZ )
134{
135 set (ccolX, ccolY, ccolZ);
136}
137
139 const Hep3Vector & rrowY,
140 const Hep3Vector & rrowZ ) {
141 set (rrowX, rrowY, rrowZ);
142 invert();
143 return *this;
144}
145
146
147// ------- Rectify a near-rotation
148
150 // Assuming the representation of this is close to a true Rotation,
151 // but may have drifted due to round-off error from many operations,
152 // this forms an "exact" orthonormal matrix for the rotation again.
153
154 // The first step is to average with the transposed inverse. This
155 // will correct for small errors such as those occuring when decomposing
156 // a LorentzTransformation. Then we take the bull by the horns and
157 // formally extract the axis and delta (assuming the Rotation were true)
158 // and re-setting the rotation according to those.
159
160 double det = rxx * ryy * rzz +
161 rxy * ryz * rzx +
162 rxz * ryx * rzy -
163 rxx * ryz * rzy -
164 rxy * ryx * rzz -
165 rxz * ryy * rzx ;
166 if (det <= 0) {
167 ZMthrowA(ZMxpvImproperRotation(
168 "Attempt to rectify a Rotation with determinant <= 0\n"));
169 return;
170 }
171 double di = 1.0 / det;
172
173 // xx, xy, ... are components of inverse matrix:
174 double xx1 = (ryy * rzz - ryz * rzy) * di;
175 double xy1 = (rzy * rxz - rzz * rxy) * di;
176 double xz1 = (rxy * ryz - rxz * ryy) * di;
177 double yx1 = (ryz * rzx - ryx * rzz) * di;
178 double yy1 = (rzz * rxx - rzx * rxz) * di;
179 double yz1 = (rxz * ryx - rxx * ryz) * di;
180 double zx1 = (ryx * rzy - ryy * rzx) * di;
181 double zy1 = (rzx * rxy - rzy * rxx) * di;
182 double zz1 = (rxx * ryy - rxy * ryx) * di;
183
184 // Now average with the TRANSPOSE of that:
185 rxx = .5*(rxx + xx1);
186 rxy = .5*(rxy + yx1);
187 rxz = .5*(rxz + zx1);
188 ryx = .5*(ryx + xy1);
189 ryy = .5*(ryy + yy1);
190 ryz = .5*(ryz + zy1);
191 rzx = .5*(rzx + xz1);
192 rzy = .5*(rzy + yz1);
193 rzz = .5*(rzz + zz1);
194
195 // Now force feed this improved rotation
196 double del = delta();
197 Hep3Vector u = axis();
198 u = u.unit(); // Because if the rotation is inexact, then the
199 // axis() returned will not have length 1!
200 set(u, del);
201
202} // rectify()
203
204} // namespace CLHEP
205
#define ZMthrowC(A)
Definition: ZMxpv.h:133
#define ZMthrowA(A)
Definition: ZMxpv.h:128
double z() const
Hep3Vector unit() const
double x() const
double y() const
double dot(const Hep3Vector &) const
Hep3Vector axis() const
Definition: RotationA.cc:76
HepRotation & setRows(const Hep3Vector &rowX, const Hep3Vector &rowY, const Hep3Vector &rowZ)
Definition: RotationC.cc:138
double delta() const
Definition: RotationA.cc:63
HepRotation & set(const Hep3Vector &axis, double delta)
Definition: RotationA.cc:24
HepRotation & invert()