forked from shaxquan/CppClassUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3.h
175 lines (138 loc) · 3.56 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
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
#ifndef __VECTOR3_H_INCLUDED__
#define __VECTOR3_H_INCLUDED__
#include <math.h>
class Vector3
{
public:
float x, y, z;
Vector3() {}
Vector3(const Vector3 &v) : x(v.x), y(v.y), z(v.z) {}
Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}
Vector3 &operator=(const Vector3 &v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
bool operator==(const Vector3 &v) const
{
return x==v.x && y==v.y && z==v.z;
}
boll operator!=(const Vector3 &v) const
{
return x!=v.x || y!=v.y || z!=v.z;
}
void zero()
{
x = y = z = 0.0f;
}
//unary minus
Vector3 operator -() const
{
return Vector3(-x, -y, -z);
}
// Binary + and - add and subtract vectors
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);
}
// Multiplication and division by scalar
Vector3 operator *(float a) const
{
return Vector3(x*a, y*a, z*a);
}
Vector3 operator /(float a) const
{
float oneOverA = 1.0f / a; // NOTE: no check for divide by zero here
return Vector3(x*oneOverA, y*oneOverA, z*oneOverA);
}
// Combined assignment operators to conform to
// C notation convention
Vector3 &operator +=(const Vector3 &a)
{
x += a.x; y += a.y; z += a.z;
return *this;
}
Vector3 &operator -=(const Vector3 &a)
{
x -= a.x; y -= a.y; z -= a.z;
return *this;
}
Vector3 &operator *=(float a)
{
x *= a; y *= a; z *= a;
return *this;
}
Vector3 &operator /=(float a)
{
float oneOverA = 1.0f / a;
x *= oneOverA; y *= oneOverA; z *= oneOverA;
return *this;
}
// Normalize the vector
void normalize()
{
float magSq = x*x + y*y + z*z;
if (magSq > 0.0f) { // check for divide-by-zero
float oneOverMag = 1.0f / sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}
// Vector dot product. We overload the standard
// multiplication symbol to do this
float operator *(const Vector3 &a) const
{
return x*a.x + y*a.y + z*a.z;
}
};
/////////////////////////////////////////////////////////////////////////////
//
// Nonmember functions
//
/////////////////////////////////////////////////////////////////////////////
// Compute the magnitude of a vector
inline float vectorMag(const Vector3 &a) {
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
// Compute the cross product of two vectors
inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b) {
return Vector3(
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x
);
}
// Scalar on the left multiplication, for symmetry
inline Vector3 operator *(float k, const Vector3 &v) {
return Vector3(k*v.x, k*v.y, k*v.z);
}
// Compute the distance between two points
inline float distance(const Vector3 &a, const Vector3 &b) {
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return sqrt(dx*dx + dy*dy + dz*dz);
}
// Compute the distance between two points, squared. Often useful
// when comparing distances, since the square root is slow
inline float distanceSquared(const Vector3 &a, const Vector3 &b) {
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return dx*dx + dy*dy + dz*dz;
}
/////////////////////////////////////////////////////////////////////////////
//
// Global variables
//
/////////////////////////////////////////////////////////////////////////////
// We provide a global zero vector constant
extern const Vector3 kZeroVector;
#endif