00001 #ifndef _VEC_HPP_
00002 #define _VEC_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 namespace math
00015 {
00016
00019 template<typename T, int S> class Vec;
00020
00021 template<typename T, int S>
00022 struct Traits<Vec<T, S> >
00023 {
00024 Vec<T, S> static zero() { return Vec<T, S>(Vec<T, S>::ValTrait::zero()); };
00025 Vec<T, S> static one() { return Vec<T, S>(Vec<T, S>::ValTrait::one()); };
00026 };
00027
00028
00029
00030
00031 namespace vector
00032 {
00035 template<typename T, int S> inline
00036 Vec<T, S>& assign(Vec<T, S>& v, const T& scalar);
00037
00038 template<typename T, int S> inline
00039 Vec<T, S>& assign(Vec<T, S>& v, const T* valptr);
00040
00041 template<typename T, int S> inline
00042 Vec<T, S>& assign(Vec<T, S>& v, const Vec<T, S>& vec);
00044
00045
00047
00049 template<typename T, int S> inline
00050 Vec<T, S> add(const Vec<T, S>& v, const Vec<T, S>& vec);
00051
00053 template<typename T, int S> inline
00054 Vec<T, S>& addOnSpot(Vec<T, S>& v, const Vec<T, S>& vec);
00055
00057 template<typename T, int S> inline
00058 Vec<T, S> sub(const Vec<T, S>& v, const Vec<T, S>& vec);
00059
00061 template<typename T, int S> inline
00062 Vec<T, S>& subOnSpot(Vec<T, S>& v, const Vec<T, S>& vec);
00063
00064
00066 template<typename T, int S> inline
00067 Vec<T, S>& multOnSpot(Vec<T, S>& vec, const T& scalar);
00068
00070 template<typename T, int S> inline
00071 Vec<T, S> mult(const Vec<T, S>& vec, const T& scalar);
00072
00074 template<typename T, int S> inline
00075 Vec<T, S>& divideOnSpot(Vec<T, S>& vec, const T& scalar);
00076
00078 template<typename T, int S> inline
00079 Vec<T, S> divide(const Vec<T, S>& vec, const T& scalar);
00080
00082 template<typename T, int S> inline
00083 T norm2(const Vec<T, S>& vec);
00084
00086 template<typename T, int S> inline
00087 T norm(const Vec<T, S>& vec);
00088
00090 template<typename T, int S> inline
00091 Vec<T, S>& normalize(Vec<T, S>& vec);
00092
00094 template<typename T> inline
00095 Vec<T, 3> cross(const Vec<T, 3>& v, const Vec<T, 3>& vec);
00096
00098 template<typename T, int S> inline
00099 T dot(const Vec<T, S>& vec);
00100
00102
00103 };
00104
00105 template<typename T, int S>
00106 class Vec : public Chunk<T, S>
00107 {
00108 public:
00109
00110 typedef T ValType;
00111 typedef const T ConstValType;
00112 typedef T* ValTypePtr;
00113 typedef const T* ConstValTypePtr;
00114
00115 typedef Vec<ValType, S> VecType;
00116 typedef const Vec<ValType, S> ConstVecType;
00117
00118 typedef Traits<Vec<T, S> > Trait;
00119 typedef Traits<T> ValTrait;
00120
00121 public:
00122
00123
00124
00127 Vec() {};
00128 Vec(ConstValType& scalar) { operator=(scalar); }
00129 Vec(ConstValTypePtr valptr) { operator=(valptr); }
00130 Vec(ConstVecType& vec) { operator=(vec); }
00131
00134 Vec(ConstValType& x, ConstValType& y, ConstValType& z);
00135
00137
00138
00139
00140
00143 VecType& operator=(ConstValType& scalar);
00144 VecType& operator=(ConstValTypePtr valptr);
00145 VecType& operator=(ConstVecType& vec);
00146
00147 VecType operator+(ConstVecType& vec) const;
00148 VecType operator-(ConstVecType& vec) const;
00149
00150 VecType& operator+=(ConstVecType& vec);
00151 VecType& operator-=(ConstVecType& vec);
00152
00153 VecType operator*(ConstValType& scalar) const;
00154 VecType operator/(ConstValType& scalar) const;
00155
00156 VecType& operator*=(ConstValType& scalar);
00157 VecType& operator/=(ConstValType& scalar);
00158
00160
00161
00162
00163
00164
00166 ValType norm2() const;
00167
00169 ValType norm() const;
00170
00172 VecType& normalize();
00173
00174 };
00175
00176
00179 template<typename T>
00180 struct Cross
00181 {
00182 Vec<T, 3> operator()(const Vec<T, 3>& a, const Vec<T, 3>& b) const
00183 {
00184 return Vec<T, 3>(
00185 a.val[1]*b.val[2] - a.val[2]*b.val[1],
00186 a.val[2]*b.val[0] - a.val[0]*b.val[2],
00187 a.val[0]*b.val[1] - a.val[1]*b.val[0]
00188 );
00189 };
00190 };
00191
00194 template<typename T, int S>
00195 struct Dot
00196 {
00197 T operator()(const Vec<T, S>& a, const Vec<T, S>& b)
00198 {
00199 T r= Vec<T, S>::ValTrait::zero();
00200 for(typename Vec<T, S>::ConstValTypePtr pv= a.begin(), sv= b.begin(); pv!=a.end(); pv++, sv++)
00201 r+= *pv * *sv;
00202 return r;
00203 };
00204 };
00205
00206 #include<ak/vec.inl>
00207
00208 };
00209
00210 #endif // _VEC_HPP_
00211