-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3.h
81 lines (72 loc) · 1.84 KB
/
Vector3.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
#ifndef VECTOR3_H
#define VECTOR3_H
#include <cmath>
#include "btBulletDynamicsCommon.h"
class Vector3 {
private:
double X, Y, Z;
public:
Vector3() {}
Vector3(double x, double y, double z): X(x), Y(y), Z(z) {}
Vector3(const btVector3 &v): X(v.x()), Y(v.y()), Z(v.z()) {}
double x() const { return X; }
double y() const { return Y; }
double z() const { return Z; }
void setX(double x) { X = x; }
void setY(double y) { Y = y; }
void setZ(double z) { Z = z; }
Vector3 & operator += (const Vector3 &v) {
X += v.x(); Y += v.y(); Z += v.z();
return *this;
}
Vector3 & operator -= (const Vector3 &v) {
X -= v.x(); Y -= v.y(); Z -= v.z();
return *this;
}
Vector3 & operator *= (const double &v) {
X *= v; Y *= v; Z *= v;
return *this;
}
Vector3 & operator /= (const double &v) {
X /= v; Y /= v; Z /= v;
return *this;
}
Vector3 & operator *= (const Vector3 &v) {
X *= v.x(); Y *= v.y(); Z *= v.z();
return *this;
}
Vector3 operator +(const Vector3 &v) const {
return Vector3(X + v.x(), Y + v.y(), Z + v.z());
}
Vector3 operator -(const Vector3 &v) const {
return Vector3(X - v.x(), Y - v.y(), Z - v.z());
}
Vector3 operator *(const double &v) const {
return Vector3(X * v, Y * v, Z * v);
}
Vector3 operator /(const double &v) const {
return Vector3(X / v, Y / v, Z / v);
}
Vector3 operator *(const Vector3 &v) const {
return Vector3(X * v.x(), Y * v.y(), Z * v.z());
}
double dot(const Vector3 &v) const{
return X * v.x() + Y * v.y() + Z * v.z();
}
double length() const{
return sqrt(dot(*this));
}
Vector3 cross(const Vector3 &v) const {
return Vector3(Y * v.z() - Z * v.y(),
Z * v.x() - X * v.z(),
X * v.y() - Y * v.x());
}
Vector3 normalize() const {
if (length() < 1e-10) return *this;
else return (*this) / length();
}
btVector3 toBullet() const{
return btVector3(X, Y, Z);
}
};
#endif