const T &operator[](unsigned i) const { return *(&x+i); }
};
+template<typename T>
+class VectorComponents<T, 4>
+{
+public:
+ T x, y, z, w;
+
+protected:
+ VectorComponents() { }
+
+public:
+ T &operator[](unsigned i) { return *(&x+i); }
+ const T &operator[](unsigned i) const { return *(&x+i); }
+};
+
/**
A general mathematical vector.
*/
Vector(const T *);
Vector(T, T);
Vector(T, T, T);
+ Vector(T, T, T, T);
template<typename U>
Vector(const Vector<U, N> &);
+ template<unsigned M>
+ Vector<T, M> slice(unsigned) const;
+
Vector &operator*=(T);
Vector &operator/=(T);
Vector &operator+=(const Vector &);
this->VectorComponents<T, 3>::z = z_;
}
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
+{
+ this->VectorComponents<T, 4>::x = x_;
+ this->VectorComponents<T, 4>::y = y_;
+ this->VectorComponents<T, 4>::z = z_;
+ this->VectorComponents<T, 4>::w = w_;
+}
+
template<typename T, unsigned N>
template<typename U>
inline Vector<T, N>::Vector(const Vector<U, N> &v)
(*this)[i] = v[i];
}
+template<typename T, unsigned N>
+inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
+{
+ Vector<T, N+1> r;
+ for(unsigned i=0; i<N; ++i)
+ r[i] = v[i];
+ r[N] = s;
+ return r;
+}
+
+template<typename T, unsigned N>
+inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
+{
+ Vector<T, N+1> r;
+ for(unsigned i=0; i<N; ++i)
+ r[i] = v[i];
+ r[N] = s;
+ return r;
+}
+
+template<typename T, unsigned N, unsigned M>
+inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
+{
+ Vector<T, N+M> r;
+ for(unsigned i=0; i<N; ++i)
+ r[i] = v1[i];
+ for(unsigned i=0; i<M; ++i)
+ r[N+i] = v2[i];
+ return r;
+}
+
+template<typename T, unsigned N>
+template<unsigned M>
+inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
+{
+ Vector<T, M> r;
+ for(unsigned i=0; i<M; ++i)
+ r[i] = (*this)[j+i];
+ return r;
+}
+
template<typename T, unsigned N>
inline Vector<T, N> &Vector<T, N>::operator*=(T s)
{
template<typename T, unsigned N>
inline T Vector<T, N>::norm() const
{
+ using std::sqrt;
return sqrt(inner_product(*this, *this));
}