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 A general mathematical vector.
59 template<typename T, unsigned N>
60 class Vector: public VectorComponents<T, N>
68 Vector(const Vector<U, N> &);
70 Vector &operator*=(T);
71 Vector &operator/=(T);
72 Vector &operator+=(const Vector &);
73 Vector &operator-=(const Vector &);
80 template<typename T, unsigned N>
81 inline Vector<T, N>::Vector()
83 for(unsigned i=0; i<N; ++i)
87 template<typename T, unsigned N>
88 inline Vector<T, N>::Vector(const T *d)
90 for(unsigned i=0; i<N; ++i)
94 /* The compiler won't instantiate these unless they are used. Trying to use
95 them on the wrong class results in an error. */
96 template<typename T, unsigned N>
97 inline Vector<T, N>::Vector(T x_, T y_)
99 this->VectorComponents<T, 2>::x = x_;
100 this->VectorComponents<T, 2>::y = y_;
103 template<typename T, unsigned N>
104 inline Vector<T, N>::Vector(T x_, T y_, T z_)
106 this->VectorComponents<T, 3>::x = x_;
107 this->VectorComponents<T, 3>::y = y_;
108 this->VectorComponents<T, 3>::z = z_;
111 template<typename T, unsigned N>
113 inline Vector<T, N>::Vector(const Vector<U, N> &v)
115 for(unsigned i=0; i<N; ++i)
119 template<typename T, unsigned N>
120 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
122 for(unsigned i=0; i<N; ++i)
127 template<typename T, unsigned N>
128 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
134 template<typename T, unsigned N>
135 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
140 template<typename T, unsigned N>
141 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
143 for(unsigned i=0; i<N; ++i)
148 template<typename T, unsigned N>
149 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
155 template<typename T, unsigned N>
156 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
158 for(unsigned i=0; i<N; ++i)
163 template<typename T, unsigned N>
164 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
170 template<typename T, unsigned N>
171 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
173 for(unsigned i=0; i<N; ++i)
178 template<typename T, unsigned N>
179 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
185 template<typename T, unsigned N>
186 inline Vector<T, N> operator-(const Vector<T, N> &v)
189 for(unsigned i=0; i<N; ++i)
194 template<typename T, unsigned N>
195 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
197 for(unsigned i=0; i<N; ++i)
203 template<typename T, unsigned N>
204 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
207 for(unsigned i=0; i<N; ++i)
212 template<typename T, unsigned N>
213 inline T Vector<T, N>::norm() const
215 return sqrt(inner_product(*this, *this));
218 template<typename T, unsigned N>
219 inline Vector<T, N> &Vector<T, N>::normalize()
221 return *this /= norm();
224 template<typename T, unsigned N>
225 inline Vector<T, N> normalize(const Vector<T, N> &v)
228 return r.normalize();
232 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
234 return inner_product(v1, v2);
238 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
240 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);