📄 as_vector.hxx
字号:
// Header for vector.//// A vector represents a displacement in three-dimensional // cartesian space. It is subject to transformations as well as the // usual vector operations.// We define several different types of vector-like entities, and// will only permit meaningful combinations of them.#if !defined( AS_VECTOR_CLASS )#define AS_VECTOR_CLASS
// Declare classes to avoid forward references#define logical int
extern double resabs; // this may (will) be variable
extern double resnor; // ditto. They also depend on whether "real"
extern double resfit;
extern double resmch;
extern int init_count;
class unit_vector;class position;class matrix;class transf;class vector{ double comp[ 3 ]; // the x, y and z components of the vectorpublic:// MMGR_SUPPORT_THIS#if defined( osf1 ) vector() { comp[0] = 0.0 ; comp[1] = 0.0 ; comp[2] = 0.0 ; }#else vector() {} // allow uninitialised vectors#endif // Construct a vector from three doubles. vector( double x, double y, double z ) { comp[ 0 ] = x; comp[ 1 ] = y; comp[ 2 ] = z; } // Construct a vector from an array of three doubles. vector( double v[ 3 ] ) { comp[ 0 ] = v[ 0 ]; comp[ 1 ] = v[ 1 ]; comp[ 2 ] = v[ 2 ]; } // Copy a vector. vector( vector const &v ) { comp[ 0 ] = v.comp[ 0 ]; comp[ 1 ] = v.comp[ 1 ]; comp[ 2 ] = v.comp[ 2 ]; } // Extract the components of a vector. double x() const { return comp[ 0 ]; } double y() const { return comp[ 1 ]; } double z() const { return comp[ 2 ]; } double component( int i ) const { return comp[ i ]; } // Extract the components of a vector for update double &x() { return comp[ 0 ]; } double &y() { return comp[ 1 ]; } double &z() { return comp[ 2 ]; } double &component( int i ) { return comp[ i ]; } // Set component values. void set_x( double new_x ) { comp[ 0 ] = new_x; } void set_y( double new_y ) { comp[ 1 ] = new_y; } void set_z( double new_z ) { comp[ 2 ] = new_z; } void set_component( int i, double new_c ) { comp[ i ] = new_c; } // Unary minus. friend vector operator-( vector const & ); // Addition of vectors. friend vector operator+( vector const &, vector const & ); vector const &operator+=( vector const & ); // Binary minus. friend vector operator-( vector const &, vector const & ); vector const &operator-=( vector const & ); // Scalar product of two vectors. friend double operator%( vector const &, vector const & ); // Scalar product of a position. friend double operator%( position const &, vector const & ); friend double operator%( vector const &, position const & ); // Cross product of general vectors. Also applies to unit vectors. friend vector operator*( vector const &, vector const & ); // Multiplication of a vector by a scalar. friend vector operator*( double, vector const & ); friend vector operator*( vector const &, double ); vector const &operator*=( double ); // Division of a vector by a scalar. friend vector operator/( vector const &, double ); // scalar division vector const &operator/=( double ); // Length of a vector. double len_sq() const; // v.len_sq() = v%v; double len() const; // v.len() = sqrt(v%v); // This is a method that is more expensive than len(), but gives // (theoretically) the exact same value and is stable for VERY // small (those for which v%v would be lost in numerical noise) // or VERY large norms (those for which v%v would give overflow). // Don't use this generally. double numerically_stable_len() const; // this gets the max of the fabs of each component, and tells (via // "int& i") which component was the max. Note that in case of a "tie" // the index "i" will default to the larger index. That is for the // vector (1,1,1) "i" will be "2". Note that the two norms "max_norm(i)" // and "len()" define equivalent topologies on R3 due to the inequality: // v.len() <= sqrt(3) * v.max_norm() <= sqrt(3) * v.len() double max_norm( int& i ) const; // This will return some unit_vector which is orthogonal to the // given one. If the given vector is less than resmch in lenght, // it returns the unit vector (0,0,1). unit_vector orthogonal() const; // Transform a vector by a 3x3 matrix. friend vector operator*( matrix const &, vector const & ); friend vector operator*( vector const &, matrix const & ); vector const &operator*=( matrix const & ); // Transform a vector i.e. by an affine transformation. friend vector operator*( vector const &, transf const & ); friend vector operator*( vector const &, transf const * ); vector const &operator*=( transf const & ); // Form a unit_vector by normalising a vector. friend unit_vector normalise( vector const & ); // STI aed: make method from function in sg_husk/inter/vec_util.cxx vector make_ortho(); // STI aed: end // Determine if 2 vectors are equal, given some resolution friend bool same_vector( vector const&, vector const&, const double res = resabs); // Determine if 2 vectors are parallel (within some resolution) friend bool parallel( vector const &, vector const &, const double res = resnor ); friend bool parallel( unit_vector const &, vector const &, const double res = resnor ); friend bool parallel( unit_vector const &, unit_vector const &, const double res = resnor ); // Determine if 2 vectors are anti-parallel (within some resolution) friend bool antiparallel( vector const &, vector const &, const double res = resnor ); friend bool antiparallel( unit_vector const &, vector const &, const double res = resnor ); friend bool antiparallel( unit_vector const &, unit_vector const &, const double res = resnor ); // Determine if 2 vectors are bi-parallel // i.e., either parallel or anti-parallel (within some resolution) friend bool biparallel( vector const &, vector const &, const double res = resnor ); friend bool biparallel( unit_vector const &, vector const &, const double res = resnor ); friend bool biparallel( unit_vector const &, unit_vector const &, const double res = resnor ); // Determine if 2 vectors are perpendicular (within some resolution) friend bool perpendicular( vector const &, vector const &, const double res = resnor ); friend bool perpendicular( unit_vector const &, vector const &, const double res = resnor ); friend bool perpendicular( unit_vector const &, unit_vector const &, const double res = resnor ); // STI let (3/97): add a new member function // Determine if a vector is a zero vector, // i.e., its length is less than a given tolerance bool is_zero( const double tol = resabs ) const; // STI let: end // Output vector.};// STI let (7/96): Some global inline operators and functions// Same description as above; these are just inline functions// Determine if 2 vectors are within resabs of each otherinline bool operator==( vector const &v1, vector const &v2 ) { return same_vector( v1, v2, resabs ); }inline bool operator!=( vector const &v1, vector const &v2 ) { return !same_vector( v1, v2, resabs ); }inline bool parallel( vector const &v1, unit_vector const &v2, const double res = resnor ) { return parallel( v2, v1, res ); }inline bool antiparallel( vector const &v1, unit_vector const &v2, const double res = resnor ) { return antiparallel( v2, v1, res ); }inline bool biparallel( vector const &v1, unit_vector const &v2, const double res = resnor ) { return biparallel( v2, v1, res ); }inline bool perpendicular( vector const &v1, unit_vector const &v2, const double res = resnor ) { return perpendicular( v2, v1, res ); }// STI let: end// Useful constant vector#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -