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