Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JVersor3Z.hh
Go to the documentation of this file.
1 #ifndef __JVERSOR3Z__
2 #define __JVERSOR3Z__
3 
4 #include <istream>
5 #include <ostream>
6 #include <cmath>
7 
8 #include "JIO/JSerialisable.hh"
9 #include "JMath/JMath.hh"
10 #include "JGeometry2D/JVersor2D.hh"
11 #include "JGeometry3D/JAngle3D.hh"
12 #include "JGeometry3D/JVector3D.hh"
13 #include "JGeometry3D/JVersor3D.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JGEOMETRY3D {}
21 namespace JPP { using namespace JGEOMETRY3D; }
22 
23 namespace JGEOMETRY3D {
24 
25  using JIO::JReader;
26  using JIO::JWriter;
28 
29 
30  /**
31  * Data structure for normalised vector in positive z-direction.
32  * The member methods getDX() and getDY() refer to the corresponding direction cosines.
33  * Note that the direction cosines may have any value.
34  * The method getDZ() will simply return 0 if the sum of the squares of the direction cosines is larger than 1.
35  */
36  class JVersor3Z :
37  public JMATH::JMath<JVersor3Z>
38  {
39  public:
40  /**
41  * Default constructor.
42  */
44  __dx(0.0),
45  __dy(0.0)
46  {}
47 
48 
49  /**
50  * Constructor.
51  *
52  * \param dir direction cosines
53  */
54  JVersor3Z(const JVersor2D& dir) :
55  __dx(dir.getDX()),
56  __dy(dir.getDY())
57  {}
58 
59 
60  /**
61  * Constructor.
62  *
63  * \param dx direction cosine dx/dz
64  * \param dy direction cosine dy/dz
65  */
66  JVersor3Z(const double dx,
67  const double dy) :
68  __dx(dx),
69  __dy(dy)
70  {}
71 
72 
73  /**
74  * Get direction.
75  *
76  * \return direction
77  */
78  const JVersor3Z& getDirection() const
79  {
80  return static_cast<const JVersor3Z&>(*this);
81  }
82 
83 
84  /**
85  * Get direction.
86  *
87  * \return direction
88  */
90  {
91  return static_cast<JVersor3Z&>(*this);
92  }
93 
94 
95  /**
96  * Set direction.
97  *
98  * \param direction direction
99  */
100  void setDirection(const JVersor3Z& direction)
101  {
102  static_cast<JVersor3Z&>(*this) = direction;
103  }
104 
105 
106  /**
107  * Get angle.
108  *
109  * \return angle
110  */
111  operator JAngle3D() const
112  {
113  return JAngle3D(getDX(), getDY(), getDZ());
114  }
115 
116 
117  /**
118  * Type conversion operator.
119  *
120  * \return position
121  */
122  operator JVector3D() const
123  {
124  return JVector3D(getDX(), getDY(), getDZ());
125  }
126 
127 
128  /**
129  * Type conversion operator.
130  *
131  * \return direction
132  */
133  operator JVersor3D() const
134  {
135  return JVersor3D(getDX(), getDY(), getDZ());
136  }
137 
138 
139  /**
140  * Get x direction.
141  *
142  * \return x direction
143  */
144  double getDX() const
145  {
146  return __dx;
147  }
148 
149 
150  /**
151  * Get y direction.
152  *
153  * \return y direction
154  */
155  double getDY() const
156  {
157  return __dy;
158  }
159 
160 
161  /**
162  * Get z direction.
163  *
164  * \return z direction
165  */
166  double getDZ() const
167  {
168  const double v = getDX()*getDX() + getDY()*getDY();
169 
170  if (v <= 1.0)
171  return sqrt(1.0 - v);
172  else
173  return 0.0;
174  }
175 
176 
177  /**
178  * Prefix unary minus.
179  *
180  * \return this versor
181  */
183  {
184  __dx = -__dx;
185  __dy = -__dy;
186 
187  return *this;
188  }
189 
190 
191  /**
192  * Addition operator.
193  *
194  * \param value versor
195  * \return this versor
196  */
197  JVersor3Z& add(const JVersor3Z& value)
198  {
199  __dx += value.getDX();
200  __dy += value.getDY();
201 
202  return *this;
203  }
204 
205 
206  /**
207  * Subtraction operator.
208  *
209  * \param value versor
210  * \return this versor
211  */
212  JVersor3Z& sub(const JVersor3Z& value)
213  {
214  __dx -= value.getDX();
215  __dy -= value.getDY();
216 
217  return *this;
218  }
219 
220 
221  /**
222  * Multiplication operator.
223  *
224  * \param value multiplication factor
225  * \return this versor
226  */
227  JVersor3Z& mul(const double value)
228  {
229  __dx *= value;
230  __dy *= value;
231 
232  return *this;
233  }
234 
235 
236  /**
237  * Division operator.
238  *
239  * \param value multiplication factor
240  * \return this versor
241  */
242  JVersor3Z& div(const double value)
243  {
244  __dx /= value;
245  __dy /= value;
246 
247  return *this;
248  }
249 
250 
251  /**
252  * Check equality.
253  *
254  * \param versor versor
255  * \return true if versors are equal; else false
256  */
257  bool equals(const JVersor3Z& versor) const
258  {
259  return (getDX() == versor.getDX() &&
260  getDY() == versor.getDY());
261  }
262 
263 
264  /**
265  * Get dot product.
266  *
267  * \param dir direction
268  * \return dot product
269  */
270  double getDot(const JVersor3Z& dir) const
271  {
272  return
273  getDX() * dir.getDX() +
274  getDY() * dir.getDY() +
275  getDZ() * dir.getDZ();
276  }
277 
278 
279  /**
280  * Get dot product.
281  *
282  * \param angle angle
283  * \return dot product
284  */
285  double getDot(const JAngle3D& angle) const
286  {
287  return
288  getDX() * angle.getDX() +
289  getDY() * angle.getDY() +
290  getDZ() * angle.getDZ();
291  }
292 
293 
294  /**
295  * Get dot product.
296  *
297  * \param pos position
298  * \return dot product
299  */
300  double getDot(const JVector3D& pos) const
301  {
302  return
303  getDX() * pos.getX() +
304  getDY() * pos.getY() +
305  getDZ() * pos.getZ();
306  }
307 
308 
309  /**
310  * Get dot product.
311  *
312  * \param dir direction
313  * \return dot product
314  */
315  double getDot(const JVersor3D& dir) const
316  {
317  return
318  getDX() * dir.getDX() +
319  getDY() * dir.getDY() +
320  getDZ() * dir.getDZ();
321  }
322 
323 
324  /**
325  * Read versor from input.
326  *
327  * \param in input stream
328  * \param versor versor
329  * \return input stream
330  */
331  friend inline std::istream& operator>>(std::istream& in, JVersor3Z& versor)
332  {
333  return in >> versor.__dx >> versor.__dy;
334  }
335 
336 
337  /**
338  * Write versor to output.
339  *
340  * \param out output stream
341  * \param versor versor
342  * \return output stream
343  */
344  friend inline std::ostream& operator<<(std::ostream& out, const JVersor3Z& versor)
345  {
346  return out << versor.getDX() << ' ' << versor.getDY();
347  }
348 
349 
350  /**
351  * Read versor from input.
352  *
353  * \param in reader
354  * \param versor versor
355  * \return reader
356  */
357  friend inline JReader& operator>>(JReader& in, JVersor3Z& versor)
358  {
359  return in >> versor.__dx >> versor.__dy;
360  }
361 
362 
363  /**
364  * Write versor to output.
365  *
366  * \param out writer
367  * \param versor versor
368  * \return writer
369  */
370  friend inline JWriter& operator<<(JWriter& out, const JVersor3Z& versor)
371  {
372  return out << versor.__dx << versor.__dy;
373  }
374 
375  protected:
376  double __dx;
377  double __dy;
378  };
379 }
380 
381 #endif
friend std::istream & operator>>(std::istream &in, JVersor3Z &versor)
Read versor from input.
Definition: JVersor3Z.hh:331
double getDot(const JVersor3Z &dir) const
Get dot product.
Definition: JVersor3Z.hh:270
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
friend JWriter & operator<<(JWriter &out, const JVersor3Z &versor)
Write versor to output.
Definition: JVersor3Z.hh:370
Interface for binary output.
JVersor3Z(const JVersor2D &dir)
Constructor.
Definition: JVersor3Z.hh:54
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JVersor3Z & negate()
Prefix unary minus.
Definition: JVersor3Z.hh:182
JVersor3Z & sub(const JVersor3Z &value)
Subtraction operator.
Definition: JVersor3Z.hh:212
double getDot(const JAngle3D &angle) const
Get dot product.
Definition: JVersor3Z.hh:285
JVersor3Z & add(const JVersor3Z &value)
Addition operator.
Definition: JVersor3Z.hh:197
double getDY() const
Get y direction.
Definition: JVersor3Z.hh:155
JVersor3Z & mul(const double value)
Multiplication operator.
Definition: JVersor3Z.hh:227
JVersor3Z()
Default constructor.
Definition: JVersor3Z.hh:43
friend std::ostream & operator<<(std::ostream &out, const JVersor3Z &versor)
Write versor to output.
Definition: JVersor3Z.hh:344
double getDot(const JVersor3D &dir) const
Get dot product.
Definition: JVersor3Z.hh:315
Data structure for normalised vector in two dimensions.
Definition: JVersor2D.hh:19
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
double getDY() const
Get y direction.
Definition: JVersor3D.hh:101
JVersor3Z & div(const double value)
Division operator.
Definition: JVersor3Z.hh:242
double getDX() const
Get x direction.
Definition: JVersor3D.hh:90
double getY() const
Get y position.
Definition: JVector3D.hh:102
void setDirection(const JVersor3Z &direction)
Set direction.
Definition: JVersor3Z.hh:100
Interface for binary input.
JVersor3Z & getDirection()
Get direction.
Definition: JVersor3Z.hh:89
JVersor3Z(const double dx, const double dy)
Constructor.
Definition: JVersor3Z.hh:66
const JVersor3Z & getDirection() const
Get direction.
Definition: JVersor3Z.hh:78
double getDX() const
Get x direction.
Definition: JVersor3Z.hh:144
double getX() const
Get x position.
Definition: JVector3D.hh:92
Base class for data structures with artithmetic capabilities.
double getDot(const JVector3D &pos) const
Get dot product.
Definition: JVersor3Z.hh:300
double getDZ() const
Get z direction.
Definition: JVersor3Z.hh:166
friend JReader & operator>>(JReader &in, JVersor3Z &versor)
Read versor from input.
Definition: JVersor3Z.hh:357
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
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
bool equals(const JVersor3Z &versor) const
Check equality.
Definition: JVersor3Z.hh:257
double getDZ() const
Get z direction.
Definition: JAngle3D.hh:127
double getDY() const
Get y direction.
Definition: JAngle3D.hh:116