Jpp
JVector3D.hh
Go to the documentation of this file.
1 #ifndef __JVECTOR3D__
2 #define __JVECTOR3D__
3 
4 #include <limits>
5 #include <cmath>
6 
7 #include "JTools/JRange.hh"
8 #include "JMath/JMath.hh"
10 #include "JGeometry3D/JMatrix3D.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JGEOMETRY3D {}
18 namespace JPP { using namespace JGEOMETRY3D; }
19 
20 namespace JGEOMETRY3D {
21 
23  using JGEOMETRY2D::JRangeX; // Type definition of range along x-axis.
24  using JGEOMETRY2D::JRangeY; // Type definition of range along y-axis.
25  typedef JTOOLS::JRange<double> JRangeZ; //!< Type definition of range along z-axis.
26 
27 
28  /**
29  * Data structure for vector in three dimensions.
30  *
31  * This class implements the JMATH::JMath interface.
32  */
33  class JVector3D :
34  public JMATH::JMath<JVector3D>
35  {
36  public:
37  /**
38  * Default constructor.
39  */
41  __x(0.0),
42  __y(0.0),
43  __z(0.0)
44  {}
45 
46 
47  /**
48  * Constructor.
49  *
50  * \param x x value
51  * \param y y value
52  * \param z z value
53  */
54  JVector3D(const double x,
55  const double y,
56  const double z) :
57  __x(x),
58  __y(y),
59  __z(z)
60  {}
61 
62 
63  /**
64  * Constructor.
65  *
66  * \param vector (x,y) values
67  * \param z z value
68  */
69  JVector3D(const JVector2D& vector,
70  const double z) :
71  __x(vector.getX()),
72  __y(vector.getY()),
73  __z(z)
74  {}
75 
76 
77  /**
78  * Type conversion operator.
79  *
80  * \return JVector2D
81  */
82  operator JVector2D() const
83  {
84  return JVector2D(this->getX(), this->getY());
85  }
86 
87 
88  /**
89  * Get x position.
90  *
91  * \return x position
92  */
93  double getX() const
94  {
95  return __x;
96  }
97 
98  /**
99  * Get y position.
100  *
101  * \return y position
102  */
103  double getY() const
104  {
105  return __y;
106  }
107 
108 
109  /**
110  * Get z position.
111  *
112  * \return z position
113  */
114  double getZ() const
115  {
116  return __z;
117  }
118 
119 
120  /**
121  * Negate vector.
122  *
123  * \return this vector
124  */
126  {
127  __x = -__x;
128  __y = -__y;
129  __z = -__z;
130 
131  return *this;
132  }
133 
134 
135  /**
136  * Add vector.
137  *
138  * \param vector vector
139  * \return this vector
140  */
141  JVector3D& add(const JVector3D& vector)
142  {
143  __x += vector.getX();
144  __y += vector.getY();
145  __z += vector.getZ();
146 
147  return *this;
148  }
149 
150 
151  /**
152  * Subtract vector.
153  *
154  * \param vector vector
155  * \return this vector
156  */
157  JVector3D& sub(const JVector3D& vector)
158  {
159  __x -= vector.getX();
160  __y -= vector.getY();
161  __z -= vector.getZ();
162 
163  return *this;
164  }
165 
166 
167  /**
168  * Scale vector.
169  *
170  * \param factor multiplication factor
171  * \return this vector
172  */
173  JVector3D& mul(const double factor)
174  {
175  __x *= factor;
176  __y *= factor;
177  __z *= factor;
178 
179  return *this;
180  }
181 
182 
183  /**
184  * Scale vector.
185  *
186  * \param factor division factor
187  * \return this vector
188  */
189  JVector3D& div(const double factor)
190  {
191  __x /= factor;
192  __y /= factor;
193  __z /= factor;
194 
195  return *this;
196  }
197 
198 
199  /**
200  * Transform.
201  *
202  * \param T matrix
203  * \return this vector
204  */
206  {
207  T.transform(__x, __y, __z);
208 
209  return *this;
210  }
211 
212 
213  /**
214  * Check equality.
215  *
216  * \param vector vector
217  * \return true if vectors are equal; else false
218  */
219  bool equals(const JVector3D& vector,
220  const double precision = std::numeric_limits<double>::min()) const
221  {
222  return (fabs(getX() - vector.getX()) <= precision &&
223  fabs(getY() - vector.getY()) <= precision &&
224  fabs(getZ() - vector.getZ()) <= precision);
225  }
226 
227 
228  /**
229  * Get length squared.
230  *
231  * \return square of length
232  */
233  double getLengthSquared() const
234  {
235  return getX()*getX() + getY()*getY() + getZ()*getZ();
236  }
237 
238 
239  /**
240  * Get length.
241  *
242  * \return length
243  */
244  double getLength() const
245  {
246  return sqrt(getLengthSquared());
247  }
248 
249 
250  /**
251  * Get squared of distance to point.
252  *
253  * \param pos position
254  * \return square of distance
255  */
256  double getDistanceSquared(const JVector3D& pos) const
257  {
258  return JVector3D(pos).sub(*this).getLengthSquared();
259  }
260 
261 
262  /**
263  * Get distance to point.
264  *
265  * \param pos position
266  * \return distance
267  */
268  double getDistance(const JVector3D& pos) const
269  {
270  return sqrt(getDistanceSquared(pos));
271  }
272 
273 
274  /**
275  * Get dot product.
276  *
277  * \param vector vector
278  * \return dot product
279  */
280  double getDot(const JVector3D& vector) const
281  {
282  return
283  getX() * vector.getX() +
284  getY() * vector.getY() +
285  getZ() * vector.getZ();
286  }
287 
288 
289  /**
290  * Get cross product.
291  * Note that this vector should not overlap with the first or second vector,
292  *
293  * \param first first vector
294  * \param second second vector
295  * \return this vector
296  */
297  JVector3D& cross(const JVector3D& first,
298  const JVector3D& second)
299  {
300  __x = first .getY() * second.getZ() - second.getY() * first .getZ();
301  __y = second.getX() * first .getZ() - first .getX() * second.getZ();
302  __z = first .getX() * second.getY() - second.getX() * first .getY();
303 
304  return *this;
305  }
306 
307  protected:
308  double __x;
309  double __y;
310  double __z;
311  };
312 }
313 
314 #endif
JGEOMETRY3D::JVector3D::JVector3D
JVector3D()
Default constructor.
Definition: JVector3D.hh:40
JGEOMETRY3D::JVector3D::__y
double __y
Definition: JVector3D.hh:309
JGEOMETRY3D::JVector3D::getLengthSquared
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:233
JGEOMETRY3D::JVector3D::getZ
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JMATH::JMatrix3D
3 x 3 matrix
Definition: JMath/JMatrix3D.hh:32
JGEOMETRY3D::JVector3D::getDistance
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:268
JGEOMETRY3D::JRangeZ
JTOOLS::JRange< double > JRangeZ
Type definition of range along z-axis.
Definition: JVector3D.hh:25
JTOOLS::JRange< double >
JGEOMETRY3D::JVector3D::mul
JVector3D & mul(const double factor)
Scale vector.
Definition: JVector3D.hh:173
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JGEOMETRY3D::JVector3D::cross
JVector3D & cross(const JVector3D &first, const JVector3D &second)
Get cross product.
Definition: JVector3D.hh:297
JGEOMETRY3D::JVector3D::negate
JVector3D & negate()
Negate vector.
Definition: JVector3D.hh:125
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JRange.hh
JGEOMETRY3D::JVector3D
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
JGEOMETRY2D::JRangeX
JTOOLS::JRange< double > JRangeX
Type definition of range along x-axis.
Definition: JVector2D.hh:22
JGEOMETRY3D::JVector3D::JVector3D
JVector3D(const double x, const double y, const double z)
Constructor.
Definition: JVector3D.hh:54
JGEOMETRY3D::JVector3D::JVector3D
JVector3D(const JVector2D &vector, const double z)
Constructor.
Definition: JVector3D.hh:69
JVector2D.hh
JMath.hh
JGEOMETRY2D::JRangeY
JTOOLS::JRange< double > JRangeY
Type definition of range along y-axis.
Definition: JVector2D.hh:23
JMATH::JMatrix3D::transform
void transform(double &__x, double &__y, double &__z) const
Transform.
Definition: JMath/JMatrix3D.hh:318
JGEOMETRY3D::JVector3D::getDistanceSquared
double getDistanceSquared(const JVector3D &pos) const
Get squared of distance to point.
Definition: JVector3D.hh:256
JGEOMETRY3D::JVector3D::getY
double getY() const
Get y position.
Definition: JVector3D.hh:103
JMatrix3D.hh
JGEOMETRY3D::JVector3D::getDot
double getDot(const JVector3D &vector) const
Get dot product.
Definition: JVector3D.hh:280
JGEOMETRY3D
Auxiliary classes and methods for 3D geometrical objects and operations.
Definition: JAngle3D.hh:18
JGEOMETRY3D::JVector3D::__z
double __z
Definition: JVector3D.hh:310
JGEOMETRY3D::JVector3D::equals
bool equals(const JVector3D &vector, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition: JVector3D.hh:219
JGEOMETRY3D::JVector3D::__x
double __x
Definition: JVector3D.hh:308
JGEOMETRY3D::JVector3D::add
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:141
JGEOMETRY3D::JVector3D::getX
double getX() const
Get x position.
Definition: JVector3D.hh:93
JGEOMETRY3D::JVector3D::div
JVector3D & div(const double factor)
Scale vector.
Definition: JVector3D.hh:189
JGEOMETRY3D::JVector3D::transform
JVector3D & transform(const JMatrix3D &T)
Transform.
Definition: JVector3D.hh:205
JGEOMETRY3D::JVector3D::sub
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:157
JGEOMETRY3D::JVector3D::getLength
double getLength() const
Get length.
Definition: JVector3D.hh:244
JGEOMETRY2D::JVector2D
Data structure for vector in two dimensions.
Definition: JVector2D.hh:31