X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fvector.h;h=11292f7441dae7e7f69cefb58aadb73572f687e4;hb=aa47cdf95914ae706b99a48d528260b3261f9882;hp=933611eca6d63096f22b3ce9375c8b09a04f1d3e;hpb=839a88cd2b9a2f054323627c4e76cdcefc652c63;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 933611e..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]; +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 *d); + Vector(const T *); + Vector(T, T); + Vector(T, T, T); template - Vector(const Vector &v); - - T &operator[](unsigned i); - const T &operator[](unsigned i) const; + Vector(const Vector &); Vector &operator*=(T); Vector &operator/=(T); @@ -38,39 +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 -template -inline Vector::Vector(const Vector &v) +inline Vector::Vector(T x_, T y_) { - std::copy(v.data, v.data+N, data); + this->VectorComponents::x = x_; + this->VectorComponents::y = y_; } template -T &Vector::operator[](unsigned i) +inline Vector::Vector(T x_, T y_, T z_) { - return data[i]; + this->VectorComponents::x = x_; + this->VectorComponents::y = y_; + this->VectorComponents::z = z_; } template -const T &Vector::operator[](unsigned i) const +template +inline Vector::Vector(const Vector &v) { - return data[i]; + 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 operator-(const Vector &v) return r; } +template +inline bool operator==(const Vector &v, const Vector &w) +{ + for(unsigned i=0; i inline T inner_product(const Vector &v1, const Vector &v2) { @@ -169,6 +228,18 @@ inline Vector 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