summaryrefslogtreecommitdiffstats
path: root/engine/include/symbasic/dvector.tpp
blob: 3aa792364ef0a6ba1beea76ba0681471b24de2d7 (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
#ifndef __DVECTOR_TPP__
#define __DVECTOR_TPP__

template<class T>
DVector<T>::DVector(size_t dim) 
    : components(dim)
{

}

template<class T>
DVector<T>::DVector(const std::vector<T>& init)
    : components(init)
{

}

template<class T>
DVector<T>::DVector(const std::initializer_list<T> &init)
    : components(init.begin(), init.end())
{

}

template<class T>
const size_t DVector<T>::size() const
{
    return components.size();
}

template<class T>
T& DVector<T>::operator[](size_t i)
{
    return components[i];
}

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

template<class T>
DVector<T>& DVector<T>::operator<<(const T &value)
{
    components.push_back(value);
}

template<class T>
DVector<T>& DVector<T>::operator+=(const Vector<T> &w)
{
    return static_cast<DVector<T>&>(this->add(w));
}

template<class T>
DVector<T>& DVector<T>::operator-=(const Vector<T> &w)
{
    return static_cast<DVector<T>&>(this->sub(w));
}

template<class T>
DVector<T>& DVector<T>::operator*=(double k)
{
    return static_cast<DVector<T>&>(this->mult(k));
}

template<class T>
DVector<T>& DVector<T>::operator/=(double k)
{
    return static_cast<DVector<T>&>(this->divide(k));
}

template<class T>
DVector<T>& DVector<T>::operator~()
{
    return static_cast<DVector<T>&>(this->reverse());
}

template<class T>
DVector<T>& DVector<T>::unit()
{
    return static_cast<DVector<T>&>(this->_unit());
}

template<class T>
const DVector<T> operator-(DVector<T> v)
{
    return ~v;
}

template<class T>
const DVector<T> operator+(DVector<T> v, const Vector<T>& w)
{
    return v += w;
}

template<class T>
const DVector<T> operator-(DVector<T> v, const Vector<T>& w)
{
    return v -= w;
}

template<class T>
const DVector<T> operator*(DVector<T> v, double k)
{
    return v *= k;
}

template<class T>
const DVector<T> operator/(DVector<T> v, double k)
{
    return v /= k;
}

template<class T>
const DVector<T> operator*(double k, DVector<T> v)
{
    return v *= k;
}

template<class T>
const DVector<T> operator/(double k, DVector<T> v)
{
    return v /= k;
}

inline const DVector<double> operator^(const DVector<double>& v, const Vector<double>& w)
{
    DVector<double> u;

    size_t n = (v.size() <= w.size()) ? v.size() : w.size();

    if (n > 2)
    {
        u << v[1] * w[2] - v[2] * w[1];
        u << v[2] * w[0] - v[0] * w[2];
        u << v[0] * w[1] - v[1] * w[0];

    } else if (n == 2)
        u << v[0] * w[1] - v[1] * w[0];
    else if (n == 1)
        u << v[0] * w[0];

    return u;
}

#endif