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>
83 Vector(const Vector<U, N> &);
85 Vector &operator*=(T);
86 Vector &operator/=(T);
87 Vector &operator+=(const Vector &);
88 Vector &operator-=(const Vector &);
95 template<typename T, unsigned N>
96 inline Vector<T, N>::Vector()
98 for(unsigned i=0; i<N; ++i)
102 template<typename T, unsigned N>
103 inline Vector<T, N>::Vector(const T *d)
105 for(unsigned i=0; i<N; ++i)
109 /* The compiler won't instantiate these unless they are used. Trying to use
110 them on the wrong class results in an error. */
111 template<typename T, unsigned N>
112 inline Vector<T, N>::Vector(T x_, T y_)
114 this->VectorComponents<T, 2>::x = x_;
115 this->VectorComponents<T, 2>::y = y_;
118 template<typename T, unsigned N>
119 inline Vector<T, N>::Vector(T x_, T y_, T z_)
121 this->VectorComponents<T, 3>::x = x_;
122 this->VectorComponents<T, 3>::y = y_;
123 this->VectorComponents<T, 3>::z = z_;
126 template<typename T, unsigned N>
127 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
129 this->VectorComponents<T, 4>::x = x_;
130 this->VectorComponents<T, 4>::y = y_;
131 this->VectorComponents<T, 4>::z = z_;
132 this->VectorComponents<T, 4>::w = w_;
135 template<typename T, unsigned N>
137 inline Vector<T, N>::Vector(const Vector<U, N> &v)
139 for(unsigned i=0; i<N; ++i)
143 template<typename T, unsigned N>
144 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
146 for(unsigned i=0; i<N; ++i)
151 template<typename T, unsigned N>
152 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
158 template<typename T, unsigned N>
159 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
164 template<typename T, unsigned N>
165 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
167 for(unsigned i=0; i<N; ++i)
172 template<typename T, unsigned N>
173 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
179 template<typename T, unsigned N>
180 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
182 for(unsigned i=0; i<N; ++i)
187 template<typename T, unsigned N>
188 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
194 template<typename T, unsigned N>
195 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
197 for(unsigned i=0; i<N; ++i)
202 template<typename T, unsigned N>
203 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
209 template<typename T, unsigned N>
210 inline Vector<T, N> operator-(const Vector<T, N> &v)
213 for(unsigned i=0; i<N; ++i)
218 template<typename T, unsigned N>
219 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
221 for(unsigned i=0; i<N; ++i)
227 template<typename T, unsigned N>
228 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
231 for(unsigned i=0; i<N; ++i)
236 template<typename T, unsigned N>
237 inline T Vector<T, N>::norm() const
239 return sqrt(inner_product(*this, *this));
242 template<typename T, unsigned N>
243 inline Vector<T, N> &Vector<T, N>::normalize()
245 return *this /= norm();
248 template<typename T, unsigned N>
249 inline Vector<T, N> normalize(const Vector<T, N> &v)
252 return r.normalize();
256 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
258 return inner_product(v1, v2);
262 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
264 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);