Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef APF_VECTOR_H
00009 #define APF_VECTOR_H
00010
00014 #include <cmath>
00015 #include "apfArray.h"
00016 #include <ostream>
00017
00018 namespace apf {
00019
00023 extern double const pi;
00024
00025 template <std::size_t N>
00026 class Vector;
00027
00045 template <std::size_t N>
00046 class Vector : public Array<double,N>
00047 {
00048 public:
00050 Vector() {}
00052 Vector(double const* v)
00053 {
00054 for (std::size_t i=0; i < N; ++i)
00055 (*this)[i] = v[i];
00056 }
00058 Vector<N> operator+(Vector<N> const& b) const
00059 {
00060 Vector<N> c;
00061 for (std::size_t i=0; i < N; ++i)
00062 c[i] = (*this)[i] + b[i];
00063 return c;
00064 }
00066 Vector<N>& operator+=(Vector<N> const& b)
00067 {
00068 for (std::size_t i=0; i < N; ++i)
00069 (*this)[i] += b[i];
00070 return (*this);
00071 }
00073 Vector<N> operator-(Vector<N> const& b) const
00074 {
00075 Vector<N> c;
00076 for (std::size_t i=0; i < N; ++i)
00077 c[i] = (*this)[i] - b[i];
00078 return c;
00079 }
00083 Vector<N> operator*(double s) const
00084 {
00085 Vector<N> c;
00086 for (std::size_t i=0; i < N; ++i)
00087 c[i] = (*this)[i] * s;
00088 return c;
00089 }
00092 Vector<N> operator/(double s) const
00093 {
00094 Vector<N> c;
00095 for (std::size_t i=0; i < N; ++i)
00096 c[i] = (*this)[i] / s;
00097 return c;
00098 }
00103 double operator*(Vector<N> const& b) const
00104 {
00105 double r=0;
00106 for (std::size_t i=0; i < N; ++i)
00107 r += (*this)[i] * b[i];
00108 return r;
00109 }
00111 double getLength() const {return sqrt((*this)*(*this));}
00113 Vector<N> normalize() const {return (*this) / getLength();}
00115 void zero()
00116 {
00117 for (std::size_t i=0; i < N; ++i)
00118 (*this)[i] = 0.0;
00119 }
00120 };
00121
00123 inline Vector<3> cross(Vector<3> const& a, Vector<3> const& b)
00124 {
00125 Vector<3> r;
00126 r[0] = a[1]*b[2] - a[2]*b[1];
00127 r[1] = a[2]*b[0] - a[0]*b[2];
00128 r[2] = a[0]*b[1] - a[1]*b[0];
00129 return r;
00130 }
00131
00133 template<std::size_t N>
00134 Vector<N> project(Vector<N> const& a, Vector<N> const& b)
00135 {
00136 return b*((a*b)/(b*b));
00137 }
00138
00140 template<std::size_t N>
00141 Vector<N> reject(Vector<N> const& a, Vector<N> const& b)
00142 {
00143 return a - project(a, b);
00144 }
00145
00150 class Vector3 : public Vector<3>
00151 {
00152 public:
00153 Vector3() {}
00154 Vector3(Vector<3> const& other):
00155 Vector<3>(other)
00156 {}
00159 Vector3(double a, double b, double c)
00160 {
00161 (*this)[0] = a;
00162 (*this)[1] = b;
00163 (*this)[2] = c;
00164 }
00167 Vector3(double const* abc)
00168 {
00169 (*this)[0] = abc[0];
00170 (*this)[1] = abc[1];
00171 (*this)[2] = abc[2];
00172 }
00175 void toArray(double* abc) const
00176 {
00177 abc[0] = (*this)[0];
00178 abc[1] = (*this)[1];
00179 abc[2] = (*this)[2];
00180 }
00183 void fromArray(const double* abc)
00184 {
00185 (*this)[0] = abc[0];
00186 (*this)[1] = abc[1];
00187 (*this)[2] = abc[2];
00188 }
00190 double x() const {return (*this)[0];}
00192 double y() const {return (*this)[1];}
00194 double z() const {return (*this)[2];}
00196 double& x() {return (*this)[0];}
00198 double& y() {return (*this)[1];}
00200 double& z() {return (*this)[2];}
00201 };
00202
00203 }
00204
00206 std::ostream& operator<<(std::ostream& s, apf::Vector3 const& v);
00207
00208 #endif