1 #ifndef MSP_LINAL_VECTOR_H_
2 #define MSP_LINAL_VECTOR_H_
12 Base class to provide the components of a vector. This is used so that
13 specializations with individual members can be provided in some dimensions.
15 template<typename T, unsigned N>
16 class VectorComponents
22 VectorComponents() { }
25 T &operator[](unsigned i) { return data[i]; }
26 const T &operator[](unsigned i) const { return data[i]; }
30 class VectorComponents<T, 2>
36 VectorComponents() { }
39 T &operator[](unsigned i) { return *(&x+i); }
40 const T &operator[](unsigned i) const { return *(&x+i); }
44 class VectorComponents<T, 3>
50 VectorComponents() { }
53 T &operator[](unsigned i) { return *(&x+i); }
54 const T &operator[](unsigned i) const { return *(&x+i); }
58 class VectorComponents<T, 4>
64 VectorComponents() { }
67 T &operator[](unsigned i) { return *(&x+i); }
68 const T &operator[](unsigned i) const { return *(&x+i); }
72 A general mathematical vector.
74 template<typename T, unsigned N>
75 class Vector: public VectorComponents<T, N>
78 typedef T ElementType;
83 /** Constructs a vector from an array of interleaved values. Intended for
84 use by Matrix row accessor. */
85 Vector(const T *, unsigned);
87 template<typename... Args>
91 Vector(const Vector<U, N> &);
93 unsigned size() const { return N; }
96 Vector<T, M> slice(unsigned) const;
98 Vector &operator*=(T);
99 Vector &operator/=(T);
100 Vector &operator+=(const Vector &);
101 Vector &operator-=(const Vector &);
108 template<typename T, unsigned N>
109 inline Vector<T, N>::Vector()
111 for(unsigned i=0; i<N; ++i)
115 template<typename T, unsigned N>
116 inline Vector<T, N>::Vector(const T *d)
118 for(unsigned i=0; i<N; ++i)
122 template<typename T, unsigned N>
123 inline Vector<T, N>::Vector(const T *d, unsigned stride)
125 for(unsigned i=0; i<N; ++i)
126 (*this)[i] = d[i*stride];
129 template<typename T, unsigned N>
130 template<typename... Args>
131 inline Vector<T, N>::Vector(T x_, Args... v)
133 static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor");
136 for(auto c: std::initializer_list<T> { static_cast<T>(v)... })
140 template<typename T, unsigned N>
142 inline Vector<T, N>::Vector(const Vector<U, N> &v)
144 for(unsigned i=0; i<N; ++i)
148 template<typename T, unsigned N>
149 inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
152 for(unsigned i=0; i<N; ++i)
158 template<typename T, unsigned N>
159 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
163 for(unsigned i=0; i<N; ++i)
168 template<typename T, unsigned N, unsigned M>
169 inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
172 for(unsigned i=0; i<N; ++i)
174 for(unsigned i=0; i<M; ++i)
179 template<typename T, unsigned N>
181 inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
184 for(unsigned i=0; i<M; ++i)
189 template<typename T, unsigned N>
190 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
192 for(unsigned i=0; i<N; ++i)
197 template<typename T, unsigned N>
198 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
204 template<typename T, unsigned N>
205 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
210 template<typename T, unsigned N>
211 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
213 for(unsigned i=0; i<N; ++i)
218 template<typename T, unsigned N>
219 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
225 template<typename T, unsigned N>
226 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
228 for(unsigned i=0; i<N; ++i)
233 template<typename T, unsigned N>
234 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
240 template<typename T, unsigned N>
241 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
243 for(unsigned i=0; i<N; ++i)
248 template<typename T, unsigned N>
249 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
255 template<typename T, unsigned N>
256 inline Vector<T, N> operator-(const Vector<T, N> &v)
259 for(unsigned i=0; i<N; ++i)
264 template<typename T, unsigned N>
265 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
267 for(unsigned i=0; i<N; ++i)
273 template<typename T, unsigned N>
274 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
277 for(unsigned i=0; i<N; ++i)
282 template<typename T, unsigned N>
283 inline T Vector<T, N>::norm() const
286 return sqrt(inner_product(*this, *this));
289 template<typename T, unsigned N>
290 inline Vector<T, N> &Vector<T, N>::normalize()
292 return *this /= norm();
295 template<typename T, unsigned N>
296 inline Vector<T, N> normalize(const Vector<T, N> &v)
299 return r.normalize();
303 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
305 return inner_product(v1, v2);
309 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
311 return Vector<T, 3>(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x);
314 template<typename T, unsigned N>
315 inline std::ostream &operator<<(std::ostream &s, const Vector<T, N> &v)
317 s << "Vector" << N << '(';
318 for(unsigned i=0; i<N; ++i)