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