//====================================================================== // Copyright (C) 2002 Daniel Heck // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. //====================================================================== #ifndef PX_MATH_HH #define PX_MATH_HH #include #include namespace px { template class Vector { T v[N]; typedef Vector vector_type; protected: class noinit {}; Vector(noinit) {} public: // Constructors. Vector() { for (int i=0; i inline Vector operator- (const Vector &a) { Vector res(a); for (int i=0; i inline double length(const Vector &a) { return std::sqrt(a*a); } template inline Vector normalize(const Vector &a) { if (T aa=length(a)) return a/aa; else return a; } template inline Vector operator+ (const Vector & a, const Vector & b) { Vector res (a); res += b; return res; } template inline Vector operator- (const Vector & a, const Vector & b) { Vector res (a); res -= b; return res; } // scalar multiplication in different operand orders template inline Vector operator*(const Vector & v, double a) { Vector res (v); res *= a; return res; } template inline Vector operator*(double a, const Vector & v) { Vector res (v); res *= a; return res; } // scalar division in different operand orders template inline Vector operator/(const Vector & v, double a) { Vector res (v); res /= a; return res; } template inline Vector operator/(double a, const Vector & v) { Vector res (v); res /= a; return res; } template inline bool operator==(const Vector & v,const Vector & w) { for (int i=0; i { public: V3() {} V3(double x, double y, double z) : Vector(noinit()) { (*this)[0] = x; (*this)[1] = y; (*this)[2] = z; } V3(const Vector &v) : Vector(v) {} V3 &operator=(const Vector &v) { Vector::operator=(v); return *this; } }; //typedef Vector V3; typedef Vector V2; inline V3 crossprod(const V3 & a, const V3 & b) { V3 r; r[0] = a[1]*b[2] - a[2]*b[1]; r[1] = a[2]*b[0] - a[0]*b[2]; r[2] = a[0]*b[1] - a[1]*b[0]; return r; } inline V3 make_v3(double x, double y, double z) { V3 r; r[0] = x; r[1] = y; r[2] = z; return r; } inline V2 make_v2(double x, double y) { V2 r; r[0] = x; r[1] =y; return r; } std::ostream& operator<<(std::ostream& os, const V3 & v); std::ostream& operator<<(std::ostream& os, const V2 & v); } #endif