Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRotation3D.hh
Go to the documentation of this file.
1 #ifndef __JROTATION3D__
2 #define __JROTATION3D__
3 
4 #include <cmath>
5 
9 #include "JMath/JMath.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JGEOMETRY3D {}
17 namespace JPP { using namespace JGEOMETRY3D; }
18 
19 namespace JGEOMETRY3D {
20 
22  using JMATH::JMath;
23 
24 
25  /**
26  * Rotation around X-axis.
27  */
28  class JRotation3X :
29  public JRotation2D
30  {
31  public:
32  /**
33  * Default constructor (= identity matrix).
34  */
36  JRotation2D()
37  {}
38 
39 
40  /**
41  * Constructor.
42  *
43  * \param phi rotation angle (anti-clock wise) [rad]
44  */
45  JRotation3X(const double phi) :
46  JRotation2D(phi)
47  {}
48  };
49 
50 
51  /**
52  * Rotation around Y-axis.
53  */
54  class JRotation3Y :
55  public JRotation2D
56  {
57  public:
58  /**
59  * Default constructor (= identity matrix).
60  */
62  JRotation2D()
63  {}
64 
65 
66  /**
67  * Constructor.
68  *
69  * Note that in the x-z plane the corresponding rotation is clock wise.
70  *
71  * \param phi rotation angle (anti-clock wise) [rad]
72  */
73  JRotation3Y(const double phi) :
74  JRotation2D(-phi)
75  {}
76  };
77 
78 
79  /**
80  * Rotation around Z-axis.
81  */
82  class JRotation3Z :
83  public JRotation2D
84  {
85  public:
86  /**
87  * Default constructor (= identity matrix).
88  */
90  JRotation2D()
91  {}
92 
93 
94  /**
95  * Constructor.
96  *
97  * \param phi rotation angle (anti-clock wise) [rad]
98  */
99  JRotation3Z(const double phi) :
100  JRotation2D(phi)
101  {}
102  };
103 
104 
105  /**
106  * Rotation matrix
107  */
108  class JRotation3D :
109  public JMatrix3D,
110  public JMath<JRotation3D>
111  {
112  public:
113 
115 
117 
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  * \param dev derivative
159  */
160  JRotation3D(const JAngle3D& dir,
161  const JDerivative_t& dev) :
162  JMatrix3D()
163  {
164  const double ct = cos(dir.getTheta());
165  const double st = sin(dir.getTheta());
166  const double cp = cos(dir.getPhi());
167  const double sp = sin(dir.getPhi());
168 
169  switch (dev) {
170 
171  case THETA:
172 
173  a00 = -st*cp; a01 = -st*sp; a02 = -ct;
174  a10 = 0.0; a11 = 0.0; a12 = 0.0;
175  a20 = ct*cp; a21 = ct*sp; a22 = -st;
176 
177  break;
178 
179  case PHI:
180 
181  a00 = -ct*sp; a01 = ct*cp; a02 = 0.0;
182  a10 = -cp; a11 = -sp; a12 = 0.0;
183  a20 = -st*sp; a21 = st*cp; a22 = 0.0;
184 
185  break;
186  }
187  }
188 
189 
190  /**
191  * Constructor.
192  *
193  * \param R 2D rotation matrix around X-axis
194  */
196  JMatrix3D()
197  {
198  a00 = 1.0; a01 = 0.0; a02 = 0.0;
199  a10 = 0.0; a11 = R.a00; a12 = R.a01;
200  a20 = 0.0; a21 = R.a10; a22 = R.a11;
201  }
202 
203 
204  /**
205  * Constructor.
206  *
207  * \param R 2D rotation matrix around Y-axis
208  */
210  JMatrix3D()
211  {
212  a00 = R.a00; a01 = 0.0; a02 = R.a01;
213  a10 = 0.0; a11 = 1.0; a12 = 0.0;
214  a20 = R.a10; a21 = 0.0; a22 = R.a11;
215  }
216 
217 
218  /**
219  * Constructor.
220  *
221  * \param R 2D rotation matrix around Z-axis
222  */
224  JMatrix3D()
225  {
226  a00 = R.a00; a01 = R.a01; a02 = 0.0;
227  a10 = R.a10; a11 = R.a11; a12 = 0.0;
228  a20 = 0.0; a21 = 0.0; a22 = 1.0;
229  }
230 
231 
232  /**
233  * Get rotation.
234  *
235  * \return rotation
236  */
237  const JRotation3D& getRotation() const
238  {
239  return static_cast<const JRotation3D&>(*this);
240  }
241 
242 
243  /**
244  * Transpose.
245  */
247  {
248  static_cast<JMatrix3D&>(*this).transpose();
249 
250  return *this;
251  }
252 
253 
254  /**
255  * Matrix multiplication.
256  *
257  * \param A matrix
258  * \param B matrix
259  * \return this matrix
260  */
262  const JRotation3D& B)
263  {
264  static_cast<JMatrix3D&>(*this).mul(A, B);
265 
266  return *this;
267  }
268 
269 
270  /**
271  * Rotate.
272  *
273  * \param __x x value
274  * \param __y y value
275  * \param __z z value
276  */
277  void rotate(double& __x, double& __y, double& __z) const
278  {
279  const double x = a00 * __x + a01 * __y + a02 * __z;
280  const double y = a10 * __x + a11 * __y + a12 * __z;
281  const double z = a20 * __x + a21 * __y + a22 * __z;
282 
283  __x = x;
284  __y = y;
285  __z = z;
286  }
287 
288 
289  /**
290  * Rotate back.
291  *
292  * \param __x x value
293  * \param __y y value
294  * \param __z z value
295  */
296  void rotate_back(double& __x, double& __y, double& __z) const
297  {
298  const double x = a00 * __x + a10 * __y + a20 * __z;
299  const double y = a01 * __x + a11 * __y + a21 * __z;
300  const double z = a02 * __x + a12 * __y + a22 * __z;
301 
302  __x = x;
303  __y = y;
304  __z = z;
305  }
306  };
307 }
308 
309 #endif
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
JRotation3D & transpose()
Transpose.
Definition: JRotation3D.hh:246
Rotation matrix.
Definition: JRotation2D.hh:23
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
double getPhi() const
Get phi angle.
Definition: JAngle3D.hh:94
JRotation3D(const JRotation3Y &R)
Constructor.
Definition: JRotation3D.hh:209
JRotation3Y(const double phi)
Constructor.
Definition: JRotation3D.hh:73
void rotate(double &__x, double &__y, double &__z) const
Rotate.
Definition: JRotation3D.hh:277
3 x 3 matrix
void rotate_back(double &__x, double &__y, double &__z) const
Rotate back.
Definition: JRotation3D.hh:296
JRotation3Y()
Default constructor (= identity matrix).
Definition: JRotation3D.hh:61
JRotation3D(const JAngle3D &dir, const JDerivative_t &dev)
Constructor.
Definition: JRotation3D.hh:160
Rotation matrix.
Definition: JRotation3D.hh:108
JMatrix3D & mul(const double factor)
Scale matrix.
JMatrix3D & transpose()
Transpose.
JRotation3Z()
Default constructor (= identity matrix).
Definition: JRotation3D.hh:89
double getTheta() const
Get theta angle.
Definition: JAngle3D.hh:83
Rotation around Z-axis.
Definition: JRotation3D.hh:82
JRotation3D()
Default constructor (= identity matrix).
Definition: JRotation3D.hh:122
JRotation3X()
Default constructor (= identity matrix).
Definition: JRotation3D.hh:35
Rotation around Y-axis.
Definition: JRotation3D.hh:54
JRotation3D(const JAngle3D &dir)
Constructor.
Definition: JRotation3D.hh:137
const JRotation3D & getRotation() const
Get rotation.
Definition: JRotation3D.hh:237
JRotation3X(const double phi)
Constructor.
Definition: JRotation3D.hh:45
JRotation3Z(const double phi)
Constructor.
Definition: JRotation3D.hh:99
Rotation around X-axis.
Definition: JRotation3D.hh:28
Base class for data structures with artithmetic capabilities.
JRotation3D(const JRotation3Z &R)
Constructor.
Definition: JRotation3D.hh:223
JMatrix3D & setIdentity()
Set to identity matrix.
JRotation3D & mul(const JRotation3D &A, const JRotation3D &B)
Matrix multiplication.
Definition: JRotation3D.hh:261
JRotation3D(const JRotation3X &R)
Constructor.
Definition: JRotation3D.hh:195