-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3.hpp
225 lines (194 loc) · 7.71 KB
/
Vec3.hpp
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
#ifndef VEC3_HPP
#define VEC3_HPP
#include <iostream>
#include <math.h>
template <class T> class Vec3
{
private:
// A Vec3 simply has three properties called x, y and z
T x, y, z;
public:
// ------------ Constructors ------------
// Default constructor
Vec3() { x = y = z = 0; };
// Three parameter constructor
Vec3(T xValue, T yValue, T zValue)
{
x = xValue;
y = yValue;
z = zValue;
}
// ------------ Getters and setters ------------
void set(const T &xValue, const T &yValue, const T &zValue)
{
x = xValue;
y = yValue;
z = zValue;
}
T getX() const { return x; }
T getY() const { return y; }
T getZ() const { return z; }
void setX(const T &xValue) { x = xValue; }
void setY(const T &yValue) { y = yValue; }
void setZ(const T &zValue) { z = zValue; }
// ------------ Helper methods ------------
// Method to reset a vector to zero
void zero()
{
x = y = z = 0;
}
// Method to normalise a vector
void normalise()
{
// Calculate the magnitude of our vector
T magnitude = sqrt((x * x) + (y * y) + (z * z));
// As long as the magnitude isn't zero, divide each element by the magnitude
// to get the normalised value between -1 and +1
if (magnitude != 0)
{
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
}
// Static method to calculate and return the scalar dot product of two vectors
//
// Note: The dot product of two vectors tell us things about the angle between
// the vectors. That is, it tells us if they are pointing in the same direction
// (i.e. are they parallel? If so, the dot product will be 1), or if they're
// perpendicular (i.e. at 90 degrees to each other) the dot product will be 0,
// or if they're pointing in opposite directions then the dot product will be -1.
//
// Usage example: double foo = Vec3<double>::dotProduct(vectorA, vectorB);
static T dotProduct(const Vec3 &vec1, const Vec3 &vec2)
{
return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
}
// Non-static method to calculate and return the scalar dot product of this vector and another vector
//
// Usage example: double foo = vectorA.dotProduct(vectorB);
T dotProduct(const Vec3 &vec) const
{
return x * vec.x + y * vec.y + z * vec.z;
}
// Static method to calculate and return a vector which is the cross product of two vectors
//
// Note: The cross product is simply a vector which is perpendicular to the plane formed by
// the first two vectors. Think of a desk like the one your laptop or keyboard is sitting on.
// If you put one pencil pointing directly away from you, and then another pencil pointing to the
// right so they form a "L" shape, the vector perpendicular to the plane made by these two pencils
// points directly upwards.
//
// Whether the vector is perpendicularly pointing "up" or "down" depends on the "handedness" of the
// coordinate system that you're using.
//
// Further reading: http://en.wikipedia.org/wiki/Cross_product
//
// Usage example: Vec3<double> crossVect = Vec3<double>::crossProduct(vectorA, vectorB);
static Vec3 crossProduct(const Vec3 &vec1, const Vec3 &vec2)
{
return Vec3(vec1.y * vec2.z - vec1.z * vec2.y, vec1.z * vec2.x - vec1.x * vec2.z, vec1.x * vec2.y - vec1.y * vec2.x);
}
// Easy adders
void addX(T value) { x += value; }
void addY(T value) { y += value; }
void addZ(T value) { z += value; }
// Method to return the distance between two vectors in 3D space
//
// Note: This is accurate, but not especially fast - depending on your needs you might
// like to use the Manhattan Distance instead: http://en.wikipedia.org/wiki/Taxicab_geometry
// There's a good discussion of it here: http://stackoverflow.com/questions/3693514/very-fast-3d-distance-check
// The gist is, to find if we're within a given distance between two vectors you can use:
//
// bool within3DManhattanDistance(Vec3 c1, Vec3 c2, float distance)
// {
// float dx = abs(c2.x - c1.x);
// if (dx > distance) return false; // too far in x direction
//
// float dy = abs(c2.y - c1.y);
// if (dy > distance) return false; // too far in y direction
//
// float dz = abs(c2.z - c1.z);
// if (dz > distance) return false; // too far in z direction
//
// return true; // we're within the cube
// }
//
// Or to just calculate the straight Manhattan distance you could use:
//
// float getManhattanDistance(Vec3 c1, Vec3 c2)
// {
// float dx = abs(c2.x - c1.x);
// float dy = abs(c2.y - c1.y);
// float dz = abs(c2.z - c1.z);
// return dx+dy+dz;
// }
//
static T getDistance(const Vec3 &v1, const Vec3 &v2)
{
T dx = v2.x - v1.x;
T dy = v2.y - v1.y;
T dz = v2.z - v1.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
// Method to display the vector so you can easily check the values
void display()
{
std::cout << "X: " << x << "\t Y: " << y << "\t Z: " << z << std::endl;
}
// ------------ Overloaded operators ------------
// Overloaded addition operator to add Vec3s together
Vec3 operator+(const Vec3 &vector) const
{
return Vec3<T>(x + vector.x, y + vector.y, z + vector.z);
}
// Overloaded add and asssign operator to add Vec3s together
void operator+=(const Vec3 &vector)
{
x += vector.x;
y += vector.y;
z += vector.z;
}
// Overloaded subtraction operator to subtract a Vec3 from another Vec3
Vec3 operator-(const Vec3 &vector) const
{
return Vec3<T>(x - vector.x, y - vector.y, z - vector.z);
}
// Overloaded subtract and assign operator to subtract a Vec3 from another Vec3
void operator-=(const Vec3 &vector)
{
x -= vector.x;
y -= vector.y;
z -= vector.z;
}
// Overloaded multiplication operator to multiply two Vec3s together
Vec3 operator*(const Vec3 &vector) const
{
return Vec3<T>(x * vector.x, y * vector.y, z * vector.z);
}
// Overloaded multiply operator to multiply a vector by a scalar
Vec3 operator*(const T &value) const
{
return Vec3<T>(x * value, y * value, z * value);
}
// Overloaded multiply and assign operator to multiply a vector by a scalar
void operator*=(const T &value)
{
x *= value;
y *= value;
z *= value;
}
// Overloaded multiply operator to multiply a vector by a scalar
Vec3 operator/(const T &value) const
{
return Vec3<T>(x / value, y / value, z / value);
}
// Overloaded multiply and assign operator to multiply a vector by a scalar
void operator/=(const T &value)
{
x /= value;
y /= value;
z /= value;
}
};
#endif