Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef MTH_VECTOR_H
00012 #define MTH_VECTOR_H
00013
00014 #include "canArray.h"
00015 #include <cmath>
00016 #include <ostream>
00017
00021 namespace mth {
00022
00028 template <class T, unsigned N=0>
00029 class Vector : public can::Array<T,N>
00030 {
00031 public:
00033 Vector() {}
00037 Vector(unsigned n) {(void)n;}
00039 Vector(T const* v)
00040 {
00041 for (unsigned i=0; i < N; ++i)
00042 (*this)[i] = v[i];
00043 }
00047 T& operator()(unsigned i) {return (*this)[i];}
00049 T const& operator()(unsigned i) const {return (*this)[i];}
00051 Vector<T,N>& operator+=(Vector<T,N> const& b)
00052 {
00053 for (unsigned i=0; i < N; ++i)
00054 (*this)[i] += b[i];
00055 return (*this);
00056 }
00058 Vector<T,N> operator+(Vector<T,N> const& b) const
00059 {
00060 Vector<T,N> r;
00061 for (unsigned i=0; i < N; ++i)
00062 r[i] = (*this)[i] + b[i];
00063 return r;
00064 }
00066 Vector<T,N>& operator-=(Vector<T,N> const& b)
00067 {
00068 for (unsigned i=0; i < N; ++i)
00069 (*this)[i] -= b[i];
00070 return (*this);
00071 }
00073 Vector<T,N> operator-(Vector<T,N> const& b) const
00074 {
00075 Vector<T,N> r;
00076 for (unsigned i=0; i < N; ++i)
00077 r[i] = (*this)[i] - b[i];
00078 return r;
00079 }
00084 Vector<T,N> operator*(T const& s) const
00085 {
00086 Vector<T,N> r;
00087 for (unsigned i=0; i < N; ++i)
00088 r[i] = (*this)[i] * s;
00089 return r;
00090 }
00093 Vector<T,N> operator/(T const& s) const
00094 {
00095 Vector<T,N> r;
00096 for (unsigned i=0; i < N; ++i)
00097 r[i] = (*this)[i] / s;
00098 return r;
00099 }
00104 T operator*(Vector<T,N> const& b) const
00105 {
00106 T r = (T)0.0;
00107 for (unsigned i=0; i < N; ++i)
00108 r += (*this)[i] * b[i];
00109 return r;
00110 }
00112 T getLength() const {return sqrt((*this)*(*this));}
00114 Vector<T,N> normalize() const {return (*this)/getLength();}
00116 void zero()
00117 {
00118 for (unsigned i=0; i < N; ++i)
00119 (*this)[i] = (T)0.0;
00120 }
00121 };
00122
00131 template <class T>
00132 class Vector<T,0> : public can::Array<T,0>
00133 {
00134 public:
00136 Vector() {}
00138 Vector(unsigned n) : can::Array<T>(n) {}
00142 T& operator()(unsigned i) {return (*this)[i];}
00144 T const& operator()(unsigned i) const {return (*this)[i];}
00146 Vector<T,0>& operator+=(Vector<T,0> const& b)
00147 {
00148 for (unsigned i=0; i < this->sz; ++i)
00149 (*this)[i] += b[i];
00150 return *this;
00151 }
00153 Vector<T,0>& operator-=(Vector<T,0> const& b)
00154 {
00155 for (unsigned i=0; i < this->sz; ++i)
00156 (*this)[i] -= b[i];
00157 return *this;
00158 }
00160 Vector<T,0>& operator*=(T const& s)
00161 {
00162 for (unsigned i=0; i < this->sz; ++i)
00163 (*this)[i] *= s;
00164 return *this;
00165 }
00167 Vector<T,0>& operator/=(T const& s)
00168 {
00169 for (unsigned i=0; i < this->sz; ++i)
00170 (*this)[i] *= s;
00171 return *this;
00172 }
00174 T getLength() const {return sqrt((*this)*(*this));}
00176 void zero()
00177 {
00178 for (unsigned i=0; i < this->sz; ++i)
00179 (*this)[i] = (T)0.0;
00180 }
00181 double operator*(Vector<T,0> const& b)
00182 {
00183 double s = 0;
00184 for (unsigned i=0; i < this->sz; ++i)
00185 s += (*this)[i] * b[i];
00186 return s;
00187 }
00188 };
00189
00194 template <class T>
00195 class Vector3 : public Vector<T,3>
00196 {
00197 public:
00199 Vector3() {}
00201 Vector3(Vector<T,3> const& other) : Vector<T,3>(other) {};
00203 Vector3(T const& a, T const& b, T const& c)
00204 {
00205 (*this)[0] = a;
00206 (*this)[1] = b;
00207 (*this)[2] = c;
00208 }
00210 Vector3(T const* abc)
00211 {
00212 (*this)[0] = abc[0];
00213 (*this)[1] = abc[1];
00214 (*this)[2] = abc[2];
00215 }
00217 void toArray(T* abc) const
00218 {
00219 abc[0] = (*this)[0];
00220 abc[1] = (*this)[1];
00221 abc[2] = (*this)[2];
00222 }
00224 void fromArray(T const* abc)
00225 {
00226 (*this)[0] = abc[0];
00227 (*this)[1] = abc[1];
00228 (*this)[2] = abc[2];
00229 }
00231 T& x() {return (*this[0]);}
00233 T& y() {return (*this[1]);}
00235 T& z() {return (*this[2]);}
00237 T const& x() const {return (*this)[0];}
00239 T const& y() const {return (*this)[1];}
00241 T const& z() const {return (*this)[2];}
00242 };
00243
00244 }
00245
00246 template <class T, unsigned N>
00247 std::ostream& operator<<(std::ostream& s, mth::Vector<T,N> const& a)
00248 {
00249 for (unsigned i=0; i < a.size(); ++i)
00250 s << a[i] << ' ';
00251 s << '\n';
00252 return s;
00253 }
00254
00255 #endif