Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAngle2D.hh
Go to the documentation of this file.
1 #ifndef __JANGLE2D__
2 #define __JANGLE2D__
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 JGEOMETRY2D {}
19 namespace JPP { using namespace JGEOMETRY2D; }
20 
21 namespace JGEOMETRY2D {
22 
23  using JIO::JReader;
24  using JIO::JWriter;
25 
26 
27  /**
28  * Data structure for angle in two dimensions.
29  * This class serves as input to the rotation matrix JRotation2D.
30  */
31  class JAngle2D :
32  public JMATH::JMath<JAngle2D>
33  {
34  public:
35  /**
36  * Default constructor.
37  */
39  __phi(0.0)
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param phi phi angle [rad]
47  */
48  JAngle2D(const double phi) :
49  __phi(phi)
50  {}
51 
52 
53  /**
54  * Constructor.
55  *
56  * \param x x value
57  * \param y y value
58  */
59  JAngle2D(const double x,
60  const double y) :
61  __phi(atan2(y,x))
62  {}
63 
64 
65  /**
66  * Get phi angle.
67  *
68  * \return phi angle
69  */
70  double getPhi() const
71  {
72  return __phi;
73  }
74 
75 
76  /**
77  * Get x direction.
78  *
79  * \return x direction
80  */
81  double getDX() const
82  {
83  return cos(__phi);
84  }
85 
86 
87  /**
88  * Get y direction.
89  *
90  * \return y direction
91  */
92  double getDY() const
93  {
94  return sin(__phi);
95  }
96 
97 
98  /**
99  * Negate angle.
100  *
101  * \return this angle
102  */
104  {
105  __phi = -__phi;
106 
107  return *this;
108  }
109 
110 
111  /**
112  * Add angle.
113  *
114  * \param angle angle
115  * \return this angle
116  */
117  JAngle2D& add(const JAngle2D& angle)
118  {
119  __phi += angle.getPhi();
120 
121  return *this;
122  }
123 
124 
125  /**
126  * Subtract angle.
127  *
128  * \param angle angle
129  * \return this angle
130  */
131  JAngle2D& sub(const JAngle2D& angle)
132  {
133  __phi -= angle.getPhi();
134 
135  return *this;
136  }
137 
138 
139  /**
140  * Scale angle.
141  *
142  * \param factor multiplication factor
143  * \return this angle
144  */
145  JAngle2D& mul(const double factor)
146  {
147  __phi *= factor;
148 
149  return *this;
150  }
151 
152 
153  /**
154  * Scale angle.
155  *
156  * \param factor division factor
157  * \return this angle
158  */
159  JAngle2D& div(const double factor)
160  {
161  __phi /= factor;
162 
163  return *this;
164  }
165 
166 
167  /**
168  * Check equality.
169  *
170  * \param angle angle
171  * \param precision precision
172  * \return true if angles are equal; else false
173  */
174  bool equals(const JAngle2D& angle,
175  const double precision = std::numeric_limits<double>::min()) const
176  {
177  return fabs(getPhi() - angle.getPhi()) <= precision;
178  }
179 
180 
181  /**
182  * Get dot product.
183  *
184  * \param angle angle
185  * \return dot product
186  */
187  double getDot(const JAngle2D& angle) const
188  {
189  return cos(getPhi() - angle.getPhi());
190  }
191 
192 
193  /**
194  * Normalise angle.
195 
196  * - phi angle will be between 0 and 2&pi;
197  *
198  * \return this angle
199  */
201  {
202  using JTOOLS::PI;
203 
204  if (__phi > 2*PI) { do { __phi -= 2*PI; } while (__phi > 2*PI); }
205  if (__phi < 0.0) { do { __phi += 2*PI; } while (__phi < 0.0); }
206 
207  return *this;
208  }
209 
210 
211  /**
212  * Read angle from input.
213  *
214  * \param in input stream
215  * \param angle angle
216  * \return input stream
217  */
218  friend inline std::istream& operator>>(std::istream& in, JAngle2D& angle)
219  {
220  return in >> angle.__phi;
221  }
222 
223 
224  /**
225  * Write angle to output.
226  *
227  * \param out output stream
228  * \param angle angle
229  * \return output stream
230  */
231  friend inline std::ostream& operator<<(std::ostream& out, const JAngle2D& angle)
232  {
233  return out << angle.getPhi();
234  }
235 
236 
237  /**
238  * Read angle from input.
239  *
240  * \param in reader
241  * \param angle angle
242  * \return reader
243  */
244  friend inline JReader& operator>>(JReader& in, JAngle2D& angle)
245  {
246  return in >> angle.__phi;
247  }
248 
249 
250  /**
251  * Write angle to output.
252  *
253  * \param out writer
254  * \param angle angle
255  * \return writer
256  */
257  friend inline JWriter& operator<<(JWriter& out, const JAngle2D& angle)
258  {
259  return out << angle.__phi;
260  }
261 
262  protected:
263  double __phi;
264  };
265 }
266 
267 #endif
JAngle2D & div(const double factor)
Scale angle.
Definition: JAngle2D.hh:159
double getDX() const
Get x direction.
Definition: JAngle2D.hh:81
Data structure for angle in two dimensions.
Definition: JAngle2D.hh:31
Interface for binary output.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
friend JWriter & operator<<(JWriter &out, const JAngle2D &angle)
Write angle to output.
Definition: JAngle2D.hh:257
bool equals(const JAngle2D &angle, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition: JAngle2D.hh:174
friend std::ostream & operator<<(std::ostream &out, const JAngle2D &angle)
Write angle to output.
Definition: JAngle2D.hh:231
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
friend JReader & operator>>(JReader &in, JAngle2D &angle)
Read angle from input.
Definition: JAngle2D.hh:244
JAngle2D & sub(const JAngle2D &angle)
Subtract angle.
Definition: JAngle2D.hh:131
JAngle2D & mul(const double factor)
Scale angle.
Definition: JAngle2D.hh:145
JAngle2D & normalise()
Normalise angle.
Definition: JAngle2D.hh:200
friend std::istream & operator>>(std::istream &in, JAngle2D &angle)
Read angle from input.
Definition: JAngle2D.hh:218
Constants.
JAngle2D & add(const JAngle2D &angle)
Add angle.
Definition: JAngle2D.hh:117
JAngle2D()
Default constructor.
Definition: JAngle2D.hh:38
Interface for binary input.
double getDot(const JAngle2D &angle) const
Get dot product.
Definition: JAngle2D.hh:187
double getPhi() const
Get phi angle.
Definition: JAngle2D.hh:70
JAngle2D(const double x, const double y)
Constructor.
Definition: JAngle2D.hh:59
JAngle2D(const double phi)
Constructor.
Definition: JAngle2D.hh:48
Base class for data structures with artithmetic capabilities.
double getDY() const
Get y direction.
Definition: JAngle2D.hh:92
JAngle2D & negate()
Negate angle.
Definition: JAngle2D.hh:103