aboutsummaryrefslogtreecommitdiff
path: root/src/math/vector.hpp
blob: def469b9594794b06f272ac20275375a4d31808a (plain) (blame)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#ifndef MATH_VECTOR_HPP
#define MATH_VECTOR_HPP

#include <cmath>

namespace engine::math {

namespace vector_coords {

enum class VectorCoord { x, y, z, w };

template<VectorCoord id1, VectorCoord id2>
struct transpose { template<VectorCoord input> static constexpr VectorCoord id(); };

template<> struct transpose<VectorCoord::x, VectorCoord::x> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::x, VectorCoord::y> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::x, VectorCoord::z> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::x, VectorCoord::w> { template<VectorCoord input> static constexpr VectorCoord id(); };

template<> struct transpose<VectorCoord::y, VectorCoord::x> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::y, VectorCoord::y> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::y, VectorCoord::z> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::y, VectorCoord::w> { template<VectorCoord input> static constexpr VectorCoord id(); };

template<> struct transpose<VectorCoord::z, VectorCoord::x> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::z, VectorCoord::y> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::z, VectorCoord::z> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::z, VectorCoord::w> { template<VectorCoord input> static constexpr VectorCoord id(); };

template<> struct transpose<VectorCoord::w, VectorCoord::x> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::w, VectorCoord::y> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::w, VectorCoord::z> { template<VectorCoord input> static constexpr VectorCoord id(); };
template<> struct transpose<VectorCoord::w, VectorCoord::w> { template<VectorCoord input> static constexpr VectorCoord id(); };

template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::x>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::x>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::x>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::x>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::y>::id<VectorCoord::x>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::y>::id<VectorCoord::y>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::y>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::y>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::z>::id<VectorCoord::x>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::z>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::z>::id<VectorCoord::z>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::z>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::w>::id<VectorCoord::x>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::w>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::w>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::x, VectorCoord::w>::id<VectorCoord::w>() { return VectorCoord::x; }

template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::x>::id<VectorCoord::x>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::x>::id<VectorCoord::y>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::x>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::x>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::y>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::y>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::y>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::y>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::z>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::z>::id<VectorCoord::y>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::z>::id<VectorCoord::z>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::z>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::w>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::w>::id<VectorCoord::y>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::w>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::y, VectorCoord::w>::id<VectorCoord::w>() { return VectorCoord::y; }

template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::x>::id<VectorCoord::x>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::x>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::x>::id<VectorCoord::z>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::x>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::y>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::y>::id<VectorCoord::y>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::y>::id<VectorCoord::z>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::y>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::z>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::z>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::z>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::z>::id<VectorCoord::w>() { return VectorCoord::w; }

template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::w>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::w>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::w>::id<VectorCoord::z>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::z, VectorCoord::w>::id<VectorCoord::w>() { return VectorCoord::z; }

template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::x>::id<VectorCoord::x>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::x>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::x>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::x>::id<VectorCoord::w>() { return VectorCoord::x; }

template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::y>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::y>::id<VectorCoord::y>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::y>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::y>::id<VectorCoord::w>() { return VectorCoord::y; }

template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::z>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::z>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::z>::id<VectorCoord::z>() { return VectorCoord::w; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::z>::id<VectorCoord::w>() { return VectorCoord::z; }

template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::w>::id<VectorCoord::x>() { return VectorCoord::x; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::w>::id<VectorCoord::y>() { return VectorCoord::y; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::w>::id<VectorCoord::z>() { return VectorCoord::z; }
template<> constexpr VectorCoord transpose<VectorCoord::w, VectorCoord::w>::id<VectorCoord::w>() { return VectorCoord::w; }

}

struct Vector2;
struct Vector3;
struct Vector4;

template<typename T>
concept VectorTypeConcept = std::is_same_v<T, Vector2> || std::is_same_v<T, Vector3> || std::is_same_v<T, Vector4>;

constexpr Vector2 operator*(float n, const Vector2& other);
constexpr Vector2 operator/(const Vector2& other, float n);

struct Vector2 {
    static constexpr size_t size = 2;

    static constexpr Vector2 bilerp(const Vector2& v1, const Vector2& v2, const Vector2& v3, float b0, float b1) {
        return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3;
    }

    float x, y;

    constexpr Vector2() {}
    constexpr Vector2(float x, float y) : x { x }, y { y } {}

    template<vector_coords::VectorCoord id>
    constexpr float& v() &
    requires (id == vector_coords::VectorCoord::x || id == vector_coords::VectorCoord::y)
    {
        if constexpr (id == vector_coords::VectorCoord::x) return x;
        else                                               return y;
    }

    template<vector_coords::VectorCoord id>
    constexpr const float& v() const &
    requires (id == vector_coords::VectorCoord::x || id == vector_coords::VectorCoord::y)
    {
        if constexpr (id == vector_coords::VectorCoord::x) return x;
        else                                               return y;
    }

    constexpr bool operator==(const Vector2& other) const & {
        return x == other.x && y == other.y;
    }

    constexpr bool operator!=(const Vector2& other) const & {
        return !(*this == other);
    }

    constexpr Vector2 operator+() const & {
        return *this;
    }

    constexpr Vector2 operator-() const & {
        return { -x, -y };
    }

    constexpr Vector2 operator+(const Vector2& other) const & {
        return { x + other.x, y + other.y };
    }

    constexpr Vector2 operator-(const Vector2& other) const & {
        return *this + (-other);
    }

    constexpr Vector2 operator+=(const Vector2& other) & {
        x += other.x;
        y += other.y;
        return *this;
    }

    constexpr float det(const Vector2& other) const & {
        return this->x * other.y - other.x * this->y;
    }

    constexpr Vector2 round() const & {
        return { std::round(x), std::round(y) };
    }

    constexpr Vector2 mul_term(const Vector2& other) const & {
        return { x * other.x, y * other.y };
    }

    constexpr Vector2 map(const Vector2& from1, const Vector2& from2, const Vector2& to1, const Vector2& to2) {
        return {
            to1.x + (x - from1.x) * (to2.x - to1.x) / (from2.x - from1.x),
            to1.y + (y - from1.y) * (to2.y - to1.y) / (from2.y - from1.y),
        };
    }
};

constexpr Vector2 operator*(float n, const Vector2& other) {
    return { n * other.x, n * other.y };
}

constexpr Vector2 operator*(const Vector2& other, float n) {
    return n * other;
}

constexpr Vector2 operator/(const Vector2& other, float n) {
    return { other.x / n, other.y / n };
}

constexpr Vector2 operator+(const Vector2& other, float n) {
    return { other.x + n, other.y + n };
}

constexpr Vector2 operator-(const Vector2& other, float n) {
    return { other.x - n, other.y - n };
}

constexpr Vector3 operator*(float n, const Vector3& other);
constexpr Vector3 operator/(const Vector3& other, float n);

struct Vector3 {
    static constexpr size_t size = 3;

    static constexpr Vector3 bilerp(const Vector3& v1, const Vector3& v2, const Vector3& v3, float b0, float b1) {
        return b0 * v1 + b1 * v2 + (1.f - b0 - b1) * v3;
    }

    float x, y, z;

    constexpr Vector3() {}
    constexpr Vector3(float x, float y, float z) : x { x }, y { y }, z { z } {}
    constexpr Vector3(const Vector2& v, float z) : x { v.x }, y { v.y }, z { z } {}

    template<vector_coords::VectorCoord id>
    constexpr float& v() &
    requires (id == vector_coords::VectorCoord::x || id == vector_coords::VectorCoord::y || id == vector_coords::VectorCoord::z)
    {
        if constexpr      (id == vector_coords::VectorCoord::x) return x;
        else if constexpr (id == vector_coords::VectorCoord::y) return y;
        else                                                    return z;
    }

    template<vector_coords::VectorCoord id>
    constexpr const float& v() const &
    requires (id == vector_coords::VectorCoord::x || id == vector_coords::VectorCoord::y || id == vector_coords::VectorCoord::z)
    {
        if constexpr      (id == vector_coords::VectorCoord::x) return x;
        else if constexpr (id == vector_coords::VectorCoord::y) return y;
        else                                                    return z;
    }

    constexpr bool operator==(const Vector3& other) const & {
        return x == other.x && y == other.y && z == other.z;
    }

    constexpr bool operator!=(const Vector3& other) const & {
        return !(*this == other);
    }

    constexpr Vector3 operator+() const & {
        return *this;
    }

    constexpr Vector3 operator-() const & {
        return { -x, -y, -z };
    }

    constexpr Vector3 operator+(const Vector3& other) const & {
        return { x + other.x, y + other.y, z + other.z };
    }

    constexpr Vector3 operator-(const Vector3& other) const & {
        return *this + (-other);
    }

    constexpr Vector3 operator+=(const Vector3& other) & {
        x += other.x;
        y += other.y;
        z += other.z;
        return *this;
    }

    constexpr Vector2 xy() const & {
        return { x, y };
    }

    constexpr Vector3 round() {
        return { std::round(x), std::round(y), std::round(z) };
    }

    constexpr Vector3 cross(const Vector3& other) const & {
        return {
            y * other.z - z * other.y,
            z * other.x - x * other.z,
            x * other.y - y * other.x
        };
    }

    constexpr float dot(const Vector3& other) const & {
        return x * other.x + y * other.y + z * other.z;
    }

    constexpr float length_squared() const & {
        return dot(*this);
    }

    constexpr float length() const & {
        return std::sqrt(length_squared());
    }

    constexpr Vector3 normalize() const & {
        return *this / length();
    }

    constexpr Vector3 map(const Vector3& from1, const Vector3& from2, const Vector3& to1, const Vector3& to2) {
        return {
            to1.x + (x - from1.x) * (to2.x - to1.x) / (from2.x - from1.x),
            to1.y + (y - from1.y) * (to2.y - to1.y) / (from2.y - from1.y),
            to1.z + (z - from1.z) * (to2.z - to1.z) / (from2.z - from1.z),
        };
    }
};

constexpr Vector3 operator*(float n, const Vector3& other) {
    return { n * other.x, n * other.y, n * other.z };
}

constexpr Vector3 operator*(const Vector3& other, float n) {
    return n * other;
}

constexpr Vector3 operator/(float n, const Vector3& other) {
    return { n / other.x, n / other.y, n / other.z };
}

constexpr Vector3 operator/(const Vector3& other, float n) {
    return { other.x / n, other.y / n, other.z / n };
}

struct Vector4 {
    static constexpr size_t size = 4;

    float x, y, z, w;

    template<vector_coords::VectorCoord id>
    constexpr float& v() & {
        if constexpr      (id == vector_coords::VectorCoord::x) return x;
        else if constexpr (id == vector_coords::VectorCoord::y) return y;
        else if constexpr (id == vector_coords::VectorCoord::z) return z;
        else                                                    return w;
    }

    template<vector_coords::VectorCoord id>
    constexpr const float& v() const & {
        if constexpr      (id == vector_coords::VectorCoord::x) return x;
        else if constexpr (id == vector_coords::VectorCoord::y) return y;
        else if constexpr (id == vector_coords::VectorCoord::z) return z;
        else                                                    return w;
    }

    constexpr Vector4() {}
    constexpr Vector4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {}
    constexpr Vector4(const Vector2& v, float z, float w) : x{v.x}, y{v.y}, z{z}, w{w} {}
    constexpr Vector4(const Vector3& v, float w) : x{v.x}, y{v.y}, z{v.z}, w{w} {}

    constexpr bool operator==(const Vector4& other) const & {
        return x == other.x && y == other.y && z == other.z && w == other.w;
    }

    constexpr bool operator!=(const Vector4& other) const & {
        return !(*this == other);
    }

    constexpr Vector4 operator+() const & {
        return *this;
    }

    constexpr Vector4 operator-() const & {
        return { -x, -y, -z, -w };
    }

    constexpr Vector4 operator+(const Vector4& other) const & {
        return { x + other.x, y + other.y, z + other.z, w + other.w };
    }

    constexpr Vector4 operator-(const Vector4& other) const & {
        return *this + (-other);
    }

    constexpr Vector4 round() const & {
        return { std::round(x), std::round(y), std::round(z), std::round(w) };
    }

    constexpr Vector2 xy() const & {
        return { x, y };
    }

    constexpr Vector3 xyz() const & {
        return { x, y, z };
    }

    constexpr Vector3 div_by_w() const & {
        return { x / w, y / w, w };
    }

    constexpr Vector4 map(const Vector4& from1, const Vector4& from2, const Vector4& to1, const Vector4& to2) {
        return {
            to1.x + (x - from1.x) * (to2.x - to1.x) / (from2.x - from1.x),
            to1.y + (y - from1.y) * (to2.y - to1.y) / (from2.y - from1.y),
            to1.z + (z - from1.z) * (to2.z - to1.z) / (from2.z - from1.z),
            to1.w + (w - from1.w) * (to2.w - to1.w) / (from2.w - from1.w),
        };
    }
};

constexpr Vector4 operator*(float n, const Vector4& other) {
    return { n * other.x, n * other.y, n * other.z, n * other.w };
}

constexpr Vector4 operator*(const Vector4& other, float n) {
    return n * other;
}

constexpr Vector4 operator/(const Vector4& other, float n) {
    return { other.x / n, other.y / n, other.z / n, other.w / n };
}

}

#endif // MATH_VECTOR_HPP