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