Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JDirection2D.hh
Go to the documentation of this file.
1#ifndef __JDIRECTION2D__
2#define __JDIRECTION2D__
3
4#include <istream>
5#include <ostream>
6
8#include "JLang/JManip.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
27
28 /**
29 * Data structure for direction in two dimensions.
30 */
32 public JVersor2D
33 {
34 public:
35
38
39
40 /**
41 * Default constructor.
42 */
44 JVersor2D()
45 {}
46
47
48 /**
49 * Constructor.
50 *
51 * \param dir direction
52 */
53 JDirection2D(const JVersor2D& dir) :
54 JVersor2D(dir.getDY(), dir.getDX())
55 {}
56
57
58 /**
59 * Constructor.
60 *
61 * \param angle angle
62 */
63 JDirection2D(const JAngle2D& angle) :
64 JVersor2D(angle.getDX(), angle.getDY())
65 {}
66
67
68 /**
69 * Constructor.
70 *
71 * \param pos position
72 */
73 JDirection2D(const JVector2D& pos) :
74 JVersor2D(pos.getY(),pos.getX())
75 {}
76
77
78 /**
79 * Constructor.
80 *
81 * \param dx dx value
82 * \param dy dy value
83 */
84 JDirection2D(const double dx,
85 const double dy) :
86 JVersor2D(dx, dy)
87 {}
88
89
90 /**
91 * Get direction.
92 *
93 * \return direction
94 */
96 {
97 return static_cast<const JDirection2D&>(*this);
98 }
99
100
101 /**
102 * Get direction.
103 *
104 * \return direction
105 */
107 {
108 return static_cast<JDirection2D&>(*this);
109 }
110
111
112 /**
113 * Set direction.
114 *
115 * \param dir direction
116 */
117 void setDirection(const JDirection2D& dir)
118 {
119 static_cast<JDirection2D&>(*this) = dir;
120 }
121
122
123 /**
124 * Type conversion operator.
125 *
126 * \return angle
127 */
128 operator JAngle2D() const
129 {
130 return JAngle2D(getDX(), getDY());
131 }
132
133
134 /**
135 * Type conversion operator.
136 *
137 * \return position
138 */
139 operator JVector2D() const
140 {
141 return JVector2D(getDX(), getDY());
142 }
143
144
145 /**
146 * Transform.
147 *
148 * \param T matrix
149 * \return this direction
150 */
152 {
153 T.transform(__dx, __dy);
154
155 normalise();
156
157 return *this;
158 }
159
160
161 /**
162 * Rotate.
163 *
164 * \param R rotation matrix
165 * \return this direction
166 */
168 {
169 R.rotate(__dx, __dy);
170
171 normalise();
172
173 return *this;
174 }
175
176
177 /**
178 * Rotate back.
179 *
180 * \param R rotation matrix
181 * \return this direction
182 */
184 {
186
187 normalise();
188
189 return *this;
190 }
191
192
193 /**
194 * Get dot product.
195 *
196 * \param angle angle
197 * \return dot product
198 */
199 double getDot(const JAngle2D& angle) const
200 {
201 return
202 getDX() * angle.getDX() +
203 getDY() * angle.getDY();
204 }
205
206
207 /**
208 * Get dot product.
209 *
210 * \param pos position
211 * \return dot product
212 */
213 double getDot(const JVector2D& pos) const
214 {
215 return
216 getDX() * pos.getX() +
217 getDY() * pos.getY();
218 }
219
220
221 /**
222 * Get perpendicular dot product.
223 *
224 * \param angle angle
225 * \return perpendicular dot product
226 */
227 double getPerpDot(const JAngle2D& angle) const
228 {
229 return
230 getDX() * angle.getDY() -
231 getDY() * angle.getDX();
232 }
233
234
235 /**
236 * Get perpendicular dot product.
237 *
238 * \param pos position
239 * \return perpendicular dot product
240 */
241 double getPerpDot(const JVector2D& pos) const
242 {
243 return
244 getDX() * pos.getY() -
245 getDY() * pos.getX();
246 }
247
248
249 /**
250 * Read direction from input.
251 *
252 * \param in input stream
253 * \param direction direction
254 * \return input stream
255 */
256 friend inline std::istream& operator>>(std::istream& in, JDirection2D& direction)
257 {
258 in >> direction.__dx >> direction.__dy;
259
260 direction.normalise();
261
262 return in;
263 }
264
265
266 /**
267 * Write direction to output.
268 *
269 * \param out output stream
270 * \param direction direction
271 * \return output stream
272 */
273 friend inline std::ostream& operator<<(std::ostream& out, const JDirection2D& direction)
274 {
275 const JFormat format(out, getFormat<JDirection2D>(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos)));
276
277 out << format << direction.getDX() << ' '
278 << format << direction.getDY();
279
280 return out;
281 }
282
283
284 /**
285 * Read direction from input.
286 *
287 * \param in reader
288 * \param direction direction
289 * \return reader
290 */
291 friend inline JReader& operator>>(JReader& in, JDirection2D& direction)
292 {
293 in >> direction.__dx;
294 in >> direction.__dy;
295
296 return in;
297 }
298
299
300 /**
301 * Write direction to output.
302 *
303 * \param out writer
304 * \param direction direction
305 * \return writer
306 */
307 friend inline JWriter& operator<<(JWriter& out, const JDirection2D& direction)
308 {
309 out << direction.__dx;
310 out << direction.__dy;
311
312 return out;
313 }
314 };
315}
316
317#endif
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
Data structure for angle in two dimensions.
Definition JAngle2D.hh:35
double getDY() const
Get y direction.
Definition JAngle2D.hh:94
double getDX() const
Get x direction.
Definition JAngle2D.hh:83
Data structure for direction in two dimensions.
JDirection2D()
Default constructor.
double getDot(const JAngle2D &angle) const
Get dot product.
void setDirection(const JDirection2D &dir)
Set direction.
JDirection2D & getDirection()
Get direction.
JDirection2D(const JVector2D &pos)
Constructor.
JDirection2D(const double dx, const double dy)
Constructor.
friend std::ostream & operator<<(std::ostream &out, const JDirection2D &direction)
Write direction to output.
friend std::istream & operator>>(std::istream &in, JDirection2D &direction)
Read direction from input.
JDirection2D(const JAngle2D &angle)
Constructor.
double getDot(const JVector2D &pos) const
Get dot product.
friend JReader & operator>>(JReader &in, JDirection2D &direction)
Read direction from input.
JDirection2D & rotate(const JRotation2D &R)
Rotate.
JDirection2D & transform(const JMatrix2D &T)
Transform.
double getPerpDot(const JVector2D &pos) const
Get perpendicular dot product.
double getPerpDot(const JAngle2D &angle) const
Get perpendicular dot product.
friend JWriter & operator<<(JWriter &out, const JDirection2D &direction)
Write direction to output.
JDirection2D & rotate_back(const JRotation2D &R)
Rotate back.
const JDirection2D & getDirection() const
Get direction.
JDirection2D(const JVersor2D &dir)
Constructor.
void rotate(double &__x, double &__y) const
Rotate.
void rotate_back(double &__x, double &__y) const
Rotate back.
Data structure for vector in two dimensions.
Definition JVector2D.hh:34
double getY() const
Get y position.
Definition JVector2D.hh:74
double getX() const
Get x position.
Definition JVector2D.hh:63
Data structure for normalised vector in two dimensions.
Definition JVersor2D.hh:21
double getDX() const
Get x direction.
Definition JVersor2D.hh:53
JVersor2D & normalise()
Normalise versor.
Definition JVersor2D.hh:144
double getPerpDot(const JVersor2D &versor) const
Get perpendicular dot product.
Definition JVersor2D.hh:130
double getDY() const
Get y direction.
Definition JVersor2D.hh:64
double getDot(const JVersor2D &versor) const
Get dot product.
Definition JVersor2D.hh:116
Interface for binary input.
Interface for binary output.
Auxiliary classes and methods for 2D geometrical objects and operations.
Definition JAngle2D.hh:19
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