-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVectorMaths4.hpp
377 lines (336 loc) · 8.95 KB
/
VectorMaths4.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
MessyBsp. BSP collision and loading example code.
Copyright (C) 2014 Richard Maxwell <[email protected]>
This file is part of MessyBsp
MessyBsp is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#pragma once
#include "Geometry.hpp"
#include <cmath>
// ///////////////////
// Operators
// ///////////////////
inline Vec4& operator+=(Vec4& lhs, const Vec4& rhs)
{
lhs.data[0] += rhs.data[0];
lhs.data[1] += rhs.data[1];
lhs.data[2] += rhs.data[2];
lhs.data[3] += rhs.data[3];
return lhs;
}
inline Vec4& operator-=(Vec4& lhs, const Vec4& rhs)
{
lhs.data[0] -= rhs.data[0];
lhs.data[1] -= rhs.data[1];
lhs.data[2] -= rhs.data[2];
lhs.data[3] -= rhs.data[3];
return lhs;
}
inline Vec4& operator*=(Vec4& lhs, const Vec4& rhs)
{
lhs.data[0] *= rhs.data[0];
lhs.data[1] *= rhs.data[1];
lhs.data[2] *= rhs.data[2];
lhs.data[3] *= rhs.data[3];
return lhs;
}
inline Vec4& operator/=(Vec4& lhs, const Vec4& rhs)
{
lhs.data[0] /= rhs.data[0];
lhs.data[1] /= rhs.data[1];
lhs.data[2] /= rhs.data[2];
lhs.data[3] /= rhs.data[3];
return lhs;
}
inline constexpr Vec4 operator-(const Vec4& lhs)
{
return Vec4
{
-lhs.data[0],
-lhs.data[1],
-lhs.data[2],
-lhs.data[3],
};
}
inline msvc_constexpr Vec4 operator+(const Vec4& lhs, const Vec4& rhs)
{
return
{
lhs.data[0] + rhs.data[0],
lhs.data[1] + rhs.data[1],
lhs.data[2] + rhs.data[2],
lhs.data[3] + rhs.data[3],
};
}
inline msvc_constexpr Vec4 operator-(const Vec4& lhs, const Vec4& rhs)
{
return
{
lhs.data[0] - rhs.data[0],
lhs.data[1] - rhs.data[1],
lhs.data[2] - rhs.data[2],
lhs.data[3] - rhs.data[3],
};
}
inline msvc_constexpr Vec4 operator*(const Vec4& lhs, const Vec4& rhs)
{
return
{
lhs.data[0] * rhs.data[0],
lhs.data[1] * rhs.data[1],
lhs.data[2] * rhs.data[2],
lhs.data[3] * rhs.data[3],
};
}
inline msvc_constexpr Vec4 operator/(const Vec4& lhs, const Vec4& rhs)
{
return
{
lhs.data[0] / rhs.data[0],
lhs.data[1] / rhs.data[1],
lhs.data[2] / rhs.data[2],
lhs.data[3] / rhs.data[3],
};
}
inline Vec4& operator+=(Vec4& lhs, float rhs)
{
lhs.data[0] += rhs;
lhs.data[1] += rhs;
lhs.data[2] += rhs;
lhs.data[3] += rhs;
return lhs;
}
inline Vec4& operator-=(Vec4& lhs, float rhs)
{
lhs.data[0] -= rhs;
lhs.data[1] -= rhs;
lhs.data[2] -= rhs;
lhs.data[3] -= rhs;
return lhs;
}
inline Vec4& operator*=(Vec4& lhs, float rhs)
{
lhs.data[0] *= rhs;
lhs.data[1] *= rhs;
lhs.data[2] *= rhs;
lhs.data[3] *= rhs;
return lhs;
}
inline Vec4& operator/=(Vec4& lhs, float rhs)
{
return lhs *= 1.0f / rhs;
}
inline msvc_constexpr Vec4 operator+(const Vec4& lhs, float rhs)
{
return
{
lhs.data[0] + rhs,
lhs.data[1] + rhs,
lhs.data[2] + rhs,
lhs.data[3] + rhs,
};
}
inline msvc_constexpr Vec4 operator-(const Vec4& lhs, float rhs)
{
return
{
lhs.data[0] - rhs,
lhs.data[1] - rhs,
lhs.data[2] - rhs,
lhs.data[3] - rhs,
};
}
inline msvc_constexpr Vec4 operator*(const Vec4& lhs, float rhs)
{
return
{
lhs.data[0] * rhs,
lhs.data[1] * rhs,
lhs.data[2] * rhs,
lhs.data[3] * rhs,
};
}
inline msvc_constexpr Vec4 operator/(const Vec4& lhs, float rhs)
{
return
{
lhs.data[0] / rhs,
lhs.data[1] / rhs,
lhs.data[2] / rhs,
lhs.data[3] / rhs,
};
}
// ///////////////////
// Vector Return Maths
// ///////////////////
inline msvc_constexpr Vec4 Sqrt(const Vec4& lhs)
{
return Vec4
{
std::sqrt(lhs.data[0]),
std::sqrt(lhs.data[1]),
std::sqrt(lhs.data[2]),
std::sqrt(lhs.data[3])
};
}
/// If it uses the SIMD invsqrt, then it will be less precision
/// than explicitly doing 1.0f/Sqrt(lhs)
inline msvc_constexpr Vec4 InvSqrt(const Vec4& lhs)
{
return
{
1.0f / std::sqrt(lhs.data[0]),
1.0f / std::sqrt(lhs.data[1]),
1.0f / std::sqrt(lhs.data[2]),
1.0f / std::sqrt(lhs.data[3])
};
}
inline msvc_constexpr Vec4 Absolute(const Vec4& lhs)
{
return Vec4
{
std::fabs(lhs.data[0]),
std::fabs(lhs.data[1]),
std::fabs(lhs.data[2]),
std::fabs(lhs.data[3])
};
}
inline Vec4 Dot(const Vec4& lhs, const Vec4& rhs)
{
// If this compiler is too dumb to do a decent DOT4, then do this instead:
/*
// http://www.gamedev.net/topic/617959-c-dot-product-vs-sse-dot-product/
//__m128 m = _mm_mul_ps(v1, v2);
//__m128 t = _mm_add_ps(m, _mm_shuffle_ps(m, m, _MM_SHUFFLE(2, 3, 0, 1)));
//__m128 result = _mm_add_ps(t, _mm_shuffle_ps(t, t, _MM_SHUFFLE(1, 0, 3, 2)));
auto multiply = lhs * rhs;
auto shuffle1 = Vec4
{
multiply.data[1],
multiply.data[0],
multiply.data[3],
multiply.data[2],
};
// x = x + y
// y = y + x
// z = z + w
// w = w + z
auto first = multiply + shuffle1;
auto shuffle2 = Vec4
{
first.data[2],
first.data[3],
first.data[0],
first.data[1],
};
// x = x + y + (z + w)
// y = y + x + (w + z)
// z = z + w + (x + y)
// w = w + z + (y + x)
return first + shuffle2;
*/
// hope the compiler picks up on this pattern and recognises it as a Dot.
auto mult = lhs * rhs;
return Vec4
{
mult.data[0] + mult.data[1] + mult.data[2] + mult.data[3],
mult.data[0] + mult.data[1] + mult.data[2] + mult.data[3],
mult.data[0] + mult.data[1] + mult.data[2] + mult.data[3],
mult.data[0] + mult.data[1] + mult.data[2] + mult.data[3],
};
}
// Cross product doesn't exist for Vec4, only Vector3 and Vector7.
inline Vec4 Square(const Vec4& lhs)
{
return Dot(lhs, lhs);
}
inline Vec4 Magnitude(const Vec4& lhs)
{
return Sqrt(Square(lhs));
}
inline Vec4N Normalise(const Vec4& lhs)
{
auto length = Magnitude(lhs);
if (length.data[0] > 0.0f)
{
auto norm = lhs / length;
return
{
norm.data[0],
norm.data[1],
norm.data[2],
norm.data[3],
};
}
// Put an assert here, and stuff will start going
// wrong as vector is too small. But for now,
// just assume the vector is normalised in the
// x direction.
Vec4N result;
result.data[0] = 1.0f;
return result;
}
inline constexpr Vec4 Lerp(const Vec4& lhs, const Vec4& rhs, float scale)
{
return Vec4
{
lhs.data[0] + (rhs.data[0] - lhs.data[0]) * scale,
lhs.data[1] + (rhs.data[1] - lhs.data[1]) * scale,
lhs.data[2] + (rhs.data[2] - lhs.data[2]) * scale,
lhs.data[3] + (rhs.data[3] - lhs.data[3]) * scale
};
}
inline constexpr Vec4 Max(const Vec4& lhs, const Vec4& rhs)
{
return Vec4
{
lhs.data[0] > rhs.data[0] ? lhs.data[0] : rhs.data[0],
lhs.data[1] > rhs.data[1] ? lhs.data[1] : rhs.data[1],
lhs.data[2] > rhs.data[2] ? lhs.data[2] : rhs.data[2],
lhs.data[3] > rhs.data[3] ? lhs.data[3] : rhs.data[3]
};
}
inline constexpr Vec4 Min(const Vec4& lhs, const Vec4& rhs)
{
return Vec4
{
lhs.data[0] < rhs.data[0] ? lhs.data[0] : rhs.data[0],
lhs.data[1] < rhs.data[1] ? lhs.data[1] : rhs.data[1],
lhs.data[2] < rhs.data[2] ? lhs.data[2] : rhs.data[2],
lhs.data[3] < rhs.data[3] ? lhs.data[3] : rhs.data[3]
};
}
inline msvc_constexpr Vec4 Clamp(const Vec4& lhs, float min = 0.0f, float max = 1.0f)
{
return Max(Min({max}, lhs),{min});
}
// ///////////////////
// Scalar Return Maths
// ///////////////////
// Avoid these as they convert from Vectors to floats which
// apparently is a performance penalty, especially if you then
// use the value in more vector calculations.
// http://www.gamasutra.com/view/feature/132636/designing_fast_crossplatform_simd_.php?print=1
inline constexpr float DotF(const Vec4& lhs, const Vec4& rhs)
{
return
(lhs.data[0] * rhs.data[0]) +
(lhs.data[1] * rhs.data[1]) +
(lhs.data[2] * rhs.data[2]) +
(lhs.data[3] * rhs.data[3]);
}
/// Returns the area of the square formed with one corner
/// at origin and the other at the point lhs.
inline constexpr float SquareF(const Vec4& lhs)
{
return DotF(lhs, lhs);
}