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