Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JVersor2D.hh
Go to the documentation of this file.
1 #ifndef __JVERSOR2D__
2 #define __JVERSOR2D__
3 
4 #include <cmath>
5 
6 
7 /**
8  * \author mdejong
9  */
10 
11 namespace JGEOMETRY2D {}
12 namespace JPP { using namespace JGEOMETRY2D; }
13 
14 namespace JGEOMETRY2D {
15 
16  /**
17  * Data structure for normalised vector in two dimensions.
18  */
19  class JVersor2D
20  {
21  public:
22  /**
23  * Default constructor.
24  * This constructor yields a unit y-vector.
25  */
27  __dx(0.0),
28  __dy(1.0)
29  {}
30 
31 
32  /**
33  * Constructor.
34  *
35  * \param dx dx value
36  * \param dy dy value
37  */
38  JVersor2D(const double dx,
39  const double dy) :
40  __dx(dx),
41  __dy(dy)
42  {
43  normalise();
44  }
45 
46 
47  /**
48  * Get x direction.
49  *
50  * \return x direction
51  */
52  double getDX() const
53  {
54  return __dx;
55  }
56 
57 
58  /**
59  * Get y direction.
60  *
61  * \return y direction
62  */
63  double getDY() const
64  {
65  return __dy;
66  }
67 
68 
69  /**
70  * Get phi angle.
71  *
72  * \return phi angle [rad]
73  */
74  double getPhi() const
75  {
76  return atan2(__dy, __dx);
77  }
78 
79 
80  /**
81  * Negate versor.
82  *
83  * \return this versor
84  */
86  {
87  __dx = -__dx;
88  __dy = -__dy;
89 
90  return *this;
91  }
92 
93 
94  /**
95  * Check equality.
96  *
97  * \param versor versor
98  * \return true if versors are equal; else false
99  */
100  bool equals(const JVersor2D& versor) const
101  {
102  return (getDX() == versor.getDX() &&
103  getDY() == versor.getDY());
104  }
105 
106 
107  /**
108  * Get dot product.
109  *
110  * \param versor versor
111  * \return dot product
112  */
113  double getDot(const JVersor2D& versor) const
114  {
115  return
116  getDX() * versor.getDX() +
117  getDY() * versor.getDY();
118  }
119 
120 
121  /**
122  * Get perpendicular dot product.
123  *
124  * \param versor versor
125  * \return perpendicular dot product
126  */
127  double getPerpDot(const JVersor2D& versor) const
128  {
129  return
130  getDX() * versor.getDY() -
131  getDY() * versor.getDX();
132  }
133 
134 
135  /**
136  * Normalise versor.
137  * This operation may set the result to the unit y-vector.
138  *
139  * \return this versor
140  */
142  {
143  const double v = sqrt(getDX()*getDX() + getDY()*getDY());
144 
145  if (v != 0.0) {
146  __dx /= v;
147  __dy /= v;
148  } else {
149  __dx = 0.0;
150  __dy = 1.0;
151  }
152 
153  return *this;
154  }
155 
156  protected:
157  double __dx;
158  double __dy;
159  };
160 
161 
162  static const JVersor2D JVersor2X_t(1,0); //!< unit x-vector
163  static const JVersor2D JVersor2Y_t(0,1); //!< unit y-vector
164 }
165 
166 #endif
double getDot(const JVersor2D &versor) const
Get dot product.
Definition: JVersor2D.hh:113
JVersor2D & negate()
Negate versor.
Definition: JVersor2D.hh:85
static const JVersor2D JVersor2X_t(1, 0)
unit x-vector
double getDX() const
Get x direction.
Definition: JVersor2D.hh:52
double getPhi() const
Get phi angle.
Definition: JVersor2D.hh:74
Data structure for normalised vector in two dimensions.
Definition: JVersor2D.hh:19
JVersor2D & normalise()
Normalise versor.
Definition: JVersor2D.hh:141
JVersor2D()
Default constructor.
Definition: JVersor2D.hh:26
JVersor2D(const double dx, const double dy)
Constructor.
Definition: JVersor2D.hh:38
double getPerpDot(const JVersor2D &versor) const
Get perpendicular dot product.
Definition: JVersor2D.hh:127
bool equals(const JVersor2D &versor) const
Check equality.
Definition: JVersor2D.hh:100
double getDY() const
Get y direction.
Definition: JVersor2D.hh:63
static const JVersor2D JVersor2Y_t(0, 1)
unit y-vector