]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Redesign the Vector interface
[libs/math.git] / source / linal / vector.h
index 392e3a12772dcdc6e3b9f12dba937c7d38779e29..11292f7441dae7e7f69cefb58aadb73572f687e4 100644 (file)
@@ -8,22 +8,64 @@ 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];
 
-public:
-       Vector();
-       Vector(const T *d);
-       template<typename U>
-       Vector(const Vector<U, N> &v);
+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); }
+};
+
+/**
+A general mathematical vector.
+*/
+template<typename T, unsigned N>
+class Vector: public VectorComponents<T, N>
+{
+public:
+       Vector();
+       Vector(const T *);
+       Vector(T, T);
+       Vector(T, T, T);
+       template<typename U>
+       Vector(const Vector<U, N> &);
 
        Vector &operator*=(T);
        Vector &operator/=(T);
@@ -38,27 +80,47 @@ 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];
+}
+
+/* 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>
 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>
 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;
 }
 
@@ -79,7 +141,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;
 }
 
@@ -94,7 +156,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;
 }
 
@@ -109,7 +171,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;
 }
 
@@ -166,6 +228,18 @@ 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);
+}
+
 } // namespace LinAl
 } // namespace Msp