-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathvector4.cpp
More file actions
296 lines (268 loc) · 7.63 KB
/
Copy pathvector4.cpp
File metadata and controls
296 lines (268 loc) · 7.63 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
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
/*
This file is part of Thunder Next.
Thunder Next is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Thunder Next is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Thunder Next. If not, see <http://www.gnu.org/licenses/>.
Copyright: 2008-2023 Evgeniy Prikazchikov
*/
#include "math/amath.h"
/*!
\class Vector4
\brief The Vector4 class represents a vector or vertex in 4D space.
\since Next 1.0
\inmodule Math
Vectors are one of the main building blocks of 4D representation and
drawing. They consist of three coordinates, traditionally called
x, y, z and w.
The Vector4 class can also be used to represent vertices in 4D space.
We therefore do not need to provide a separate vertex class.
\note By design values in the Vector4 instance are stored as \c float.
This means that on platforms where the \c areal arguments to Vector4
functions are represented by \c double values, it is possible to
lose precision.
\sa Vector2, Vector3, Quaternion
*/
/*!
Constructs a null vector, i.e. with coordinates (0, 0, 0, 1).
*/
Vector4::Vector4() :
x(0),
y(0),
z(0),
w(0) {
}
/*!
Constructs a vector with coordinates (\a v).
*/
Vector4::Vector4(areal v) :
x(v),
y(v),
z(v),
w(v) {
}
/*!
Constructs a vector with coordinates (\a x, \a y, \a z, \a w).
*/
Vector4::Vector4(areal x, areal y, areal z, areal w) :
x(x),
y(y),
z(z),
w(w) {
}
/*!
Constructs a 4D vector from the specified 2D \a vector.
*/
Vector4::Vector4(const Vector2 &vector) :
x(vector.x),
y(vector.y),
z(0.0f),
w(0.0f) {
}
/*!
Constructs a 4D vector from the specified 2D \a vector. The z and w
coordinates is set to \a z and \a w.
*/
Vector4::Vector4(const Vector2 &vector, areal z, areal w) :
x(vector.x),
y(vector.y),
z(z),
w(w) {
}
/*!
Constructs a 4D vector from the specified 3D \a vector.
\sa Vector3::Vector3()
*/
Vector4::Vector4(const Vector3 &vector) :
x(vector.x),
y(vector.y),
z(vector.z),
w(0.0f) {
}
/*!
Constructs a 4D vector from the specified 3D \a vector. The w
coordinate is set to \a w.
\sa Vector3::Vector3()
*/
Vector4::Vector4(const Vector3 &vector, areal w) :
x(vector.x),
y(vector.y),
z(vector.z),
w(w) {
}
/*!
Copy constructor.
*/
Vector4::Vector4(const Vector4 &vector) {
x = vector.x;
y = vector.y;
z = vector.z;
w = vector.w;
}
/*!
Assignment operator.
The \a value will be assigned to this object.
*/
Vector4 &Vector4::operator=(const Vector4 &value) {
x = value.x;
y = value.y;
z = value.z;
w = value.w;
return *this;
}
/*!
Returns true if this vector is equal to given \a vector; otherwise returns false.
This operator uses an exact floating-point comparison.
*/
bool Vector4::operator==(const Vector4 &vector) const {
return (x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w);
}
/*!
Returns true if this vector is NOT equal to given \a vector; otherwise returns false.
This operator uses an exact floating-point comparison.
*/
bool Vector4::operator!=(const Vector4 &vector) const {
return !(*this == vector);
}
/*!
Returns true if this vector is bigger than given \a vector; otherwise returns false.
This operator uses an exact floating-point comparison.
*/
bool Vector4::operator>(const Vector4 &vector) const {
return (x > vector.x) && (y > vector.y) && (z > vector.z) && (w > vector.w);
}
/*!
Returns true if this vector is less than \a vector; otherwise returns false.
This operator uses an exact floating-point comparison.
*/
bool Vector4::operator<(const Vector4 &vector) const {
return (x < vector.x) && (y < vector.y) && (z < vector.z) && (w < vector.w);
}
/*!
Returns a copy of this vector, multiplied by the given \a factor.
\sa operator*=()
*/
Vector4 Vector4::operator*(areal factor) const {
return Vector4(x * factor, y * factor, z * factor, w * factor);
}
/*!
Returns a copy of this vector, multiplied by the given \a vector.
\sa operator*=()
*/
Vector4 Vector4::operator*(const Vector4 &vector) const {
return Vector4(x * vector.x, y * vector.y, z * vector.z, w * vector.w);
}
/*!
Returns a copy of this vector, divided by the given \a divisor.
\sa operator/=()
*/
Vector4 Vector4::operator/(areal divisor) const {
return Vector4(x / divisor, y / divisor, z / divisor, w / divisor);
}
/*!
Returns a Vector4 object that is the sum of the this vector and \a vector; each component is added separately.
\sa operator+=()
*/
Vector4 Vector4::operator+(const Vector4 &vector) const {
return Vector4(x + vector.x, y + vector.y, z + vector.z, w + vector.w);
}
/*!
Returns a Vector4 object that is formed by changing the sign of
all three components of the this vector.
Equivalent to \c {Vector4(0,0,0,1) - vector}.
*/
Vector4 Vector4::operator-() const {
return Vector4(-x, -y, -z, -w);
}
/*!
Returns a Vector4 object that is formed by subtracting \a vector from this vector;
each component is subtracted separately.
\sa operator-=()
*/
Vector4 Vector4::operator-(const Vector4 &vector) const {
return Vector4(x - vector.x, y - vector.y, z - vector.z, z - vector.w);
}
/*!
Multiplies this vector's coordinates by the given \a factor, and
returns a reference to this vector.
\sa operator/=()
*/
Vector4 &Vector4::operator*=(areal factor) {
return *this = *this * factor;
}
/*!
Divides this vector's coordinates by the given \a divisor, and
returns a reference to this vector.
\sa operator*=()
*/
Vector4 &Vector4::operator/=(areal divisor) {
return *this = *this / divisor;
}
/*!
Adds the given \a vector to this vector and returns a reference to
this vector.
\sa operator-=()
*/
Vector4 &Vector4::operator+=(const Vector4 &vector) {
return *this = *this + vector;
}
/*!
Subtracts the given \a vector from this vector and returns a reference to
this vector.
\sa operator+=()
*/
Vector4 &Vector4::operator-=(const Vector4 &vector) {
return *this = *this - vector;
}
/*!
Returns the component of the vector at index position i as a modifiable reference.
\a i must be a valid index position in the vector (i.e., 0 <= i < 4).
*/
areal &Vector4::operator[](int i) {
return v[i];
}
/*!
Returns the component of the vector at index position.
\a i must be a valid index position in the vector (i.e., 0 <= i < 4).
*/
areal Vector4::operator[](int i) const {
return v[i];
}
/*!
Returns the length of this vector.
\sa sqrLength()
*/
areal Vector4::length() const {
return (areal)sqrt(sqrLength());
}
/*!
Returns the squared length of this vector.
\sa length()
*/
areal Vector4::sqrLength() const {
return x * x + y * y + z * z + w * w;
}
/*!
Normalizes the currect vector in place.
Returns length of prenormalized vector.
\sa length()
*/
areal Vector4::normalize() {
areal len = length();
if(len == 0.0f) {
return 0.0f;
}
(*this) *= (1.0f / len);
return len;
}
/*!
Returns the dot-product of this vector and given \a vector.
*/
areal Vector4::dot(const Vector4 &vector) const {
return x * vector.x + y * vector.y + z * vector.z + w * vector.w;
}