www.pudn.com > zfxcengine-0.1.0.zip > ceVec3f.cpp
// $Id: ceVec3f.cpp,v 1.12 2005/07/24 16:22:38 kimmi Exp $ #include#include "Math/ceMath.h" #include namespace ZFXCE { //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Set(const FLOAT X, const FLOAT Y, const FLOAT Z) { x = X; y = Y; z = Z; } //////////////////////////////////////////////////////////////////////////////// FLOAT ceVec3f::GetLength() const { return (FLOAT) sqrt(x*x+y*y+z*z); } //////////////////////////////////////////////////////////////////////////////// FLOAT ceVec3f::GetSqrLength() const { return (FLOAT) (x*x+y*y+z*z); } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Negate() { x = -x; y = -y; z = -z; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Normalize() { const FLOAT f = GetLength(); if (0.0f != f) { const FLOAT fVal = 1/f; x *= fVal; y *= fVal; z *= fVal; } } //////////////////////////////////////////////////////////////////////////////// FLOAT ceVec3f::AngleWith(ceVec3f& v) const { return (FLOAT) acos( ((*this) * v) / (this->GetLength()*v.GetLength()) ); } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Cross(const ceVec3f& v1, const ceVec3f& 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; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Difference(const ceVec3f& v1, const ceVec3f& v2) { x = v2.x - v1.x; y = v2.y - v1.y; z = v2.z - v1.z; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Add(const ceVec3f &v) { x += v.x; y += v.y; z += v.z; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::Dump() { std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator +=(const ceVec3f& v) { x += v.x; y += v.y; z += v.z; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator -=(const ceVec3f& v) { x -= v.x; y -= v.y; z -= v.z; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator *= (const FLOAT f) { x *= f; y *= f; z *= f; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator /=(const FLOAT f) { if (0.0f != f) { const FLOAT fVal = 1 / f; x *= fVal; y *= fVal; z *= fVal; } } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator +=(const FLOAT f) { x += f; y += f; z += f; } //////////////////////////////////////////////////////////////////////////////// void ceVec3f::operator -=(const FLOAT f) { x -= f; y -= f; z -= f; } //////////////////////////////////////////////////////////////////////////////// // Punktprodukt //////////////////////////////////////////////////////////////////////////////// FLOAT ceVec3f::operator * (const ceVec3f& v) const { return (x*v.x + y*v.y + z*v.z); } //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator * (const FLOAT f) const { return ceVec3f(x*f, y*f, z*f); } //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator +(const FLOAT f) const { return ceVec3f(x+f, y+f, z+f); } //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator - (const FLOAT f) const { return ceVec3f(x-f, y-f, z-f); } //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator /(FLOAT f)const { if (0.0f != f) return ceVec3f(x/f, y/f, z/f); return ceVec3f(x,y,z); } //////////////////////////////////////////////////////////////////////////////// //! \remark Wir nehmen einfach 1.0f als w //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator *(const ceMatrix& m)const { const FLOAT fW = 1.0f / (m._ms._41 * x + m._ms._42 * y + m._ms._43 * z + m._ms._44); FLOAT r[3]; r[0] = ( m._ms._11 * x + m._ms._12 * y + m._ms._13 * z + m._ms._14 ) * fW; r[1] = ( m._ms._21 * x + m._ms._22 * y + m._ms._23 * z + m._ms._24 ) * fW; r[2] = ( m._ms._31 * x + m._ms._32 * y + m._ms._33 * z + m._ms._34 ) * fW; return (ceVec3f(r[0], r[1], r[2])); } //////////////////////////////////////////////////////////////////////////////// ceVec3f ceVec3f::operator - (const ceVec3f& v) const { return ceVec3f(x-v.x, y-v.y, z-v.z); } //////////////////////////////////////////////////////////////////////////////// FLOAT ceVec3f::operator *(const ceVec4f& v)const { return (x*v.x + y*v.y + z*v.z); } //////////////////////////////////////////////////////////////////////////////// bool ceVec3f::operator ==(const ceVec3f &v) const { if (x != v.x ||y != v.y || z != v.z) return false; return true; } //////////////////////////////////////////////////////////////////////////////// } // Namespace ZFXCE