X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flinal%2Fvector.h;h=11292f7441dae7e7f69cefb58aadb73572f687e4;hb=03f70f6ba9622bb2840a14fc50ac72201cc533b5;hp=392e3a12772dcdc6e3b9f12dba937c7d38779e29;hpb=b60ee0fb060790277bfc68722f85a137a58ad771;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 392e3a1..11292f7 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -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 -class Vector +class VectorComponents { -protected: +private: T data[N]; -public: - Vector(); - Vector(const T *d); - template - Vector(const Vector &v); +protected: + VectorComponents() { } +public: T &operator[](unsigned i) { return data[i]; } const T &operator[](unsigned i) const { return data[i]; } +}; + +template +class VectorComponents +{ +public: + T x, y; + +protected: + VectorComponents() { } + +public: + T &operator[](unsigned i) { return *(&x+i); } + const T &operator[](unsigned i) const { return *(&x+i); } +}; + +template +class VectorComponents +{ +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 +class Vector: public VectorComponents +{ +public: + Vector(); + Vector(const T *); + Vector(T, T); + Vector(T, T, T); + template + Vector(const Vector &); Vector &operator*=(T); Vector &operator/=(T); @@ -38,27 +80,47 @@ public: template inline Vector::Vector() { - std::fill(data, data+N, T()); + for(unsigned i=0; i inline Vector::Vector(const T *d) { - std::copy(d, d+N, data); + for(unsigned i=0; i +inline Vector::Vector(T x_, T y_) +{ + this->VectorComponents::x = x_; + this->VectorComponents::y = y_; +} + +template +inline Vector::Vector(T x_, T y_, T z_) +{ + this->VectorComponents::x = x_; + this->VectorComponents::y = y_; + this->VectorComponents::z = z_; } template template inline Vector::Vector(const Vector &v) { - std::copy(v.data, v.data+N, data); + for(unsigned i=0; i inline Vector &Vector::operator*=(T s) { for(unsigned i=0; i inline Vector &Vector::operator/=(T s) { for(unsigned i=0; i inline Vector &Vector::operator+=(const Vector &v) { for(unsigned i=0; i inline Vector &Vector::operator-=(const Vector &v) { for(unsigned i=0; i normalize(const Vector &v) return r.normalize(); } +template +inline T dot(const Vector &v1, const Vector &v2) +{ + return inner_product(v1, v2); +} + +template +inline Vector cross(const Vector &v1, const Vector &v2) +{ + return Vector(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