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