]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Add formatted output operators for vector and matrix classes
[libs/math.git] / source / linal / vector.h
index 933611eca6d63096f22b3ce9375c8b09a04f1d3e..c9b2a179fe5fd0ce595e4dda796b3c2d7b13ab5d 100644 (file)
@@ -3,27 +3,97 @@
 
 #include <algorithm>
 #include <cmath>
+#include <ostream>
 
 namespace Msp {
 namespace LinAl {
 
 /**
-A general mathematical vector.
+Base class to provide the components of a vector.  This is used so that
+specializations with individual members can be provided in some dimensions.
 */
 template<typename T, unsigned N>
-class Vector
+class VectorComponents
 {
-protected:
+private:
        T data[N];
 
+protected:
+       VectorComponents() { }
+
+public:
+       T &operator[](unsigned i) { return data[i]; }
+       const T &operator[](unsigned i) const { return data[i]; }
+};
+
+template<typename T>
+class VectorComponents<T, 2>
+{
+public:
+       T x, y;
+
+protected:
+       VectorComponents() { }
+
 public:
+       T &operator[](unsigned i) { return *(&x+i); }
+       const T &operator[](unsigned i) const { return *(&x+i); }
+};
+
+template<typename T>
+class VectorComponents<T, 3>
+{
+public:
+       T x, y, z;
+
+protected:
+       VectorComponents() { }
+
+public:
+       T &operator[](unsigned i) { return *(&x+i); }
+       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.
+*/
+template<typename T, unsigned N>
+class Vector: public VectorComponents<T, N>
+{
+public:
+       typedef T ElementType;
+
        Vector();
-       Vector(const T *d);
+       Vector(const T *);
+
+       /** Constructs a vector from an array of interleaved values.  Intended for
+       use by Matrix row accessor. */
+       Vector(const T *, unsigned);
+
+       Vector(T, T);
+       Vector(T, T, T);
+       Vector(T, T, T, T);
        template<typename U>
-       Vector(const Vector<U, N> &v);
+       Vector(const Vector<U, N> &);
+
+       unsigned size() const { return N; }
 
-       T &operator[](unsigned i);
-       const T &operator[](unsigned i) const;
+       template<unsigned M>
+       Vector<T, M> slice(unsigned) const;
 
        Vector &operator*=(T);
        Vector &operator/=(T);
@@ -38,39 +108,104 @@ public:
 template<typename T, unsigned N>
 inline Vector<T, N>::Vector()
 {
-       std::fill(data, data+N, T());
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = T();
 }
 
 template<typename T, unsigned N>
 inline Vector<T, N>::Vector(const T *d)
 {
-       std::copy(d, d+N, data);
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = d[i];
+}
+
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(const T *d, unsigned stride)
+{
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = d[i*stride];
+}
+
+/* The compiler won't instantiate these unless they are used.  Trying to use
+them on the wrong class results in an error. */
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(T x_, T y_)
+{
+       this->VectorComponents<T, 2>::x = x_;
+       this->VectorComponents<T, 2>::y = y_;
+}
+
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(T x_, T y_, T z_)
+{
+       this->VectorComponents<T, 3>::x = x_;
+       this->VectorComponents<T, 3>::y = y_;
+       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)
 {
-       std::copy(v.data, v.data+N, data);
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = v[i];
 }
 
 template<typename T, unsigned N>
-T &Vector<T, N>::operator[](unsigned i)
+inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
 {
-       return data[i];
+       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>
-const T &Vector<T, N>::operator[](unsigned i) const
+inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
 {
-       return data[i];
+       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)
 {
        for(unsigned i=0; i<N; ++i)     
-               data[i] *= s;
+               (*this)[i] *= s;
        return *this;
 }
 
@@ -91,7 +226,7 @@ template<typename T, unsigned N>
 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
 {
        for(unsigned i=0; i<N; ++i)     
-               data[i] /= s;
+               (*this)[i] /= s;
        return *this;
 }
 
@@ -106,7 +241,7 @@ template<typename T, unsigned N>
 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
 {
        for(unsigned i=0; i<N; ++i)     
-               data[i] += v[i];
+               (*this)[i] += v[i];
        return *this;
 }
 
@@ -121,7 +256,7 @@ template<typename T, unsigned N>
 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
 {
        for(unsigned i=0; i<N; ++i)     
-               data[i] -= v[i];
+               (*this)[i] -= v[i];
        return *this;
 }
 
@@ -141,6 +276,15 @@ inline Vector<T, N> operator-(const Vector<T, N> &v)
        return r;
 }
 
+template<typename T, unsigned N>
+inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
+{
+       for(unsigned i=0; i<N; ++i)
+               if(v[i]!=w[i])
+                       return false;
+       return true;
+}
+
 template<typename T, unsigned N>
 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
 {
@@ -153,6 +297,7 @@ inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
 template<typename T, unsigned N>
 inline T Vector<T, N>::norm() const
 {
+       using std::sqrt;
        return sqrt(inner_product(*this, *this));
 }
 
@@ -169,6 +314,32 @@ inline Vector<T, N> normalize(const Vector<T, N> &v)
        return r.normalize();
 }
 
+template<typename T>
+inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
+{
+       return inner_product(v1, v2);
+}
+
+template<typename T>
+inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
+{
+       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);
+}
+
+template<typename T, unsigned N>
+inline std::ostream &operator<<(std::ostream &s, const Vector<T, N> &v)
+{
+       s << "Vector" << N << '(';
+       for(unsigned i=0; i<N; ++i)
+       {
+               if(i)
+                       s << ", ";
+               s << v[i];
+       }
+       s << ')';
+       return s;
+}
+
 } // namespace LinAl
 } // namespace Msp