Jpp 21.0.0-rc.3
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
26 using JMATH::JMath;
27
28
29 /**
30 * Rotation around X-axis.
31 */
33 public JRotation2D
34 {
35 public:
36 /**
37 * Default constructor (= identity matrix).
38 */
41 {}
42
43
44 /**
45 * Constructor.
46 *
47 * \param phi rotation angle (anti-clock wise) [rad]
48 */
49 JRotation3X(const double phi) :
50 JRotation2D(phi)
51 {}
52
53
54 /**
55 * Constructor.
56 * The rotation is defined such that back ration of the same direction aligns with the y-axis.
57 *
58 * \param y y
59 * \param z z
60 */
61 JRotation3X(const double y, const double z) :
63 {}
64 };
65
66
67 /**
68 * Rotation around Y-axis.
69 */
71 public JRotation2D
72 {
73 public:
74 /**
75 * Default constructor (= identity matrix).
76 */
79 {}
80
81
82 /**
83 * Constructor.
84 *
85 * Note that in the x-z plane the corresponding rotation is clock wise.
86 *
87 * \param phi rotation angle (anti-clock wise) [rad]
88 */
89 JRotation3Y(const double phi) :
90 JRotation2D(-phi)
91 {}
92
93
94 /**
95 * Constructor.
96 * The rotation is defined such that back ration of the same direction aligns with the z-axis.
97 *
98 * \param x x
99 * \param z z
100 */
101 JRotation3Y(const double x, const double z) :
103 {}
104 };
105
106
107 /**
108 * Rotation around Z-axis.
109 */
111 public JRotation2D
112 {
113 public:
114 /**
115 * Default constructor (= identity matrix).
116 */
119 {}
120
121
122 /**
123 * Constructor.
124 *
125 * \param phi rotation angle (anti-clock wise) [rad]
126 */
127 JRotation3Z(const double phi) :
128 JRotation2D(phi)
129 {}
130
131
132 /**
133 * Constructor.
134 * The rotation is defined such that back ration of the same direction aligns with the x-axis.
135 *
136 * \param x x
137 * \param y y
138 */
139 JRotation3Z(const double x, const double y) :
141 {}
142 };
143
144
145 /**
146 * Rotation matrix
147 */
149 public JMatrix3D,
150 public JMath<JRotation3D>
151 {
152 public:
153
154 using JMath<JRotation3D>::mul;
155
156 /**
157 * Default constructor (= identity matrix).
158 */
160 JMatrix3D()
161 {
162 setIdentity();
163 }
164
165
166 /**
167 * Constructor.
168 *
169 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
170 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
171 *
172 * \param dir direction
173 */
174 JRotation3D(const JAngle3D& dir) :
175 JMatrix3D()
176 {
177 const double ct = cos(dir.getTheta());
178 const double st = sin(dir.getTheta());
179 const double cp = cos(dir.getPhi());
180 const double sp = sin(dir.getPhi());
181
182 a00 = ct*cp; a01 = ct*sp; a02 = -st;
183 a10 = -sp; a11 = cp; a12 = 0.0;
184 a20 = st*cp; a21 = st*sp; a22 = +ct;
185 }
186
187
188 /**
189 * Constructor.
190 *
191 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
192 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
193 *
194 * \param dir direction
195 */
196 JRotation3D(const JVersor3D& dir) :
197 JMatrix3D()
198 {
199 const double ct = dir.getDZ();
200 const double st = sqrt((1.0 + ct)*(1.0 - ct));
201 const double phi = atan2(dir.getDY(), dir.getDX());
202 const double cp = cos(phi);
203 const double sp = sin(phi);
204
205 a00 = ct*cp; a01 = ct*sp; a02 = -st;
206 a10 = -sp; a11 = cp; a12 = 0.0;
207 a20 = st*cp; a21 = st*sp; a22 = +ct;
208 }
209
210
211 /**
212 * Constructor.
213 *
214 * The matrix is defined such that the rotation of a vector in the given direction ends up along the z-axis
215 * and the back rotation of a vector parallel to the z-axis ends up in the given direction.
216 *
217 * \param dir direction
218 */
219 JRotation3D(const JVersor3Z& dir) :
220 JMatrix3D()
221 {
222 const double ct = dir.getDZ();
223 const double st = sqrt((1.0 + ct)*(1.0 - ct));
224 const double phi = atan2(dir.getDY(), dir.getDX());
225 const double cp = cos(phi);
226 const double sp = sin(phi);
227
228 a00 = ct*cp; a01 = ct*sp; a02 = -st;
229 a10 = -sp; a11 = cp; a12 = 0.0;
230 a20 = st*cp; a21 = st*sp; a22 = +ct;
231 }
232
233
234 /**
235 * Constructor.
236 *
237 * \param R 2D rotation matrix around X-axis
238 */
240 JMatrix3D()
241 {
242 a00 = 1.0; a01 = 0.0; a02 = 0.0;
243 a10 = 0.0; a11 = R.a00; a12 = R.a01;
244 a20 = 0.0; a21 = R.a10; a22 = R.a11;
245 }
246
247
248 /**
249 * Constructor.
250 *
251 * \param R 2D rotation matrix around Y-axis
252 */
254 JMatrix3D()
255 {
256 a00 = R.a00; a01 = 0.0; a02 = R.a01;
257 a10 = 0.0; a11 = 1.0; a12 = 0.0;
258 a20 = R.a10; a21 = 0.0; a22 = R.a11;
259 }
260
261
262 /**
263 * Constructor.
264 *
265 * \param R 2D rotation matrix around Z-axis
266 */
268 JMatrix3D()
269 {
270 a00 = R.a00; a01 = R.a01; a02 = 0.0;
271 a10 = R.a10; a11 = R.a11; a12 = 0.0;
272 a20 = 0.0; a21 = 0.0; a22 = 1.0;
273 }
274
275
276 /**
277 * Constructor.
278 *
279 * \param Q quaternion
280 */
282 JMatrix3D()
283 {
284 const double a2 = Q.getA()*Q.getA();
285 const double b2 = Q.getB()*Q.getB();
286 const double c2 = Q.getC()*Q.getC();
287 const double d2 = Q.getD()*Q.getD();
288
289 const double ab = Q.getA()*Q.getB();
290 const double ac = Q.getA()*Q.getC();
291 const double ad = Q.getA()*Q.getD();
292
293 const double bc = Q.getB()*Q.getC();
294 const double bd = Q.getB()*Q.getD();
295
296 const double cd = Q.getC()*Q.getD();
297
298 a00 = a2 + b2 - c2 - d2; a01 = 2.0*bc - 2.0*ad; a02 = 2.0*bd + 2.0*ac;
299 a10 = 2.0*bc + 2.0*ad; a11 = a2 - b2 + c2 - d2; a12 = 2.0*cd - 2.0*ab;
300 a20 = 2.0*bd - 2.0*ac; a21 = 2.0*cd + 2.0*ab; a22 = a2 - b2 - c2 + d2;
301 }
302
303
304 /**
305 * Get rotation.
306 *
307 * \return rotation
308 */
310 {
311 return static_cast<const JRotation3D&>(*this);
312 }
313
314
315 /**
316 * Type conversion operator.
317 *
318 * \return quaternion
319 */
320 operator JQuaternion3D() const
321 {
322 const double q2 = 0.25 * (1.0 + a00 + a11 + a22);
323
324 if (q2 > 0.0) {
325
326 const double a = sqrt(q2);
327 const double w = 0.25 / a;
328 const double b = (a21 - a12) * w;
329 const double c = (a02 - a20) * w;
330 const double d = (a10 - a01) * w;
331
332 return JQuaternion3D(a,b,c,d).normalise();
333 }
334
335 return JQuaternion3D(1.0, 0.0, 0.0, 0.0);
336 }
337
338
339 /**
340 * Transpose.
341 */
343 {
344 static_cast<JMatrix3D&>(*this).transpose();
345
346 return *this;
347 }
348
349
350 /**
351 * Matrix multiplication.
352 *
353 * \param A matrix
354 * \param B matrix
355 * \return this matrix
356 */
358 const JRotation3D& B)
359 {
360 static_cast<JMatrix3D&>(*this).mul(A, B);
361
362 return *this;
363 }
364
365
366 /**
367 * Rotate.
368 *
369 * \param __x x value
370 * \param __y y value
371 * \param __z z value
372 */
373 void rotate(double& __x, double& __y, double& __z) const
374 {
375 const double x = a00 * __x + a01 * __y + a02 * __z;
376 const double y = a10 * __x + a11 * __y + a12 * __z;
377 const double z = a20 * __x + a21 * __y + a22 * __z;
378
379 __x = x;
380 __y = y;
381 __z = z;
382 }
383
384
385 /**
386 * Rotate back.
387 *
388 * \param __x x value
389 * \param __y y value
390 * \param __z z value
391 */
392 void rotate_back(double& __x, double& __y, double& __z) const
393 {
394 const double x = a00 * __x + a10 * __y + a20 * __z;
395 const double y = a01 * __x + a11 * __y + a21 * __z;
396 const double z = a02 * __x + a12 * __y + a22 * __z;
397
398 __x = x;
399 __y = y;
400 __z = z;
401 }
402 };
403}
404
405#endif
Base class for data structures with artithmetic capabilities.
Data structure for vector in two dimensions.
Definition JVector2D.hh:34
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 y, const double z)
Constructor.
JRotation3X(const double phi)
Constructor.
JRotation3X()
Default constructor (= identity matrix).
Rotation around Y-axis.
JRotation3Y()
Default constructor (= identity matrix).
JRotation3Y(const double x, const double z)
Constructor.
JRotation3Y(const double phi)
Constructor.
Rotation around Z-axis.
JRotation3Z()
Default constructor (= identity matrix).
JRotation3Z(const double x, const double y)
Constructor.
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