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);
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 /* The compiler won't instantiate these unless they are used. Trying to use
130 them on the wrong class results in an error. */
131 template<typename T, unsigned N>
132 inline Vector<T, N>::Vector(T x_, T y_)
134 this->VectorComponents<T, 2>::x = x_;
135 this->VectorComponents<T, 2>::y = y_;
138 template<typename T, unsigned N>
139 inline Vector<T, N>::Vector(T x_, T y_, T z_)
141 this->VectorComponents<T, 3>::x = x_;
142 this->VectorComponents<T, 3>::y = y_;
143 this->VectorComponents<T, 3>::z = z_;
146 template<typename T, unsigned N>
147 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
149 this->VectorComponents<T, 4>::x = x_;
150 this->VectorComponents<T, 4>::y = y_;
151 this->VectorComponents<T, 4>::z = z_;
152 this->VectorComponents<T, 4>::w = w_;
155 template<typename T, unsigned N>
157 inline Vector<T, N>::Vector(const Vector<U, N> &v)
159 for(unsigned i=0; i<N; ++i)
163 template<typename T, unsigned N>
164 inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
167 for(unsigned i=0; i<N; ++i)
173 template<typename T, unsigned N>
174 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
178 for(unsigned i=0; i<N; ++i)
183 template<typename T, unsigned N, unsigned M>
184 inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
187 for(unsigned i=0; i<N; ++i)
189 for(unsigned i=0; i<M; ++i)
194 template<typename T, unsigned N>
196 inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
199 for(unsigned i=0; i<M; ++i)
204 template<typename T, unsigned N>
205 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
207 for(unsigned i=0; i<N; ++i)
212 template<typename T, unsigned N>
213 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
219 template<typename T, unsigned N>
220 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
225 template<typename T, unsigned N>
226 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
228 for(unsigned i=0; i<N; ++i)
233 template<typename T, unsigned N>
234 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
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> &Vector<T, N>::operator-=(const Vector<T, N> &v)
258 for(unsigned i=0; i<N; ++i)
263 template<typename T, unsigned N>
264 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
270 template<typename T, unsigned N>
271 inline Vector<T, N> operator-(const Vector<T, N> &v)
274 for(unsigned i=0; i<N; ++i)
279 template<typename T, unsigned N>
280 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
282 for(unsigned i=0; i<N; ++i)
288 template<typename T, unsigned N>
289 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
292 for(unsigned i=0; i<N; ++i)
297 template<typename T, unsigned N>
298 inline T Vector<T, N>::norm() const
301 return sqrt(inner_product(*this, *this));
304 template<typename T, unsigned N>
305 inline Vector<T, N> &Vector<T, N>::normalize()
307 return *this /= norm();
310 template<typename T, unsigned N>
311 inline Vector<T, N> normalize(const Vector<T, N> &v)
314 return r.normalize();
318 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
320 return inner_product(v1, v2);
324 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
326 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);
329 template<typename T, unsigned N>
330 inline std::ostream &operator<<(std::ostream &s, const Vector<T, N> &v)
332 s << "Vector" << N << '(';
333 for(unsigned i=0; i<N; ++i)