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