forked from WWU-cptr143-sprouts/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
423 lines (356 loc) · 9.42 KB
/
vector.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "util.h"
#ifndef VECTOR_H
#define VECTOR_H
#include <cmath>
#include <stdexcept>
#include <random>
#include <ostream>
using namespace std;
struct VectorF;
struct VectorI //integer vector
{
int x, y, z;
constexpr VectorI(int x, int y, int z)
: x(x), y(y), z(z)
{
}
private:
VectorI(float x, float y, float z) = delete;
public:
constexpr VectorI(int v = 0)
: x(v), y(v), z(v)
{
}
constexpr const VectorI operator -() const //switching sign
{
return VectorI(-x, -y, -z);
}
constexpr const VectorI & operator +() const
{
return *this;
}
constexpr const VectorI operator +(const VectorI & r) const //addition
{
return VectorI(x + r.x, y + r.y, z + r.z);
}
constexpr const VectorI operator -(const VectorI & r) const //subtraction
{
return VectorI(x - r.x, y - r.y, z - r.z);
}
constexpr const VectorI operator *(const VectorI & r) const //multiplication
{
return VectorI(x * r.x, y * r.y, z * r.z);
}
constexpr const VectorI operator *(int r) const//scalar multiplication
{
return VectorI(x * r, y * r, z * r);
}
friend constexpr VectorI operator *(int a, const VectorI & b) //multiplication
{
return VectorI(a * b.x, a * b.y, a * b.z);
}
constexpr bool operator ==(const VectorI & r) const //comparison
{
return x == r.x && y == r.y && z == r.z;
}
constexpr bool operator !=(const VectorI & r) const //inequality
{
return x != r.x || y != r.y || z != r.z;
}
const VectorI & operator +=(const VectorI & r) //adding to itself
{
x += r.x;
y += r.y;
z += r.z;
return *this;
}
const VectorI & operator -=(const VectorI & r)//subtracting to itself
{
x -= r.x;
y -= r.y;
z -= r.z;
return *this;
}
const VectorI & operator *=(const VectorI & r)//multiplying to itself
{
x *= r.x;
y *= r.y;
z *= r.z;
return *this;
}
const VectorI & operator *=(int r) //multiplying by itself
{
x *= r;
y *= r;
z *= r;
return *this;
}
friend constexpr int dot(const VectorI & a, const VectorI & b)//dot product
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
friend constexpr int absSquared(const VectorI & v)//length squared
{
return dot(v, v);
}
friend float abs(const VectorI & v)//length
{
return sqrt(absSquared(v));
}
friend constexpr VectorI cross(const VectorI & a, const VectorI & b)//cross product
{
return VectorI(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
friend ostream & operator <<(ostream & os, const VectorI & v)//prints vector
{
return os << "<" << v.x << ", " << v.y << ", " << v.z << ">";
}
};
struct VectorF //floating point vector
{
float x, y, z;
constexpr VectorF(float x, float y, float z)//constructor
: x(x), y(y), z(z)
{
}
constexpr VectorF(float v = 0)//constructor for the origin
: x(v), y(v), z(v)
{
}
constexpr VectorF(const VectorI & v)//converts integer vector to float vector
: x(v.x), y(v.y), z(v.z)
{
}
explicit operator VectorI() const //sets vector to lowest it can be
{
return VectorI(ifloor(x), ifloor(y), ifloor(z));
}
constexpr const VectorF operator -() const//negates a vector
{
return VectorF(-x, -y, -z);
}
constexpr const VectorF & operator +() const//leaves a vector positive
{
return *this;
}
constexpr const VectorF operator +(const VectorF & r) const//adds two vectors
{
return VectorF(x + r.x, y + r.y, z + r.z);
}
friend constexpr VectorF operator +(const VectorF & l, const VectorI & r)//adds int and float vectors
{
return l + (VectorF)r;
}
friend constexpr VectorF operator +(const VectorI & l, const VectorF & r)//adds int and float vectors
{
return (VectorF)l + r;
}
constexpr const VectorF operator -(const VectorF & r) const
{
return VectorF(x - r.x, y - r.y, z - r.z);
}
friend constexpr VectorF operator -(const VectorF & l, const VectorI & r)
{
return l - (VectorF)r;
}
friend constexpr VectorF operator -(const VectorI & l, const VectorF & r)
{
return (VectorF)l - r;
}
constexpr const VectorF operator *(const VectorF & r) const
{
return VectorF(x * r.x, y * r.y, z * r.z);
}
friend constexpr VectorF operator *(const VectorF & l, const VectorI & r)
{
return l * (VectorF)r;
}
friend constexpr VectorF operator *(const VectorI & l, const VectorF & r)
{
return (VectorF)l * r;
}
constexpr const VectorF operator /(const VectorF & r) const
{
return VectorF(x / r.x, y / r.y, z / r.z);
}
constexpr const VectorF operator *(float r) const
{
return VectorF(x * r, y * r, z * r);
}
friend constexpr VectorF operator *(float a, const VectorF & b)
{
return VectorF(a * b.x, a * b.y, a * b.z);
}
constexpr const VectorF operator /(float r) const
{
return VectorF(x / r, y / r, z / r);
}
constexpr bool operator ==(const VectorF & r) const
{
return x == r.x && y == r.y && z == r.z;
}
constexpr bool operator !=(const VectorF & r) const
{
return x != r.x || y != r.y || z != r.z;
}
const VectorF & operator +=(const VectorF & r)
{
x += r.x;
y += r.y;
z += r.z;
return *this;
}
const VectorF & operator -=(const VectorF & r)
{
x -= r.x;
y -= r.y;
z -= r.z;
return *this;
}
const VectorF & operator *=(const VectorF & r)
{
x *= r.x;
y *= r.y;
z *= r.z;
return *this;
}
const VectorF & operator /=(const VectorF & r)
{
x /= r.x;
y /= r.y;
z /= r.z;
return *this;
}
const VectorF & operator *=(float r)
{
x *= r;
y *= r;
z *= r;
return *this;
}
const VectorF & operator /=(float r)
{
x /= r;
y /= r;
z /= r;
return *this;
}
friend constexpr float dot(const VectorF & a, const VectorF & b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
friend constexpr float dot(const VectorI & a, const VectorF & b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
friend constexpr float dot(const VectorF & a, const VectorI & b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
friend constexpr float absSquared(const VectorF & v)
{
return dot(v, v);
}
friend float abs(const VectorF & v)
{
return sqrt(absSquared(v));
}
friend const VectorF normalizeNoThrow(const VectorF & v)
{
float r = abs(v);
if(r == 0)
{
r = 1;
}
return v / r;
}
friend const VectorF normalize(const VectorF v)
{
float r = abs(v);
if(v == 0)
{
throw domain_error("can't normalize <0, 0, 0>");
}
return v / r;
}
const VectorF static normalize(float x, float y, float z)
{
VectorF v(x, y, z);
float r = abs(v);
if(v == 0)
{
throw domain_error("can't normalize <0, 0, 0>");
}
return v / r;
}
float phi() const
{
float r = abs(*this);
if(r == 0)
{
return 0;
}
float v = y / r;
v = limit(v, -1.0f, 1.0f);
return asin(v);
}
float theta() const
{
return atan2(x, z);
}
float rSpherical() const
{
return abs(*this);
}
friend constexpr VectorF cross(const VectorF & a, const VectorF & b)
{
return VectorF(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
template<typename RE>
static VectorF random(RE & re)
{
VectorF retval;
do
{
retval.x = uniform_real_distribution<float>(-1, 1)(re);
retval.y = uniform_real_distribution<float>(-1, 1)(re);
retval.z = uniform_real_distribution<float>(-1, 1)(re);
}
while(absSquared(retval) > 1 || absSquared(retval) < eps);
return retval;
}
friend ostream & operator <<(ostream & os, const VectorF & v)
{
return os << "<" << v.x << ", " << v.y << ", " << v.z << ">";
}
};
constexpr inline bool operator ==(const VectorF & a, const VectorI & b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
constexpr inline bool operator ==(const VectorI & a, const VectorF & b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
constexpr inline bool operator !=(const VectorF & a, const VectorI & b)
{
return a.x != b.x || a.y != b.y || a.z != b.z;
}
constexpr inline bool operator !=(const VectorI & a, const VectorF & b)
{
return a.x != b.x || a.y != b.y || a.z != b.z;
}
inline VectorF normalize(const VectorI & v)
{
return normalize((VectorF)v);
}
inline VectorF normalizeNoThrow(const VectorI & v)
{
return normalizeNoThrow((VectorF)v);
}
constexpr VectorF gravityVector = VectorF(0, -9.8, 0);
inline float findIntersectionPoint(VectorF a, VectorF b, VectorF normal, float d)
{
return (dot(a, normal) + d) / dot(a - b, normal);
}
#endif // VECTOR_H