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

📄 vector.h

📁 liu7788414
💻 H
字号:
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include "define.h"
#include "..\System\Sysdef.h"



class Vector4s
{

public:
	typedef long Type;

	Type x;
	Type y;
	Type z;
	Type s;

	inline Vector4s(){};
	inline Vector4s(Type ix,Type iy,Type iz):x(ix),y(iy),z(iz){};
	inline Vector4s(Type ix,Type iy,Type iz,Type is):x(ix),y(iy),z(iz),s(is){};

	inline void Init(const Vector4s *v2) { x = v2->x; y = v2->y; z = v2->z; };
	inline void Init(const Vector4s& v2) { x = v2.x; y = v2.y; z = v2.z; };

	inline void Init(Type x2, Type y2, Type z2) { x = x2; y = y2; z = z2; };
	inline void Init(Type x2, Type y2, Type z2,Type s2) { x = x2; y = y2; z = z2; s=s2;};
	inline void Copy(Vector4s *v2) { v2->x = x; v2->y = y;  v2->z = z; };	
	inline void Add(const Vector4s *v2) { x += v2->x; y += v2->y; z += v2->z; };
	inline void Add(Type x2, Type y2, Type z2) { x += x2; y += y2; z += z2; };
	inline void Sub(const Vector4s *v2) { x -= v2->x; y -= v2->y; z -= v2->z; };
	inline void Sub(Type x2, Type y2, Type z2) { x -= x2; y -= y2; z -= z2; };
  

	inline static int	Dot(const Vector4s& v1,const Vector4s& v2)	
	{
		CHK_MULT(v1.x,v2.x);
		CHK_MULT(v1.y,v2.y);
		CHK_MULT(v1.z,v2.z);
		return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; 
	};

	inline Type Lenght2() const {return Length2(); };
	inline int  Length2(void) const { return (int)x*x + (int)y*y + (int)z*z; };
	int	getLength() const;

	inline void GetDelta(const Vector4s *A, const Vector4s *B) { x = A->x - B->x; y = A->y - B->y; z = A->z - B->z; };
	inline int  EqualXYZ(const Vector4s *v2) const { return (x == v2->x) && (y == v2->y) && (z == v2->z); };

//	int  LinIndex(int Ix, int Iy, int Iz);                   // vector used as 3D array index
	void Scale(int s);
	void getMin(const Vector4s *v2);
	void getMax(const Vector4s *v2);
	void CrossShift(const Vector4s *V2, Vector4s *Res)const;      // use if one input vector is normalized
	void Normalize();                           // fast normalize, require to pass square root object ptr

	inline void Clear(){x=0;y=0;z=0;s=0;}


	Type	operator[](int i) const {SYS_ASSERT(i>=0 && i<3); return ((const Type*)(this))[i];}

	Vector4s & operator=( const Vector4s & src ) { x = src.x; y = src.y; z = src.z; s = src.s; return *this; }
	Vector4s & operator=( const Point3d & src ) { x = src.x; y = src.y; z = src.z; return *this; }
	Vector4s operator-() const {return Vector4s(-x,-y,-z,s);}

	bool operator==(const Vector4s& v) const {return x==v.x && y==v.y && z==v.z;}
	bool operator!=(const Vector4s& v) const {return x!=v.x || y!=v.y || z!=v.z;}

	void	operator+=(const Vector4s& v) {x+=v.x;y+=v.y;z+=v.z;}
	void	operator-=(const Vector4s& v) {x-=v.x;y-=v.y;z-=v.z;}

	void	operator/=(int i) {x/=i;y/=i;z/=i;}
	void	operator*=(int i) {	CHK_MULT(x,i);CHK_MULT(y,i);CHK_MULT(z,i);x*=i;y*=i;z*=i;}
	void	operator*=(const Vector4s& v) {	CHK_MULT(x,v.x);CHK_MULT(y,v.y);CHK_MULT(z,v.z);x*=v.z;y*=v.y;z*=v.z;}
	void	operator>>=(int i)	{x>>=i;y>>=i;z>>=i;}
	void	operator<<=(int i)	{x<<=i;y<<=i;z<<=i;}


	void			SelfRotateY(int);
	Vector4s		GetRotatedY(int) const;

    static Vector4s GetProjection(const Vector4s& v, const Vector4s& normal);
    static Vector4s GetReflexion(const Vector4s& v, const Vector4s& normal);
	
	static inline Vector4s Cross(const Vector4s &V1,const Vector4s &V2)
	{
		CHK_MULT(V1.x,V2.y);CHK_MULT(V1.y,V2.x);CHK_MULT(V1.y,V2.z);CHK_MULT(V1.z,V2.y);CHK_MULT(V1.z,V2.x);CHK_MULT(V1.x,V2.z);

		return Vector4s(	V1.y * V2.z - V1.z * V2.y,
							V1.z * V2.x - V1.x * V2.z,
							V1.x * V2.y - V1.y * V2.x);
	}

	static const int*	GetReciprocalAxis(int axis);
};





inline Vector4s operator+(const Vector4s& v1,const Vector4s& v2)
{
	return Vector4s(v1.x + v2.x,  v1.y+v2.y, v1.z+v2.z);
}


inline Vector4s operator-(const Vector4s& v1,const Vector4s& v2)
{
	return Vector4s(v1.x - v2.x,  v1.y-v2.y, v1.z-v2.z);
}

inline int operator*(const Vector4s& v1,const Vector4s& v2)
{
	CHK_MULT(v1.x,v2.x);
	CHK_MULT(v1.y,v2.y);
	CHK_MULT(v1.z,v2.z);
	
	return (v1.x * v2.x + v1.y*v2.y + v1.z*v2.z);
}

inline Vector4s operator*(int x,const Vector4s& v2)
{
	CHK_MULT(x,v2.x);
	CHK_MULT(x,v2.y);
	CHK_MULT(x,v2.z);
	
	return Vector4s(x * v2.x,  x*v2.y, x*v2.z);
}

inline Vector4s operator*(const Vector4s& v2,int x)
{
	return operator*(x,v2);
}

inline Vector4s operator/(const Vector4s& v2,int x)
{
	return Vector4s(v2.x/x , v2.y/x ,v2.z/x);
}

#endif // _VECTOR_H_

⌨️ 快捷键说明

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