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 #if __cplusplus >= 201103L
88 template<typename... Args>
96 Vector(const Vector<U, N> &);
98 unsigned size() const { return N; }
101 Vector<T, M> slice(unsigned) const;
103 Vector &operator*=(T);
104 Vector &operator/=(T);
105 Vector &operator+=(const Vector &);
106 Vector &operator-=(const Vector &);
113 template<typename T, unsigned N>
114 inline Vector<T, N>::Vector()
116 for(unsigned i=0; i<N; ++i)
120 template<typename T, unsigned N>
121 inline Vector<T, N>::Vector(const T *d)
123 for(unsigned i=0; i<N; ++i)
127 template<typename T, unsigned N>
128 inline Vector<T, N>::Vector(const T *d, unsigned stride)
130 for(unsigned i=0; i<N; ++i)
131 (*this)[i] = d[i*stride];
134 #if __cplusplus >= 201103L
135 template<typename T, unsigned N>
136 template<typename... Args>
137 inline Vector<T, N>::Vector(T x_, Args... v)
139 static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor");
142 for(auto c: std::initializer_list<T> { static_cast<T>(v)... })
146 /* The compiler won't instantiate these unless they are used. Trying to use
147 them on the wrong class results in an error. */
148 template<typename T, unsigned N>
149 inline Vector<T, N>::Vector(T x_, T y_)
151 this->VectorComponents<T, 2>::x = x_;
152 this->VectorComponents<T, 2>::y = y_;
155 template<typename T, unsigned N>
156 inline Vector<T, N>::Vector(T x_, T y_, T z_)
158 this->VectorComponents<T, 3>::x = x_;
159 this->VectorComponents<T, 3>::y = y_;
160 this->VectorComponents<T, 3>::z = z_;
163 template<typename T, unsigned N>
164 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
166 this->VectorComponents<T, 4>::x = x_;
167 this->VectorComponents<T, 4>::y = y_;
168 this->VectorComponents<T, 4>::z = z_;
169 this->VectorComponents<T, 4>::w = w_;
173 template<typename T, unsigned N>
175 inline Vector<T, N>::Vector(const Vector<U, N> &v)
177 for(unsigned i=0; i<N; ++i)
181 template<typename T, unsigned N>
182 inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
185 for(unsigned i=0; i<N; ++i)
191 template<typename T, unsigned N>
192 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
196 for(unsigned i=0; i<N; ++i)
201 template<typename T, unsigned N, unsigned M>
202 inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
205 for(unsigned i=0; i<N; ++i)
207 for(unsigned i=0; i<M; ++i)
212 template<typename T, unsigned N>
214 inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
217 for(unsigned i=0; i<M; ++i)
222 template<typename T, unsigned N>
223 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
225 for(unsigned i=0; i<N; ++i)
230 template<typename T, unsigned N>
231 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
237 template<typename T, unsigned N>
238 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
243 template<typename T, unsigned N>
244 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
246 for(unsigned i=0; i<N; ++i)
251 template<typename T, unsigned N>
252 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
258 template<typename T, unsigned N>
259 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
261 for(unsigned i=0; i<N; ++i)
266 template<typename T, unsigned N>
267 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
273 template<typename T, unsigned N>
274 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
276 for(unsigned i=0; i<N; ++i)
281 template<typename T, unsigned N>
282 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
288 template<typename T, unsigned N>
289 inline Vector<T, N> operator-(const Vector<T, N> &v)
292 for(unsigned i=0; i<N; ++i)
297 template<typename T, unsigned N>
298 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
300 for(unsigned i=0; i<N; ++i)
306 template<typename T, unsigned N>
307 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
310 for(unsigned i=0; i<N; ++i)
315 template<typename T, unsigned N>
316 inline T Vector<T, N>::norm() const
319 return sqrt(inner_product(*this, *this));
322 template<typename T, unsigned N>
323 inline Vector<T, N> &Vector<T, N>::normalize()
325 return *this /= norm();
328 template<typename T, unsigned N>
329 inline Vector<T, N> normalize(const Vector<T, N> &v)
332 return r.normalize();
336 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
338 return inner_product(v1, v2);
342 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
344 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);
347 template<typename T, unsigned N>
348 inline std::ostream &operator<<(std::ostream &s, const Vector<T, N> &v)
350 s << "Vector" << N << '(';
351 for(unsigned i=0; i<N; ++i)