www.pudn.com > zfxcengine-0.1.0.zip > ceVec4f.cpp


// $Id: ceVec4f.cpp,v 1.8 2005/09/12 11:58:37 andreaskohn Exp $ 
#include  
 
#include "Math/ceMath.h" 
#include  
 
namespace ZFXCE 
{ 
	//////////////////////////////////////////////////////////////////////////////// 
	FLOAT _fabs(FLOAT f) 
	{ 
		if(f < 0.0f) 
			return -f; 
	 
		return f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Set(const FLOAT X, const FLOAT Y, const FLOAT Z, const FLOAT W) 
	{ 
		x = X; 
		y = Y; 
		z = Z; 
		w = W; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	FLOAT ceVec4f::GetLength() const 
	{ 
		return (FLOAT) sqrt(x*x + y*y + z*z);; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	FLOAT ceVec4f::GetSqrLength() const 
	{ 
		return (FLOAT) (x*x) + (y*y) + (z*z); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Negate() 
	{ 
		x = -x; 
		y = -y; 
		z = -z; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Normalize() 
	{ 
		const FLOAT f = GetLength(); 
 
		if(f != 0.0f) 
		{ 
			x /= f; 
			y /= f; 
			z /= f; 
		} 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	FLOAT ceVec4f::AngleWith(ceVec4f& v) const 
	{ 
		return (float)acos( ((*this) * v) / (this->GetLength()*v.GetLength()) ); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Difference(const ceVec4f& v1, const ceVec4f& v2) 
	{ 
		x = v2.x - v1.x; 
		y = v2.y - v1.y; 
		z = v2.z - v2.z; 
		w = 1.0f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Cross(const ceVec4f& v1, const ceVec4f& v2) 
	{ 
		x = v1.y * v2.z - v1.z * v2.y; 
		y = v1.z * v2.x - v1.x * v2.z; 
		z = v1.x * v2.y - v1.y * v2.x; 
		w = 1.0f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::RotateWith(const ceMatrix& m) 
	{ 
		x = x * m._ms._11 + y * m._ms._21 + z * m._ms._31; 
		y = x * m._ms._12 + y * m._ms._22 + z * m._ms._32; 
		z = x * m._ms._13 + y * m._ms._23 + z * m._ms._33; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::RotateInverseWith(const ceMatrix& m) 
	{ 
		x = x * m._ms._11 + y * m._ms._12 + z * m._ms._13; 
		y = x * m._ms._21 + y * m._ms._22 + z * m._ms._23; 
		z = x * m._ms._31 + y * m._ms._32 + z * m._ms._33; 
	} 
	//////////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Add(const ceVec4f &v) 
	{ 
		x += v.x; 
		y += v.y; 
		z += v.z; 
	} 
	//////////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::Dump() 
	{ 
		std::cout << " x = " << x << std::endl; 
		std::cout << " y = " << y << std::endl; 
		std::cout << " z = " << z << std::endl; 
		std::cout << " w = " << w << std::endl; 
	} 
	//////////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator += (const ceVec4f& v) 
	{ 
		x += v.x; 
		y += v.y; 
		z += v.z; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator +(const ceVec4f& v) const 
	{ 
		return ceVec4f(x+v.x, y+v.y, z+v.z); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator -=(const ceVec4f& v) 
	{ 
		x -= v.x; 
		y -= v.y; 
		z -= v.z; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator -(const ceVec4f& v)const 
	{ 
		return ceVec4f(x-v.x, y-v.y, z-v.z); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator *= (const FLOAT f) 
	{ 
		x *= f; 
		y *= f; 
		z *= f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator /= (const FLOAT f) 
	{ 
		if (f!=0.0f) 
		{ 
			x /= f; 
			y /= f; 
			z /= f; 
		} 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator * (const FLOAT f)const 
	{ 
		return ceVec4f(x*f, y*f, z*f); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator /(const FLOAT f)const 
	{ 
		assert(0.0f != f); 
		return ceVec4f(x/f, y/f, z/f); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator += (const FLOAT f) 
	{ 
		x += f; 
		y += f; 
		z += f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	void ceVec4f::operator -= (const FLOAT f) 
	{ 
		x -= f; 
		y -= f; 
		z -= f; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator + (const FLOAT f) const 
	{ 
		return ceVec4f(x+f, y+f, z+f); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator - (const FLOAT f) const 
	{ 
		return ceVec4f(x-f, y-f, z-f); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	// Punktprodukt 
	//////////////////////////////////////////////////////////////////////////////// 
	FLOAT ceVec4f::operator *(const ceVec4f& v)const 
	{ 
		return (x*v.x + y*v.y + z*v.z); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceQuaternion ceVec4f::operator *(const ceQuaternion& q)const 
	{ 
		return ceQuaternion(q.w*x + q.z*y - q.y*z, 
							q.w*y + q.x*z - q.z*x, 
							q.w*z + q.y*x - q.x*y, 
							-(q.x*x + q.y*y + q.z*z) ); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	//! \remark Vektor * Matrix 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator *(const ceMatrix& m)const 
	{ 
		ceVec4f r; 
 
		const FLOAT fW = 1.0f / (m._ms._41 * x + m._ms._42 * y + m._ms._43 * z + m._ms._44); 
 
		r.x = ( m._ms._11 * x + m._ms._12 * y + m._ms._13 * z + m._ms._14 ) * fW; 
		r.y = ( m._ms._21 * x + m._ms._22 * y + m._ms._23 * z + m._ms._24 ) * fW; 
		r.z = ( m._ms._31 * x + m._ms._32 * y + m._ms._33 * z + m._ms._34 ) * fW; 
		r.w = 1.0f; 
 
		return r; 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	ceVec4f ceVec4f::operator +(const ceVec3f& v)const 
	{ 
		return ceVec4f(x+v.x, y+v.y, z+v.z); 
	} 
	//////////////////////////////////////////////////////////////////////////////// 
	bool ceVec4f::operator == (const ceVec4f &v) const 
	{ 
		return (x == v.x && y == v.y && z == v.z && w == v.w); 
	} 
} // Nampespace ZFXCE