-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector3d.h
431 lines (385 loc) · 13.2 KB
/
vector3d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#ifndef VECTOR3D_H
#define VECTOR3D_H
#include "vector2d.h"
#include <cmath>
#include <iostream>
#include <QMatrix3x3>
class Vector2D;
/**
* @brief The Vector3D class
*/
class Vector3D
{
public:
/**
* Default constructor, initializes xp, yp and zp to 0
*/
Vector3D();
/**
* Constructor of Vector3D
* @param[in] xpos the x position of the vector
* @param[in] ypos the y position of the vector
* @param[in] zpos the z position of the vector
*/
Vector3D(double xpos, double ypos, double zpos):xp(xpos), yp(ypos), zp(zpos){}
/**
* Constructor of Vector3D from a Vector2D, zp set to 0
* @param[in] vector the Vector2D to base the xp and yp coordinates on
*/
Vector3D(const Vector2D& vector);
/**
* Constructor of Vector3D from a Vector2D
* @param[in] vector the Vector2D to base the xp and yp coordinates on
* @param[in] zpos the z position of the vector
*/
Vector3D(const Vector2D& vector, double zpos);
/**
* @brief Return the x value of the vector
* @return the x value of the vector
*/
double x() const;
/**
* @brief Return the y value of the vector
* @return the y value of the vector
*/
double y() const;
/**
* @brief Return the z value of the vector
* @return the z value of the vector
*/
double z() const;
/**
* Sets the value of xp
* @param[in] xpos the new value of xp
*/
void setX(double xpos);
/**
* Sets the value of yp
* @param[in] ypos the new value of yp
*/
void setY(double ypos);
/**
* Sets the value of zp
* @param[in] zpos the new value of zp
*/
void setZ(double zpos);
/**
* Returns the length of the vector. Uses a square root
* @return the length of the vector
*/
double length() const;
/**
* Returns the squared length of the vector
* @return the squared length of the vector
*/
double lengthSquared() const;
/**
* Returns the normalized vector
* @return a new Vector2D corresponding to the normalized vector
*/
Vector3D normalized() const;
/**
* Normalizes the vector
*/
void normalize();
/**
* Rotates a vector according to a rotation matrix
* @param[in] mat The rotation matrix to base the rotation on
*/
void rotate(const QMatrix3x3 &mat);
/**
* Adds a vector to this vector and returns its reference
* @param[in] vector the vector to add
* @return a reference to the vector
*/
Vector3D &operator+=(const Vector3D &vector);
/**
* Substracts a vector to this vector and returns its reference
* @param[in] vector the vector to substract
* @return a reference to the vector
*/
Vector3D &operator-=(const Vector3D &vector);
/**
* Multiplies the vector by a factor and returns its reference
* @param[in] factor the factor to multiply by
* @return a reference to the vector
*/
Vector3D &operator*=(double factor);
/**
* Divides the vector by a divisor and returns its reference
* @param[in] divisor the divisor to divide by
* @return a reference to the vector
*/
Vector3D &operator/=(double divisor);
/**
* Dot product of the vector and another one
* @param[in] v1 the other vector for the dot product
* @return the value of the dot product of the two vectors
*/
double operator*(const Vector3D& v1) const;
/**
* Cross product of the vector and another one
* @param[in] v2 the other vector for the cross product
* @return the Vector3D resulting from the cross product
*/
const Vector3D operator^(const Vector3D& v2)const ;
/**
* Read-access to the i-th member of the vector
* @param i the number of the member
* @return
*/
double operator()(int i) const;
/**
* Write-access to the i-th member of the vector
* @param i the number of the member
* @return
*/
double& operator()(int i);
/**
* Returns the normal of the plane described by the two vectors
* @param[in] v1 the first vector describing the plane
* @param[in] v2 the second vector describing the plane
* @return a Vector3D representing the normal
*/
static Vector3D normal(const Vector3D& v1, const Vector3D& v2);
/**
* Returns the normal of the plane described by the three points
* @param[in] v1 the first point
* @param[in] v2 the second point
* @param[in] v3 the third point
* @return a Vector3D representing the normal
*/
static Vector3D normal(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3);
/**
* Returns the distance between the point represented by the vector and another point. Uses a square root
* @param[in] point the point we want the distance to
* @return the distance between the two points
*/
double distanceToPoint(const Vector3D& point) const;
/**
* Returns the square of the distance between the point represented by the vector and another point
* @param[in] point the point we want the distance to
* @return the square of the distance between the two points
*/
double distanceToPointSquared(const Vector3D& point) const;
/**
* Returns the shortest distance between the 3D point and a plane represented by a point and a normal
* @param[in] plane the point on the plane
* @param[in] normal the normal of the plane
* @return the shortest distance between the point and the plane
*/
double distanceToPlane(const Vector3D& plane, const Vector3D& normal) const;
/**
* Returns the shortest distance between the 3D point and a plane described by three points
* @param[in] plane1 the first point on the plane
* @param[in] plane2 the second point on the plane
* @param[in] plane3 he third point on the plane
* @return the shortest distance between the point and the plane
*/
double distanceToPlane(const Vector3D& plane1, const Vector3D& plane2, const Vector3D& plane3) const;
/**
* Returns the shortest distance between the 3D point and a line
* @param[in] point a point on the line
* @param[in] direction the direction of the line
* @return the shortest distance between the 3D point and the line
*/
double distanceToLine(const Vector3D& point, const Vector3D& direction) const;
/**
* Checks if the coordinates of the two vectors are strictly equal
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the coordinates xp and yp of the vectors are equals, else false
*/
friend inline bool operator==(const Vector3D &v1, const Vector3D &v2);
/**
* Checks if the coordinates of the two vectors are not strictly equal
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the coordinates xp and yp of the vectors are not equals, else false
*/
friend inline bool operator!=(const Vector3D &v1, const Vector3D &v2);
/**
* Adds two vectors and returns the result of the addition
* @param[in] v1 the first vector to add
* @param[in] v2 the second vector to add
* @return a new Vector3D resulting of the addition of the two others
*/
friend inline const Vector3D operator+(const Vector3D &v1, const Vector3D &v2);
/**
* Substracts a vector to the other and returns the result of the addition
* @param[in] v1 the first vector to be substracted
* @param[in] v2 the second vector to substract
* @return a new Vector3D resulting of the substraction
*/
friend inline const Vector3D operator-(const Vector3D &v1, const Vector3D &v2);
/**
* Multiplies a vector by a factor and returns the result of the multiplication
* @param[in] factor the factor to multiply by
* @param[in] vector the vector to multiply
* @return a new Vector3D resulting of the multiplication
*/
friend inline const Vector3D operator*(double factor, const Vector3D &vector);
/**
* Multiplies a vector by a factor and returns the result of the multiplication
* @param[in] vector the vector to multiply
* @param[in] factor the factor to multiply by
* @return a new Vector3D resulting of the multiplication
*/
friend inline const Vector3D operator*(const Vector3D &vector, double factor);
/**
* Reverses the coordinates of the vector
* @param[in] vector the vector to reverse
* @return a new Vector3D resulting of the reversing
*/
friend inline const Vector3D operator-(const Vector3D &vector);
/**
* Divides a vector by a divisor and returns the result of the division
* @param[in] vector the vector to divide
* @param[in] divisor the divisor to divide by
* @return a new Vector3D resulting of the division
*/
friend inline const Vector3D operator/(const Vector3D &vector, double divisor);
/**
* Compares the coordinates of the two vectors, allowing a small difference
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the vectors are at worst slightly different, else false
*/
friend inline bool fuzzyCompare(const Vector3D& v1, const Vector3D& v2);
/**
* Prints the coordinates of the vector in the ostream
* @param[in] out the stream to write in
* @param[in] v the vector to write in the stream
* @return a reference to the stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const Vector3D& v);
private:
double xp; /**< The x coordinate of the vector */
double yp; /**< The y coordinate of the vector */
double zp; /**< The z coordinate of the vector */
};
inline Vector3D::Vector3D() : xp(0.0), yp(0.0), zp(0.0) {}
inline double Vector3D::x() const { return xp; }
inline double Vector3D::y() const { return yp; }
inline double Vector3D::z() const { return zp; }
inline void Vector3D::setX(double aX) { xp = aX; }
inline void Vector3D::setY(double aY) { yp = aY; }
inline void Vector3D::setZ(double aZ) { zp = aZ; }
inline Vector3D &Vector3D::operator+=(const Vector3D &vector)
{
xp += vector.xp;
yp += vector.yp;
zp += vector.zp;
return *this;
}
inline Vector3D &Vector3D::operator-=(const Vector3D &vector)
{
xp -= vector.xp;
yp -= vector.yp;
zp -= vector.zp;
return *this;
}
inline Vector3D &Vector3D::operator*=(double factor)
{
xp *= factor;
yp *= factor;
zp *= factor;
return *this;
}
inline Vector3D &Vector3D::operator/=(double factor)
{
xp /= factor;
yp /= factor;
zp /= factor;
return *this;
}
inline bool operator==(const Vector3D &v1, const Vector3D &v2)
{
return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
}
inline bool operator!=(const Vector3D &v1, const Vector3D &v2)
{
return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp;
}
inline const Vector3D operator+(const Vector3D &v1, const Vector3D &v2)
{
return Vector3D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp);
}
inline const Vector3D operator-(const Vector3D &v1, const Vector3D &v2)
{
return Vector3D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp);
}
inline const Vector3D operator*(double factor, const Vector3D &vector)
{
return Vector3D(vector.x() * factor, vector.y() * factor, vector.z() * factor);
}
inline const Vector3D operator*(const Vector3D &vector, double factor)
{
return Vector3D(vector.x() * factor, vector.y() * factor, vector.z() * factor);
}
inline double Vector3D::operator*(const Vector3D &v1) const
{
return xp * v1.x()+ yp * v1.y()+ zp * v1.z();
}
inline const Vector3D Vector3D::operator^(const Vector3D &v1) const
{
return Vector3D(yp*v1.z()-zp*v1.y(),zp*v1.x()-xp*v1.z(),xp*v1.y()-yp*v1.x());
}
inline double Vector3D::operator()(int i) const {
if(i == 0)
return xp;
if(i == 1)
return yp;
return zp;
}
inline double& Vector3D::operator()(int i) {
if(i == 0)
return xp;
if(i == 1)
return yp;
return zp;
}
inline const Vector3D operator-(const Vector3D &vector)
{
return Vector3D(-vector.xp, -vector.yp, -vector.zp);
}
inline const Vector3D operator/(const Vector3D &vector, double divisor)
{
return Vector3D(vector.x() / divisor, vector.y() / divisor, vector.z() / divisor);
}
inline bool fuzzyCompare(const Vector3D& v1, const Vector3D& v2)
{
return v1.distanceToPointSquared(v2)<0.0000001;
}
inline double Vector3D::length() const
{
return sqrt(xp*xp+yp*yp+zp*zp);
}
inline double Vector3D::lengthSquared() const
{
return xp*xp+yp*yp+zp*zp;
}
inline Vector3D Vector3D::normalized() const
{
double invlength =1/length();
return Vector3D(xp*invlength,yp*invlength,zp*invlength);
}
inline void Vector3D::normalize()
{
double invlength =1/length();
xp*=invlength;
yp*=invlength;
zp*=invlength;
}
inline double Vector3D::distanceToPoint(const Vector3D& point) const
{
return (point-(*this)).length();
}
inline double Vector3D::distanceToPointSquared(const Vector3D& point) const
{
return (point-(*this)).lengthSquared();
}
inline std::ostream& operator<<(std::ostream& out, const Vector3D& v){
return out<<"("<<v.x()<<", "<<v.y()<<", "<<v.z()<<")";
}
#endif // VECTOR3D_H