summary refs log tree commit diff
path: root/src/Math/Vector.hpp
blob: 3525e428403a375a4e8d4eaee5c306513ef129b7 (plain)
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
#pragma once

#include <sstream>
#include <cmath>
#include "../Common/Sizes.hpp"
#include "Functions.hpp"

template <uint S, typename T = Real>
struct Vector {
    Vector(): elements{} {}

    template<typename ...Args, std::enable_if_t<sizeof...(Args) == S, Int> = 0>
    Vector(Args... args) : elements{ static_cast<T>(args)... } {}

    explicit Vector(const T values[S]) {
        std::copy(values, values + S, elements);
    }

    explicit Vector(const T scalar) {
        std::fill(elements, elements + S, scalar);
    }

    Vector(const Vector<S - 1, T> vector, const T scalar) {
        std::copy(vector.elements, vector.elements + S - 1, elements);
        elements[S - 1] = scalar;
    }

    template<uint N, std::enable_if_t<(N > S), Int> = 0>
    explicit Vector(const Vector<N, T> vector) {
        std::copy(vector.elements, vector.elements + S, elements);
    }

    template<typename T2>
    explicit Vector(const Vector<S, T2> vector) {
        for (Int i = 0; i < S; i++) {
            elements[i] = (T)vector[i];
        }
    }

    template<typename F>
    Vector map(F f) const {
        Vector result{};
        for (Int i = 0; i < S; i++) {
            result[i] = f(elements[i]);
        }
        return result;
    }

    template<typename F>
    Vector zip(const Vector other, F f) const {
        Vector result{};
        for (Int i = 0; i < S; i++) {
            result[i] = f(elements[i], other[i]);
        }
        return result;
    }

    template<typename F>
    T reduce(F f) const {
        T result = elements[0];
        for (Int i = 1; i < S; i++) {
            result = f(result, elements[i]);
        }
        return result;
    }

    T sum() const {
        return reduce([](auto x, auto y) { return x + y; });
    }

    T magnitude_squared() const {
        return map([](auto x) { return x * x;}).sum();
    }

    T magnitude() const {
        return sqrt(magnitude_squared());
    }

    Vector normalize() const {
        auto m = magnitude();
        return map([=](auto x) { return x / m; });
    }

    T distance(const Vector other) const {
        return (*this - other).magnitude();
    }

    T distance_squared(const Vector other) const {
        return (*this - other).magnitude_squared();
    }

    Vector<3, T> any_orthogonal() {
        if (Vector a{y(), -x(), 0.0f}; a != zero()) return a;
        if (Vector b{z(), 0.0f, -x()}; b != zero()) return b;
        if (Vector c{0.0f, z(), -y()}; c != zero()) return c;
        return zero();
    }

    Vector abs() const {
        return map([=](auto x) { return std::abs(x); });
    }

    Vector<3, T> cross(const Vector<3, T> other) const {
        return {
            y() * other.z() - z() * other.y(),
            z() * other.x() - x() * other.z(),
            x() * other.y() - y() * other.x(),
        };
    }

    Vector<3, T> project_onto(const Vector<3, T> other) const {
        return other * (*this * other) / (other * other);
    }

    Bool mostly_equal(const Vector other, T epsilon = 0.0001f) const {
        for (Int i = 0; i < S; i++) {
            if (!Math::floats_equal(elements[i], other[i], epsilon)) {
                return false;
            }
        }
        return true;
    }

    Bool is_zero() const {
        return mostly_equal(zero());
    }

    Bool is_nan() const {
        for (UInt i = 0; i < S; i++) {
            if (std::isnan(elements[i])) {
                return true;
            }
        }
        return false;
    }

    T operator[](USize index) const {
        return elements[index];
    }

    T& operator[](USize index) {
        return elements[index];
    }

    Vector operator+(const Vector other) const {
        return zip(other, [](auto a, auto b) { return a + b; });
    }

    Vector operator+(T scalar) const {
        return map([=](auto x) { return x + scalar; });
    }

    Vector operator*(T scalar) const {
        return map([=](auto x) { return x * scalar; });
    }

    T operator*(const Vector other) const {
        return zip(other, [](auto a, auto b) { return a * b; }).sum();
    }

    Vector operator-(const Vector other) const {
        return zip(other, [](auto a, auto b) { return a - b; });
    }

    Vector operator-() const {
        return map([](T x) -> T { return -x; });
    }

    Vector operator/(T scalar) const {
        return map([=](auto x) { return x / scalar; });
    }

    Vector operator/(const Vector other) const {
        return zip(other, [](auto a, auto b) { return a / b; });
    }

    Bool operator==(const Vector& other) {
        for (Int i = 0; i < S; i++) {
            if (elements[i] != other[i]) {
                return false;
            }
        }
        return true;
    }

    Bool operator!=(const Vector& other) {
        return !this->operator==(other);
    }

    Vector& operator+=(const Vector& other) {
        *this = *this + other;
        return *this;
    }

    T& x() { static_assert(S > 0); return elements[0]; }
    const T& x() const { static_assert(S > 0); return elements[0]; }

    T& y() { static_assert(S > 1); return elements[1]; }
    const T& y() const { static_assert(S > 1); return elements[1]; }

    T& z() { static_assert(S > 2); return elements[2]; }
    const T& z() const { static_assert(S > 2); return elements[2]; }

    T& w() { static_assert(S > 3); return elements[3]; }
    const T& w() const { static_assert(S > 3); return elements[3]; }

    std::string string() const {
        std::stringstream str{};

        str << "[ ";
        for (Int i = 0; i < S; i++) {
            str << elements[i] << " ";
        }
        str << "]";

        return str.str();
    }

    static Vector<3, T> up() { return {(T)0, (T)1, (T)0}; }
    static Vector<3, T> down() { return {(T)0, (T)-1, (T)0}; }
    static Vector<3, T> forward() { return {(T)0, (T)0, (T)1}; }
    static Vector<3, T> back() { return {(T)0, (T)0, (T)-1}; }
    static Vector<3, T> right() { return {(T)1, (T)0, (T)0}; }
    static Vector<3, T> left() { return {(T)-1, (T)0, (T)0}; }

    static Vector<3, T> one() { return Vector{(T)1}; }
    static Vector<3, T> zero() { return Vector{(T)0}; }
    static Vector<3, T> max() { return Vector{(T)std::numeric_limits<T>::max()}; }

    T elements[S];
};

using Vec2 = Vector<2, Real>;
using Vec3 = Vector<3, Real>;
using Vec4 = Vector<4, Real>;