1 #ifndef MSP_LINAL_VECTOR_H_
2 #define MSP_LINAL_VECTOR_H_
11 Base class to provide the components of a vector. This is used so that
12 specializations with individual members can be provided in some dimensions.
14 template<typename T, unsigned N>
15 class VectorComponents
21 VectorComponents() { }
24 T &operator[](unsigned i) { return data[i]; }
25 const T &operator[](unsigned i) const { return data[i]; }
29 class VectorComponents<T, 2>
35 VectorComponents() { }
38 T &operator[](unsigned i) { return *(&x+i); }
39 const T &operator[](unsigned i) const { return *(&x+i); }
43 class VectorComponents<T, 3>
49 VectorComponents() { }
52 T &operator[](unsigned i) { return *(&x+i); }
53 const T &operator[](unsigned i) const { return *(&x+i); }
57 class VectorComponents<T, 4>
63 VectorComponents() { }
66 T &operator[](unsigned i) { return *(&x+i); }
67 const T &operator[](unsigned i) const { return *(&x+i); }
71 A general mathematical vector.
73 template<typename T, unsigned N>
74 class Vector: public VectorComponents<T, N>
77 typedef T ElementType;
82 /** Constructs a vector from an array of interleaved values. Intended for
83 use by Matrix row accessor. */
84 Vector(const T *, unsigned);
90 Vector(const Vector<U, N> &);
92 unsigned size() const { return N; }
95 Vector<T, M> slice(unsigned) const;
97 Vector &operator*=(T);
98 Vector &operator/=(T);
99 Vector &operator+=(const Vector &);
100 Vector &operator-=(const Vector &);
107 template<typename T, unsigned N>
108 inline Vector<T, N>::Vector()
110 for(unsigned i=0; i<N; ++i)
114 template<typename T, unsigned N>
115 inline Vector<T, N>::Vector(const T *d)
117 for(unsigned i=0; i<N; ++i)
121 template<typename T, unsigned N>
122 inline Vector<T, N>::Vector(const T *d, unsigned stride)
124 for(unsigned i=0; i<N; ++i)
125 (*this)[i] = d[i*stride];
128 /* The compiler won't instantiate these unless they are used. Trying to use
129 them on the wrong class results in an error. */
130 template<typename T, unsigned N>
131 inline Vector<T, N>::Vector(T x_, T y_)
133 this->VectorComponents<T, 2>::x = x_;
134 this->VectorComponents<T, 2>::y = y_;
137 template<typename T, unsigned N>
138 inline Vector<T, N>::Vector(T x_, T y_, T z_)
140 this->VectorComponents<T, 3>::x = x_;
141 this->VectorComponents<T, 3>::y = y_;
142 this->VectorComponents<T, 3>::z = z_;
145 template<typename T, unsigned N>
146 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
148 this->VectorComponents<T, 4>::x = x_;
149 this->VectorComponents<T, 4>::y = y_;
150 this->VectorComponents<T, 4>::z = z_;
151 this->VectorComponents<T, 4>::w = w_;
154 template<typename T, unsigned N>
156 inline Vector<T, N>::Vector(const Vector<U, N> &v)
158 for(unsigned i=0; i<N; ++i)
162 template<typename T, unsigned N>
163 inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
166 for(unsigned i=0; i<N; ++i)
172 template<typename T, unsigned N>
173 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
176 for(unsigned i=0; i<N; ++i)
182 template<typename T, unsigned N, unsigned M>
183 inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
186 for(unsigned i=0; i<N; ++i)
188 for(unsigned i=0; i<M; ++i)
193 template<typename T, unsigned N>
195 inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
198 for(unsigned i=0; i<M; ++i)
203 template<typename T, unsigned N>
204 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
206 for(unsigned i=0; i<N; ++i)
211 template<typename T, unsigned N>
212 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
218 template<typename T, unsigned N>
219 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
224 template<typename T, unsigned N>
225 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
227 for(unsigned i=0; i<N; ++i)
232 template<typename T, unsigned N>
233 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
239 template<typename T, unsigned N>
240 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
242 for(unsigned i=0; i<N; ++i)
247 template<typename T, unsigned N>
248 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
254 template<typename T, unsigned N>
255 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
257 for(unsigned i=0; i<N; ++i)
262 template<typename T, unsigned N>
263 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
269 template<typename T, unsigned N>
270 inline Vector<T, N> operator-(const Vector<T, N> &v)
273 for(unsigned i=0; i<N; ++i)
278 template<typename T, unsigned N>
279 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
281 for(unsigned i=0; i<N; ++i)
287 template<typename T, unsigned N>
288 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
291 for(unsigned i=0; i<N; ++i)
296 template<typename T, unsigned N>
297 inline T Vector<T, N>::norm() const
300 return sqrt(inner_product(*this, *this));
303 template<typename T, unsigned N>
304 inline Vector<T, N> &Vector<T, N>::normalize()
306 return *this /= norm();
309 template<typename T, unsigned N>
310 inline Vector<T, N> normalize(const Vector<T, N> &v)
313 return r.normalize();
317 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
319 return inner_product(v1, v2);
323 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
325 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);