Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JRotation3D.hh
Go to the documentation of this file.
1#ifndef __JROTATION3D__
2#define __JROTATION3D__
3
4#include <cmath>
5
12#include "JMath/JMath.hh"
13
14
15/**
16 * \author mdejong
17 */
18
19namespace JGEOMETRY3D {}
20namespace JPP { using namespace JGEOMETRY3D; }
21
22namespace JGEOMETRY3D {
23
25 using JMATH::JMath;
26
27
28 /**
29 * Rotation around X-axis.
30 */
32 public JRotation2D
33 {
34 public:
35 /**
36 * Default constructor (= identity matrix).
37 */
40 {}
41
42
43 /**
44 * Constructor.
45 *
46 * \param phi rotation angle (anti-clock wise) [rad]
47 */
48 JRotation3X(const double phi) :
49 JRotation2D(phi)
50 {}
51 };
52
53
54 /**
55 * Rotation around Y-axis.
56 */
58 public JRotation2D
59 {
60 public:
61 /**
62 * Default constructor (= identity matrix).
63 */
66 {}
67
68
69 /**
70 * Constructor.
71 *
72 * Note that in the x-z plane the corresponding rotation is clock wise.
73 *
74 * \param phi rotation angle (anti-clock wise) [rad]
75 */
76 JRotation3Y(const double phi) :
77 JRotation2D(-phi)
78 {}
79 };
80
81
82 /**
83 * Rotation around Z-axis.
84 */
86 public JRotation2D
87 {
88 public:
89 /**
90 * Default constructor (= identity matrix).
91 */
94 {}
95
96
97 /**
98 * Constructor.
99 *
100 * \param phi rotation angle (anti-clock wise) [rad]
101 */
102 JRotation3Z(const double phi) :
103 JRotation2D(phi)
104 {}
105 };
106
107
108 /**
109 * Rotation matrix
110 */
112 public JMatrix3D,
113 public JMath<JRotation3D>
114 {
115 public:
116
117 using JMath<JRotation3D>::mul;
118
119 /**
120 * Default constructor (= identity matrix).
121 */
123 JMatrix3D()
124 {
125 setIdentity();
126 }
127
128
129 /**
130 * Constructor.
131 *
132 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
133 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
134 *
135 * \param dir direction
136 */
137 JRotation3D(const JAngle3D& dir) :
138 JMatrix3D()
139 {
140 const double ct = cos(dir.getTheta());
141 const double st = sin(dir.getTheta());
142 const double cp = cos(dir.getPhi());
143 const double sp = sin(dir.getPhi());
144
145 a00 = ct*cp; a01 = ct*sp; a02 = -st;
146 a10 = -sp; a11 = cp; a12 = 0.0;
147 a20 = st*cp; a21 = st*sp; a22 = +ct;
148 }
149
150
151 /**
152 * Constructor.
153 *
154 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
155 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
156 *
157 * \param dir direction
158 */
159 JRotation3D(const JVersor3D& dir) :
160 JMatrix3D()
161 {
162 const double ct = dir.getDZ();
163 const double st = sqrt((1.0 + ct)*(1.0 - ct));
164 const double phi = atan2(dir.getDY(), dir.getDX());
165 const double cp = cos(phi);
166 const double sp = sin(phi);
167
168 a00 = ct*cp; a01 = ct*sp; a02 = -st;
169 a10 = -sp; a11 = cp; a12 = 0.0;
170 a20 = st*cp; a21 = st*sp; a22 = +ct;
171 }
172
173
174 /**
175 * Constructor.
176 *
177 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
178 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
179 *
180 * \param dir direction
181 */
182 JRotation3D(const JVersor3Z& dir) :
183 JMatrix3D()
184 {
185 const double ct = dir.getDZ();
186 const double st = sqrt((1.0 + ct)*(1.0 - ct));
187 const double phi = atan2(dir.getDY(), dir.getDX());
188 const double cp = cos(phi);
189 const double sp = sin(phi);
190
191 a00 = ct*cp; a01 = ct*sp; a02 = -st;
192 a10 = -sp; a11 = cp; a12 = 0.0;
193 a20 = st*cp; a21 = st*sp; a22 = +ct;
194 }
195
196
197 /**
198 * Constructor.
199 *
200 * \param R 2D rotation matrix around X-axis
201 */
203 JMatrix3D()
204 {
205 a00 = 1.0; a01 = 0.0; a02 = 0.0;
206 a10 = 0.0; a11 = R.a00; a12 = R.a01;
207 a20 = 0.0; a21 = R.a10; a22 = R.a11;
208 }
209
210
211 /**
212 * Constructor.
213 *
214 * \param R 2D rotation matrix around Y-axis
215 */
217 JMatrix3D()
218 {
219 a00 = R.a00; a01 = 0.0; a02 = R.a01;
220 a10 = 0.0; a11 = 1.0; a12 = 0.0;
221 a20 = R.a10; a21 = 0.0; a22 = R.a11;
222 }
223
224
225 /**
226 * Constructor.
227 *
228 * \param R 2D rotation matrix around Z-axis
229 */
231 JMatrix3D()
232 {
233 a00 = R.a00; a01 = R.a01; a02 = 0.0;
234 a10 = R.a10; a11 = R.a11; a12 = 0.0;
235 a20 = 0.0; a21 = 0.0; a22 = 1.0;
236 }
237
238
239 /**
240 * Constructor.
241 *
242 * \param Q quaternion
243 */
245 JMatrix3D()
246 {
247 const double a2 = Q.getA()*Q.getA();
248 const double b2 = Q.getB()*Q.getB();
249 const double c2 = Q.getC()*Q.getC();
250 const double d2 = Q.getD()*Q.getD();
251
252 const double ab = Q.getA()*Q.getB();
253 const double ac = Q.getA()*Q.getC();
254 const double ad = Q.getA()*Q.getD();
255
256 const double bc = Q.getB()*Q.getC();
257 const double bd = Q.getB()*Q.getD();
258
259 const double cd = Q.getC()*Q.getD();
260
261 a00 = a2 + b2 - c2 - d2; a01 = 2.0*bc - 2.0*ad; a02 = 2.0*bd + 2.0*ac;
262 a10 = 2.0*bc + 2.0*ad; a11 = a2 - b2 + c2 - d2; a12 = 2.0*cd - 2.0*ab;
263 a20 = 2.0*bd - 2.0*ac; a21 = 2.0*cd + 2.0*ab; a22 = a2 - b2 - c2 + d2;
264 }
265
266
267 /**
268 * Get rotation.
269 *
270 * \return rotation
271 */
273 {
274 return static_cast<const JRotation3D&>(*this);
275 }
276
277
278 /**
279 * Type conversion operator.
280 *
281 * \return quaternion
282 */
283 operator JQuaternion3D() const
284 {
285 const double q2 = 0.25 * (1.0 + a00 + a11 + a22);
286
287 if (q2 > 0.0) {
288
289 const double a = sqrt(q2);
290 const double w = 0.25 / a;
291 const double b = (a21 - a12) * w;
292 const double c = (a02 - a20) * w;
293 const double d = (a10 - a01) * w;
294
295 return JQuaternion3D(a,b,c,d).normalise();
296 }
297
298 return JQuaternion3D(1.0, 0.0, 0.0, 0.0);
299 }
300
301
302 /**
303 * Transpose.
304 */
306 {
307 static_cast<JMatrix3D&>(*this).transpose();
308
309 return *this;
310 }
311
312
313 /**
314 * Matrix multiplication.
315 *
316 * \param A matrix
317 * \param B matrix
318 * \return this matrix
319 */
321 const JRotation3D& B)
322 {
323 static_cast<JMatrix3D&>(*this).mul(A, B);
324
325 return *this;
326 }
327
328
329 /**
330 * Rotate.
331 *
332 * \param __x x value
333 * \param __y y value
334 * \param __z z value
335 */
336 void rotate(double& __x, double& __y, double& __z) const
337 {
338 const double x = a00 * __x + a01 * __y + a02 * __z;
339 const double y = a10 * __x + a11 * __y + a12 * __z;
340 const double z = a20 * __x + a21 * __y + a22 * __z;
341
342 __x = x;
343 __y = y;
344 __z = z;
345 }
346
347
348 /**
349 * Rotate back.
350 *
351 * \param __x x value
352 * \param __y y value
353 * \param __z z value
354 */
355 void rotate_back(double& __x, double& __y, double& __z) const
356 {
357 const double x = a00 * __x + a10 * __y + a20 * __z;
358 const double y = a01 * __x + a11 * __y + a21 * __z;
359 const double z = a02 * __x + a12 * __y + a22 * __z;
360
361 __x = x;
362 __y = y;
363 __z = z;
364 }
365 };
366}
367
368#endif
Base class for data structures with artithmetic capabilities.
Data structure for angles in three dimensions.
Definition JAngle3D.hh:35
double getTheta() const
Get theta angle.
Definition JAngle3D.hh:86
double getPhi() const
Get phi angle.
Definition JAngle3D.hh:97
Data structure for unit quaternion in three dimensions.
JQuaternion3D & normalise()
Normalise quaternion.
double getB() const
Get b value.
double getD() const
Get d value.
double getC() const
Get c value.
double getA() const
Get a value.
JRotation3D(const JRotation3Y &R)
Constructor.
const JRotation3D & getRotation() const
Get rotation.
JRotation3D & transpose()
Transpose.
JRotation3D(const JRotation3Z &R)
Constructor.
void rotate_back(double &__x, double &__y, double &__z) const
Rotate back.
JRotation3D(const JRotation3X &R)
Constructor.
JRotation3D(const JVersor3D &dir)
Constructor.
JRotation3D(const JQuaternion3D &Q)
Constructor.
JRotation3D()
Default constructor (= identity matrix).
JRotation3D & mul(const JRotation3D &A, const JRotation3D &B)
Matrix multiplication.
JRotation3D(const JAngle3D &dir)
Constructor.
void rotate(double &__x, double &__y, double &__z) const
Rotate.
JRotation3D(const JVersor3Z &dir)
Constructor.
Rotation around X-axis.
JRotation3X(const double phi)
Constructor.
JRotation3X()
Default constructor (= identity matrix).
Rotation around Y-axis.
JRotation3Y()
Default constructor (= identity matrix).
JRotation3Y(const double phi)
Constructor.
Rotation around Z-axis.
JRotation3Z()
Default constructor (= identity matrix).
JRotation3Z(const double phi)
Constructor.
Data structure for normalised vector in three dimensions.
Definition JVersor3D.hh:28
double getDY() const
Get y direction.
Definition JVersor3D.hh:106
double getDX() const
Get x direction.
Definition JVersor3D.hh:95
double getDZ() const
Get z direction.
Definition JVersor3D.hh:117
Data structure for normalised vector in positive z-direction.
Definition JVersor3Z.hh:41
double getDZ() const
Get z direction.
Definition JVersor3Z.hh:169
double getDY() const
Get y direction.
Definition JVersor3Z.hh:158
double getDX() const
Get x direction.
Definition JVersor3Z.hh:147
JMatrix3D & mul(const double factor)
Scale matrix.
JMatrix3D & transpose()
Transpose.
JMatrix3D & setIdentity()
Set to identity matrix.
Auxiliary classes and methods for 3D geometrical objects and operations.
Definition JAngle3D.hh:19
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347