⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xvector.inl

📁 XMathLib是一个通用的3D图形数学库。 其中包含两个部分: XMathLib和XGeomLib。分别处理数学和几何运算。 数学部分包含向量、矩阵、四元数的运算。以及其它的运算。 几何部分
💻 INL
字号:
#ifndef __XVECTOR_INLINE_INCLUDE__ 
#define __XVECTOR_INLINE_INCLUDE__ 

#ifndef IN_MATHLIB_NAMESPACE
#error   You cann't include this file out the XMathLib  namespace
#endif

    class  _MATH_LIB_EXPORT_ XVector
    {
    public:
        //Constructor build a vector
        XVector(float _x = 0,float _y = 0,float _z = 0,float _w = 1):x(_x),y(_y),z(_z),w(_w){};
        XVector(XVector2D& v){x = v.x ; y = v.y ; z = 0   ; w = 1 ;};
        XVector(XVector3D& v){x = v.x ; y = v.y ; z = v.z ; w = 1 ;};
        ~XVector(){};//Force a Null destorier

    public:
        float XVector::fabs()           {return (float) sqrt(x*x + y*y + z*z); }
        float XVector::dp3(XVector& v1) {return v1.x * x + v1.y * y + v1.z * z;}
        float XVector::dp4(XVector& v1) {return v1.x * x + v1.y * y + v1.z * z + v1.w * w;}
        void  XVector::add(XVector& v1) { x += v1.x; y += v1.y; z += v1.z; }
        void  XVector::sub(XVector& v1) { x += v1.x; y += v1.y; z += v1.z; }

        void       rhw(){ x/=w ; y/=w ; z/=w ; w=1; }//归一化
        float      len(){ return sqrt(x * x + y * y + z * z) ;}
        float      squardlen(){return (x * x + y * y + z * z) ;}

        //-------------------------------------------------------
        //叉积
        //-------------------------------------------------------
        XVector XVector::cp(XVector& v1)
        {
            float nx = y * v1.z - v1.y * z;
            float ny = z * v1.x - v1.z * x;
            float nz = x * v1.y - v1.x * y;
            return XVector(nx,ny,nz,1);
        }

        //-------------------------------------------------------
        //向量单位化
        //-------------------------------------------------------
        void XVector::normalize()
        {
            float len = (float) sqrt(x*x + y*y + z*z);

            if(len == 0)
                return ;
            x/=len;
            y/=len;
            z/=len;
            w = 1;
        };
        void XVector::normalize(XVector& out)
        {
            float len = (float) sqrt(x*x + y*y + z*z);
            if(len == 0)
                return ;
            out.x/=len;
            out.y/=len;
            out.z/=len;
            out.w = 1;
        };
    public:


        //-------------------------------------------------------
        //以下为重载的运算
        //让向量能和其它数字一样运算.
        //-------------------------------------------------------
        void    operator *=(float s){x*=s;y*=s;z*=s;};

        bool      operator ==(XVector& v) {return (v.x == x && v.y == y && v.z == z && v.w == w) ; }

        XVector XVector::operator -(XVector& v1)
        {
            return XVector(x - v1.x , y-v1.y ,z - v1.z, 1  );
        }

        XVector XVector::operator -()
        {
            return XVector(-x , -y ,- z, w );
        }

        XVector XVector::operator +(XVector& v1)
        {
            return XVector(x + v1.x , y + v1.y ,z + v1.z, 1  );
        }
        XVector XVector::operator *(float s)
        {
            return XVector(x * s,y * s,z * s , 1);
        }
        float   XVector::operator *(XVector& v1)
        {
            return (v1.x * x + v1.y * y + v1.z * z) ;
        }
        //-------------------------------------------------------
        //Cross product
        //-------------------------------------------------------
        XVector XVector::operator ^(XVector& v1)
        {
            float nx = y * v1.z - v1.y * z;
            float ny = z * v1.x - v1.z * x;
            float nz = x * v1.y - v1.x * y;
            return XVector(nx,ny,nz,1);
        }
        //-------------------------------------------------------
        //求夹角
        //-------------------------------------------------------
        float XVector::operator |(XVector& v1)
        {
            float t = dp3(v1)/(fabs() * v1.fabs());
            return (float)XM_ACosD(t);
        }
        //-------------------------------------------------------
        //是否平行
        //-------------------------------------------------------
        bool XVector::operator||(XVector& v1)
        {
            float nx = y * v1.z - v1.y * z;
            float ny = z * v1.x - v1.z * x;
            float nz = x * v1.y - v1.x * y;

            if(nx == 0 && ny == 0 && nz == 0)
                return true;
            else 
                return false;
        }
        //-------------------------------------------------------
        //向量夹角
        //-------------------------------------------------------
        float XVector::Clamp(XVector& v1)
        {
            float t = dp3(v1)/(fabs() * v1.fabs());
            return (float)XM_ACosD(t);
        }



    public://We set this data public is for easy access
        union
        {
            struct
            {
                float x;
                float y;
                float z;
                float w;//Four component the w is useful ......
            };
            //for texture coordinate
            struct
            {
                float s,t,r,q;
            };
            float v[4];
        };
    };
    typedef XVector XVector4D;
#endif



















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -