//-*-c++-*- #ifndef VEC2_H #define VEC2_H /** * @file vec2.h * @brief Basic static 2d 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 Vec2; // 2d Vector //////////////////// Vec2 //////////////////// /** * A 2-D vector. */ template class Vec2: public Vec { public: // inherit member data and functions of parent using Vec::x; using Vec::set; // Constructors Vec2(); Vec2(const T* d); Vec2(const T a); Vec2(const T x0, const T x1); Vec2(const Vec& v); // Cast Operation template Vec2(const Vec& v); // Mutators void set(const T x0, const T x1); // Cross Product T cross(const Vec2& v) const; Vec2 cross(const T v) const; }; // Declare a few common typdefs typedef Vec2 Vec2b; typedef Vec2 Vec2c; typedef Vec2 Vec2uc; typedef Vec2 Vec2i; typedef Vec2 Vec2f; typedef Vec2 Vec2d; //////////////////////////////////////////////////////////////////////////// // DEFINITIONS //////////////////////////////////////////////////////////////////////////// //////////////////// Vec2 //////////////////// // Constructors /** Ctor that doesn't initialize. */ template inline Vec2::Vec2() { } /** Ctor that intalizes from array. */ template inline Vec2::Vec2(const T* d) : Vec(d) {} /** Ctor that intalizes all elements from a scalar. */ template inline Vec2::Vec2(const T a) : Vec(a) {} /** Ctor that initializes vector with given values. */ template inline Vec2::Vec2(const T x0, const T x1) { set(x0,x1); } /** Ctor that initializes an Vec2 with a Vec. */ template inline Vec2::Vec2(const Vec& v) : Vec(v) {} // Casting Operation /** Casting Ctor that initializes an Vec2 with a Vec. */ template template inline Vec2::Vec2(const Vec& v) : Vec(v) {} // Mutators /** Set vector. */ template inline void Vec2::set(const T x0, const T x1) { x[0] = x0; x[1] = x1; } // Cross Product /** Calculate the cross product. * @param v a 2-vector * @return the scalar cross product: "this" cross "v" */ template inline T Vec2::cross(const Vec2& v) const { return x[0]*v.x[1]-x[1]*v.x[0]; } /** Calculate the cross product. * @param v a scalar representing an orthogonal vector * @return the cross product: "this" cross "v" */ template inline Vec2 Vec2::cross(T v) const { return Vec2(v*x[1],-v*x[0]); } } // end namespace sla #endif