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