summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/VectorN.h
blob: 6e07a3b49dbf16b001cfdc5c3fbfd124d79ce725 (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
/*
 * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/Math.h>
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <AK/StringView.h>

#define LOOP_UNROLL_N 4

#define STRINGIFY_HELPER(x) #x
#define STRINGIFY(x) STRINGIFY_HELPER(x)

#ifdef __clang__
#    define UNROLL_LOOP _Pragma(STRINGIFY(unroll))
#else
#    define UNROLL_LOOP _Pragma(STRINGIFY(GCC unroll(LOOP_UNROLL_N)))
#endif

namespace Gfx {
template<size_t N, typename T>
requires(N >= 2 && N <= 4) class VectorN final {
    static_assert(LOOP_UNROLL_N >= N, "Unroll the entire loop for performance.");

public:
    [[nodiscard]] constexpr VectorN() = default;
    [[nodiscard]] constexpr VectorN(T x, T y) requires(N == 2)
        : m_data { x, y }
    {
    }
    [[nodiscard]] constexpr VectorN(T x, T y, T z) requires(N == 3)
        : m_data { x, y, z }
    {
    }
    [[nodiscard]] constexpr VectorN(T x, T y, T z, T w) requires(N == 4)
        : m_data { x, y, z, w }
    {
    }

    [[nodiscard]] constexpr T x() const { return m_data[0]; }
    [[nodiscard]] constexpr T y() const { return m_data[1]; }
    [[nodiscard]] constexpr T z() const requires(N >= 3) { return m_data[2]; }
    [[nodiscard]] constexpr T w() const requires(N >= 4) { return m_data[3]; }

    constexpr void set_x(T value) { m_data[0] = value; }
    constexpr void set_y(T value) { m_data[1] = value; }
    constexpr void set_z(T value) requires(N >= 3) { m_data[2] = value; }
    constexpr void set_w(T value) requires(N >= 4) { m_data[3] = value; }

    [[nodiscard]] constexpr T operator[](size_t index) const
    {
        VERIFY(index < N);
        return m_data[index];
    }

    constexpr VectorN& operator+=(VectorN const& other)
    {
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            m_data[i] += other.m_data[i];
        return *this;
    }

    constexpr VectorN& operator-=(VectorN const& other)
    {
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            m_data[i] -= other.m_data[i];
        return *this;
    }

    constexpr VectorN& operator*=(const T& t)
    {
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            m_data[i] *= t;
        return *this;
    }

    [[nodiscard]] constexpr VectorN operator+(VectorN const& other) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] + other.m_data[i];
        return result;
    }

    [[nodiscard]] constexpr VectorN operator-(VectorN const& other) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] - other.m_data[i];
        return result;
    }

    [[nodiscard]] constexpr VectorN operator*(VectorN const& other) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] * other.m_data[i];
        return result;
    }

    [[nodiscard]] constexpr VectorN operator-() const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = -m_data[i];
        return result;
    }

    [[nodiscard]] constexpr VectorN operator/(VectorN const& other) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] / other.m_data[i];
        return result;
    }

    template<typename U>
    [[nodiscard]] constexpr VectorN operator*(U f) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] * f;
        return result;
    }

    template<typename U>
    [[nodiscard]] constexpr VectorN operator/(U f) const
    {
        VectorN result;
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result.m_data[i] = m_data[i] / f;
        return result;
    }

    [[nodiscard]] constexpr T dot(VectorN const& other) const
    {
        T result {};
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            result += m_data[i] * other.m_data[i];
        return result;
    }

    [[nodiscard]] constexpr VectorN cross(VectorN const& other) const requires(N == 3)
    {
        return VectorN(
            y() * other.z() - z() * other.y(),
            z() * other.x() - x() * other.z(),
            x() * other.y() - y() * other.x());
    }

    [[nodiscard]] constexpr VectorN normalized() const
    {
        VectorN copy { *this };
        copy.normalize();
        return copy;
    }

    [[nodiscard]] constexpr VectorN clamped(T m, T x) const
    {
        VectorN copy { *this };
        copy.clamp(m, x);
        return copy;
    }

    constexpr void clamp(T min_value, T max_value)
    {
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i) {
            m_data[i] = max(min_value, m_data[i]);
            m_data[i] = min(max_value, m_data[i]);
        }
    }

    constexpr void normalize()
    {
        T const inv_length = 1 / length();
        operator*=(inv_length);
    }

    [[nodiscard]] constexpr T length() const
    {
        T squared_sum {};
        UNROLL_LOOP
        for (auto i = 0u; i < N; ++i)
            squared_sum += m_data[i] * m_data[i];
        return AK::sqrt(squared_sum);
    }

    [[nodiscard]] constexpr VectorN<2, T> xy() const requires(N >= 3)
    {
        return VectorN<2, T>(x(), y());
    }

    [[nodiscard]] constexpr VectorN<3, T> xyz() const requires(N >= 4)
    {
        return VectorN<3, T>(x(), y(), z());
    }

    [[nodiscard]] String to_string() const
    {
        if constexpr (N == 2)
            return String::formatted("[{},{}]", x(), y());
        else if constexpr (N == 3)
            return String::formatted("[{},{},{}]", x(), y(), z());
        else
            return String::formatted("[{},{},{},{}]", x(), y(), z(), w());
    }

private:
    AK::Array<T, N> m_data;
};
}