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(const Vector<U, N-1> &, U);
87 explicit Vector(const Vector<U, N+1> &);
89 Vector &operator*=(T);
90 Vector &operator/=(T);
91 Vector &operator+=(const Vector &);
92 Vector &operator-=(const Vector &);
99 template<typename T, unsigned N>
100 inline Vector<T, N>::Vector()
102 for(unsigned i=0; i<N; ++i)
106 template<typename T, unsigned N>
107 inline Vector<T, N>::Vector(const T *d)
109 for(unsigned i=0; i<N; ++i)
113 /* The compiler won't instantiate these unless they are used. Trying to use
114 them on the wrong class results in an error. */
115 template<typename T, unsigned N>
116 inline Vector<T, N>::Vector(T x_, T y_)
118 this->VectorComponents<T, 2>::x = x_;
119 this->VectorComponents<T, 2>::y = y_;
122 template<typename T, unsigned N>
123 inline Vector<T, N>::Vector(T x_, T y_, T z_)
125 this->VectorComponents<T, 3>::x = x_;
126 this->VectorComponents<T, 3>::y = y_;
127 this->VectorComponents<T, 3>::z = z_;
130 template<typename T, unsigned N>
131 inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
133 this->VectorComponents<T, 4>::x = x_;
134 this->VectorComponents<T, 4>::y = y_;
135 this->VectorComponents<T, 4>::z = z_;
136 this->VectorComponents<T, 4>::w = w_;
139 template<typename T, unsigned N>
141 inline Vector<T, N>::Vector(const Vector<U, N> &v)
143 for(unsigned i=0; i<N; ++i)
147 template<typename T, unsigned N>
149 inline Vector<T, N>::Vector(const Vector<U, N-1> &v, U s)
151 for(unsigned i=0; i<N-1; ++i)
156 template<typename T, unsigned N>
158 inline Vector<T, N>::Vector(const Vector<U, N+1> &v)
160 for(unsigned i=0; i<N; ++i)
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> operator*(T s, const Vector<T, N> &v)
185 template<typename T, unsigned N>
186 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
188 for(unsigned i=0; i<N; ++i)
193 template<typename T, unsigned N>
194 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
200 template<typename T, unsigned N>
201 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
203 for(unsigned i=0; i<N; ++i)
208 template<typename T, unsigned N>
209 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
215 template<typename T, unsigned N>
216 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
218 for(unsigned i=0; i<N; ++i)
223 template<typename T, unsigned N>
224 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
230 template<typename T, unsigned N>
231 inline Vector<T, N> operator-(const Vector<T, N> &v)
234 for(unsigned i=0; i<N; ++i)
239 template<typename T, unsigned N>
240 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
242 for(unsigned i=0; i<N; ++i)
248 template<typename T, unsigned N>
249 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
252 for(unsigned i=0; i<N; ++i)
257 template<typename T, unsigned N>
258 inline T Vector<T, N>::norm() const
260 return sqrt(inner_product(*this, *this));
263 template<typename T, unsigned N>
264 inline Vector<T, N> &Vector<T, N>::normalize()
266 return *this /= norm();
269 template<typename T, unsigned N>
270 inline Vector<T, N> normalize(const Vector<T, N> &v)
273 return r.normalize();
277 inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
279 return inner_product(v1, v2);
283 inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
285 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);