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