Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPosition3D.hh
Go to the documentation of this file.
1 #ifndef __JPOSITION3D__
2 #define __JPOSITION3D__
3 
4 #include <istream>
5 #include <ostream>
6 #include <cmath>
7 
8 #include "JIO/JSerialisable.hh"
10 #include "JGeometry3D/JVector3D.hh"
11 #include "JGeometry3D/JAngle3D.hh"
12 #include "JGeometry3D/JVersor3D.hh"
13 #include "JGeometry3D/JVersor3Z.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JGEOMETRY3D {}
23 namespace JPP { using namespace JGEOMETRY3D; }
24 
25 namespace JGEOMETRY3D {
26 
27  using JIO::JReader;
28  using JIO::JWriter;
30 
31 
32  /**
33  * Data structure for position in three dimensions.
34  */
35  class JPosition3D :
36  public JVector3D
37  {
38  public:
39 
40  using JVector3D::getDot;
42 
43 
44  /**
45  * Default constructor.
46  */
48  JVector3D()
49  {}
50 
51 
52  /**
53  * Constructor.
54  *
55  * \param pos position
56  */
57  JPosition3D(const JVector3D& pos) :
58  JVector3D(pos)
59  {}
60 
61 
62  /**
63  * Constructor.
64  *
65  * \param angle angle
66  */
67  JPosition3D(const JAngle3D& angle) :
68  JVector3D(angle.getDX(),
69  angle.getDY(),
70  angle.getDZ())
71  {}
72 
73 
74  /**
75  * Constructor.
76  *
77  * \param dir direction
78  */
79  JPosition3D(const JVersor3D& dir) :
80  JVector3D(dir.getDX(),
81  dir.getDY(),
82  dir.getDZ())
83  {}
84 
85 
86  /**
87  * Constructor.
88  *
89  * \param dir direction
90  */
91  JPosition3D(const JVersor3Z& dir) :
92  JVector3D(dir.getDX(),
93  dir.getDY(),
94  dir.getDZ())
95  {}
96 
97 
98  /**
99  * Constructor.
100  *
101  * \param pos 2D position
102  * \param z z value
103  */
104  JPosition3D(const JVector2D& pos,
105  const double z) :
106  JVector3D(pos.getX(), pos.getY(), z)
107  {}
108 
109 
110  /**
111  * Constructor.
112  *
113  * \param x x value
114  * \param y y value
115  * \param z z value
116  */
117  JPosition3D(const double x,
118  const double y,
119  const double z) :
120  JVector3D(x,y,z)
121  {}
122 
123 
124  /**
125  * Get position.
126  *
127  * \return position
128  */
129  const JPosition3D& getPosition() const
130  {
131  return static_cast<const JPosition3D&>(*this);
132  }
133 
134 
135  /**
136  * Get position.
137  *
138  * \return position
139  */
141  {
142  return static_cast<JPosition3D&>(*this);
143  }
144 
145 
146  /**
147  * Set position.
148  *
149  * \param pos position
150  */
151  void setPosition(const JVector3D& pos)
152  {
153  static_cast<JVector3D&>(*this) = pos;
154  }
155 
156 
157  /**
158  * Type conversion operator.
159  *
160  * \return angle
161  */
162  operator JAngle3D() const
163  {
164  return JAngle3D(getX(), getY(), getZ());
165  }
166 
167 
168  /**
169  * Type conversion operator.
170  *
171  * \return direction
172  */
173  operator JVersor3D() const
174  {
175  return JVersor3D(getX(), getY(), getZ());
176  }
177 
178 
179  /**
180  * Rotate.
181  *
182  * \param R rotation matrix
183  * \return this position
184  */
186  {
187  R.rotate(__x, __y, __z);
188 
189  return *this;
190  }
191 
192 
193  /**
194  * Rotate back.
195  *
196  * \param R rotation matrix
197  * \return this position
198  */
200  {
201  R.rotate_back(__x, __y, __z);
202 
203  return *this;
204  }
205 
206 
207  /**
208  * Rotate around X-axis.
209  *
210  * \param R rotation matrix
211  * \return this position
212  */
214  {
215  R.rotate(__y, __z);
216 
217  return *this;
218  }
219 
220 
221  /**
222  * Rotate back around X-axis.
223  *
224  * \param R rotation matrix
225  * \return this position
226  */
228  {
229  R.rotate_back(__y, __z);
230 
231  return *this;
232  }
233 
234 
235  /**
236  * Rotate around Y-axis.
237  *
238  * \param R rotation matrix
239  * \return this position
240  */
242  {
243  R.rotate(__x, __z);
244 
245  return *this;
246  }
247 
248 
249  /**
250  * Rotate back around Y-axis.
251  *
252  * \param R rotation matrix
253  * \return this position
254  */
256  {
257  R.rotate_back(__x, __z);
258 
259  return *this;
260  }
261 
262 
263  /**
264  * Rotate around Z-axis.
265  *
266  * \param R rotation matrix
267  * \return this position
268  */
270  {
271  R.rotate(__x, __y);
272 
273  return *this;
274  }
275 
276 
277  /**
278  * Rotate back around Z-axis.
279  *
280  * \param R rotation matrix
281  * \return this position
282  */
284  {
285  R.rotate_back(__x, __y);
286 
287  return *this;
288  }
289 
290 
291  /**
292  * Rotate.
293  *
294  * \param Q quaternion
295  * \return this position
296  */
298  {
299  JQuaternion3D b(Q);
300  JQuaternion3D a;
301 
302  a.mul(getPosition(), b.conjugate());
303 
304  setPosition(b.mul(Q,a));
305 
306  return *this;
307  }
308 
309 
310  /**
311  * Transform position.
312  *
313  * The final position is obtained as follows:
314  * -# rotation of the position according matrix R;
315  * -# offset position with pos;
316  * -# rotation of position around z-axis, such that final position lies in x-z plane;
317  *
318  * \param R rotation matrix
319  * \param pos position of origin (after rotation)
320  */
321  void transform(const JRotation3D& R,
322  const JVector3D& pos)
323  {
324  // rotate geometry to system with particle direction along z-axis
325 
326  rotate(R);
327 
328  // offset with respect to origin
329 
330  sub(pos);
331 
332  // rotate geometry to x-z plane
333 
334  __x = sqrt(__x*__x + __y*__y);
335  __y = 0.0;
336  }
337 
338 
339  /**
340  * Transform back position.
341  *
342  * The final position is obtained as follows:
343  * -# offset position with position pos;
344  * -# rotation of postion according matrix R;
345  *
346  * \param R rotation matrix
347  * \param pos position of origin (before rotation)
348  */
350  const JVector3D& pos)
351  {
352  // offset with respect to origin
353 
354  add(pos);
355 
356  // rotate back geometry to system with particle direction along z-axis
357 
358  rotate_back(R);
359  }
360 
361 
362  /**
363  * Get dot product.
364  *
365  * \param angle angle
366  * \return dot product
367  */
368  double getDot(const JAngle3D& angle) const
369  {
370  return
371  getX() * angle.getDX() +
372  getY() * angle.getDY() +
373  getZ() * angle.getDZ();
374  }
375 
376 
377  /**
378  * Get dot product.
379  *
380  * \param dir direction
381  * \return dot product
382  */
383  double getDot(const JVersor3D& dir) const
384  {
385  return
386  getX() * dir.getDX() +
387  getY() * dir.getDY() +
388  getZ() * dir.getDZ();
389  }
390 
391 
392  /**
393  * Get dot product.
394  *
395  * \param dir direction
396  * \return dot product
397  */
398  double getDot(const JVersor3Z& dir) const
399  {
400  return
401  getX() * dir.getDX() +
402  getY() * dir.getDY() +
403  getZ() * dir.getDZ();
404  }
405 
406 
407  /**
408  * Read position from input.
409  *
410  * \param in input stream
411  * \param position position
412  * \return input stream
413  */
414  friend inline std::istream& operator>>(std::istream& in, JPosition3D& position)
415  {
416  in >> position.__x >> position.__y >> position.__z;
417 
418  return in;
419  }
420 
421 
422  /**
423  * Write position to output.
424  *
425  * \param out output stream
426  * \param position position
427  * \return output stream
428  */
429  friend inline std::ostream& operator<<(std::ostream& out, const JPosition3D& position)
430  {
431  out << position.getX() << ' ' << position.getY() << ' ' << position.getZ();
432 
433  return out;
434  }
435 
436 
437  /**
438  * Read position from input.
439  *
440  * \param in reader
441  * \param position position
442  * \return reader
443  */
444  friend inline JReader& operator>>(JReader& in, JPosition3D& position)
445  {
446  return in >> position.__x >> position.__y >> position.__z;
447  }
448 
449 
450  /**
451  * Write position to output.
452  *
453  * \param out writer
454  * \param position position
455  * \return writer
456  */
457  friend inline JWriter& operator<<(JWriter& out, const JPosition3D& position)
458  {
459  return out << position.getX() << position.getY() << position.getZ();
460  }
461  };
462 }
463 
464 #endif
double getDot(const JVersor3D &dir) const
Get dot product.
Definition: JPosition3D.hh:383
JPosition3D(const JVector3D &pos)
Constructor.
Definition: JPosition3D.hh:57
Data structure for vector in two dimensions.
Definition: JVector2D.hh:30
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
JPosition3D & rotate(const JRotation3Z &R)
Rotate around Z-axis.
Definition: JPosition3D.hh:269
Interface for binary output.
double getDot(const JAngle3D &angle) const
Get dot product.
Definition: JPosition3D.hh:368
void transform_back(const JRotation3D &R, const JVector3D &pos)
Transform back position.
Definition: JPosition3D.hh:349
JPosition3D & rotate(const JRotation3Y &R)
Rotate around Y-axis.
Definition: JPosition3D.hh:241
void rotate(double &__x, double &__y) const
Rotate.
Definition: JRotation2D.hh:96
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:199
JPosition3D(const JVersor3D &dir)
Constructor.
Definition: JPosition3D.hh:79
void rotate(double &__x, double &__y, double &__z) const
Rotate.
Definition: JRotation3D.hh:277
double getDot(const JVersor3Z &dir) const
Get dot product.
Definition: JPosition3D.hh:398
void rotate_back(double &__x, double &__y) const
Rotate back.
Definition: JRotation2D.hh:112
void rotate_back(double &__x, double &__y, double &__z) const
Rotate back.
Definition: JRotation3D.hh:296
Rotation matrix.
Definition: JRotation3D.hh:108
friend JReader & operator>>(JReader &in, JPosition3D &position)
Read position from input.
Definition: JPosition3D.hh:444
JPosition3D & rotate_back(const JRotation3Y &R)
Rotate back around Y-axis.
Definition: JPosition3D.hh:255
friend JWriter & operator<<(JWriter &out, const JPosition3D &position)
Write position to output.
Definition: JPosition3D.hh:457
void transform(const JRotation3D &R, const JVector3D &pos)
Transform position.
Definition: JPosition3D.hh:321
JQuaternion3D & mul(const double factor)
Scale quaternion.
double getDY() const
Get y direction.
Definition: JVersor3Z.hh:155
Rotation around Z-axis.
Definition: JRotation3D.hh:82
JPosition3D(const JAngle3D &angle)
Constructor.
Definition: JPosition3D.hh:67
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:156
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
double getDY() const
Get y direction.
Definition: JVersor3D.hh:101
double getDX() const
Get x direction.
Definition: JVersor3D.hh:90
JPosition3D(const JVector2D &pos, const double z)
Constructor.
Definition: JPosition3D.hh:104
JPosition3D & getPosition()
Get position.
Definition: JPosition3D.hh:140
JPosition3D & rotate_back(const JRotation3Z &R)
Rotate back around Z-axis.
Definition: JPosition3D.hh:283
double getY() const
Get y position.
Definition: JVector3D.hh:102
Rotation around Y-axis.
Definition: JRotation3D.hh:54
Interface for binary input.
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
Data structure for quaternion in three dimensions.
JPosition3D & rotate(const JQuaternion3D &Q)
Rotate.
Definition: JPosition3D.hh:297
JPosition3D(const double x, const double y, const double z)
Constructor.
Definition: JPosition3D.hh:117
JPosition3D()
Default constructor.
Definition: JPosition3D.hh:47
double getDX() const
Get x direction.
Definition: JVersor3Z.hh:144
double getX() const
Get x position.
Definition: JVector3D.hh:92
Rotation around X-axis.
Definition: JRotation3D.hh:28
JPosition3D(const JVersor3Z &dir)
Constructor.
Definition: JPosition3D.hh:91
double getDot(const JVector3D &vector) const
Get dot product.
Definition: JVector3D.hh:278
JPosition3D & rotate(const JRotation3X &R)
Rotate around X-axis.
Definition: JPosition3D.hh:213
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
friend std::ostream & operator<<(std::ostream &out, const JPosition3D &position)
Write position to output.
Definition: JPosition3D.hh:429
double getDZ() const
Get z direction.
Definition: JVersor3Z.hh:166
JPosition3D & rotate_back(const JRotation3X &R)
Rotate back around X-axis.
Definition: JPosition3D.hh:227
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:23
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:36
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
double getZ() const
Get z position.
Definition: JVector3D.hh:113
double getDZ() const
Get z direction.
Definition: JVersor3D.hh:112
double getDX() const
Get x direction.
Definition: JAngle3D.hh:105
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:140
friend std::istream & operator>>(std::istream &in, JPosition3D &position)
Read position from input.
Definition: JPosition3D.hh:414
double getDZ() const
Get z direction.
Definition: JAngle3D.hh:127
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:151
double getDY() const
Get y direction.
Definition: JAngle3D.hh:116
JQuaternion3D & conjugate()
Conjugate quaternion.
JVector3D & transform(const JMatrix3D &T)
Transform.
Definition: JVector3D.hh:204