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