Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * \param precision precision
218  * \return true if vectors are equal; else false
219  */
220  bool equals(const JVector3D& vector,
221  const double precision = std::numeric_limits<double>::min()) const
222  {
223  return (fabs(getX() - vector.getX()) <= precision &&
224  fabs(getY() - vector.getY()) <= precision &&
225  fabs(getZ() - vector.getZ()) <= precision);
226  }
227 
228 
229  /**
230  * Get length squared.
231  *
232  * \return square of length
233  */
234  double getLengthSquared() const
235  {
236  return getX()*getX() + getY()*getY() + getZ()*getZ();
237  }
238 
239 
240  /**
241  * Get length.
242  *
243  * \return length
244  */
245  double getLength() const
246  {
247  return sqrt(getLengthSquared());
248  }
249 
250 
251  /**
252  * Get squared of distance to point.
253  *
254  * \param pos position
255  * \return square of distance
256  */
257  double getDistanceSquared(const JVector3D& pos) const
258  {
259  return JVector3D(pos).sub(*this).getLengthSquared();
260  }
261 
262 
263  /**
264  * Get distance to point.
265  *
266  * \param pos position
267  * \return distance
268  */
269  double getDistance(const JVector3D& pos) const
270  {
271  return sqrt(getDistanceSquared(pos));
272  }
273 
274 
275  /**
276  * Get dot product.
277  *
278  * \param vector vector
279  * \return dot product
280  */
281  double getDot(const JVector3D& vector) const
282  {
283  return
284  getX() * vector.getX() +
285  getY() * vector.getY() +
286  getZ() * vector.getZ();
287  }
288 
289 
290  /**
291  * Get cross product.
292  * Note that this vector should not overlap with the first or second vector,
293  *
294  * \param first first vector
295  * \param second second vector
296  * \return this vector
297  */
299  const JVector3D& second)
300  {
301  __x = first .getY() * second.getZ() - second.getY() * first .getZ();
302  __y = second.getX() * first .getZ() - first .getX() * second.getZ();
303  __z = first .getX() * second.getY() - second.getX() * first .getY();
304 
305  return *this;
306  }
307 
308  protected:
309  double __x;
310  double __y;
311  double __z;
312  };
313 }
314 
315 #endif
Data structure for vector in two dimensions.
Definition: JVector2D.hh:31
JVector3D & mul(const double factor)
Scale vector.
Definition: JVector3D.hh:173
JVector3D(const JVector2D &vector, const double z)
Constructor.
Definition: JVector3D.hh:69
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
bool equals(const JVector3D &vector, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition: JVector3D.hh:220
3 x 3 matrix
JTOOLS::JRange< double > JRangeX
Type definition of range along x-axis.
Definition: JVector2D.hh:22
double getDistanceSquared(const JVector3D &pos) const
Get squared of distance to point.
Definition: JVector3D.hh:257
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:269
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JTOOLS::JRange< double > JRangeZ
Type definition of range along z-axis.
Definition: JVector3D.hh:25
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:157
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
do set_variable OUTPUT_DIRECTORY $WORKDIR T
double getY() const
Get y position.
Definition: JVector3D.hh:103
double getLength() const
Get length.
Definition: JVector3D.hh:245
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:234
JVector3D & cross(const JVector3D &first, const JVector3D &second)
Get cross product.
Definition: JVector3D.hh:298
Auxiliary class to define a range between two values.
JTOOLS::JRange< double > JRangeY
Type definition of range along y-axis.
Definition: JVector2D.hh:23
double getX() const
Get x position.
Definition: JVector3D.hh:93
Base class for data structures with artithmetic capabilities.
double getDot(const JVector3D &vector) const
Get dot product.
Definition: JVector3D.hh:281
JVector3D & negate()
Negate vector.
Definition: JVector3D.hh:125
JVector3D & div(const double factor)
Scale vector.
Definition: JVector3D.hh:189
void transform(double &__x, double &__y, double &__z) const
Transform.
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:141
JVector3D(const double x, const double y, const double z)
Constructor.
Definition: JVector3D.hh:54
JVector3D()
Default constructor.
Definition: JVector3D.hh:40
JVector3D & transform(const JMatrix3D &T)
Transform.
Definition: JVector3D.hh:205