refactor normalize in Vector4

This commit is contained in:
Huabing.Xu 2014-04-30 19:59:06 +08:00
parent 3ccb6dcbed
commit 441279c7ab
3 changed files with 17 additions and 26 deletions

View File

@ -534,7 +534,7 @@ Vector4* kmVec4Lerp(Vector4* pOut, const Vector4* pV1, const Vector4* pV2, float
Vector4* kmVec4Normalize(Vector4* pOut, const Vector4* pIn)
{
pIn->normalize(pOut);
*pOut = pIn->getNormalize();
return pOut;
}

View File

@ -233,39 +233,30 @@ void Vector4::negate()
w = -w;
}
Vector4& Vector4::normalize()
void Vector4::normalize()
{
normalize(this);
return *this;
}
void Vector4::normalize(Vector4* dst) const
{
GP_ASSERT(dst);
if (dst != this)
{
dst->x = x;
dst->y = y;
dst->z = z;
dst->w = w;
}
float n = x * x + y * y + z * z + w * w;
// Already normalized.
if (n == 1.0f)
return;
n = sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
return;
n = 1.0f / n;
dst->x *= n;
dst->y *= n;
dst->z *= n;
dst->w *= n;
x *= n;
y *= n;
z *= n;
w *= n;
}
Vector4 Vector4::getNormalize() const
{
Vector4 v(*this);
v.normalize();
return v;
}
void Vector4::scale(float scalar)

View File

@ -273,7 +273,7 @@ public:
*
* @return This vector, after the normalization occurs.
*/
Vector4& normalize();
void normalize();
/**
* Normalizes this vector and stores the result in dst.
@ -284,7 +284,7 @@ public:
*
* @param dst The destination vector.
*/
void normalize(Vector4* dst) const;
Vector4 getNormalize() const;
/**
* Scales all elements of this vector by the specified value.