//-*-c++-*- #ifndef VEC3_H #define VEC3_H /** * @file vec3.h * @brief Basic static 3d vector type, templated in type * @author James R. Diebel, Stanford University * * - History: * - 31 July 2005 - Started (JD) * - 30 Aug 2005 - Commented and tested (KH) * - 30 Aug 2005 forked off from matmath.h */ #include #include /** * @namespace sla * @brief Simple Linear Algebra - library of matrix and vector classes */ namespace sla { ///////////////////////////////////////////////////////////////////////////// // DECLARATIONS ///////////////////////////////////////////////////////////////////////////// // Forward declarations template class Vec; // M-d vector template class Vec3; // 3d Vector template class SMat3; // 3x3 square matrix //////////////////// Vec3 //////////////////// /** * A 3-D vector. */ template class Vec3: public Vec { public: // inherit member data and functions of parent using Vec::x; using Vec::set; // Constructors Vec3(); Vec3(const T* d); Vec3(const T a); Vec3(const T x0, const T x1, const T x2); Vec3(const Vec& v); // Cast Operation template Vec3(const Vec& v); // Mutators void set(const T x0, const T x1, const T x2); // Cross Product Vec3 cross(const Vec3& v) const; SMat3 crossMat() const; static SMat3 dcrossMat_dxj(const int j); }; // Declare a few common typdefs typedef Vec3 Vec3b; typedef Vec3 Vec3c; typedef Vec3 Vec3uc; typedef Vec3 Vec3i; typedef Vec3 Vec3f; typedef Vec3 Vec3d; //////////////////////////////////////////////////////////////////////////// // DEFINITIONS //////////////////////////////////////////////////////////////////////////// //////////////////// Vec3 //////////////////// // Constructors /** Ctor that doesn't initialize. */ template inline Vec3::Vec3() {} /** Ctor that intalizes from array. */ template inline Vec3::Vec3(const T* d) : Vec(d) {} /** Ctor that intalizes all elements from a scalar. */ template inline Vec3::Vec3(const T a) : Vec(a) {} /** Ctor that initializes vector with given values. */ template inline Vec3::Vec3(const T x0, const T x1, const T x2) { set(x0,x1,x2); } /** Ctor that initializes an Vec3 with an Vec. */ template inline Vec3::Vec3(const Vec& v) : Vec(v){} // Casting Operation /** Casting Ctor that initializes an Vec3 with a Vec. */ template template inline Vec3::Vec3(const Vec& v) : Vec(v) {} // Mutators /** Set vector. */ template inline void Vec3::set(const T x0, const T x1, const T x2) { x[0] = x0; x[1] = x1; x[2] = x2; } // Cross Product /** Calculate the cross product. * @param v a 3-vector * @return the cross product: "this" cross "v" */ template inline Vec3 Vec3::cross(const Vec3& v) const { return Vec3(x[1]*v.x[2]-x[2]*v.x[1], x[2]*v.x[0]-x[0]*v.x[2], x[0]*v.x[1]-x[1]*v.x[0]); } /** Calculate the cross product matrix * @return the cross product matrix */ template inline SMat3 Vec3::crossMat() const { return SMat3(0, -x[2], x[1], x[2], 0, -x[0], -x[1], x[0], 0); } /** Calculate the cross product matrix * @return the cross product matrix */ template inline SMat3 Vec3::dcrossMat_dxj(const int j) { switch(j) { case 0: return SMat3(T(0),T(0),T(0),T(0),T(0),-T(1),T(0),T(1),T(0)); case 1: return SMat3(T(0),T(0),T(1),T(0),T(0),T(0),-T(1),T(0),T(0)); case 2: return SMat3(T(0),-T(1),T(0),T(1),T(0),T(0),T(0),T(0),T(0)); } } } // end namespace sla #endif