-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.cpp
More file actions
212 lines (172 loc) · 4.3 KB
/
Vector3.cpp
File metadata and controls
212 lines (172 loc) · 4.3 KB
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
#include "Vector3.h"
#include "Vector4.h"
#include <cassert>
#include "Random/RandomNumberGenerator.h"
#include <cmath>
namespace dae {
const Vector3 Vector3::UnitX = Vector3{ 1, 0, 0 };
const Vector3 Vector3::UnitY = Vector3{ 0, 1, 0 };
const Vector3 Vector3::UnitZ = Vector3{ 0, 0, 1 };
const Vector3 Vector3::Zero = Vector3{ 0, 0, 0 };
Vector3::Vector3(float _x, float _y, float _z) : x(_x), y(_y), z(_z){}
Vector3::Vector3(const Vector4& v) : x(v.x), y(v.y), z(v.z){}
Vector3::Vector3(const Vector3& from, const Vector3& to) : x(to.x - from.x), y(to.y - from.y), z(to.z - from.z){}
float Vector3::Magnitude() const
{
return sqrtf(x * x + y * y + z * z);
}
float Vector3::SqrMagnitude() const
{
return x * x + y * y + z * z;
}
//Nroramlize the vector and return the magnitude before normalizing
float Vector3::Normalize()
{
const float m = Magnitude();
x /= m;
y /= m;
z /= m;
return m;
}
Vector3 Vector3::Normalized() const
{
const float m = Magnitude();
return { x / m, y / m, z / m };
}
float Vector3::Dot(const Vector3& v1, const Vector3& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2)
{
return Vector3{ v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x };
}
Vector3 Vector3::Project(const Vector3& v1, const Vector3& v2)
{
return (v2 * (Dot(v1, v2) / Dot(v2, v2)));
}
Vector3 Vector3::Reject(const Vector3& v1, const Vector3& v2)
{
return (v1 - v2 * (Dot(v1, v2) / Dot(v2, v2)));
}
Vector3 Vector3::Reflect(const Vector3& v1, const Vector3& v2)
{
return v1 - (2.f * Vector3::Dot(v1, v2) * v2);
}
Vector3 Vector3::Lico(float f1, const Vector3& v1, float f2, const Vector3& v2, float f3, const Vector3& v3)
{
return (v1 * f1 + v2 * f2 + v3 * f3).Normalized();
}
Vector3 Vector3::Min(const Vector3& a, const Vector3& b)
{
return { std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z) };
}
Vector3 Vector3::Max(const Vector3& a, const Vector3& b)
{
return { std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z) };
}
Vector3 Vector3::GetRandomVector3(float min, float max)
{
return Vector3
{
RandomNumberGenerator::GetRandomValue(min, max),
RandomNumberGenerator::GetRandomValue(min, max),
RandomNumberGenerator::GetRandomValue(min, max)
};
}
Vector3 Vector3::GetRandomInUnitSphere()
{
//Use a simple rejection method to generate a random point in a unit sphere
while(true)
{
const auto point = GetRandomVector3(-1,1);
if(point.SqrMagnitude() < 1) return point;
}
}
Vector3 Vector3::GetRandomUnitVector()
{
return GetRandomInUnitSphere().Normalized();
}
Vector3 Vector3::GetRandomInHemisphere(const Vector3& normal)
{
const auto unitVector = GetRandomUnitVector();
//if in the same hemisphere as the normal, return it
if(Dot(unitVector, normal) > 0.0f) return unitVector;
//otherwise return the inverse
return -unitVector;
}
Vector4 Vector3::ToPoint4() const
{
return { x, y, z, 1 };
}
Vector4 Vector3::ToVector4() const
{
return { x, y, z, 0 };
}
#pragma region Operator Overloads
Vector3 Vector3::operator*(float scale) const
{
return { x * scale, y * scale, z * scale };
}
Vector3 Vector3::operator/(float scale) const
{
return { x / scale, y / scale, z / scale };
}
Vector3 Vector3::operator+(const Vector3& v) const
{
return { x + v.x, y + v.y, z + v.z };
}
Vector3 Vector3::operator-(const Vector3& v) const
{
return { x - v.x, y - v.y, z - v.z };
}
Vector3 Vector3::operator-() const
{
return { -x ,-y,-z };
}
Vector3& Vector3::operator*=(float scale)
{
x *= scale;
y *= scale;
z *= scale;
return *this;
}
Vector3& Vector3::operator/=(float scale)
{
x /= scale;
y /= scale;
z /= scale;
return *this;
}
Vector3& Vector3::operator-=(const Vector3& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vector3& Vector3::operator+=(const Vector3& v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
float& Vector3::operator[](int index)
{
assert(index <= 2 && index >= 0);
if (index == 0) return x;
if (index == 1) return y;
return z;
}
float Vector3::operator[](int index) const
{
assert(index <= 2 && index >= 0);
if (index == 0) return x;
if (index == 1) return y;
return z;
}
#pragma endregion
}