summaryrefslogtreecommitdiffstats
path: root/vector/vector.cpp
blob: 9e0ca6c313cab40f05571367871bfb7fc53e1d24 (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
#include <iostream>

#include <iterator>
#include <cassert>
#include <array>
#include <initializer_list>
#include <algorithm>

template<typename T, std::size_t d>
struct basic_vec : public std::array<T, d> {
    static constexpr std::size_t dimensions = d;

    basic_vec();
    basic_vec(const std::initializer_list<T> l);
    template<std::size_t n> basic_vec(const basic_vec<T, n>& other);

    T length() const;
};

template<typename T, std::size_t d>
basic_vec<T, d>::basic_vec() : std::array<T, d>() {
    this->fill(static_cast<T>(0));
}

template<typename T, std::size_t d>
basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) {
    basic_vec();

    // why can't this sh*t be a constexpr with static_assert???
    assert(l.size() <= d);
    std::copy(l.begin(), l.end(), this->begin());
}

template<typename T, std::size_t d>
template<std::size_t n>
basic_vec<T, d>::basic_vec(const basic_vec<T, n>& other) {
    basic_vec();

    static_assert(d >= n);
    for (std::size_t i = 0;  i < n; i++) {
        this->at(i) = other.at(i);
    }
}

template<typename T, std::size_t d>
T basic_vec<T, d>::length() const {
    T res = static_cast<T>(0);
    for (const T& val : *this) {
        res += val * val;
    }

    return res;
}

template<typename T, std::size_t d>
basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
    basic_vec<T, d> out;
    auto rit = rhs.cbegin(), lit = lhs.cbegin();
    std::generate(out.begin(), out.end(), [rit, lit] () mutable {
        return *(rit++) + *(lit++);
    });

    return out;
}

template<typename T, std::size_t d>
basic_vec<T, d> operator*(const T& rhs, const basic_vec<T, d>& lhs) {
    basic_vec<T, d> out;
    auto lit = lhs.cbegin();

    std::generate(out.begin(), out.end(), [rhs, lit] () mutable {
        return rhs * *(lit++);
    });

    return out;
}

template<typename T, std::size_t d>
basic_vec<T, d> operator-(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
    return rhs + static_cast<T>(-1) * lhs;
}

template<typename T, std::size_t d>
T operator*(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
    T res = static_cast<T>(0);
    for (std::size_t i = 0; i < d; i++) {
        res += rhs[i] * lhs[i];
    }

    return res;
}

template<typename T, std::size_t d>
std::ostream& operator<<(std::ostream& os, const basic_vec<T, d>& v) {
    os << "<";
    for (std::size_t i = 0; i < d -1; i++) {
        os << v[i] << ", ";
    }
    os << v[d-1] << ">";
    return os;
}


template<typename T, std::size_t d>
class vec: public basic_vec<T, d> {
public:
    vec(std::initializer_list<T> l) : basic_vec<T, d>(l) {}

    template<std::size_t n>
    vec(const basic_vec<T, n>& other) : basic_vec<T, d>(other) {}
};

template<typename T>
class vec3 : public basic_vec<T, 3> {
public:
    vec3(std::initializer_list<T> l) : basic_vec<T, 3>(l) {}

    template<std::size_t n>
    vec3(const basic_vec<T, n>& other) : basic_vec<T, 3>(other) {}

    static vec3<T> cross(const vec3<T>& rhs, const vec3<T>& lhs);
};

template<typename T>
vec3<T> vec3<T>::cross(const vec3<T>& rhs, const vec3<T>& lhs) {
    vec3<T> res;

    res[0] = (rhs[1] * lhs[2]) - (rhs[2] * lhs[1]);
    res[1] = (rhs[2] * lhs[0]) - (rhs[0] * lhs[2]);
    res[2] = (rhs[0] * lhs[1]) - (rhs[1] * lhs[0]);

    return res;
}


template<typename T>
class vec2: public basic_vec<T, 2> {
public:
    vec2(std::initializer_list<T> l) : basic_vec<T, 2>(l) {}

    template<std::size_t n>
    vec2(const basic_vec<T, n>& other) : basic_vec<T, 2>(other) {}

    T angle();
    static vec3<T> cross(const vec2<T>& rhs, const vec2<T>& lhs);
};

template<typename T>
vec3<T> vec2<T>::cross(const vec2<T>& rhs, const vec2<T>& lhs) {
    return vec3<T>::cross(vec3<T>(rhs), vec3<T>(lhs));
}


int main(int argc, char *argv[]) {
    vec3<double> v{1, 2, 3};
    vec3<double> u{3, 4, 5};

    std::cout << v << std::endl;
    std::cout << v.length() << std::endl;
    std::cout << v + u << std::endl;
    std::cout << v - u << std::endl;
    std::cout << 2.0 * u << std::endl;
    std::cout << v * u << std::endl;

    return 0;
}