summaryrefslogtreecommitdiffstats
path: root/engine/include/symbasic/svector.tpp
blob: e0540490a2a50e8d474825595469b3c622c27fe5 (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
#ifndef __SVECTOR_TPP__
#define __SVECTOR_TPP__

template<class T, std::size_t N>
SVector<T, N>::SVector(const T &value)
{
    components.fill(value);
}

template<class T, std::size_t N>
SVector<T, N>::SVector(const std::initializer_list<T> &init)
{
    size_t n = (init.size() < N) ? init.size() : N;

    /* copy init list without include <algorithm> */
    size_t index = 0;

    /* copy the initializer list until its size */
    for (auto c : init)
    {
        if (index >= n)
            break;

        components[index++] = c;
    }

    /* fill the remaining components with zero */
    while(index < N)
        components[index++] = 0;
}

template<class T, std::size_t N>
SVector<T, N>::SVector(const SVector<T, N> &cpy)
{
    this->components = cpy.components;
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator=(const SVector<T, N> &v)
{
    this->components = v.components;
}

template<class T, std::size_t N>
const size_t SVector<T, N>::size() const
{
    return N;
}

/* Access operators */
template<class T, std::size_t N>
T& SVector<T, N>::operator[](size_t i)    
{ 
    return components[i]; 
}

template<class T, std::size_t N>
const T& SVector<T, N>::operator[](size_t i) const 
{ 
    return components[i]; 
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator+=(const Vector<T> &w)
{
    return static_cast<SVector<T, N>&>(this->add(w));
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator-=(const Vector<T> &w)
{
    return static_cast<SVector<T, N>&>(this->sub(w));
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator*=(double k)
{
    return static_cast<SVector<T, N>&>(this->mult(k));
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator/=(double k)
{
    return static_cast<SVector<T, N>&>(this->divide(k));
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::operator~()
{
    return static_cast<SVector<T, N>&>(this->reverse());
}

template<class T, std::size_t N>
SVector<T, N>& SVector<T, N>::unit()
{
    return static_cast<SVector<T, N>&>(this->_unit());
}

template<class T, std::size_t N>
const SVector<T, N> operator+(SVector<T, N> v, const Vector<T>& w)
{
    return v += w;
}

template<class T, std::size_t N>
const SVector<T, N> operator-(SVector<T, N> v, const Vector<T>& w)
{
    return v -= w;
}

template<class T, std::size_t N>
const SVector<T, N> operator*(SVector<T, N> v, double k)
{
    return v *= k;
}

template<class T, std::size_t N>
const SVector<T, N> operator/(SVector<T, N> v, double k)
{
    return v /= k;
}

template<class T, std::size_t N>
const SVector<T, N> operator*(double k, SVector<T, N> v)
{
    return v *= k;
}

template<class T, std::size_t N>
const SVector<T, N> operator/(double k, SVector<T, N> v)
{
    return v /= k;
}

template<class T, std::size_t N>
const SVector<T, N> operator-(SVector<T, N> v)
{
    return ~v;
}

inline const SVector<double, 3> operator^(const SVector<double, 3>& v, const SVector<double, 3>& w)
{
    SVector<double, 3> u = {0, 0, 0};

    u[0] = v[1] * w[2] - v[2] * w[1];
    u[1] = v[2] * w[0] - v[0] * w[2];
    u[2] = v[0] * w[1] - v[1] * w[0];

    return u;
}

inline double operator^(const SVector<double, 2>& v, const SVector<double, 2>& w)
{
    return v[0] * w[1] - v[1] * w[0];
}

inline double operator^(const SVector<double, 1>& v, const SVector<double, 1>& w)
{
    return v[0] * w[0];
}

#endif