]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Give Vector constructors from one higher/lower dimension
[libs/math.git] / source / linal / vector.h
index 11292f7441dae7e7f69cefb58aadb73572f687e4..a5c9a26cfc1e8152fd4cece0158aa553f3b97c8d 100644 (file)
@@ -53,6 +53,20 @@ public:
        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.
 */
@@ -64,8 +78,13 @@ public:
        Vector(const T *);
        Vector(T, T);
        Vector(T, T, T);
+       Vector(T, T, T, T);
        template<typename U>
        Vector(const Vector<U, N> &);
+       template<typename U>
+       Vector(const Vector<U, N-1> &, U);
+       template<typename U>
+       explicit Vector(const Vector<U, N+1> &);
 
        Vector &operator*=(T);
        Vector &operator/=(T);
@@ -108,6 +127,15 @@ inline Vector<T, N>::Vector(T x_, T y_, T z_)
        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)
@@ -116,6 +144,23 @@ inline Vector<T, N>::Vector(const Vector<U, N> &v)
                (*this)[i] = v[i];
 }
 
+template<typename T, unsigned N>
+template<typename U>
+inline Vector<T, N>::Vector(const Vector<U, N-1> &v, U s)
+{
+       for(unsigned i=0; i<N-1; ++i)
+               (*this)[i] = v[i];
+       (*this)[N-1] = s;
+}
+
+template<typename T, unsigned N>
+template<typename U>
+inline Vector<T, N>::Vector(const Vector<U, N+1> &v)
+{
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = v[i];
+}
+
 template<typename T, unsigned N>
 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
 {