Jpp  16.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDirection2D.hh
Go to the documentation of this file.
1 #ifndef __JDIRECTION2D__
2 #define __JDIRECTION2D__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JIO/JSerialisable.hh"
8 #include "JLang/JManip.hh"
10 #include "JGeometry2D/JAngle2D.hh"
11 #include "JGeometry2D/JVector2D.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JGEOMETRY2D {}
20 namespace JPP { using namespace JGEOMETRY2D; }
21 
22 namespace JGEOMETRY2D {
23 
24  using JIO::JReader;
25  using JIO::JWriter;
26 
27 
28  /**
29  * Data structure for direction in two dimensions.
30  */
31  class JDirection2D :
32  public JVersor2D
33  {
34  public:
35 
36  using JVersor2D::getDot;
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  */
95  const JDirection2D& getDirection() const
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  {
185  R.rotate_back(__dx, __dy);
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  return in;
261  }
262 
263 
264  /**
265  * Write direction to output.
266  *
267  * \param out output stream
268  * \param direction direction
269  * \return output stream
270  */
271  friend inline std::ostream& operator<<(std::ostream& out, const JDirection2D& direction)
272  {
273  const JFormat format(out, getFormat<JDirection2D>(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos)));
274 
275  out << format << direction.getDX() << ' '
276  << format << direction.getDY();
277 
278  return out;
279  }
280 
281 
282  /**
283  * Read direction from input.
284  *
285  * \param in reader
286  * \param direction direction
287  * \return reader
288  */
289  friend inline JReader& operator>>(JReader& in, JDirection2D& direction)
290  {
291  in >> direction.__dx;
292  in >> direction.__dy;
293 
294  return in;
295  }
296 
297 
298  /**
299  * Write direction to output.
300  *
301  * \param out writer
302  * \param direction direction
303  * \return writer
304  */
305  friend inline JWriter& operator<<(JWriter& out, const JDirection2D& direction)
306  {
307  out << direction.__dx;
308  out << direction.__dy;
309 
310  return out;
311  }
312  };
313 }
314 
315 #endif
Data structure for vector in two dimensions.
Definition: JVector2D.hh:32
double getDX() const
Get x direction.
Definition: JAngle2D.hh:83
Data structure for angle in two dimensions.
Definition: JAngle2D.hh:33
void transform(double &__x, double &__y) const
Transform.
double getDot(const JVector2D &pos) const
Get dot product.
friend std::ostream & operator<<(std::ostream &out, const JDirection2D &direction)
Write direction to output.
Interface for binary output.
Rotation matrix.
Definition: JRotation2D.hh:23
JDirection2D(const JVector2D &pos)
Constructor.
Definition: JDirection2D.hh:73
double getDot(const JVersor2D &versor) const
Get dot product.
Definition: JVersor2D.hh:116
void rotate(double &__x, double &__y) const
Rotate.
Definition: JRotation2D.hh:96
double getPerpDot(const JAngle2D &angle) const
Get perpendicular dot product.
void rotate_back(double &__x, double &__y) const
Rotate back.
Definition: JRotation2D.hh:112
JDirection2D & rotate_back(const JRotation2D &R)
Rotate back.
double getPerpDot(const JVector2D &pos) const
Get perpendicular dot product.
Data structure for direction in two dimensions.
Definition: JDirection2D.hh:31
double getY() const
Get y position.
Definition: JVector2D.hh:74
double getDX() const
Get x direction.
Definition: JVersor2D.hh:53
void setDirection(const JDirection2D &dir)
Set direction.
JDirection2D & transform(const JMatrix2D &T)
Transform.
Auxiliary class to temporarily define format specifications.
Definition: JManip.hh:632
Data structure for normalised vector in two dimensions.
Definition: JVersor2D.hh:20
double getDot(const JAngle2D &angle) const
Get dot product.
JDirection2D & rotate(const JRotation2D &R)
Rotate.
JVersor2D & normalise()
Normalise versor.
Definition: JVersor2D.hh:144
JDirection2D(const double dx, const double dy)
Constructor.
Definition: JDirection2D.hh:84
double getX() const
Get x position.
Definition: JVector2D.hh:63
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JDirection2D()
Default constructor.
Definition: JDirection2D.hh:43
JDirection2D(const JVersor2D &dir)
Constructor.
Definition: JDirection2D.hh:53
double getPerpDot(const JVersor2D &versor) const
Get perpendicular dot product.
Definition: JVersor2D.hh:130
Interface for binary input.
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:43
friend JWriter & operator<<(JWriter &out, const JDirection2D &direction)
Write direction to output.
I/O manipulators.
JDirection2D & getDirection()
Get direction.
friend std::istream & operator>>(std::istream &in, JDirection2D &direction)
Read direction from input.
2 x 2 matrix
const JDirection2D & getDirection() const
Get direction.
Definition: JDirection2D.hh:95
double getDY() const
Get y direction.
Definition: JVersor2D.hh:64
JDirection2D(const JAngle2D &angle)
Constructor.
Definition: JDirection2D.hh:63
double getDY() const
Get y direction.
Definition: JAngle2D.hh:94
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
Data structure for format specifications.
Definition: JManip.hh:522
friend JReader & operator>>(JReader &in, JDirection2D &direction)
Read direction from input.