summaryrefslogtreecommitdiffstats
path: root/mvector.hpp
blob: b7347d1d35992e911310c7517ec419a7f0c6f318 (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
#ifndef MATH_VECTOR_HPP
#define MATH_VECTOR_HPP 1

#include <concepts>
#include <array>

namespace mvec
{

template<typename T, std::size_t S>
    requires(std::is_arithmetic_v<T>)
class vec
{
protected:
    std::array<T, S> v;
public:
    constexpr vec(const std::array<T, S> &vs)
        : v(vs)
    {}
    constexpr vec(T vs)
        : v(vs)
    {}

    template<std::size_t S2>
    constexpr vec &operator+=(const vec<T, S2> &rhs) {
        for(std::size_t i = 0; i < S2 && i < S; i++) v[i] += rhs[i];
        return *this;
    }

    template<std::size_t S2>
    constexpr vec &operator-=(const vec<T, S2> &rhs) {
        for(std::size_t i = 0; i < S2 && i < S; i++) v[i] -= rhs[i];
        return *this;
    }
    template<std::size_t S2>
    constexpr vec &operator*=(const vec<T, S2> &rhs) {
        for(std::size_t i = 0; i < S2 && i < S; i++) v[i] *= rhs[i];
        return *this;
    }
    template<std::size_t S2>
    constexpr vec &operator/=(const vec<T, S2> &rhs) {
        for(std::size_t i = 0; i < S2 && i < S; i++) v[i] /= rhs[i];
        return *this;
    }

    template<std::size_t S2>
    friend constexpr vec operator+(vec lhs, const vec<T, S2> &rhs) {
        lhs += rhs;
        return lhs;
    }
    template<std::size_t S2>
    friend constexpr vec operator-(vec lhs, const vec<T, S2> &rhs) {
        lhs -= rhs;
        return lhs;
    }
    template<std::size_t S2>
    friend constexpr vec operator*(vec lhs, const vec<T, S2> &rhs) {
        lhs *= rhs;
        return lhs;
    }
    template<std::size_t S2>
    friend constexpr vec operator/(vec lhs, const vec<T, S2> &rhs) {
        lhs /= rhs;
        return lhs;
    }
    
    constexpr T &operator[](std::size_t i) {
        return v[i];
    }
    constexpr T operator[](std::size_t i) const {
        return v[i];
    }
};

template<typename T>
    requires(std::is_arithmetic_v<T>)
class vec2 final : public vec<T, 2>
{
public:
    T &x;
    T &y;

    constexpr vec2(T X, T Y) :
        vec<T, 2>(std::array<T, 2>{X, Y}), 
        x(this->v[0]), 
        y(this->v[1]) 
    {}
    constexpr vec2(std::array<T, 2> vs) :
        vec<T, 2>(vs), 
        x(this->v[0]), 
        y(this->v[1]) 
    {}
    constexpr vec2(T vs) :
        vec<T, 2>(vs), 
        x(this->v[0]), 
        y(this->v[1]) 
    {}
};

template<typename T>
    requires(std::is_arithmetic_v<T>)
class vec3 final : public vec<T, 3>
{
public:
    T &x;
    T &y;
    T &z;

    constexpr vec3(T X, T Y, T Z) :
        vec<T, 3>(std::array<T, 3>{X, Y, Z}), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2])
    {}
    constexpr vec3(std::array<T, 3> vs) :
        vec<T, 3>(vs), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2])
    {}
    constexpr vec3(T vs) :
        vec<T, 3>(vs), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2])
    {}
};

template<typename T>
    requires(std::is_arithmetic_v<T>)
class vec4 final : public vec<T, 4>
{
public:
    T &x;
    T &y;
    T &z;
    T &w;

    constexpr vec4(T X, T Y, T Z, T W) :
        vec<T, 4>(std::array<T, 4>{X, Y, Z, W}), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2]),
        w(this->v[3])
    {}
    constexpr vec4(std::array<T, 4> vs) :
        vec<T, 4>(vs), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2]),
        w(this->v[3])
    {}
    constexpr vec4(T vs) :
        vec<T, 4>(vs), 
        x(this->v[0]), 
        y(this->v[1]),
        z(this->v[2]),
        w(this->v[3])
    {}
};

}

#endif