Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAxis3D.hh
Go to the documentation of this file.
1 #ifndef __JAXIS3D__
2 #define __JAXIS3D__
3 
4 #include <istream>
5 #include <ostream>
6 #include <cmath>
7 
8 #include "JIO/JSerialisable.hh"
10 #include "JGeometry3D/JVersor3D.hh"
11 #include "JGeometry3D/JVersor3Z.hh"
17 
18 
19 /**
20  * \author mdejong
21  */
22 
23 namespace JGEOMETRY3D {}
24 namespace JPP { using namespace JGEOMETRY3D; }
25 
26 namespace JGEOMETRY3D {
27 
28  using JIO::JReader;
29  using JIO::JWriter;
30 
31 
32  /**
33  * Axis object.
34  *
35  * An axis object consists of a position and a direction.
36  */
37  class JAxis3D :
38  public JPosition3D,
39  public JDirection3D
40  {
41  public:
42 
44 
45 
46  /**
47  * Default constructor.
48  */
49  JAxis3D() :
50  JPosition3D (),
51  JDirection3D()
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param pos position
59  */
60  JAxis3D(const JVector3D& pos) :
61  JPosition3D (pos),
62  JDirection3D()
63  {}
64 
65 
66  /**
67  * Constructor.
68  *
69  * \param pos position
70  * \param dir direction
71  */
72  JAxis3D(const JVector3D& pos,
73  const JVersor3D& dir) :
74  JPosition3D (pos),
75  JDirection3D(dir)
76  {}
77 
78 
79  /**
80  * Constructor.
81  *
82  * \param pos position
83  * \param dir direction
84  */
85  JAxis3D(const JVector3D& pos,
86  const JVersor3Z& dir) :
87  JPosition3D (pos),
88  JDirection3D(dir)
89  {}
90 
91 
92  /**
93  * Constructor.
94  *
95  * \param segment line segment
96  */
97  JAxis3D(const JSegment3D& segment) :
98  JPosition3D (segment.first),
99  JDirection3D(segment.second - segment.first)
100  {}
101 
102 
103  /**
104  * Get axis.
105  *
106  * \return axis
107  */
108  const JAxis3D& getAxis() const
109  {
110  return *this;
111  }
112 
113 
114  /**
115  * Set axis.
116  *
117  * \param axis axis
118  */
119  void setAxis(const JAxis3D& axis)
120  {
121  *this = axis;
122  }
123 
124 
125  /**
126  * Negate axis.
127  *
128  * \return this axis
129  */
131  {
132  static_cast<JPosition3D&> (*this).negate();
133  static_cast<JDirection3D&>(*this).negate();
134 
135  return *this;
136  }
137 
138 
139  /**
140  * Move vertex along this axis.
141  *
142  * \param step step
143  */
144  void move(const double step)
145  {
146  getPosition() += step * JPosition3D(getDirection());
147  }
148 
149 
150  /**
151  * Get longitudinal position along axis of position of closest approach with given position.
152  *
153  * \param pos position
154  * \return longitudinal position
155  */
156  inline double getIntersection(const JVector3D& pos) const
157  {
158  JPosition3D D(pos);
159 
160  D.sub(this->getPosition());
161 
162  return D.getDot(this->getDirection());
163  }
164 
165 
166  /**
167  * Get longitudinal position along axis of position of closest approach with given axis.\n
168  * If the axes are paralel, this position corresponds to the vertex of the given axis.
169  *
170  * \param axis axis
171  * \param precision precision
172  * \return longitudinal position
173  */
174  double getIntersection(const JAxis3D& axis, const double precision = 1.0e-8) const
175  {
176  JPosition3D pos(getPosition ());
177  JDirection3D dir(getDirection());
178 
179  const JRotation3D R(axis.getDirection());
180 
181  // offset this position with position of given axis
182 
183  pos.sub(axis.getPosition());
184 
185  // rotate along given axis
186 
187  pos.rotate(R);
188  dir.rotate(R);
189 
190  // longitudinal position at minimal distance of approach
191 
192  const double alpha = pos.getX() * dir.getDX() + pos.getY() * dir.getDY();
193  const double beta = dir.getDX() * dir.getDX() + dir.getDY() * dir.getDY();
194 
195  return (fabs(beta) > precision ? -alpha/beta : -pos.getZ());
196  }
197 
198 
199  /**
200  * Get distance squared.
201  *
202  * \param pos position
203  * \return square of distance
204  */
205  inline double getDistanceSquared(const JVector3D& pos) const
206  {
207  JPosition3D D(pos);
208 
209  D.sub(this->getPosition());
210 
211  const double dz = D.getDot(this->getDirection());
212 
213  return D.getLengthSquared() - dz*dz;
214  }
215 
216 
217  /**
218  * Get distance.
219  *
220  * \param pos osition
221  * \return distance
222  */
223  inline double getDistance(const JVector3D& pos) const
224  {
225  return sqrt(this->getDistanceSquared(pos));
226  }
227 
228 
229  /**
230  * Rotate axis.
231  *
232  * \param R rotation matrix
233  * \return this axis
234  */
236  {
237  static_cast<JPosition3D&> (*this).rotate(R);
238  static_cast<JDirection3D&>(*this).rotate(R);
239 
240  return *this;
241  }
242 
243 
244  /**
245  * Rotate back axis.
246  *
247  * \param R rotation matrix
248  * \return this axis
249  */
251  {
252  static_cast<JPosition3D&> (*this).rotate_back(R);
253  static_cast<JDirection3D&>(*this).rotate_back(R);
254 
255  return *this;
256  }
257 
258 
259  /**
260  * Rotate around X-axis.
261  *
262  * \param R rotation matrix
263  * \return this axis
264  */
266  {
267  static_cast<JPosition3D&> (*this).rotate(R);
268  static_cast<JDirection3D&>(*this).rotate(R);
269 
270  return *this;
271  }
272 
273 
274  /**
275  * Rotate back around X-axis.
276  *
277  * \param R rotation matrix
278  * \return this axis
279  */
281  {
282  static_cast<JPosition3D&> (*this).rotate_back(R);
283  static_cast<JDirection3D&>(*this).rotate_back(R);
284 
285  return *this;
286  }
287 
288 
289  /**
290  * Rotate around Y-axis.
291  *
292  * \param R rotation matrix
293  * \return this axis
294  */
296  {
297  static_cast<JPosition3D&> (*this).rotate(R);
298  static_cast<JDirection3D&>(*this).rotate(R);
299 
300  return *this;
301  }
302 
303 
304  /**
305  * Rotate back around Y-axis.
306  *
307  * \param R rotation matrix
308  * \return this axis
309  */
311  {
312  static_cast<JPosition3D&> (*this).rotate_back(R);
313  static_cast<JDirection3D&>(*this).rotate_back(R);
314 
315  return *this;
316  }
317 
318 
319  /**
320  * Rotate around Z-axis.
321  *
322  * \param R rotation matrix
323  * \return this axis
324  */
326  {
327  static_cast<JPosition3D&> (*this).rotate(R);
328  static_cast<JDirection3D&>(*this).rotate(R);
329 
330  return *this;
331  }
332 
333 
334  /**
335  * Rotate back around Z-axis.
336  *
337  * \param R rotation matrix
338  * \return his axis
339  */
341  {
342  static_cast<JPosition3D&> (*this).rotate_back(R);
343  static_cast<JDirection3D&>(*this).rotate_back(R);
344 
345  return *this;
346  }
347 
348 
349  /**
350  * Transform axis to reference frame of given axis.
351  *
352  * \param axis axis
353  */
354  void transform(const JAxis3D& axis)
355  {
356  JRotation3D R (axis.getDirection());
357  JPosition3D pos(axis.getPosition ());
358 
359  transform(R, pos.rotate(R));
360  }
361 
362 
363  /**
364  * Transform axis.
365  *
366  * The final position and direction are obtained as follows:
367  * -# rotation of the position and the direction according rotation matrix (\c R);
368  * -# offset position with position of origin in rotated system (\c pos);
369  * -# rotation of position and direction around z-axis, such that final position is in x-z plane;
370  *
371  * \param R rotation matrix
372  * \param pos position of origin
373  */
374  void transform(const JRotation3D& R,
375  const JVector3D& pos)
376  {
377  JPosition3D& u = getPosition ();
378  JDirection3D& v = getDirection();
379 
380  // rotate axis to system such that direction is along z-axis
381 
382  u.rotate(R);
383  v.rotate(R);
384 
385  // offset with respect to origin
386 
387  u.sub(pos);
388 
389  // rotate axis to x-z plane such that position is in x-z plane
390 
391  const double phi = atan2(u.getY(), u.getX());
392 
393  const JRotation3Z r(-phi);
394 
395  u.rotate(r);
396  v.rotate(r);
397  }
398 
399 
400  /**
401  * Transform back axis.
402  *
403  * The final position and direction are obtained as follows:
404  * -# offset position with position pos;
405  * -# rotation of position and direction according matrix R;
406  *
407  * \param R rotation matrix
408  * \param pos position of origin (before rotation)
409  */
411  const JVector3D& pos)
412  {
413  JPosition3D& u = getPosition ();
414  JDirection3D& v = getDirection();
415 
416  // offset with respect to origin
417 
418  u.add(pos);
419 
420  // rotate back axis to system with original particle direction
421 
422  u.rotate_back(R);
423  v.rotate_back(R);
424  }
425 
426 
427  /**
428  * Transform axis.
429  *
430  * \param T transformation
431  */
433  {
435  }
436 
437 
438  /**
439  * Transform back axis.
440  *
441  * \param T transformation
442  */
444  {
446  }
447 
448 
449  /**
450  * Read axis from input.
451  *
452  * \param in input stream
453  * \param axis axis
454  * \return input stream
455  */
456  friend inline std::istream& operator>>(std::istream& in, JAxis3D& axis)
457  {
458  in >> static_cast<JPosition3D&> (axis);
459  in >> static_cast<JDirection3D&>(axis);
460 
461  return in;
462  }
463 
464 
465  /**
466  * Write axis to output.
467  *
468  * \param out output stream
469  * \param axis axis
470  * \return output stream
471  */
472  friend inline std::ostream& operator<<(std::ostream& out, const JAxis3D& axis)
473  {
474  out << static_cast<const JPosition3D&> (axis);
475  out << ' ';
476  out << static_cast<const JDirection3D&>(axis);
477 
478  return out;
479  }
480 
481 
482  /**
483  * Read axis from input.
484  *
485  * \param in reader
486  * \param axis axis
487  * \return reader
488  */
489  friend inline JReader& operator>>(JReader& in, JAxis3D& axis)
490  {
491  in >> static_cast<JPosition3D&> (axis);
492  in >> static_cast<JDirection3D&>(axis);
493 
494  return in;
495  }
496 
497 
498  /**
499  * Write axis to output.
500  *
501  * \param out writer
502  * \param axis axis
503  * \return writer
504  */
505  friend inline JWriter& operator<<(JWriter& out, const JAxis3D& axis)
506  {
507  out << static_cast<const JPosition3D&> (axis);
508  out << static_cast<const JDirection3D&>(axis);
509 
510  return out;
511  }
512  };
513 }
514 
515 #endif
Interface for binary output.
double getDot(const JAngle3D &angle) const
Get dot product.
Definition: JPosition3D.hh:368
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:32
JDirection3D & rotate_back(const JRotation3D &R)
Rotate back.
double getDistanceSquared(const JVector3D &pos) const
Get distance squared.
Definition: JAxis3D.hh:205
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:199
JAxis3D & rotate(const JRotation3Y &R)
Rotate around Y-axis.
Definition: JAxis3D.hh:295
void transform(const JRotation3D &R, const JVector3D &pos)
Transform axis.
Definition: JAxis3D.hh:374
void transform_back(const JTransformation3D &T)
Transform back axis.
Definition: JAxis3D.hh:443
const JDirection3D & getDirection() const
Get direction.
double getIntersection(const JVector3D &pos) const
Get longitudinal position along axis of position of closest approach with given position.
Definition: JAxis3D.hh:156
JAxis3D()
Default constructor.
Definition: JAxis3D.hh:49
friend JReader & operator>>(JReader &in, JAxis3D &axis)
Read axis from input.
Definition: JAxis3D.hh:489
Rotation matrix.
Definition: JRotation3D.hh:108
double getIntersection(const JAxis3D &axis, const double precision=1.0e-8) const
Get longitudinal position along axis of position of closest approach with given axis.
Definition: JAxis3D.hh:174
void setAxis(const JAxis3D &axis)
Set axis.
Definition: JAxis3D.hh:119
JAxis3D & rotate_back(const JRotation3X &R)
Rotate back around X-axis.
Definition: JAxis3D.hh:280
void move(const double step)
Move vertex along this axis.
Definition: JAxis3D.hh:144
double getDot(const JAngle3D &angle) const
Get dot product.
JAxis3D & rotate_back(const JRotation3Y &R)
Rotate back around Y-axis.
Definition: JAxis3D.hh:310
void transform(const JAxis3D &axis)
Transform axis to reference frame of given axis.
Definition: JAxis3D.hh:354
JAxis3D(const JVector3D &pos, const JVersor3Z &dir)
Constructor.
Definition: JAxis3D.hh:85
Axis object.
Definition: JAxis3D.hh:37
Rotation around Z-axis.
Definition: JRotation3D.hh:82
void transform_back(const JRotation3D &R, const JVector3D &pos)
Transform back axis.
Definition: JAxis3D.hh:410
JAxis3D & rotate(const JRotation3Z &R)
Rotate around Z-axis.
Definition: JAxis3D.hh:325
JAxis3D & rotate(const JRotation3X &R)
Rotate around X-axis.
Definition: JAxis3D.hh:265
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:156
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:235
JVersor3D & negate()
Negate versor.
Definition: JVersor3D.hh:61
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
JDirection3D & rotate(const JRotation3D &R)
Rotate.
friend JWriter & operator<<(JWriter &out, const JAxis3D &axis)
Write axis to output.
Definition: JAxis3D.hh:505
friend std::istream & operator>>(std::istream &in, JAxis3D &axis)
Read axis from input.
Definition: JAxis3D.hh:456
double getDY() const
Get y direction.
Definition: JVersor3D.hh:101
double getDX() const
Get x direction.
Definition: JVersor3D.hh:90
JAxis3D & rotate_back(const JRotation3D &R)
Rotate back axis.
Definition: JAxis3D.hh:250
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
Line segment in two dimensions.
Definition: JSegment3D.hh:33
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:231
void transform(const JTransformation3D &T)
Transform axis.
Definition: JAxis3D.hh:432
const JRotation3D & getRotation() const
Get rotation.
Definition: JRotation3D.hh:237
double getDistance(const JVector3D &pos) const
Get distance.
Definition: JAxis3D.hh:223
JPosition3D()
Default constructor.
Definition: JPosition3D.hh:47
JAxis3D(const JVector3D &pos, const JVersor3D &dir)
Constructor.
Definition: JAxis3D.hh:72
JAxis3D(const JSegment3D &segment)
Constructor.
Definition: JAxis3D.hh:97
const JAxis3D & getAxis() const
Get axis.
Definition: JAxis3D.hh:108
double getX() const
Get x position.
Definition: JVector3D.hh:92
Rotation around X-axis.
Definition: JRotation3D.hh:28
JAxis3D & negate()
Negate axis.
Definition: JAxis3D.hh:130
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
JVector3D & negate()
Negate vector.
Definition: JVector3D.hh:124
JAxis3D(const JVector3D &pos)
Constructor.
Definition: JAxis3D.hh:60
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
JAxis3D & rotate_back(const JRotation3Z &R)
Rotate back around Z-axis.
Definition: JAxis3D.hh:340
double getZ() const
Get z position.
Definition: JVector3D.hh:113
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:140
friend std::ostream & operator<<(std::ostream &out, const JAxis3D &axis)
Write axis to output.
Definition: JAxis3D.hh:472