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