afVec3.h
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:6k
源码类别:
其他游戏
开发平台:
Visual C++
- #ifndef AF_VEC3
- #define AF_VEC3
- #include <assert.h>
- class afPlane;
- /// 3D vector class
- class afVec3
- {
- public:
- float x, y, z;
- //constructors
- afVec3() { x=y=z=0.0f; }
- afVec3(float nX, float nY, float nZ ) { x=nX; y=nY; z=nZ; }
- afVec3(const afVec3& tocopy) { *this = tocopy; }
- afVec3(const float *nVec) { set(nVec); }
- void
- set(const float *nVec) { x = nVec[0];y = nVec[1];z = nVec[2]; }
- void
- set(float nX, float nY, float nZ) { x = nX; y = nY; z = nZ; }
- void
- setLength(float nLength) { (*this)*=(nLength/length()); }
- const float&
- getX() const { return x; }
- const float&
- getY() const { return y; }
- const float&
- getZ() const { return z; }
- float
- length() const;
- float
- sqrLength() const { return x*x + y*y + z*z; }
- float
- normalize();
- afVec3&
- cross( const afVec3& v1, const afVec3& v2 );
- static float
- cosine(const afVec3& left,const afVec3& right);
- /// Returns the dot product between this vec3 and nV
- inline float
- dot(const afVec3& nV) const;
- inline bool
- isInfrontOf(const afPlane& nPlane) const;
- //returns one normal vector
- inline afVec3
- normal();
- inline void
- negate() { x = -x; y = -y; z = -z; }
- inline afVec3&
- add(const afVec3& v1, const afVec3& v2);
- inline afVec3&
- addScaled(const afVec3& v1, float s, const afVec3& v2);
- inline afVec3&
- operator=( const afVec3& );
- inline afVec3&
- operator+=( const afVec3& );
- inline afVec3&
- operator-=( const afVec3& );
- inline afVec3&
- operator*=( const afVec3& );
- inline afVec3&
- operator*=( float mult );
- inline afVec3&
- operator/=( float div );
- inline afVec3
- operator+( float value );
- inline afVec3
- operator-( float value );
- inline afVec3
- operator*( float value );
- friend inline bool
- operator==(const afVec3& left, const afVec3& right);
- friend inline bool
- operator<(const afVec3& left, const afVec3& right);
- friend inline bool
- operator>(const afVec3& left, const afVec3& right);
- friend inline bool
- operator<=(const afVec3& left, const afVec3& right);
- friend inline bool
- operator>=(const afVec3& left, const afVec3& right);
- static float
- distance(const afVec3& left, const afVec3& right);
- friend inline const afVec3
- operator+(const afVec3& left, const afVec3& right);
- friend inline const afVec3
- operator-(const afVec3& left, const afVec3& right);
- friend inline const afVec3
- operator+(const afVec3& right);
- friend inline const afVec3
- operator-(const afVec3& right);
- friend inline float
- operator*(const afVec3& left, const afVec3& right);
- friend inline float
- operator/(const afVec3& left, const afVec3& right);
- friend inline afVec3
- operator*(const afVec3& left, float right);
- friend inline afVec3
- operator/(const afVec3& left, float right );
- const float&
- operator[](unsigned index) const { return (&x)[index]; }
- float&
- operator[](unsigned index) { return (&x)[index]; }
- };
- /// Direction in euler values
- typedef afVec3 afEuler;
- typedef afVec3 *afVec3Ptr;
- inline
- afVec3& afVec3::add(const afVec3& vec1, const afVec3& vec2)
- {
- x = vec1.x+vec2.x;
- y = vec1.y+vec2.y;
- z = vec1.z+vec2.z;
- return *this;
- }
- inline
- afVec3& afVec3::addScaled(const afVec3& vec1, float s, const afVec3& vec2)
- {
- x = vec1.x+s*vec2.x;
- y = vec1.y+s*vec2.y;
- z = vec1.z+s*vec2.z;
- return *this;
- }
- inline afVec3&
- afVec3::operator=( const afVec3& other)
- {
- x = other.x;
- y = other.y;
- z = other.z;
- return *this;
- }
- inline afVec3&
- afVec3::operator+=(const afVec3& vec)
- {
- x += vec.x;
- y += vec.y;
- z += vec.z;
- return *this;
- }
- inline
- afVec3& afVec3::operator-=(const afVec3& vec)
- {
- x -= vec.x;
- y -= vec.y;
- z -= vec.z;
- return *this;
- }
- inline
- afVec3& afVec3::operator*=(float scale)
- {
- x *= scale;
- y *= scale;
- z *= scale;
- return *this;
- }
- inline afVec3&
- afVec3::operator*=( const afVec3& other)
- {
- x *= other.x;
- y *= other.y;
- z *= other.z;
- return *this;
- }
- inline afVec3&
- afVec3::operator/=(float div)
- {
- x /= div;
- y /= div;
- z /= div;
- return *this;
- }
- inline bool
- operator==(const afVec3& left, const afVec3& right)
- {
- return ( left.x == right.x
- && left.y == right.y
- && left.z == right.z );
- }
- inline bool
- operator<(const afVec3& left, const afVec3& right)
- {
- return ( left.x < right.x
- && left.y < right.y
- && left.z < right.z );
- }
- inline bool
- operator>(const afVec3& left, const afVec3& right)
- {
- return ( left.x > right.x
- && left.y > right.y
- && left.z > right.z );
- }
- inline bool
- operator<=(const afVec3& left, const afVec3& right)
- {
- return ( left.x <= right.x
- && left.y <= right.y
- && left.z <= right.z );
- }
- inline bool
- operator>=(const afVec3& left, const afVec3& right)
- {
- return ( left.x >= right.x
- && left.y >= right.y
- && left.z >= right.z );
- }
- inline const afVec3
- operator+(const afVec3& left, const afVec3& right)
- {
- return afVec3(left.x+right.x,left.y+right.y,left.z+right.z);
- }
- inline const afVec3
- operator-(const afVec3& left, const afVec3& right)
- {
- return afVec3(left.x-right.x,left.y-right.y,left.z-right.z);
- }
- inline const afVec3
- operator+(const afVec3& right)
- {
- return afVec3(right.x,right.y,right.z);
- }
- inline const afVec3
- operator-(const afVec3& right)
- {
- return afVec3(-right.x,-right.y,-right.z);
- }
- inline float
- operator*(const afVec3& left, const afVec3& right)
- {
- return left.x*right.x + left.y*right.y + left.z*right.z;
- }
- inline float
- operator/(const afVec3& left, const afVec3& right)
- {
- return left.x/right.x + left.y/right.y + left.z/right.z;
- }
- inline afVec3
- afVec3::operator+( float value )
- {
- return afVec3(x+value,y+value,z+value);
- }
- inline afVec3
- afVec3::operator-( float value )
- {
- return afVec3(x-value,y-value,z-value);
- }
- inline afVec3
- afVec3::operator*( float value )
- {
- return afVec3(x*value,y*value,z*value);
- }
- inline afVec3
- operator*( const afVec3& left, float value )
- {
- return afVec3(left.x*value,left.y*value,left.z*value);
- }
- inline afVec3
- operator/( const afVec3& left, float value )
- {
- assert(value!=0.0f);
- return afVec3(left.x/value,left.y/value,left.z/value);
- }
- #endif