Jpp - 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"
10 #include "JGeometry2D/JVector2D.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 
26 
27  /**
28  * Data structure for direction in two dimensions.
29  */
30  class JDirection2D :
31  public JVersor2D
32  {
33  public:
34 
35  using JVersor2D::getDot;
37 
38 
39  /**
40  * Default constructor.
41  */
43  JVersor2D()
44  {}
45 
46 
47  /**
48  * Constructor.
49  *
50  * \param dir direction
51  */
52  JDirection2D(const JVersor2D& dir) :
53  JVersor2D(dir.getDY(), dir.getDX())
54  {}
55 
56 
57  /**
58  * Constructor.
59  *
60  * \param angle angle
61  */
62  JDirection2D(const JAngle2D& angle) :
63  JVersor2D(angle.getDX(), angle.getDY())
64  {}
65 
66 
67  /**
68  * Constructor.
69  *
70  * \param pos position
71  */
72  JDirection2D(const JVector2D& pos) :
73  JVersor2D(pos.getY(),pos.getX())
74  {}
75 
76 
77  /**
78  * Constructor.
79  *
80  * \param dx dx value
81  * \param dy dy value
82  */
83  JDirection2D(const double dx,
84  const double dy) :
85  JVersor2D(dx, dy)
86  {}
87 
88 
89  /**
90  * Get direction.
91  *
92  * \return direction
93  */
94  const JDirection2D& getDirection() const
95  {
96  return static_cast<const JDirection2D&>(*this);
97  }
98 
99 
100  /**
101  * Get direction.
102  *
103  * \return direction
104  */
106  {
107  return static_cast<JDirection2D&>(*this);
108  }
109 
110 
111  /**
112  * Set direction.
113  *
114  * \param dir direction
115  */
116  void setDirection(const JDirection2D& dir)
117  {
118  static_cast<JDirection2D&>(*this) = dir;
119  }
120 
121 
122  /**
123  * Type conversion operator.
124  *
125  * \return angle
126  */
127  operator JAngle2D() const
128  {
129  return JAngle2D(getDX(), getDY());
130  }
131 
132 
133  /**
134  * Type conversion operator.
135  *
136  * \return position
137  */
138  operator JVector2D() const
139  {
140  return JVector2D(getDX(), getDY());
141  }
142 
143 
144  /**
145  * Transform.
146  *
147  * \param T matrix
148  * \return this direction
149  */
151  {
152  T.transform(__dx, __dy);
153 
154  normalise();
155 
156  return *this;
157  }
158 
159 
160  /**
161  * Rotate.
162  *
163  * \param R rotation matrix
164  * \return this direction
165  */
167  {
168  R.rotate(__dx, __dy);
169 
170  normalise();
171 
172  return *this;
173  }
174 
175 
176  /**
177  * Rotate back.
178  *
179  * \param R rotation matrix
180  * \return this direction
181  */
183  {
184  R.rotate_back(__dx, __dy);
185 
186  normalise();
187 
188  return *this;
189  }
190 
191 
192  /**
193  * Get dot product.
194  *
195  * \param angle angle
196  * \return dot product
197  */
198  double getDot(const JAngle2D& angle) const
199  {
200  return
201  getDX() * angle.getDX() +
202  getDY() * angle.getDY();
203  }
204 
205 
206  /**
207  * Get dot product.
208  *
209  * \param pos position
210  * \return dot product
211  */
212  double getDot(const JVector2D& pos) const
213  {
214  return
215  getDX() * pos.getX() +
216  getDY() * pos.getY();
217  }
218 
219 
220  /**
221  * Get perpendicular dot product.
222  *
223  * \param angle angle
224  * \return perpendicular dot product
225  */
226  double getPerpDot(const JAngle2D& angle) const
227  {
228  return
229  getDX() * angle.getDY() -
230  getDY() * angle.getDX();
231  }
232 
233 
234  /**
235  * Get perpendicular dot product.
236  *
237  * \param pos position
238  * \return perpendicular dot product
239  */
240  double getPerpDot(const JVector2D& pos) const
241  {
242  return
243  getDX() * pos.getY() -
244  getDY() * pos.getX();
245  }
246 
247 
248  /**
249  * Read direction from input.
250  *
251  * \param in input stream
252  * \param direction direction
253  * \return input stream
254  */
255  friend inline std::istream& operator>>(std::istream& in, JDirection2D& direction)
256  {
257  in >> direction.__dx >> direction.__dy;
258 
259  return in;
260  }
261 
262 
263  /**
264  * Write direction to output.
265  *
266  * \param out output stream
267  * \param direction direction
268  * \return output stream
269  */
270  friend inline std::ostream& operator<<(std::ostream& out, const JDirection2D& direction)
271  {
272  out << direction.getDX() << ' ' << direction.getDY();
273 
274  return out;
275  }
276 
277 
278  /**
279  * Read direction from input.
280  *
281  * \param in reader
282  * \param direction direction
283  * \return reader
284  */
285  friend inline JReader& operator>>(JReader& in, JDirection2D& direction)
286  {
287  in >> direction.__dx;
288  in >> direction.__dy;
289 
290  return in;
291  }
292 
293 
294  /**
295  * Write direction to output.
296  *
297  * \param out writer
298  * \param direction direction
299  * \return writer
300  */
301  friend inline JWriter& operator<<(JWriter& out, const JDirection2D& direction)
302  {
303  out << direction.getDX();
304  out << direction.getDY();
305 
306  return out;
307  }
308  };
309 }
310 
311 #endif
Data structure for vector in two dimensions.
Definition: JVector2D.hh:32
double getDX() const
Get x direction.
Definition: JAngle2D.hh:82
Data structure for angle in two dimensions.
Definition: JAngle2D.hh:32
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:72
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:30
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.
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:83
double getX() const
Get x position.
Definition: JVector2D.hh:63
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JDirection2D()
Default constructor.
Definition: JDirection2D.hh:42
JDirection2D(const JVersor2D &dir)
Constructor.
Definition: JDirection2D.hh:52
double getPerpDot(const JVersor2D &versor) const
Get perpendicular dot product.
Definition: JVersor2D.hh:130
Interface for binary input.
friend JWriter & operator<<(JWriter &out, const JDirection2D &direction)
Write direction to output.
JDirection2D & getDirection()
Get direction.
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR set_variable NORTH set_variable EAST set_variable SOUTH set_variable WEST set_variable WORKDIR tmp set_variable R set_variable CT set_variable YMAX set_variable YMIN if do_usage *then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:35
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:94
double getDY() const
Get y direction.
Definition: JVersor2D.hh:64
JDirection2D(const JAngle2D &angle)
Constructor.
Definition: JDirection2D.hh:62
double getDY() const
Get y direction.
Definition: JAngle2D.hh:93
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:38
friend JReader & operator>>(JReader &in, JDirection2D &direction)
Read direction from input.