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;
85 Vector(const Vector<U, N> &);
87 unsigned size() const { return N; }
90 Vector<T, M> slice(unsigned) const;
92 Vector &operator*=(T);
93 Vector &operator/=(T);
94 Vector &operator+=(const Vector &);
95 Vector &operator-=(const Vector &);
102 template<typename T, unsigned N>
103 inline Vector<T, N>::Vector()
105 for(unsigned i=0; i<N; ++i)
109 template<typename T, unsigned N>
110 inline Vector<T, N>::Vector(const T *d)
112 for(unsigned i=0; i<N; ++i)
116 /* The compiler won't instantiate these unless they are used. Trying to use
117 them on the wrong class results in an error. */
118 template<typename T, unsigned N>
119 inline Vector<T, N>::Vector(T x_, T y_)
121 this->VectorComponents<T, 2>::x = x_;
122 this->VectorComponents<T, 2>::y = y_;
125 template<typename T, unsigned N>
126 inline Vector<T, N>::Vector(T x_, T y_, T z_)
128 this->VectorComponents<T, 3>::x = x_;
129 this->VectorComponents<T, 3>::y = y_;
130 this->VectorComponents<T, 3>::z = z_;
133 template<typename T, unsigned N>
134 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
136 this->VectorComponents<T, 4>::x = x_;
137 this->VectorComponents<T, 4>::y = y_;
138 this->VectorComponents<T, 4>::z = z_;
139 this->VectorComponents<T, 4>::w = w_;
142 template<typename T, unsigned N>
144 inline Vector<T, N>::Vector(const Vector<U, N> &v)
146 for(unsigned i=0; i<N; ++i)
150 template<typename T, unsigned N>
151 inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
154 for(unsigned i=0; i<N; ++i)
160 template<typename T, unsigned N>
161 inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
164 for(unsigned i=0; i<N; ++i)
170 template<typename T, unsigned N, unsigned M>
171 inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
174 for(unsigned i=0; i<N; ++i)
176 for(unsigned i=0; i<M; ++i)
181 template<typename T, unsigned N>
183 inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
186 for(unsigned i=0; i<M; ++i)
191 template<typename T, unsigned N>
192 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
194 for(unsigned i=0; i<N; ++i)
199 template<typename T, unsigned N>
200 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
206 template<typename T, unsigned N>
207 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
212 template<typename T, unsigned N>
213 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
215 for(unsigned i=0; i<N; ++i)
220 template<typename T, unsigned N>
221 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
227 template<typename T, unsigned N>
228 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
230 for(unsigned i=0; i<N; ++i)
235 template<typename T, unsigned N>
236 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
242 template<typename T, unsigned N>
243 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
245 for(unsigned i=0; i<N; ++i)
250 template<typename T, unsigned N>
251 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
257 template<typename T, unsigned N>
258 inline 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 bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
269 for(unsigned i=0; i<N; ++i)
275 template<typename T, unsigned N>
276 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
279 for(unsigned i=0; i<N; ++i)
284 template<typename T, unsigned N>
285 inline T Vector<T, N>::norm() const
288 return sqrt(inner_product(*this, *this));
291 template<typename T, unsigned N>
292 inline Vector<T, N> &Vector<T, N>::normalize()
294 return *this /= norm();
297 template<typename T, unsigned N>
298 inline Vector<T, N> normalize(const Vector<T, N> &v)
301 return r.normalize();
305 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
307 return inner_product(v1, v2);
311 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
313 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);