]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Improve Vector constructor for C++11
[libs/math.git] / source / linal / vector.h
index 11292f7441dae7e7f69cefb58aadb73572f687e4..31597d4282410e2606590eaea2faa6d3c22c2cd9 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <algorithm>
 #include <cmath>
+#include <ostream>
 
 namespace Msp {
 namespace LinAl {
@@ -53,6 +54,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.
 */
@@ -60,13 +75,31 @@ template<typename T, unsigned N>
 class Vector: public VectorComponents<T, N>
 {
 public:
+       typedef T ElementType;
+
        Vector();
        Vector(const T *);
+
+       /** Constructs a vector from an array of interleaved values.  Intended for
+       use by Matrix row accessor. */
+       Vector(const T *, unsigned);
+
+#if __cplusplus >= 201103L
+       template<typename... Args>
+       Vector(T, Args...);
+#else
        Vector(T, T);
        Vector(T, T, T);
+       Vector(T, T, T, T);
+#endif
        template<typename U>
        Vector(const Vector<U, N> &);
 
+       unsigned size() const { return N; }
+
+       template<unsigned M>
+       Vector<T, M> slice(unsigned) const;
+
        Vector &operator*=(T);
        Vector &operator/=(T);
        Vector &operator+=(const Vector &);
@@ -91,6 +124,25 @@ inline Vector<T, N>::Vector(const T *d)
                (*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];
+}
+
+#if __cplusplus >= 201103L
+template<typename T, unsigned N>
+template<typename... Args>
+inline Vector<T, N>::Vector(T x_, Args... v)
+{
+       static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor");
+       (*this)[0] = x_;
+       unsigned i = 1;
+       for(auto c: std::initializer_list<T> { static_cast<T>(v)... })
+               (*this)[i++] = c;
+}
+#else
 /* 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>
@@ -108,6 +160,16 @@ 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_;
+}
+#endif
+
 template<typename T, unsigned N>
 template<typename U>
 inline Vector<T, N>::Vector(const Vector<U, N> &v)
@@ -116,6 +178,47 @@ 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;
+       r[0] = s;
+       for(unsigned i=0; i<N; ++i)
+               r[i+1] = v[i];
+       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)
 {
@@ -212,6 +315,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));
 }
 
@@ -240,6 +344,20 @@ 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