Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAngle3D.hh
Go to the documentation of this file.
1 #ifndef __JANGLE3D__
2 #define __JANGLE3D__
3 
4 #include <istream>
5 #include <ostream>
6 #include <cmath>
7 
8 #include "JTools/JConstants.hh"
9 #include "JIO/JSerialisable.hh"
10 #include "JMath/JMath.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JGEOMETRY3D {}
18 namespace JPP { using namespace JGEOMETRY3D; }
19 
20 namespace JGEOMETRY3D {
21 
22  using JIO::JReader;
23  using JIO::JWriter;
24 
25 
26  /**
27  * Data structure for angles in three dimensions.
28  * This class serves as input to the rotation matrix JRotation3D.
29  */
30  class JAngle3D :
31  public JMATH::JMath<JAngle3D>
32  {
33  public:
34  /**
35  * Default constructor.
36  */
38  __theta(0.0),
39  __phi (0.0)
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param theta theta angle [rad]
47  * \param phi phi angle [rad]
48  */
49  JAngle3D(const double theta,
50  const double phi) :
51  __theta(theta),
52  __phi (phi)
53  {}
54 
55 
56  /**
57  * Constructor.
58  *
59  * \param x x value
60  * \param y y value
61  * \param z z value
62  */
63  JAngle3D(const double x,
64  const double y,
65  const double z) :
66  __theta(0.0),
67  __phi (0.0)
68  {
69  const double v = x*x + y*y + z*z;
70 
71  if (v != 0.0) {
72  __theta = acos (z / sqrt(v));
73  __phi = atan2(y, x);
74  }
75  }
76 
77 
78  /**
79  * Get theta angle.
80  *
81  * \return theta angle
82  */
83  double getTheta() const
84  {
85  return __theta;
86  }
87 
88 
89  /**
90  * Get phi angle.
91  *
92  * \return phi angle
93  */
94  double getPhi() const
95  {
96  return __phi;
97  }
98 
99 
100  /**
101  * Get x direction.
102  *
103  * \return x direction
104  */
105  double getDX() const
106  {
107  return sin(__theta) * cos(__phi);
108  }
109 
110 
111  /**
112  * Get y direction.
113  *
114  * \return y direction
115  */
116  double getDY() const
117  {
118  return sin(__theta) * sin(__phi);
119  }
120 
121 
122  /**
123  * Get z direction.
124  *
125  * \return z direction
126  */
127  double getDZ() const
128  {
129  return cos(__theta);
130  }
131 
132 
133  /**
134  * Negate angle.
135  *
136  * \return this angle
137  */
139  {
140  __theta = -__theta;
141  __phi = -__phi;
142 
143  return *this;
144  }
145 
146 
147  /**
148  * Add angle.
149  *
150  * \param angle angle
151  * \return this angle
152  */
153  JAngle3D& add(const JAngle3D& angle)
154  {
155  __theta += angle.getTheta();
156  __phi += angle.getPhi();
157 
158  return *this;
159  }
160 
161 
162  /**
163  * Subtract angle.
164  *
165  * \param angle angle
166  * \return this angle
167  */
168  JAngle3D& sub(const JAngle3D& angle)
169  {
170  __theta -= angle.getTheta();
171  __phi -= angle.getPhi();
172 
173  return *this;
174  }
175 
176  /**
177  * Scale angle.
178  *
179  * \param factor multiplication factor
180  * \return this angle
181  */
182  JAngle3D& mul(const double factor)
183  {
184  __theta *= factor;
185  __phi *= factor;
186 
187  return *this;
188  }
189 
190 
191  /**
192  * Scale angle.
193  *
194  * \param factor division factor
195  * \return this angle
196  */
197  JAngle3D& div(const double factor)
198  {
199  __theta /= factor;
200  __phi /= factor;
201 
202  return *this;
203  }
204 
205 
206  /**
207  * Check equality.
208  *
209  * \param angle angle
210  * \return true if angles are equal; else false
211  */
212  bool equals(const JAngle3D& angle) const
213  {
214  return (getTheta() == angle.getTheta() &&
215  getPhi() == angle.getPhi());
216  }
217 
218 
219  /**
220  * Get dot product.
221  *
222  * \param angle angle
223  * \return dot product
224  */
225  double getDot(const JAngle3D& angle) const
226  {
227  return
228  cos(getTheta()) * cos(angle.getTheta()) +
229  sin(getTheta()) * sin(angle.getTheta()) *
230  cos(getPhi() - angle.getPhi());
231  }
232 
233 
234  /**
235  * Normalise angles.
236  *
237  * - theta angle will be between 0 and &pi;
238  * - phi angle will be between -&pi; and +&pi;
239  *
240  * \return this angle
241  */
243  {
244  using JTOOLS::PI;
245 
246  if (__theta > PI) { do { __theta -= PI; } while (__theta > PI); }
247  if (__theta < 0.0) { do { __theta += PI; } while (__theta < 0.0); }
248 
249  if (__phi > +PI) { do { __phi -= 2*PI; } while (__phi > +PI); }
250  if (__phi < -PI) { do { __phi += 2*PI; } while (__phi < -PI); }
251 
252  return *this;
253  }
254 
255 
256  /**
257  * Write angle from input.
258  *
259  * \param in input stream
260  * \param angle angle
261  * \return input stream
262  */
263  friend inline std::istream& operator>>(std::istream& in, JAngle3D& angle)
264  {
265  in >> angle.__theta >> angle.__phi;
266 
267  return in;
268  }
269 
270 
271  /**
272  * Write angle to output.
273  *
274  * \param out output stream
275  * \param angle angle
276  * \return output stream
277  */
278  friend inline std::ostream& operator<<(std::ostream& out, const JAngle3D& angle)
279  {
280  out << angle.getTheta() << ' ' << angle.getPhi();
281 
282  return out;
283  }
284 
285 
286  /**
287  * Read angle from input.
288  *
289  * \param in reader
290  * \param angle angle
291  * \return reader
292  */
293  friend inline JReader& operator>>(JReader& in, JAngle3D& angle)
294  {
295  in >> angle.__theta;
296  in >> angle.__phi;
297 
298  return in;
299  }
300 
301 
302  /**
303  * Write angle to output.
304  *
305  * \param out writer
306  * \param angle angle
307  * \return writer
308  */
309  friend inline JWriter& operator<<(JWriter& out, const JAngle3D& angle)
310  {
311  out << angle.getTheta();
312  out << angle.getPhi();
313 
314  return out;
315  }
316 
317  protected:
318  double __theta;
319  double __phi;
320  };
321 }
322 
323 #endif
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
Interface for binary output.
friend JReader & operator>>(JReader &in, JAngle3D &angle)
Read angle from input.
Definition: JAngle3D.hh:293
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
double getPhi() const
Get phi angle.
Definition: JAngle3D.hh:94
friend JWriter & operator<<(JWriter &out, const JAngle3D &angle)
Write angle to output.
Definition: JAngle3D.hh:309
JAngle3D & add(const JAngle3D &angle)
Add angle.
Definition: JAngle3D.hh:153
JAngle3D & normalise()
Normalise angles.
Definition: JAngle3D.hh:242
double getDot(const JAngle3D &angle) const
Get dot product.
Definition: JAngle3D.hh:225
static const double PI
Constants.
Definition: JConstants.hh:20
double getTheta() const
Get theta angle.
Definition: JAngle3D.hh:83
friend std::istream & operator>>(std::istream &in, JAngle3D &angle)
Write angle from input.
Definition: JAngle3D.hh:263
JAngle3D()
Default constructor.
Definition: JAngle3D.hh:37
Constants.
JAngle3D(const double theta, const double phi)
Constructor.
Definition: JAngle3D.hh:49
bool equals(const JAngle3D &angle) const
Check equality.
Definition: JAngle3D.hh:212
Interface for binary input.
JAngle3D & sub(const JAngle3D &angle)
Subtract angle.
Definition: JAngle3D.hh:168
JAngle3D & div(const double factor)
Scale angle.
Definition: JAngle3D.hh:197
Base class for data structures with artithmetic capabilities.
JAngle3D(const double x, const double y, const double z)
Constructor.
Definition: JAngle3D.hh:63
JAngle3D & negate()
Negate angle.
Definition: JAngle3D.hh:138
double getDX() const
Get x direction.
Definition: JAngle3D.hh:105
JAngle3D & mul(const double factor)
Scale angle.
Definition: JAngle3D.hh:182
friend std::ostream & operator<<(std::ostream &out, const JAngle3D &angle)
Write angle to output.
Definition: JAngle3D.hh:278
double getDZ() const
Get z direction.
Definition: JAngle3D.hh:127
double getDY() const
Get y direction.
Definition: JAngle3D.hh:116