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) Vector4* kmVec4Normalize(Vector4* pOut, const Vector4* pIn)
{ {
pIn->normalize(pOut); *pOut = pIn->getNormalize();
return pOut; return pOut;
} }

View File

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

View File

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